var remove_url = baseurl + "includes/remove_from_cart.php?system_id="; 
var same_as_billing_url = baseurl + "includes/same_as_billing.php";
var http_request = false;
var http = getHTTPObject(); 
var state = "";
var style;
var display;

// We create the HTTP Object

// Output response back to form. The server-side script
function handleHttpResponse() {
  if (http.readyState == 4) {
    // Split the comma delimited response into an array
    results = http.responseText.split(",");
	var remove = results[0];

	if (remove=="1")
	{
		window.location = getUrl();
	}
  }      
}

function closeToggledDivs(id) {
	
	var e = document.getElementById(id);
	
	if(e.style.display == 'none') {
		e.style.display = 'block';
	} else {
		e.style.display = 'none';
	}

}

function toggleVisibility(id) {

var e = document.getElementById(id);

if(e.style.display == 'none') {
    e.style.display = 'block';
} else {
    e.style.display = 'none';
}
}

function calculate_shipping()
{	
	//var state = document.getElementById("calculate_shipping").value;

	document.getElementById("calculate_shipping").value = "1";	

	
	if (state=="")
	{
		document.getElementById("calculate_shipping").value = "1";	
	}	
	if (state=="1")
	{
		document.getElementById("calculate_shipping").value = "";	
	}	
}

function enable_coupon()
{
	document.getElementById("process_coupon").value = "1";	
}

function process_login()
{
	document.getElementById("process_login").value = "1";	
}


function checkscript()
{		
	/* BEGIN Check to see if the customer has filled in the password boxes */
	var register_details_valid=false;
	var passwords_match=false;
	var errors="";
	
	if ((document.getElementById("txtpassword"))&&(document.getElementById("txtconfirm_password")))
	{
		if ((document.checkout.txtpassword.value!="")||(document.checkout.txtconfirm_password.value!=""))
		{
			register_details_valid=true;
			if (document.checkout.txtpassword.value=="") 
			{
				errors=errors+"Password missing\n";	
			}
			if (document.checkout.txtconfirm_password.value=="")
			{
				errors=errors+"Confirm password missing\n";	
			}
			
			if(document.checkout.txtpassword.value==document.checkout.txtconfirm_password.value)
				passwords_match=true;
			else
				errors="Unable to save your details\n"+errors+"Passwords don't match\n\n";
		}
	}
	/* END Check to see if the customer has filled in the password boxes */
	
	
	
	//var errors="";
	var billing_first_name = document.getElementById("billing_first_name").value;	
	if (billing_first_name=="")
	{
		errors=errors+"Billing first name missing\n";
	}
	
	var billing_last_name = document.getElementById("billing_last_name").value;	
	if (billing_last_name=="")
	{
		errors=errors+"Billing last name missing\n";
	}
	
	var email_address = document.getElementById("email_address").value;	
	if (email_address=="")
	{
		errors=errors+"Email address missing\n";
	}
	
	var billing_telephone = document.getElementById("billing_telephone").value;	
	if (billing_telephone=="")
	{
		errors=errors+"Billing telephone missing\n";
	}
	
	var billing_street_address1 = document.getElementById("billing_street_address1").value;	
	if (billing_street_address1=="")
	{
		errors=errors+"Billing street address 1 missing\n";
	}
	
	
	var billing_county_state = document.getElementById("billing_county_state").value;	
	if (billing_county_state=="")
	{
		errors=errors+"Billing county state missing\n";
	}
	
	var billing_postcode_zipcode = document.getElementById("billing_postcode_zipcode").value;	
	if (billing_postcode_zipcode=="")
	{
		errors=errors+"Billing postcode zipcode missing\n";
	}
	
	
	
	var shipping_first_name = document.getElementById("shipping_first_name").value;	
	if (shipping_first_name=="")
	{
		errors=errors+"\nShipping first name missing\n";
	}
	
	var shipping_last_name = document.getElementById("shipping_last_name").value;	
	if (shipping_last_name=="")
	{
		errors=errors+"Shipping last name missing\n";
	}
	
	var shipping_telephone = document.getElementById("shipping_telephone").value;	
	if (shipping_telephone=="")
	{
		errors=errors+"Shipping telephone missing\n";
	}
	
	var shipping_street_address1 = document.getElementById("shipping_street_address1").value;	
	if (shipping_street_address1=="")
	{
		errors=errors+"Shipping street address 1 missing\n";
	}
	
	var shipping_county_state = document.getElementById("shipping_county_state").value;	
	if (shipping_county_state=="")
	{
		errors=errors+"Shipping county state missing\n";
	}
	
	var shipping_postcode_zipcode = document.getElementById("shipping_postcode_zipcode").value;	
	if (shipping_postcode_zipcode=="")
	{
		errors=errors+"Shipping postcode zipcode missing\n";
	}
	
	if (errors=="")
	{
		return true;
	}
	else
	{
		alert(errors);
		return false;
	}
}


function same_as_billing()
{		
	/** Check to see if all the billing fields have been completed  **/
	var errors="";
	var billing_first_name = document.getElementById("billing_first_name").value;	
	if (billing_first_name=="")
	{
		errors="Billing first name missing\n";
	}
	
	var billing_last_name = document.getElementById("billing_last_name").value;	
	if (billing_last_name=="")
	{
		errors=errors+"Billing last name missing\n";
	}
	
	var email_address = document.getElementById("email_address").value;	
	if (email_address=="")
	{
		errors=errors+"Email address missing\n";
	}
	
	var billing_telephone = document.getElementById("billing_telephone").value;	
	if (billing_telephone=="")
	{
		errors=errors+"Billing telephone missing\n";
	}
	
	var billing_street_address1 = document.getElementById("billing_street_address1").value;	
	if (billing_street_address1=="")
	{
		errors=errors+"Billing street address 1 missing\n";
	}
	
	var billing_county_state = document.getElementById("billing_county_state").value;	
	if (billing_county_state=="")
	{
		errors=errors+"Billing county state missing\n";
	}
	
	var billing_postcode_zipcode = document.getElementById("billing_postcode_zipcode").value;	
	if (billing_postcode_zipcode=="")
	{
		errors=errors+"Billing postcode zipcode missing\n";
	}
	/** END Check to see if all the billing fields have been completed  **/

	if (errors!="")
	{
		document.getElementById("_same_as_billing").checked = false;
		alert(errors);
	}
	else
	{
	
		var billing_ticked = document.getElementById("_same_as_billing").value;	
		
		if (billing_ticked=="")
		{
			document.getElementById("_same_as_billing").value = "1";
			
			// Get billing title
			var billing_title = document.getElementById("billing_title").value;	
			document.getElementById("shipping_title").value = billing_title;
			
			// Get billing first name
			var billing_first_name = document.getElementById("billing_first_name").value;	
			document.getElementById("shipping_first_name").value = billing_first_name;
			
			// Get billing last name
			var billing_last_name = document.getElementById("billing_last_name").value;	
			document.getElementById("shipping_last_name").value = billing_last_name;
			
			// Get billing telephone
			var billing_telephone = document.getElementById("billing_telephone").value;	
			document.getElementById("shipping_telephone").value = billing_telephone;
		
			// Get billing street address 1
			var billing_street_address1 = document.getElementById("billing_street_address1").value;	
			document.getElementById("shipping_street_address1").value = billing_street_address1;
			
			// Get billing street address 2
			var billing_street_address2 = document.getElementById("billing_street_address2").value;	
			document.getElementById("shipping_street_address2").value = billing_street_address2;
			
			// Get billing street address 3
			var billing_street_address3 = document.getElementById("billing_street_address3").value;	
			document.getElementById("shipping_street_address3").value = billing_street_address3;
			
			// Get billing country state
			var billing_country = document.getElementById("billing_country").value;	
			document.getElementById("shipping_country").value = billing_country;
			
			// Get billing county state
			var billing_county_state = document.getElementById("billing_county_state").value;	
			document.getElementById("shipping_county_state").value = billing_county_state;
			
			// Get billing postcode zipcode
			var billing_postcode_zipcode = document.getElementById("billing_postcode_zipcode").value;	
			document.getElementById("shipping_postcode_zipcode").value = billing_postcode_zipcode;
		}
		if (billing_ticked=="1")
		{
			document.getElementById("_same_as_billing").value = "";
			
			document.getElementById("shipping_first_name").value = "";		
			document.getElementById("shipping_last_name").value = "";		
			document.getElementById("shipping_telephone").value = "";
			document.getElementById("shipping_street_address1").value = "";
			document.getElementById("shipping_street_address2").value = "";
			document.getElementById("shipping_street_address3").value = "";
			document.getElementById("shipping_county_state").value = "";
			document.getElementById("shipping_postcode_zipcode").value = "";
		}
	}
	
	// Update 'Same as Billing' session state
	http.open("GET", same_as_billing_url, true);
  	http.onreadystatechange = handleHttpResponse;
  	http.send(null);   
}



function remove(system_id,cart_id)
{	
	http.open("GET", remove_url + system_id + "&&cart_id=" + cart_id, true);
  	http.onreadystatechange = handleHttpResponse;
  	http.send(null);      
}

function store_credit()
{
	document.getElementById("redeem_credit").value = "1";	
}

function update_quantities()
{
	document.getElementById("update_quantity").value = "1";	
}


/*** These functions are for the Checkout (invoice/shipping) page ***/

function closeToggledDivs(state) 
{
	if (state=="1")
	{
		var e = document.getElementById(1);
		
		if(e.style.display == 'none') {
			e.style.display = 'block';
		} else {
			e.style.display = 'none';
		}
	}

}

function toggleVisibility(id) {
var e = document.getElementById(id);

	if(document.getElementById(id))
	{
		if(e.style.display == 'none') {
			e.style.display = 'block';
		} else {
			e.style.display = 'none';
		}
	}
}

function enable_process()
{
	document.getElementById("process").value = "1";
}

/*** These functions are for the Checkout (invoice/shipping) page ***/



function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  
 
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

