	/* ********************************************************************************** /
	/ Yodel&trade; Prices JavaScript Calculation / Communication Driver					 /
	/ ==========================================================					 /
	/ This is one of two JavaScript documents which the prices page of the Yodel&trade; website	 /
	/ uses. This script controls the data operations in the form of validation and calcu- /
	/ lation of figures.													 /
	/ 																	 /
	/ 																	 /
	/ Developed by Kashi Samarweera for Yodel&trade; Australia, 2009.						 /
	/ © FulfilNET | Yodel&trade; Australia 2009										 /
	/ ************************************************************************************/

	var currencySymbol = "$";
	
	var formContents;
	
	var fillName, fillContact;
	
	var currentPriceBox;

	
	function getPkgDetails(priceBox) {
		watchBack(document.getElementById('contactForm'));
		watchBack(document.getElementById('nameForm'));

		currentPriceBox = priceBox;
		// Get the Price
		price = "";
		priceString = priceBox.innerHTML;
		priceString = priceString.toLowerCase();
		priceString = priceString.replace(' ', '');
		priceString = priceString.replace('&nbsp;', '');

		for (i = 0; i < priceString.length; i++) {
//			window.alert("Character: '" + priceString.substring(i, i + 1) + " | regEx: " + /^\d+$/.test(priceString.substring(i, i + 1)));	
			if (/^\d+$/.test(priceString.substring(i, i + 1))) {
				price = price + String(priceString.substring(i, i + 1));
			}
		}

		price = Number(price);

		
		// Get the Time Period for the Package
		columnNo = String(priceBox.id).substring(5);
		columnNo = Number(columnNo);
		
		columnNo = (columnNo) % 3;

		switch (columnNo) {
			case 0 : pkgTime = "3";
					 break;
			case 1 : pkgTime = "6";
					 break;
			case 2 : pkgTime = "12";
					 break;
			default : pkgTime = "~";
		}
		
		
		// Get the Row
		rowNo =  String(priceBox.id).substring(5);
		rowNo = Math.floor((Number(rowNo)) / 3);
	
			
		// Get the Package Colour
		pkgColour = String(priceBox.className).substring(0,String(priceBox.className).indexOf('Pkg'));
		
		// Get the cost per day
		dailySpend = "";
		dailySpendString = priceBox.parentNode.innerHTML;
		dailySpendString = dailySpendString.toLowerCase(dailySpendString);
		dailySpendString = dailySpendString.substring(dailySpendString.indexOf(currencySymbol) + currencySymbol.length, dailySpendString.indexOf('</div'));
		dailySpendString.replace(' ', '');

		for (i = 0; i < dailySpendString.length; i++) {
			//window.alert(dailySpendString.substring(i, i + 1));
			if (!isNaN(Number(dailySpendString.substring(i, i + 1)))) {
				dailySpend = dailySpend + String(dailySpendString.substring(i, i + 1));
			}
		}
		
		if (document.getElementById('pkgDetails') != undefined) {
			document.getElementById('pkgDetails').value = currencySymbol + dailySpend + "|" + pkgTime + "|" + priceBox.innerHTML;
			// Output the details
			//window.alert("Price Box " + priceBox.id + "\n=================\nTime Period: " + pkgTime + "\nDaily Spend: " + currencySymbol + dailySpend + "\n Package Price : " + currencySymbol + price);
			document.getElementById('spendDiv').innerHTML = "$" + dailySpend;
			document.getElementById('timeDiv').innerHTML = pkgTime;
			document.getElementById('pkgPrice').innerHTML = priceBox.innerHTML;
			
			document.getElementById('spendDiv').style.backgroundImage = 'url(../images/' + pkgColour + 'AdSpend.png)';
			document.getElementById('timeDiv').style.backgroundImage = 'url(../images/' + pkgColour + 'Duration.png)';
			document.getElementById('pkgPrice').style.backgroundImage = 'url(../images/' + pkgColour + 'PkgPrice.png)';
		}
		
		// Get Y Position
		var pkgSpecifics = document.getElementById('formDiv');
//		window.alert(pkgSpecifics.offsetHeight);
		formTop = rowNo * 78;
		contentHeight = Number(document.getElementById('contentMainPrices').offsetHeight);
		pkgSpecificsHeight = 228;
		if ((formTop + pkgSpecificsHeight + 62)  < contentHeight) {
			pkgSpecifics.style.top = String(formTop + 62) + "px";
		} else {
			//Work out the approriate height for correct display so that users can correlate the form to their chosen price
			pkgSpecifics.style.top = String(formTop - (pkgSpecificsHeight - 16)) + "px";
		}
		pkgSpecifics.style.display = 'block';
		startWatch = false;
		if (String(priceBox.className).indexOf('Zoom') < 0) {
			priceBox.className = String(priceBox.className) + "Zoom";
		}
		
		priceBoxNo = 0;
		while (document.getElementById("price" + priceBoxNo) != undefined) {
			currentPrice = document.getElementById("price" + priceBoxNo);
			if (String(currentPrice.className).indexOf('Zoom') > 0 && currentPrice != priceBox) {
				currentPrice.className = String(currentPrice.className).substring(0, String(currentPrice.className).indexOf('Zoom'));
			}
//			window.alert(String(currentPrice.className));
			if (String(currentPrice.className).indexOf('Static') < 0 && currentPrice != priceBox) {
				currentPrice.className = String(currentPrice.className) + "Static";
				changeOpacity(35, currentPrice.id);
			}

			priceBoxNo++;
		}
		selectedPkg = priceBox;
		if (document.getElementById('nameForm')!= undefined) document.getElementById('nameForm').focus();
	}

	function getSaving(priceBox) {
		if (startWatch == true) {
			// Get the Time Period for the Package
			rowNo = String(priceBox.id).substring(5);
			rowNo = Number(rowNo);
			
			rowNo = (rowNo) % 3;
			if (rowNo > 0) {
				price = "";
				priceString = priceBox.innerHTML;
				priceString = priceString.replace(' ', '');
		
				for (i = 0; i < priceString.length; i++) {
					if (/^\d+$/.test(priceString.substring(i, i + 1))) {
						price = price + String(priceString.substring(i, i + 1));
					}
				}
				
				price = Number(price);
				
				lastPrice = "";
				// Get the Time Period for last Package laterally
				lastPriceBox = Number(String(priceBox.id).substring(5)) - rowNo;
				lastPriceBox = document.getElementById("price" + lastPriceBox);
	
				lastPriceString = lastPriceBox.innerHTML;
				lastPriceString = lastPriceString.toLowerCase();
				lastPriceString = lastPriceString.replace(' ', '');
				lastPriceString = lastPriceString.replace('&nbsp;', '');
				
				for (i = 0; i < lastPriceString.length; i++) {
					if (/^\d+$/.test(lastPriceString.substring(i, i + 1))) {
						lastPrice = lastPrice + String(lastPriceString.substring(i, i + 1));
					}
				}
				lastPrice = parseInt(lastPrice);
				
				if (!isNaN(price) && !isNaN(lastPrice)) {
					saving = (lastPrice * (rowNo * 2)) - price;
				}
	//			document.getElementById('savingDiv').innerHTML = "Saving " + currencySymbol + saving;
				
				if (saving != undefined) {
				//document.title = "Last Price Box" + lastPriceBox.id + " Current Price Box: " + priceBox.id + " lastPriceString: " + lastPriceString + " price: " + price + " saving: " + saving;					
				return saving;

				} else {
					return 0;
				}
				//Clear on exit.
			}
			
			return 0;
		}
	}

	function closeForm() {
		document.getElementById('formDiv').style.display = 'none';
		
		priceBoxNo = 0;
		while (document.getElementById("price" + priceBoxNo) != undefined) {

			currentPrice = document.getElementById("price" + priceBoxNo);
			if (String(currentPrice.className).indexOf('Static') > 0) {
				currentPrice.className = String(currentPrice.className).substring(0, String(currentPrice.className).indexOf('Static'));
				changeOpacity(outAlpha, currentPrice.id);
			}
			if (String(currentPrice.className).indexOf('Zoom') > 0) {
				currentPrice.className = String(currentPrice.className).substring(0, String(currentPrice.className).indexOf('Zoom'));
			}			
			priceBoxNo++;

		}
		
		selectedPkg = null;
			
		startWatch = true;
	}
	
	function findPackageColour(pkg) {
		
		// Find the CSS Name
		if (pkg != undefined) {
			pkgClass = pkg.className;
			pkgClass = pkgClass.substring(0, pkgClass.indexOf('Pkg'));
			window.alert(pkgClass);
		}
	}
	
	
	/* ========================================== AJAX / PHP Communication ========================================== */
	
	
	function submitForm(formElement) {
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		
		if (keycode == 13) {
		   sendMail();
		}
	}
	
	function watchBack(formElement) {
		if (formElement != undefined) {
			if (formElement.value == "") {
				if (formElement.id == "nameForm") {
					formElement.style.backgroundImage = "url(../images/nameFormBack.gif)";
				}
				if (formElement.id == "contactForm") {
					formElement.style.backgroundImage = "url(../images/contactFormBack.gif)";
				}			
			} else {
				if (formElement.id == "nameForm") {
					formElement.style.backgroundImage = "url(../images/nameFormBackFocus.gif)";
				}
				if (formElement.id == "contactForm") {
					formElement.style.backgroundImage = "url(../images/contactFormBackFocus.gif)";
				}			
			}
/*		bgImage = formElement.style.backgroundImage;
		bgImage.substring(0, bgImage.length - 4);
		//window.alert (bgImage);*/
		}
	}
	
	
	function sendMail() {
		if (validateForm() == true) {
			var 	mailStatus;
			try {
				// This should work for most browsers
				mailStatus = new XMLHttpRequest();
			} catch (e) { 
			// Internet Explorer
				try {
					mailStatus = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					
					try {
						mailStatus = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {
						alert("Your browser does not support AJAX!");
						return false;
					}
				}
			}
			
			mailStatus.onreadystatechange = function () {
	
				if (mailStatus.readyState == 4) {
					// The request has completed.
					document.getElementById('formDiv').innerHTML = mailStatus.responseText;
				}
			}
			
			mailStatus.open('POST', '../php/pricesMailer.php', true);
		
			postData = "customerName=" + encodeURI( document.getElementById("nameForm").value ) + "&contact=" + encodeURI( document.getElementById("contactForm").value ) + "&pkgDetails=" + encodeURI( document.getElementById("pkgDetails").value ) + "&submitted=" + encodeURI( "true" );
			
			mailStatus.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			mailStatus.setRequestHeader("Content-length", postData.length);
			mailStatus.setRequestHeader("Connection", "close");
			
			mailStatus.send(postData);
			pageTracker._trackPageview('/priceenquiry.php'); 
			
			
			
		}
	}
	
	function validateForm() {
		(document.getElementById('nameForm') != undefined) ? fillName = document.getElementById('nameForm').value : null;
		(document.getElementById('contactForm') != undefined) ? fillContact = document.getElementById('contactForm').value : null;

		nameForm = document.getElementById('nameForm').value;
		contactForm = document.getElementById('contactForm').value;
		
		nameCheck = true;
		contactCheck = true;
		
		emailCheck = true;
		phoneCheck = true;
		
		errorMsg = '<div id="pkgPrompt"><div id="closeBtn" onclick="closeForm();" onmouseover="this.style.backgroundImage=\'url(../images/closeBtnOver.png)\';" onmouseout="this.style.backgroundImage=\'url(../images/closeBtn.png)\';" onmousedown="this.style.backgroundImage=\'url(../images/closeBtnDown.png)\';" onmouseup="this.style.backgroundImage=\'url(../images/closeBtnOver.png)\';"></div></div>';
		errorMsg += "<h3>Missing Details</h3><p class=\"thankyouText\">The follwing information was insufficient:</p><ul>";
		if (nameForm.length < 2) nameCheck = false;
		
		// Check for an email address
		
		if (contactForm.length < 5) {
			emailCheck = false;
			phoneCheck = false;
		}

		if (contactForm.indexOf('@') < 1) emailCheck = false;

		if (contactForm.indexOf('.') < 1) emailCheck = false;
		
		phoneCheck = /\d{4,}/.test(contactForm);
		
		if (emailCheck == false && phoneCheck == false) contactCheck = false;
				
		if (nameCheck == false) {
			errorMsg += "<li>Name.</li>";
		}

		if (contactCheck == false) {
			errorMsg += "<li>Contact Info - phone number (with area code) or email address.</li>";
		}
		
		errorMsg += "</ul>";
		if (contactCheck == true && nameCheck == true) {
			return true;
		} else {
			changeForm(errorMsg);
			return false;
		}

	}
	
	function changeForm(newContent) {
		(document.getElementById('nameForm') != undefined) ? fillName = document.getElementById('nameForm').value : null;
		(document.getElementById('contactForm') != undefined) ? fillContact = document.getElementById('contactForm').value : null;
		
		document.getElementById('formDiv').innerHTML = newContent;
		document.getElementById('formDiv').innerHTML += '<p class="thankyouText">To enter in your details again, please <a onclick="resetForm();" style="cursor: hand">click here</a></p>';
	}
	
	function resetForm() {
		document.getElementById('formDiv').innerHTML = formContents;
		getPkgDetails(currentPriceBox);
		if (fillName != undefined) (document.getElementById('nameForm') != undefined) ? document.getElementById('nameForm').value = fillName : null;
		if (fillContact != undefined) (document.getElementById('contactForm') != undefined) ? document.getElementById('contactForm').value = fillContact : null;
		watchBack(document.getElementById('contactForm'));
		watchBack(document.getElementById('nameForm'));
		
	}