var site = "http://www.rgbprojectorlamps.com/";

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 get_brands(secure)
{
	// reset the site variable if we are on a secure connection
	if(secure)
		site = "https://www.rgbprojectorlamps.com/";
	
	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');\">";
				brand_select += "<option value=\"0\" selected=\"selected\">Select a Brand</option>";
				brand_select += req.responseText;
				brand_select += "</select><br />";
				brand_select += "<span class=\"header-loading\" id=\"projector-models\">&nbsp;</span>";				
				document.getElementById('projector-brand-span').innerHTML = brand_select;

				var lamp_select = "<select class=\"header-select\" id=\"lamp-brand\" name=\"lamp-brand\" onchange=\"get_data(this.value, 'lamp');\">";
				lamp_select += "<option value=\"0\" selected=\"selected\">Select a Brand</option>";
				lamp_select += req.responseText;
				lamp_select += "</select><br />";
				lamp_select += "<span class=\"header-loading\" id=\"lamp-models\">&nbsp;</span>";
				document.getElementById('projector-lamp-span').innerHTML = lamp_select;
			}
			else
				alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
	req.open("GET", site + "ajax/get-brands.php?ms=" + new Date().getTime(), true);
	req.send("");	
}

function get_data(brand_id, type)
{
	if(type == "projector" || type == "lamp")
	{
		var req = get_request_obj();

		document.getElementById(type + '-models').innerHTML = "Loading...";

		req.onreadystatechange = function()
		{
			if (req.readyState == 4)
			{
				if (req.status == 200)
				{
					//var dynDiv = document.getElementById('dynamic_div');
					//dynDiv.innerHTML = req.responseText;
					document.getElementById(type + '-models').innerHTML = req.responseText;
					//alert(req.responseText);
				}
				else
					alert("There was a problem retrieving the XML data:\n" + req.statusText);
			}
		}
		req.open("GET", site + "ajax/get-data.php?ms=" + new Date().getTime() + "&brand_id=" + brand_id + "&type=" + type, true);
		req.send("");
	}
}

function addToCart(action, screen)
{
	
	document.Form1.action = action;
	if(document.Form1.Secure)
		document.Form1.Secure.value = "";
	document.Form1.SCREEN.value = screen;
	document.Form1.ACTION.value = "ADD_TO_CART";
	document.Form1.submit(); // Submit the page
	return true;
}

function OnButton2()
{
	document.Form1.action = "https://www.paypal.com/cgi-bin/webscr";
	document.Form1.submit();			// Submit the page
	return true;
}

function update_shipping(shipping, price)
{
	var new_shipping = parseFloat(shipping.value).toFixed(2);
	var new_total = (parseFloat(shipping.value) + parseFloat(price)).toFixed(2);
	document.getElementById("shipping_td").innerHTML = "USD $" + new_shipping;
	document.getElementById("total_td").innerHTML = "USD $" + new_total;
	document.getElementById("new_shipping").value = new_shipping;
}

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.value < " ") {
		alert("Please enter the country.");
		f.inputCountry.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;
		}
		
	}
	
	
	
}

/*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 == "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>";

	}

	else
	{
		var grandTotalDiv = document.getElementById('grandTotalDiv');
		var beforeTax = document.getElementById('totalPriceForGST').value;

		grandTotalDiv.innerHTML = "<span style='font-family: arial; sans-serif; font-size: 14px;'><strong>Grand Total:</strong>&nbsp;&nbsp;&nbsp;USD "
					+ (parseFloat(beforeTax)) + "</span>";
	}
	
	status = "update";
}
