
/*******************************************************************************************************************************
	Notes: the calling function should be like this:

	if(form_name_validation_error("form_name","control_name","data_type","atttribute","message")) return false;

	the function returns true if error occurs!

	data_type parameters:
		char 			=>	returns true if nothing is entered
		space 			=> 	returns true if space is entered
		in_space 		=> 	returns true if space present between string/words
		sp_char 		=> 	returns true if special characters are present in the string entered  **Note: by default takes !@#$%^&*()+=-[]\\\';,./{}|\":<>? else takes the character sent through parameter 'attribute'
		length	 		=> 	returns true if length of the string entered is not equal to the value sent through parameter 'attribute'
		max_length 		=> 	returns true if length of the string entered is greater than the value sent through parameter 'attribute'
		min_length 		=> 	returns true if length of the string entered is less than the value sent through parameter 'attribute'
		char_not_match 	=> 	returns true if string entered matches the value sent through parameter 'attribute'
		char_match 		=> 	returns true if string entered does not matches the value sent through parameter 'attribute'
		email 			=> 	returns true if email id entered does not match the standard email id format
		num 			=> 	returns true if non numeric value is entered
		max_num 		=> 	returns true if value entered is greater than the value sent through parameter 'attribute'
		min_num 		=> 	returns true if value entered is less than the value sent through parameter 'attribute'
		eq_num 			=> 	returns true if value entered is not equal to the value sent through parameter 'attribute'
		not_eq_num 		=> 	returns true if value entered equals to the value sent through parameter 'attribute'
		check_box 		=> 	returns true if the checkbox specified by 'control_name' is not checked
		check_box_group	=> 	returns true if non of the checkbox in the checkbox group specified by 'control_name' is not checked
		radio_group 	=> 	returns true if non of the radio buttons in the radio button group specified by 'control_name' is not checked

******************************************************************************************************************************/

function form_name_validation_error(form_name, control_name, data_type, atttribute, message)
{

	
	if(data_type != 'check_box_group' && data_type != 'radio_group')
	{
		var control_value = eval("document."+form_name+"."+control_name).value;
		control_value = form_name_validation_Trim(control_value);
	}
	else
	{
		var obj = eval("window.document."+form_name).elements;
		var objcount = obj.length;
	}

	//------------------------ detect blank fields(entries) -----------------------//

	if(data_type == 'char')
	{
		if(control_value == '')
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}
		else
			return false;
	}

	//------------------------ detect space in entries -----------------------//

	if(data_type == 'space') 
	{
		if(control_value == '' && eval("document."+form_name+"."+control_name).value != "")
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}
		else
			return false;
	}
	
	//------------------------ check for space in string -----------------------//

	if(data_type == 'in_space')
	{
		var iSpace = " ";
		if (control_value.indexOf(iSpace) != -1) 
		{
			alert (message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}
  	}
	
	
	//------------------------ check for special characters -----------------------//
	
	if(data_type == 'sp_char')
	{
		var iChars;
		if(form_name_validation_Trim(atttribute) != '')
			iChars = form_name_validation_Trim(atttribute);
		else
		 	iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
		for (var i = 0; i < control_value.length; i++) 
		{
			if (iChars.indexOf(control_value.charAt(i)) != -1) 
			{
				alert (message);
				eval("document."+form_name+"."+control_name).focus();
				return true;
			}
  		}
	}
	
	
	//------------------------ check for special characters -----------------------//
	
	if(data_type == 'sp_char_mod')
	{
		var iChars;
		if(form_name_validation_Trim(atttribute) != '')
			iChars = form_name_validation_Trim(atttribute);
		else
		 	iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		for (var i = 0; i < control_value.length; i++) 
		{
			if (iChars.indexOf(control_value.charAt(i)) != -1) 
			{
				alert (message);
				eval("document."+form_name+"."+control_name).focus();
				return true;
			}
  		}
	}
	
	//---------------------- check for special characters except x----------------------//
	
	if(data_type == 'sp_char_mod_x')
	{
		var iChars;
		if(form_name_validation_Trim(atttribute) != '')
			iChars = form_name_validation_Trim(atttribute);
		else
		 	iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?abcdefghijklmnopqrstuvwyzABCDEFGHIJKLMNOPQRSTUVWYZ";
		for (var i = 0; i < control_value.length; i++) 
		{
			if (iChars.indexOf(control_value.charAt(i)) != -1) 
			{
				alert (message);
				eval("document."+form_name+"."+control_name).focus();
				return true;
			}
  		}
	}
	
	//------------------------ check character length -----------------------//
	
	if(data_type == 'length')
	{
		attrib = parseInt(atttribute);
		if((control_value.length != attrib))
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}	
		else
			return false;
	}
	
	//------------------------ check maximum character length -----------------------//
	
	if(data_type == 'max_length')
	{
		attrib = parseInt(atttribute);
		if((control_value.length > attrib))
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}	
		else
			return false;
	}
	
	//------------------------ check minimum character length -----------------------//
	
	if(data_type == 'min_length')
	{
		attrib = parseInt(atttribute);
		if((control_value.length < attrib))
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}	
		else
			return false;
	}
	
	//------------------------ check for specific string -----------------------//
	
	if(data_type == 'char_not_match')
	{
		if((control_value == atttribute))
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}	
		else
			return false;
	}
	
	//------------------------ check for absence specific string -----------------------//
	
	if(data_type == 'char_match')
	{
		if((control_value != atttribute))
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}	
		else
			return false;
	}
	
		
	//------------------------ check email values -----------------------//
	
	if(data_type == 'email')
	{
		var emailStr = control_value;
		var emailPat=/^(.+)@(.+)$/
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		var validChars="\[^\\s" + specialChars + "\]"
		var quotedUser="(\"[^\"]*\")"
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		var atom=validChars + '+'
		var word="(" + atom + "|" + quotedUser + ")"
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		var matchArray=emailStr.match(emailPat)
		if (matchArray==null) 
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}
		var user=matchArray[1]
		var domain=matchArray[2]
		if (user.match(userPat)==null)
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) 
		{
			for (var i=1;i<=4;i++) 
			{
				if (IPArray[i]>255) 
				{
					alert(message);
					eval("document."+form_name+"."+control_name).focus();
					return true;
				}
			}
				 
		}
		var domainArray=domain.match(domainPat)
		if (domainArray==null) 
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}
		var atomPat=new RegExp(atom,"g")
		var domArr=domain.match(atomPat)
		var len=domArr.length
		if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) 
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}
		if (len<2) 
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}
		return false;
	}
	
		
	//------------------------ check numerical values -----------------------//
	
	if(data_type == 'num')
	{
		if(isNaN(control_value) == true)
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}	
		else
			return false;
	}
	
	//------------------------ check for maximum numerical values -----------------------//
	
	if(data_type == 'max_num')
	{
		if(parseInt(control_value) > parseInt(atttribute))
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}	
		else
			return false;
	}
	
	//------------------------ check for minimum numerical values -----------------------//
	
	if(data_type == 'min_num')
	{
		if(parseInt(control_value) < parseInt(atttribute))
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}	
		else
			return false;
	}
	
	//------------------------ check for equality of specific numerical values -----------------------//
	
	if(data_type == 'eq_num')
	{
		if(parseInt(control_value) != parseInt(atttribute))
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}	
		else
			return false;
	}
	
	//------------------------ check for non-equality of specific numerical values -----------------------//
	
	if(data_type == 'not_eq_num')
	{
		if(parseInt(control_value) == parseInt(atttribute))
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}	
		else
			return false;
	}
	
	//------------------------ check checked check-box -----------------------//
	
	if(data_type == 'check_box')
	{
		if(eval("document."+form_name+"."+control_name).checked == false)
		{
			alert(message);
			eval("document."+form_name+"."+control_name).focus();
			return true;
		}
		else
			return false;
	}
	
	//------------------------ check checked check-box in check-box group -----------------------//
	
	if(data_type == 'check_box_group')
	{
		var check_box_group_not_selected = true;
		var focus_check_box_group;
		for(i=0; i<objcount; i++)
		{
			if(obj[i].name == control_name && obj[i].checked == true)
			{
				check_box_group_not_selected = false;
				focus_check_box_group = i;
			}
		}
		if(check_box_not_selected)
		{
			alert(message);
			obj[focus_check_box_group].focus();
			return true;
		}
		else
			return false;
	}
	
	//------------------------ check checked radio_button in radio_button group -----------------------//
	
	if(data_type == 'radio_group')
	{
		var radio_group_not_selected = true;
		var focus_radio_group;
		for(i=0; i<objcount; i++)
		{
			if(obj[i].name == control_name && obj[i].checked == true)
			{
				radio_group_not_selected = false;
				focus_radio_group = i;
			}
		}
		if(radio_group_not_selected)
		{
			alert(message);
			//obj[focus_radio_group].focus();
			return true;
		}
		else
			return false;
	}
}

//----------------------------------------- TRIM FUNCTIONS --------------------------------------------------------//

function form_name_validation_Trim(TRIM_VALUE) //---> All Trim
{
	if(TRIM_VALUE.length < 1) 
		return "";
	
	TRIM_VALUE = Rform_name_validation_Trim(TRIM_VALUE);
	TRIM_VALUE = Lform_name_validation_Trim(TRIM_VALUE);
	
	if(TRIM_VALUE == "")
		return "";
	else
		return TRIM_VALUE;
}


function Rform_name_validation_Trim(VALUE) //---> Right Trim
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	
	if(v_length < 0)
		return	"";

	var iTemp = v_length -1;

	while(iTemp > -1)
	{
		if(VALUE.charAt(iTemp) != w_space)
		{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	}
	return strTemp;
}


function Lform_name_validation_Trim(VALUE) //---> Left Trim
{
	var w_space = String.fromCharCode(32);
	
	if(v_length < 1)
		return "";

	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length)
	{
		if(VALUE.charAt(iTemp) != w_space)
		{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	}
	return strTemp;
}

