// Add/Remove valid types of cards accepted or set invalid types to zero
var oCardTypes = {'mastercard':1,'visa':2,'american express':3,'discover':0};
var oChrVals   = {'first':'-1','second':'-1','third':'-1','two':'','four':''};
// Helper Functions
function gO(x){return document.getElementById(x);}
function msg(m,x){alert(m);x.focus(); return false;}
// iV() short for inputValue
function iV(x){return gO(x).value;}
// sIdx() short for selectedIndex property of provided select object
function sIdx(x){return gO(x).selectedIndex;}
// sV() short for selectedValue
function sV(x){
 var s_idx = sIdx(x);
 return ((s_idx < 0) ? null : gO(x).options[s_idx].value);
}
function getLastDayOfMonth(yr,mn){
 if(mn==12){mn=1;yr++;}else{mn++;}
 return ((new Date()).setTime((new Date(yr,mn,1)).getTime() - 86400000));
}
function die(fid, msg){
 alert(msg);
 gO(fid).focus();
 gO(fid).select();
 return false;
}
function update_someoneelse(){
 var r_y = gO("usr_usrtype_id_y");
 var r_s = gO("usr_usrtype_id_s");
 if(r_y == null) return;
 if(r_s == null) return;
 if(r_y.checked)
  gO("someoneelse").style.display = "none";
 else
  gO("someoneelse").style.display = "block";
}
function get_started_onload(){
 update_someoneelse();
}
function validate_form(){
 if (gO("usr_vpswd").value != gO("usr_pswd").value)
  return die("usr_pswd", "Could not validate your password, please re-enter.");
 if (gO("usr_vemail").value != gO("usr_email").value)
  return die("usr_email", "Could not validate your e-mail address, please re-enter.");
 return true;
}
function driver_form_start(){
 var fo = gO("usr_fname");
 if (fo.value == "")
 	fo.value = "Enter your name...";
 fo.focus();
 fo.select();
}
function validateDriverForm(){
 if (! gO("terms1").checked)
 	return msg("Please check the first checkbox stating that you do have a cell phone and agree to use it in communication with DDO.",gO("terms1"));
 if (! gO("terms2").checked)
  return msg("Please check the second checkbox stating that you agree to maintain an availability schedule on a weekly basis.",gO("terms2"));
 if (! gO("terms3").checked)
  return msg("Please check the third checkbox stating that you will maintain a teritory within which to pickup customers.",gO("terms3"));
 if (! gO("terms4").checked)
  return msg("Please check the fourth checkbox stating that you will submit a digital photo.",gO("terms4"));
 if (gO("usr_fname").value == "")
  return msg("The first name field is required.",gO("usr_fname"));
 if (gO("usr_lname").value == "")
  return msg("The last name field is required.",gO("usr_lname"));
 if (gO("usr_address").value == "")
  return msg("The address field is required.",gO("usr_address"));
 if (gO("usr_city").value == "")
  return msg("The city field is required.",gO("usr_city"));
 if (gO("usr_zoc").value == "")
  return msg("The zip code field is required.",gO("usr_zip"));
 if (gO("usr_phonehome").value == "")
  return msg("The primary phone number field is required.",gO("usr_phone"));
 if (gO("usr_email").value == "")
  return msg("The e-mail address field is required.",gO("usr_email"));
 return true;
}
function cc_validate(){
 var r_v = cc_verify(sV('cc_type'), iV('cc_number'));
 var exp = getLastDayOfMonth(sV('cc_expyear'), sV('cc_expmonth'));
 var now = new Date().getTime();
 if(r_v != ''){
  alert(r_v + '. Please check your credit card information.');
  return false;
 }
 if(exp < now){
  alert('The card expiration date has passed, please check the value or select a different card.');
  return false;
 }
 return true;
}
// Verify credit card number of valid/acceptable type
function cc_verify(type, ccno){
 var total = 0, tmp = 0;
 var num = '', len = ccno.length;
 // Make sure there are only numbers in the string...
 for(i = 0; i < len; i++)
  if(ccno.charAt(i) >= '0'&&ccno.charAt(i) <= '9')
   num = num + ccno.charAt(i);
 // Shorter than allowed for any type
 if(len < 13) return 'Card length is too short';
 // Retrieve first few chars from the card num
 if(isNaN(num.charAt(0))) return 'The first character is not a number';
 oChrVals.first = num.charAt(0).toString();
 if(isNaN(num.charAt(1))) return 'The second character is not a number';
 oChrVals.second = num.charAt(1).toString();
 if(isNaN(num.charAt(2))) return 'The third character is not a number';
 oChrVals.third = num.charAt(2).toString();
 if(isNaN(num.charAt(3))) return 'The fourth character is not a number';
 oChrVals.two = oChrVals.first.concat(oChrVals.second);
 oChrVals.four = oChrVals.two.concat(oChrVals.third, num.charAt(3).toString());
 // Length vs. Type
 var itype = parseInt(oCardTypes[type.toLowerCase()], 10);
 switch (itype){
 case 1:
  if(oChrVals.first != '5' || oChrVals.second < '1' || oChrVals.second > '5')
   return 'Invalid card prefix';
  if(len != 16)
   return 'Invalid card length';
  break;
 case 2:
  if(oChrVals.first != "4")
   return 'Invalid card prefix';
  if(len != 13 && len != 16)
   return 'Invalid card length';
  break;
 case 3:
  if(oChrVals.first != '3' || (oChrVals.second != '4' && oChrVals.second != '7'))
   return 'Invalid card prefix';
  if(len != 15)
   return 'Invalid card length';
  break;
 case 4:
  if(oChrVals.first != '6')
   return 'Invalid card prefix';
  if(len != 13)
   return 'Invalid card length';
  break;
 default:
  return 6;
 }
 // Mod 10 Card Number Test
 for(loc = (len - 2); loc >= 0; loc -= 2){
  total += 1 * num.charAt(loc + 1);
  tmp = (num.charAt(loc) * 2);
  if(tmp > 9) total += 1;
  total += (tmp % 10);
 }
 if((len % 2) > 0)
  total += 1 * num.charAt(0);
 return ((total % 10) ? 'Invalid card number' : '');
}
/*EOF*/

