//FileName: validating.js
//
//This file consists of several common validating functions.
//They can be called from a form just by including the file
//between script tags:
//
//   <script src="validating.js"></script>
//
//The following is a list of functions that can be called. They 
//all take FormElement and Required as parameters.
// FormElement represents the element on the form you are validating
// (i.e. document.formname.formelement )
// (     document.masterform.lastname  )
// Required should be either true or false. (no quotes around the value)
// Those functions that take Min and Max should be integers that
// define the minimum number of characters allowed and the maximum number
// of characters allowed.
// Each function will display a message box if there was an error and 
// return false, if no errors it will return true.
//
//	   FN_ValidateEmail(FormElement,Required)
//     FN_ValidateRadioButtonSelection(FormElement,Required)
//     FN_ValidateCompanySymbol(FormElement,Required)
//     FN_ValidateAlphaNum(FormElement,Required,Min, Max)
//	   FN_ValidateAlphaNumBlanks(FormElement,Required,Min,Max)
//     FN_ValidateAlpha(FormElement,Required,Min,Max)
//     FN_ValidateAlphaBlanks(FormElement,Required,Min,Max)
// 	   FN_ValidateNum(FormElement,Required,Min, Max)
//     FN_ValidateListSelection(FormElement,Required)
//

//set up global error messages

var msgRequired = "This is a required field.";
var msgMin = "Minimum number of characters are ";
var msgMax = "Maximum number of characters allowed are ";


//****Validates to see if email is valid with "@" and "."

function FN_ValidateEmail(FormElement,Required)
{
   var msg = "";
   var val = FormElement.value;
   var msgInvalid = "Invalid E-Mail";
   //var requiredMsg = "This is a required field." 	   
   // var requiredMsg = FormElement.name.toUpperCase() + " is a required field." 
   var theLen = FN_StripChars(" ",val).length
   if (theLen == 0)	     
   {
   	if (!Required) 
     {return true}
   	else 
   	 {msg = msgRequired}
   }
   if (val.indexOf("@",0) < 0 || val.indexOf(".") < 0) 
   {
   	msg = msgInvalid;
   }
   if (msg != "")  {
  	 FormElement.focus();
     alert(msg);
	 return false;
	 
	 }
  return true;
  }

//****Validates to see if Radio Button has been checked.
//****Radio buttons don't have a focus method so focus should be set to false.

function FN_ValidateRadioButtonSelection(FormElement,Required)
{
    var radio = FormElement;
    var msg = "";
	var msgSelection = "You must make a selection ";
 
	for (var i = 0; i < radio.length; i++)
    {  
	
       if (radio[i].checked) { return true;}
    }
    if (!Required )
	 {return true} 
		 
    alert(msgSelection);
	return false;
   
    
}

//****Validates to see if Company Symbol has been entered with no more that 3 characters.

function FN_ValidateCompanySymbol(FormElement,Required)
{
       var msg = "";
	   var msgNoSpacesAllowed = "No spaces allowed for this field.";
  	   var theString = FormElement.value;
	   var min = 1;
	   var max = 3;
		
	   var theLen = FN_StripChars(" ",theString).length

	   if ((theLen == 0)&& (!Required))
	     { return true }

	   if (theString.indexOf(" ",0) >= 0) 
         { msg = msgNoSpacesAllowed }
       else {
	        
	        if (!(FN_ValidateAlphaNum(FormElement,Required,min, max)))
	          {return false}
			}
	   if (msg != "")  
	   {
  	 	 FormElement.focus();
      	 alert(msg);
	  	 return false;
	   }
   return true;
}

//****Validates for Alpha & Numeric characters

function FN_ValidateAlphaNum(FormElement,Required,Min, Max)
{
	var msg = "";
	var i, m, s, firstNonWhite
	var theString = FormElement.value;
	var msgAlphaNum = "Must be alpha numerics only";
	var min = Min;
	var max = Max;
	if (FN_StripChars(" ",theString).length == 0)	     
	 {
		if (!Required) 
        	{return true}
		else       
	      {msg = msgRequired}
    }
	else 
	{
		//Strip spaces off of the sides of the string
 		theString = FN_Trim(theString);
    	if (Max == 0) {
		   max = theString.length;
		   } 

    	for (var n=0; n < theString.length; n++)     
	  	{
         		theChar = theString.substring(n,n+1);
         		if (!FN_AllInRange("0","9",theChar) && !FN_AllInRange("A","Z",theChar.toUpperCase()))// && !(theChar == " "))     
		 		 {
					if( !(FN_SpecialChar(FormElement,theChar))) {
					msg = msgAlphaNum;
					break;
					}
      	  		 }
        }	//end of for
        if (theString.length < min) 
		 { msg = msgMin + min }
		if (theString.length > max)
		 { msg = msgMax + max }
	 }//end of else

    if (msg != "")
	   {
  	 	 FormElement.focus();
      	 alert(msg);
		 return false;
	   }
   return true;

}

//****Validates for Alpha & Numeric characters and Blanks

function FN_ValidateAlphaNumBlanks(FormElement,Required,Min, Max)
{
	var msg = "";
	var i, m, s, firstNonWhite
	var theString = FormElement.value;
	var msgAlphaNum = "Must be alpha numerics only";
	var min = Min;
	var max = Max;
	if (FN_StripChars(" ",theString).length == 0)	     
	 {
		if (!Required) 
        	{return true}
		else       
	      {msg = msgRequired}
    }
	else 
	{
		//Strip spaces off of the sides of the string
 		theString = FN_Trim(theString);
    	if (Max == 0) {
		   max = theString.length;
		   } 

    	for (var n=0; n < theString.length; n++)     
	  	{
         		theChar = theString.substring(n,n+1);
         		if (!FN_AllInRange("0","9",theChar) && !FN_AllInRange("A","Z",theChar.toUpperCase()) && !(theChar == " "))     
		 		 {
					if( !(FN_SpecialChar(FormElement,theChar))) {
					msg = msgAlphaNum;
					break;
					}
      	  		 }
        }	//end of for
        if (theString.length < min) 
		 { msg = msgMin + min }
		if (theString.length > max)
		 { msg = msgMax + max }
	 }//end of else

    if (msg != "")
	   {
  	 	 FormElement.focus();
      	 alert(msg);
		 return false;
	   }
   return true;

}



//****Validates Special Characters***********
//this function will return true or false
//it will NOT DISPLAY an error message box.
//it is used as a utility functionm, called from 
//other functions within this file.
//shouldnt be directly from the form.

function FN_SpecialChar(FormElement,TestChar)
{
	var spCharSharingCos = new Array(" ", ".", "-", ",", "'");
	var spCharTermOper = new Array(" ", ".", "-", "'", ",");
	var spCharUrl = new Array("/", "_", ":", "-", ".", "@");
	var spCharDept = new Array(",", "'", "(", ")");
	var spCharTitle = new Array("-", "'", "/", ",", "(", ")", ".");
	var spCharDest = new Array("-");
	var spCharMedDir = new Array(" ", ".", "-", "'", ",");
	var spCharUnderwriter = new Array(" ", ".", "-", "'", ",");
	var spCharCoName = new Array(" ", "." , "-", "@", ",", "(", ")", "'");
	var spCharAddress = new Array(" ", ".", "-", ",", "'" , "\r" , "\n" );
	var spCharFName = new Array("*", "\'", "-", "'");
	var spCharLName = new Array(" ","*", "\'", "-", "'");
	var msg = "";
	var msgInvalid = "You have entered an invalid Character.";
	var TestArray="";
	var found  ="false";
	
	if (FormElement.name.indexOf("sharingcos") != -1) 
	{
		TestArray = spCharSharingCos;
	}
	if (FormElement.name.indexOf("aname") != -1) 
	{
		TestArray = spCharTermOper;
	}
	if (FormElement.name.indexOf("medicaldirector") != -1) 
	{
		TestArray = spCharMedDir;
	}
	if (FormElement.name.indexOf("firstname") != -1) 
	{
		TestArray = spCharFName;
	}
	if (FormElement.name.indexOf("lastname") != -1)
	{
		TestArray = spCharLName;
	}
	//validate company for title
	if  (FormElement.name.indexOf("title") != -1 )
	{
		TestArray = spCharTitle;
	}
	//validate company for Department
	if  (FormElement.name.indexOf("dept") != -1 )
	{
		TestArray = spCharDept;
	}
	//validate company for name
	if  (FormElement.name.indexOf("coname") != -1 )
	{
		TestArray = spCharCoName;
	}
	//validate company for Underwriter 
	if  (FormElement.name.indexOf("underwriter") != -1 )
	{
		TestArray = spCharUnderwriter;
	}
	//validate company for destination code 
	if  (FormElement.name.indexOf("dest") != -1 )
	{
		TestArray = spCharDest;
	}
	
	//validate Web Site Url 
	if  (FormElement.name.indexOf("url") != -1 )
	{
		TestArray = spCharUrl;
	}

	if ( (FormElement.name.indexOf("add") != -1 ) || (FormElement.name == "pobox")) 
	//if ((FormElement.name == "add1") || (FormElement.name == "add2") ||
	//(FormElement.name == "pobox"))
	{
		TestArray = spCharAddress;
	}
	
	for(i=0; i < TestArray.length;i++)
	{
		if (TestChar==TestArray[i])
		{
			found = "true";
			break;
		}
	}
	if (found == "true")
		return true;
	else
	  {	
		return false;
	  }
		
}

//****Validates for Alpha characters

function FN_ValidateAlpha(FormElement,Required,Min,Max)
{
	var msg = "";
	var i, m, s, firstNonWhite
	var theString = FormElement.value;
	var msgAlpha = "Must be alpha characters only";
	var min = Min;	
	var max = Max;
	if (FN_StripChars(" ",theString).length == 0)	     
	{
	 if (!Required) 
    	{ return true }
 	 else       
	    { msg = msgRequired }
    }
	else
	{
		  //Strip spaces off of the sides of the string
 		  theString = FN_Trim(theString);
          if (Max == 0)                 
		   { max = theString.length; }		
          for (var n=0; n < theString.length; n++)     
		     {
                theChar = theString.substring(n,n+1);
                if (!FN_AllInRange("A","Z",theChar.toUpperCase())) //&& !(theChar == " "))     
	           	{
			       if(!(FN_SpecialChar(FormElement,theChar)))
				    { 
					
       			       msg = msgAlpha;
					   break;
					}
      	   		}
   		     }//end of for
	 
          if (theString.length < min)
		   { msg = msgMin + min}
		   
		  if (theString.length > max)
           { msg =msgMax + max }
        
     }	    //else

	 if (msg != "")
	  { 
	     FormElement.focus();
		 alert(msg);
		 return false;
	  }
	   
    return true;
}

//****Validates for Alpha characters and blanks

function FN_ValidateAlphaBlanks(FormElement,Required,Min,Max)
{
	var msg = "";
	var i, m, s, firstNonWhite
	var theString = FormElement.value;
	var msgAlpha = "Must be alpha characters only";
	var min = Min;	
	var max = Max;
	if (FN_StripChars(" ",theString).length == 0)	     
	{
	 if (!Required) 
    	{ return true }
 	 else       
	    { msg = msgRequired }
    }
	else
	{
		  //Strip spaces off of the sides of the string
 		  theString = FN_Trim(theString);
          if (Max == 0)                 
		   { max = theString.length; }		
          for (var n=0; n < theString.length; n++)     
		     {
                theChar = theString.substring(n,n+1);
                if (!FN_AllInRange("A","Z",theChar.toUpperCase()) && !(theChar == " "))     
	           	{
			       if(!(FN_SpecialChar(FormElement,theChar)))
				    { 
					
       			       msg = msgAlpha;
					   break;
					}
      	   		}
   		     }//end of for
	 
          if (theString.length < min)
		   { msg = msgMin + min}
		   
		  if (theString.length > max)
           { msg =msgMax + max }
        
     }	    //else

	 if (msg != "")
	  { 
	     FormElement.focus();
		 alert(msg);
		 return false;
	  }
	   
    return true;
}

//****Validates for Numeric characters

function FN_ValidateNum(FormElement,Required,Min, Max)
{	
	var msg = "";
	var i, m, s, firstNonWhite
	var theString = FormElement.value;
 	var msgNumerics  = "Must be numerics only";
	var min = Min;
	var max = Max;
	if (FN_StripChars(" ",theString).length == 0)	     
	{
	  if (!Required)       
	  { return true;	}	
      else
	  { msg =msgRequired }
    }
	else
	{
	   //Strip spaces off of the sides of the string
 	   theString = FN_Trim(theString);
	   if (Max == 0)     
	    {max = theString.length }
       for (var n=0; n < theString.length; n++)     
	   {
        theChar = theString.substring(n,n+1);
        if (!FN_AllInRange("0","9",theChar) && !(theChar == " "))    
		{  
		   msg = msgNumerics;
		   break;
		}
       } //end for
    
	   if (theString.length < min)  
	   { msg = msgMin + min}
	   if (theString.length > max)
	    { msg = msgMax + max }
       
     }   //end else
	if (msg != "")
	 {
	  FormElement.focus();
	  alert(msg);
	  return false;
	 }
 return true;
}

//****Validates for select list
//Note -if the list does have a size attribute
//then the selectedIndex will be -1 if nothing is selected.
//If the list doesn't have a size attribute then the selected
//
//will be the first item in the list so selectedIndex will be 0,
//which is the first item in the list.
//If the item selected has a a value of '' then this is 
//not considered a valid selection. This is in there to accomodate 
//those lists that don't have size attributes and want to show a blank
//as the first item in the list. In this case, a blank is not allowed
//as a valid selection.

function FN_ValidateListSelection(FormElement,Required)

{
    var list = FormElement;
    var msg = "";
	var msgSelection = "You must make a selection ";
	var selectedIndex = FormElement.selectedIndex;
									  
 	if (selectedIndex >= 0)
    {
	  	for (var i =0;i < list.length; i++)
     {  
	  //if the option selected has a blank value then this
	  //is not considered a valid selection.

      if ((list[i].selected) && (list[i].value != ""))
	    { return true;}
     } //end of for

	} //end of if


    if (!Required )
	 {return true}

	FormElement.focus();
    alert(msgSelection);
	return false;
   
    
}


//****Strips out spaces
//This functions strips theFilter out of the theString sent
//returns a new string minus those characters.

function FN_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
}

//****Trims the string of spaces at beginning and end.
//returns new string.

function FN_Trim(theString)
{
	var i,firstNonWhite
	if (FN_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)
}

//****Validates for characters in range

function FN_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
}

//****reformats

function FN_reformat(s)
{
	var arg;
    	var sPos = 0;
    	var resultString = "";
	for (var i = 1; i < FN_reformat.arguments.length; i++) {
       	arg = FN_reformat.arguments[i];
       	if (i % 2 == 1) 
        resultString += arg;
       	else 
       {
        resultString += s.substring(sPos, sPos + arg);
        sPos += arg;
       }
    }
    	return resultString;
}

