﻿// JScript File

// disable enter key
function stopRKey(evt) { 
  var evt = (evt) ? evt : ((event) ? event : null); 
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
} 

document.onkeypress = stopRKey; 
//
function calTotal(inputItem)
 
  {
var Myqty = parseInt(document.frmOrder.total_qty.value);

 with (inputItem.form) {
    // Process each of the different input types in the form.
    if (inputItem.type == "radio") {   // Process radio buttons.
      // Subtract the previously selected radio button value from the total.
      total.value = eval(total.value) - eval(previouslySelectedRadioButton.value);
      // Save the current radio selection value.
      previouslySelectedRadioButton.value = eval(inputItem.value)* Myqty;
      // Add the current radio button selection value to the total.
     // total.value = (eval(total.value) + eval(inputItem.value) * Myqty).toFixed(2);
          total.value = (eval(total.value) + (eval(inputItem.value) * Myqty)).toFixed(2);
    } else {   // Process check boxes.
      if (inputItem.checked == false) {   // Item was uncheck. Subtract item value from total.
          total.value = eval(total.value) - eval(inputItem.value);
      } else {   // Item was checked. Add the item value to the total.
          total.value = (eval(total.value) + (eval(inputItem.value)* Myqty)).toFixed(2);
      }
    }
 

    // Total value should never be less than 0.
    if (total.value < 0) {
      InitForm();
    }
    // Return total value.
    return(total.value);
  }
}
 
// calculate tax here
function CalTax(inputItem)
  {
 //
var old_tax = document.frmOrder.tax.value;
   document.frmOrder.tax.value = 0;
   document.frmOrder.total.value = document.frmOrder.total.value - old_tax;
  return(document.frmOrder.tax.value);

}

// Auto Tabs
function autotab(original,destination){
if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
destination.focus()
}

function leadingZero(nr)
{
            if (nr < 10) nr = "0" + nr;
            return nr;
}
// if oh state will be taxed.

function Chkstate(instr) {
 var Myval;
 var MyTax;
 var Myqty = parseInt(document.frmOrder.total_qty.value);
MyTax = 0;
Myval = " ";
   // if it's a oh or not
   if (instr == "OH") 
    {
     // OHio let the tax stand
    // alert ("State of " + instr );
     return true;
    }
     else
      {
    // which button is checked      
    for (i = 0; i < 4; i++)
      {
     if (document.frmOrder.dType[i].checked == true)
     Myval = document.frmOrder.dType[i].value;
// assign ordertype here
       if (Myval == "PickUp") {
           document.frmOrder.OrderType.value = "PickUp";
       } else if (Myval == "7.00") {
          document.frmOrder.OrderType.value = "Regular";
       } else if (Myval == "13.00") {
          document.frmOrder.OrderType.value = "Priority";
        } else if (Myval == "21.00") {
          document.frmOrder.OrderType.value = "Trilogy-Regular";
        } else {
          document.frmOrder.OrderType.value = "Regular ??";
        }
//
    }
 // out of state here
    
   //  alert (instr + " Out of state " + Myval + document.frmOrder.OrderType.value);
     document.frmOrder.total.value = (eval(document.frmOrder.total.value) - eval(document.frmOrder.tax.value * Myqty)).toFixed(2);
     document.frmOrder.tax.value = 0; 
}
   //  alert (instr + " where is this " + Myval); 
}
//
function Chkstate2() {
 var instr;
 var MyTaxs;
MyTax = 0;
instr = document.frmOrder.to_state.value;
   // if it's a oh or not
   if (instr == "OH") 
    {
     // OHio let the tax stand
     return true;  alert ("State of " + instr );
    }
     else
      {
     
   //  alert (instr + " Out of state " + Myval + document.frmOrder.OrderType.value);
     document.frmOrder.total.value = (eval(document.frmOrder.total.value) - eval(document.frmOrder.tax.value)).toFixed(2);
    document.frmOrder.tax.value = 0; 
}
}


//
function CalAmount(nb)
 {
//var qt = number(nb);
var order_total=0;
var item = 0;
  
var Myqty = parseInt(document.frmOrder.total_qty.value);

 for (var i=0; i < document.frmOrder.elements.length; ++i) {
  // Get the current field
        form_field = document.frmOrder.elements[i]
        // Get the field's name
        form_name = form_field.name
        // Is it a "quantity" field?
        if (form_name.substring(0,3) == "qty") {
            // If so, extract the price from the name
            item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))/100
            // Get the quantity
            item_quantity = parseInt(form_field.value)
            // Update the order total
            if (item_quantity >= 1) {
               order_total += item_quantity * item_price;
	  document.frmOrder.total_qty.value = Myqty + item_quantity;
          
                item += 1;
   
            }
        }
    }
    
//var MyTotal = nb * 19.95;
var MyTax = order_total * 0.06757679;
   document.frmOrder.total.value = (order_total + MyTax ).toFixed(2);
   document.frmOrder.tax.value =  (MyTax).toFixed(2);
 //  document.frmOrder.c_Total.value = (order_total + MyTax).toFixed(2);
   document.frmOrder.item_selected.value = item;
//alert(document.frmOrder.c_Total.value);
//alert(document.frmOrder.total_qty.value );

   return true;
   }

function CopyAddress(inv)
     {
      document.frmOrder.to_lname.value = document.frmOrder.lname.value;
      document.frmOrder.to_fname.value = document.frmOrder.fname.value;
      document.frmOrder.to_street1.value = document.frmOrder.street1.value;
      document.frmOrder.to_street2.value = document.frmOrder.street2.value;
      document.frmOrder.to_city.value = document.frmOrder.city.value;
      document.frmOrder.to_state.value = document.frmOrder.state.value;
      document.frmOrder.to_zip.value = document.frmOrder.zip.value;
//
  if (document.frmOrder.state.value != "OH" ) 
      {
      document.frmOrder.total.value = (eval(document.frmOrder.total.value) - eval(document.frmOrder.tax.value)).toFixed(2);
    document.frmOrder.tax.value = 0; 
   }
 }
String.prototype.toTitleCase = function() {
var words = this.split(" ");
for(var i = 0; i < words.length; ++i)
words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1).toLowerCase();
return words.join(" ");
}

// check credit card
function Mod10() {  // v2.0
var ccNumb = document.frmOrder.CC1.value + document.frmOrder.CC2.value + document.frmOrder.CC3.value + document.frmOrder.CC4.value
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
    }
  }
}
// change alert to on-page display or other indication as needed.
if(bResult) {
  //alert("This IS a valid Credit Card Number!");
}
if(!bResult){
  alert("These numbers are NOT a valid Credit Card Number!");
}
  return bResult; // Return the results
}

//function isEmpty(ins) {
//    // if the field is empty
//   if (ins == ""){
//        alert("Error:  First Name Field cannot be empty.");
//     document.frmOrder.fname.focus(); 
// 
// }

function CheckDeliveryType()
{
var item;
var retVal;
item = " ";

     for (i = 0; i < 4; i++)
      {
     if (i != 3 && document.frmOrder.dType[i].checked == true) {
     item = "selected";
        }
     } 
     
     retVal = item == "selected";

       if (retVal){
     // alert ("selected");
     document.getElementById('dTypelabel').innerHTML = "";
     } else {
     document.getElementById('dTypelabel').innerHTML = "Must select Delivery Type";
   }  
   
   return retVal;
 }
 
 function isEmpty(elementID, elementName)
 {
    var element = document.getElementById(elementID);
    var previousLabel;
    var label = document.getElementById(elementID + 'label');
    
    var retVal = element.value == "";
    
    switch(elementID)
    {
        case "zip": 
            previousLabel = document.getElementById('statelabel');
            break;
        case "CC2": 
            previousLabel = document.getElementById('CC1label');
            break;
        case "CC3": 
            previousLabel = document.getElementById('CC2label');
            break;
        case "CC4": 
            previousLabel = document.getElementById('CC3label');
            break;
        case "Cvv": 
            previousLabel = document.getElementById('CC4label');
            break;
        case "cardYear": 
            previousLabel = document.getElementById('cardMonthlabel');
            break;
    }
        
//    if(elementID == "cardYear")
//        alert(previousLabel.innerHTML);
        
    if(retVal)
    {
        label.innerHTML = (previousLabel && previousLabel.innerHTML != "" ? "<br />" : "") + elementName + " cannot be empty.";
    }
    else
    {
        label.innerHTML = "";
    }
    
//   if (ins == ""){
//        alert("Error:  First Name Field cannot be empty.");
//     document.frmOrder.fname.focus(); 
     
     return !retVal;
 }
 
 function CheckForm()
 {
    var retVal = true;
    var empty;
    
    // Check that 'fname' is not empty
    empty = isEmpty('fname', 'First Name');
    retVal = retVal ? empty : retVal;
    
    // Check that 'lname' is not empty
    empty = isEmpty('lname', 'Last Name');
    retVal = retVal ? empty : retVal;
    
    // Check that 'street1' is not empty
    empty = isEmpty('street1', 'Street 1');
    retVal = retVal ? empty : retVal;
    
    // Check that 'street2' is not 
    empty = isEmpty('street2', 'Street 2');
    retVal = retVal ? empty : retVal;
    
    // Check that 'city' is not empty
    empty = isEmpty('city', 'City');
    retVal = retVal ? empty : retVal;
    
    // Check that 'state' is not empty
    empty = isEmpty('state', 'State');
    retVal = retVal ? empty : retVal;
    
    // Check that 'zip' is not empty
    empty = isEmpty('zip', 'Zip');
    retVal = retVal ? empty : retVal;
    
    // Check that 'phone' is not empty
    empty = isEmpty('phone', 'Phone Number');
    retVal = retVal ? empty : retVal;
    
    // Check that 'email' is not empty
    empty = isEmpty('email', 'Email');
    retVal = retVal ? empty : retVal;
    
    // UNCOMMENT BELOW FOR SHIPPING VALIDATION
    
    /*
    // Check that 'to_fname' is not empty
    empty = isEmpty('to_fname', 'First Name');
    retVal = retVal ? empty : retVal;
    
    // Check that 'to_lname' is not empty
    empty = isEmpty('to_lname', 'Last Name');
    retVal = retVal ? empty : retVal;
    
    // Check that 'to_street1' is not empty
    empty = isEmpty('to_street1', 'Street 1');
    retVal = retVal ? empty : retVal;
    
    // Check that 'to_street2' is not 
    empty = isEmpty('to_street2', 'Street 2');
    retVal = retVal ? empty : retVal;
    
    // Check that 'to_city' is not empty
    empty = isEmpty('to_city', 'City');
    retVal = retVal ? empty : retVal;
    
    // Check that 'to_state' is not empty
    empty = isEmpty('to_state', 'State');
    retVal = retVal ? empty : retVal;
    
    // Check that 'to_zip' is not 
    empty = isEmpty('to_zip', 'Zip');
    retVal = retVal ? empty : retVal;
    */
    
    empty = CheckDeliveryType();
    retVal = retVal ? empty : retVal;
    
    // Credit Card Information
    
    if(document.getElementById('payby4').checked || document.getElementById('payby5').checked ||
        document.getElementById('payby6').checked)
    {
        empty = isEmpty('CC1', 'Credit Card 1');
        retVal = retVal ? empty : retVal;
        
        empty = isEmpty('CC2', 'Credit Card 2');
        retVal = retVal ? empty : retVal;
        
        empty = isEmpty('CC3', 'Credit Card 3');
        retVal = retVal ? empty : retVal;
        
        empty = isEmpty('CC4', 'Credit Card 4');
        retVal = retVal ? empty : retVal;
        
        empty = isEmpty('Cvv', 'CVV');
        retVal = retVal ? empty : retVal;
        
        empty = isEmpty('cardMonth', 'Credit Card Month');
        retVal = retVal ? empty : retVal;
        
        empty = isEmpty('cardYear', 'Credit Card Year');
        retVal = retVal ? empty : retVal;
    }
    
    return retVal;
 }