/* ----------------------------------------------------------------------------------------------
Author:	Brent McClintock - bmcclintock@legis.state.pa.us
Date:	08/08/2002
Purpose: collection of general form validation functions
Includes:	
	1) isInteger(object_id, value)
	2) isNumber(value)
	3) isRequired(object_id)	***** REQUIRES stringFunctions.js library for TRIM() function *****
	4) textAreaTooLong(object_id,max_length)
	
----------------------------------------------------------------------------------------------- */

//**********************************************************************************************
//returns true if value is integer, else false
function isDecimal(obj_id) 
{
	var thisObj = document.getElementById(obj_id);
	var val = Trim(document.getElementById(obj_id).value);
	var digits="1234567890.";
	//added this line to catch if user only enters spaces.  pjg 6/2004
	if(val.length == 0){
		alert("Please enter a decimal value.");
		highlightField(thisObj);
		thisObj.focus();
		thisObj.select();		
		return false;
	}
	for (var i=0; i < val.length; i++) 
	{
		if (digits.indexOf(val.charAt(i))==-1)
		{ 
			alert("Please enter a decimal value");
			highlightField(thisObj);
			thisObj.focus();
			thisObj.select();
			return false; 
		}
	}
	return true;
}

//**********************************************************************************************
//returns true if value is integer, else false
function isInteger(obj_id,val) {
	var thisObj = document.getElementById(obj_id);
	val = Trim(val);
	var digits="1234567890";
	//added this line to catch if user only enters spaces.  pjg 6/2004
	if(val.length == 0){
		alert("Please enter an integer.");
		highlightField(thisObj);		
		thisObj.focus();
		thisObj.select();		
		return false;
	}
	for (var i=0; i < val.length; i++) 
	{
		if (digits.indexOf(val.charAt(i))==-1)
		{ 
			alert("Please enter an integer");
			highlightField(thisObj);
			thisObj.focus();
			thisObj.select();
			return false; 
		}
	}
	return true;
}
	
//**********************************************************************************************
//returns true if value is a number, else false
function isNumber(val) 
{ 
	val = Trim(val);
	return !isNaN(val); 
}	
//**********************************************************************************************
//returns true if field contains data, false if field is empty
//NOTE: requires stringFunctions.js for		function Trim()	!!!!
function isRequired(object_id)
{
	var thisObj = document.getElementById(object_id);
	temp_val = thisObj.value;
	
	if(temp_val.length > 0){ temp_val = Trim(temp_val); }
	if(temp_val.length == 0)
	{
		alert("Please enter a value, this is a required field.");
		highlightField(thisObj);
		thisObj.focus();
		thisObj.select();
		return false;
	}
	else
	{ return true; }
}
//**********************************************************************************************
//returns true if field contains data, false if field is empty
//requires you to include the field id name as well as the name of the field you want to appear
// in the alert box.
//NOTE: requires stringFunctions.js for   function Trim() !!!!!
// pjg 05/2004
function fieldRequired(object_id, alertName){
	var thisObj = document.getElementById(object_id);
	temp_val = thisObj.value;
	
	if(temp_val.length > 0){ temp_val = Trim(temp_val); }
	if(temp_val.length == 0){
		alert("Please enter a value in the " + alertName + " field, this value is required.");
		highlightField(thisObj);		
		thisObj.focus();
		thisObj.select();
		return false;
	}
	else {
	return true; 
	}
}//end function isRequired
//**********************************************************************************************
// checks each keystroke on a TEXT AREA field to make sure it hasn't exceeded it's maximum
function textAreaTooLong(object_id,maxx)
{
	var thisObj = document.getElementById(object_id);
	var theTxt = thisObj.value;
	var txtLen = theTxt.length;

	if(txtLen >= maxx) 
	{ thisObj.value = theTxt.substr(0,maxx); }
}
//**********************************************************************************************
// checks in a select box that has "multiple" as an attribute to see if any options have been
//  selected.
// pjg 5/2004
function checkSelect(objectId, alertName){
	var sw = 0;
 	var thisObj = document.getElementById(objectId);
	
	for (i = 0; i < thisObj.length; i++){
		if(thisObj.options[i].selected == true){
			sw = 1;
			break;			 
		} // end if 
	} // end for  
	if(sw == 0){
		alert("Please choose a value in the " + alertName + " field, this value is required.");
		thisObj.focus();			
		return false;
	} else {
    	return true;
  	} // if
} // end function checkSelect()  
//**********************************************************************************************
//returns true if field contains data, false if field is empty
//NOTE: requires stringFunctions.js for		function Trim()	!!!!
// this function is exactly like isRequired() and fieldRequired() but doesn't do an alert.
//6/2004 pjg 
function strExists(object_id)
{
	var thisObj = document.getElementById(object_id);
	temp_val = thisObj.value;
	
	if(temp_val.length > 0){ temp_val = Trim(temp_val); }
	if(temp_val.length == 0)
	{
		return false;
	}
	else
	{ return true; }
}

//**********************************************************************************************
//BAM added 02202009 - users were complaining that validation wasn't obvious.  highlight field to make it stand out
function highlightField(obj)
{
	obj.style.backgroundColor = '#FFCCCC';	
	obj.style.borderColor = '#FF0000';
}


