/********************************************************
*	Function: areYouSure						  		*
*		Determines if the user is sure if he/she 		*
*		wants to do what he/she is about to do.			*
*********************************************************/	

function areYouSure(sQuestion){
	if(!sQuestion || trim(sQuestion).length == 0) sQuestion = "Are you sure you want to do this?"	//default question if none provided
	bAreYouSure = confirm(sQuestion);
	if(!bAreYouSure) return false;
	return true;
}

/********************************************************
*	Function: passwordsMatch					  		*
*		Checks a two password fields to see if the		*
*		user enters the same password twice.			*
*********************************************************/	

function passwordsMatch(pw1, pw2){
	if(pw1.value != pw2.value){
		alert("The passwords you have entered are not the same.  Please re-enter the passwords.");
		pw1.focus();
		error_occurred = true;
	}
}

/********************************************************
*	Function: requiredFound						  		*
*		Checks a text field to see if it is empty		*
*		If so, it alerts and sets an error variable		*
*********************************************************/	

function requiredFound(fld, fldDsp){
	if(fld.value.length == 0){
		alert("Please complete the " + fldDsp + " field.");
		fld.focus();
		error_occurred = true;
	}
}

/********************************************************
*	Function: isGreaterThanZero					  		*
*		Checks a text field to see if its value is		*
*		equals zero. If so, it alerts and sets an		*
*		error variable									*
*********************************************************/	

function isGreaterThanZero(fld, fldDsp, fldToMoveTo){
	if(fld.value == 0){
		alert("Please provide a positive value for the " + fldDsp + " field.");
		if(!fldToMoveTo) fldToMoveTo = fld;
		fldToMoveTo.select();
		error_occurred = true;
	}
}

/********************************************************
*	Function: isEntryAllowed					  		*
*		Checks to see if a text field is allowed to		*
*		have a value entered in it.  This decision is	*
*		based upon whether or not a radio button (or	*
*		checkbox) is checked.  The first argument is	*
*		the radio button field to test, and the second	*
*		argument is the message to display to the user.	*
*		The third argument is used to determine if the	*
*		radio button should be tested to see if it is	*
*		checked or not checked.  To test for checked	*
*		do not provide a third argument.  To test for	*
*		unchecked, provide a value of true for the		*
*		third argument.
*********************************************************/	

function isEntryAllowed(fldDecisionBasedUpon, message, fldNotChecked){
	// In NN 4.x, there is some weird behavior with the focus getting reset that
	// causes this function and, therefore, the alert() to be called twice.  The
	// code in this function only shows the alert() if x == 1.  Each time the
	// function gets called, x is incremented.  When the alert is called, a
	// setTimeout is used to reset x to 0 after .5 seconds.  Therefore, once .5
	// seconds has gone by, it will reset itself and be able to issue another alert(). 

	if(typeof(x)=="undefined") x=0;

	if(!fldNotChecked){// if no third argument is given, assume you're testing if the fld is CHECKED
		if(isThisRadioButtonChecked(fldDecisionBasedUpon)){
			if(message && x==0){
				alert(message); 
				setTimeout("x=0",.5*1000)
				x++;
			}
			return false;
		}
	} else {// if third argument holds value of true, assume you're testing if the fld in NOT CHECKED
		if(!isThisRadioButtonChecked(fldDecisionBasedUpon)){
			if(message && x==0){
				alert(message); 
				setTimeout("x=0",.5*1000)
				x++;
			}
			return false;
		}	
	}
	return true;
}

/********************************************************
*	Function: isThisRadioButtonChecked			  		*
*		Checks a radio button (or checkbox) to see		*
*		if it is checked.  If so, it returns true,		*
*		otherwise it returns false.						*
*********************************************************/	

function isThisRadioButtonChecked(fld,fldDsp){
	if(!fld.checked){
		alert("Please complete the " + fldDsp + " field.");
		fld[0].focus();
		error_occurred = true;
	}
}

/********************************************************
*	Function: setFieldBlank						  		*
*		Sets a text field to blank.  The field to be 	*
*		set is provided as an argument.  Multiple		*
*		fields can be set at once by providing			*
*		additional arguments.							*
*********************************************************/	

function setFieldBlank(fld){
	for(i=0;i<arguments.length;i++){
		setFieldBlank.arguments[i].value = "";
	}
}

/********************************************************
*	Function: blurFocus							  		*
*		Blurs the focus to the field provided as	 	*
*		the argument.									*
*********************************************************/

function blurFocus(fld){
	fld.blur();
}

/********************************************************
*	Function: isCCNumber						  		*
*		Checks to see if a text field is holding a	 	*
*		credit card number.  It makes two types of		*
*		checks, allowing for 16 digits without any		*
*		dashes.  It also allows for the following 		*
*		format (xxxx-xxxx-xxxx-xxxx).  However, it 		*
*		does not currently allow for other formats 		*
*		of CC numbers (such as Amer. Express).	A 		*
*		message is returned and an error variable set	*
*		if the CC Number is not validated.		 		*
*********************************************************/	

function isCCNumber(fld, fldDsp){
	if(fld.value.length == 15 || fld.value.length == 16){
		if(isNaN(fld.value)){
			alert("Please provide a valid value for the " + fldDsp + " field.");
			fld.select();
			error_occurred = true;
		}
	} else if (fld.value.length == 19) {
		for(i=0;i<fld.value.length;i++){
			if(i==4 || i==9 || i== 14){
				if(fld.value.charAt(i) != "-"){
					alert("Please provide a valid value for the " + fldDsp + " field.");
					fld.select();
					error_occurred = true;	
				}
			} else {
				if(isNaN(fld.value.charAt(i))){
					alert("Please provide a valid value for the " + fldDsp + " field.");
					fld.select();
					error_occurred = true;			
				}
			}
		}
	} else {
		alert("Please provide a valid value for the " + fldDsp + " field.");
		fld.select();
		error_occurred = true;	
	}
}

/********************************************************
*	Function: isNumeric							  		*
*		Checks to see if a text field is holding a	 	*
*		number.  It only uses the javascript isNaN()	*
*		function to test.  A messsage is provided and	*
*		an error variable set if it's not a number.		*
*********************************************************/	

function isNumeric(fld, fldDsp){
	if(isNaN(fld.value)){
		alert("Please provide a numeric value for the " + fldDsp + " field.");
		fld.select();
		error_occurred = true;
	}
}	

/********************************************************
*	Function: radioSelected						  		*
*		Checks to see if one of a set of radio buttons 	*
*		has been selected.  This is a validation to 	*
*		ensure that the person has not left the entire	*
*		set blank.  The first argument is the radio		*
*		button set to be tested.  The second argument	*
*		is optional - is you want a message to be 		*
*		displayed provide it as the second argument.	*
*		If there is no second argument, a value of 		*
*		true or false will simply be returned.			*
*********************************************************/	

function radioSelected(fld, fldDsp){
	radio_selected = false;
	for(i=0;i<fld.length;i++){
		if(fld[i].checked){
			radio_selected = true;			
		}
	}
	if(fldDsp){
		if(!radio_selected){
			alert("Please select a value for the " + fldDsp + " field.");
			fld[0].focus();
			error_occurred = true;
		}
	} else {
		return radio_selected;
	}
}

/********************************************************
*	Function: menuSelected						  		*
*		Checks to see if one of a set of drop down	 	*
*		menu has been selected.  If the item selected 	*
*		is the first item, this assumes that nothing	*
*		has been selected.  Therefore, make sure that	*
*		the first item is blank, or some type of		*
*		direction to the user.  This does not work 		*
*		for a select list - only a drop down.			*
*		If no item is selected a message is displayed 	*
*		and an error variable is set.					*
*********************************************************/	

function menuSelected(fld, fldDsp){
	if(fld.selectedIndex == 0){
		alert("Please select a value for the " + fldDsp + " field.");
		fld.focus();
		error_occurred = true;
	}
}

/********************************************************
*	Function: listSelected						  		*
*		Checks to see if one of a set of select list 	*
*		menu has been selected.  If no item is selected	*
*		a message is displayed and an error variable	*
*		is set.											*
*********************************************************/	

function listSelected(fld, fldDsp){
	if(fld.selectedIndex == -1){
		alert("Please select a value for the " + fldDsp + " field.");
		fld.focus();
		error_occurred = true;
	}
}

/************************************************************
*	Function: trim								  			*
*		Trims a string of any leading or trailing spaces.	*
*************************************************************/	

function trim(strText) { 
	//alert("trim");
     // this will get rid of leading spaces 
     while (strText.substring(0,1) == ' ') 
         strText = strText.substring(1, strText.length);

     // this will get rid of trailing spaces 
     while (strText.substring(strText.length-1,strText.length) == ' ')
         strText = strText.substring(0, strText.length-1);

    return strText;
} 

/************************************************************
*	Function: isEmail and checkEmail			  			*
*		These two functions validate email addresses.		*
*************************************************************/	

// function isEmail2 splits the Email based on @ symbol
// isEmail2 accepts 2 parameters - email and flag 
// flag should be set to 0.
function isEmail(email){
	//alert("isEmail");
	var temp = email.indexOf('@');
	var tempstring = email.substring(temp+1);
	var period = tempstring.indexOf('.');
	
	var iChars = "*|,\":<>[]{}`\'()&$#% !^+=\\/;?";
	for(var i=0; i < temp; i++){
		if(iChars.indexOf(email.charAt(i))!= -1)
			return false;
	}
	
	if(period == -1)
		return false;
	if(temp == -1 )
		return false;
		
	iChars = "*|,\":<>[]{}`\'()&$#% @!^+=\\/;?";
	for(var i=temp+1; i < email.length; i++){
		if(iChars.indexOf(email.charAt(i))!= -1)
			return false;
	}

	if(iChars.indexOf(tempstring.charAt(period+1))!= -1)
		return false;

	return true
}

function checkEmail(field, sDisplayText){
	if(sDisplayText == null){
		sDisplayText = "Email Address";
	}
	if(trim(field.value) != "")
	if(!isEmail(field.value)){
		alert("Please enter a correctly formatted " + sDisplayText + ".");
		field.select();
		error_occurred = true;
		return false;
	}
	return true;
}

/************************************************************
*	Function: checkZip							  			*
*		Tests a zip code field to see if it's holding a		*
*		valid format for a zip code.  (xxxxx or xxxxx-xxxx)	*
*		If not, message displayed and error variable set.	*
*************************************************************/	

function checkZip(fld){
	rExp = /^\d{5}(-\d{4})?$/;
	results = fld.value.match(rExp);
	if(results == null){
		alert("Please provide a valid Zip/Postal Code.");
		fld.select();
		error_occurred = true;
	}
}

/************************************************************
*	Function: checkPhone						  			*
*		Tests a phone number field to see if it's holding a	*
*		valid format for a phone number.					*
*		(xxx) xxx-xxxx										*
*		xxx-xxx-xxxx										*
*		xxx.xxx.xxxx										*
*		xxxxxxxxxx											*
*		If not, message displayed and error variable set.	*
*															*
*		Note: This function has been modified to only 		*
*			accept the following format: xxx-xxx-xxxx		*
*************************************************************/	

function checkPhone(fld, fldDsp){
	rExp = /^\(?\d{3}\)?[\-\.\s]?\d{3}[\-\.\s]?\d{4}$/;
	//rExp = /^\(\d{3}\)\s\d{3}\-\d{4}$/
	results = fld.value.match(rExp);
	if(fld.value.length > 0 && results == null){
		alert("Please provide a valid phone number for the " + fldDsp + " field.  (xxx) xxx-xxxx");
		fld.select();
		error_occurred = true;
	}
}

/************************************************************
*	Function: checkSSN							  			*
*		Tests a Social Security Number field to see if		*
*		it's holding a valid format for a phone number.		*
*		xxxxxxxxx	(no dashes)								*
*		If not, message displayed and error variable set.	*
*************************************************************/	

function checkSSN(fld){
	if(fld.value.length != 9 || isNaN(fld.value)){
		validSSN = false;
	} else {
		validSSN = true;
	}
	
	if(!validSSN){
		alert("Please provide a valid Social Security Number.\nBe sure not to include any dashes.");
		fld.select();
		error_occurred = true;
	}
}

/************************************************************
*	Function: checkCouponNumber					  			*
*		Tests a Coupon Number field to see if it's holding	*
*		an 8-digit number									*
*		If not, message displayed and error variable set.	*
*************************************************************/	

function checkCodeNumber(fld, type){
	switch (type){
		case "coupon":
			sCodeDisplay = "Coupon Number";
			iCodeNumberLength = 8;
			break;
		case "giftcert":
			sCodeDisplay = "Gift Certificate Number";
			iCodeNumberLength = 6;
			break;
		default:
			sCodeDisplay = "Code Number";
			iCodeNumberLength = 8;
	}
	if(fld.value.length != iCodeNumberLength || isNaN(fld.value)){
		validCodeNumber = false;
	} else {
		validCodeNumber = true;
	}
	
	if(!validCodeNumber){
		alert("Please provide a valid " + sCodeDisplay + ".\nBe sure it is a(n) " + iCodeNumberLength + "-digit number.");
		fld.select();
		error_occurred = true;
	}
}

/************************************************************
*	Function: checkForDecimal					  			*
*		Tests a text field to see if it's holding a decimal	*
*		value.												*
*		If not, message displayed and error variable set.	*
*************************************************************/	

function checkForDecimal(fld, fldDsp){
	if(isNaN(trim(fld.value))){
		validDecimalNumber = false;
	} else {
		validDecimalNumber = true;
	}
	
	if(!validDecimalNumber){
		alert("Please provide a valid value for the " + fldDsp + " field.\nBe sure it is a number.");
		fld.select();
		error_occurred = true;
	}
}

/************************************************************
*	Function: checkStateZipBasedOnCountry		  			*
*		Determines whether the state/zip fields need to be	*
*		validated based on the selected country.			*
*************************************************************/	
	
function checkStateZipBasedOnCountry(theStateField, theZipField, theCountryField, sFieldType){
	
	determineSelectedCountry(theCountryField);
	
	if(bUSAIsSelected || bCanadaIsSelected){
		if(theStateField.options[theStateField.selectedIndex].value == ""){
			alert("Please select a state/province for the " + sFieldType + " State/Province field.");
			theStateField.focus();
			error_occurred = true;
		}
		if(!error_occurred){
			sSelectedState = theStateField.options[theStateField.selectedIndex].text;
			sUSStatesList = "Alabama|American Samoa|Alaska|Arizona|Arkansas|Armed Forces Africa|Armed Forces Americas|Armed Forces Canada|Armed Forces Europe|Armed Forces Middle East|Armed Forces Pacific|California|Colorado|Connecticut|Delaware|District of Columbia|Federated States of Micronesia|Florida|Georgia|Guam|Hawaii|Idaho|Illinois|Indiana|Iowa|Kansas|Kentucky|Louisiana|Maine|Marshall Islands|Maryland|Massachusetts|Michigan|Minnesota|Mississippi|Missouri|Montana|Nebraska|Nevada|New Hampshire|New Jersey|New Mexico|New York|North Carolina|North Dakota|Northern Mariana Islands|Ohio|Oklahoma|Oregon|Palau|Pennsylvania|Puerto Rico|Rhode Island|South Carolina|South Dakota|Tennessee|Texas|Utah|Vermont|Virgin Islands|Virginia|Washington|West Virginia|Wisconsin|Wyoming";
			USStatesArray = sUSStatesList.split("|");
			bUSStateFound = false;
			for(i=0;i<USStatesArray.length;i++){
				if(USStatesArray[i] == sSelectedState){
					bUSStateFound = true;
					break;
				}
			}
			if(bUSStateFound && bCanadaIsSelected){
				alert(sSelectedState + " is not a Canadian province.  Please select a Canadian province for the " + sFieldType + " State/Province field.");
				theStateField.focus();
				error_occurred = true;
			} else if(!bUSStateFound && bUSAIsSelected){
				alert(sSelectedState + " is not a US state.  Please select a US state for the " + sFieldType + " State/Province field.");
				theStateField.focus();
				error_occurred = true;
			}
			if(!error_occurred) requiredFound(theZipField, sFieldType + " Zip/Postal Code");
			if(bUSAIsSelected){
				if(!error_occurred) checkZip(theZipField);
			}
		}
	} else {
		if(theStateField.selectIndex > 0){
			alert("You have not selected the United States or Canada, so please do not select a state/province for the " + sFieldType + " State/Province field.");
			theStateField.focus();
			error_occurred = true;
		}
	}
}


function determineSelectedCountry(theCountryField){
	if(theCountryField.options[theCountryField.selectedIndex].value == "USA"){
		bUSAIsSelected = true;
	} else if(theCountryField.options[theCountryField.selectedIndex].value == "Canada"){
		bCanadaIsSelected = true;
	}
}

/************************************************************
*	Function: y2k and DateCheck					  			*
*		These two functions check to see if a text field	*
*		is holding a date in a correct format.				*
*		If not, message displayed and error variable set.	*
*************************************************************/	

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function DateCheck (myDate,sep, fldDsp) {
// checks if date passed is in valid mm/dd/yyyy, mm/d/yyyy, m/dd/yyyy, or m/d/yyyy format

	// checks if date passed is in valid mm/dd/yyyy format
    if (myDate.length == 10) {
        if (myDate.substring(2,3) == sep && myDate.substring(5,6) == sep) {
            var month  = myDate.substring(0,2);
            var date = myDate.substring(3,5);
            var year  = myDate.substring(6,10);

            var test = new Date(year,month-1,date);

            if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) {
                return true;
            }
            else {
            	alert("Please enter the correct format for the " + fldDsp + " field.");
				error_occurred = true;
                return false;
            }
        }
        else {
        	alert("Please enter the correct format for the " + fldDsp + " field.");
			error_occurred = true;		
            return false;
        }
    }
    else {
		// checks if date passed is in valid mm/d/yyyy, or m/dd/yyyy format
		// checks if date passed is in valid mm/d/yyyy format
	    if (myDate.length == 9) {
	        if (myDate.substring(2,3) == sep && myDate.substring(4,5) == sep) {
	            var month  = myDate.substring(0,2);
	            var date  = myDate.substring(3,4);
	            var year  = myDate.substring(5,9);
	
	            var test = new Date(year,month-1,date);
	
	            if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) {
	                return true;
	            }
	            else {
					alert("Please enter the correct format for the " + fldDsp + " field.");
					myDate.focus;
					error_occurred = true;
	                return false;
	            }
	        }
	        else {
				// checks if date passed is in valid m/dd/yyyy format
		        if (myDate.substring(1,2) == sep && myDate.substring(4,5) == sep) {
		            var month  = myDate.substring(0,1);
		            var date = myDate.substring(2,4);
		            var year  = myDate.substring(5,9);
		
		            var test = new Date(year,month-1,date);
		
		            if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) {
		                return true;
		            }
		            else {
		            	alert("Please enter the correct format for the " + fldDsp + " field.");
						error_occurred = true;
		                return false;
		            }
		        }
		        else {
		        	alert("Please enter the correct format for the " + fldDsp + " field.");
					error_occurred = true;
					return false;
		        }
	        }
	    }
	    else {
		// checks if date passed is in valid m/d/yyyy format
	    	if (myDate.length == 8) {
		        if (myDate.substring(1,2) == sep && myDate.substring(3,4) == sep) {
		            var month  = myDate.substring(0,1);
		            var date = myDate.substring(2,3);
		            var year  = myDate.substring(4,8);
		
		            var test = new Date(year,month-1,date);
		
		            if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) {
		                return true;
		            }
		            else {
			            alert("Please enter the correct format for the " + fldDsp + " field.");
						error_occurred = true;
		                return false;
		            }
		        }
		        else {
		        	alert("Please enter the correct format for the " + fldDsp + " field.");
					error_occurred = true;
		            return false;
		        }
		    }
		    else {
		    	if (myDate.length == 0) {
		           	return true;
			    }
			    else {
			    	alert("Please enter the correct format for the " + fldDsp + " field.");
					error_occurred = true;
			        return false;
			    }
		    }
	    }
    }
}

