// JavaScript Document

function same_as_contact(){
   document.order_form.ship_name.value = document.order_form.name.value;
   document.order_form.ship_address.value = document.order_form.address.value;
   document.order_form.ship_address2.value = document.order_form.address2.value;
   document.order_form.ship_city.value = document.order_form.city.value;
   document.order_form.ship_state.value = document.order_form.state.value;
   document.order_form.ship_zip.value = document.order_form.zip.value;
}


function render_body_header(header_string){
   var the_element = document.getElementById("body_header");
      the_element.innerHTML = header_string;
}


function confirmDelete(pk,url){
   page = url;
   reply = confirm( "Are you sure you want to delete " + " ?", "OK" );
   if(reply){
      window.location = page + "?delete=" + pk;
   }
}


function removeFromCart(index,url){
   reply = confirm( "Are you sure you want to remove this item from your cart ?", "OK" );
   if(reply){
      window.location = url + "?remove=" + index;
   }
}


function validate(){
 
   //item title
   if ( isempty( document.jewelery_edit_form.title )) {
      document.jewelery_edit_form.title.focus();
      alert("Please enter a value in the \"Title\" field.");
      return false;} 
   
   //item price
   if ( iscurrency( document.jewelery_edit_form.price )) {
      document.jewelery_edit_form.price.focus();
      alert("Please enter a valid currency (i.e. 1000.00 100.00 10.00 1.00 ) in the \"Price\" field.");
      return false;} 
   
   //item description
   if ( isempty( document.jewelery_edit_form.description )) {
      document.jewelery_edit_form.description.focus();
      alert("Please enter a value in the \"Description\" field.");
      return false;} 
      
   return true;
}


function validate_order_form(){
 
   //contact name 
   if ( isempty( document.order_form.name )) {
      document.order_form.name.focus();
      alert("Please enter a value in the \"Contact Information Name\" field.");
      return false;} 
   
   //contact address 
   if ( isempty( document.order_form.address )) {
      document.order_form.address.focus();
      alert("Please enter a value in the \"Contact Information Address\" field.");
      return false;} 
      
   //contact city 
   if ( isempty( document.order_form.city )) {
      document.order_form.city.focus();
      alert("Please enter a value in the \"Contact Information City\" field.");
      return false;}
   
   //contact zip 
   if ( iszipcode( document.order_form.zip )) {
      document.order_form.zip.focus();
      alert("Please enter a value in the \"Contact Information Zip\" field.");
      return false;} 
   
   //contact phone
   if ( isphonenumber( document.order_form.phone )) {
      document.order_form.phone.focus();
      alert("Please enter a valid Phone Number i.e. 1-888-549-5771 in the \"Contact Information Phone Number\" field.");
      return false;} 
   
   //contact email not empty 
   if ( isempty( document.order_form.email )) {
      document.order_form.email.focus();
      alert("Please enter a value in the \"Contact Information Email\" field.");
      return false;}

   //contact email
   if ( !checkemail( document.order_form.email )) {
      document.order_form.email.focus();
      return false;} 
   
   //-------------------------------------------shipping----------------------------------------------------
   //shipping name 
   if ( isempty( document.order_form.ship_name )) {
      document.order_form.ship_name.focus();
      alert("Please enter a value in the \"Ship To Name\" field.");
      return false;} 
   
   //Shipping address 
   if ( isempty( document.order_form.ship_address )) {
      document.order_form.ship_address.focus();
      alert("Please enter a value in the \"Ship To Address\" field.");
      return false;} 
   
   //contact city 
   if ( isempty( document.order_form.ship_city )) {
      document.order_form.ship_city.focus();
      alert("Please enter a value in the \"Ship To City\" field.");
      return false;}
   
   //contact zip 
   if ( iszipcode( document.order_form.ship_zip )) {
      document.order_form.ship_zip.focus();
      alert("Please enter a value in the \"Ship To Zip\" field.");
      return false;} 
   
   //-------------------------------------------------Credit Card----------------------------------------------------
   
   //Credit Card Name 
   if ( isempty( document.order_form.cc_name)) {
      document.order_form.cc_name.focus();
      alert("Please enter a value in the \"Credit Card Information Name\" field.");
      return false;}

   //Credit Card Name 
   if ( isempty( document.order_form.cc_number)) {
      document.order_form.cc_number.focus();
      alert("Please enter a value in the \"Credit Card Number\" field.");
      return false;}
   
   //Credit Card CVV 
   if ( isempty( document.order_form.cc_cvv_code)) {
      document.order_form.cc_cvv_code.focus();
      alert("Please enter a value in the \"Credit Card CVV Code\" field.");
      return false;}
   
}



function validate_order_edit_form(){
 
   //contact name 
   if ( isempty( document.order_edit_form.name )) {
      document.order_edit_form.name.focus();
      alert("Please enter a value in the \"Contact Information Name\" field.");
      return false;} 
   
   //contact address 
   if ( isempty( document.order_edit_form.address )) {
      document.order_edit_form.address.focus();
      alert("Please enter a value in the \"Contact Information Address\" field.");
      return false;} 
      
   //contact city 
   if ( isempty( document.order_edit_form.city )) {
      document.order_edit_form.city.focus();
      alert("Please enter a value in the \"Contact Information City\" field.");
      return false;}
   
   //contact zip 
   if ( iszipcode( document.order_edit_form.zip )) {
      document.order_edit_form.zip.focus();
      alert("Please enter a value in the \"Contact Information Zip\" field.");
      return false;} 
   
   //contact phone
   if ( isphonenumber( document.order_edit_form.phone )) {
      document.order_edit_form.phone.focus();
      alert("Please enter a valid Phone Number i.e. 1-888-549-5771 in the \"Contact Information Phone Number\" field.");
      return false;} 
   
   //contact email not empty 
   if ( isempty( document.order_edit_form.email )) {
      document.order_edit_form.email.focus();
      alert("Please enter a value in the \"Contact Information Email\" field.");
      return false;}

   //contact email
   if ( !checkemail( document.order_edit_form.email )) {
      document.order_edit_form.email.focus();
      return false;} 
   
   //-------------------------------------------shipping----------------------------------------------------
   //shipping name 
   if ( isempty( document.order_edit_form.ship_name )) {
      document.order_edit_form.ship_name.focus();
      alert("Please enter a value in the \"Ship To Name\" field.");
      return false;} 
   
   //Shipping address 
   if ( isempty( document.order_edit_form.ship_address )) {
      document.order_edit_form.ship_address.focus();
      alert("Please enter a value in the \"Ship To Address\" field.");
      return false;} 
   
   //contact city 
   if ( isempty( document.order_edit_form.ship_city )) {
      document.order_edit_form.ship_city.focus();
      alert("Please enter a value in the \"Ship To City\" field.");
      return false;}
   
   //contact zip 
   if ( iszipcode( document.order_edit_form.ship_zip )) {
      document.order_edit_form.ship_zip.focus();
      alert("Please enter a value in the \"Ship To Zip\" field.");
      return false;} 
   
   //-------------------------------------------------Credit Card----------------------------------------------------
   
   //Credit Card Name 
   if ( isempty( document.order_edit_form.cc_name)) {
      document.order_edit_form.cc_name.focus();
      alert("Please enter a value in the \"Credit Card Information Name\" field.");
      return false;}

   //Credit Card Name 
   if ( isempty( document.order_edit_form.cc_number)) {
      document.order_edit_form.cc_number.focus();
      alert("Please enter a value in the \"Credit Card Number\" field.");
      return false;}
   
   //Credit Card CVV 
   if ( isempty( document.order_edit_form.cc_cvv_code)) {
      document.order_edit_form.cc_cvv_code.focus();
      alert("Please enter a value in the \"Credit Card CVV Code\" field.");
      return false;}
   
}


//-----------------------------------------------------Regular Expression Checks----------------------------------------------
//this doesn't work
function isValidCreditCard(type,ccnum) {
  alert(type + ccnum);
  if (type == "VISA") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "MASTERCARD") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "DISCOVER") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "AMERICAN_EXP") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "DINERS") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   }
   if (re.test(ccnum)){
         alert 
      return true;
   }
   else return false;   
}


function checkemail(oField){
   var invalidaddress=new Array()
/* 
   invalidaddress[0]="hotmail"
   invalidaddress[1]="rocketmail"
   invalidaddress[2]="yahoo"
   invalidaddress[3]="zdnetmail"
*/
   var invalidcheck=0
   var testresults
   var str=oField.value
   var filter=/^.+@.+\..{2,3}$/
   
   if (isempty(oField)){
      return true;}
   
   if (filter.test(str)){
      var tempstring=str.split("@") 
      tempstring=tempstring[1].split(".") 
      for (i=0;i<invalidaddress.length;i++){
         if (tempstring[0]==invalidaddress[i]){
            invalidcheck=1 ;
         }
      }
      if (invalidcheck!=1){
         testresults=true ;
      }
      else{
         alert("Please input a more official email address.");
         testresults=false;
      }
   }
   else{
      alert("Please input a valid email address.");
      testresults=false;
   }
   if (testresults == false){
      oField.focus();
   }
   return (testresults);
}

function ishttp(ofld){
   var expr=/^http:\/\/\S+$/;
   return expr.test(ofld.value);
}
   
function isdate(ofld) {
var expr = /^\d{1,2}(\-|\/|\.)\d{1,2}(\-|\/|\.)(\d{2}|\d{4})$/;
   if ( expr.test(ofld.value) ) {
      return true;
   }
   
    expr = /^\d{1,2}(\-|\/|\.)\d{1,2}(\-|\/|\.)(\d{2}|\d{2})$/;
   if ( expr.test(ofld.value) ) {
      return true;
   }
   return false;
}

function iscurrency(ofld) {
   
   var expr = /(^\d*$)|(^\d+\.\d{1,2}$)/ ;
   
   if ( expr.test(ofld.value)) {
      return false;
   }
   return true;
}

function isempty(ofld) {
   var expr = /\S/ ;
   if ( expr.test(ofld.value)) {
      return false;
   }
   
   return true;
} 

function isphonenumber(ofld) {
   var expr = /^1\s*-?\s*(\d{3}|\(\s*\d{3}\s*\))\s*-?\s*\d{3}\s*-?\s*\d{4}$/ ;
   if ( expr.test(ofld.value)) {
      return false;
   }
   return true;
} 

function iszipcode(ofld) {
   var expr = /(^\d{5}$)|(^\d{5}-\d{4}$)/ ;
   if ( expr.test(ofld.value)) {
      return false;
   }
   return true;
}   

function isyear(ofld){
   var expr = /(^\d{4}$)/;
   if ( expr.test(ofld.value)) {
      return false;
   }
   return true;
} 

function enable_other_specified(){
   if ( document.proposal_form.tech_other.checked == true){
       document.proposal_form.other_specified.disabled = false;
       document.proposal_form.other_specified.focus();
   }
   else{document.proposal_form.other_specified.disabled = true;}
}

function autoFillForm( theForm ){

            if( confirm("Do You Want to AutoFill the " + theForm.id + "?")){
            
               var debug = new Array();

                        debug['name']="Rick Davenger";

                        debug['address']= "2801 S. Russell";

                        debug['city']= "Missoula";

                        debug['address2']= "Suite #15";

                        debug['zip']= "59801";

                        debug['phone']= "1-406-721-8876";

                        debug['email']= "dustin@univision-computers.com";

                        debug['cc_name']= "Dustin";

                        debug['cc_number']= "1234123412341234";

                        debug['cc_cvv_code']= "123";

                        debug['ship_name']= "Rick Davenger";

                        debug['ship_address']= "2801 S Russell";

                        debug['ship_address2']= "Suite 15";

                        debug['ship_city']= "Missoula";

                        debug['ship_zip']= "59801";

                        debug['ship_phone']= "1-406-721-8876";
 

                        var inputElements = theForm.elements.length;

                        for( var i = 0; i < inputElements; i++){

                                    if( debug[ theForm.elements[i].name ] ){

                                                theForm.elements[i].value = debug[ theForm.elements[i].name ];
                                    }

                        } //end of for

            }//end of if( confirm )

}// end of function

function confirmDelete(pk,name,page,args){
   reply = confirm( "Are you sure you want to delete " + name + " ?", "OK" );
   if(reply){
      window.location = page + "?delete=" + pk + args;
   }
}

