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 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){
	//alert("checkEmail");
	if (trim(field.value) != "")
	if(!isEmail(field.value)){
		alert("Please enter a correctly formatted Email Address.");
		field.focus();
		return false;
	}	
	return true;
}

function FieldRequired(field, Name){
	//alert("FieldRequired");
	//return false;
	if(trim(field.value) == ""){
		var strError = 'Please enter a value for ' + Name + '.';
		alert(strError);
		field.focus();
		return false;
	}
	return true;
}

function SelectRequired(field, Name, illegalValue){

	if(trim(field.options[field.selectedIndex].text) == illegalValue){
		
		var strError = 'Please select a value for ' + Name + '.';
		alert(strError);
		field.focus();
		return false;
	}
	return true;
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function DateCheck (myDate,sep) {
// 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())) {
                reason = '';
                return true;
            }
            else {
                reason = 'Please enter a correctly formatted date.';
					alert(reason);
                return false;
            }
        }
        else {
            reason = 'Please enter a correctly formatted date.';
					alert(reason);
            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())) {
	                reason = '';
	                return true;
	            }
	            else {
	                reason = 'Please enter a correctly formatted date.';
					alert(reason);
					myDate.focus;
	                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())) {
		                reason = '';
		                return true;
		            }
		            else {
		                reason = 'Please enter a correctly formatted date.';					alert(reason);
		                return false;
		            }
		        }
		        else {
		            reason = 'Please enter a correctly formatted date.';
					alert(reason);
		            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())) {
		                reason = '';
		                return true;
		            }
		            else {
		                reason = 'Please enter a correctly formatted date.';alert(reason);
		                return false;
		            }
		        }
		        else {
		            reason = 'Please enter a correctly formatted date.';
					alert(reason);
		            return false;
		        }
		    }
		    else {
		    	if (myDate.length == 0) {
		           	return true;
			    }
			    else {
			        reason = 'Please enter a correctly formatted date.';
					alert(reason);
			        return false;
			    }
		    }
	    }
    }
}


function validate(form){
	if(!FieldRequired(form.strFirstName, 'First Name')) return false;
	if(!FieldRequired(form.strLastName, 'Last Name')) return false;
	if(!FieldRequired(form.strAddress1, 'Address')) return false;
	if(!FieldRequired(form.strCity, 'City')) return false;
	if(!SelectRequired(form.strState, 'State', "")) return false;
	if(!FieldRequired(form.strZip, 'Zip')) return false;
	if(!FieldRequired(form.strLoginName, 'Login Name')) return false;
	if(!FieldRequired(form.strPassword, 'Password')) return false;
	if(!FieldRequired(form.strEmail, 'Email Address')) return false;
	if(!checkEmail(form.strEmail)) return false;
	
	
	if (trim(form.dtBirthday.value) != '')
	 { 
	 if(!DateCheck(form.dtBirthday.value, "/")){
	 form.dtBirthday.focus();
	 return false; 
	 }
	}
	if (!form.PrivPolicy.checked)
  	{
	alert("You must read the Privacy Policy and agree to its conditions.");
	form.PrivPolicy.focus();
	return (false);
	}
	return true;	

}
