function ShipToBillPerson(form) 
{
  if(form.copy.checked)
  {
    form.sname.value = form.bname.value;
    form.sadd1.value = form.add1.value;
    form.sadd2.value = form.add2.value;
    form.scity.value = form.city.value;
    form.szip.value = form.zip.value;
    form.sstate.selectedIndex = form.state.selectedIndex;
    form.sprov.value = form.prov.value;
    form.shipcountry.selectedIndex = form.country.selectedIndex;
  }else
  {
    form.sname.value = "";
    form.sadd1.value = "";
    form.sadd2.value = "";
    form.scity.value = "";
    form.szip.value = "";
    form.sstate.selectedIndex = 0;
    form.sprov.value = "";
    form.shipcountry.selectedIndex = "US - United States";
  }                  
}


function ShipSameAsBill(form) 
{
  if(form.copy.checked)
  {
    form.sname.value = form.name.value;
    form.sadd1.value = form.add1.value;
    form.sadd2.value = form.add2.value;
    form.scity.value = form.city.value;
    form.szip.value = form.zip.value;
    form.sstate.selectedIndex = form.state.selectedIndex;
    form.sprov.value = form.prov.value;
    form.scountry.selectedIndex = form.country.selectedIndex;
  }
  else{
    form.sname.value = "";
    form.sadd1.value = "";
    form.sadd2.value = "";
    form.scity.value = "";
    form.szip.value = "";
    form.sstate.selectedIndex = 0;
    form.sprov.value = "";
    form.scountry.selectedIndex = "US - United States";
  }                  
}


function SetTracking( f )
{
  shipval = getCheckedValue(f.shipmethod);
  
/*  alert(shipval); */

  if( shipval == "priority" )
  {
    f.confirmation.checked = true;
    f.confamt.value = "0.00";
    f.dispconfamt.value = "0.00";
  }else
  {
    f.confamt.value = "0.75";
    f.dispconfamt.value = "0.75";
  }
}


function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function SetProdPrice1(form, num, ProdArray, WeightArray, prods)
{
  prod  = form["prod"+num];
  cost  = form["cost"+num];
  total = form["total"];
  shipping = form["shipping"];

  price = parseInt(prod.value) * parseFloat(ProdArray[num]);
  price = Currency(price);
  cost.value = price;
   
  totprice = 0;
  for (i=1;i<=prods;i++) 
  {
    tmpcost = form["cost"+i];
    if(tmpcost != undefined)
    {
      totprice = parseFloat(totprice) + parseFloat(tmpcost.value);
    }
  }

  SetShipping(form, prods, WeightArray);
   
  totprice = Currency(totprice);
  totprice = parseFloat(totprice) + parseFloat(shipping.value);
  total.value = totprice;
  
}


function SetShipping(form, prods, WeightArray)
{
  var fcarr = new Array(1.13,1.30,1.47,1.64,1.81,1.98,2.15,2.32,2.49,2.66,2.83,3.00,3.17);
  var priorityarr = new Array(4.60,4.60,5.70,6.30,6.85,7.35,7.75,8.15,8.50,8.80,9.15,9.50,9.80,10.15,10.35,10.50,10.70,11.10,11.60,12.00,12.50,12.90,13.35,13.85,14.25,14.70,15.15,15.60,16.10);
  var fcintarr = new Array(1.13,1.80,2.70,3.60,4.50,5.40,6.30,7.20,7.20,7.20,7.20,7.20,8.65,8.65,8.65,8.65,10.10,10.10,10.10,10.10,11.55,11.55,11.55,11.55,13.00,13.00,13.00,13.00,14.45);
  var priorityintarr = new Array(20.00,24.00,28.00,32.00,36.00,39.30,42.60,45.90,49.20,52.50);
  
  ship = form["shipping"];
  country = form["country"];
  
  totweight = 0;
  for (i=1;i<=prods;i++) 
  {
    prod  = form["prod"+i];
    if(prod != undefined)
    {
      prodweight = prod.value * parseFloat(WeightArray[i]);
      totweight = parseFloat(totweight) + parseFloat(prodweight);
    }
  }

  totweight = Math.round(totweight);
  if(country.value == "US")
  {
    fcmax = 13;
    if(totweight <= fcmax)
    {
      shipamt = fcarr[totweight-1];
    }else
    {
      totpounds = totweight/16;
      totpounds = Math.round(totpounds);
      shipamt = priorityarr[totpounds-1];
    }
  }else
  {
    fcmax = 29;
    if(totweight <= fcmax)
    {
      shipamt = fcintarr[totweight-1];
    }else
    {
      totpounds = totweight/16;
      totpounds = Math.round(totpounds);
      shipamt = priorityintarr[totpounds-1];
    }
  }
/*  alert("totweight = "+totweight+"country = "+country.value); */
  shipamt = shipamt + 1;
  shipamt = Currency(shipamt);
  ship.value = shipamt;
  
}


function Currency(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}


function chkpaypal( f )
{
/*  alert("cctype = " + f.cctype.value); */

  if( f.cctype.value == "Paypal" )
  {
    f.ccnum.disabled = true;
    f.expmm.disabled = true;
    f.expyy.disabled = true;
    f.ccname.disabled = true;
    f.ccid.disabled = true;
  }else
  {
    f.ccnum.disabled = false;
    f.expmm.disabled = false;
    f.expyy.disabled = false;
    f.ccname.disabled = false;
    f.ccid.disabled = false;
  }
}

/* Used on the account login page to validate the email and password */

function validateEmail( f )
{
  a1 = f.uemail.value.indexOf("@");

  if(f.uemail.value.length < 7 || a1 == -1)
  {
    alert("Please enter a valid email address");
    f.uemail.focus();
    return(false);
  }else if(f.upass.value.length < 5 || f.upass.value.length > 12)
  {
    alert("Password must be between 5 and 12 characters long");
    f.upass.focus();
    return(false);
  }else
  {
    return(true);
  }
}

/* Used on the account login creation page to verify the login info */

function validateLogin( f )
{
  a1 = f.uemail.value.indexOf("@");
  a2 = f.uemail2.value.indexOf("@");

  if(f.uemail.value.length < 7 || a1 == -1)
  {
    alert("Please enter a valid email address");
    f.uemail.focus();
    return(false);
  }else if(f.uemail2.value.length < 7 || a2 == -1)
  {
    alert("Please enter a valid confirmation email address");
    f.uemail2.focus();
    return(false);
  }else if(f.uemail.value != f.uemail2.value)
  {
    alert("Email addresses must match");
    f.uemail.focus();
    return(false);
  }else if(f.upass.value.length < 5 || f.upass.value.length > 12)
  {
    alert("Password must be between 5 and 12 characters long");
    f.upass.focus();
    return(false);
  }else if(f.uname.value.length < 2)
  {
    alert("Please enter your full name");
    f.uname.focus();
    return(false);
  }else if(f.uhow.value.length < 3)
  {
    alert("Please enter how you heard about us");
    f.uhow.focus();
    return(false);
  }
}

/* Used in the catalog page */

function validateQuantity( f )
{
  if(isNaN(f.quantity.value) == true || f.quantity.value <= 0 || f.quantity.value >= 51 || f.quantity.value.length == 0)
  {
    alert("Quantity must be between 1 and 50");
    f.quantity.focus();
    return(false);
  }else
  {
    return(true);
  }
}


/* Used to validate the password matching on the change password page */

function validatePass( f )
{
  if(f.cpass.value.length < 5 || f.cpass.value.length > 12)
  {
    alert("Old password must be between 5 and 12 characters long");
    f.cpass.focus();
    return(false);
  }else if(f.npass1.value.length < 5 || f.npass1.value.length > 12)
  {
    alert("New password must be between 5 and 12 characters long");
    f.npass1.focus();
    return(false);
  }else if(f.npass2.value.length < 5 || f.npass2.value.length > 12)
  {
    alert("New password must be between 5 and 12 characters long");
    f.npass2.focus();
    return(false);
  }else if(f.npass1.value != f.npass2.value)
  {
    alert("New passwords must match");
    f.npass1.focus();
    return(false);
  }
}

/* Used on the account login creation page to verify the login info */

function validateEmail2( f )
{
  c1 = f.cemail.value.indexOf("@");
  n1 = f.nemail1.value.indexOf("@");
  n2 = f.nemail2.value.indexOf("@");

  if(f.nemail1.value.length > 0)
  {
    if(f.cemail.value.length < 7 || c1 == -1)
    {
      alert("Please enter a valid email address");
      f.cemail.focus();
      return(false);
    }else if(f.nemail1.value.length < 7 || n1 == -1)
    {
      alert("Please enter a valid confirmation email address");
      f.nemail1.focus();
      return(false);
    }else if(f.nemail2.value.length < 7 || n2 == -1)
    {
      alert("Please enter a valid confirmation email address");
      f.nemail2.focus();
      return(false);
    }else if(f.nemail1.value != f.nemail2.value)
    {
      alert("Email addresses must match");
      f.nemail1.focus();
      return(false);
    }
  }
}

/* Used to validate the add payment form */

function validatePayment( f, m, y)
{
  if(f.cctype.value != "Paypal")
  {
    c1 = f.ccnum.value.indexOf(" ");

    if(c1 >= 0)
    {
      alert("Please enter your credit card number without any spaces");
      f.ccnum.focus();
      return(false);
    }
    else if(f.cctype.value == "American Express" && f.ccnum.value.length != 15)
    {
      alert("American Express card number must be 15 digits long");
      f.ccnum.focus();
      return(false);
    }else if(f.cctype.value != "American Express" && f.ccnum.value.length != 16)
    {
      alert("Credit card number must be 16 digits");
      f.ccnum.focus();
      return(false);
    }
    if(f.expmm.value < m && f.expyy.value <= y)
    {
      alert("Expiration date is in the past");
      f.expmm.focus();
      return(false);
    }
    if(f.ccname.value.length < 5)
    {
      alert("Please enter the name as it appears on the card");
      f.ccname.focus();
      return(false);
    }
    if(f.ccid.value.length != 3)
    {
      alert("Please enter the card ID");
      f.ccid.focus();
      return(false);
    }
  }
}

/* Used to validate the edit payment form */

function validateEditPayment( f, m, y)
{
  if(f.expmm.value < m && f.expyy.value <= y)
  {
    alert("Expiration date is in the past");
    f.expmm.focus();
    return(false);
  }
  if(f.ccname.value.length < 5)
  {
    alert("Please enter the name as it appears on the card");
    f.ccname.focus();
    return(false);
  }
  if(f.ccid.value.length != 3)
  {
    alert("Please enter the card ID");
    f.ccid.focus();
    return(false);
  }
}


/* Used to validate the add shipping address form */

function validateShipping( f )
{
  if(f.shiploc.value.length < 3)
  {
    alert("Please give this shipping address a location name");
    f.shiploc.focus();
    return(false);
  }
  if(f.shipname.value.length < 5)
  {
    alert("Please enter a shipping name");
    f.shipname.focus();
    return(false);
  }
  if(f.shipadd1.value.length < 5)
  {
    alert("Please enter a shipping address");
    f.shipadd1.focus();
    return(false);
  }
  if(f.shipcity.value.length < 3)
  {
    alert("Please enter a shipping city");
    f.shipcity.focus();
    return(false);
  }
  if(f.shipcountry.value == "US" && f.shipst.value.length < 2)
  {
    alert("Please select a state");
    f.shipst.focus();
    return(false);
  }
  if(f.shipzip.value.length < 4)
  {
    alert("Please enter a zip/postal code");
    f.shipzip.focus();
    return(false);
  }
}


/* Used to validate the edit billing info form */

function validateBilling( f )
{
  if(f.billname.value.length < 5)
  {
    alert("Please enter a name");
    f.billname.focus();
    return(false);
  }
  if(f.billadd1.value.length < 5)
  {
    alert("Please enter an address");
    f.billadd1.focus();
    return(false);
  }
  if(f.billcity.value.length < 3)
  {
    alert("Please enter a city name");
    f.billcity.focus();
    return(false);
  }
  if(f.billcountry.value == "US" && f.billst.value.length < 2)
  {
    alert("Please select a state");
    f.billst.focus();
    return(false);
  }
  if(f.billzip.value.length < 4)
  {
    alert("Please enter a zip/postal code");
    f.billzip.focus();
    return(false);
  }
  if(f.billphone.value.length < 10)
  {
    alert("Please enter a telephone number");
    f.billphone.focus();
    return(false);
  }
  if(f.billhow.value.length < 5)
  {
    alert("Please enter how you heard about us");
    f.billhow.focus();
    return(false);
  }
}


/* Used on the checkout page to validate the user has selected a shipping address and a payment */

function validateInfo( f, scnt, ecnt )
{
  a1 = 0;
  if(scnt > 1)
  {
    for (i=0;i<scnt;i++) 
    {
      if (f.loc[i].checked) 
      {
        a1 = f.loc[i].value;
      }
    }
    if(a1 == 0)
    {
      alert("Please select a shipping address");
      return(false);
    }
  }

  if(ecnt > 1)
  {
    a1 = 0;
    for (i=0;i<ecnt;i++) 
    {
      if (f.payment[i].checked) 
      {
        a1 = f.payment[i].value;
      }
    }
    if(a1 == 0)
    {
      alert("Please select a form of payment");
      return(false);
    }
  }
}


/* Used on the shipping checkout page to validate the user has selected a shipping method */

function validateShipInfo( f )
{
  a1 = 0;
  for (i=0;i<=1;i++) 
  {
    if (f.shipmethod[i].checked) 
    {
      a1 = f.shipmethod[i].value;
    }
  }
  if(a1 == 0)
  {
    alert("Please select a shipping method");
    return(false);
  }
}


function populateAtts( f, prodnum, ProdArray, AttArray, AttTxtArray ) 
{ 

  prodbox = "pid"+prodnum;
  pid = f[prodbox].value;

//  Clear out the attribute selection lists

  for (i=1; i<=4; i++)
  {
    aidbox = "aid"+i+prodnum;
    for (x = f[aidbox].options.length; x >= 0; x--) 
    {
       f[aidbox].options[x] = null;
    }
    f[aidbox].options[f[aidbox].options.length] = new Option('', '-1');
  }

// loop thru the language array and find the language that matches the selected country

  for (i=0; i<ProdArray.length; i++)
  {
    if(ProdArray[i] == pid)
    {
      for (j=0; j<AttArray[i].length; j++) 
      {
        AttVal = AttArray[i][j];
        AttTxt = AttTxtArray[i][j];

// loop thru the languages select list and see if this language is already chosen        

        aidbox = "aid1"+prodnum;
        f[aidbox].options[f[aidbox].options.length] = new Option(AttTxt, AttVal);
        aidbox = "aid2"+prodnum;
        f[aidbox].options[f[aidbox].options.length] = new Option(AttTxt, AttVal);
        aidbox = "aid3"+prodnum;
        f[aidbox].options[f[aidbox].options.length] = new Option(AttTxt, AttVal);
        aidbox = "aid4"+prodnum;
        f[aidbox].options[f[aidbox].options.length] = new Option(AttTxt, AttVal);
      }
    }
  }

}

function winpop(target,h,w) 
{
  window.open(target,"BilgePumps","height=" + h + ",width=" + w + ",left=30,top=10,toolbars=no,scrollbars=no,location=no,statusbars=no,menubars=no");
}



