



//-------------------------------------------------------------------
// getElementIndex(input_object)
//   Pass an input object, returns index in form.elements[] for the object
//   Returns -1 if error
//-------------------------------------------------------------------
function getElementIndex(obj) 
	{
	var theform = obj.form;
	for (var i=0; i<theform.elements.length; i++) 
		{
		if (obj.name == theform.elements[i].name) 
			{return i;}
		}
		
	return -1;
	}

// -------------------------------------------------------------------
// tabNext(input_object)
//   Pass an form input object. Will focus() the next field in the form
//   after the passed element.
//   a) Will not focus to hidden or disabled fields
//   b) If end of form is reached, it will loop to beginning
//   c) If it loops through and reaches the original field again without
//      finding a valid field to focus, it stops
// -------------------------------------------------------------------
function tabNext(obj) 
	{
	if (navigator.platform.toUpperCase().indexOf("SUNOS") != -1) 
		{obj.blur(); return; }

	var theform = obj.form;
	var i = getElementIndex(obj);
	var j=i+1;

	if (j >= theform.elements.length) 
		{ j=0; }
	if (i == -1) 
		{ return; }
		
	while (j != i) 
		{
		if ((theform.elements[j].type!="hidden") && 
		    (theform.elements[j].name != theform.elements[i].name) && 
			(!theform.elements[j].disabled)) 
			{
			theform.elements[j].focus();
			break;
			}
		
		j++;
		
		if (j >= theform.elements.length) 
			{ j=0; }
		}
	}

function jump(obj)
	{
	if(obj.value.length == obj.maxLength)
		{ tabNext(obj); }
	}
//======= Auto Tab Ends =========================================================================
//-------------------------------------------------------------------------------------------------
//---> Alltrim an Expression
//-------------------------------------------------------------------------------------------------

// Function trim : deletes the leading and ending blank spaces
function trim(strComp)
	{
	ltrim = /^\s+/
	rtrim = /\s+$/
	strComp = strComp.replace(ltrim,'');
	strComp = strComp.replace(rtrim,'');
	return strComp;
	}

//-------------------------------------------------------------------------------------------------
//---> Required Fields
//-------------------------------------------------------------------------------------------------

// Function to require fields
function isRequired(formObject, fieldDescription) 
	{

	var tempFormValue ;
	var strError ="";	
	var iFocus =-1;

	for (var i =0; i< isRequired.arguments.length;i=i+2)
	{
	if(typeof(isRequired.arguments[i]) == 'undefined')
		{
		//alert('IsRequired-Error: Parameter no.'+(i+1)+' is not an object.')
		return false;
		}
		
	tempFormValue =trim(isRequired.arguments[i].value);
	
	if (tempFormValue.length < 15)
		{deleteLoop = tempFormValue.length}
	else
		{deleteLoop = 15}
		for (var j = 0; j < deleteLoop; j++) 
			{tempFormValue = tempFormValue.replace(/ / , "");
		}

	if (tempFormValue.length == 0)
		{	
		strError = strError+ isRequired.arguments[i+1] + "\n"
		if (iFocus ==-1)
			iFocus = i;
		}
	}

	if (strError.length != 0){	
		alert( 'Following fields are required.\n\n' + strError)
		
		isRequired.arguments[iFocus].focus()
		return(false)
	}
	else
		return(true)
	}


//-------------------------------------------------------------------------------------------------
//---> Validate SElection boxes
//-------------------------------------------------------------------------------------------------

// validate the select 
function chkSel(obj,strMsg)
{
	var iFocus =-1;
	var strError="";

	for(var i=0;i<chkSel.arguments.length;i=i+2)
	{
			if(chkSel.arguments[i].value=="" || chkSel.arguments[i].value=="-1")
			{
				
				strError = strError+chkSel.arguments[i+1] + "\n"
					if (iFocus ==-1)
							iFocus = i;					

			}
	}
	if (strError.length != 0)
	{	
		alert( 'Following fields are required.\n\n' + strError);
		chkSel.arguments[iFocus].focus()
		return false;

	}
	else
		return true;

}

//-------------------------------------------------------------------------------------------------
//---> Validate Email
//-------------------------------------------------------------------------------------------------

// Function isEmail: Validates if the value of 'IsItReal' is a valid email address
function isEmail(IsItReal)
	{

	if(IsItReal.value!="")
		{
			if (typeof(IsItReal) != "undefined")
			{
			var valEmail = IsItReal.value;
			
			valEmail = valEmail.match(/(\w+)@(.+)\.(\w+)$/);
			
			if (valEmail !=null)
				{
				if ((valEmail[3].length==2) || (valEmail[3].length==3) || (valEmail[3].length==4))
					return true;
				}
			
			}
			alert('Invalid Email Address');
			IsItReal.focus();
			IsItReal.select();
			return false;
		}
	}

//-------------------------------------------------------------------------------------------------
//---> Validates if the field obeys the rule
//-------------------------------------------------------------------------------------------------
/**
** Function isRule: Validates if the value of oComp object follows the rule specified through sRule
** oComp: Text box Object for which validation needs to be done
** sRule: Specifies the rule of validatin
** .....: Ex: if sRule is specified as 'ABCDEFGH...Zabcdef....xyz', any other character entered other
** .....: than those specified disobeys the rule. and false value is returned.
** sMessage: The message that has to be popped up. if no message is specified, 
** ........: the function will not popup any alert.
***/
function isRule(oComp, sRule, sMessage, nLength, fdecimal) 
	{
	if(fdecimal=="" || typeof('fdecimal')=='undefined')
		fdecimal=false;
		
	//If the object is not specified return false
	if (typeof(oComp) == 'undefined' || oComp == null || oComp == '')
		{
		alert('Error: Input object not specified.');
		return false;
		}
	//If neither rule nor max length is specified, return false
	else if (typeof(sRule) == 'undefined' && typeof(nLength) == 'undefined')
		{
		alert('Error: No rule/maximum lenght for input object specified.');
		return false;
		}
		
	//If object is specified and either of rule is specified,
	if(typeof(sRule) != 'undefined' && sRule != null)
		{
		var temp;
		sRule=sRule + "";

		if(typeof(oComp) == "undefined" || typeof(sRule) == "undefined")
			return false;
		
		for (var i=0; i<oComp.value.length; i++) 
			{
			temp = "" + oComp.value.substring(i, i+1);
		
			if (sRule.indexOf(temp) == "-1") 
				{
				if(typeof(sMessage) != "undefined")	
					alert(sMessage);
				else
					alert(oComp.name + " disobeys the rule")
					
				oComp.value=oComp.value.substring(0,i)
				return false;
				}
			}
		}
	if(fdecimal && oComp.value.indexOf(".")==-1)
		nLength = nLength -2;
		
	if(typeof(nLength)=='number' && oComp.value.length >= nLength)
		{
		if(typeof(nLength)=='number' && oComp.value.length >= nLength)
			{
			if(sMessage && (sRule==null || typeof(sRule)=='undefined'))
				alert(sMessage);
			else
				alert('Entry limited to '+nLength+' !');
				
			oComp.value=oComp.value.substring(0,nLength)
			}
		}
	
	return true;
	}
	
