var req = createXMLHTTPObject();
var iPrice;
var iSuppliment;
var sValidCode;	
		
		function checkPromoCode(sCode, iStandardPrice, iCottageSuppliment, dStartDate, iNights)
		{
		
			if (sCode != "" && iStandardPrice != "" && dStartDate != "")
			{
				sValidCode = sCode;
				iPrice = iStandardPrice;	
				iSuppliment = iCottageSuppliment;
				req.open("POST", "ajax/getOfferPrice.asp?pcode=" + sCode + "&datStartDate=" + dStartDate + "&iNights=" + iNights);
				req.onreadystatechange = showPrice;
				
				
				req.send("");
				if (!req) return;
			}
			else
			{
				//document.getElementById("divPromoCode").style.display = "none";
				alert('No Promo Code was entered!')	;
			}
			
		}	
		
		function showPrice()
		{
			if(req.readyState==4)
			{
				req.close;
				
				if (req.responseText != "invalid")
				{
				
										//percentOff = req.responseText;
					result = req.responseText;
					var tmpSplit = result.split("|");
					var tmpPrice = iPrice - iSuppliment;	
					var iNewPrice; 
					discount = tmpSplit[0];
					type = tmpSplit[1];
					
					
					if (sValidCode.toUpperCase() == 'FREE09'){
						iNewPrice = (iPrice - 100);
					}else{
						if (type == "percent"){
							iNewPrice = (iPrice * ((100-discount)/100));
						}
						else{
							iNewPrice = (iPrice - discount);
						}
						
					}
					
					//var iNewPrice = (tmpPrice * ((100-percentOff)/100));
					//iNewPrice = (iNewPrice * 1) + (iSuppliment * 1); // multiplying by one stops the script from thinking its concatenating 2 strings
					
					//alert(tmpPrice + " sup:" + iSuppliment + "\n newprice" + iNewPrice );
					
					//now to update the webpage display appropriately
					document.getElementById('iPriceH').value = roundDown(iNewPrice);
					document.getElementById('priceHeader').innerHTML = "Offer Price*";
					
					document.getElementById('promoText').innerHTML = "The standard price for this cottage is &pound;" + iPrice;
					
					if (sValidCode.toUpperCase() == 'FREE09'){
					document.getElementById('promoText').innerHTML += ". Price includes a £100 promo reduction. ";
					}else{
						if (type == "percent"){
						document.getElementById('promoText').innerHTML += ". Price includes a " + discount + "% promo reduction. (excluding any suppliments)";
						}
						else{
						document.getElementById('promoText').innerHTML += ". Price includes a £" + discount + " promo reduction. (excluding any suppliments)";
						}
					}
					document.getElementById("promoText").style.display = "";
					document.getElementById("aspText").style.display = "none";
					updatePrice();
					
					if (sValidCode.toUpperCase() == 'FREE09'){
					document.getElementById("promoBox").innerHTML = "Promotional code: <b>" + sValidCode + "</b>, entitling you to a " + "£100 reduction (before any suppliments). <input type=hidden name=\"PromoCode\" value=\""+ sValidCode + "\"><input type=hidden name=booPromoCalculated value=\"true\">";
					}else{
						if (type == "percent"){
						document.getElementById("promoBox").innerHTML = "Promotional code: <b>" + sValidCode + "</b>, entitling you to a " + discount + "% reduction (excluding any suppliments). <input type=hidden name=\"PromoCode\" value=\""+ sValidCode + "\"><input type=hidden name=booPromoCalculated value=\"true\">";
						}
						else{
						document.getElementById("promoBox").innerHTML = "Promotional code: <b>" + sValidCode + "</b>, entitling you to a £" + discount + " reduction. <input type=hidden name=\"PromoCode\" value=\""+ sValidCode + "\"><input type=hidden name=booPromoCalculated value=\"true\">";
						}
					}
					document.getElementById("promoCell").style.display = "none";
					
				
				}
				else
				{
					alert("The code you supplied was invalid for your booking");
				}
			}
		}
		
function roundDown(decPrice) {
	var price = decPrice.toString();
	var split = price.split(".");
	
	if (price.indexOf(".") != -1 && split.length > 1){
		if (split[1].length > 2) {
			return split[0] + "." + split[1].substr(0,2);
		}
		else
		{
			return price;
		}
	}
	else{
		return price;
	}
	
}


function createXMLHTTPObject()
		{
			var xmlhttp;
			try {
				xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
			} catch (e) {
				try {
					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
				} catch (e) {
					xmlhttp=false;
				}
			}
			if (!xmlhttp) {
				try {
					xmlhttp = new XMLHttpRequest();
				} catch (e) {
					xmlhttp=false;
				}
			}
			return xmlhttp;
		}

		function resetPromoCodeBox(){
			document.getElementById('txtPromoCode').value = ''; 
			document.getElementById('txtPromoCode').readOnly = '';
		
			document.getElementById('txtPromoCode').style.width = '68px'; 
			document.getElementById('txtPromoCode').style.color = '#000000'; 
			//document.getElementById('txtPromoCode').style.fontWeight = '400'; 
			document.getElementById('btnPromo').style.display = 'inline';
			
		}
		
