
function calcData() {
  var count;
  var fldName;
  var fldVal;
  var eaPrice;
  var tPrice = 0; 
  var gTotal = 0 ;
  var gTax = 0;
  var gShip = 0;
  var gState;
  var chked;
  var taxPct = 0;
  var iSel = 0;
  var sDesc;
  sDesc = "";
  chked = document.frmOrder.ChkShip.checked;
  for (count=1; count<=10; count++){
    fldName =  "QT" + count;
    fldVal = document.frmOrder[fldName].value
    if (isNaN(fldVal)) {
         document.frmOrder[fldName].value = 0;
       };
   fldVal = document.frmOrder[fldName].value; 
   eaPrice = document.frmOrder["C" + count].value;
   tPrice = eaPrice * fldVal;
   gTotal = gTotal + tPrice;
   
   document.frmOrder["E" + count].value = formatCurrency(tPrice);

   // build description
   if (tPrice > 0) { 
     sDesc = sDesc + document.frmOrder["QT" + count].value + " " + document.frmOrder["ITC" + count].value + " " + document.frmOrder["E" + count].value + " / " ; 
    }
  }
 
document.frmOrder.DESCRIPTION.value=sDesc;
// ship address is different
  if (chked == false) {
    if (document.frmOrder.State2.options[document.frmOrder.State2.selectedIndex].value == "NY" ) taxPct = 0.0825;}
   
  else
   {if (document.frmOrder.State1.options[document.frmOrder.State1.selectedIndex].value == "NY" ) taxPct = 0.0825; }
// Check Overseas
 if (chked == false) {
    fldName =  document.frmOrder.COUNTRYTOSHIP.options[document.frmOrder.COUNTRYTOSHIP.selectedIndex].value; }
 else
   {fldName = document.frmOrder.COUNTRY.options[document.frmOrder.COUNTRY.selectedIndex].value ; }
 

  gTax = gTotal * taxPct ;
  
  
  gShip = eval(document.frmOrder.D1.options[document.frmOrder.D1.selectedIndex].value);
  document.frmOrder.ShipBy.value = document.frmOrder.D1.selectedIndex;
  if (gTotal == 0 ) {gShip = 0};
  gTotal = gTotal + gShip + gTax;
  // Payflow does not like commas in amount
  document.frmOrder.AMOUNT.value = formatNumber(gTotal); 
  document.frmOrder.TAX.value = formatCurrency(gTax);
  document.frmOrder.SHIPAMOUNT.value = formatCurrency(gShip);
  
}

function chkData() {
var tmpstr;
var tmpstr1;
var chked;
var agree = false;
chked = document.frmOrder.ChkShip.checked;
copyAddress();
calcData();
// Check name
tmpstr = document.frmOrder.NAME.value;
if (isEmpty(tmpstr) == true) {
  alert("Please enter Name");
  document.frmOrder.NAME.focus();
  return false;
}
// Check for email
tmpstr = document.frmOrder.EMAIL.value
if (isEmail(tmpstr) == false ) { 
  alert("Invalid eMail Address");
  document.frmOrder.EMAIL.focus();
  return false;
} 
// Check for phone
tmpstr = document.frmOrder.PHONE.value
if (isEmpty(tmpstr) == true) { 
  alert("Invalid Phone no");
  document.frmOrder.PHONE.focus();
  return false;
} 
// Check Address
tmpstr = document.frmOrder.ADDRESS.value;
if (isEmpty(tmpstr) == true) {
  alert("Please enter Address");
  document.frmOrder.ADDRESS.focus();
  return false;
}
// Check City
tmpstr = document.frmOrder.CITY.value;
if (isEmpty(tmpstr) == true) {
  alert("Please enter City");
  document.frmOrder.CITY.focus();
  return false;
}
// Check Country
 tmpstr = document.frmOrder.COUNTRY.options[document.frmOrder.COUNTRY.selectedIndex].value;
 if (tmpstr == "-1" ) {
   alert("Please Select Country");
   document.frmOrder.COUNTRY.focus();
   return false;
 }
// Check State
if (tmpstr != "USA" && tmpstr != "Canada") {
 tmpstr = document.frmOrder.StateOther.value;
 if (isEmpty(tmpstr) == true) {
   alert("Please enter State or Province");
   document.frmOrder.StateOther.focus();
   return false;}
 else
   document.frmOrder.STATE.value = tmpstr;
   }
else {
  iSel = document.frmOrder.State1.selectedIndex;
  if (tmpstr == "USA" && ( iSel < 1 || iSel > 53 )) {
     alert("Please Select State in United States");
     document.frmOrder.State1.focus();
     return false;}
  else 
    { if (tmpstr == "Canada" &&  iSel < 54 ) {
        alert("Please Select Province in Canada");
        document.frmOrder.State1.focus();
       return false;}     
    }
   tmpstr = document.frmOrder.State1.options[iSel].value;
   document.frmOrder.STATE.value = tmpstr;
   }
   
 // Check Zip Code
 tmpstr = document.frmOrder.ZIP.value;
 if (isEmpty(tmpstr) == true) {
   alert("Please enter  Zip / Postal Code");
   document.frmOrder.ZIP.focus();
   return false;
 }

// if Ship address is different
if (chked == false){
 // Check Ship to name
 tmpstr = document.frmOrder.NAMETOSHIP.value;
 if (isEmpty(tmpstr) == true) {
   alert("Please enter Ship to Name");
   document.frmOrder.NAMETOSHIP.focus();
   return false;
 }
 // Check Address
 tmpstr = document.frmOrder.ADDRESSTOSHIP.value;
 if (isEmpty(tmpstr) == true) {
   alert("Please enter Ship Address");
   document.frmOrder.StreetAddress1.focus();
   return false;
 }
 // Check City
 tmpstr = document.frmOrder.CITYTOSHIP.value;
 if (isEmpty(tmpstr) == true) {
   alert("Please enter Ship to City");
   document.frmOrder.CITYTOSHIP.focus();
   return false;
 }
// Check Country
 tmpstr = document.frmOrder.COUNTRYTOSHIP.options[document.frmOrder.COUNTRYTOSHIP.selectedIndex].value;
 if (tmpstr == "-1" ) {
   alert("Please Select Ship to Country");
   document.frmOrder.COUNTRYTOSHIP.focus();
   return false;
 }
// Check State
if (tmpstr != "USA" && tmpstr != "Canada") {
 tmpstr = document.frmOrder.StateOther2.value;
 if (isEmpty(tmpstr) == true) {
   alert("Please enter Ship to State or Province");
   document.frmOrder.StateOther2.focus();
   return false;}
 else
   document.frmOrder.STATETOSHIP.value = tmpstr;  
 }
else {
  iSel = document.frmOrder.State2.selectedIndex;
  if (tmpstr == "USA" && ( iSel < 1 || iSel > 53 )) {
     alert("Please Select State in United States");
     document.frmOrder.State2.focus();
     return false;}
  else 
    { if (tmpstr == "Canada" &&  iSel < 54 ) {
        alert("Please Select Province in Canada");
        document.frmOrder.State2.focus();
       return false;}     
    } 
    tmpstr = document.frmOrder.State1.options[iSel].value;
    document.frmOrder.STATETOSHIP.value = tmpstr;    
   }
 // Check Zip code
 tmpstr = document.frmOrder.ZIPTOSHIP.value;
 if (isEmpty(tmpstr) == true) {
   alert("Please enter Ship to Zip / Postal Code");
   document.frmOrder.ZIPTOSHIP.focus();
   return false;
 }
 
}
// Items
tmpstr = document.frmOrder.AMOUNT.value;
if (eval(tmpstr) == 0) {
   alert("Please select some items");
   document.frmOrder.QT1.focus();
   return false;
}

// Final Confirmation
tmpstr = document.frmOrder.AMOUNT.value;
tmpstr1 = "Your total order is US $ " + tmpstr + "\nDo you want to proceed";
agree = confirm(tmpstr1);
if ( agree == false ) return false; 

return true; 
}
// end of Chkdata function


function doSubmit() {
 if (chkData()) {
     document.frmOrder.submit();
   }
}

function copyAddress() {
// copies shipping address
var chked;
chked = document.frmOrder.ChkShip.checked;
calcData();
if (chked == true) {
 document.frmOrder.NAMETOSHIP.value =  document.frmOrder.NAME.value;
 document.frmOrder.StreetAddress1.value =  document.frmOrder.ADDRESS.value; 
document.frmOrder.ADDRESSTOSHIP.value =  document.frmOrder.ADDRESS.value;
 document.frmOrder.CITYTOSHIP.value =  document.frmOrder.CITY.value;
 document.frmOrder.State2.selectedIndex =  document.frmOrder.State1.selectedIndex;
 document.frmOrder.StateOther2.value =  document.frmOrder.StateOther.value;
 document.frmOrder.STATETOSHIP.value =  document.frmOrder.STATE.value;
 document.frmOrder.ZIPTOSHIP.value =  document.frmOrder.ZIP.value;
 document.frmOrder.COUNTRYTOSHIP.selectedIndex =  document.frmOrder.COUNTRY.selectedIndex;}
else
 {document.frmOrder.ADDRESSTOSHIP.value = document.frmOrder.StreetAddress1.value + " " + document.frmOrder.Address1.value;
 }
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num)) num = "0";
cents = Math.floor((num*100+0.5)%100); 
num = Math.floor(num).toString();
if(cents < 10) cents = "0" + cents; 
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 
num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3)); 
return ( num + '.' + cents); 
}

function formatNumber(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num)) num = "0";
cents = Math.floor((num*100+0.5)%100); 
num = Math.floor(num).toString();
if(cents < 10) cents = "0" + cents; 
return ( num + '.' + cents); 
}
function isEmail(str) {
// validation of eMail Address
// Checks if the EMAIL Address syntax is valid  
// not the actual existence of the address
// Accepts a string and returns a Boolean value:
// Works with all but provides a more advanced algorithm //for fourth-generation browsers (Navigator 4.0x, // Internet Explorer 4.0x, and above).
// are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function isEmpty(s) {
// Strip the initial whitespace
var s1;
var whitespace = " \t\n\r";
if (s == null) {
 return false;
}

 {   var i = 0;

    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    
    s1= s.substring (i, s.length);
}
  
 return ((s1 == null) || (s1.length == 0))
}

// charInString (CHARACTER c, STRING s)
//
// Returns true if single character c (actually a string)
// is contained within string s.

function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}


