// 
//  Check Field Javascript File 
//
//  	Index of Functions
//    	==================
//	
//		checkrequired		- 
//		checkallrequired	-
//		checklength		 	- 
//  	removespaces		- 
//  	checkpassword		-
//  	checknumber			-
//		radiorequired	 	-
//		checkboxrequired	-
//		checktime			-
//		strip_characters	- 
//		check_textarea_maxlength - 
//
//--------------------------------------------------------------------------------------------------------------------
// Begin Functions 
//--------------------------------------------------------------------------------------------------------------------


function checkrequired(thisField)
// This function will check to see if a field is empty.  If it
// is false will be returned.  Otherwise true will be returned.
	{
	thisField = removespaces(thisField);
	if (thisField == "")
	   { return false; }
	return true;
	}

function checkallrequired(formname, arrayname, fields)
{
//----------------------------------------------------------------------------------------
//  FORMNAME	- name of the form the controls belong to
//  FIELDS		- names of the controls that you want to make required
//  notes:		- this function requires that you manually build a 2 dimensional 
//				  array before the include for this file. (see example below)
//				- the fields parameter should be an array of strings,
//				  enclosed by double quotes.
//				- there is a call to error() in this function, the error() function
//				  resides in globalutility.js
//	example:  	arrayname = new Array();
//				requiredarray[0]     = new Array();
//				requiredarray[0][0]  = 'control_name1';	
//				requiredarray[0][1]  = 'label_for_control_1name';			
//				requiredarray[1]     = new Array();
//				requiredarray[1][0]  = 'control_name1';	
//				requiredarray[1][1]  = 'label for control_1 name';	
//				...			
//				checkalldates('formname', 'arrayname', "'required1', 'required2', 'required3',...")
//----------------------------------------------------------------------------------------

	eval('fieldarray = new Array(' + fields + ');');
	for(var i=0; i<fieldarray.length; i++)
	{
		rtn = checkrequired(eval('document.' + formname + '.' + fieldarray[i] + '.value'));
		if(rtn == false)
		{
			fielddisplay  = requiredarray[i][1];
			var msg = fielddisplay +  ' is a required field';
			return error(msg, formname, fieldarray[i]);
		}
	}
	return true;
}//END FUNCTION
//----------------------------------------------------------------------------------------

function checklength(thisField, maxl)
// This function will check the length of the field passed in.
// If it exceeds the max length passed, the false will be returned.
	{   
	ll = thisField.length;
	if (ll > maxl)
	   { return false; }
	return true;
	}
	
function removespaces(somestr)
	{
 	do  // remove all the spaces in case the value is just spaces
      { i = somestr.search(" ");
       if (i > -1)
          { somestr = somestr.replace(" ", ""); }
      } while (i > -1);
	return somestr;
	}


function checktime(thetime){

// This function will verify a valid time field 
//
// 1.	- length is 8
// 2.	- characters 3 and 6 are ':'
// 3. 	- characters 1 and 2 are numeric and between 00 and 23
// 4.	- characters 4 and 5 are numeric and between 00 and 59 
// 5.	- characters 7 and 8 are numeric and between 00 and 59 
//

	var len = thetime.length;

// check length
	if(len != 8) return false;

// check for colons
	if(thetime.charAt(2) != ':' || thetime.charAt(5) != ':') return false;

// check hours
	if(thetime.substring(0,2) < '00' || thetime.substring(0,2) > '23') return false; 

// check minutes
	if(thetime.substring(3,5) < '00' || thetime.substring(3,5) > '59') return false; 

// check seconds
	if(thetime.substring(6,8) < '00' || thetime.substring(6,8) > '59') return false; 


	return true;  // Valid Time in 24hr HH:MM:SS format!

} 


	
function checkpassword(thisPassword)

// This function will verify a password has the following characteristics:
//
// 1.	- 8 to 10 characters in length
// 2.	- Upper and lower case characters present
// 3. 	- Numeric characters present
//
{
	var upperstring   = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var lowerstring   = "abcdefghijklmnopqrstuvwxyz";
	var numberstring  = "0123456789";

	var thepassword = thisPassword;

// check length
	ll = thepassword.length;
	if(ll < 6 || ll > 10) return false;

// check uppers
	var notfound = true;
	for (var ii=0; ii < ll; ii++)
	{
		if(upperstring.indexOf(thepassword.charAt(ii)) >= 0){
			notfound = false;
			ii = ll;			
		}
	}
	if(notfound) return false;

// check lowers
	notfound = true;
	for (var ii=0; ii < ll; ii++)
	{
		if(lowerstring.indexOf(thepassword.charAt(ii)) >= 0){
			notfound = false;
			ii = ll;			
		}
	}
	if(notfound) return false;

// check numbers
	notfound = true;
	for (var ii=0; ii < ll; ii++)
	{
		if(numberstring.indexOf(thepassword.charAt(ii)) >= 0){
			notfound = false;
			ii = ll;			
		}
	}
	if(notfound) return false;
	
	return true;  // Valid Password!
}
	


function checknumber(numval, maxval, dec, neg)
// This functions will take a value passed in and check to see if
// it is numeric.  It will also do other checks:
//	maxval: the maximum the value is allowed to be
//			smallint: 32,767
//			integer: 2,147,483,647
//	dec: The number of decimal places allowed
//	neg: Y negative allowed, N negative not allowed
//
//	returns: 0 - Everything is OK
//			-1 - Not a number
//		    -2 - Out of range
//		    -3 - Too many decimal places
	{
	if (numval == "" || numval == " " || numval == "  " || numval == "   ") { return -1 }
	if (neg == "Y")
	   { minval = maxval * -1; }
	else
	   { minval = 0; }
	if (numval == "")
	   { numval = 0; }
	else
	   { if (isNaN(numval) == true) 
		   { return -1; }
		 if (numval > maxval || numval < minval)
		   { return -2; }	
		}
	 var cc = numval.indexOf(".");
	 if (cc > -1)
	    { chk = numval.slice(cc + 1);
		  if (chk.length > dec )
		    { return -3;}	
		}
	return 0;
	}
/*
function checkallrequired(formname, fields)
{
//----------------------------------------------------------------------------------------
//  FORMNAME	- name of the form the controls belong to
//  FIELDS		- names of the controls that you want to make required
//  notes:		- the fields parameter should be an array of strings,
//				  enclosed by double quotes.
//				- there is a call to error() in this function, the error() function
//				  resides in globalutility.js
//	example:  	checkalldates('formname', "'required1', 'required2', 'required3',...")
//----------------------------------------------------------------------------------------
	var msg = 'Required field';

	eval('fieldarray = new Array(' + fields + ');');
	for(var i=0; i<fieldarray.length; i++)
	{
		rtn = checkrequired(eval('document.' + formname + '.' + fieldarray[i] + '.value'));
		if(rtn == false)
		{
			return error(msg, formname, fieldarray[i]);
		}
	}
	return true;
}//END FUNCTION
//----------------------------------------------------------------------------------------
*/




function radiorequired(form, control)
{
//----------------------------------------------------------------------------------------
//  FORM	- name of the form the controls belong to
//  CONTROL	- name of the radio control you are checking that is required
//  notes:	- after checking for the return value of radiorequired, you cannot
//			  call error() due to the fact that a radio button does not have a
//			  select() method (so, perform alert(); and return false;).
//----------------------------------------------------------------------------------------
		var arraylength;
		var counter = 0;
		eval('arraylength = document.' + form + '.' + control + '.length');
		for(var rdo=0; rdo<arraylength; rdo++)
		{
			if(eval('document.' + form + '.' + control + '[' + rdo + '].checked == true'))
			{
				counter++;
			}
		}
		if(counter == 0)
		{
			return false;
		}
		//we passed
		return true;
}//END FUNCTION
//----------------------------------------------------------------------------------------

function checkboxrequired(form, control)
{
//----------------------------------------------------------------------------------------
//  FORM	- name of the form the controls belong to
//  CONTROL	- name of the checkbox array you are checking that is required
//  notes:	- after checking for the return value of checkboxrequired, you cannot
//			  call error() due to the fact that a radio button does not have a
//			  select() method (so, perform alert(); and return false;).
//----------------------------------------------------------------------------------------
		var arraylength;
		var counter = 0;
		eval('arraylength = document.' + form + '.' + control + '.length');
		for(var chx=0; chx<arraylength; chx++)
		{
			if(eval('document.' + form + '.' + control + '[' + chx + '].checked == true'))
			{
				counter++;
			}
		}
		if(counter == 0)
		{
			return false;
		}
		//we passed
		return true;
}//END FUNCTION
//----------------------------------------------------------------------------------------

function strip_characters(inputstring, charstostrip){ 

	var outputstring = ""; 
	
	for(var charix = 0; charix < inputstring.length; charix++) {
		if(charstostrip.indexOf(inputstring.charAt(charix))== -1){
			outputstring = outputstring + inputstring.charAt(charix);
		}
	}

	return outputstring;

} //END FUNCTION
//----------------------------------------------------------------------------------------

function check_textarea_maxlength(maxlength, form, control, displayname){
	
	var currentlength = eval("document." + form + "." + control + ".value.length;");

	if(currentlength > maxlength){
		eval("document." + form + "." + control + ".focus();");
		eval("document." + form + "." + control + ".select();");
		alert("You have entered " + currentlength + " characters in the " + displayname + " field.  The maxiumum allowed is " + maxlength + "."); 
		return false;
	} 		

	return true;
} //END FUNCTION
//----------------------------------------------------------------------------------------

function check_blanket_fields(aifield, subrogationfield){ 
	// This function looks at Blanket Additional Insured or Blanket Subrogation 
	// fields checked attributes as arguments.
	// If either is checked a confirm is put up for the user to respond to. 
	if(aifield == true || subrogationfield == true){ 
		return confirm('You are certifying that this policy provides Blanket Additional Insured and/or Blanket Waiver of Subrogation.  This means your policy will automatically provide Additional Insured coverage and/or Waiver of Subrogation to any party required by contract between your Insured and any Certificate Holder.  Show any restrictions other than those in the standard endorsements in the Special Exclusions area.  Press OK to save and proceed, Cancel to return to editing the data.'); 
	} else { 
		return true; 
	} 	
} //END FUNCTION
//---------------------------------------------------------------------------------------- 
  
