// JavaScript Document
var currentColour = null;

function toPoundsPence(n) {
	var s = "" + Math.round(n * 100) / 100
	var i = s.indexOf('.')
	if (i < 0) return s + ".00"
	var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
	if (i + 2 == s.length) t += "0"
	return t
}

function updateTotal() {
	//alert(basePrice);
	var qty2
	qty2 = document.getElementById("txtquantity").value;
	basePrice = basePrice.replace(",","");

	var thisBasePrice = parseFloat(basePrice);
	optionCost = parseFloat(optionCost);
	colourCost = parseFloat(colourCost);
	qty = qty2;
	qty = parseInt(qty);
	
	document.getElementById("totalPrice").innerHTML = "£" + toPoundsPence((thisBasePrice+optionCost+colourCost)*qty);
	
	/*var bitch
	bitch = ((parseFloat(basePrice)+parseFloat(colourCost)+parseFloat(optionCost))*qty);
	bitch = '£' + bitch;
	document.getElementById("totalPrice").innerHTML = "£" + ((parseFloat(basePrice)+parseFloat(colourCost)+parseFloat(optionCost))*qty);*/
}

function changeTotal(frmObject) {
	qty = frmObject[frmObject.selectedIndex].value;
	updateTotal();
}

function optionCostFunc(obj) {
	optionCost = obj[obj.selectedIndex].title;
	optionCost = optionCost.replace("£","");
	optionCost = optionCost.replace(" extra","");
	updateTotal();
}

function colourCostFunc(obj) {  
	colourCost = obj.title;
	colourCost = colourCost.replace("£","");
	colourCost = colourCost.replace(" extra","");
	updateTotal();
} 

function showTerms(location, strTitle){
	newWindow = window.open(location,'newWin','scrollbars=yes,width=630,height=400') 
	//newWindow.document.write("<HTML><HEAD><TITLE>" + strTitle + "</TITLE></HEAD><BODY bgcolor='#FFFFFF' text='#000000'><DIV ALIGN='center'><IMG SRC=" + imgLocation + "></DIV></BODY></HTML> ") 
	newWindow.document.close 
} 
function newPopupWindow(imgLocation, strTitle){
	newWindow = window.open('','newWin','scrollbars=yes,width=630,height=400') 
	newWindow.document.write("<HTML><HEAD><TITLE>" + strTitle + "</TITLE></HEAD><BODY bgcolor='#FFFFFF' text='#000000'><DIV ALIGN='center'><IMG SRC=" + imgLocation + "></DIV></BODY></HTML> ") 
	newWindow.document.close 
} 
function termsc() {
  if (document.form2.terms.checked) {
    //window.open('','checkout','resizable=no,status=yes,height=400,width=600,toolbar=no,scrollbars=yes,copyhistory=no').focus();
    //location='thankyou.asp';
  } else {
    alert('Please read and accept our terms & conditions');
  }
  return document.form2.terms.checked;
}

function showDelivery(whichLayer)
{
	if (document.getElementById) {
		var style = document.getElementById(whichLayer).style;
		style.display = style.display? "":"none";
	} else if (document.all) {
		var style = document.all[whichLayer].style;
		style.display = style.display? "":"none";
	} else if (document.layers) {
		var style = document.layers[whichLayer].style;
		style.display = style.display? "":"none";
	}
}



function getObject(getID) {
	return document.getElementById(getID)	
}
function setBorder(obj) {
	if (currentColour !== null) {
		clearBorder(getObject(currentColour))
	}
	var currentClass = obj.className;
	obj.className = currentClass + " setBorder";
	currentClass = null;
}

function clearBorder(obj) {
	if (currentColour != obj.id) {		
		var currentClass = obj.className.split(" ");
		obj.className = currentClass[0];
		currentClass = null;
	}
}

function sendToFriend(pid) {
	var pAlertTitle, pAlertText, pAlertType, pEndText;
	var pAlertButtons = new Array();
	var pAlertFunctions = new Array();
	
	pAlertTitle = "Email Item To A Friend";
	pAlertText = "You have chosen to send a product to a friend by email.<br />"
	pAlertText += "Please fill in the fields below and then click submit to make your friend aware of this great product from Kalusto<br />";
	pAlertType = "cancel";
	
	pAlertFunctions[0] = "getPage('emailAFriend.asp?pid="+pid+"')";
	pEndText = "";

	buildAlert(pAlertTitle,pAlertText,pAlertType,pAlertFunctions,pAlertButtons,pEndText);
}

function sendToFriendPOST(frm) {
	var e = frm;
	var x = "";
	var params = "go=1";
	var postForm = true;
	var currentID = "";
	var cleanedString = "";
	for (var i = 0; i < e.length; i++) {
		cleanedString = e[i].value;//.replace(/&/,"");
		params += "&"+e[i].name+"="+encodeURI(cleanedString);
		if (e[i].value == "" && e[i].name !== "comments") {
			currentID = e[i].id;
			currentID = currentID.replace(/_/g," ");
			x += "Please Enter " + currentID + "<br />";
			postForm = false;
		}
	}
	if (postForm) { //Ajax post
		document.getElementById("customAlertContent").innerHTML = "<div id='imageLoadingAlert' style='display:block'><img src='/images/loading.gif' border='0' alt='Sending Message' title='Sending Message' /><br />Sending Message</div>";
		resizeAlert(document.getElementById("customAlertBox"));
		
		url = "emailAFriend.asp";
		xmlHttp = GetXmlHttpObject()
		//xmlHttp.overrideMimeType('text/html');
		xmlHttp.onreadystatechange=postStateChanged
		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
      	xmlHttp.setRequestHeader("Connection", "close");
        xmlHttp.send(params);
		
		} else { 
			x += "<br />";
			showAlertMessage(x);
		}
	return false;
}

function postStateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		if (xmlHttp.status == 200) {
			var statusMsg = xmlHttp.responseText.split("RETURNMSG//");
			document.getElementById("customAlertContent").innerHTML = statusMsg[1];
			resizeAlert(document.getElementById("customAlertBox"));
			xmlHttp = null;
		} else {
			document.getElementById("customAlertContent").innerHTML = "An error occurred sending the message. Please try again";
		}
	}
}

function showAlertMessage(errMessage) {
	var e = getObject("errorMessageHolder");
	e.innerHTML = errMessage;
	resizeAlert(document.getElementById("customAlertBox"));
}