var http = null;
if(window.XMLHttpRequest)
http = new XMLHttpRequest();
else if (window.ActiveXObject)
http = new ActiveXObject('Microsoft.XMLHTTP');

function AddItem(id, form) {
  var BookListID = id;
  if (form == 1) {
    var newQty = $('#quantity1').val();
  } else {
    var newQty = $('#quantity2').val();
  }
  var change_page = 'includes/functions.inc.php';
  if (newQty == 1) {
    var premsg = newQty + ' copy has ';
  } else {
    var premsg = newQty + ' copies have ';
  } 
  $('#added_message').html(premsg + 'been added to your cart.  Thank you!');
  $('#added_message').show('fast');
	$('#added_message').css({'background-color':'#ffe'});
  http.open('POST', change_page, true);
  http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  http.send('BookListID=' + BookListID + '&quantity=' + newQty + '&action=add_item');
  http.onreadystatechange=stateChanged;
  return false;
  function stateChanged() {
    if(http.readyState==4) {
      document.getElementById('top_cart').innerHTML=http.responseText;
    }
  }
}

function UpdateQty(id) {
  var BookListID = id; 
  var newQty = document.forms.order.quantity.value
  var change_page = 'dropchange.php';
  http.open('POST', change_page, true);
  http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  http.send('BookListID=' + BookListID + '&quantity=' + newQty);
  http.onreadystatechange=stateChanged;
  function stateChanged() {
    if(http.readyState==4) {
      document.getElementById('top_cart').innerHTML=http.responseText;
    }
  }
}

function change_cart(do_what) {
  document.forms.cart_form.f.value=do_what;
}

function lengthRestriction(elem, min, max){alert(elem);
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

function validate() {
  if (document.forms.checkout_form.email.value == '') {
    alert ("Please enter an email address!");
    document.forms.checkout_form.email.focus();
    return false;
  }
  if (document.forms.checkout_form.phone.value == '') {
    alert ("Please enter a phone number!");
    document.forms.checkout_form.phone.focus();
    return false;
  }
  if (document.forms.checkout_form.name.value == '') {
    alert ("Please enter your name!");
    document.forms.checkout_form.name.focus();
    return false;
  }
  if (document.forms.checkout_form.address1.value == '') {
    alert ("Please enter an address!");
    document.forms.checkout_form.address1.focus();
    return false;
  }
  if (document.forms.checkout_form.city.value == '') {
    alert ("Please enter a city!");
    document.forms.checkout_form.city.focus();
    return false;
  }
  if (document.forms.checkout_form.state.value == '') {
    alert ("Please enter a state!");
    document.forms.checkout_form.state.focus();
    return false;
  }
  if (document.forms.checkout_form.zip.value == '') {
    alert ("Please enter a zip code!");
    document.forms.checkout_form.zip.focus();
    return false;
  }
/*  
commented out to allow Brits and Canadians to record their codes, 
which are combinations of numbers and letters
  if (document.forms.checkout_form.zip.value != parseInt(document.forms.checkout_form.zip.value)){
    alert("Please enter a numeric value for the zip code!");
    document.forms.checkout_form.zip.focus();
    return false;
  }
*/
  if (document.forms.checkout_form.country.value == '') {
    alert ("Please enter a country!");
    document.forms.checkout_form.country.focus();
    return false;
  }
   if (document.forms.checkout_form.shipping_checkbox.checked == 1) {
    if (document.forms.checkout_form.ship_name.value == '') {
      alert ("Please enter a shipping name!");
      document.forms.checkout_form.ship_name.focus();
      return false;
    }
    if (document.forms.checkout_form.ship_address1.value == '') {
      alert ("Please enter a shipping address!");
      document.forms.checkout_form.ship_address1.focus();
      return false;
    }
    if (document.forms.checkout_form.ship_city.value == '') {
      alert ("Please enter a shipping city!");
      document.forms.checkout_form.ship_city.focus();
      return false;
    }
    if (document.forms.checkout_form.ship_state.value == '') {
      alert ("Please enter a shipping state!");
      document.forms.checkout_form.ship_state.focus();
      return false;
    }
    if (document.forms.checkout_form.ship_zip.value == '') {
      alert ("Please enter a shipping zip code!");
      document.forms.checkout_form.ship_zip.focus();
      return false;
    }
    if (document.forms.checkout_form.ship_zip.value != parseInt(document.forms.checkout_form.ship_zip.value)){
      alert("Please enter a numeric value for the shipping zip code!");
      document.forms.checkout_form.ship_zip.focus();
      return false;
    }
    if (document.forms.checkout_form.ship_country.value == '') {
      alert ("Please enter a shipping country!");
      document.forms.checkout_form.ship_country.focus();
      return false;
    }
  } else {
    
  }
  if (document.forms.checkout_form.acct.value == '') {
    alert ("Please enter a credit card number!");
    document.forms.checkout_form.acct.focus();
    return false;
  }
  if (document.forms.checkout_form.card_name.value == '') {
    alert ("Please enter the name on the credit card!");
    document.forms.checkout_form.card_name.focus();
    return false;
  }
  document.forms.checkout_form.submit();
}

function loadStates(value, qty) {
  $.ajax({
    type: "POST",
    url: "loadshipping.php",
    data: "US=" + value + "&qty=" + qty,
    success: function(html){
      $("#ship_select").html(html);
    }
  });
}

function loadShipTotal(value,qty) {
  if (value == 'select') {
    alert ("No selection made!");
    return false;
  }
  $.ajax({
    type: "POST",
    url: "loadshipping.php",
    data: "ship=" + value + "&qty=" + qty,
    success: function(html){
      $("#ship_select").html(html);
      $("#ship_charge").show('slow');
    }
  });
  return false;
}
function continue2checkout(amt) {
	$.ajax({
		type: "POST",
		url: "loadshipping.php",
		data: "amt=" + amt,
		success: function(){
			location.href='checkout.php';
		}
	});
	return false;
}

function toggle_shipping() {
	if ($('#shipping_info').css('display')== 'inline') {
		$('#shipping_info').css({display:'none'});
	} else {
		$('#shipping_info').css({display:'inline'});
	}
}
function order(email,phone,name,address,city,state,zip,country,ship_name,ship_address,ship_city,ship_state,ship_zip,ship_country,card_type,card_acct,card_exp,card_name,total_charge) {
	/*	$.ajax({
    type: "POST",
    url: "/includes/po.php",
    data: "email=" + email + "&phone=" + phone + "&name=" + name + "&address=" + address + "&city=" + city + "&state=" + state + "&zip=" + zip + "&country=" + country + "&ship_name=" + ship_name + "&ship_address=" + ship_address + "&ship_city=" + ship_city + "&ship_state=" + ship_state + "&ship_zip=" + ship_zip + "&ship_country=" + ship_country + "&card_type=" + card_type + "&card_acct=" + card_acct + "&card_acct=" + card_acct + "&card_acct=" + card_acct + "&card_exp=" + card_exp + "&card_name=" + card_name,
    success: function(){
      location.href='/checkout.php';
    }
  });
	return false;*/
	
	var dataString = "email=" + email + "&phone=" + phone + "&name=" + name + "&address=" + address + "&city=" + city + "&state=" + state + "&zip=" + zip + "&country=" + country + "&ship_name=" + ship_name + "&ship_address=" + ship_address + "&ship_city=" + ship_city + "&ship_state=" + ship_state + "&ship_zip=" + ship_zip + "&ship_country=" + ship_country + "&card_type=" + card_type + "&card_acct=" + card_acct + "&card_exp=" + card_exp + "&card_name=" + card_name + "&total_charge=" + total_charge;
	//alert(dataString);return false;
		var message = $.ajax({
		   type: "POST",
		   url: "includes/po.php",
		   data: dataString,
	   	   async: false 
		}).responseText;
	if (parseInt(message) != 0){
	  $('#mainContent2').hide();
		$('#mainContent2').html(message);
		$('#mainContent2').fadeIn('slow');
  }
  return false;
}


function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function validEmaila(){
	var txt_email 	= document.emailform.email;
	var trim_email 	= trim(txt_email.value);
	var email_regex = /^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.(([A-Za-z]{2,3})|(aero|coop|info|museum|name))$/;
	if(!email_regex.test(trim_email))
	{
		if(txt_email.value == ""){
			alert("Please enter an email address.");
			txt_email.focus();
		}
		else{
			alert("The email address you entered is not valid.");
			txt_email.focus();
		}
		return false;
	}
	return true;
}
