function isInt(myNum) {
 // get the modulus: if it's 0, then it's an integer
  var myMod = myNum % 1;
 if (myMod == 0) {
  return true;
 } else {
  return false;
 }
}

function checkEmail(email) {
 if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.toLowerCase())) {
  return true;
 } else {
  return false;
 }
}

function copy2ship(object) {
 object.ship_name.value=object.bill_name.value;
 object.ship_address.value=object.bill_address.value;
 object.ship_city.value=object.bill_city.value;
 object.ship_prov.value=object.bill_prov.value;
 object.ship_pc.value=object.bill_pc.value;
}


 function checkShipForm(object) {
  if (object.name.value.length<3) { alert("Please enter a name of at least 3 characters.");object.name.focus();return false;}
  else if (object.company.value.length<2) { alert("Please enter a company name of at least 2 characters.");object.company.focus();return false;}
  else if (object.position.value.length<2) { alert("Please enter a position of at least 2 characters.");object.position.focus();return false;}
  else if (object.phone.value.length<7) { alert("Please enter your phone number.");object.phone.focus();return false;}
  else if (!checkEmail(object.email.value)) { alert("Please enter your email address.");object.email.focus();return false;}
  else if (object.password && object.password.value.length<4) { alert("Please enter a password of at least 4 characters.");object.password.focus();return false;}
  else if (object.password && object.password.value!=object.password2.value) { alert("The passwords you entered do not match.");object.password2.focus();return false;}

  else if (object.npassword && object.npassword.value!="" && object.npassword.length<4) { alert("Please enter a new password of at least 4 characters.");object.npassword.focus();return false;}
  else if (object.npassword && object.npassword.value!="" && object.cpassword.value=="") { alert("Please enter your current password if you want to change your existing one.");object.npassword.focus();return false;}
  else if (object.npassword && object.npassword.value!="" && object.npassword.value!=object.npassword2.value) { alert("Your new passwords do not match.");object.npassword2.focus();return false;}

  else if (object.method[1].checked && object.ship_name.value.length<3) { alert("Please enter a shipping contact name of at least 3 characters.");object.ship_name.focus();return false;}
  else if (object.method[1].checked && object.ship_address.value.length<3) { alert("Please enter your shipping address.");object.ship_address.focus();return false;}
  else if (object.method[1].checked && object.ship_city.value.length<2) { alert("Please enter your shipping city.");object.ship_city.focus();return false;}
  else if (object.method[1].checked && object.ship_prov.value.length<2) { alert("Please enter your shipping province or state.");object.ship_province.focus();return false;}
  else if (object.method[1].checked && object.ship_pc.value.length<5) { alert("Please enter your shipping postal code.");object.ship_pc.focus();return false;}

  else if (object.bill_name.value.length<3) { alert("Please enter a billing contact name of at least 3 characters.");object.bill_name.focus();return false;}
  else if (object.bill_address.value.length<3) { alert("Please enter your billing address.");object.bill_address.focus();return false;}
  else if (object.bill_city.value.length<2) { alert("Please enter your billing city.");object.bill_city.focus();return false;}
  else if (object.bill_prov.value.length<2) { alert("Please enter your billing province or state.");object.bill_province.focus();return false;}
  else if (object.bill_pc.value.length<5) { alert("Please enter your billing postal code.");object.bill_pc.focus();return false;}
  else if (object.method[1].checked) {
   getDirections('V6A 2A8',object.ship_pc.value);
   return false;
  } else return true;
 }

function calcTotal() {
 var orderform=document.orderForm;
 var t_im, t_imex, t_imws, t_imen, t_imsw, t_imcb, t_imss, t_immc, t_scoop, t_plough, t_hybrid, t_ms16, t_ms10, t_ms11, t_ms14;
 var q_im, q_imex, q_imws, q_imen, q_imsw, q_imcb, q_imss, q_immc, q_scoop, q_plough, q_hybrid, q_ms16, q_ms10, q_ms11, q_ms14;
 var subtotal;

 if (!isInt(orderform.q_im.value)) { alert('The quantity field must contain numbers only.');orderform.q_im.value=0;orderform.q_im.focus();return false;}
 else if (!isInt(orderform.q_imex.value)) { alert('The quantity field must contain numbers only.');orderform.q_imex.value=0;orderform.q_imex.focus();return false;}
 else if (!isInt(orderform.q_imws.value)) { alert('The quantity field must contain numbers only.');orderform.q_imws.value=0;orderform.q_imws.focus();return false;}
 else if (!isInt(orderform.q_imen.value)) { alert('The quantity field must contain numbers only.');orderform.q_imen.value=0;orderform.q_imen.focus();return false;}
 else if (!isInt(orderform.q_imsw.value)) { alert('The quantity field must contain numbers only.');orderform.q_imsw.value=0;orderform.q_imsw.focus();return false;}
 else if (!isInt(orderform.q_imcb.value)) { alert('The quantity field must contain numbers only.');orderform.q_imcb.value=0;orderform.q_imcb.focus();return false;}
 else if (!isInt(orderform.q_imss.value)) { alert('The quantity field must contain numbers only.');orderform.q_imss.value=0;orderform.q_imss.focus();return false;}
 else if (!isInt(orderform.q_immc.value)) { alert('The quantity field must contain numbers only.');orderform.q_immc.value=0;orderform.q_immc.focus();return false;}
 else if (!isInt(orderform.q_scoop.value)) { alert('The quantity field must contain numbers only.');orderform.q_scoop.value=0;orderform.q_scoop.focus();return false;}
 else if (!isInt(orderform.q_plough.value)) { alert('The quantity field must contain numbers only.');orderform.q_plough.value=0;orderform.q_plough.focus();return false;}
 else if (!isInt(orderform.q_hybrid.value)) { alert('The quantity field must contain numbers only.');orderform.q_hybrid.value=0;orderform.q_hybrid.focus();return false;}
 else if (!isInt(orderform.q_ms16.value)) { alert('The quantity field must contain numbers only.');orderform.q_ms16.value=0;orderform.q_ms16.focus();return false;}
 else if (!isInt(orderform.q_ms10.value)) { alert('The quantity field must contain numbers only.');orderform.q_ms10.value=0;orderform.q_ms10.focus();return false;}
 else if (!isInt(orderform.q_ms11.value)) { alert('The quantity field must contain numbers only.');orderform.q_ms11.value=0;orderform.q_ms11.focus();return false;}
 else if (!isInt(orderform.q_ms14.value)) { alert('The quantity field must contain numbers only.');orderform.q_ms14.value=0;orderform.q_ms14.focus();return false;}

 if (orderform.s_im.options[orderform.s_im.selectedIndex].value=="pallet") q_im=orderform.q_im.value*bpp['im']; else q_im=orderform.q_im.value;
 if (orderform.s_imex.options[orderform.s_imex.selectedIndex].value=="pallet") q_imex=orderform.q_imex.value*bpp['imex']; else q_imex=orderform.q_imex.value;
 if (orderform.s_imws.options[orderform.s_imws.selectedIndex].value=="pallet") q_imws=orderform.q_imws.value*bpp['imws']; else q_imws=orderform.q_imws.value;
 if (orderform.s_imen.options[orderform.s_imen.selectedIndex].value=="pallet") q_imen=orderform.q_imen.value*bpp['imen']; else q_imen=orderform.q_imen.value;
 if (orderform.s_imsw.options[orderform.s_imsw.selectedIndex].value=="pallet") q_imsw=orderform.q_imsw.value*bpp['imsw']; else q_imsw=orderform.q_imsw.value;
 if (orderform.s_imcb.options[orderform.s_imcb.selectedIndex].value=="pallet") q_imcb=orderform.q_imcb.value*bpp['imcb']; else q_imcb=orderform.q_imcb.value;
 if (orderform.s_imss.options[orderform.s_imss.selectedIndex].value=="pallet") q_imss=orderform.q_imss.value*bpp['imss']; else q_imss=orderform.q_imss.value;
 if (orderform.s_immc.options[orderform.s_immc.selectedIndex].value=="pallet") q_immc=orderform.q_immc.value*bpp['immc']; else q_immc=orderform.q_immc.value;
 q_scoop=orderform.q_scoop.value;
 q_plough=orderform.q_plough.value;
 q_hybrid=orderform.q_hybrid.value;
 q_ms16=orderform.q_ms16.value;
 q_ms10=orderform.q_ms10.value;
 q_ms11=orderform.q_ms11.value;
 q_ms14=orderform.q_ms14.value;

 t_im = q_im * document.getElementById('p_im').innerHTML;
 t_imex = q_imex * document.getElementById('p_imex').innerHTML;
 t_imws = q_imws * document.getElementById('p_imws').innerHTML;
 t_imen = q_imen * document.getElementById('p_imen').innerHTML;
 t_imsw = q_imsw * document.getElementById('p_imsw').innerHTML;
 t_imss = q_imss * document.getElementById('p_imss').innerHTML;
 t_immc = q_immc * document.getElementById('p_immc').innerHTML;
 if (document.getElementById('p_imcb')) t_imcb = q_imcb * document.getElementById('p_imcb').innerHTML; else t_imcb = 0;
 t_scoop = q_scoop * document.getElementById('p_scoop').innerHTML;
 t_plough = q_plough * document.getElementById('p_plough').innerHTML;
 t_hybrid = q_hybrid * document.getElementById('p_hybrid').innerHTML;
 t_ms16 = q_ms16 * document.getElementById('p_ms16').innerHTML;
 t_ms10 = q_ms10 * document.getElementById('p_ms10').innerHTML;
 t_ms11 = q_ms11 * document.getElementById('p_ms11').innerHTML;
 t_ms14 = q_ms14 * document.getElementById('p_ms14').innerHTML;

 subtotal=t_im + t_imex + t_imws + t_imen + t_imsw + t_imcb + t_imss + t_immc + t_scoop + t_plough + t_hybrid + t_ms16 + t_ms10 + t_ms11 + t_ms14;

 document.getElementById('t_im').innerHTML=t_im.toFixed(2);
 document.getElementById('t_imex').innerHTML=t_imex.toFixed(2);
 document.getElementById('t_imws').innerHTML=t_imws.toFixed(2);
 document.getElementById('t_imen').innerHTML=t_imen.toFixed(2);
 document.getElementById('t_imsw').innerHTML=t_imsw.toFixed(2);
 if (document.getElementById('t_imcb')) document.getElementById('t_imcb').innerHTML=t_imcb.toFixed(2);
 document.getElementById('t_imss').innerHTML=t_imss.toFixed(2);
 document.getElementById('t_immc').innerHTML=t_immc.toFixed(2);
 document.getElementById('t_scoop').innerHTML=t_scoop.toFixed(2);
 document.getElementById('t_plough').innerHTML=t_plough.toFixed(2);
 document.getElementById('t_hybrid').innerHTML=t_hybrid.toFixed(2);
 document.getElementById('t_ms16').innerHTML=t_ms16.toFixed(2);
 document.getElementById('t_ms10').innerHTML=t_ms10.toFixed(2);
 document.getElementById('t_ms11').innerHTML=t_ms11.toFixed(2);
 document.getElementById('t_ms14').innerHTML=t_ms14.toFixed(2);

 document.getElementById('subtotal').innerHTML=subtotal.toFixed(2);
 return true;
}

function showPic(object) {
 hidePics();
 document.getElementById('r_'+object).style.backgroundColor="#EEEEEE";
 swapPic(object);
}

function swapPic(object) {
 document.getElementById('pic').style.backgroundImage="url(img/prod-"+object+".gif)";
}

function hidePics() {
 document.getElementById('r_im').style.backgroundColor="";
 document.getElementById('r_imex').style.backgroundColor="";
 document.getElementById('r_imws').style.backgroundColor="";
 document.getElementById('r_imen').style.backgroundColor="";
 document.getElementById('r_imsw').style.backgroundColor="";
 document.getElementById('r_imcb').style.backgroundColor="";
 document.getElementById('r_imss').style.backgroundColor="";
 document.getElementById('r_immc').style.backgroundColor="";
 document.getElementById('r_scoop').style.backgroundColor="";
 document.getElementById('r_plough').style.backgroundColor="";
 document.getElementById('r_hybrid').style.backgroundColor="";
 document.getElementById('r_ms16').style.backgroundColor="";
 document.getElementById('r_ms10').style.backgroundColor="";
 document.getElementById('r_ms11').style.backgroundColor="";
 document.getElementById('r_ms14').style.backgroundColor="";
}

function getDirections(startPC,endPC) {
 var directions = new GDirections();
 directions.load(startPC+" to "+endPC);
 GEvent.addListener(directions, "load", function() {
  var distance = directions.getDistance().meters;
  var kilometers = distance/1000;
  document.shipForm.distance.value=kilometers;
  document.shipForm.submit();
 });
}
