var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null;

var scriptName = /delivery.html/;

/*--------------- Added for restricted words ----------------------*/
var restrictedWords = new Array();

var phaseNo = 0;
var functionsToCall = new Array();
var globalPhaseCount = 0;
var globalValfield;

function functionDispatcher() {
	if (phaseNo <= globalPhaseCount)
		eval(functionsToCall[phaseNo]);
	else
		if (document.getElementById('progress')) document.getElementById('progress').style.display = 'none';
}

/*
function initialPhases(phaseCount, script) {
	functionsToCall = new Array();
	globalPhaseCount = phaseCount;
	phaseNo = 0;
	functionsToCall[0] = 'loadRibbonColors(\'ribbonColor\', ' + script + ')';
	functionsToCall[1] = 'loadUrl2(\'http://www.sashesnribbons.co.uk/control/rpc.php?siteid=4&mode=8&t=' + ((new Date()).valueOf()) + '\', 5);'; 
	functionDispatcher();	
}
*/

function restrictedWordsLoaded() {
  if (req.readyState == 4 && req.status == 200 ) {
  	restrictedWords = eval(req.responseText);
  	
/*
		if (document.getElementById('uplLink') != null) {
			var randVal = Math.round(Math.random() * 10000000 + 1);
			document.getElementById('uplLink').href = 'http://www.cardsnbadges.co.uk/mailsending/sendimage.php?id=' + randVal;
		}
*/	
		phaseNo++;
		functionDispatcher();	
  }
}

function checkForRestrictedWords(elem) {
	if (isNotEmpty(elem)) {
		var valueString = elem.value.toUpperCase();
		for (var i = 0; i < restrictedWords.length; i++) {
			if (valueString.indexOf(restrictedWords[i].toUpperCase()) >= 0) {
				alert('We believe that the word/phrase \'' + restrictedWords[i] +'\' that you have requested is subject to TradeMark or Copyright restrictions.\nWe are therefore not able to reproduce this text on your item.');
				setfocus(elem);
				break;
			}
		}
	}
}

function setFocusDelayed() {
	globalValfield.focus();
}

function setfocus(valfield) {
	globalValfield = valfield;
	setTimeout('setFocusDelayed()', 100);
}

function isNotEmpty(elem) {
	if (elem == null) return false;
  var str = elem.value;
  var re = /.+/;
  
  return str.match(re);
}

function loadUrl2(url, mode) {
  if(window.XMLHttpRequest) {
    try { req = new XMLHttpRequest();
    } catch(e) { req = false; }
  } else if(window.ActiveXObject) {
    try { req = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
    try { req = new ActiveXObject('Microsoft.XMLHTTP');
    } catch(e) { req = false; }
  } }
  if(req) {
  	switch (mode) {
 			case 1: req.onreadystatechange = restrictedWordsLoaded; break;
 		}
    req.open('GET', url, true);
    req.send('');
  }
}

/*-----------------------------------------------------------------*/

/* 
 * Loads requested url 
 * url - url to load
 * mode - # of function to use for processing loaded data
 */
function loadUrl(url, mode) {
	if (typeof mode == "undefined") {
    mode = 1;
  }	
  if(window.XMLHttpRequest) {
    try { req = new XMLHttpRequest();
    } catch(e) { req = false; }
  } else if(window.ActiveXObject) {
    try { req = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
    try { req = new ActiveXObject('Microsoft.XMLHTTP');
    } catch(e) { req = false; }
  } }
  if(req) {
  	switch (mode) {
 			case 1: req.onreadystatechange = calendarLoaded; break;
 			case 2: req.onreadystatechange = dataLoaded; break;
 		}
    req.open('GET', url, true);
    req.send('');
  }
}

function getData(scriptname, mode) {
	var url = window.location.toString();
	if (url.substr(url.length - 1, 1) == '/')
		url += scriptname + 't=' + ((new Date()).valueOf());
	else
		url = url.replace(scriptName, scriptname + 't=' + ((new Date()).valueOf()));
	loadUrl(url, mode);
}

function getSizeName(size) {
	for (var i = 0; i < sizes.length; i++)
		if (sizes[i] == size) return sizeNames[i];
}

function checkRequiredFields(elemArray, formName) {
	var emptyElem = false;
	var alertMessage = "The following required fields not filled/chosen:\n";
	for (var i = 0; i < elemArray.length; i++) {
		if (!isNotEmpty(document.getElementById(elemArray[i].elemId))) {
			emptyElem = true;
			alertMessage += ('* ' + elemArray[i].alertMessage + "\n");
		}
	}
	
	if (emptyElem) {
		alert(alertMessage);
		return false;
	} else {
		document.forms[formName].submit();
		return true;
	}
}

function checkDate() {
	lastDate = document.getElementById('chosenDay').innerHTML;
	document.getElementById('progress').style.display = 'block';
	getData('datevalidation.php?', 1);
	return true;
}

function checkDate2(elem) {
	lastDate = document.getElementById('chosenDay').innerHTML;
	document.getElementById('progress').style.display = 'block';
	getData('datevalidation.php?datetime=' + elem.value + '&', 1);
	return true;
}

function calendarLoaded() {
  if (req.readyState == 4 && req.status == 200 ) {
    document.getElementById('resSpan').innerHTML = req.responseText;

/*
		var delivDateEnds = req.responseText.search('#%#');
		var delivDateStarts = req.responseText.search('delivDate_') + 11;
		var delivDate = req.responseText.substr(delivDateStarts, delivDateEnds - delivDateStarts);

		var optEnds = req.responseText.search('%#%');
		var optStarts = req.responseText.search('availoption_') + 13;
		var currOption = req.responseText.substr(optStarts, optEnds - optStarts);

		var optValEnds = req.responseText.search('#@#');
		var optValStarts = req.responseText.search('optvalue_') + 10;
		var currOptionValue = req.responseText.substr(optValStarts, optValEnds - optValStarts);

		var unOptValEnds = req.responseText.search('@#@');
		var unOptValStarts = req.responseText.search('unavail_') + 9;
		var unOptionValue = req.responseText.substr(unOptValStarts, unOptValEnds - unOptValStarts);

		var saturday = req.responseText.substr(req.responseText.search('saturday_')+10, 4);

   	document.getElementById('chosenDay').innerHTML = delivDate;
		document.getElementById('itemname2').value = delivDate;
   	document.getElementById('chosenOption').innerHTML = currOption;
		document.getElementById('itemname3').value = currOptionValue;

		
		
		document.getElementById('optionsWarning').innerHTML = '<font color=navy face=arial size=2><strong>Important:</strong></font><BR>' + 
		'<font face=arial size=2>For the delivery date chosen,<BR>' + 
			' &nbsp;&nbsp;&nbsp;&nbsp;- Delivery option(s) is/are: <strong>' + currOption +
			'</strong> <BR>' + 
			'&nbsp;&nbsp;&nbsp;&nbsp;- The option(s) <strong>' + unOptionValue + '</strong> is/are not available.<BR>' + 
			'Any unavailable options could become available if a later date is selected.<br><br>' + 
			'<strong><font color=navy>Code:</font></strong><br><strong>FC</strong> = First Class UK Post,<br><strong>C</strong> = Next Day Courier to UK mainland,<br><strong>SD</strong> = Royal Mail Special Delivery to the UK (Mon-Fri)<br>' + 
			'<br><i><strong><font color=navy>Please note:</font></strong></i><BR> ' + 
			'The Delivery date is not guaranteed if you choose the First Class option - although of course we will try our best<BR>' +
			'Guaranteed Deliveries only apply to the UK';


		if (saturday == 'true') {
			document.getElementById('saturdayWarning').style.display = 'block';
			//document.getElementById('saturdaySurcharge').name = 'X1include';
		} else {
			document.getElementById('saturdayWarning').style.display = 'none';
			//document.getElementById('saturdaySurcharge').name = 'X1includeNO';
		}
*/
		document.getElementById('progress').style.display = 'none';

		getDataSpecial('holiday/fetchholiday.php?siteid=4&simple=1&', /delivery.html/, 2);
	}
}

function chooseDay(delivDay, saturday, currOption, currOptionValue, showOW) {

	document.getElementById('chosenDay').innerHTML = delivDay;
	document.getElementById('itemname2').value = delivDay;
 	document.getElementById('chosenOption').innerHTML = currOption;
	document.getElementById('itemname3').value = currOptionValue;

	if (showOW) {
		var unOptionValue = '';
		if (currOption.indexOf('C ') == -1) unOptionValue += ' C';
		if (currOption.indexOf('FC') == -1) unOptionValue += ' FC';
		if (currOption.indexOf('SD') == -1) unOptionValue += ' SD';

		document.getElementById('optionsWarning').innerHTML = '<font color=navy face=arial size=2><strong>Important:</strong></font><BR>' + 
		'<font face=arial size=2>For the delivery date chosen,<BR>' + 
			' &nbsp;&nbsp;&nbsp;&nbsp;- Delivery option(s) is/are: <strong>' + currOption +
			'</strong> <BR>' + 
			((unOptionValue != '')?'&nbsp;&nbsp;&nbsp;&nbsp;- The option(s) <strong>' + unOptionValue + '</strong> is/are not available.<BR>' + 
			'Any unavailable options could become available if another date is selected.<br><br>':'') + 
			'<strong><font color=navy>Code:</font></strong><br><strong>FC</strong> = First Class UK Post,<br><strong>C</strong> = Next Day Courier to UK mainland,<br><strong>SD</strong> = Royal Mail Special Delivery to the UK (Mon-Fri)<br>' + 
			'<br><i><strong><font color=navy>Please note:</font></strong></i><BR> ' + 
			'The Delivery date is not guaranteed if you choose the First Class option - although of course we will try our best<BR>' +
			'Guaranteed Deliveries only apply to the UK';

		document.getElementById('optionsWarning').style.display = 'block';
	} else
		document.getElementById('optionsWarning').style.display = 'none';

	if (saturday) {
		document.getElementById('saturdayWarning').style.display = 'block';
		//document.getElementById('saturdaySurcharge').name = 'X1include';
	} else {
		document.getElementById('saturdayWarning').style.display = 'none';
		//document.getElementById('saturdaySurcharge').name = 'X1includeNO';
	}
	
}

function setLink() {
	var randVal = Math.round(Math.random() * 10000000 + 1);
	document.getElementById('uplLink').href = 'http://www.cardsnbadges.co.uk/mailsending/sendimage.php?id=' + randVal;
}

function checkFields() {
	if ((document.getElementById('itemname2').value == '') || (document.getElementById('itemname3').value == '')) {
		alert('Please specify delivery date!');
	} else {
		document.forms['delivForm'].submit();
	}
}

