function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}
function validate() {
  var okSoFar2=true 
  if (document.purchase.first_name.value=="") {
    okSoFar2=false
    alert("Please fill in the First Name field.")
	document.purchase.first_name.focus()
  }
    if (document.purchase.last_name.value=="") {
    okSoFar2=false
    alert("Please fill in the Last Name field.")
	document.purchase.last_name.focus()
  }
  if (document.purchase.address.value=="") {
    okSoFar2=false
    alert("Please fill in the Home Address field.")
	document.purchase.address.focus()
  }
  if (document.purchase.city.value=="") {
    okSoFar2=false
    alert("Please fill in the City field.")
	document.purchase.city.focus()
  }
  if (document.purchase.state.value=="") {
    okSoFar2=false
    alert("Please fill in the State field.")
	document.purchase.state.focus()
  }
  if (document.purchase.zip.value=="") {
    okSoFar2=false
    alert("Please fill in the ZIP field.")
	document.purchase.zip.focus()
  }
  if (document.purchase.country.value=="") {
    okSoFar2=false
    alert("Please fill in the Country field.")
	document.purchase.country.focus()
  }

  if (document.purchase.ship_first_name.value=="") {
    okSoFar2=false
    alert("Please fill in the First Name field.")
	document.purchase.ship_first_name.focus()
  }
    if (document.purchase.ship_last_name.value=="") {
    okSoFar2=false
    alert("Please fill in the Last Name field.")
	document.purchase.ship_last_name.focus()
  }
  if (document.purchase.ship_address.value=="") {
    okSoFar2=false
    alert("Please fill in the Home Address field.")
	document.purchase.ship_address.focus()
  }
  if (document.purchase.ship_city.value=="") {
    okSoFar2=false
    alert("Please fill in the City field.")
	document.purchase.ship_city.focus()
  }
  if (document.purchase.ship_state.value=="") {
    okSoFar2=false
    alert("Please fill in the State field.")
	document.purchase.ship_state.focus()
  }
  if (document.purchase.ship_zip.value=="") {
    okSoFar2=false
    alert("Please fill in the ZIP field.")
	document.purchase.ship_zip.focus()
  }
  if (document.purchase.ship_country.value=="") {
    okSoFar2=false
    alert("Please fill in the Country field.")
	document.purchase.ship_country.focus()
  }  
    if (document.purchase.price.value=="") {
    okSoFar2=false
    alert("Please indicate total dollar amount of your order.")
	document.purchase.price.focus()
  }  
      if (document.purchase.cc_number.value=="") {
    okSoFar2=false
    alert("Please fill in the Credit Card Number field.")
	document.purchase.cc_number.focus()
  }  
        if (document.purchase.exp_month.value=="") {
    okSoFar2=false
    alert("Please specify Expiration Month.")
	document.purchase.exp_month.focus()
  } 
  if (document.purchase.exp_year.value=="") {
    okSoFar2=false
    alert("Please specify Expiration Year.")
	document.purchase.exp_year.focus()
  }  
if (document.purchase.email.value == "") {
    okSoFar2=false
    alert("Please fill in the Email field.")
	document.purchase.email.focus()
  }
  if (!isEmailAddr(document.purchase.email.value)) {
    okSoFar2=false
    alert("Please enter a complete email address in the form: yourname@yourdomain.com");
    document.purchase.email.focus()
  }
if (document.purchase.phone.value=="") {
    okSoFar2=false
    alert("Please fill in the Phone field!")
    document.purchase.phone.focus()
  }
else{
    var phoneInput = document.purchase.phone.value;
    var validPhone = false;

    if(!checkPhone(phoneInput)){
      alert("Phone number is invalid. Please use the following standard: xxx-xxx-xxxx")
okSoFar2=false
document.purchase.phone.focus()
    }
  }
function checkPhone(str){
  var regexp = /^(\d{10}|\d{3}-\d{3}-\d{4}|\(\d{3}\)\d{3}-\d{4})$/;
  return regexp.test(str);
}
if (okSoFar2==true) {
document.purchase.submit()
  }
}

var address_fields = new Array("address", "city", "state", "zip", "first_name", "last_name", "country", "apt", "company");
// Address Copy
function copyAddress(cntl){

var f = cntl.form;
   for (var i=0; i<address_fields.length; i++){
      var fn = address_fields[i];
      var pfn = "ship_" + fn;
      var from = f.elements[fn];
      var to = f.elements[pfn];
      if(!(to) || !(from)){return;}
      if (cntl.checked == true){
         to.value = from.value;
      }else{
         to.value = "";
      }
   }
}

