function checkEmptyFieldEntry(frmCtlName, strMsg, intTextLength)
{
	var ctlObj = window.document.getElementsByName(frmCtlName)[0];
	ctlObj.value = Trim(ctlObj.value);
	if (ctlObj.value == "")
	{
		alert("Please enter a valid entry for [" + strMsg + "] !");
		ctlObj.focus();
		return false;
	}
	if (ctlObj.value.length < intTextLength)
	{
		alert("The entry for " + strMsg + " should be atleast [" + intTextLength + "] characters long !");
		ctlObj.focus();
		return false;
	}
	return true;
}


function Trim(STRING){
STRING = LTrim(STRING);
return RTrim(STRING);
}

function RTrim(STRING){
while(STRING.charAt((STRING.length -1))==" "){
STRING = STRING.substring(0,STRING.length-1);
}

return STRING;
}


function LTrim(STRING){
while(STRING.charAt(0)==" "){
STRING = STRING.replace(STRING.charAt(0),"");
}
return STRING;
}


function checkDecimals(fieldName, decallowed)
{
	//decallowed = 2;  // how many decimals are allowed?
	if (fieldName.value == "") fieldName.value=0
	if (isNaN(fieldName.value) || fieldName.value == "") 
	{
		alert("Oops!  That does not appear to be a valid number.  Please try again.");
		fieldName.select();
		fieldName.focus();
		return false;
	}
	else 
	{
		//if (fieldName.value.indexOf('.') == -1) fieldName.value += ".";
		var strTemp = fieldName.value;
		if (strTemp.indexOf('.') == -1) strTemp += ".";
		dectext = strTemp.substring(strTemp.indexOf('.')+1, strTemp.length);
		
		if ((dectext.length > decallowed) & (dectext.length > 0) )
		{
			alert ("Oops!  Please enter a number with up to " + decallowed + " decimal places.  Please try again.");
			fieldName.select();
			fieldName.focus();
			return false;
		}
		else 
		{		// Added on 2007-09-08
				var strTemp = fieldName.value;
				var strAmt = 0;
				strAmt =  strTemp.split(".");
				if(parseInt(strAmt) > 9999999)
				{
					alert('Amount can not be greater than  9999999');
					fieldName.select();
					fieldName.focus();
					return false;
				}	
				else
					return true;
		}
 }
}


function checkFieldLength(txtBox, lngAllowed)
{
	var strValue = txtBox.value;
	if (strValue.length > lngAllowed)
	{
		alert("The maximum length of this field could be "+ lngAllowed +" characters only.\nThe length right now is " + strValue.length +""); 
		txtBox.focus(); 
		return false;
	}
	else
		return true;
	
}


function InStr(strSearch, charSearchFor)
/*
InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                           was found in the string str.  (If the character is not
                           found, -1 is returned.)
                           
Requires use of:
	Mid function
	Len function
*/
{
	for (i=0; i < Len(strSearch); i++)
	{
	    if (charSearchFor == Mid(strSearch, i, 1))
	    {
			return i;
	    }
	}
	return -1;
}

function Mid(str, start, len)
        /***
                IN: str - the string we are LEFTing
                    start - our string's starting position (0 based!!)
                    len - how many characters from start we want to get

                RETVAL: The substring from start to start+len
        ***/
        {
                // Make sure start and len are within proper bounds
                if (start < 0 || len < 0) return "";

                var iEnd, iLen = String(str).length;
                if (start + len > iLen)
                        iEnd = iLen;
                else
                        iEnd = start + len;

                return String(str).substring(start,iEnd);
        }


// Keep in mind that strings in JavaScript are zero-based, so if you ask
// for Mid("Hello",1,1), you will get "e", not "H".  To get "H", you would
// simply type in Mid("Hello",0,1)

// You can alter the above function so that the string is one-based.  Just
// check to make sure start is not <= 0, alter the iEnd = start + len to
// iEnd = (start - 1) + len, and in your final return statement, just
// return ...substring(start-1,iEnd)

function Len(str)
        /***
                IN: str - the string whose length we are interested in

                RETVAL: The number of characters in the string
        ***/
        {  return String(str).length;  }



 function Left(str, n)
        /***
                IN: str - the string we are LEFTing
                    n - the number of characters we want to return

                RETVAL: n characters from the left side of the string
        ***/
        {
                if (n <= 0)     // Invalid bound, return blank string
                        return "";
                else if (n > String(str).length)   // Invalid bound, return
                        return str;                // entire string
                else // Valid bound, return appropriate substring
                        return String(str).substring(0,n);
        }



function Right(str, n)
        /***
                IN: str - the string we are RIGHTing
                    n - the number of characters we want to return

                RETVAL: n characters from the right side of the string
        ***/
        {
                if (n <= 0)     // Invalid bound, return blank string
                   return "";
                else if (n > String(str).length)   // Invalid bound, return
                   return str;                     // entire string
                else { // Valid bound, return appropriate substring
                   var iLen = String(str).length;
                   return String(str).substring(iLen, iLen - n);
                }
        }	


	//function to validate the Email ID
	function IsValidEmail(txtBox) 
	{
		var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
		var regex = new RegExp(emailReg);
		if (txtBox.value!="") 
		{
			if (regex.test(txtBox.value)==false)
			{
				alert ("Please enter a valid email address !!");
				txtBox.focus(); txtBox.select(); return false;
			}
			if (validateEmail(txtBox.value)==false)
			{
				alert ("Please enter a valid email address !!");
				txtBox.focus(); txtBox.select(); return false;
			}
		}
	}
	
	function validateEmail(addr,man,db) 
	{
		if (addr == '' && man) {
		   if (db) alert('email address is mandatory');
		   return false;
		}
		var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
		for (i=0; i<invalidChars.length; i++) {
		   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		      if (db) alert('email address contains invalid characters');
		      return false;
		   }
		}
		for (i=0; i<addr.length; i++) {
		   if (addr.charCodeAt(i)>127) {
		      if (db) alert("email address contains non ascii characters.");
		      return false;
		   }
		}

		var atPos = addr.indexOf('@',0);
		if (atPos == -1) {
		   if (db) alert('email address must contain an @');
		   return false;
		}
		if (atPos == 0) {
		   if (db) alert('email address must not start with @');
		   return false;
		}
		if (addr.indexOf('@', atPos + 1) > - 1) {
		   if (db) alert('email address must contain only one @');
		   return false;
		}
		if (addr.indexOf('.', atPos) == -1) {
		   if (db) alert('email address must contain a period in the domain name');
		   return false;
		}
		if (addr.indexOf('@.',0) != -1) {
		   if (db) alert('period must not immediately follow @ in email address');
		   return false;
		}
		if (addr.indexOf('.@',0) != -1){
		   if (db) alert('period must not immediately precede @ in email address');
		   return false;
		}
		if (addr.indexOf('..',0) != -1) {
		   if (db) alert('two periods must not be adjacent in email address');
		   return false;
		}
		var suffix = addr.substring(addr.lastIndexOf('.')+1);
		if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
		   if (db) alert('invalid primary domain in email address');
		   return false;
		}
		return true;
	}

// Added on 2007-09-28	
	function avoidZero(txtPhone)
	{
	// if 0 is the first number of GSM Phone Number is not allowed
		var strPhone = txtPhone.value;
		var strNum   = strPhone.charAt(0);
		if (strNum=="0")
		{
			alert ("Please enter do not enter 0 is first number of GSM Phone Number is not allowed");
			txtPhone.focus(); txtPhone.select(); return false;
		}

	}
	
	function validateGSMPhoneNumber(txtPhone)
	{

		var validPhone = "^\\d{3}-\\d{3}-\\d{4}$";
		var regex = new RegExp(validPhone);
		if (regex.test(txtPhone.value)==false)
		{
			alert ("Please enter a valid GSM phone number !!\nThe number should be in the format \n XXX-XXX-XXXX ");
			txtPhone.focus(); txtPhone.select(); return false;
		}
	}

	function validatePhoneNumber(txtPhone)
	{
		var validPhone = "^\\d{1,3}-\\d{2,4}-\\d{3,4}-\\d{3,4}$";
		var regex = new RegExp(validPhone);
		if (regex.test(txtPhone.value)==false)
		{
			alert ("Please enter a valid phone number !!\nThe number should be in the format \n XXX-XXX-XXXX-XXXX or\n XX-XXX-XXX-XXXX or\n X-XXX-XXX-XXXX ");
			txtPhone.focus(); txtPhone.select(); return false;
		}
	}
	
	function validateCardNumber(txtCard)
	{
		var validCard = "^\\d{9}$";
		var regex = new RegExp(validCard);
		if (regex.test(txtCard.value)==false)
		{
			alert ("Please enter a valid card number !!\nThe number should be in the format \n 987654321 ! ");
			txtCard.focus(); txtCard.select(); return false;
		}
	}
