// JavaScript Document
function update_attr1(form)
{
	var prod_ref = form.prod_ref.value;
	var attr1_val = form.attr1.value+'';
	
	if(attr1_val != '')
	{
		window.location = "product_detail.cfm?id=" + prod_ref + "&attr1=" + attr1_val;
	}
}

function update_attr2(form)
{
	var prod_ref = form.prod_ref.value;
	var attr1_val = form.attr1.value+'';
	var attr2_val = form.attr2.value+'';
	
	if(attr1_val != '' && attr2_val != '')
	{
		//alert(attr1_val);
		//alert(attr2_val);
		window.location = "product_detail.cfm?id=" + prod_ref + "&attr1=" + attr1_val + "&attr2=" + attr2_val;
	}
	else if(attr1_val != '')
	{
		//alert(attr1_val);
		window.location = "product_detail.cfm?id=" + prod_ref + "&attr1=" + attr1_val;
	}
}

function update_attr3(form)
{
	var prod_ref = form.prod_ref.value;
	var attr1_val = form.attr1.value+'';
	var attr2_val = form.attr2.value+'';
	var attr3_val = form.attr3.value+'';
	
	if(attr1_val != '' && attr2_val != '' && attr3_val != '')
	{
		//alert(attr1_val);
		//alert(attr2_val);
		window.location = "product_detail.cfm?id=" + prod_ref + "&attr1=" + attr1_val + "&attr2=" + attr2_val + "&attr3=" + attr3_val ;
	}
	else if(attr1_val != '')
	{
		//alert(attr1_val);
		window.location = "product_detail.cfm?id=" + prod_ref + "&attr1=" + attr1_val;
	}
}

function update_attr4(form)
{
	var prod_ref = form.prod_ref.value;
	var attr1_val = form.attr1.value+'';
	var attr2_val = form.attr2.value+'';
	var attr3_val = form.attr3.value+'';
	var attr4_val = form.attr4.value+'';
	
	if(attr1_val != '' && attr2_val != '' && attr3_val != '' && attr4_val != '')
	{
		//alert(attr1_val);
		//alert(attr2_val);
		window.location = "product_detail.cfm?id=" + prod_ref + "&attr1=" + attr1_val + "&attr2=" + attr2_val + "&attr3=" + attr3_val + "&attr4=" + attr4_val;
	}
	else if(attr1_val != '')
	{
		//alert(attr1_val);
		window.location = "product_detail.cfm?id=" + prod_ref + "&attr1=" + attr1_val;
	}
}

function update_attr_dropdowns(form,field)
{
	var price = base_price;
	var attr_sku = "";
	var attr1_val = form.attr1.value;
	var attr2_val = form.attr2.value;
	var field_name = field.name;
	var description = form.prod_description.value;

	for(i=0;i<a_attr_skus.length;i++)
	{
		if((a_attr_skus[i][1] == attr1_val) && ((a_attr_skus[i][2] == attr2_val) || attr2_val < 1))
		{
			attr_sku = a_attr_skus[i][0];
			price = a_attr_skus[i][3];
			description = a_attr_skus[i][4];
			break;
		}
	}
	
	form.prod_attr_sku.value = attr_sku;
	form.prod_price.value = price;
	form.prod_description.value = description;
	window.display_price.innerHTML = "&pound;" + price;
	
	if(field_name == "attr1" && form.attr2.options != undefined)
	{
		var from_price = 0;
		form.attr2.options.length = 0;
		form.attr2.options[0] = new Option('-- Select One --','0');
		var pos = 1;
		for(i=0;i<a_attr2s.length;i++)
		{
			attr2_val = a_attr2s[i][0];
			attr2_desc = a_attr2s[i][1];
			for(j=0;j<a_attr_skus.length;j++)
			{
				if((a_attr_skus[j][1] == attr1_val) && (a_attr_skus[j][2] == attr2_val))
				{
					if(from_price == 0 || a_attr_skus[j][3] < from_price)
					{
						from_price = a_attr_skus[j][3];
					}
					form.attr2.options[pos] = new Option(attr2_desc,attr2_val);
					pos++;
				}
			}
		}
		if(from_price == 0)
		{
			from_price = "Please select some options";
		}
		else
		{
			from_price = "From &pound;" + from_price;
		}
		form.prod_attr_sku.value = "";
		form.prod_price.value = "";
		form.prod_description.value = "";
		window.display_price.innerHTML = from_price;
	}
}

function swap_image(new_img,img_type)
{
	document["main_image"].src = new_img;
	if(img_type == "default")
	{
		window.alt_link.innerHTML =  "<p>Main Image</p><p><a href=\"javascript:swap_image('/catalogue/images/" + alt_image + "','alternate')\">Alternate Image</a></p>";
	}
	else
	{
		window.alt_link.innerHTML =  "<p><a href=\"javascript:swap_image('" + default_image + "','default')\">Main Image</a></p><p>Alternate Image</p>";
	}
}

function add_to_basket(form)
{
	if(validate_product(form) == true)
	{
		form.submit();
	}
}

function validate_product(form)
{
	//alert("Attr SKU: " + form.prod_attr_sku.value + "\nPrice: £" + form.prod_price.value + "\nOld price: £" + form.prod_old_price.value);
	//return false;
	
	var return_value = false;
	
	if(form.attr1.value == "")
	{
		alert("Please select an option");
		form.attr1.focus();
	}
	else if(form.attr2.value == "")
	{
		alert("Please select an option");
		form.attr2.focus();
	}
	else if(!(parseFloat(form.prod_price.value) > 0))
	{
		alert("Please select some options");
		form.prod_price.focus();
	}
	else if(!(parseFloat(form.quantity.value) > 0) || (isNaN(parseFloat(form.quantity.value))))
	{
		alert("Please enter a valid quantity");
		form.quantity.focus();
	}
	else
	{
		return_value = true;
	}
	
	return return_value;
}

function submit_ch_login(form)
{
	if(validate_ch_login(form) == true)
	{
		form.submit();
	}
}

function submit_cus_login(form)
{
	if(validate_cus_login(form) == true)
	{
		form.submit();
	}
}

function validate_ch_login(form)
{
	var return_value = false;
	if(is_email(form.email.value) == false)
	{
		alert("Please enter a valid email address");
		form.email.focus();
	}
	else if(form.password.value == "")
	{
		alert("Please enter your password");
		form.password.focus();
	}
	else
	{
		return_value = true;
	}

	return return_value;
}

function validate_cus_login(form)
{
	var return_value = false;
	if(form.email.value == "")
	{
		alert("Please enter a valid email address");
		form.email.focus();
	}
	else if(form.password.value == "")
	{
		alert("Please enter your password");
		form.password.focus();
	}
	else
	{
		return_value = true;
	}

	return return_value;
}

function submit_ch_address(form)
{
	if(validate_ch_address(form) == true)
	{
		form.submit();
	}
}

function submit_cus_address(form)
{
	if(validate_cus_address(form) == true)
	{
		form.submit();
	}
}

function validate_ch_address(form)
{
	var return_value = false;
	
	if(form.contact_name.value == "")
	{
		alert("Please enter your Contact Name");
		form.contact_name.focus();
	}
	else if(form.addr1.value == "")
	{
		alert("Please enter your Address");
		form.addr1.focus();
	}
	else if(form.town.value == "")
	{
		alert("Please enter your Town");
		form.town.focus();
	}
	else if(form.county.value == "")
	{
		alert("Please enter your County");
		form.county.focus();
	}
	else if(form.postcode.value == "")
	{
		alert("Please enter your Postcode");
		form.postcode.focus();
	}
	else if(form.country.value == 0)
	{
		alert("Please select your Country");
		form.country.focus();
	}
	else if(form.phone.value == "")
	{
		alert("Please enter your Contact Phone Number");
		form.phone.focus();
	}
	else if(form.copy_billing_address != undefined)
	{
		if(form.copy_billing_address.checked == false)
		{
			if(form.del_addr1.value == "")
			{
				alert("Please enter your Delivery Address");
				form.del_addr1.focus();
			}
			else if(form.del_town.value == "")
			{
				alert("Please enter your Delivery Town");
				form.del_town.focus();
			}
			else if(form.del_county.value == "")
			{
				alert("Please enter your Delivery County");
				form.del_county.focus();
			}
			else if(form.del_postcode.value == "")
			{
				alert("Please enter your Delivery Postcode");
				form.del_postcode.focus();
			}
			else if(form.del_country.value == 0)
			{
				alert("Please select your Delivery Country");
				form.del_country.focus();
			}
			else
			{
				return_value = true;
			}
		}
		else
		{
			return_value = true;
		}
	}
	else if(form.password != undefined)
	{
		if(form.conf_email.value != form.email.value)
		{
			alert("Please verify that your email address is correct");
			form.email.focus();
		}
		else if(form.password.value == "")
		{
			alert("Please enter a Password");
			form.password.focus();
		}
		else if(form.conf_password.value == "")
		{
			alert("Please confirm your Password");
			form.conf_password.focus();
		}
		else if(form.conf_password.value != form.password.value)
		{
			alert("Your passwords do not match");
			form.conf_password.focus();
		}
		else
		{
			return_value = true;
		}
	}
	else
	{
		return_value = true;
	}
	
/*	alert(return_value);
	return false;*/
	
	return return_value;
}

function set_del_addr_status(form)
{
	if(form.copy_billing_address.checked == true)
	{
		form.del_addr1.disabled = true;
		form.del_addr2.disabled = true;
		form.del_town.disabled = true;
		form.del_county.disabled = true;
		form.del_postcode.disabled = true;
		form.del_country.disabled = true;
	}
	else
	{
		form.del_addr1.disabled = false;
		form.del_addr2.disabled = false;
		form.del_town.disabled = false;
		form.del_county.disabled = false;
		form.del_postcode.disabled = false;
		form.del_country.disabled = false;
	}
}

function set_payment_status(form)
{
	if(form.pay_on_account.checked == true)
	{
		form.card_type.disabled = true;
		form.card_number.disabled = true;
		form.start_month.disabled = true;
		form.start_year.disabled = true;
		form.end_month.disabled = true;
		form.end_year.disabled = true;
		form.card_issue_number.disabled = true;
		form.card_name.disabled = true;
		form.card_security_code.disabled = true;
	}
	else
	{
		form.card_type.disabled = false;
		form.card_number.disabled = false;
		form.start_month.disabled = false;
		form.start_year.disabled = false;
		form.end_month.disabled = false;
		form.end_year.disabled = false;
		form.card_issue_number.disabled = false;
		form.card_name.disabled = false;
		form.card_security_code.disabled = false;
	}
}

function validate_cus_address(form)
{
	if(form.country.value != 0)
	{
		var a_country = form.country.value.split("::");
		var tmp_country = a_country[0];
	}
	else
	{
		var tmp_country = "";
	}
	var return_value = false;
	if(form.contact_name.value == "")
	{
		alert("Please enter your Name");
		form.contact_name.focus();
	}
	else if(form.addr1.value == "")
	{
		alert("Please enter your Address");
		form.addr1.focus();
	}
	/*else if(form.town.value == "")
	{
		alert("Please enter your Town");
		form.town.focus();
	}
	else if(form.county.value == "")
	{
		alert("Please enter your County");
		form.county.focus();
	}*/
	else if(tmp_country == "GB" && form.postcode.value == "")
	{
		alert("Please enter your Postcode");
		form.postcode.focus();
	}
	else if(form.country.value == 0)
	{
		alert("Please select your Country");
		form.country.focus();
	}
	else if(form.phone.value == "")
	{
		alert("Please enter your Contact Phone Number");
		form.phone.focus();
	}
	else if(is_email(form.email.value) == false)
	{
		alert("Please verify that your email address is correct");
		form.email.focus();
	}
	else if(form.copy_billing_address.checked == false)
	{
		if(form.del_addr1.value == "")
		{
			alert("Please enter your Delivery Address");
			form.del_addr1.focus();
		}
		/*else if(form.del_town.value == "")
		{
			alert("Please enter your Delivery Town");
			form.del_town.focus();
		}
		else if(form.del_county.value == "")
		{
			alert("Please enter your Delivery County");
			form.del_county.focus();
		}*/
		else if(form.del_postcode.value == "")
		{
			alert("Please enter your Delivery Postcode");
			form.del_postcode.focus();
		}
		else if(form.del_country.value == 0)
		{
			alert("Please select your Delivery Country");
			form.del_country.focus();
		}
		else
		{
			return_value = true;
		}
	}
	else
	{
		return_value = true;
	}
	
/*	alert(return_value);
	return false;*/
	
	return return_value;
}

function submit_ch_promo_code(form)
{
	if(validate_ch_promo_code(form) == true)
	{
		form.submit();
	}
}

function validate_ch_promo_code(form)
{
	var return_value = false;
	
	if(form.promo_code.value == "")
	{
		alert("Please enter a Promotion Code");
		form.promo_code.focus();
	}
	else
	{
		return_value = true;
	}
	
	return return_value;
}

function submit_ch_billing(form)
{
	if(validate_ch_billing(form) == true)
	{
		form.submit();
	}
}

function validate_ch_billing(form)
{
	var return_value = false;
	var check_card = true;
	
	if(form.check_account.value == "Y")
	{
		if(form.pay_on_account.checked == true)
		{
			check_card = false;
		}
	}
	
	if(check_card == true)
	{
		if(form.card_type.value == 0)
		{
			alert("Please select a Card Type");
			form.card_type.focus();
		}
		else if((form.card_number.value.length < 13) || (form.card_number.value.length > 19))
		{
			alert("Please enter a valid Card Number");
			form.card_number.focus();
		}
		else if(form.end_month.value == 0)
		{
			alert("Please select an End Date");
			form.end_month.focus();
		}
		else if(form.end_year.value == 0)
		{
			alert("Please select an End Date");
			form.end_year.focus();
		}
		else if(form.card_name.value == "")
		{
			alert("Please enter your Name");
			form.card_name.focus();
		}
		else if(form.card_security_code.value == "")
		{
			alert("Please enter the Security Code from your card (usually the last 3 or 4 digits of the number on the back of the card)");
			form.card_security_code.focus();
		}
		else if(form.ts_and_cs.checked == false)
		{
			alert("Please confirm that you have read and agree to our Terms and Conditions");
			form.ts_and_cs.focus();
		}
		else
		{
			return_value = true;
		}
	}
	else if(form.ts_and_cs.checked == false)
	{
		alert("Please confirm that you have read and agree to our Terms and Conditions");
		form.ts_and_cs.focus();
	}
	else
	{
		return_value = true;
	}
	
	//alert(return_value);
	//return false;
	return return_value;
}

function cardval(s) {
// remove non-numerics
var v = "0123456789";
var w = "";
for (i=0; i < s.length; i++) {
x = s.charAt(i);
if (v.indexOf(x,0) != -1)
w += x;
}
// validate number
j = w.length / 2;
if (j < 6.5 || j > 9 || j == 7) return false;
k = Math.floor(j);
m = Math.ceil(j) - k;
c = 0;
for (i=0; i<k; i++) {
a = w.charAt(i*2+m) * 2;
c += a > 9 ? Math.floor(a/10 + a%10) : a;
}
alert('here3');
for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
return (c%10 == 0);
}

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 submit_try_at_home(form)
{
	form.try_at_home.value = "Y";
	form.lenses_rqd.value = "N";
	if(validate_product(form) == true)
	{
		//alert(form.name);
		form.submit();
	}
}

function validate_pd_photo(form)
{
	var photo_method = "";
	for(i=0;i<form.pd_photo_method.length;i++)
	{
		if(form.pd_photo_method[i].checked == true)
		{
			photo_method = form.pd_photo_method[i].value;
		}
	}
	var return_value = true;
	if(photo_method == '')
	{
		alert('Please select how you want to send your photograph');
		return_value = false;
	}
	if(photo_method == 'Upload')
	{
		if(form.photo_file.value == "")
		{
			alert('Please select a file');
			return_value = false;
		}
	}
	return return_value;
}

function set_pd_unknown(form)
{
	for(i=1;i<form.centration.length;i++)
	{
		if(form.centration[i].value == 0)
		{
			form.centration[i].selected = true;
		}
		else
		{
			form.centration[i].selected = false;
		}
	}
	form.cent_conf.value = "Y";
}

function cardval2(s)
{
	// remove non-numerics
	var v = "0123456789";
	var w = "";

	for (i=0; i < s.length; i++)
	{
		x = s.charAt(i);
		if (v.indexOf(x,0) != -1)
		w += x;
	}

	// validate number
	j = w.length / 2;
	if (j < 6.5 || j > 9.5 || j == 7)
	{
		//return false;
		alert(false);
	}
	
	k = Math.floor(j);
	m = Math.ceil(j) - k;
	c = 0;
	for (i=0; i<k; i++)
	{
		a = w.charAt(i*2+m) * 2;
		//alert(a);
		//alert(a/10);
		//alert(a%10);
		//alert(9 ? Math.floor(a/10 + a%10) : a);
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
		//alert(c);
	}
	alert(c);
	for (i=0; i<k+m; i++)
	{
		c += w.charAt(i*2+1-m) * 1;
	}
	//return (c%10 == 0);
	alert(c%10 == 0);
}

function submit_search(form)
{
	if(validate_search(form) == true)
	{
		form.submit();
	}
}

function validate_search(form)
{
	var return_value = false;
	
	if(form.criteria.value == "")
	{
		alert("Please enter some search criteria");
		form.criteria.focus();
	}
	else
	{
		return_value = true;
	}
	
	return return_value;
}

function update_basket(form)
{
	form.submit();
}

function show_hide_trade()
{
	var cbx = document.getElementById('trade_account');
	var userId = document.getElementById('user_id');
	
	if(userId.value == 0)
	{
		if (cbx.checked == true)
		{
			document.getElementById('span_trade').style.display = 'block';
			//doc.getElementById('span_security').style.display = 'none';
		}
		else
		{
			document.getElementById('span_trade').style.display = 'none';
			//doc.getElementById('span_security').style.display = 'block';
		}
	}
}

function show_hide_delivery()
{
	var cbx = document.getElementById('copy_billing_address');
	var userId = document.getElementById('user_id');
	
	if (cbx.checked == false)
	{
		document.getElementById('fsDeliveryDetails').style.display = 'block';
	}
	else
	{
		document.getElementById('fsDeliveryDetails').style.display = 'none';
	}
}

function updateTotal(form)
{
	var premium = 0;
	var delVatRate = parseFloat(form.del_vat_rate.value);
	var delVatMultiplier = delVatRate / 100;
	var subTotal = parseFloat(form.sub_total.value);
	var prodVat = parseFloat(form.prod_vat.value);
	var vatTotal = parseFloat(form.vat_sub_total.value);
	var tmpShipping = form.shipping.value;
	var aShipping = tmpShipping.split("::");
	var shipping_desc = aShipping[0];
	var shipping = parseFloat(aShipping[1]);
	var delVat = (Math.round((shipping * delVatMultiplier) * 100)) / 100;
	vatTotal = prodVat + delVat;

	var grandTotal = subTotal + shipping + vatTotal;
	form.del_vat.value = delVat;
	form.vat_total.value = vatTotal;
	form.grand_total.value = grandTotal;
	strVatTotal = vatTotal.toFixed(2);
	strDisplayTotal = grandTotal.toFixed(2);
	var strText = "£" + strDisplayTotal;
	
	var newEm = document.createElement("em");
	var newText = document.createTextNode(strText);
	newEm.appendChild(newText);
	
	var objContainer = document.getElementById("spanDisplayTotal");
	var oldEm = document.getElementsByTagName("em").item(2);
	var replaced = objContainer.replaceChild(newEm,oldEm);
	
	var strText2 = "£" + strVatTotal;
	
	var newEm2 = document.createElement("em");
	var newText2 = document.createTextNode(strText2);
	newEm2.appendChild(newText2);
	
	var objContainer2 = document.getElementById("spanVatTotal");
	var oldEm2 = document.getElementsByTagName("em").item(1);
	var replaced2 = objContainer2.replaceChild(newEm2,oldEm2);
}

function resize_thumbs()
{
	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version < 7)
	{
		var counter = window.document.data.counter.value;
		for(i=1;i<=counter;i++)
		{
			var max_width = 100;
			var max_height = 200;
			var image_id = 'thumbnail' + i;
			var objImg = document.getElementById(image_id);
			var img_width = objImg.width;
			var img_height = objImg.height;
			if(img_width > max_width)
			{
				var multiplier = max_width / img_width;
				var new_width = max_width;
				var new_height = img_height * multiplier;
				objImg.width = new_width;
				objImg.height = new_height;
				img_width = objImg.width;
				img_height = objImg.height;
			}
			if(img_height > max_height)
			{
				var multiplier = max_height / img_height;
				var new_height = max_height;
				var new_width = img_width * multiplier;
				objImg.width = new_width;
				objImg.height = new_height;
			}
		}
	}
}

function resize_images(iCount)
{
	/*if (BrowserDetect.browser == "Explorer" && BrowserDetect.version < 7)
	{*/
		var max_width = 338;
		var max_height = 500;
		var objImg = document.getElementById('prod_image');
		var img_width = objImg.width;
		var img_height = objImg.height;
		if(img_width > max_width)
		{
			var multiplier = max_width / img_width;
			var new_width = max_width;
			var new_height = img_height * multiplier;
			objImg.width = new_width;
			objImg.height = new_height;
			img_width = objImg.width;
			img_height = objImg.height;
		}
		if(img_height > max_height)
		{
			var multiplier = max_height / img_height;
			var new_height = max_height;
			var new_width = img_width * multiplier;
			objImg.width = new_width;
			objImg.height = new_height;
		}
		
		if(iCount > 1)
		{
			var max_width = 100;
			var max_height = 140;
			for(i=1;i<=iCount;i++)
			{
				var objImg = document.getElementById('thumbnail' + i);
				var img_width = objImg.width;
				var img_height = objImg.height;
				if(img_width > max_width)
				{
					var multiplier = max_width / img_width;
					var new_width = max_width;
					var new_height = img_height * multiplier;
					objImg.width = new_width;
					objImg.height = new_height;
					img_width = objImg.width;
					img_height = objImg.height;
				}
				if(img_height > max_height)
				{
					var multiplier = max_height / img_height;
					var new_height = max_height;
					var new_width = img_width * multiplier;
					objImg.width = new_width;
					objImg.height = new_height;
				}
			}
		}
	//}
}