// *************************************************************************
// * xTrim - Strip space (ASCII 32) from the beginning and end of a string *
// *************************************************************************

function xTrim( str )
{
    str = str.replace( /^[ ]*/g, "" );
    str = str.replace( /[ ]*$/g, "" );

	return str;
}
// ************************************************************************
// * TEXT *****************************************************************
// ************************************************************************

// *
// * checkBlanks( cField1, sErrMsg )
// * cField1 - name of the field to be tested.
// * sErrMsg - alert error message if field value contents blank(s).
// * RETURN VALUE : true  - if field contains blank(s)
// * 		    flase - if field !contains blank(s)
// *
function checkBlanks( cField1, sErrMsg )
{
        if( cField1.value.indexOf( ' ', 0 ) != -1 || cField1.value == '')
        {
                alert( sErrMsg );

                cField1.focus();
                cField1.select();

                return true;
        }

        return false;
}

// *
// * checkRange( cField1, iMinRange, sErrMsg )
// * cField1   - name of the field to be tested. 
// * iMinRange - field value length should be >= minimum range.
// * sErrMsg   - alert error message if field value length is < minimum range.
// *
function checkRange( cField1, iMinRange, sErrMsg )
{
 		var iStr = 'Invalid Operation !\n===============\n';

        if( cField1.value.length < iMinRange )
        {      
                alert( iStr +  sErrMsg );

                cField1.focus();
                cField1.select();

                return true;
        }

        return false;
}

// *  *
// *  *
// *  *
function checkContents( cField1, cFName, bNum, bLower, bUpper )
{
        var i = 0, j = 0;
        var sStr = cField1.value;
        var iStrLen = sStr.length;
		sCopy = new Array(iStrLen);
 		var iStr = 'Invalid Operation !\n===============\n';

	for(i = 0; i < iStrLen; i++)
		sCopy[i] = sStr.charAt(i);

        if( bNum )      // cField1 should not contain 0-9
        {
                for(i=0; i<iStrLen; i++)
                {
                        if( sStr.charAt(i) >= '0' && sStr.charAt(i) <= '9' )
                        {
                                alert( iStr + cFName + " should not contain digits (0-9) !" );

				cField1.focus();
				cField1.select();

                                return true;    // cField1 contains 0-9
                        }
                }
        }

        if( bLower )      // cField1 should not contain a-z
        {
                for(i=0; i<iStrLen; i++)
                {
                        if( sStr.charAt(i) >= 'a' && sStr.charAt(i) <= 'z' )
                        {
                                alert( iStr + cFName + " should not contain lower case alphabets (a-z) !" );

                                cField1.focus();
                                cField1.select();

                                return true;    // cField1 contains a-z
                        }
                }
        }

        if( bUpper )      // cField1 should not contain A-Z
        {
                for(i=0; i<iStrLen; i++)
                {
                        if( sStr.charAt(i) >= 'A' && sStr.charAt(i) <= 'Z' )
                        {
                                alert( iStr + cFName + " should not contain upper case alphabets (A-Z) !" );

                                cField1.focus();
                                cField1.select();

                                return true;    // cField1 contains A-Z
                        }
                }
        }

	return(false); //added by rs 
}

// *
// *
// *
function checkSymbol( cField1, sStr, sErrMsg )
{
	var i, j;
 	var iStr = 'Invalid Operation !\n===============\n';

        for(i = 0; i < cField1.value.length; i++)
        {
        	for(j = 0; j < sStr.length; j++)
                {
                	if( cField1.value.charAt(i) == sStr.charAt(j) )
                        {
                        	alert( iStr + sErrMsg + "'" +  cField1.value.charAt(i) + "'" );

                                cField1.focus();
                                cField1.select();

                                return true;
                        }
                 }
        }

	return false;
}

// ************************************************************************
// * SELECT ***************************************************************
// ************************************************************************

// *
// *
// *
function validIndex( cField1, sErrMsg )
{
 	var iStr = 'Invalid Operation !\n===============\n';

        if( cField1.selectedIndex == 0 )
        {
                alert( iStr +  sErrMsg );

                cField1.focus();

                return true;
        }

        return false;
}

function validIndex_n( cField1, sErrMsg )
{
 	var iStr = 'Requisite Operation ! \n************************\n';

        if( cField1.selectedIndex == 0 )
        {
                alert( iStr +  sErrMsg );

                cField1.focus();

                return true;
        }

        return false;
}

function xcheckEmail(cField,cTitle) {
	var j=0,i,ch='@';
	var emailfield;
	// Collect the value form the field
	emailfield = cField;
	if(/[!$%^&*()\" \"|.+@~#=\\`;:"'\]\],<>}{?\/]/.test(emailfield.charAt(emailfield.length-1)) ) {
		alert(iStr + 'Do NOT use illegal character(s) in ' + cTitle+ ' !');
		return(true);
	}
	// Scan the Email address char by char
	for(i=0; i<=emailfield.length; i++) {
		if(emailfield.charAt(i)=='@' &&  /[!#$%^&*().|@+~=\\`;:"'\]\],<>}{?\/]/.test(emailfield.charAt(i+1)) ) {
			alert(iStr + cTitle +' contains illegal character after \'@\' !');
			return(true);
		}
		if(emailfield.charAt(i)=='.' &&  /[!#$%^&*()|.+@~=\\`;:"'\]\],<>}{?\/]/.test(emailfield.charAt(i+1)) ) {
			alert(iStr + cTitle +' contains illegal character after \'.\' !');
			return(true);
		}
		if( /[!$%^_&*()|.+@~#=\\`;:"'\]\],<>}{?\/-]/.test(emailfield.charAt(0)) ) {
			alert(iStr + cTitle +' contains illegal character at beginning !');
			return(true);
		}
		if (ch==emailfield.charAt(i)) j++;
			//  if (emailfield.charAt(emailfield.length - 1)=='.') j++;
	}//End For Loop
	// If found two or more '@' or ends with '.'
	if(j>1){
		alert(iStr + " "+cTitle+" is NOT valid ! Enter correctly."  );
		return(true);
	}
	// Check for specials characters
	// Note: (Special char \char are  )
	if(/[!#$%^&*()|\' \'+~=\\`;:"'\]\],<>}{?\/]/.test(emailfield)) {
		alert(iStr + 'Do NOT use illegal character(s) in  ' + cTitle+ ' !' );
		return(true);
	}
	// Check for Email Pattern
	// Create a pattern and a regular expression and test it
	//Old var s2="\\w{1,}@\\w{1,}\\.\\w{1,}|\\w{1,}@\\w{1,}-\\w{1,}\\.\\w{1,}";
	//Created new RegExp for - and _ chars
	var s2="\\w{1,}@\\w{1,}\\.\\w{1,}|\\w{1,}@\\w{1,}-\\w{1,}\\.\\w{1,}|\\w{1,}\-{1,}\@\\w{1,}\\.\\w{1,}";
	emailre = new RegExp(s2);
	emailok=emailre.test(emailfield);
	// Check Email address for Blank
	if (emailfield!="") {
		// Check for the Email pattern
		if(!emailok) {
			alert(iStr + " Please enter "+cTitle+" in correct format.");
			return(true);
		}
	}
	else {
		alert(iStr + " "+cTitle+" can not be blank ! ( , Comma at end )\nEnter correctly.");
		return(true);
	}
}// End of function

function checkEmail(cField,cTitle) {
	var j=0,i,ch='@';
	var emailfield;
	// Collect the value form the field
	emailfield=cField.value;
	if(xcheckEmail(emailfield,cTitle)) {
		cField.focus();
		cField.select();
		return(true);	
	}
}// End of function
