var ie4 = false; if(document.all) { ie4 = true; }
 
function getObject(id) { if (ie4) { return document.all[id]; } else { return document.getElementById(id); } }

function toggle(link, divId) { var lText = link.innerHTML; var d = getObject(divId);
 if (lText == '+') { link.innerHTML = '-'; d.style.display = 'block'; }
 else { link.innerHTML = '+'; d.style.display = 'none'; } }
 
function changeShipping() {
 var d = getObject('dShipping');
 var b = document.sInfo.chkBillAddress.checked;
 if (document.sInfo.chkBillAddress.checked == false){
  d.style.display = 'block';
 }else{d.style.display = 'none';}
}
 
function changePayment(){
 var oV = getCheckedValue(document.forms['sInfo'].elements['rPaymentType']);
 var oC = 'byCredit';
 var lC = 'dCreditCard';
 var sC = getObject(lC);
 var oM = 'byMail';
 var lM = 'dMail';
 var sM = getObject(lM);
 if(oV == oC){
  sC.style.display = 'block';
  sM.style.display = 'none'; 
 }else{
  sC.style.display = 'none';
  sM.style.display = 'block';
 }
}
 
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 cartInfoForm(){
 for(i=0; i<document.sInfo.elements.length; i++){
  var str = document.sInfo.elements[i].value;
  str = str.replace(/^\s+|\s+$/g,"");
  document.sInfo.elements[i].value = str;
 }
 var sErr;	
 var numericPattern = /^[a-zA-Z0-9]+$/;
 sErr = ""   
 with(document.sInfo){
  if(txtFirstName.value == ''){sErr += " - First Name cannot be blank\n";}
  if(txtLastName.value ==''){sErr += " - Last Name cannot be blank\n";}
  if(txtAddressOne.value ==''){sErr += " - Address cannot be blank\n";}
  if(txtCity.value ==''){sErr += " - City cannot be blank\n";}
  if(cmbState.options[cmbState.selectedIndex].value == ''){
   sErr += " - State cannot be blank\n";      
  }else{
   if((txtOther.value =='') && (cmbState.options[cmbState.selectedIndex].value =='US')){
    sErr += " - Please enter Non-US State\n";      
   }else{				
    if((txtOther.value !='') && (cmbState.options[cmbState.selectedIndex].value !='US')){
     sErr+=" - You have chosen a US state as well as Non-US state. Please choose only one.\n";}
   }	
  }
  if(txtZip.value ==''){sErr += " - Zip/Postal Code cannot be blank\n";}
  if(cmbCountry.options[cmbCountry.selectedIndex].value == ''){sErr += " - Country cannot be blank\n";}
  if(txtEmail.value == ''){sErr += " - Email Address cannot be blank\n";}
  if(chkBillAddress.checked == false){
  //
   if(txtFirstNameS.value == ''){
    sErr += " - Billing First Name cannot be blank\n";}
   if(txtLastNameS.value ==''){
    sErr += " - Billing Last Name cannot be blank\n";}
   if(txtAddressOneS.value ==''){sErr += " - Billing Address cannot be blank\n";}
   if(txtCityS.value ==''){sErr += " - Billing City cannot be blank\n";}
   if(cmbStateS.options[cmbStateS.selectedIndex].value == ''){
    sErr += " - Billing State cannot be blank\n";      
   }else{
    if((txtOtherS.value =='') && (cmbStateS.options[cmbStateS.selectedIndex].value =='US')){
     sErr += " - Please enter Non-US Billing State\n";      
    }else{				
     if((txtOtherS.value !='') && (cmbStateS.options[cmbStateS.selectedIndex].value !='US')){
      sErr+=" - You have chosen a US state as well as Non-US state. Please choose only one billing state.\n";}
    }	
   }
   if(txtZipS.value ==''){sErr += " - Billing Zip/Postal Code cannot be blank\n";}
   if(cmbCountryS.options[cmbCountryS.selectedIndex].value == ''){sErr += " - Billing Country cannot be blank\n";}
  
  //
  }
  //check different billing address here
  
  var oV = getCheckedValue(document.forms['sInfo'].elements['rPaymentType']);
  if(oV == 'byCredit'){
   var vcc = Mod10(txtCardNumber.value);
   if(vcc == false){sErr+=" -You have entered an invalid credit card number..\n";}
   
   ccExpField = eval('document.sInfo.cmbExpireMonth.value') + '/' + eval('document.sInfo.cmbExpireYear.value')
   var ccDate = isValidExpDate(ccExpField,"Expiration Date",true,'sInfo');
   if(ccDate == false){sErr+=" -You have entered an invalid credit card expiration..\n";}
  }
  //check credit card info here if mail is not selected.
  
  if (sErr != ""){  
   alert("The following errors occured :\n\n" + sErr);      
  }else{
   //document.sInfo.btnsubmit.disabled=true;
   document.sInfo.submit(); 
  } 
 } 
}

function allDigits(str){return inValidCharSet(str,"0123456789");}

function inValidCharSet(str,charset){
var result = true;
 for (var i=0;i<str.length;i++)
  if (charset.indexOf(str.substr(i,1))<0){
   result = false;         
   break;
  }
 return result;
}

function isValidExpDate(formField,fieldLabel,required,frmName){
 var result = true;
 var formValue = formField;
 if (required && !validRequired(formField,fieldLabel,frmName))
  result = false;
  if (result && (formField.length>0)){
   var elems = formValue.split("/");
   result = (elems.length == 2); // should be two components
   var expired = false;
   if (result){
    var month = parseInt(elems[0],10);
    var year = parseInt(elems[1],10);
    if (elems[1].length == 2)
     year += 2000;
     var now = new Date();
     var nowMonth = now.getMonth() + 1;
     var nowYear = now.getFullYear();
     expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));
     result = allDigits(elems[0]) && (month > 0) && (month < 13) && 
         allDigits(elems[1]) && ((elems[1].length == 2) || (elems[1].length == 4));
   }
   if (!result){
    alert('Please select the Date and Year for the "' + fieldLabel +'" field.');
    eval('document.' + frmName + '.cmbExpireMonth.focus()');
        }
        else if (expired)
        {
            result = false;
            alert('The date for "' + fieldLabel +'" has expired.');
            eval('document.' + frmName + '.cmbExpireMonth.focus()');
        }
    } 
    
 return result;
}

function validRequired(formField,fieldLabel,frmName){
var result = true;  
if (formField == ""){
 alert('Please enter a value for the "' + fieldLabel +'" field.');
 eval('document.' + frmName + '.txtCardNumber.focus()');
 result = false;
 }
 return result;
}

function Mod10(ccNumb) {  // v2.0
var valid = "0123456789"  // Valid digits in a credit card number
var len = ccNumb.length;  // The length of the submitted cc number
var iCCN = parseInt(ccNumb);  // integer of ccNumb
var sCCN = ccNumb.toString();  // string of ccNumb
sCCN = sCCN.replace (/^s+|s+$/g,'');  // strip spaces
var iTotal = 0;  // integer total set at zero
var bNum = true;  // by default assume it is a number
var bResult = false;  // by default assume it is NOT a valid cc
var temp;  // temp variable for parsing string
var calc;  // used for calculation of each digit

// Determine if the ccNumb is in fact all numbers
for (var j=0; j<len; j++) {
  temp = "" + sCCN.substring(j, j+1);
  if (valid.indexOf(temp) == "-1"){bNum = false;}
}

// if it is NOT a number, you can either alert to the fact, or just pass a failure
if(!bNum){
  /*alert("Not a Number");*/bResult = false;
}

// Determine if it is the proper length 
if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
  bResult = false;
} else{  // ccNumb is a number and the proper length - let's see if it is a valid card number
  if(len >= 15){  // 15 or 16 for Amex or V/MC
    for(var i=len;i>0;i--){  // LOOP throught the digits of the card
      calc = parseInt(iCCN) % 10;  // right most digit
      calc = parseInt(calc);  // assure it is an integer
      iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
      i--;  // decrement the count - move to the next digit in the card
      iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
      calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
      calc = calc *2;                                 // multiply the digit by two
      // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
      // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
      switch(calc){
        case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
        case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
        case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
        case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
        case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
        default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
      }                                               
    iCCN = iCCN / 10;  // subtracts right most digit from ccNum
    iTotal += calc;  // running total of the card number as we loop
  }  // END OF LOOP
  if ((iTotal%10)==0){  // check to see if the sum Mod 10 is zero
    bResult = true;  // This IS (or could be) a valid credit card number.
  } else {
    bResult = false;  // This could NOT be a valid credit card number
    }
  }
}
  return bResult; // Return the results
}