/** 
 * This file contains common JavaScript utilities webpage.  
 * 
 * Written by Matthew C. Hoover, Kurtz Bros. - Feb 21, 2002
 * Updated Sep 21, 2004 - Added check to valid credit card function to 
 * make sure that the user has selected an actual credit card type 
 * when they enter a credit card number.
 * Updated Aug 29, 2009 - Make sure length of CVV matches the card 
 * type.
 */
     
/**
 * Method to load a help page in a new window.
 *
 * @param String url to load
 */
function help(myurl) { 
	myurl = '/help/' + myurl + '.html';
	var config = "toolbar=no,width=400,height=400,status=no,"
			 + "scrollbars=yes,resize=no"; 
	window.open(myurl, 'max', config);
	return false;
}

/** 
 * Function that opens up the Thawte window to check on SSL certificate
 * validity.
 */
function OpenCertDetails(){
	var url='https://www.thawte.com/cgi/server/certdetails.exe?code=USKURT17-1X';
	var cfg='height=400,width=450,toolbar=no,menubar=no,scrollbars=yes,'
	       +'resizable=no,location=no,directories=no,status=yes';
	thewindow = window.open(url, 'anew', config=cfg);
	}

/**
 * Checks an entry to make sure that it exists.  This will examine 
 * the value in the form input specified as the first argument,
 * and will post a message containing the string specified in the 
 * second argument.  This will also set focus to the input specified.
 *
 * @param input containing text to be verified
 * @param String name of the input for failure notification.
 * @return boolean valid input
 */
function validInput(input, name){ 
	if(input.value.match(/^\s*$/) || input.value.toUpperCase() == 'NONE'){
		alert("Please enter a valid " + name + "."); 
		input.focus();
		return false;
		}
	return true;
	}

/**
 * Checks an email address for validity.  The email address must have 
 * only one @ and at least one period to be valid. 
 *
 * @param input containing email address string
 * @return boolean
 */
function validEmail(email){
	if(!email.value.match(/@/) || !email.value.match(/./) || 
			email.value.match(/\s/)){
		alert("Please enter a valid email address.");
		email.select();
		return false;
		}
	return true;
     } 

/** 
 * Checks a phone number for validity.  The number must have at least 
 * ten numeric digits to be acceptable.
 *
 * @param input containing the phone number string 
 * @return boolean
 */
function validPhone(phone){
	if(phone.value == "" || phone.value.match(/\d/g).length < 9){
		alert("Please enter a valid phone number (XXX) XXX-XXXX."); 
		phone.select(); 
		return false;
		}
	return true; 
	}

/**
 * Checks a zip code to make sure that it contains only numeric digits,
 * and possibly a dash, and is five or ten characters long.
 *
 * @param input containing zip code
 * @return boolean
 */
function validZipCode(zip){
	if(zip.value.match(/^\s*$/)){
		alert("Please enter a valid zip code.");
		zip.select();
		return false;
		}
	return true;
	}

/**
 * Validates a user ID fields for an input form.  This will make 
 * sure that the user ID is between 5 and 10 alphanumeric characters.
 *
 * @param input element containing user ID information 
 */
function validUserID(user_id){
	if(user_id.value == "" || user_id.value.length > 60 || 
			user_id.value.length < 5){
		alert("Please enter a valid user ID of at least five alphanumeric characters.");
		user_id.select(); 
		return false; 
		}
	return true;
	} 

/**
 * Checks a credit card number to make sure that it contains the proper
 * number of digits, based on the card type.  Visa cards must have either
 * 13 or 16 digits, American Express must have 15, and Discover and 
 * MasterCard must have 16.  Other types of accounts will always return
 * true.
 * 
 * @param input field containing card type 
 * @param input field containign card account name 
 * @param input field containing card number
 * @param input field containing card verification number
 * @param input field containing expiration month
 * @param input field containing expiration year
 * @return boolean
 */
function validCreditCard(type, name, number, verification_num, exp_month, 
		exp_year){

	// Make sure there's input in the credit card type 
	// field 
	if(type.value == '0'){
		alert("Please select payment type."); 
		type.focus();
		return false;
		}

	// If the card type is a Kurtz Bros. account, make sure that the 
	// credit card number field is blank.
	if(type.value == ' ' && number.value != ''){
		alert("Please leave the credit card number blank when specifying "
				+ "Kurtz Bros. Account.");
		number.value = ""; 
		return false;
		}

	// For a Kurtz Bros account, just validate the name 
	if(type.value == ' ')
		return validInput(name, "Kurtz Bros. account name"); 

	// Validate the card holder name 
	if(!validInput(name, "account name"))
		return false; 

	// Validate the card number
	if(!validInput(number, "credit card number"))
		return false; 

	// American Express - must have fifteen digits 
	if(type.value == 'A' && number.value.match(/\d/g).length != 15){
		alert("Please enter a valid credit card number."); 
		number.select();
		return false; 
		} 
		
	// Visa - must have sixteen digits and start with 4
	if(type.value == 'V' && number.value.match(/\d/g).length != 13 && 
			number.value.match(/\d/g).length != 16){
		alert("Please enter a valid credit card number."); 
		number.select();
		return false; 
		} 
	if(type.value == 'V' && number.value.charAt(0) != '4'){
		alert("Please enter a valid credit card number."); 
		number.select();
		return false; 
		} 	
		
	// MasterCard - must have sixteen digits and start with 6
	if(type.value == 'M' && number.value.match(/\d/g).length != 16){
		alert("Please enter a valid credit card number."); 
		number.select();
		return false; 
		} 
	if(type.value == 'M' && number.value.charAt(0) != '5'){
		alert("Please enter a valid credit card number."); 
		number.select();
		return false; 
		} 
		
	// Validate the verification number from the back of the card
	if(verification_num.value == '') {
		alert("Please enter the three or four digit verification number "
				+ "from the back of the credit card.");
		verification_num.select();
		return false;
	} else if((type.value == 'V' || type.value == 'M') 
			&& verification_num.value > 999) { 
		alert("Invalid verification number for credit card.");
		verification_num.select();
	} else if(type.value == 'A' && verification_num.value < 1000) { 
		alert("Invalid verification number for credit card.");
		verification_num.select();
	}
		
	// Validate the expiration month and verification number.
	return validInput(exp_month, "expiration date") &&
			validInput(exp_year, "expiration date");
	}

/**
 * Checks a user ID input and two password inputs for validity.
 * This will make sure that the passwords are identical to 
 * each other, that the passwords are not the same as the user 
 * ID, and that the user ID is of 6-10 characters and the 
 * password of 5-8.  This will prompt messages to the user 
 * notifying them of any failures.
 *
 * @param input containing the user ID value 
 * @param input containing the user password
 * @param input containing the copy of the password
 */
function validPasswords(user_id, password, password_copy){ 

	// Validate the user ID field 
	if(!validUserID(user_id))
		return false; 

	// Make sure there's input in the password fields 
	if(!validInput(password, "password") || !validInput(password_copy, 
			"password"))
		return false;

	// Make sure the password values match
	if(password.value != password_copy.value){
		alert("Passwords don't match");
		password.value = '';
		password_copy.value = '';
		password.select();
		return false;
		}
		
	// Validate the password length 
	if(password.value.length > 8 || password.value.length < 5){
		alert("Please choose a password of 5-8 characters.");
		password.value = '';
		password_copy.value = '';
		password.select(); 
		return false;
		}		

	// Make sure there's only alphanumeric characters in the 
	// password 
	if(password.value.match(/\W/)){
		alert("Please use only alphanumeric characters in your password."); 
		password.value = '';
		password_copy.value = '';
		password.select(); 
		return false;
		}	
		
	// Make sure the user ID is not the same as the password
	if(user_id.value.toUpperCase() == password.value.toUpperCase()){
		alert("Password must be different than your user ID.");
		password.value = '';
		password_copy.value = '';
		password.select();
		return false;
		}
		
	return true;
	}
	
/**
 * This function will check the user_id and password fields in the 
 * provided form to see if they are valid.
 *
 * @param form containing a user_id and password input
 * @return boolean
 */
function validLogin(theform){
	return (validUserID(theform.user_id) && validInput(theform.password, 
			"password"));
	}
