
// personal info page
function validateForm1OnSubmit(theForm) {
// bool for form checking
var formchecking = "1";

if(formchecking != "0") {
  var reason = "";
  
	reason += validateFirstname(theForm.firstname);
	reason += validateLastname(theForm.lastname);
	reason += validatePhone(theForm.Phone);
	reason += validatePhone(theForm.Phone1);
	reason += validatePhone(theForm.Phone2);
	reason += validateAddress(theForm.Address);
	reason += validateAddress(theForm.Address3);
	reason += validateAddress(theForm.Address4);
	reason += validateAddress(theForm.Address5);
	reason += validateAddress(theForm.Address6);
	reason += validateEmail(theForm.Email);
  
		
	if (reason != "") {
	  alert("You forgot to fill in required information:\n" + reason);
	  return false;
	}

  return true;
} else {
	return true;
}
}

// billing info page
function validateForm2OnSubmit(theForm) {
// bool for form checking
var formchecking = "1";

if(formchecking != "0") {  
  	var reason = "";
	
	reason += ccVal(theForm.ccNum, theForm.cctype);
	reason += validateCCV(theForm.ccCCV2);
	reason += validateExp(theForm.ccExp1);
	reason += validateExp(theForm.ccExp);
	reason += validateName(theForm.ccName);
	reason += validateAddress(theForm.ccBill);
	reason += validateAddress(theForm.ccBill3);
	reason += validateAddress(theForm.ccBill4);
	reason += validateAddress(theForm.ccBill5);
	reason += validateAddress(theForm.ccBill6);
	
	
			
	if (reason != "") {
	  alert("You forgot to fill in required information:\n" + reason);
	  return false;
	} else {
	  return true;
	}
 }
}


// legal info page
function validateForm3OnSubmit(theForm) {
// bool for form checking
var formchecking = "1";

if(formchecking != "0") {  
  	var reason = "";
	
	reason += validateSSN(theForm.ssn);
	reason += validateSSN(theForm.ssn1);
	reason += validateSSN(theForm.ssn2);
	//month
	reason += validateBDate(document.legal[3]);
	//day
	reason += validateBDate(document.legal[4]);
	//year
	reason += validateBDate(document.legal[5]);
	reason += validateLegalCheck(theForm.acknowledgement);
	
			
	if (reason != "") {
	  alert("You forgot to fill in required information:\n" + reason);
	  return false;
	} else {
	  return true;
	}
 }
}

// email contact page
function validateContactForm(theForm) {
// bool for form checking
var formchecking = "1";

if(formchecking != "0") {  
  	var reason = "";
	
	reason += validateFirstname(theForm.contactFirst);
	reason += validateLastname(theForm.contactLast);
	reason += validatePhone(theForm.contactPhone);
	reason += validatePhone(theForm.contactPhone1);
	reason += validatePhone(theForm.contactPhone2);
	reason += validateEmail(theForm.contactEmail);
	reason += validateMessage(theForm.contactMessage);
	reason += validateSecurity(theForm.security_code);
	
			
	if (reason != "") {
	  alert("You forgot to fill in required information:\n" + reason);
	  return false;
	} else {
	  return true;
	}
 }
}
/*========================================================================*/
/* Functions                                                              */
/*========================================================================*/

//validate empty field
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#ffdfdf';
        error = fld.name + '\n';
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

//validate contact message
function validateMessage(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#ffdfdf';
        error = 'Please enter a message\n';
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

//validate security message
function validateSecurity(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#ffdfdf';
        error = 'Please enter the security code';
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

//validate if checked
function validateLegalCheck(fld) {
    var error = "";
 
    if (fld.checked == 0) {
        fld.style.background = '#ffdfdf';
        error = 'Please Check the Box to Agree.\n';
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

//validate credit card ccv
function validateCCV(fld) {
    var error = "";
    var illegalChars = /[^0-9]/;
    if (fld.value.length == 0) {
        fld.style.background = '#ffdfdf';
        error = "Please enter security code.\n";
    } else if (illegalChars.test(fld.value)) {
		fld.style.background = '#ffdfdf'; 
        error = "Security Code should only have numbers.\n";
	} else {
        fld.style.background = 'White';
    }
    return error;  
}

//validate credit card exp date
function validateExp(fld) {
    var error = "";
    var illegalChars = /[^0-9]/;
	//alert("field.name"+fld.name);
	//alert("fld"+fld);
	if (fld.name == "ccExp1") {
		if (fld.value.length == 0) {
        	fld.style.background = '#ffdfdf';
        	error = "Please enter month.\n";
		} else if (fld.value > 12) {
			fld.style.background = '#ffdfdf';
        	error = "Please enter a valid month.\n";
		} else if (illegalChars.test(fld.value)) {
			fld.style.background = '#ffdfdf';
        	error = "Month has invalid characters.\n";
		} else {
			fld.style.background = 'White';
		}
    } else if (fld.name == "ccExp") {
		if ((fld.value.length == 0) || (fld.value.length < 4)){
        	fld.style.background = '#ffdfdf';
        	error = "Please enter year.\n";
		} else if (fld.value < 2009) {
			//yeah that year is hack job, will change later
			fld.style.background = '#ffdfdf';
        	error = "Card appears to be expired.\n";
		} else if (illegalChars.test(fld.value)) {
			fld.style.background = '#ffdfdf';
        	error = "Year has invalid characters.\n";
		} else {
			fld.style.background = 'White';
		}
	}
    return error;  
}

//validate ssn parts
function validateSSN(fld) {
    var error = "";
    var illegalChars = /[^0-9]/;
	if (fld.name == "ssn") {
		if ((fld.value.length == 0) || (fld.value.length < 3)){
			fld.style.background = '#ffdfdf';
			error = "Please enter first part of SSN.\n";
		} else if (illegalChars.test(fld.value)) {
			fld.style.background = '#ffdfdf'; 
			error = "First part of SSN should only have numbers.\n";
		} else {
			fld.style.background = 'White';
		}
	} else if (fld.name == "ssn1") {
		if ((fld.value.length == 0) || (fld.value.length < 2)){
			fld.style.background = '#ffdfdf';
			error = "Please enter second part of SSN.\n";
		} else if (illegalChars.test(fld.value)) {
			fld.style.background = '#ffdfdf'; 
			error = "Second part of SSN should only have numbers.\n";
		} else {
			fld.style.background = 'White';
		}
	} else if (fld.name == "ssn2") {
		if ((fld.value.length == 0) || (fld.value.length < 4)){
			fld.style.background = '#ffdfdf';
			error = "Please enter third part of SSN.\n";
		} else if (illegalChars.test(fld.value)) {
			fld.style.background = '#ffdfdf'; 
			error = "Third part of SSN should only have numbers.\n";
		} else {
			fld.style.background = 'White';
		}
	} 
		
    return error;  
}

//validate birth date
function validateBDate(fld) {
    var error = "";
    var illegalChars = /[^0-9]/;
	//month
	if (fld.name == "bDate-1") {
		
		if ((fld.value.length == 0) || (fld.value.length < 2)) {
        	fld.style.background = '#ffdfdf';
        	error = "Please enter month.\n";
		} else if ((fld.value > 12) || (fld.value == 0)) {
			fld.style.background = '#ffdfdf';
        	error = "Please enter a valid month.\n";
		} else if (illegalChars.test(fld.value)) {
			fld.style.background = '#ffdfdf';
        	error = "Month has invalid characters.\n";
		} else {
			fld.style.background = 'White';
		}
	//day
    } else if (fld.name == "bDate-2") {
		
		if ((fld.value.length == 0) || (fld.value.length < 2)){
        	fld.style.background = '#ffdfdf';
        	error = "Please enter day.\n";
		} else if ((fld.value > 31) || (fld.value == 0)) {
			//yeah that year is hack job, will change later
			fld.style.background = '#ffdfdf';
        	error = "Please enter a valid day.\n";
		} else if (illegalChars.test(fld.value)) {
			fld.style.background = '#ffdfdf';
        	error = "Day has invalid characters.\n";
		} else {
			fld.style.background = 'White';
		}
	//year
	} else if (fld.name == "bDate") {
		
		if ((fld.value.length == 0) || (fld.value.length < 4)){
        	fld.style.background = '#ffdfdf';
        	error = "Please enter year.\n";
		} else if (fld.value < 1900) {
			//yeah that year is hack job, will change later
			fld.style.background = '#ffdfdf';
        	error = "Please enter a valid year.\n";
		} else if (illegalChars.test(fld.value)) {
			fld.style.background = '#ffdfdf';
        	error = "Year has invalid characters.\n";
		} else {
			fld.style.background = 'White';
		}
	}
    return error;  
}

//validate addresses
function validateAddress(fld) {
    var error = "";
    var illegalChars1 = /[0-9]/;
	var illegalChars2 = /[^a-zA-Z ]/;
    if (fld.value.length == 0) {
        fld.style.background = '#ffdfdf';
		if ((fld.name == "Address") || (fld.name == "ccBill")){
        	error = "You didn't enter a Street Address.\n";
		} else if ((fld.name == "Address3") || (fld.name == "ccBill3")){
			error = "You didn't enter a City.\n";
		} else if ((fld.name == "Address4") || (fld.name == "ccBill4")){
			error = "You didn't enter a State.\n";
		} else if ((fld.name == "Address5") || (fld.name == "ccBill5")){
			error = "You didn't enter a Zip Code.\n";
		} else if ((fld.name == "Address6") || (fld.name == "ccBill6")){
			error = "A Country is required.\n";
		}
    } else {
        fld.style.background = 'White';
    }
	switch(fld.name) {
		case "Address3":
			if (illegalChars1.test(fld.value)) {
				fld.style.background = '#ffdfdf'; 
        		error = "City shouldn't have numbers in it.\n";
			}
		break;
		
		case "Address4":
			if (illegalChars2.test(fld.value)) {
				fld.style.background = '#ffdfdf'; 
        		error = "State has invalid characters.\n";
			}
		break;
		
		case "Address5":
			if (fld.value.length < 5) {
				fld.style.background = '#ffdfdf'; 
				if(error ==""){
					error = "Zip Code is too short.\n";
				}
			}
		break;
		
		case "ccBill3":
			if (illegalChars1.test(fld.value)) {
				fld.style.background = '#ffdfdf'; 
        		error = "City shouldn't have numbers in it.\n";
			}
		break;
		
		case "ccBill4":
			if (illegalChars2.test(fld.value)) {
				fld.style.background = '#ffdfdf'; 
        		error = "State has invalid characters.\n";
			}
		break;
		
		case "ccBill5":
			if (fld.value.length < 5) {
				fld.style.background = '#ffdfdf'; 
				if(error ==""){
					error = "Zip Code is too short.\n";
				}
			}
		break;
		
	}
		
		
    return error;  
}

//validate full name field
function validateName(fld) {
    var error = "";
    var illegalChars = /[^a-zA-Z ]/; // allow letters and spaces
 
    if (fld.value == "") {
        fld.style.background = '#ffdfdf'; 
        error = "You didn't enter a name.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 35)) {
        fld.style.background = '#ffdfdf'; 
        error = "Name is wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#ffdfdf'; 
        error = "Name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

//validate first name field
function validateFirstname(fld) {
    var error = "";
    var illegalChars = /[^a-zA-Z]/; // allow letters
 
    if (fld.value == "") {
        fld.style.background = '#ffdfdf'; 
        error = "You didn't enter a first name.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 15)) {
        fld.style.background = '#ffdfdf'; 
        error = "The first name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#ffdfdf'; 
        error = "The first name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

//validate last name field
function validateLastname(fld) {
    var error = "";
    var illegalChars = /[^a-zA-Z]/; // allow letters
 
    if (fld.value == "") {
        fld.style.background = '#ffdfdf'; 
        error = "You didn't enter a last name.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 20)) {
        fld.style.background = '#ffdfdf'; 
        error = "The last name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#ffdfdf'; 
        error = "The last name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

//validate password if/when needed
function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = '#ffdfdf';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = '#ffdfdf';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = '#ffdfdf';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = '#ffdfdf';
    } else {
        fld.style.background = 'White';
    }
   return error;
} 


function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

//validate email address
function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = '#ffdfdf';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#ffdfdf';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#ffdfdf';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

//validate phone number fields one section at a time
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

	switch (fld.name) {
		case "Phone":
		   if (fld.value.length == "") {
				error = "You didn't enter an area code.\n";
				fld.style.background = '#ffdfdf';
			} else if (isNaN(parseInt(stripped))) {
				error = "The phone number contains illegal characters.\n";
				fld.style.background = '#ffdfdf';
			} else if (!(stripped.length == 3)) {
				error = "The phone number is missing digits.\n";
				fld.style.background = '#ffdfdf';
			} else {
				fld.style.background = 'White';
			}
		break;
		
		case "Phone1":
		   if (fld.value == "") {
				error = "The second part of the phone number is missing digits.\n";
				fld.style.background = '#ffdfdf';
			} else if (isNaN(parseInt(stripped))) {
				error = "The second part of the phone number contains illegal characters.\n";
				fld.style.background = '#ffdfdf';
			} else if (!(stripped.length == 3)) {
				error = "The second part of the phone number is missing digits.\n";
				fld.style.background = '#ffdfdf';
			} else {
				fld.style.background = 'White';
			}
		break;
		
		case "Phone2":
		   if (fld.value == "") {
				error = "The third part of the phone number is missing digits.\n";
				fld.style.background = '#ffdfdf';
			} else if (isNaN(parseInt(stripped))) {
				error = "The third part of the phone number contains illegal characters.\n";
				fld.style.background = '#ffdfdf';
			} else if (!(stripped.length == 4)) {
				error = "The third part of the phone number is missing digits.\n";
				fld.style.background = '#ffdfdf';
			} else {
				fld.style.background = 'White';
			}
		break;
		
		case "contactPhone":
		   if (fld.value.length == "") {
				error = "You didn't enter an area code.\n";
				fld.style.background = '#ffdfdf';
			} else if (isNaN(parseInt(stripped))) {
				error = "The phone number contains illegal characters.\n";
				fld.style.background = '#ffdfdf';
			} else if (!(stripped.length == 3)) {
				error = "The phone number is missing digits.\n";
				fld.style.background = '#ffdfdf';
			} else {
				fld.style.background = 'White';
			}
		break;
		
		case "contactPhone1":
		   if (fld.value == "") {
				error = "The second part of the phone number is missing digits.\n";
				fld.style.background = '#ffdfdf';
			} else if (isNaN(parseInt(stripped))) {
				error = "The second part of the phone number contains illegal characters.\n";
				fld.style.background = '#ffdfdf';
			} else if (!(stripped.length == 3)) {
				error = "The second part of the phone number is missing digits.\n";
				fld.style.background = '#ffdfdf';
			} else {
				fld.style.background = 'White';
			}
		break;
		
		case "contactPhone2":
		   if (fld.value == "") {
				error = "The third part of the phone number is missing digits.\n";
				fld.style.background = '#ffdfdf';
			} else if (isNaN(parseInt(stripped))) {
				error = "The third part of the phone number contains illegal characters.\n";
				fld.style.background = '#ffdfdf';
			} else if (!(stripped.length == 4)) {
				error = "The third part of the phone number is missing digits.\n";
				fld.style.background = '#ffdfdf';
			} else {
				fld.style.background = 'White';
			}
		break;
	}
    return error;
}



/*============================================================================*/
/*credit card processing                                                      */
/*============================================================================*/
function ccVal(cardNumberF, cardTypeF)
{
  var error = "";
  var cardType = "";
  var cardNumber = "";
  
  cardType = getCheckedValue(cardTypeF);
  cardNumber = (cardNumberF.value);
  
  var isValid = false;
  var ccCheckRegExp = /[^\d ]/;
  isValid = !ccCheckRegExp.test(cardNumber);
  if (!isValid){
	  error = "Please remove formatting characters\n";
	  cardNumberF.style.background = '#ffdfdf';
  }
  if (isValid)
  {
	
    var cardNumbersOnly = cardNumber.replace(/ /g,"");
	
    var cardNumberLength = cardNumbersOnly.length;
    var lengthIsValid = false;
    var prefixIsValid = false;
	var cardFound = false;
    var prefixRegExp;

    switch(cardType)
    {
      case "Mastercard":
        lengthIsValid = (cardNumberLength == 16);
        prefixRegExp = /^5[1-5]/;
		cardFound = true;
        break;

      case "Visa":
        lengthIsValid = (cardNumberLength == 16 || cardNumberLength == 13);
        prefixRegExp = /^4/;
		cardFound = true;
        break;

      case "American Express":
        lengthIsValid = (cardNumberLength == 15);
        prefixRegExp = /^3(4|7)/;
		cardFound = true;
        break;

  	  case "Discovery":
        lengthIsValid = (cardNumberLength == 16);
        prefixRegExp = /^6011/;
		cardFound = true;
        break;

      default:
        prefixRegExp = /^$/;
		error = "Card type not found, please select one\n";
		cardTypeF[0].focus();
        
    }
	if (cardFound) {
	  prefixIsValid = prefixRegExp.test(cardNumbersOnly);
	  
	  if (!lengthIsValid){
		  error = "Wrong number of digits for card type\n";
		  cardNumberF.style.background = '#ffdfdf';
	  } else if(!prefixIsValid){
		  error = "Card number doesn't match card type\n";
		  cardNumberF.style.background = '#ffdfdf';
	  }
	  isValid = prefixIsValid && lengthIsValid;
	}
  }

  if (isValid)
  {
	
    var numberProduct;
    var numberProductDigitIndex;
    var checkSumTotal = 0;

    for (digitCounter = cardNumberLength - 1; 
      digitCounter >= 0; 
      digitCounter--)
    {
      checkSumTotal += parseInt (cardNumbersOnly.charAt(digitCounter));
      digitCounter--;
      numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
      for (var productDigitCounter = 0;
        productDigitCounter < numberProduct.length; 
        productDigitCounter++)
      {
        checkSumTotal += 
          parseInt(numberProduct.charAt(productDigitCounter));
      }
    }
	
	if (checkSumTotal % 10 != 0) {
		error = "Card number seems to be invalid, please check\n";
		cardNumberF.style.background = '#ffdfdf';
	} else {
		cardNumberF.style.background = 'White';
	}
    
  }


 
  return error;
}

// gets checked value from radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}