//----------------------------------------------------------------------------------------------------------------------
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: David Leppek :: https://www.azcode.com/Mod10

Basically, the alorithum takes each digit, from right to left and muliplies each second
digit by two. If the multiple is two-digits long (i.e.: 6 * 2 = 12) the two digits of
the multiple are then added together for a new number (1 + 2 = 3). You then add up the 
string of numbers, both unaltered and new values and get a total sum. This sum is then
divided by 10 and the remainder should be zero if it is a valid credit card. Hense the
name Mod 10 or Modulus 10. */
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
    }
  }
}
// change alert to on-page display or other indication as needed.
if(!bResult){
  alert("The credit card number you have provided is not valid!");
  return false;
}
  return true; // Return the results
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
//----------------------------------------------------------------------------------------------------------------------
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//----------------------------------------------------------------------------------------------------------------------
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
//----------------------------------------------------------------------------------------------------------------------
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//----------------------------------------------------------------------------------------------------------------------
function planned(el){
	$(el).style.display='block';
}
//----------------------------------------------------------------------------------------------------------------------
function unplanned(el){
	$(el).style.display='none';
}
//----------------------------------------------------------------------------------------------------------------------
function show(el){
	elem = $(el).style.display;
	if(elem=='none'){
		$(el).style.display='block';
	} else {
		$(el).style.display='none';
	}
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
function change_requirement(rid,field_name,field_value){
	//alert(rid+"-"+field_name+"-"+field_value);
	var url = 'inc/get_info.php';
	var para = 'req=change_requirement&rid=' + rid + '&field_name=' + field_name + '&field_value=' + field_value;
 //alert(para);
	/*generate ajax request to query info from the database and returnit from the getinfo.php file.*/
	var myAjax = new Ajax.Request
		(
			url,
				{
					method: 'get',
					parameters: para
				}
		);
	}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
function change_value(rid,field_name,id){
  var field_value = $(id).value;
  //alert(field_value);
	//alert(rid+"-"+field_name+"-"+field_value);
	var url = 'inc/get_info.php';
	var para = 'req=change_value&rid=' + rid + '&field_name=' + field_name + '&field_value=' + field_value;
 //alert(para);
	/*generate ajax request to query info from the database and returnit from the getinfo.php file.*/
	var myAjax = new Ajax.Request
		(
			url,
				{
					method: 'get',
					parameters: para,
					onComplete: shownewtotal
				}
			);
	}
//---------------------------------------------------------------------------------------------------------------------
// Show results from value update
	function shownewtotal(originalRequest)
		{
			var info = originalRequest.responseText;
			//alert(info);
			$('r_total_fee').value = info;
		}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
function update_value(rid,field_name,id){
  var field_value = $(id).value;
  //alert(field_value);
	//alert(rid+"-"+field_name+"-"+field_value);
	var url = 'inc/get_info.php';
	var para = 'req=update_value&rid=' + rid + '&field_name=' + field_name + '&field_value=' + field_value;
 //alert(para);
	/*generate ajax request to query info from the database and returnit from the getinfo.php file.*/
	var myAjax = new Ajax.Request
		(
			url,
				{
					method: 'get',
					parameters: para
				}
			);
	}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
function make_bill_ship(){
  if(jQuery('#same_check').attr('checked') == true){
  jQuery('#r_ship_fname').val(jQuery('#r_bill_fname').val());
  jQuery('#r_ship_lname').val(jQuery('#r_bill_lname').val());
  jQuery('#r_ship_address').val(jQuery('#r_bill_address').val());
  jQuery('#r_ship_city').val(jQuery('#r_bill_city').val());
  jQuery('#r_ship_state').val(jQuery('#r_bill_state').val());
  jQuery('#r_ship_zip').val(jQuery('#r_bill_zip').val());
  } else {
  jQuery('#r_ship_fname').val('');
  jQuery('#r_ship_lname').val('');
  jQuery('#r_ship_address').val('');
  jQuery('#r_ship_city').val('');
  jQuery('#r_ship_state').val('');
  jQuery('#r_ship_zip').val(''); 
  }
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
function add_ereg(cost)
{  
  v = jQuery('#ereg').attr('checked') ? cost : 0;
  jQuery('#ereg_cost').html(parseFloat(v).toFixed(2))
  total = parseFloat(jQuery('#dmv_cost').html().replace(',','')) + parseFloat(jQuery('#cartagz_cost').html().replace(',', '')) + parseFloat(jQuery('#ship_cost').html().replace(',', '')) + parseFloat(jQuery('#ereg_cost').html().replace(',',''));
  if(jQuery('#convenience_cost'))
  {
    total += parseFloat(jQuery('#convenience_cost').html().replace(',',''));
  }
  jQuery('#total_cost').html(parseFloat(total).toFixed(2));
  

}
function add_shipping(cost,rid){
  jQuery('#ship_cost').html(parseFloat(cost).toFixed(2));
  jQuery('#order_button').disabled = true;

  var field_value = cost;
  var field_name = 'r_ship_fee';
  //alert(field_value);
	//alert(rid+"-"+field_name+"-"+field_value);
	var url = '../inc/get_info.php';
	var para = 'req=change_value&rid=' + rid + '&field_name=' + field_name + '&field_value=' + field_value + '&null=null';
 //alert(para);
	/*generate ajax request to query info from the database and returnit from the getinfo.php file.*/
	var myAjax = new Ajax.Request
		(
			url,
				{
					method: 'get',
					parameters: para,
					onComplete: show_totals
				}
			);
	}
function toggleAutoLookup()
{
  var url = '../inc/toggle_auto_lookup.php';
  var myAjax = new Ajax.Request
  (
    url,
    {
      method: 'get',
      onComplete: function(e) 
      { 
        var a = jQuery('#auto_lookup');
        a.innerHTML = e.responseText;
        a.className = e.responseText;
      }
      //onComplete: function(e) { $('auto_lookup').innerHtml = e.responseText;}
    }
  );
}
//----------------------------------------------------------------------------------------------------------------------
// Show results from value update
	function show_totals(originalRequest)
		{
			var info = originalRequest.responseText;
		//	alert(info);
			jQuery('#total_cost').html(info);
			jQuery('#total_cost2').html(info);
                        jQuery('#order_button').disabled = false;
		}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
function setnote(){
  var opt = $F('auto_note');
  var put = $('r_staff_note').value;
  if(opt == 'z')
    {
      $('r_staff_note').value = "";
    } else {
        var nid = opt;
        //alert(nid);
      	var url = 'inc/get_info.php';
      	var para = 'req=change_note&nid=' + nid + '&null=null';
       //alert(para);
      	/*generate ajax request to query info from the database and returnit from the getinfo.php file.*/
      	var myAjax = new Ajax.Request
      		(
      			url,
      				{
      					method: 'get',
      					parameters: para,
      					onComplete: show_note
      				}
      			);  
  
    }

	}
//----------------------------------------------------------------------------------------------------------------------
// Show results from value update
	function show_note(originalRequest)
		{
			var info = originalRequest.responseText;
			//alert(info);
			$('r_staff_note').value = info;
		}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
function show_new(){
	var url = '../inc/get_info.php';
	var para = 'req=show_new';
 //alert(para);
	/*generate ajax request to query info from the database and returnit from the getinfo.php file.*/
	var myAjax = new Ajax.Request
		(
			url,
				{
					method: 'get',
					parameters: para,
					onComplete: show_new_ret
				}
			);
	}
//----------------------------------------------------------------------------------------------------------------------
// Show results from value update
	function show_new_ret(originalRequest)
		{
		    var info = originalRequest.responseText;
       		 $('count').value = info;
		}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------

