<!--
// ************************************************************
// ************************************************************
// * FILE NAME:	misc.js										  *
// *														  *
// * FUNTIONS:	function isTelephone(phoneObj, label)		  *
// * 			function chkCountry(stateVal)				  *
// *			function isZipCode(zipObj, label, stateVal)	  *
// *			function ChkDateVal(dateval)				  *
// *			function isValidCCNum(f, ccNumObj, cardType)  *
// *			function isEmail(emailObj)					  *
// *			function isBusEmail(emailObj)					  *
// *			function chkAmount(strObj)					  *
// *			function textFilter(fieldObj, label, code)	  *
// *			function isPOBox(addrObj, fedExInd)			  *
// *			function getLastDay(monthNum)				  *
// *			function fieldAlertMsg(label, type)           *
// *														  *
// *														  *
// ************************************************************
// ************************************************************

//Validate for a telephone number
function isTelephone(phoneObj, label){

	var phoneNum = eval("'" + phoneObj.value + "'");

	var re_spc	  = /\s/g;	
	var re_hyphen =	/-/g;
	var re_openp  = /\(/g;
	var re_closep = /\)/g;

	//remove spaces, hyphens, open and closed parens
	phoneNum = phoneNum.replace(re_spc, '');
	phoneNum = phoneNum.replace(re_hyphen, '');
	phoneNum = phoneNum.replace(re_openp, '');
	phoneNum = phoneNum.replace(re_closep, '');
			 
	if (isNaN(phoneNum))
	{
		alert("Please enter a valid " + label + " number (only numbers).", "Error");
		return false;
	}
	
	var phoneNumLength = phoneNum.length
		
	if (phoneNumLength != 10){
		alert("Please enter the " + label +  " number in a 'XXX-XXX-XXXX' format.", "Error");
		return false;
	}
	
	phoneObj.value = phoneNum;
	
	return true;
}

//***********************************
//Determines which country the address is related to (for zip code purposes)
function chkCountry(stateVal)
{
	switch(stateVal)
	{
		case 'AB':
			return "CANADA"
			break;
		case 'BC':
			return "CANADA"
			break;
		case 'MB':
			return "CANADA"
			break;
		case 'NB':
			return "CANADA"
			break;
		case 'NF':
			return "CANADA"
			break;
		case 'NT':
			return "CANADA"
			break;
		case 'NS':
			return "CANADA"
			break;
		case 'ON':
			return "CANADA"
			break;
		case 'PE':
			return "CANADA"
			break;
		case 'QC':
			return "CANADA"
			break;
		case 'PQ':
			return "CANADA"
			break;
		case 'SK':
			return "CANADA"
			break;
		case 'YT':
			return "CANADA"
			break;
		case 'PR':
			return "PUERTO RICO";
			break;
		default:
			return "US";
			break;
	}
	
	//force a return for browsers that don't handle switch very well
	return 'US';
}

//***********************************
//Validate Zip Code
function isZipCode(zipObj, label, stateVal)
{
	var zipCode = eval("'" + zipObj.value + "'");
	
	var re_spc = /\s/g;
	var re_hyphen = /-/g;

	//remove hyphens and spaces
	zipCode = zipCode.replace(re_spc, '');
	zipCode = zipCode.replace(re_hyphen, '');
		
	var country = chkCountry(stateVal);
	
	if (country == "CANADA")
	{
		var postalCodeLength = zipCode.length;
				
		if (postalCodeLength != 6)
		{
			alert("The postal code " + label + " must be 6 characters.", "Error");
			return false;
		}
			
	} else 
	{
	
		var zipCodeNumeric = ""
		
		//make sure it's a number
		if (isNaN(zipCode))	{
			alert("Please enter a valid zip code(only numbers) " + label + ".", "Error");
			return false;
		}
	
		var zipCodeLength = zipCode.length
		
		//make sure the length is correct
		if (zipCodeLength != 5){
			if (zipCodeLength != 9){
				alert("The zip code " + label + " must be 5 or 9 characters.", "Error");
				return false;
			}
		}
	}
	
	//set field to corrected value
	zipObj.value = zipCode
	return true;
}

//***********************************
//Make sure the expiration date for a credit card entered has not already expired
function ChkDateVal(dateval){
	
	var newDate = Date.parse(dateval)
	var today = Date.parse(Date())
	
		
	if (newDate < today){
		alert ("The entered credit card expiration date has already expired.", "Error");
		return false;
	} 
	else {
		return true;
	}
}


//***********************************
//Validate a credit card number based on credit card rules
function isValidCCNum(f, ccNumObj, cardType){

	var ccNum = eval("'" + ccNumObj.value + "'");
	
	var re_spc = /\s/g;
	var re_hyphen = /-/g;

	//remove spaces, hyphens, open and closed parens
	ccNum = ccNum.replace(re_spc, '');
	ccNum = ccNum.replace(re_hyphen, '');
		
	if (isNaN(ccNum) && ccNum != "")
	{
		alert("Please enter a valid credit card number (only numbers).", "Error");
		return false;
	}

	switch (cardType){
		case 'VISA':
		if (!(ccNum.substr(0,1) == "4")){
				alert("VISA Cards must start with a 4.", "Error")
				return false;
			}
						
			if (!(ccNum.length == 13) && !(ccNum.length == 16)){
				alert ("Visa cards must be 13 or 16 characters.", "Error")
				return false;
			}
			break;
			
		case 'MAST':
			if (!(ccNum.substr(0,1) == "5")){
				alert("MasterCard Cards must start with a 5.", "Error")
				return false;
			}
										
			if (!(ccNum.length == 13) && !(ccNum.length == 16)){
				alert ("MasterCard cards must be 16 characters.", "Error")
				return false;
			}
			break;
			
		case 'AMEX':
			if (!(ccNum.substr(0,1) == "3")){
				alert("Americatn Express Cards must start with a 3.", "Error")
				return false;
			}
			if (!(ccNum.length == 15)){
				alert ("American Express cards must be 15 characters.", "Error")
				return false;
			}
			break;
			
		case 'DISC':
			if (!(ccNum.substr(0,4) == "6011")){
				alert("Discover Cards must start with a 6.", "Error")
				return false;
			}
			if (!(ccNum.length == 16)){
				alert ("Discover cards must be 16 characters.", "Error")
				return false;
			}
			break;
			
		case 'DINE':
			if (!(ccNum.substr(0,1) == "3")){
				alert("Diner's Club Cards must start with a 3.", "Error")
				return false;
			}
			if (!(ccNum.length == 14)){
				alert ("Diner's Club cards must be 14 characters.", "Error")
				return false;
			}
			break;
				
		case 'MasterCard':
			if (!(ccNum.substr(0,1) == "5")){
				alert("MasterCard Cards must start with a 5.", "Error")
				return false;
			}
			if (!(ccNum.length == 13) && !(ccNum.length == 16)){
				alert ("MasterCard cards must be 16 characters.", "Error")
				return false;
			}
			break;
			
		case 'American Express':
			if (!(ccNum.substr(0,1) == "3")){
				alert("American Express Cards must start with a 3.", "Error")
				return false;
			}
			if (!(ccNum.length == 15)){
				alert ("American Express cards must be 15 characters.", "Error")
				return false;
			}
			break;
			
		case 'Discover':
			if (!(ccNum.substr(0,4) == "6011")){
				alert("Discover Cards must start with a 6.", "Error")
				return false;
			}
			if (!(ccNum.length == 16)){
				alert ("Discover cards must be 16 characters.", "Error")
				return false;
			}
			break;
			
		case "Diner's Clubs":
			if (!(ccNum.substr(0,1) == "3")){
				alert("Diner's Club Cards must start with a 3.", "Error")
				return false;
			}
			if (!(ccNum.length == 14)){
				alert ("Diner's Club cards must be 14 characters.", "Error")
				return false;
			}
			break;
			
		default:
			alert ("You entered an invalid credit card number.", "Error")
			return false;
	}
	
	ccNumObj.value = ccNum;
	return true;
}


//***********************************
//Verifies an email address
function isEmail(emailObj){
	var email = emailObj.value;
	
	var re_Comma	= /\,/g;
	var re_At		= /@/g;
	var re_Period	= /\./g;
	var re_Space	= /\s/g;
	
	if(email.search(re_Comma) > -1){
		alert("You must enter a valid email address.", "Error");
		return false;
	}
	
	if(email.search(re_At) == -1){
		alert("You must enter a valid email address.", "Error");
		return false;
	}
	
	
	if(email.search(re_Period) == -1){
		alert("You must enter a valid email address.", "Error");
		return false;
	}
	
	if(email.search(re_Space) > -1){
		alert("You must enter a valid email address.", "Error");
		return false;
	}
	
	
	return true;	
}
//***********************************
//Verifies an email address is a business address
function isBusEmail(emailObj){
	var email = emailObj.value;
			email = email.toUpperCase();
			if(email.search('YAHOO.COM') > -1){
				alert("You must enter a valid business email address.", "Error");
				return false;
			}
			if(email.search('AOL.COM') > -1){
				alert("You must enter a valid business email address.", "Error");
				return false;
			}
			if(email.search('EARTHLINK.NET') > -1){
				alert("You must enter a valid business email address.", "Error");
				return false;
			}
			if(email.search('HOTMAIL.COM') > -1){
				alert("You must enter a valid business email address.", "Error");
				return false;
			}
			if(email.search('MSN.COM') > -1){
				alert("You must enter a valid business email address.", "Error");
				return false;
			}
			if(email.search('NETSCAPE.COM') > -1){
				alert("You must enter a valid business email address.", "Error");
				return false;
			}
			if(email.search('NETSCAPE.NET') > -1){
				alert("You must enter a valid business email address.", "Error");
				return false;
			}
			if(email.search('NETZERO.NET') > -1){
				alert("You must enter a valid business email address.", "Error");
				return false;
			}
			
				return true;	
}
//***********************************
//Verifies values of a text box based
function textFilter(fieldObj, label, code){

	/*
		code (ie. 1111) that gets passed in is split into x parts
		to determine which filters must be run.
		1st char	- checks for all spaces
		2nd char	- checks for null
		3rd char	- checks for non word characters
		4th char	- replaces double quotes with single quotes
		5th char	- checks if numeric
	*/
	
	//set up reg expressions
	//spaces
	var re_spc = /\s/g;
	
	//ampersand
	var re_ampersand = /&/g;
	
	
	//non-word characters
	var re_nonWord = /\W/g;
	
	//double quotes
	var re_dblqt = /"/g;
	
	var codeArray = code.split('');	
		
	//check for all spaces, sets field to null if all spaces are entered
	if (codeArray[0] == '1'){
	
		strVal = fieldObj.value;
		
		strVal = strVal.replace(re_spc, '');
	
		if (strVal.length == 0){
			fieldObj.value = '';
		}
	}
		
	//check for null
	if (codeArray[1] == '1'){
		if (fieldObj.value == ''){
			alert('You must enter a value for ' + label + '.');
			return false;
		}
	}
	
	//check for all letters/numbers (No special characters)
	if (codeArray[2] == '1'){
		if (fieldObj.value.search(re_nonWord) >= 0){
			alert('You must enter only letters or numbers for ' + label + '.');
			return false;
		}
	}
	
	//replace any existing double quotes with 
	//single quotes to avoid database issues
	if (codeArray[3] == '1'){
		fieldObj.value = fieldObj.value.replace(re_dblqt, "'");
	}
	
	//checks to see if numeric
	if (codeArray[4] == '1'){
		if(isNaN(fieldObj.value)){
			alert('You must enter a number for ' + label + '.');
			return false;
		}
	}
	
	return true;
}

//Check to see if this address field resembles a po box
function isPOBox(addrObj, fedExInd){
	
	//set up reg expressions
	//spaces
	var re_spc = /\s/g;
	
	//period
	var re_period = /\./g;

	var addrVal = addrObj.value;
	
	//allow for spaces
	addrVal = addrVal.replace(re_spc, '');
	
	//allow for periods
	addrVal = addrVal.replace(re_period, '');
	
	addrVal = addrVal.toLowerCase();

	if (addrVal.indexOf("pobox") > -1){
	
		if(fedExInd == '1'){
			alert('You cannot upgrade your shipping when shipping to a PO Box.');
		} else {
			alert('You cannot ship to a PO Box.');
		}
		
		return true;
	}
	
	return false;
}

//Validates Maximum order value -- NOT CURRENTLY USED
//function bases calculation off of Total Face Value (exluding service/shipping fees)
function chkOrderVal(orderVal){

	var maxOrderVal;
	maxOrderVal = 500.00;
	
	if (orderVal > maxOrderVal){
		alert('You have exceeded the maximum amount allowed for an order.');
		return false;
	}
	
	return true;
}

//Used to determine the last day of the month for credit card dates
function getLastDay(monthNum){
	switch(monthNum){
		case 1:
			return "31";
			break;
		case 2:
			return "28";
			break;
		case 3:
			return "31";
			break;
		case 4:
			return "30";
			break;
		case 5:
			return "31";
			break;
		case 6:
			return "30";
			break;
		case 7:
			return "31";
			break;
		case 8:
			return "31";
			break;
		case 9:
			return "30";
			break;
		case 10:
			return "31";
			break;
		case 11:
			return "30";
			break;
		case 12:
			return "31";
			break;
		default:
			return "28";
			break;
	}
	
	//default for browsers that don't support switch
	return "28";
}

function fieldAlertMsg(label, type){
	var lblType;
	
	if (type == 'test'){
		lblType = 'enter'
	} else {
		lblType = 'select'
	}
	
	alert('You must ' + lblType + ' a value for ' + label + '.');
}

//-->


