g_bs = false;
g_ts = false;
g_trs = false;
g_emailfilter  = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/;
g_axis = /^\d{1,3}(\.\d?)?$/;
g_oOrigV = "";
g_oOrigT = "";
g_otna = new Array();

function colourselected(page, productid) {
	// find out which colour was selected
	index = 0;
	selected = 0;
	var aControls = document.getElementsByTagName("input");
	for (var i = 0; i < aControls.length; i++) {
		var oControl = aControls[i];
		if (oControl.type == "radio") {
			if (oControl.checked) {
				// make sure we are looking at the right frame
				var id = oControl.id;
				if (page == "cp")
					var frame = id.substr(11,(id.lastIndexOf("_")-11));
				else
					var frame = id.substr(10,(id.lastIndexOf("_")-10));				
				if (frame == productid) {
					selected = oControl.value;
					break;
				}
			}
		}
	}
	
	/*do
	{
		elem = page + "_rdoFrame" + productid + "_" + index;
		if (document.getElementById(elem) == null) {
			break;
		}else{
			if (document.getElementById(elem).checked)
				selected = document.getElementById(elem).value;
			index += 1;
		}
	}
	while(!selected);*/
	if (selected) {
		// swap the main thumbnail
		for (i = 0; i < g_pa.length; i++) {
			if (g_pa[i][0] == selected) {
				document.getElementById(page + "_mainthumb" + productid).src = "images/" + g_pa[i][1];
				document.getElementById(page + "_lblFrameName" + productid).innerHTML = g_pa[i][2] + " : ";
				break;
			}
		}
		// reset the choose frame link
		if (page == "cp")
			document.getElementById("cp_hypChoose" + productid).href = "product.aspx?p=" + productid + "&v=" + selected + "&c=" + g_c;
		// set show larger image product id
		
	}
}

message="";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if (document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")

if (typeof document.cookie == 'string') {
	if (document.cookie.length==0) {
		document.cookie = 'pstestcookie';
		if (document.cookie != 'pstestcookie')
			alert('We notice that your web browser has disabled the use of cookies.\n\nTo purchase from Perfect Specs, cookies must be activated on your web browser.');
		document.cookie = '';
	}else{
		// enabled;
	}
}else{
	// enabled;
}

function coating() {
	pid = document.getElementById("drpCoating").options[document.getElementById("drpCoating").selectedIndex].value;
	newprice = 0;
	oldprice = 0;
	for (i = 0; i < g_cta.length; i++) {
		if (g_cta[i][2] == 1) {
			g_cta[i][2] = 0;
			oldprice = g_cta[i][1];
		}
		if (g_cta[i][0] == pid) {
			newprice = g_cta[i][1];
			g_cta[i][2] = 1;	
		}
	}
	g_rt -= oldprice;
	g_rt += newprice;
	setprice();	
}

function cordchain() {
	pid = document.getElementById("drpCord").options[document.getElementById("drpCord").selectedIndex].value;
	newprice = 0;
	oldprice = 0;
	for (i = 0; i < g_cca.length; i++) {
		if (g_cca[i][2] == 1) {
			g_cca[i][2] = 0;
			oldprice = g_cca[i][1];
		}
		if (g_cca[i][0] == pid) {
			newprice = g_cca[i][1];
			g_cca[i][2] = 1;	
		}
	}
	g_rt -= oldprice;
	g_rt += newprice;
	setprice();	
} //cordchain()

function changeframe(Cart) {
	document.getElementById("c").value = Cart;
	document.getElementById("a").value = "c";
	document.forms[0].submit();
}//changeframe

function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}

function glassescase() {
	pid = document.getElementById("drpCase").options[document.getElementById("drpCase").selectedIndex].value;
	newprice = 0;
	oldprice = 0;
	for (i = 0; i < g_csa.length; i++) {
		if (g_csa[i][2] == 1) {
			g_csa[i][2] = 0;
			oldprice = g_csa[i][1];
		}
		if (g_csa[i][0] == pid) {
			newprice = g_csa[i][1];
			g_csa[i][2] = 1;	
		}
	}
	g_rt -= oldprice;
	g_rt += newprice;
	setprice();	
} //glassescase

function init() {
	setprice();
	if (g_blt) lenstype();
	if (g_btrl) transitionallenses();
	if (g_bpl) polarisedlenses();
	if (g_bc) coating();
	if (g_btl) thinlenses();
	if (g_bt) tint();
	if (g_bg) glassescase();
	if (g_bcc) cordchain();
	if (g_blc) lenscleaner();
}

function initsummary() {
	shipping();
	settotal();
	if (document.getElementById("a").value == "s")
	{
		document.getElementById("btnPurchase").disabled = true;
		document.getElementById("btnReset").disabled = true;
		document.forms[1].submit();
	}
}

function lenscleaner() {
	if (document.getElementById("chkLensCleaner").checked){
		g_rt += g_lcs;
	}else{
		g_rt -= g_lcs;
	}
	setprice();
}

function lenstype() {
	pid = document.getElementById("drpLensType").options[document.getElementById("drpLensType").selectedIndex].value;
	newprice = 0;
	oldprice = 0;
	for (i = 0; i < g_lta.length; i++) {
		if (g_lta[i][2] == 1) {
			g_lta[i][2] = 0;
			oldprice = g_lta[i][1];
			if (g_lta[i][0] == 17)
				setbifocalpricing(false);
		}
		if (g_lta[i][0] == pid) {
			newprice = g_lta[i][1];
			g_lta[i][2] = 1;	
			if (g_lta[i][0] == 17)
				setbifocalpricing(true);			
		}
	}
	g_rt -= oldprice;
	g_rt += newprice;
	setprice();	
	//
	if ((pid == 17) || (pid == 19))
		document.getElementById("txtPD").value = 60;
	else
		if (document.getElementById("txtPD").value == 60)
			document.getElementById("txtPD").value = 63;
}

function addition(elem) {
	if (elem.id == "drpRightAddition"){
		othadd = document.getElementById("drpLeftAddition");
	}else{
		othadd = document.getElementById("drpRightAddition");
	}
	othadd.options[elem.selectedIndex].selected = true;
	othadd.disabled = true;
}//addition

function sphere(elem) {
	sph = elem.options[elem.selectedIndex].value;
	if (sph >= -10.00 && sph <= -6.00) {
		for (k=0;k<document.getElementById("drpThinLenses").options.length;k++) {
			if (document.getElementById("drpThinLenses").options[k].value == 11) 
				document.getElementById("drpThinLenses").selectedIndex = k;
		}
		//document.getElementById("drpThinLenses").selectedIndex = 1;
		thinlenses();
	}else{
		if (elem.id == "drpRightSphere"){
			othsph = document.getElementById("drpLeftSphere").options[document.getElementById("drpLeftSphere").selectedIndex].value;			
		}else{
			othsph = document.getElementById("drpRightSphere").options[document.getElementById("drpRightSphere").selectedIndex].value;			
		}
		if (othsph >= -10.00 && othsph <= -6.00) {
			// do nada
		}else{
			//document.getElementById("drpThinLenses").selectedIndex = 0;
			//thinlenses();		
		}
	}
}//sphere

function setbifocalpricing(bBiFocalSelected) {
	// thin lense
	bUpdatePrice = false;	
	// determine price first	
	for (j = 0; j < g_tna.length; j++) {
		if (g_tna[j][0] == 11) {	// is this 1.60 thin lense?
			oprice = g_tna[j][1];		// get the current price
			if (g_tna[j][1] <= 30){	// new price will be 85
				nprice = 85;
			}else{
				if (g_bDiscount160){		// discount by catalogue applicable?
					nprice = 15;
				}else{
					if (g_bFreeThinLens) {
						nprice = 0;
					}else{
						nprice = 30;			
					}
				}
			}
			g_tna[j][1] = nprice;			
			if (g_tna[j][2] == 1) 	//product selected
				bUpdatePrice = true;
			// update dropdownlist text to show new price of thin lense
			for (k=0;k<document.getElementById("drpThinLenses").options.length;k++) {
				if (document.getElementById("drpThinLenses").options[k].value == 11) {
					// Free Thin Lens promotion has 1.60 offered free, unless Bifocal
					if ((oprice == 0) && (g_bFreeThinLens)) {
						ntext = g_CurrencySymbol + "85.00";
						document.getElementById("drpThinLenses").options[k].text = document.getElementById("drpThinLenses").options[k].text.replace("FREE",ntext);
					}else{
						if ((nprice == 0) && (g_bFreeThinLens)) {
							otext = g_CurrencySymbol + "85.00";
							document.getElementById("drpThinLenses").options[k].text = document.getElementById("drpThinLenses").options[k].text.replace(otext,"FREE");
						}else{
							document.getElementById("drpThinLenses").options[k].text = document.getElementById("drpThinLenses").options[k].text.replace(oprice,nprice);
						}
					}
				}
			}			
		}else{ // thin lense not 1.60
			if (g_tna[j][2] == 1) {	//product selected
				oprice = g_tna[j][1];
				nprice = 0;
				bUpdatePrice = true;
				g_tna[j][2] = 0; // unselect thin lenses as cannot be sold with bifocal
			}
		}
	}

	if (bBiFocalSelected) {
		g_otna = new Array();
		pos = 0;
		// need to remove other thin lenses
		
		// first check if anti-reflection has been offered free
		tlpid = document.getElementById("drpThinLenses").options[document.getElementById("drpThinLenses").selectedIndex].value;
		if (((tlpid == 12) || (tlpid == 531)) && (document.getElementById("drpCoating").selectedIndex == 2)) {
			// reset coating to none
			document.getElementById("drpCoating").selectedIndex = 0; 
			// correct display price of anti-reflective
			for (k=0;k<document.getElementById("drpCoating").options.length;k++) {
				if (document.getElementById("drpCoating").options[k].value == 13) {
					rtext = g_CurrencySymbol + "15.00";
					document.getElementById("drpCoating").options[k].text = document.getElementById("drpCoating").options[k].text.replace("FREE",rtext);
				}
			}			
			// revert pricing in coating array and unselect current price
			g_cta[1][1] = 15;
			g_cta[1][2] = 0;
			if (g_bFreeThinLens) {
				g_rt -= 15;
				setprice();
			}
		}

		for (k=(document.getElementById("drpThinLenses").options.length-1);k>0;k--) {
			if (document.getElementById("drpThinLenses").options[k].value != 11) {
				pos += 1;
				g_otna[pos] = new Array(document.getElementById("drpThinLenses").options[k].value,document.getElementById("drpThinLenses").options[k].text);
				//document.getElementById("drpThinLenses").options.remove(k);	
				document.getElementById("drpThinLenses").remove(k);		
			}
		}
	}else{
		oTarget = document.getElementById("drpThinLenses");
		for (j = (g_otna.length-1); j > 0 ; j--) {
			oNewOption = document.createElement("OPTION");
			oTarget.options.add(oNewOption);
			oNewOption.value = g_otna[j][0];
			oNewOption.innerText = g_otna[j][1];		
		}
	}
	
	if (bUpdatePrice) {
		g_rt -= oprice;
		g_rt += nprice;
		setprice();
	}

	// photochromic lense
	bUpdatePrice = false;	

	// determine price first	
	for (j = 0; j < g_tla.length; j++) {
		oprice = g_tla[j][1];
		if (g_tla[j][1] == 40)
			nprice = 70;
		else
			nprice = 40;
		g_tla[j][1] = nprice;
		if (g_tla[j][2] == 1) 	//product selected
			bUpdatePrice = true;
	}

	// update dropdownlist
	for (k=0;k<document.getElementById("drpTransitionalLenses").options.length;k++) 
		document.getElementById("drpTransitionalLenses").options[k].text = document.getElementById("drpTransitionalLenses").options[k].text.replace(oprice,nprice);
	
	if (bUpdatePrice) {
		g_rt -= oprice;
		g_rt += nprice;
		setprice();
	}

	// polarised lense
	bUpdatePrice = false;	

	// determine price first	
	for (j = 0; j < g_pla.length; j++) {
		oprice = g_pla[j][1];
		if (g_pla[j][1] == 40)
			nprice = 70;
		else
			nprice = 40;
		g_pla[j][1] = nprice;
		if (g_pla[j][2] == 1) 	//product selected
			bUpdatePrice = true;
	}

	// update dropdownlist
	for (k=0;k<document.getElementById("drpPolarisedLenses").options.length;k++)
		document.getElementById("drpPolarisedLenses").options[k].text = document.getElementById("drpPolarisedLenses").options[k].text.replace(oprice,nprice);
	
	if (bUpdatePrice) {
		g_rt -= oprice;
		g_rt += nprice;
		setprice();
	}
	
}//setbifocalpricing


function polarisedlenses() {
	pid = document.getElementById("drpPolarisedLenses").options[document.getElementById("drpPolarisedLenses").selectedIndex].value;
	newprice = 0;
	oldprice = 0;
	for (i = 0; i < g_pla.length; i++) {
		if (g_pla[i][2] == 1) {
			g_pla[i][2] = 0;
			oldprice = g_pla[i][1];
		}
		if (g_pla[i][0] == pid) {
			newprice = g_pla[i][1];
			g_pla[i][2] = 1;	
		}
	}
	g_rt -= oldprice;
	g_rt += newprice;
	setprice();	
	if (!g_bPS){  // if not in prescription sunglasses category
		if (pid > 0){
			document.getElementById("drpTransitionalLenses").disabled = true;
			document.getElementById("drpTransitionalLenses").selectedIndex = 0;		
			transitionallenses();
			document.getElementById("drpTint").disabled = true;
			document.getElementById("drpTint").selectedIndex = 0;
			tint();	
		}else{
			document.getElementById("drpTint").disabled = false;
			document.getElementById("drpTransitionalLenses").disabled = false;		
		}
	}
}

function purchase() {
	//alert("Unfortunately we are unable to take your Order right now due to technical difficulties.\n\nPlease try again at a later time. We sincerely apologise for the inconvenience.");
	//return false;
	a = document.getElementById("a");
	if ((a.value == "") || (a.value == "p")) {
		if (validatecustomisation()) {
			a.value = "p";
			document.forms[0].submit();
		}
	}else{
		alert("You cannot Trial and Purchase frames on one Order");
	}
} // purchase()

function lookUp() {

	Element.show('vouchermessages');
	var nodelist = $('vouchermessages').getElementsByTagName('p');
	var nodes = $A(nodelist);
	nodes.each(function(node){Element.hide(node);});					
	// reset running total
	g_rt += g_dt;
	settotal();	
	// reset discount fields
	g_dp = g_dvi = g_dci = g_dov = g_dt = 0;
	g_dm  = "";				

	if ($F('txtVoucher') != "") {
		
		Event.stopObserving('txtVoucher', 'change', lookUp, false);			
		
		Element.show('checking');

		var url = "services/Promotion.asmx/LookupByPromotionCode";
		var request = "PromotionCode="+$F('txtVoucher');
		var options = { method : 'post',
									postBody : request,																		
									asynchronous : true,
									onSuccess: lookUpSuccess,
									onFailure: lookUpFailure };
		var myAjax = new Ajax.Request(url, options);
	}
}//lookUp
function qtyupdate(oQty, Cart) {
	// add discount back to running total as new discount will be recalculated below
	g_rt += g_dt;
	settotal();	
				
	qty = oQty.selectedIndex + 1;
	oldprice = g_ca[Cart][1];
	newprice = qty * g_ca[Cart][0];
	g_ca[Cart][1] = newprice;
	carttotal = "sum_lblTotal" + Cart;
	document.getElementById(carttotal).innerHTML = "Total : &pound;" + formatAsMoney(newprice);
	g_rt -= oldprice;
	g_rt += newprice;
	settotal();	
	if ($F('txtVoucher') != "")
		discount();	
	shipping();
}//qtyupdate
function lookUpSuccess(r) {
	Event.observe('txtVoucher', 'change', lookUp, false);
	Element.hide('checking');
	//alert(r.responseXML.text);
	var oJSON = JSON.eval(r.responseXML.text);
	switch (oJSON.status) {
		case "1" :
		case "2" :
		case "3" :
		//alert('starts ' + oJSON.startDate);
		//alert('ends ' + oJSON.endDate);
			alert(oJSON.message);
			$('txtVoucher').focus();
			$('txtVoucher').select();
			break;
		default :
			g_dp  = oJSON.discountPercentage;
			g_dvi = oJSON.vendorId;
			g_dci = oJSON.catalogueId;
			g_dov = oJSON.orderValue;		
			g_dm  = oJSON.message;
			discount();
			break;
	}
} //lookUpSuccess
function lookUpFailure(r) {
	Event.observe('txtVoucher', 'change', lookUp, false);			
	Element.hide('vouchermessages');
	alert("An error has occurred. We are unable to process your voucher, please contact PerfectSpecs");
}	//lookUpFailure 	
			
function discount() {
	g_dt  = 0;							
	shipAmt = calcShipping();
	orderAmt = g_rt - shipAmt;
	// if discount order value specified and promotion is not limited to a vendor
	if (g_dov > 0 && g_dvi == 0) {
		if (orderAmt >= g_dov) {
			g_dt = (g_dp / 100) * orderAmt;
		}
	}else{
		// loop through each cart, calc disc on vendor (and) catalogue match
		for (i = 0; i < g_ca.length; i++) {
			//g_ca[i][0] = unit price
			//g_ca[i][2] = vendor
			//g_ca[i][3] = catalogue
			if (g_dvi == g_ca[i][2]){					// vendor match
				if (g_dci > 0){									// must do catalogue match
					if (g_dci == g_ca[i][3]) {		// catalogue match
						drpQty = "sum_drpQuantity" + (i);
						qty = document.getElementById(drpQty).selectedIndex + 1;
						lineAmt = qty * g_ca[i][0];										
						g_dt += (g_dp / 100) * lineAmt;
					}
				}else{													 // no need to match catalogue
					drpQty = "sum_drpQuantity" + (i);
					qty = document.getElementById(drpQty).selectedIndex + 1;
					lineAmt = qty * g_ca[i][0];										
					g_dt += (g_dp / 100) * lineAmt;									
				}
			}
		}							
	}
	g_rt -= g_dt;
	settotal();	
	Element.update('voucherresult',g_dm + ' ' + g_dp + '% off, you save &pound;' + formatAsMoney(g_dt));
	Element.show('voucherresult');					
	document.getElementById('da').value = g_dt;
	document.getElementById('dp').value = g_dp;
}//discount

function removeframe(Cart) {
	if (confirm('Please confirm your request to remove this Frame from your order')) {
		document.getElementById("c").value = Cart;
		document.getElementById("a").value = "r";
		document.forms[0].submit();
	}
}//removeframe

function setdeliveryaddress() {
	if (document.forms[0].rdoAddress[0].checked) {
		document.getElementById("drpDeliveryTitle").selectedIndex = document.getElementById("drpTitle").selectedIndex;
		document.getElementById("txtDeliveryFirstName").value = document.getElementById("txtFirstName").value;
		document.getElementById("txtDeliverySurname").value = document.getElementById("txtSurname").value;
		document.getElementById("txtDeliveryEmail").value = document.getElementById("txtEmail").value;
		document.getElementById("txtDeliveryContactNumber").value = document.getElementById("txtContactNumber").value;
		document.getElementById("txtDeliveryAddress").value = document.getElementById("txtAddress").value;
		document.getElementById("txtDeliveryTown").value = document.getElementById("txtTown").value;
		document.getElementById("txtDeliveryCounty").value = document.getElementById("txtCounty").value;
		document.getElementById("txtDeliveryPostcode").value = document.getElementById("txtPostcode").value;
		document.getElementById("drpDeliveryCountry").selectedIndex = document.getElementById("drpCountry").selectedIndex;
	}else{
		document.getElementById("drpDeliveryTitle").selectedIndex = 0;
		document.getElementById("txtDeliveryFirstName").value = "";
		document.getElementById("txtDeliverySurname").value = "";
		document.getElementById("txtDeliveryEmail").value = "";
		document.getElementById("txtDeliveryContactNumber").value = "";
		document.getElementById("txtDeliveryAddress").value = "";
		document.getElementById("txtDeliveryTown").value = "";
		document.getElementById("txtDeliveryCounty").value = "";
		document.getElementById("txtDeliveryPostcode").value = "";
		document.getElementById("drpDeliveryCountry").selectedIndex = 0;
	}
}//setdeliveryaddress()

function setprice() {
	document.getElementById("lblRunningTotal").innerHTML = formatAsMoney(g_rt);
}

function settotal() {
	document.getElementById("lblRunningTotal").innerHTML = formatAsMoney(g_rt);
}

function calcShipping() {
	//g_sa[i][0] = shipping cost id
	//g_sa[i][1] = shipping unit cost
	//g_sa[i][2] = currently selected shipping cost (uses totqty)
	pid = document.getElementById("drpShipping").options[document.getElementById("drpShipping").selectedIndex].value;
	shipAmt = 0;
	for (i = 0; i < g_sa.length; i++) {
		if (g_sa[i][2] > 0) {
			shipAmt = g_sa[i][1] * g_sa[i][2];
		}
	}
	return shipAmt;
}

function shipping() {
	if (document.getElementById("drpShipping") != null) {
		// calculate total qty of frames ordered
		totqty = 0;
		for (i = 1; i <= g_ca.length; i++) {
			qty = "sum_drpQuantity" + (i-1);
			totqty += document.getElementById(qty).selectedIndex + 1;
		}
		pid = document.getElementById("drpShipping").options[document.getElementById("drpShipping").selectedIndex].value;
		newprice = 0;
		oldprice = 0;
		for (i = 0; i < g_sa.length; i++) {
			if (g_sa[i][2] > 0) {
				oldprice = g_sa[i][1] * g_sa[i][2];
				g_sa[i][2] = 0;
			}
			if (g_sa[i][0] == pid) {
				newprice = g_sa[i][1] * totqty;
				g_sa[i][2] = totqty;	
			}
		}
		g_rt -= oldprice;
		g_rt += newprice;
		settotal();	
	}
}//shipping()

function showlargerimage(page, productid) {
	index = 0;
	selected = 0;
	var aControls = document.getElementsByTagName("input");
	for (var i = 0; i < aControls.length; i++) {
		var oControl = aControls[i];
		if (oControl.type == "radio") {
			if (oControl.checked) {
				// make sure we are looking at the right frame
				var id = oControl.id;
				if (page == "cp")
					var frame = id.substr(11,(id.lastIndexOf("_")-11));
				else
					var frame = id.substr(10,(id.lastIndexOf("_")-10));				
				if (frame == productid) {
					selected = oControl.value;
					break;
				}
			}
		}
	}
	if (!selected)
		selected = productid;
		
	window.open('ProductImage.aspx?p=' + selected,'','scrollbars=1;resizeable=1,height=550,width=985');
}//showlargerimage()

function thinlenses() {
	// make sure thin lenses are selected if right/left sphere between -6.00 & -10.00
	if (document.getElementById("drpThinLenses").selectedIndex == 0){
		rsph = document.getElementById("drpRightSphere").options[document.getElementById("drpRightSphere").selectedIndex].value;
		lsph = document.getElementById("drpLeftSphere").options[document.getElementById("drpLeftSphere").selectedIndex].value;
		if ((rsph >= -10.00 && rsph <= -6.00) || (lsph >= -10.00 && lsph <= -6.00)) {
			alert("As you have a Sphere value greater than -6.00, we would strongly advise you to have high index lenses.\n\nIf you do not wish to have high index lenses then you will need to obtain a separate quote for your lenses as a surcharge will apply.");
			return false;
		}
	}
	// correctly set pricing
	pid = document.getElementById("drpThinLenses").options[document.getElementById("drpThinLenses").selectedIndex].value;
	if (pid > 0) {
		if (document.getElementById("drpTint").options[document.getElementById("drpTint").selectedIndex].value > 0)
			alert('Choosing High Index and Tint options together can result in incompatibilities with some prescriptions.\nWe may contact you if this is the case.');
	}
	newprice = 0;
	oldprice = 0;
	for (i = 0; i < g_tna.length; i++) {
		if (g_tna[i][2] == 1) {
			g_tna[i][2] = 0;
			oldprice = g_tna[i][1];
		}
		if (g_tna[i][0] == pid) {
			newprice = g_tna[i][1];
			g_tna[i][2] = 1;	
		}
	}
	g_rt -= oldprice;
	g_rt += newprice;
	setprice();	
	if (document.getElementById("drpPolarisedLenses").selectedIndex == 0) {
		// if thin lens set, disable photochromic (transitional) lens
		if (pid > 0){
			document.getElementById("drpTransitionalLenses").disabled = true;
			document.getElementById("drpTransitionalLenses").selectedIndex = 0;
			transitionallenses();			
		}else{
			document.getElementById("drpTransitionalLenses").disabled = false;
		}
	}
	// if thin lens = 1.67 or 1.74 offer free anti-reflective coating
	setantireflectivecoating();
}//thinlenses

function setantireflectivecoating() {

	// figure out if customer is entitled to free anti-reflective coating
	bAntiReflectiveIsFree = false;
	pid = document.getElementById("drpThinLenses").options[document.getElementById("drpThinLenses").selectedIndex].value;
	if (g_bFreeThinLens) {
		if (pid == 11) {										// 1.60
			bAntiReflectiveIsFree = true;
		}
	}else{
		if ((pid == 12) || (pid == 531)) {  // 1.67 or 1.74
			bAntiReflectiveIsFree = true;
		}
	}
	
	// update dropdownlist text to show correct price of anti reflective coating
	for (k=0;k<document.getElementById("drpCoating").options.length;k++) {
		if (document.getElementById("drpCoating").options[k].value == 13) {
			rtext = g_CurrencySymbol + "15.00";
			if (bAntiReflectiveIsFree)
				document.getElementById("drpCoating").options[k].text = document.getElementById("drpCoating").options[k].text.replace(rtext,"FREE");
			else
				document.getElementById("drpCoating").options[k].text = document.getElementById("drpCoating").options[k].text.replace("FREE",rtext);
		}
	}

			for (i = 0; i < g_cta.length; i++) {		
				if (g_cta[i][2] == 1) {
					if (g_cta[i][0] == 13) {
						g_cta[i][2] = 0;
						oldprice = g_cta[i][1];
						g_rt -= oldprice;
						setprice();	
					}
				}
				// need to set array pricing for anti reflective coating
				if (g_cta[i][0] == 13) {
					if (bAntiReflectiveIsFree)
						g_cta[i][1] = 0;
					else
						g_cta[i][1] = 15;
				}
			}

	// set dropdownlist selected index and update pricing
	if (g_bFreeThinLens) {
		if (pid == 11) {																										// 1.60
			document.getElementById("drpCoating").selectedIndex = 2;					// select anti-reflective
		}else if(document.getElementById("drpCoating").selectedIndex == 2){
			document.getElementById("drpCoating").selectedIndex = 0;					// select none
		}
	}else{
		if ((pid == 12) || (pid == 531)) {																	// 1.67 or 1.74
			if (document.getElementById("drpCoating").selectedIndex != 3)
				document.getElementById("drpCoating").selectedIndex = 2;				// select anti-reflective
		}else if(document.getElementById("drpCoating").selectedIndex == 2){
			document.getElementById("drpCoating").selectedIndex = 0;					// select none
		}
	}
	
}//setantireflectivecoating

function tint() {
	pid = document.getElementById("drpTint").options[document.getElementById("drpTint").selectedIndex].value;
	if (pid > 0) {
		if (document.getElementById("drpThinLenses").options[document.getElementById("drpThinLenses").selectedIndex].value > 0)
			alert('Choosing High Index and Tint options together can result in incompatibilities with some prescriptions.\nWe may contact you if this is the case.');
	}
	newprice = 0;
	oldprice = 0;
	for (i = 0; i < g_ta.length; i++) {
		if (g_ta[i][2] == 1) {
			g_ta[i][2] = 0;
			oldprice = g_ta[i][1];
		}
		if (g_ta[i][0] == pid) {
			newprice = g_ta[i][1];
			g_ta[i][2] = 1;	
		}
	}
	g_rt -= oldprice;
	g_rt += newprice;
	setprice();	
}

function transitionallenses() {
	pid = document.getElementById("drpTransitionalLenses").options[document.getElementById("drpTransitionalLenses").selectedIndex].value;
	newprice = 0;
	oldprice = 0;
	for (i = 0; i < g_tla.length; i++) {
		if (g_tla[i][2] == 1) {
			g_tla[i][2] = 0;
			oldprice = g_tla[i][1];
		}
		if (g_tla[i][0] == pid) {
			newprice = g_tla[i][1];
			g_tla[i][2] = 1;	
		}
	}
	g_rt -= oldprice;
	g_rt += newprice;
	setprice();		
	if (pid > 0){
		document.getElementById("drpPolarisedLenses").disabled = true;
		document.getElementById("drpPolarisedLenses").selectedIndex = 0;
		polarisedlenses();
		document.getElementById("drpTint").disabled = true;
		document.getElementById("drpTint").selectedIndex = 0;		
		tint();
		document.getElementById("drpThinLenses").disabled = true;
		document.getElementById("drpThinLenses").selectedIndex = 0;
		thinlenses();			
	}else{
		document.getElementById("drpTint").disabled = false;
		document.getElementById("drpPolarisedLenses").disabled = false;		
		document.getElementById("drpThinLenses").disabled = false;			
	}
}

function trial() {
	//alert("Unfortunately we are unable to take your Order right now due to technical difficulties.\n\nPlease try again at a later time. We sincerely apologise for the inconvenience.");
	//return false;
	a = document.getElementById("a");
	if ((a.value == "") || (a.value == "t")) {
		// Frame colour
		index = 0;
		selected = 0;
		var aControls = document.getElementsByTagName("input");
		for (var i = 0; i < aControls.length; i++) {
			var oControl = aControls[i];
			if (oControl.type == "radio") {
				if (oControl.checked) {
					// make sure we are looking at the right frame
					var id = oControl.id;
					var frame = id.substr(10,(id.lastIndexOf("_")-10));				
					if (frame == g_p) {
						selected = oControl.value;
						break;
					}
				}
			}
		}
		if (!selected) {
			alert("Please choose a Frame colour");
			elem = "p_rdoFrame" + g_p + "_0";
			document.getElementById(elem).focus();
			return false;
		}
		else
		{
		//if (validatecustomisation()) {
			a.value = "t";
			document.forms[0].submit();
		}
	}else{
		alert("You cannot Trial and Purchase frames on one Order");
	}
} //trial()

function validatecatalogueorder()
{
	sMsg = "Please enter";
	if (document.getElementById("Title").selectedIndex == 0)
		sMsg += "\n - Title";
	if (document.getElementById("First Name").value == "")
		sMsg += "\n - First Name";
	if (document.getElementById("Surname").value == "")
		sMsg += "\n - Surname";
	if (document.getElementById("E-mail").value == "")
		sMsg += "\n - E-mail";	
	if (!g_emailfilter.test(document.getElementById("E-mail").value)) {
		alert("Invalid E-mail address");
		return false;
	}
	if (document.getElementById("Address").value == "")
		sMsg += "\n - Address";		
	if (document.getElementById("Town").value == "")
		sMsg += "\n - Town";		
	if (document.getElementById("County").value == "")
		sMsg += "\n - County";	
	if (document.getElementById("Postcode").value == "")
		sMsg += "\n - Postcode";		
	if (document.getElementById("Country").selectedIndex == 0)
		sMsg += "\n - Country";							
	if (document.getElementById("Where did you hear about us?").selectedIndex == 0)
		sMsg += "\n - Where did you hear about us";		
	if (sMsg != "Please enter") {
		alert(sMsg);
		return false;
	}else{
		return true;
	}
}

function validatecontactus()
{
	sMsg = "Please enter";
	if (document.getElementById("Title").selectedIndex == 0)
		sMsg += "\n - Title";
	if (document.getElementById("First Name").value == "")
		sMsg += "\n - First Name";
	if (document.getElementById("Surname").value == "")
		sMsg += "\n - Surname";
	if (document.getElementById("E-mail").value == "")
		sMsg += "\n - E-mail";	
	if (!g_emailfilter.test(document.getElementById("E-mail").value)) {
		alert("Invalid E-mail address");
		return false;
	}
	if (document.getElementById("Message").value == "")
		sMsg += "\n - Message";			
	if (document.getElementById("Where did you hear about us?").selectedIndex == 0)
		sMsg += "\n - Where did you hear about us";		
	if (sMsg != "Please enter") {
		alert(sMsg);
		return false;
	}else{
		return true;
	}
}

function validatecustomisation() {
// Frame colour
	index = 0;
	selected = 0;
	var aControls = document.getElementsByTagName("input");
	for (var i = 0; i < aControls.length; i++) {
		var oControl = aControls[i];
		if (oControl.type == "radio") {
			if (oControl.checked) {
				// make sure we are looking at the right frame
				var id = oControl.id;
				var frame = id.substr(10,(id.lastIndexOf("_")-10));				
				if (frame == g_p) {
					selected = oControl.value;
					break;
				}
			}
		}
	}
	/*do
	{
		elem = "p_rdoFrame" + g_p + "_" + index;
		if (document.getElementById(elem) == null) {
			break;
		}else{
			if (document.getElementById(elem).checked)
				selected = document.getElementById(elem).value;
			index += 1;
		}
	}
	while(!selected);*/
	if (!selected) {
		alert("Please choose a Frame colour");
		elem = "p_rdoFrame" + g_p + "_0";
		document.getElementById(elem).focus();
		return false;
	}
	
// Lens Type	
	if (document.getElementById("drpLensType").options[document.getElementById("drpLensType").selectedIndex].value == "0") {
		alert("You must choose a Lens Type");
		document.getElementById("drpLensType").focus();
		return false;
	}
// Papillar Distance
	pd = document.getElementById("txtPD").value;
	if (pd == "") {
		alert("Please enter your Papillary Distance");
		document.getElementById("txtPD").focus();
		return false;
	}
	if (((pd-Math.floor(pd))!=0) || (isNaN(pd)) || (pd < 45) || (pd > 80)) {
		alert("Papillary Distance must be an integer between 45 and 80");
		document.getElementById("txtPD").focus();
		return false;
	}
// Right Cylinder or Right Sphere
	if ((document.getElementById("drpRightCylinder").options[document.getElementById("drpRightCylinder").selectedIndex].value == "None") &&
		(document.getElementById("drpRightSphere").options[document.getElementById("drpRightSphere").selectedIndex].value == "None")) {
		alert("You must specify either a Right Sphere or Right Cylinder value");
		document.getElementById("drpRightSphere").focus();
		return false;
	}
// Left Cylinder or Left Sphere
	if ((document.getElementById("drpLeftCylinder").options[document.getElementById("drpLeftCylinder").selectedIndex].value == "None") &&
		(document.getElementById("drpLeftSphere").options[document.getElementById("drpLeftSphere").selectedIndex].value == "None")) {
		alert("You must specify either a Left Sphere or Left Cylinder value");
		document.getElementById("drpLeftSphere").focus();
		return false;
	}
// Right Axis
	axis = document.getElementById("txtRightAxis").value;
	if (document.getElementById("drpRightCylinder").options[document.getElementById("drpRightCylinder").selectedIndex].value != "None") {
		if (axis == "None") {
			alert("Please enter your Right Axis");
			document.getElementById("txtRightAxis").focus();
			return false;
		}
	}
	if (axis != "" && axis != "None") {
		if (!g_axis.test(document.getElementById("txtRightAxis").value)) {
			alert("Right Axis must be a value between 1 and 180 and may contain 1 decimal place");
			return false;
		}		
		//if (((axis-Math.floor(axis))!=0) || (isNaN(axis)) || (axis < 1) || (axis > 180)) {
		//	alert("Right Axis must be a value between 1 and 180");
		if ((isNaN(axis)) || (axis < 1) || (axis > 180)) {
			alert("Right Axis must be a value between 1 and 180 and may contain 1 decimal place");
			document.getElementById("txtRightAxis").focus();
			return false;
		}
	}
// Left Axis
	axis = document.getElementById("txtLeftAxis").value;
	if (document.getElementById("drpLeftCylinder").options[document.getElementById("drpLeftCylinder").selectedIndex].value != "None") {
		if (axis == "None") {
			alert("Please enter your Left Axis");
			document.getElementById("txtLeftAxis").focus();
			return false;
		}
	}
	if (axis != "" && axis != "None") {
		if (!g_axis.test(document.getElementById("txtLeftAxis").value)) {
			alert("Left Axis must be a value between 1 and 180 and may contain 1 decimal place");
			return false;
		}		
		//if (((axis-Math.floor(axis))!=0) || (isNaN(axis)) || (axis < 1) || (axis > 180)) {
		//	alert("Left Axis must be an integer between 1 and 180");
		if ((isNaN(axis)) || (axis < 1) || (axis > 180)) {
			alert("Left Axis must be a value between 1 and 180 and may contain 1 decimal place");
			document.getElementById("txtLeftAxis").focus();
			return false;
		}
	}
	
// Thin Lens required if Right or Left Sphere is between -6.00 and -10.00	
	rsph = document.getElementById("drpRightSphere").options[document.getElementById("drpRightSphere").selectedIndex].value;
	lsph = document.getElementById("drpLeftSphere").options[document.getElementById("drpLeftSphere").selectedIndex].value;
	if ((rsph >= -10.00 && rsph <= -6.00) || (lsph >= -10.00 && lsph <= -6.00)) {
		if (document.getElementById("drpThinLenses").selectedIndex == 0){
			//alert("As your Right and/or Left Sphere has a value of between -6.00 and -10.00, you must have a Thin Lens");
			alert("As you have a Sphere value greater than -6.00, we would strongly advise you to have high index lenses.\n\nIf you do not wish to have high index lenses then you will need to obtain a separate quote for your lenses as a surcharge will apply.");
			document.getElementById("drpThinLenses").focus();
			return false;
		}
	}	
	
// Addition Reqd if Bifocal Lens selected
	if (document.getElementById("drpLensType").options[document.getElementById("drpLensType").selectedIndex].value == 17) {
		if (document.getElementById("drpLeftAddition").options[document.getElementById("drpLeftAddition").selectedIndex].value == "Not Applicable") {
			alert("Left Addition is required when purchasing Bifocal Lenses");
			document.getElementById("drpLeftAddition").focus();
			return false;
		}
		if (document.getElementById("drpRightAddition").options[document.getElementById("drpRightAddition").selectedIndex].value == "Not Applicable") {
			alert("Right Addition is required when purchasing Bifocal Lenses");
			document.getElementById("drpRightAddition").focus();
			return false;
		}
	}
// Disclaimer	
	if (!document.getElementById("chkDisclaimer").checked) {
		alert("Please confirm you have read the Perfect Specs Disclaimer Notice by checking the Disclaimer checkbox");
		document.getElementById("chkDisclaimer").focus();
		return false;
	}
	document.getElementById("drpRightAddition").disabled = false;
	document.getElementById("drpLeftAddition").disabled = false;	
	return true;
}// validatecustomisation()

function validateorderenquiry()
{
	sMsg = "Please enter";
	if (document.getElementById("Title").selectedIndex == 0)
		sMsg += "\n - Title";
	if (document.getElementById("First Name").value == "")
		sMsg += "\n - First Name";
	if (document.getElementById("Surname").value == "")
		sMsg += "\n - Surname";
	if (document.getElementById("E-mail").value == "")
		sMsg += "\n - E-mail";	
	if (!g_emailfilter.test(document.getElementById("E-mail").value)) {
		alert("Invalid E-mail address");
		return false;
	}
	if (document.getElementById("Order Reference").value == "")
		sMsg += "\n - Order Reference";			
	if (sMsg != "Please enter") {
		alert(sMsg);
		return false;
	}else{
		return true;
	}
}

function validatesummary()
{
	sMsg = "Please enter";
	if (document.getElementById("drpShipping"))
		if (document.getElementById("drpShipping").selectedIndex == 0)
			sMsg += "\n - Postage Options";
	if (document.getElementById("drpTitle").selectedIndex == 0)
		sMsg += "\n - Title";
	if (document.getElementById("txtFirstName").value == "")
		sMsg += "\n - First Name";
	if (document.getElementById("txtSurname").value == "")
		sMsg += "\n - Surname";
	if (document.getElementById("txtEmail").value == "")
		sMsg += "\n - E-mail";
	if (!g_emailfilter.test(document.getElementById("txtEmail").value)) {
		alert("Invalid E-mail address");
		return false;
	}
	if (document.getElementById("txtAddress").value == "")
		sMsg += "\n - Address";		
	if (document.getElementById("txtTown").value == "")
		sMsg += "\n - Town";		
	if (document.getElementById("txtCounty").value == "")
		sMsg += "\n - County";	
	if (document.getElementById("txtPostcode").value == "")
		sMsg += "\n - Postcode";		
	if (document.getElementById("drpCountry").selectedIndex == 0)
		sMsg += "\n - Country";							
	if (document.getElementById("drpDeliveryTitle").selectedIndex == 0)
		sMsg += "\n - Delivery Address Title";
	if (document.getElementById("txtDeliveryFirstName").value == "")
		sMsg += "\n - Delivery Address First Name";
	if (document.getElementById("txtDeliverySurname").value == "")
		sMsg += "\n - Delivery Address Surname";
	if (document.getElementById("txtDeliveryContactNumber").value == "")
		sMsg += "\n - Delivery Address Contact Number";			
	if (document.getElementById("txtDeliveryEmail").value == "")
		sMsg += "\n - Delivery Address E-mail";	
	if (!g_emailfilter.test(document.getElementById("txtDeliveryEmail").value)) {
		alert("Invalid Delivery Address E-mail address");
		return false;
	}
	if (document.getElementById("txtDeliveryAddress").value == "")
		sMsg += "\n - Delivery Address Address";		
	if (document.getElementById("txtDeliveryTown").value == "")
		sMsg += "\n - Delivery Address Town";		
	//if (document.getElementById("txtDeliveryCounty").value == "")
	//	sMsg += "\n - Delivery Address County";	
	if (document.getElementById("txtDeliveryPostcode").value == "")
		sMsg += "\n - Delivery Address Postcode";		
	if (document.getElementById("drpDeliveryCountry").selectedIndex == 0)
		sMsg += "\n - Delivery Address Country";							
	if (document.getElementById("drpWhere").selectedIndex == 0)
		sMsg += "\n - Where did you hear about us";	
	if (sMsg != "Please enter") {
		alert(sMsg);
		return false;
	}else{
		if (document.getElementById("chkTerms").checked) {
			document.getElementById("btnPurchase").value = "please wait";
			return true;
		}else{
			alert("Please confirm that you have read and agree to the Terms &	Conditions of Perfect Specs");
			document.getElementById("chkTerms").focus();
			return false;
		}
	}
}//validatesummary()

function validatetellafriend()
{
	sMsg = "Please enter";
	if (document.getElementById("Send to E-mail").value == 0)
		sMsg += "\n - Send to E-mail";
	if (document.getElementById("Your Name").value == "")
		sMsg += "\n - Your Name";
	if (document.getElementById("Your E-mail").value == "")
		sMsg += "\n - Your E-mail";
	if (document.getElementById("Message").value == "")
		sMsg += "\n - Message";		
	if (!g_emailfilter.test(document.getElementById("Send to E-mail").value)) {
		alert("Invalid Send to E-mail address");
		return false;
	}		
	if (!g_emailfilter.test(document.getElementById("Your E-mail").value)) {
		alert("Invalid Your E-mail address");
		return false;
	}
	if (sMsg != "Please enter") {
		alert(sMsg);
		return false;
	}else{
		return true;
	}
}
