function checkNumericWithAlertOptional(oField, sFieldName, iLen)
{
	var count;
	var sLen;
	if (isEmptyStr(oField.value) == true ) return true

	if ((isNumeric(oField.value)) == false)
	{
		alertMsg(oField, "Please enter only numeric and character '-' for the field, " + sFieldName)
		return false;
	}
	count = isSpecialChar(oField.value);
	sLen = iLen + count;
	if (oField.value.length<sLen)
	{
		alertMsg(oField, "The minimum length for the field, " + sFieldName + " is " + iLen + " numbers." )
		return false;
	}
	return true;
}

function isNumeric(s) {

	for (var i=0; i < s.length; i++){
		//Check that current character is  number or "-"
		var c = s.charAt(i);
		if (!((c >= 0) && (c <= 9)))
		{
			if(c!="-")
				return false;
		}
	}
	return true;
}

function isSpecialChar(s) {
	var count;
	count = 0;
	for (var i=0; i < s.length; i++){
		//Check that current character is  number or "-"
		var c = s.charAt(i);
			if(c=="-")
				count = count + 1;
	}
	return count;
}

//------------------------------------------------------------------------------------
//Check That the input is a valid Email Address
//------------------------------------------------------------------------------------
function checkValidEmailWithAlertOptional(objField, txtFieldName)
{
var sztemp;

	sztemp = objField.value;
	sztemp = trim(sztemp);
	if (isEmptyStr(sztemp )==true) return true;
	if (!isValidEmail(sztemp))
	{
		alertMsg(objField, "Invalid email address for "+txtFieldName+".");
		return false;
	}

	return true;
}

//-------------------------------------------------------------------------------------
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the @ and . are both required
// * there must be only one @
// * there must be no spaces within the email
//-------------------------------------------------------------------------------------
function isValidEmail(s)
{
var pos;
var i = 0;
var j = 0;
var sLength = s.length;

	if (isEmptyStr(s))
		if (isValidEmail.arguments.length == 1) return false;
		else return(isValidEmail.arguments[1] == true);

  // check is whitespace within s
	if (isWhiteSpace(s)) return false;

  //check for the first charcter in the string is not @ or .
	if ((s.charAt(i) == "@") || (s.charAt(i) == ".")) return false;

  //look for position of @ from begin of string
	while ((i < sLength) && (s.charAt(i) != "@"))
	{
		i++;
	}
  // check if @ is the last character
	if (s.charAt(sLength-1)=="@") return false;

	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i ++;

  //look for position of . from begin of string
	while ((j < sLength) && (s.charAt(j) != "."))
	{
		j++;
	}
	if ((j >= sLength) || (s.charAt(j) != ".")) return false;
	else j ++;

	//--- check fullstop not straight before @
	//-- Added on 04 Oct 2000 by ywl
	//if (s.charAt(i-2) == ".") return false;

	//--- check fullstop not straight after @
	if (s.charAt(i) == ".") return false;

  //check any other @ in the string
	pos = i;
	while ((pos < sLength) && (s.charAt(pos) != "@"))
	{
		pos++;
	}
	if ((pos < sLength) || (s.charAt(pos) == "@")) return false;

	pos = i;
  // check that there is a dot after the
  // the @. Not dot reject
	var domaintemp
	domaintemp = s.substring(i,sLength)
	if (domaintemp.indexOf(".") == -1) return false;

  //check the last character is not .
	if (s.charAt(sLength-1) == ".") return false;

  //check there is no . after . for the first .
	if (s.charAt(j) == ".") return false;
	else j++;

  //check there is no . after . for the rest of the string
	while (j < sLength)
	{
	  	if ((s.charAt(j) == ".") && (s.charAt(j + 1) == ".")) return false;
		j++;
	}

	j = sLength - 1
  //look for position of last . in string
	while ((j != 0) && (s.charAt(j) != "."))
	{
		j--
	}

  //** Check in backend server side
  //check there is only alphanumeric characters after the last "."
	//s = s.substring(j+1 , s.length-1);
	//if (!isAlphanumeric(s)) return false;

	return true;
}

//----------------------------------------------------------------
//Return true if string s is empty or whitespace characters only.
//----------------------------------------------------------------
function isWhiteSpace(s)
{
	var i;
	var str;

		str = trim(s);

	//if s empty?
	if (isEmptyStr(str)) return true;

	//Search through string's characters one by one
	//until we find a whitespace character.
	//When we do, return true; if we don't, return false.

	if (str.indexOf(" ") == -1)			//-1 = If keyword (whitespace)/" " not in the string
		 return false
	else
		//Some character is whitespace.
		return true
}

function checkMandatory(selfield) {
var lSelected;
var sValue;
var strDisplayMsg

	lSelected = selfield.selectedIndex
	if (lSelected != -1)
		sValue =	selfield.value;

	if ((lSelected == -1) || (trim(sValue)==""))
	{
		return false;
	}
	return true ;
}

function checkMandatorySelectWithAlert( selfield, strError) {
var lSelected;
var sValue;
var strDisplayMsg

	lSelected = selfield.selectedIndex
	if (lSelected != -1)
		sValue =	selfield.options[lSelected].value;

	if ((lSelected == -1) || (trim(sValue)==""))
	{
		strDisplayMsg = "Please fill in the " + strError + " field."
		alert(strDisplayMsg) ;
		selfield.focus();
		return false;
	}
	return true ;
}

function checkMandatoryWithAlert( textfield, strError) {
var strTemp;
var strDisplayMsg

	strTemp=textfield.value;
	strTemp = trim(strTemp)
	if (isEmptyStr(strTemp) ) {
		strDisplayMsg = "Please fill in the " + strError + "."
		alertMsg( textfield, strDisplayMsg) ;
		return false;
	}
	return true ;
}

function alertMsg (theField, s) {
	alert(s);
	theField.focus();
	theField.select();
	return false
}

// checkMandatoryTextArea
function checkMandatoryTextArea(oField, sFieldName, lMaxLength)
{
var sValue = trim(oField.value);
	if (isEmptyStr(sValue))
	{
		alertMsg(oField, "Please enter value for " + sFieldName + ".")
		return false;
	}

	if (sValue.length > lMaxLength)
	{
		alertMsg(oField, "Maximum length allowed is " + lMaxLength + ". Please modify the value for " + sFieldName + ".")
		return false;
	}
	return true;
}

//--------------------------------------------------------------
//To Trim blank spaces from a string
//--------------------------------------------------------------
function trim(string) {

    var i;
    var intCount;


    //truncate the left spaces.
    // 1. get the number of spaces at left side of the string.
    // 2. get the new string without the left spaces.

    intCount = 0;
    for (i = 0; i < string.length; i++)
        if ((string.charAt(i)) != " ")
            break;
        else
            intCount = intCount + 1;

    string = string.substring(intCount, string.length);

    //truncate the right spaces.
    // 1. use the string that has been truncated just now and get
    //    the number of spaces on the right side of the string.
    // 2. get the final string and return.

    intCount = 0;
    for (i = string.length-1; i >= 0; i--)
        if ((string.charAt(i)) != " ")
            break;
        else
            intCount = intCount + 1;
    string = string.substring(0, string.length-intCount);
    return string;
}

//--------------------------------------------------------------
// Check whether string s is empty.
//--------------------------------------------------------------
function isEmptyStr(s)
{   return ((s == null) || (s.length == 0))
}
