/******************************************************************
Author name     :Elaya Manickam Narayanan.
Date Created	:19 June 2000
Purpose		    :Form Field validation
*******************************************************************/


//GENERAL FUNCTIONS

/*To set focus on the first element in the form
*****************************************************************
Purpose	: To set the focus on the first element in a form
Input	: none
Output	: none
Usage   : Call in the LOAD even in the body tag
*****************************************************************/

function setFocus()
{
	document.form1.elements[0].focus();
}
/*To trim leading spaces in the input 
*****************************************************************
Purpose	:To trim leading spaces in the input
Input	: a String
Output	: Trimmed String
*****************************************************************/
function trim(temp)
{
	var len=temp.length;
	var temp1;
	temp1="";
	for(i=0;i<len;i++)
	{
		if(temp.charAt(i)!=" ")		
				break;	
	}	
	for(j=i;j<len;j++)
	{
		temp1=temp1+temp.charAt(j);
	}		
	return temp1;
}

/*To check for spl chars in the input
*****************************************************************
Purpose	: checks for special chars in the input
Input	: a String
Output	: Boolean values - true if present, false if not present
*****************************************************************/

function splCharExists(temp)
{
	//var arr=new Array("!","@","#","%","^","'","&","*","(",")","_","+","-","|","\","{","}","[","]","`","~","<",">","?",",",".");
	var arr=new Array("!","@","#","%","^","'","&","*","(",")","_","+","-","|","\",","{","}","[","]")
	for(i=0;i<temp.length;i++)
		for(j=0;j<arr.length;j++)
			if (temp.charAt(i)==arr[j])
				return true;
	return false;			
}

/*To check if it is a number
*****************************************************************
Purpose	: checks if input is a number or not
Input	: a String
Output	: Boolean values - true if not a number, false if it is
*****************************************************************/
function isNotANumber(temp)
{
	if (isNaN(temp))
		return true;
	else
		return false;	
}

/*To clear all fields in a form and set focus to the top of the form
*****************************************************************
Purpose	: To clear all form fields
Input	: a String with which the fields have to be initialised after clearing
Output	: None, will Set the focus to the first field in the form
*****************************************************************/
function clearAll(temp)
{
	for(i=0;i<form1.length;i++)
	{
		if (document.form1.elements[i].type=="text")
		{
			document.form1.elements[i].value="";
			document.form1.elements[i].value=temp;
		}
		if(document.form1.elements[i].type=="checkbox")
		{
			document.form1.elements[i].checked=false;
		}
	}
	document.form1.elements[0].focus();		
}

/*To check if input is a valid email address (MODIFIED by Elaya Manickam Narayanan, on October 12 '2K)
****************************************************************************************
Purpose	: To check for email
Input	: a String to be parsed for email check
Output	: boolean true, if valid email , false if not valid email
*****************************************************************/
function validEmail(temp)
{
emailLength=temp.length
atTemp=temp.indexOf("@")
dotTemp=temp.indexOf(".")
//check for space inside email address
	if(temp.indexOf(" ")!=-1)
		return false;
//check for existense of @ and .
	if(atTemp==-1)
		return false;
	if(dotTemp==-1)
		return false;
//check if @ and . are in the first position		
	if(atTemp==0)
		return false;
	if(dotTemp==0)
		return false;
//check if @ and . are in the last position
	if(temp.lastIndexOf(".")==emailLength-1)
		return false;
	if(temp.lastIndexOf("@")==emailLength-1)
		return false;
//check if there is only one @		
	if(temp.lastIndexOf("@")!=atTemp)				
		return false;
//check for dot just before "@"
	if(temp.charAt(atTemp-1)==".")
		return false;
//check for dot just after "@"		
	if(temp.charAt(atTemp+1)==".")
		return false;
//check for atleast one dot after "@"
	if(temp.indexOf(".",atTemp)==-1)
		return false;		
/* Validation newly added by Elaya Manickam Narayanan, on November 22 '2K
*****************************************************************/
//	var len;
//	len=temp.length-temp.lastIndexOf(".")
//	len--;
//	if(len > 3)		
//		return false;
//	if(len < 2)
//		return false;		
//	if(!isNaN(temp))
//		return false;
//if everything is okay then email is valid , so
	return true;	
}

/*To check if input is a valid URL (Added Anew by Elaya Manickam Narayanan, on November 22 '2K)
****************************************************************************************
Purpose	: To check for URL validity
Input	: a String to be parsed for URL check
Output	: boolean true, if valid URL, false if not valid URL
*****************************************************************/
function validURL(temp)
{
//check if there are 5 characters after the last DOT (in case its something like  someurl.shtml)
	//Atleast one dot should exist in URL
	if(temp.lastIndexOf(".")==-1)
		return false;
	//calculate no. of characters after last dot
	var len;
	len=temp.length-temp.lastIndexOf(".")
	len--;
	if(len > 5)		
		return false;
	if(len < 2)
		return false;
	if(!isNaN(temp))
		return false;
//if everything is okay then email is valid , so
	return true;		
}
