// FormCheck.js 

function SetUsername() { // Used for join  
 if (document.entryform.Username.value == "") document.entryform.Username.value = document.entryform.eMail.value;
}

function CheckJoin() { // Used for join 
 if (isEmpty(document.entryform.eMail,"Email Address")){return false;} 
 if ( !ValidEmail('eMail') ) return false;
 if (isEmptyMess(document.entryform.Username,"a Username")){return false;}
 if ( !PassCheck(1) )     return false;
 if ( !UsernameCheck() )  return false;

 if (!document.entryform.Agree.checked) {
  alert("Please read the User/Fan Agreement and check the box that says you agree.");
  return false;
 }

 if (document.entryform.Disagree.checked) {
  alert("Thank you for your candor in telling us that you do not agree with the User/Fan Agreement.  If you change your mind, we welcome you to come back and join us in the future.");
  return false;
 }

 document.entryform.Join.value="Saving. Please wait...";
 return true;
}

function CheckUserPass() { // Used for ChangePassword 
 if (isEmptyMess(document.entryform.Username,"a Username")){return false;}
 if ( !PassCheck(0) )    return false; 
 if ( !UsernameCheck() ) return false;
 document.entryform.Save.value="Saving. Please wait...";
 return true;
}

// Validates Simple Message Sending Box (just name, email and comments - OTD)
function CheckMessage(CommentPrompt){  
 if (!CommentPrompt) CommentPrompt = "Enter Comments Here";
 if (isEmpty(document.entryform.FullName,"Name")){return false;}
 if (isEmpty(document.entryform.eMail,"Email Address")){return false;} 
 if ( !ValidEmail('eMail') ) return false;
 if (document.entryform.Comments.value == CommentPrompt) document.entryform.Comments.value ="";
 return true
} 
 
function CheckNameContact(){ // Checks to see that user enters at least a name and some way to be contacted.
 if (document.entryform.FirstName.value=="" 
     && document.entryform.LastName.value=="")
    {
  alert("Please enter your name.");
  document.entryform.FirstName.focus();
  return false;
    }
 
 if (document.entryform.eMail.value=="" 
     && document.entryform.Phone.value==""
     && (document.entryform.Address.value==""
         || document.entryform.City.value==""
         || document.entryform.State.value==""
         || document.entryform.Zip.value==""
        )
     )
    {
  alert("You have given us no way to contact you.  Please enter your Email, Address and/or Telephone Number.");
  document.entryform.eMail.focus();
  return false;
    }
 return true;
}
 
function CheckOther(theField,oField,NextField){
 if (theField.value.indexOf("Other") >-1){
  oField.focus();
 }else if (NextField){
  NextField.focus();
 }
 return true;
}

function charcount(oCount,oDisplay, theMax){
 if (!theMax) theMax=6000;
 oDisplay.value=oCount.length;
 if (oDisplay.value >= theMax){
  alert("You have reached the maximum number of characters.");
  return false;
 }else{
  return true;
 }
}

function StuffUsername(){ // Called onChange when you want to stuff the Username with the email address
 return ValidEmail('eMail',false,'Username');
}

function UsernameCheck(){
 if (isBadChar(document.entryform.Username,"a Username")){return false;}
 return true;
}

function PassCheck(NewMember) {
 if (NewMember==1){
  var ConfirmMess = "your Password";
  if (isEmptyMess(document.entryform.NewPass,"a Password")) return false;

 }else{
  var ConfirmMess = "your new Password"; 
 }
 
 if (isBadChar(document.entryform.NewPass,"a password")) return false;
 
 // If user changed either password or confirmation, you'd better check it.
 if (document.entryform.NewPass.value !="" || document.entryform.NewPass1.value !=""){
  if (isEmptyMess(document.entryform.NewPass1,ConfirmMess,"Please confirm")) return false;
  if (document.entryform.NewPass.value !=document.entryform.NewPass1.value){
   alert("Your password confirmation does not match your password.\nPlease enter your password again and then confirm it.");
   document.entryform.NewPass.select();
   return false;
  }
  // Only change Pass if user entered and confirmed new value
  document.entryform.Pass.value=document.entryform.NewPass.value;
  document.entryform.Password.value=document.entryform.NewPass.value;  
 }

 return true;
  
}// EOF PassCheck()


function TestForm() {
 isTesting = true;
 document.entryform.submit();
}

function isEmptyPrompt(theField,theMessage,thePrompt){  // Customized message with prompt, returns true if field is empty
 PleaseEnter = "Please enter";
 if (theField.value =="" || theField.value==thePrompt) {
  var AlertMess = PleaseEnter +" "+ theMessage;
  alert(AlertMess);  
  return true; // Don't focus so Prompt remains visible
 }
 return false;
}

function isEmptyMess(theField,theMessage,PleaseEnter){  // Customized message, returns true if field is empty
 if (!PleaseEnter) PleaseEnter = "Please enter";
 if (theField.value =="") {
  var AlertMess = PleaseEnter +" "+ theMessage;
  alert(AlertMess);
  theField.focus();
  return true;
 }
 return false;
}

function isEmpty(theField,FieldName){  // Generic Message based on passed FieldName, returns true if field is empty
 if (theField.value =="") {
  var AlertMess = "Please enter your " +FieldName;
  alert(AlertMess);
  theField.focus();
  return true;
 }
 return false;
}

function ConfirmEmpty(theObject,theMess) {
 if (theObject.value=="") {
  var ConfirmMess = "Are you sure you want to save this record with no " 
                  +theMess
                  +".\nPress OK to Save.\nPress Cancel to enter a " +theMess;
                  
  if ( confirm(ConfirmMess) ) return true;
  
  theObject.focus();
  return false;
  
 }
 
 return true;
}

function isSelectNONE(theField,AlertName) {  // returns true if "NONE" is the selected value
 if (theField.value=="NONE") {
  if (!AlertName) AlertName = theField.name;
  var theAlert = "Please Select One " + AlertName;
  alert(theAlert);
  theField.focus();
  return true; 
 }
 return false;
}
 
function BadChar(theString,theList,FirstLast){ // Allow "@_-."
                                     
 if (!theList){theList="`~!#$%^&*()+={}|[']\:;<>?,/";}

 var dq = '"'; 
 var sp = " ";
	var dd = "..";
 
 if (theString.indexOf(dq) >-1){return "Quotes are";}
 if (theString.indexOf(sp) >-1){return "Spaces are";}
 if (theString.indexOf(dd) >-1){return "Two dots next to each other are";}
	
 for (var i = 0; i < theList.length; i++) { 
  theChar = theList.charAt(i);
  if (theString.indexOf(theChar) >-1) {return theChar +" is";}
 }
 return "";
}

function isBadChar(theField,theName){
 var theBadChar = BadChar(theField.value);
 if (theBadChar !=""){
  alert(theBadChar +" is not allowed in " + theName);
  theField.focus();
  return true;
 }
}
 
function ValidEmail(theID,isPager,Field2Stuff,Field2Check) {
 var theField  = document.getElementById(theID);
 if (!theField) {return true;}       // Old Browsers can't do this.
	var theEmail  = theField.value;

 if (theEmail == "") {return true;}

 theBadChar = BadChar(theEmail);
 if (theBadChar !=""){
  alert(theBadChar +" not allowed in an email address.");
  theField.focus();
  return false;
 }
 
	var theAt     = theEmail.indexOf("@") +1; 
 if (theEmail.indexOf("@",theAt) >=0) {
  alert("Only One At Sign [ @ ] is allowed in an email address.");
  theField.focus();
  return false;
 }
 
	var DotAfter  = theEmail.indexOf(".",theAt) +1;
	var theLen   = theEmail.length;
	
	BadFirstLast = ".@";
 if (BadFirstLast.indexOf(theEmail.charAt(0))>-1){
  alert("You cannot use  " +theEmail.charAt(0)+ "  as the first character in an email address.");
  theField.focus();
  return false;		
 }
	
	if (BadFirstLast.indexOf(theEmail.charAt(theLen -1))>-1){
  alert("You cannot use  " +theEmail.charAt(theLen -1)+ "  as the last character in an email address.");
  theField.focus();
  return false;		
 }

	if (theAt >1 && theAt+1 < DotAfter && DotAfter <= theLen -2) {
  if (Field2Stuff){                     // Only stuff if target field is blank
   sField = document.getElementById(Field2Stuff);
   if (sField.value==""){sField.value = theEmail;}
  }
  
  if (Field2Check){
   document.getElementById(Field2Check).checked = true;
  }
  
  return true;
 }

 if (isPager){
  var theMess = "All pager email addresses must be formatted in the same way as a regular email address.";
 }else{
  theMess = "The Email address does not appear to be properly formatted.";
 }
	alert(theMess + 
      "  All email addresses use the same format, for example, johnq@public.com.  Please check it carefully and try again.");
	theField.focus();
	return false;
}

function GetRadioValue(theRadio) {
	for (var i = 0; i <= theRadio.length -1; i++) {
		if (theRadio[i].checked) {
			return theRadio[i].value;
		}
	}
 return "NONE"; // Nothing was checked, so return NONE
}


function BadDate(theObject,theName,NoBlank) {             // Used for one-line date validation.  Returns TRUE if date is bad.
 if (NoBlank){                                            // Set NoBlank=1 if you don't want to allow for a blank date.
  var theError = CheckDate(theObject,theName);
 }else{
  var theError = CheckDate(theObject);
 }

 if (theError != ""){                   // Don't forget, this returns TRUE if date is bad, and FALSE if date is good.
  alert(theError);
  theObject.focus();
  return true;
 }
 
 return false;
 
}

function CheckDate(theObject,BlankMess) { // Validates Dates.  Option to require input.  Returns "" if date is GOOD.
 var theValue = theObject.value;
 
 if (theValue.length == 0 && BlankMess){ // Send a string as 2nd parameter if input is required
  theObject.focus();
  return "Please enter a " +BlankMess;
 }
 
 if (!_CF_checkdate(theObject.value)){
  theObject.focus();  
  return theValue +" is not a valid date.  Please enter a valid date in the format mm/dd/yyyy";
 }
 return "";
} 

function _CF_checkdate(object_value) 
    {
    // ON 8/10/08 changed valid year range to 1800 - 2100
    // Changed it back on 10/07/08 because it prevents people from entering 2 digit years.
    
    //Returns true if value is a date format or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a date in the mm/dd/yyyy format
	isplit = object_value.indexOf('/');

	if (isplit == -1 || isplit == object_value.length)
		return false;

    sMonth = object_value.substring(0, isplit);

	if (sMonth.length == 0)
        return false;

	isplit = object_value.indexOf('/', isplit + 1);

	if (isplit == -1 || (isplit + 1 ) == object_value.length)
		return false;

    sDay = object_value.substring((sMonth.length + 1), isplit);

	if (sDay.length == 0)
        return false;

	sYear = object_value.substring(isplit + 1);

	if (!_CF_checkinteger(sMonth)) //check month
		return false;
	else
	if (!_CF_checkrange(sMonth, 1, 12)) //check month
		return false;
	else
	if (!_CF_checkinteger(sYear)) //check year
		return false;
	else
	if (!_CF_checkrange(sYear, 0, 9999)) //check year (DON'T MESS WITH THIS RANGE)
		return false;
	else
	if (!_CF_checkinteger(sDay)) //check day
		return false;
	else
	if (!_CF_checkday(sYear, sMonth, sDay)) // check day
		return false;
	else
		return true;
    }



function _CF_checkday(checkYear, checkMonth, checkDay)
    {

	maxDay = 31;

	if (checkMonth == 4 || checkMonth == 6 ||
			checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else
	if (checkMonth == 2)
	{
		if (checkYear % 4 > 0)
			maxDay =28;
		else
		if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}

	return _CF_checkrange(checkDay, 1, maxDay); //check day
    }



function _CF_checkinteger(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
	return _CF_checknumber(object_value);
    else
	return false;
    }



function _CF_numberrange(object_value, min_value, max_value)
    {
    // check minimum
    if (min_value != null)
	{
        if (object_value < min_value)
		return false;
	}

    // check maximum
    if (max_value != null)
	{
	if (object_value > max_value)
		return false;
	}
	
    //All tests passed, so...
    return true;
    }



function _CF_checknumber(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
    }



function _CF_checkrange(object_value, min_value, max_value)
    {
    //if value is in range then return true else return false

    if (object_value.length == 0)
        return true;


    if (!_CF_checknumber(object_value))
     	{
     	return false;
     	}
         else
     	{
     	return (_CF_numberrange((eval(object_value)), min_value, max_value));
     	}
	
    //All tests passed, so...
    return true;
    }
