/**
 * @author Chris Shenk
 */
function changeInputType(id,name,type,size){
	var el = document.getElementById(id);
	var prnt = el.parentNode;
	prnt.removeChild(el);
	prnt.innerHTML += '<input size="'+size+'" name="'+name+'" type="'+type+'" id="'+id+'" value="'+el.value + '">';
	el = null; // remove any reference to the old element
}
function changeRef(){
	val = document.getElementById('ref_sel').value;
	if (val=="Other"){
		document.getElementById('ref').value = "";
		changeInputType("ref","ref","text",30);
	}
	else{
		changeInputType("ref","ref","hidden",30);
		document.getElementById('ref').value = val;
	}
}
function changeAcc(){
	val = document.getElementById('acc_sel').value;
	if (val=="Other"){
		document.getElementById('acc').value = "";
		changeInputType("acc","acc","text",30);
	}
	else{
		changeInputType("acc","acc","hidden",30);
		document.getElementById('acc').value = val;
	}
}
function hideShowCC(){
	var cc = document.getElementById('cc');
	var but = document.getElementById('hidecc');
	var inp = document.getElementById('ccinput');
	if (cc.type=="text"){
		changeInputType("cc","cc","password",16);
		inp.value = "password";
		but.value = "Show Credit Card Number"
	}
	else{
		changeInputType("cc","cc","text",16);
		inp.value = "text";
		but.value = "Hide Credit Card Number"
	}
}
function ccSameName(){
	var fname = document.getElementById('first_name').value;
	var lname = document.getElementById('last_name').value;
	document.getElementById('ccname').value = fname+" "+lname;
}
function checkReservation(num){
	var errors = new Array();
	var i = 0;
	if (document.getElementById('first_name').value=="") errors[i++] = "first name";
	if (document.getElementById('last_name').value=="") errors[i++] = "last name";
	if (document.getElementById('address').value=="") errors[i++] = "address";
	if (document.getElementById('city').value=="") errors[i++] = "city";
	if (document.getElementById('state').value=="") errors[i++] = "state";
	if (document.getElementById('acc').value=="") errors[i++] = "where you are staying";
	if (document.getElementById('zip').value==""||document.getElementById('zip').value.length<5) errors[i++] = "zip code";
	if ((document.getElementById('day_phone').value==""||document.getElementById('day_phone').value.length<10)&&
		(document.getElementById('night_phone').value==""||document.getElementById('night_phone').value.length<10)) errors[i++] = "phone number";
	if (document.getElementById('cc').value.length>0&&document.getElementById('cc').value.length<16) errors[i++] = "valid credit card (or no credit card)";
	if (document.getElementById('cc').value.length==16){
		if (document.getElementById('cctype').value=="") errors[i++] = "credit card type";
		if (document.getElementById('ccname').value=="") errors[i++] = "name on credit card";
		if (document.getElementById('ccexpmo').value==""||document.getElementById('ccexpyr').value=="") errors[i++] = "credit card expiration date";
	}
	if (document.getElementById('pu_month').value==""||document.getElementById('pu_day').value==""||document.getElementById('pu_year').value=="") errors[i++] = "pick up date";
	if (document.getElementById('in_month').value==""||document.getElementById('in_day').value==""||document.getElementById('in_year').value=="") errors[i++] = "return date";
	var j;
	for (j=1;j<=num;j++){
		if (document.getElementById('name'+j).value=="") continue;
		if (document.getElementById('age'+j).value=="") errors[i++] = "age for reservation #"+j;
		if (document.getElementById('h_ft'+j).value==""||document.getElementById('h_in'+j).value=="") errors[i++] = "height for reservation #"+j;
		if (document.getElementById('package'+j).value=="") errors[i++] = "package for reservation #"+j;
	}
	if (errors.length==0){
		return true;
	}
	else{
		var x;
		var error = "";
		for (x in errors){
			error += error==""?errors[x]:", "+errors[x];
		}
		document.getElementById('errors').innerHTML = "You must fill in the following fields:<br><br>"+error;
		return false;
	}
}



// Credit Card Validation
/* 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() {  // v2.0
var ccNumb = document.getElementById('cc').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("This is NOT a valid Credit Card Number!");
}
  return bResult; // Return the results
}

