/****************************************************
File Name	:	Js_validation.JS
Purpose		:	Contains all the required Validation Functions

Modified On: August 23, 08
Modified For: Added 1 function -	checkSelect
****************************************************/


/****** Function to check if Mandatory fields are blank ************/
function checkBlank(obj,fldName)
{
	obj.value=trim_string(obj)
	if(obj.value=="")
	{
		alert("Please fill " + fldName + " field.")
		obj.select()
		obj.focus()
		return false
	}
	return true
}
//- End of function		checkBlank
/**********************************************************************/


/****** Function to Trim the String ************/
function trim_string(obj) 
{
     var ichar, icount;
     var strValue = obj.value
     ichar = strValue.length - 1;
     icount = -1;
     while (strValue.charAt(ichar)==' ' && ichar > icount)
         --ichar;
     if (ichar!=(strValue.length-1))
         strValue = strValue.slice(0,ichar+1);
     ichar = 0;
     icount = strValue.length - 1;
     while (strValue.charAt(ichar)==' ' && ichar < icount)
         ++ichar;
     if (ichar!=0)
         strValue = strValue.slice(ichar,strValue.length);
     return strValue;
}
//- End of function			trim_string
/**********************************************************************/



/****** Function to check the Valid Name ******/
function checkValidName(obj, fldName, mandflag) 
{
	obj.value=trim_string(obj)

	if (mandflag == 1) {
		if(!checkBlank(obj ,fldName))		// Check if Name is blank
			return false;
	}

	if (mandflag == 0 && obj.value == '') {
		return true;
	}
	
	var namePattern = /^[a-zA-Z. -\']+$/;		
		// Regular Expression for Valid Name. 
		// Accepts DOT(.), SPACE(Between Name like 'Vinod Tigadi), APOSTROPHE(')

	if(!namePattern.test(obj.value)) 
	{
		alert(fldName + ' is not valid \nInvalid Characters found.');
		obj.select()
		obj.focus()
		return false
	}
	
	return true	
}
//- End of function			checkValidName
/**********************************************************************/



/* Function to check the Valid Email which accepts the Label Name and Mandatory Flag */
function checkValidEmail(obj, fldName, mandflag)
{
	obj.value=trim_string(obj)
	
	if (mandflag == 1) {
		if(!checkBlank(obj ,fldName))		// Check if Name is blank
			return false;
	}

	// Check for the valid Email pattern
	//emailPattern = /^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2,4})$/;			// Won't accept the Capital Letters
	emailPattern = /^[a-zA-Z0-9_\-]+(\.[_a-zA-Z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2,4})$/;
	if(!emailPattern.test(obj.value)) 
	{
		alert('Please Enter Valid Email Address for ' + fldName);
		obj.select();
		obj.focus();		
		return false;
	}
	return true;
}
//- End of the function		checkValidEmail
/**********************************************************************/


/* Function to check the Valid Phone Number */
function checkValidPhone(obj, fldname, mandflag)
{
	obj.value=trim_string(obj)
	
	if (mandflag == 0 && obj.value == '')
	{
		return true;
	}

	// Check for the valid Email pattern
	phonePattern = /^[0-9]{7,14}$/
	if(!phonePattern.test(obj.value)) 
	{
		alert('Please Enter Valid '+fldname+' Number \n Space, - , etc are not allowed. Enter only Number ');
		obj.focus();		
		return false;
	}
	return true;
}
//- End of the function		checkValidPhone
/**********************************************************************/


/* Function to check whether the Radio Buttons are selected or not */
function checkRadio(obj, fldname)
{
	objLength = obj.length;
	for (i=0;i<objLength ; i++ )
	{
		if (obj[i].checked == true)
		{
			return true;
		}
	}
	alert('Please Select the '+fldname);
	return false;
}
//- End of the function		checkRadio
/**********************************************************************/

/* Function to check the valid selection from SELECT list */
function checkSelect(obj, fldname)
{
	if (obj.selectedIndex == '0')			// Considering that the first option from the list is not valid
	{
		alert('Please Select the '+fldname);
		obj.focus();
		return false;		
	}

	return true;
}
//- End of the function		checkSelect
/**********************************************************************/

