// JavaScript Document

/***************************************
	descriptin	: Java script function for validation
	Author		: C.R.Rajesh
	created on      : 06-06-2004	
***************************************/
function countcheckbox(fname) 
	{
	tot=0;
		the_form = document.getElementById(fname);
	for (i = 0; i < the_form.elements.length; i++) 
	{
	the_element = the_form.elements[i];
	if(the_element.type == 'checkbox' && the_element.checked == true) 
	{
	tot=tot+1
	}
	}
	return tot;
	}
function checkEntry(strObj,nameObj)
{
        
	var str=strObj.value;
               
		if(str.length!=0)
		if(checkString(strObj,nameObj)==false)
			return false;
	if(strObj.value.length==0)
	{
		alert("The field '"+ nameObj +"' must have a value");
		strObj.value="";
		strObj.focus();
		return false;
	}
		return true;
}


     
    // Function Name:checkString
   //  description  :To check the string


function checkString(strObj,strName)
{
	var str=strObj.value;
		str=trimString(str);
	if(str.length!=0)
		for(i=0;i<str.length;i++){
			if(str.charAt(i)=="\""){
				alert("Invalid charecter in '"+strName+"'"); 
				strObj.focus();
				return false;
			}
		}
	else
		strObj.value="";
	return true;
}

/*****************************************************************
Script to trim the leading and traling spaces from the entered value
	Function Name	: trimString
	Arguments       : Form object's value
	Return Value	: Trimed value 
******************************************************************/

function trimString (str) 
{
	while (str.charAt(0) == ' ')
		str = str.substring(1);
  	while (str.charAt(str.length - 1) == ' ')
		str = str.substring(0, str.length - 1);
	return str;
}

/***************************************************************
	Script for checking the validity of E-Mail
	Function Name	: emailValid
	Arguments	: Name of the form object
		
/******************************************************************/
function emailValid(strObj)
	{
		email=strObj.value; //for getting the emailid
		iIndexAt = email.indexOf('@');  //getting the position of '@'
		iIndexDot = email.indexOf('.');  //getting the position of '.'
		afterDot =''; //for getting the char after '.'
		afterAt = ''; //for getting the char after '@'
		if(iIndexAt==-1)
		{	alert("Invalid Email Address");
			strObj.focus();
			return false;
		}

		if(email.charAt(0)=='@')
		{	alert("Invalid Email Address");
			strObj.focus();
			return false;
		}
		if(iIndexDot==-1)
		{	alert("Invalid Email Address");
			strObj.focus();
			return false;
		}
		
		if(iIndexAt>iIndexDot)
		{	alert("Invalid Email Address");
			strObj.focus();
			return false;
		}
		afterDot = email.charAt(iIndexDot+1)
		if((afterDot=="")||(afterDot==" "))
		{
			alert(" Invalid Email Address ");
			strObj.focus();
			return false;
		}
		afterAt = email.charAt(iIndexAt+1)
		if((afterAt==".")||(afterAt==" "))
		{
			alert(" Invalid Email Address ");
			strObj.focus();
			return false;
		}
		for(i=iIndexAt+1;i<email.length;i++)
		{
			if(email.charAt(i)=='@'){
				alert("The charecter '@' must not repeat")
				strObj.focus();
				return false
			}
		}
		i=iIndexDot+1
		if(email.charAt(i)=='.'){
			alert("The charecter '.' must not repeat")
			strObj.focus();
			return false
		}
		
		return true;
    } 
	
	/***************************************************************
	Script for checking the validity of E-Mail
	Function Name	: Phone no and Fax validation
	Arguments	: Name of the form object and the field name
		
******************************************************************/
	function phfaxValid(strObj,strName)
{
	var len=strObj.value.length;
	numVal="0123456 ()-789";
	for(i=0;i<len;i++)
	{	if(numVal.indexOf(strObj.value.charAt(i))==-1)
		{
			alert("the charecter '"+strObj.value.charAt(i)+"' is not alowed in the "+strName+" field");
			strObj.focus();
			return false;
		}
	}
	return true;
}
/***************************************************************
	Script for checking the validity of Confirm password
	Function Name	: Phone no and Fax validation
	Arguments	: Name of the form object
		
******************************************************************/

function confirmPwd(strobj)
{
	var formObj = strobj;
	var pwd = formObj.txtpassword.value;
	var conPwd = formObj.txtcpassword.value;
	var iPassLength = pwd.length;
	if((iPassLength>=6)&&(iPassLength<12))
	{
		if(pwd==conPwd)
				return true;
		else		
		{
			alert("Password re-typed does not match");				
			formObj.txtcpassword.value="";
			formObj.txtpassword.focus();
			return false;
		}
	}
	else
		{
			alert("password must be atleast of 6 charecters and not more than 12")
			formObj.txtcpassword.value="";
			formObj.txtpassword.value="";
			formObj.txtpassword.focus();
			return false;
		}
	return true;	
}
