// Support Script (555)
function StripChars(theFilter,theString)
{
	var strOut,i,curChar

	strOut = ""
	for (i=0;i < theString.length; i++)
	{		
		curChar = theString.charAt(i)
		if (theFilter.indexOf(curChar) < 0)	// if it's not in the filter, send it thru
			strOut += curChar		
	}	
	return strOut
}

function AllInRange(x,y,theString)
{
	var i, curChar
	
	for (i=0; i < theString.length; i++)
	{
		curChar = theString.charAt(i)
		if (curChar < x || curChar > y) //the char is not in range
			return false
	}
	return true
}


function reformat (s)
{
    var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) 
           resultString += arg;
       else 
       {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}

function Trim(theString)
{
 var i,firstNonWhite

 if (StripChars(" \n\r\t",theString).length == 0 ) return ""

	i = -1
	while (1)
	{
		i++
		if (theString.charAt(i) != " ")
			break	
	}
	firstNonWhite = i
	//Count the spaces at the end
	i = theString.length
	while (1)
	{
		i--
		if (theString.charAt(i) != " ")
			break	
	}	

	return theString.substring(firstNonWhite,i + 1)

}
// Support Script (583)
function ValidateSSN(element,Required)
{
	var msg = ""
	var theString = element.value;
 var msgInvalid = "Please enter a valid Social Security Number.\nSuch as 555-66-7777";

	var theLen = StripChars(" \n\t\r",theString).length

	if (theLen == 0)	
		if (!Required) return ""		
		else return "Required field.  " + msgInvalid

	theString = StripChars(" -.\n\r",theString)		

	if (!AllInRange("0","9",theString) || theString.length != 9)
	{
		msg = msgInvalid
	}	

	if (msg == "")
	{
		element.value = reformat(theString,"",3,"-",2,"-",4)
	}

	return msg		
}
// Support Script (558)
function ValidateDate(element,Required,when,Reformat)
{
	var msg = "";
	var theString = element.value
	var dateVar = new Date(theString);
	var peavd = "Please enter a valid date. (for example\n5/3/97 or May 3, 1997 or 3 May 1997)";

	var theLen = StripChars(" ",theString).length
	if (theLen == 0)
	{
		if (!Required) return "";
		else return "Required field.  " + peavd;
	}

	if (isNaN(dateVar.valueOf()) || (dateVar.valueOf() == 0))
		return peavd;

	// Check for correct range.
	if (when == "Past")
	{
		var Today = new Date();
		if (dateVar >= Today)
		return "Please enter a date in the past (Today is " + Today.toLocaleString() +")";
	}
	if (when == "Future")
	{
		var Today = new Date();
		if (dateVar <= Today)
		return "Please enter a date in the future (Today is " + Today.toLocaleString() +")";
	}

	// We have a valid date. Reformat it and write it back to the control
	var style = Reformat;
	var d = dateVar;
	fullYear = d.getYear();
	if ((fullYear >=0) && (fullYear <=35)) fullYear += 2000;
	if ((fullYear >=36) && (fullYear <=99)) fullYear += 1900;

	if (style == "MM/DD/YYYY") {
	  var date_string = (d.getMonth()+1) + "/" + d.getDate() + "/" + fullYear;
	  }
	  else if (style == "MM-DD-YYYY") {
	  var date_string = (d.getMonth()+1) + "-" + d.getDate() + "-" + fullYear;
	  }
	  else if (style == "DD/MM/YYYY") {
	  var date_string = d.getDate() + "/" + (d.getMonth()+1) + "/" + fullYear;
	  }
	  else if (style == "Month Day, Year") {
	  var date_string = getMonthName(d) + " " + d.getDate() + ", " + fullYear;
	  }
	  else if (style == "Day of week, Month Day, Year") {
	  var date_string = getDayName(d) + " " + getMonthName(d) + " " + d.getDate() + ", " + fullYear;
	  }
	  else if (style == "Locale") {
	  var date_string = d.toLocaleString();
	  }
	  else if (style == "Don't reformat") {
	  var date_string = theString;
	  }
	  else {
	  var date_string = d.toGMTString();
  }

	element.value = date_string;

	return msg;
}

// Support Script (588)

function ValidateZipCode(element,Required)
{
	var msg = ""
	var theString = element.value;
 var msgInvalid = "Please enter a valid Zip Code.\nSuch as 92008 or 92008-1337"

	var theLen = StripChars(" \n\t\r",theString).length
	if (theLen == 0)	
		if (!Required) return ""		
		else return "Required field.  " + msgInvalid

	theString = StripChars("- \n\r",theString)		

	if (!AllInRange("0","9",theString))
	{
		msg = msgInvalid
	}
	else if (theString.length != 5 && theString.length != 9)
	{
		msg = msgInvalid
	}

	if (msg == "")
	{
		if (theString.length == 9)		
			element.value = reformat(theString,"",5,"-",4);
		else
     element.value = theString;
   
	}
	return msg	
}
// Support Script (580)
function ValidateInteger(element,Required,intMin,intMax)
{
	var theString = element.value;
	theString = StripChars(" ",theString);
	var min = intMin;
	var max = intMax;
	var pean = "Please enter an integer.";
	
	if (theString.length == 0)	
	{
		if (!Required) return ""		
		else return "Required field.  " + pean;
	}

	// remove leading zeros (zeros are only leading if there is more than one char)
	while (theString.length > 1 && theString.substring(0,1) == "0")
	{
		theString = theString.substring(1, theString.length);
	}

	var val = parseInt(theString);
	if (isNaN(val)) return pean;
	
	// check for non-digits (and minus sign). Do this by converting number
	// back to a string and comparing it to original string.
	if (val.toString() != theString) return pean;
	
	if (min < max)
	{
		if ((val < min) || (val > max))
		{
			return "Please enter an integer between " + min + " and " + max + ".";
		}
	}
	   
	// reset the entered string after removal of spaces and leading zeros.
	element.value = theString;
	return "";
}

// Support Script (578)
function ValidateFloatingPoint(element,Required,fltMin,fltMax)
{
   var theString = element.value;
   var val = parseFloat(theString);
   var msg = "";
   var min = fltMin;
   var max = fltMax;
  
  	var theLen = StripChars(" \n\t\r",theString).length
	  if (theLen == 0)	
		  if (!Required) return ""		
		  else return "Required field.  Please enter a floating point number."

   if (!isNaN(val))
   {
      if (min < max)
      {
        if ((val < min) || (val > max))
        {
           msg = "Please enter a floating point number between " + min + " and " + max + ".";
        }
      }
   }
   else
   {
      msg = "Please enter a number.";
   }
   return msg;
}

// Support Script (586)
function ValidateUSPhone(element,Required)
{
	var msg = ""
	var theString = element.value;
 var msgInvalid = "Please enter a valid U.S. phone number.\nSuch as (760) 555-1212"

	var theLen = StripChars(" \n\t\r",theString).length
	if (theLen == 0)	
		if (!Required) return ""		
		else return "Required field.  " + msgInvalid

	theString = StripChars("*() -./_\n\r",theString)		

	if (!AllInRange("0","9",theString))
	{
		msg = msgInvalid
	}
	else if (theString.length == 11)
	{
		 if (theString.charAt(0) != "1")	
			msg = msgInvalid
	}
	else if (theString.length != 10 && theString.length != 7)
	{
		msg = msgInvalid
	}

	if (msg == "")
	{
		if (theString.length == 10)
			element.value = reformat(theString,"(",3,") ",3,"-",4);
		else if (theString.length == 7)
			element.value = reformat(theString,"",3,"-",4);
		else //len == 11
			element.value = reformat(theString,"",1,"(",3,") ",3,"-",4);
	}
	return msg		
}

// Support Script (586)
function ValidateUSPhone2(element,Required){

	var msg = ""
	var theString = element.value;

 var msgInvalid = "Please enter a valid U.S. phone number.\nSuch as (760) 555-1212"


	//dont allow 3 or 6 consecutive numbers, ie 88888888
	for(var j = '0'; j <= 9; j++){
		var q = '';
		q = j.toString();
		q+= j;
		q+= j;
		var q1 = q.toString();
		
		q+= j;
		q+= j;
		q+= j;
		q = q.toString();
		

		for(var i = 0; i < 5; i++){
			if(theString.substring(i,i + 6) == q){
				msg = "\n" + theString + " is an invalid phone number.";
			}
		}
		
		if(theString.substring(0,3) == q1){
			msg = "\nThe first 3 numbers may not be " + theString.substring(0,3) + " in the phone number field";
		}

	}
	
	
	var theLen = StripChars(" \n\t\r",theString).length
	if (theLen == 0)	
		if (!Required) return ""		
		else return "\n You must enter a phone number ";

	theString = StripChars("*()!@#$^~`][{}%+|= \\ \"%&*:;<>,.?' -./_\n\r",theString)
	element.value = theString;		

	//strip the leading 1 in the phone number
/*
	 if (theString.charAt(0) == "1"){
		 theString = theString.substring(1,9);
		 element.value = theString;
	 }
*/
	//dont allow the phone to start with 0
	 if (theString.charAt(0) == "0" || theString.charAt(0) == "1"){
		msg = "\n The first number may not be 0 or 1 in the phone number field";
	 }

	//dont allow 800 numbers
	 if(theString.substring(0,3) =='800' || theString.substring(0,3) =='866' || theString.substring(0,3) =='877' || theString.substring(0,3) =='888'){
		msg = "\n Numbers 1 through 3 can not be " + theString.substring(0,3);
	 }
	 
	 if(theString.substring(3,6) =='911'){
		msg = "\n Numbers 4 through 6 can not be 911 in the phone number field";
	 }
	 if(theString.substring(3,6) =='555'){
		msg = "\n Numbers 4 through 6 can not be 555 in the phone number field";
	 }
	 
	 //fourth character must be greater than 1
	 if (theString.charAt(3) < 2){
		msg = "\n The 4th number in the phone number field must be greater than 1";
	 }
	 //only allow valid numbers
	if (!AllInRange("0","9",theString))
	{
		msg = "\n You may only enter valid numbers in the phone number field";
	}
	//must be 10 characters
	else if (theString.length != 10)
	{
		msg = "\n You must enter 10 valid numbers in the phone number field";
	}

	return msg		
}


// Support Script (576)
function ValidateEMail(element,Required)
{
   var msg = "";
   var val = element.value;
   var msgInvalid = "\n Email: Please enter an @ symbol.";
   var msgInvalid1 = "\n Email: Must contain a period.";

  	var theLen = StripChars(" ",val).length
	  if (theLen == 0)	
		  if (!Required) return ""		
		  else return "Required field.  " + msgInvalid

   if (val.indexOf("@",0) < 0) 
   {
      msg = msgInvalid 
   }

   return msg;
}


// Support Script (584)
function ValidateTime(element,Required)
{
  var msg = "";
  var val = element.value;
  var msgInvalid = "Please enter a valid time (for example\n13:30:00 or 13:30 or 1:30 pm)";

  var theString = element.value;

 	var theLen = StripChars(" \n\t\r",theString).length
  if (theLen == 0)	
	  if (!Required) return ""		
	  else return "Required field.  " + msgInvalid

  var result = Date.parse("1/1/1970 " + val);
  if (isNaN(result))
  {
    msg = msgInvalid
  }
  
  return msg;
}

function ValidateRequired(element)
{
  var msg = "";
  var theString = element.value;
  var theLen = StripChars(" \n\t\r",theString).length;
  
  if (theLen ==0){
    return "Required field.";
  }

  return msg;
}

