var site = "http://www.myprojectorlamps.eu/";

function get_request_obj()
{
	var req = false;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest)
	{
		try
		{
			req = new XMLHttpRequest();
		}
		catch(e)
		{
			req = false;
		}
	}

	// branch for IE/Windows ActiveX version
	else if(window.ActiveXObject)
	{
		try
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				req = false;
			}
		}
	}

	if(req) 
		return req;
	else
		return false;
}

function in_array(needle, haystack) { 
    var length = haystack.length; 
    for(var i = 0; i < length; i++) { 
        if(haystack[i] == needle) return true; 
    } 
    return false; 
}

function update_shipping_gst(ctry_name,qty,price,discount,currency){
	var ship_ctry_arr = Array('UK','FR','DE','LU','BE','DK','IT','CZ','CY','BG','HU','RO');
	var vat_ctry_arr = Array('AT','BE','BG','CY','CZ','DK','EE','FI','FR','DE','GR','HU','IE','IT','LV','LT','LU','MT','NL','PL','PT','RO','SK','SI','SI','ES','SE','UK');

	for(i = 0;i < ship_ctry_arr.length; i++){
		document.getElementById('shipping_'+ship_ctry_arr[i]).style.display = "none";
		document.getElementById('billing_'+ship_ctry_arr[i]).style.display = "none";
		}
	
	//document.getElementById('GST_STRING_TD').style.display = "none";
	//document.getElementById('GST_TD').style.display = "none";
	
	
	var url = "get_shipping.php";
	var http = get_request_obj();
	
	if(document.getElementById("inputAltShip").checked && ( document.set_ship.inputShipFirstName.value != "" || document.set_ship.inputShipLastName.value != "" || document.set_ship.inputShipAddress.value != "" || document.set_ship.inputShipCity.value != "" || document.set_ship.inputShipZIP.value != "" || document.set_ship.inputShipCountry_cart.value != "") ){
		ctry_name = document.set_ship.inputShipCountry_cart.value;
		ctry_name_substr = ctry_name.substr(ctry_name, 2);
		if( in_array(ctry_name_substr, ship_ctry_arr) ){
			document.getElementById('shipping_'+ctry_name_substr).style.display = "block";
			document.getElementById('billing_'+ctry_name_substr).style.display = "none";
			ctry_name = document.set_ship['shipping_'+ctry_name_substr].value;
			}
		}
	else{
		ctry_name_substr = ctry_name.substr(ctry_name, 2);
		if( in_array(ctry_name_substr, ship_ctry_arr) ){
			document.getElementById('shipping_'+ctry_name_substr).style.display = "none";
			document.getElementById('billing_'+ctry_name_substr).style.display = "block";
			ctry_name = document.set_ship['billing_'+ctry_name_substr].value;
			}
		}
		
	
	
	var params =  "cty_id=" + ctry_name + "&quantity=" + qty;
	var site1 = "http://www.myprojectorlamps.eu/";
	http.open("POST", site1 + "ajax/get-shipping.php", true);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	http.onreadystatechange = function() {//Call a function when the state changes.
		if(http.readyState == 4 && http.status == 200) {
			eval( http.responseText);
			
			//alert(http.responseText);
			var new_shipping = parseFloat(return_array[1]).toFixed(2);
			var new_total = (parseFloat(return_array[1]) + parseFloat(price) -  ( parseFloat(price)*parseFloat(discount)/100) ).toFixed(2) ;
			var new_currency = parseFloat(return_array[1] + parseFloat(price*(1-parseFloat(discount)/100)))*parseFloat(currency);
		
			document.getElementById("shipping_td").innerHTML =  "EUR €" + new_shipping;
			document.set_ship.shipping_type.value = return_array[0];
			document.set_ship.shipping.value = return_array[1];
			//document.getElementById("total_td").innerHTML = "EUR €" + new_total;
			
			var country  = return_array[2];
			if( in_array(document.set_ship.inputCountry_cart.value, vat_ctry_arr) ){
				var beforeTax = parseFloat(new_total).toFixed(2);
				document.getElementById('GST_STRING_TD').style.display = "none";
				document.getElementById('GST_TD').style.display = "none";
				document.getElementById("total_td").innerHTML = "<strong>EUR €"+ (Math.round((parseFloat(beforeTax) )*100)/100).toFixed(2) + "</strong>";	
				if(document.set_ship.inputCountry_cart.value == 'NL'){
					var GST = Math.round((beforeTax * .19)*100)/100;
					document.getElementById('GST_STRING_TD').innerHTML = "<strong><a href='http://www.myprojectorlamps.eu/explain_vat.htm' TARGET='detail' onClick='popupwindowwidth(\"http://www.myprojectorlamps.eu/explain_vat.htm\",\"550\"); return false;'><img align='absmiddle' src='http://www.myprojectorlamps.com/images/images_eu/info_logo.gif'></a> VAT 19%:</strong>";
					document.getElementById('GST_TD').innerHTML = "<strong>EUR €" + GST.toFixed(2)+ "</strong>";
					document.getElementById('GST_STRING_TD').style.display = "block";
					document.getElementById('GST_TD').style.display = "block";
					document.getElementById("total_td").innerHTML = "<strong>EUR €"+ (Math.round((parseFloat(beforeTax) + parseFloat(GST))*100)/100).toFixed(2) + "</strong>";
					new_currency = new_currency + (GST*parseFloat(currency));
					}
				if(document.set_ship.inputVAT.value == ""){
					var GST = Math.round((beforeTax * .19)*100)/100;
					document.getElementById('GST_STRING_TD').innerHTML = "<strong><a href='http://www.myprojectorlamps.eu/explain_vat.htm' TARGET='detail' onClick='popupwindowwidth(\"http://www.myprojectorlamps.eu/explain_vat.htm\",\"550\"); return false;'><img align='absmiddle' src='http://www.myprojectorlamps.com/images/images_eu/info_logo.gif'></a> VAT 19%:</strong>";
					document.getElementById('GST_TD').innerHTML = "<strong>EUR €" + GST.toFixed(2)+ "</strong>";
					document.getElementById('GST_STRING_TD').style.display = "block";
					document.getElementById('GST_TD').style.display = "block";
					document.getElementById("total_td").innerHTML = "<strong>EUR €"+ (Math.round((parseFloat(beforeTax) + parseFloat(GST))*100)/100).toFixed(2) + "</strong>";
					new_currency = new_currency + (GST*parseFloat(currency));
					}
				}
			else if(document.set_ship.inputCountry_cart.value == "CA"){
				var beforeTax = parseFloat(new_total).toFixed(2);
				var GST = Math.round((beforeTax * 0.05)*100)/100;
				document.getElementById('GST_STRING_TD').innerHTML = "<strong>GST 5%:</strong>";
				document.getElementById('GST_TD').innerHTML = "<strong>EUR €" + GST.toFixed(2)+ "</strong>";
				document.getElementById('GST_STRING_TD').style.display = "block";
				document.getElementById('GST_TD').style.display = "block";
				document.getElementById("total_td").innerHTML = "<strong>EUR €"+ (Math.round((parseFloat(beforeTax) + parseFloat(GST))*100)/100).toFixed(2) + "</strong>";
				
				new_currency = new_currency + (GST*parseFloat(currency));
				}
			else{
				document.getElementById('GST_STRING_TD').style.display = "none";
				document.getElementById('GST_TD').style.display = "none";
				document.getElementById("total_td").innerHTML = "EUR €" + new_total;
				}
			document.getElementById("currency_td").innerHTML = "<strong>GBP £" + new_currency +"</Strong>";
			//status = "update";
		}
	}
	http.send(params);
	}
	
function get_brands(secure,type)
{
	// reset the site variable if we are on a secure connection
	if(secure)
		site = "https://www.myprojectorlamps.eu/";
	
	var req = get_request_obj();

	req.onreadystatechange = function()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				var brand_select = "<select class=\"header-select\" id=\"projector-brand\" name=\"projector-brand\" onchange=\"get_data(this.value, 'projector');\">";
				var lamp_select = "<select class=\"header-select\" id=\"lamp-brand\" name=\"lamp-brand\" onchange=\"get_data(this.value, 'lamp');\">";
				var rptv_brand_select = "<select class=\"header-select\" id=\"rptvprojector-brand\" name=\"rptvprojector-brand\" onchange=\"get_data(this.value, 'rptvprojector');\">";
				var rptv_lamp_select = "<select class=\"header-select\" id=\"rptvlamp-brand\" name=\"rptvlamp-brand\" onchange=\"get_data(this.value, 'rptvlamp');\">";
				
				brand_select += "<option value=\"0\" selected=\"selected\">Select a Brand</option>";
				brand_select += req.responseText;
				brand_select += "</select><br />";
				
				rptv_brand_select += "<option value=\"0\" selected=\"selected\">Select a Brand</option>";
				rptv_brand_select += req.responseText;
				rptv_brand_select += "</select><br />";
				
				brand_select += "<span class=\"header-loading\" id=\"projector-models\">&nbsp;</span>";					
				rptv_brand_select += "<span class=\"header-loading\" id=\"rptvprojector-models\">&nbsp;</span>";
				
				if(type == 'projector'){
					document.getElementById('projector-brand-span').innerHTML = brand_select;
					}
				if(type == 'rptvprojector'){
					document.getElementById('rptv_projector-brand-span').innerHTML = rptv_brand_select;
					}

				
				lamp_select += "<option value=\"0\" selected=\"selected\">Select a Brand</option>";
				lamp_select += req.responseText;
				lamp_select += "</select><br />";
				
				rptv_lamp_select += "<option value=\"0\" selected=\"selected\">Select a Brand</option>";
				rptv_lamp_select += req.responseText;
				rptv_lamp_select += "</select><br />";
				
				lamp_select += "<span class=\"header-loading\" id=\"lamp-models\">&nbsp;</span>";					
				rptv_lamp_select += "<span class=\"header-loading\" id=\"rptvlamp-models\">&nbsp;</span>";
				
				if(type == 'lamp'){
					document.getElementById('projector-lamp-span').innerHTML = lamp_select;
					}
				if(type == 'rptvlamp'){
					document.getElementById('rptv_projector-lamp-span').innerHTML = rptv_lamp_select;
					}
				
			}
			else
				var stek = 0;
		}
	}
	req.open("GET", site + "ajax/get-brands.php?ms=" + new Date().getTime(), true);
	req.send("");	
}

function get_data(brand_id, type)
{
	document.getElementById(type + '-models').innerHTML = "Loading...";
	if(type == "projector" || type == "lamp" || type == "rptvprojector" || type == "rptvlamp")
	{
	//	var req = get_request_obj();
		//document.getElementById(type + '-models').innerHTML = "Loading...";

		//req.onreadystatechange = function()
		//{
		//	if (req.readyState == 4)
		//	{
			//	if (req.status == 200)
			//	{
				//	alert(req.responseText.size);
			////		document.getElementById(type + '-models').innerHTML = req.responseText;
				//	}
			//	else
				//	alert("There was a problem retrieving the XML data:\n" + req.statusText);
			//}
		//}
		//req.open("POST", site + "ajax/get-data.php?ms=" + new Date().getTime() + "&brand_id=" + brand_id + "&type=" + type, true);
		//req.send("");
		var url = "get_data.php";
		var http = get_request_obj();
		var params =  "brand_id=" + brand_id + "&type=" + type;
		http.open("POST", site + "ajax/get-data.php", true);
		
		//Send the proper header information along with the request
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
		
		http.onreadystatechange = function() {//Call a function when the state changes.
			if(http.readyState == 4 && http.status == 200) {
				//alert(http.responseText);
			document.getElementById(type + '-models').innerHTML = http.responseText;
				//alert(http.responseText);
			}
		}
		http.send(params);

	}
}



function addToCart(action, screen,form_name,qty)
{
	//
	if(qty <= 0){
		alert("Please Enter a Number > 0 in The Quantity Field");
		return false;
		}
	form_name.action = action;
	//alert(form_name.action);
	//if(document.form_name.Secure == true)
	//  form_name.Secure.value = "";
	form_name.SCREEN.value = screen;
	form_name.ACTION.value = "ADD_TO_CART";
	form_name.submit(); // Submit the page
	return true;
}

function OnButton2()
{
	document.Form1.auto_submit.value = '1';
	return true;
}

function update_shipping(shipping, price,currency_rate,discount)
{
	var new_shipping = parseFloat(shipping.value).toFixed(2);
	var new_total = (parseFloat(shipping.value) + parseFloat(price) -  ( parseFloat(price)*parseFloat(discount)/100) ).toFixed(2) ;

	document.getElementById("shipping_td").innerHTML =  "EUR €" + new_shipping;
	document.getElementById("total_td").innerHTML = "EUR €" + new_total;
	
}

function checkbillinginfo(f) {
	if (f.inputFirstName.value < " ") {
		alert("Please enter the first name on your credit card.");
		f.inputFirstName.focus();
		return false;
	}

	if (f.inputLastName.value < " ") {
		alert("Please enter the last name on your credit card.");
		f.inputLastName.focus();
		return false;
	}

	if (f.inputAddress.value < " ") {
		alert("Please enter your address.");
		f.inputAddress.focus();
		return false;
	}

	if (f.inputCity.value < " ") {
		alert("Please enter the city.");
		f.inputCity.focus();
		return false;
	}

	if (f.inputState.value < " ") {
		alert("Please enter the state.");
		f.inputState.focus();
		return false;
	}

	if (f.inputCountry_cart.value < " ") {
		alert("Please enter the country.");
		f.inputCountry_cart.focus();
		return false;
	}


	if (f.inputZIP.value < " ") {
		alert("Please enter the zip code.");
		f.inputZIP.focus();
		return false;
	}

	if (f.inputEmail.value < " ") {
		alert("Please enter your e-mail address.");
		f.inputEmail.focus();
		return false;
	}

	/*if(f.inputAltShip[0].checked == true)
	{
		if(f.inputShipFirstName.value == "")
		{
			alert("Please enter your Shipping First Name");
			return false;
		}

		if(f.inputShipLastName.value == "")
		{
			alert("Please enter your Shipping Last Name");
			return false;
		}

		if(f.inputShipAddress.value == "")
		{
			alert("Please enter your Shipping Address");
			return false;
		}

		if(f.inputShipCity.value == "")
		{
			alert("Please enter your Shipping City");
			return false;
		}

		if(f.inputShipState.value < " ")
		{
			alert("Please enter your Shipping State / Province");
			return false;
		}

		if(f.inputShipCountry.value < " ")
		{
			alert("Please enter your Shipping State / Province");
			return false;
		}

		if(f.inputShipZIP.value == "")
		{
			alert("Please enter your Shipping Zip / Postal Code");
			return false;
		}
		
	}*/
	
return true;	
	
}

/*REMOVED AND REPLACE BY CODE IN makeCountrySelector, as this doesn't work in FF
document.body.onload = load;
function load()
{
	//document.getElementById('inputCountry').addEventListener('onchange' = updateGST;
	
	//document.all["inputCountry"].onchange = updateGST;
}*/

function updateGST(countrySelector)
{
	if(countrySelector.value == "NL")
	{
		var beforeTax = document.getElementById('totalPriceForGST').value;
		var GST = Math.round((document.getElementById('totalPriceForGST').value * .19)*100)/100;
		var grandTotalDiv = document.getElementById('grandTotalDiv');

	//	grandTotalDiv.innerHTML = "<span style='font-family: arial; sans-serif; font-size: 14px;'><span style='color: red;'><strong>GST:</strong>&nbsp;&nbsp;&nbsp;USD " + GST + "</span><br />" +
					//"<strong>Grand Total:&nbsp;&nbsp;&nbsp;</strong>USD "
					//+ (Math.round((parseFloat(beforeTax) + parseFloat(GST))*100)/100) + "</span>";
		document.getElementById('GST_STRING_TD').innerHTML = "<strong>VAT 19%:</strong>";
		document.getElementById('GST_TD').innerHTML = "<strong>EUR €" + GST.toFixed(2)+ "</strong>";
		
		document.getElementById('GST_STRING_TD').style.display = "block";
		document.getElementById('GST_TD').style.display = "block";
		
		grandTotalDiv.innerHTML = "<strong>EUR €"+ (Math.round((parseFloat(beforeTax) + parseFloat(GST))*100)/100).toFixed(2) + "</strong>";

	}
	else if(countrySelector.value == "CA")
	{
		var beforeTax = document.getElementById('totalPriceForGST').value;
		var GST = Math.round((document.getElementById('totalPriceForGST').value * .05)*100)/100;
		var grandTotalDiv = document.getElementById('grandTotalDiv');

	//	grandTotalDiv.innerHTML = "<span style='font-family: arial; sans-serif; font-size: 14px;'><span style='color: red;'><strong>GST:</strong>&nbsp;&nbsp;&nbsp;USD " + GST + "</span><br />" +
					//"<strong>Grand Total:&nbsp;&nbsp;&nbsp;</strong>USD "
					//+ (Math.round((parseFloat(beforeTax) + parseFloat(GST))*100)/100) + "</span>";
		document.getElementById('GST_STRING_TD').innerHTML = "<strong>GST:</strong>";
		document.getElementById('GST_TD').innerHTML = "<strong>EUR €" + GST.toFixed(2)+ "</strong>";
		
		document.getElementById('GST_STRING_TD').style.display = "block";
		document.getElementById('GST_TD').style.display = "block";
		
		grandTotalDiv.innerHTML = "<strong>EUR €"+ (Math.round((parseFloat(beforeTax) + parseFloat(GST))*100)/100).toFixed(2) + "</strong>";

	}
	else
	{
		var grandTotalDiv = document.getElementById('grandTotalDiv');
		var beforeTax = document.getElementById('totalPriceForGST').value;
		
		document.getElementById('GST_STRING_TD').style.display = "none";
		document.getElementById('GST_TD').style.display = "none";
		
		//document.getElementById('GST_TD').style.display = "none";
		grandTotalDiv.innerHTML = "<strong>EUR €"+ (parseFloat(beforeTax).toFixed(2)) + "</strong>";
	}
	
	status = "update";
}

function checkresellerinfo(f) {
	
	if (f.inputEmail.value < " ") {
		alert("Please enter your e-mail address.");
		f.inputEmail.focus();
		return false;
	}
	
	if (f.inputpassword.value < " ") {
		alert("Please enter your password.");
		f.inputpassword.focus();
		return false;
	}
	
	if (f.inputpasswordver.value < " ") {
		alert("Please retype your password.");
		f.inputpasswordver.focus();
		return false;
	}
	
	if (f.inputpasswordver.value != f.inputpassword.value) {
		alert("Invalid password confirmation.");
		f.inputpassword.focus();
		return false;
	}
	
	if (f.inputFirstName.value < " ") {
		alert("Please enter the first name on your credit card.");
		f.inputFirstName.focus();
		return false;
	}

	if (f.inputLastName.value < " ") {
		alert("Please enter the last name on your credit card.");
		f.inputLastName.focus();
		return false;
	}

	if (f.inputAddress.value < " ") {
		alert("Please enter your address.");
		f.inputAddress.focus();
		return false;
	}

	if (f.inputCity.value < " ") {
		alert("Please enter the city.");
		f.inputCity.focus();
		return false;
	}

	if (f.inputState.value < " ") {
		alert("Please enter the state.");
		f.inputState.focus();
		return false;
	}

	if (f.inputShipCountry.value < " ") {
		alert("Please enter the country.");
		f.inputShipCountry.focus();
		return false;
	}


	if (f.inputZIP.value < " ") {
		alert("Please enter the zip code.");
		f.inputZIP.focus();
		return false;
	}
	
	if (f.inputPhone.value < " ") {
		alert("Please enter your phone number.");
		f.inputPhone.focus();
		return false;
	}
	
	/*if (f.cardNumber.value < " ") {
		alert("Please enter your complete card number.");
		f.cardNumber.focus();
		return false;
	}
	

	var cc_date_pattern = /^[0-9][0-9][0-9][0-9][\-][0-9][0-9]$/;

	var cc_date_result;

	cc_date_result = cc_date_pattern.exec(f.expirationDate.value);

		if (cc_date_result) {

			// good to go!

		}

		else {

			alert("That is not a valid expiration date.\nExample: 2008-05 -- YYYY-MM.");

			f.expirationDate.focus();

			return false;

		}
		
	if (f.cardCode.value < " " ||  f.cardCode.value.length < 3 ) {
		alert("Please enter your card code.");
		f.cardCode.focus();
		return false;
	}*/
	
}

function check_u_p(f){

   if(f.username.value.length < 1 ){
      alert("Please type your Email.");
      f.username.value = "";
      f.username.focus();
      return false;
      }

   if(f.password.value.length < 1){
      alert("Please type your Password.");
      f.password.value = "";
      f.password.focus();
      return false;
      }

   return true;

   }
