//Fuctions to mimmick LTrim,  RTrim, and Trim...
//==================================================================
//LTrim(string) : Returns a copy of a string without leading spaces.
//==================================================================
function LTrim(str)
/*
        PURPOSE: Remove leading blanks from our string.
        IN: str - the string we want to LTrim
*/
{
        var whitespace = new String(" \t\n\r");

        var s = new String(str);

        if (whitespace.indexOf(s.charAt(0)) != -1) {
            // We have a string with leading blank(s)...

            var j=0, i = s.length;

            // Iterate from the far left of string until we
            // don't have any more whitespace...
            while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
                j++;


            // Get the substring from the first non-whitespace
            // character to the end of the string...
            s = s.substring(j, i);
        }

        return s;
}

//==================================================================
//RTrim(string) : Returns a copy of a string without trailing spaces.
//=================================================================//=
function RTrim(str)
/*
        PURPOSE: Remove trailing blanks from our string.
        IN: str - the string we want to RTrim

*/
{
        // We don't want to trip JUST spaces, but also tabs,
        // line feeds, etc.  Add anything else you want to
        // "trim" here in Whitespace
        var whitespace = new String(" \t\n\r");

        var s = new String(str);

        if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
            // We have a string with trailing blank(s)...

            var i = s.length - 1;       // Get length of string

            // Iterate from the far right of string until we
            // don't have any more whitespace...
            while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                i--;


            // Get the substring from the front of the string to
            // where the last non-whitespace character is...
            s = s.substring(0, i+1);
        }

        return s;
}

//=============================================================
//Trim(string) : Returns a copy of a string without leading or trailing spaces
//=============================================================
        function Trim(str)
        /*
                PURPOSE: Remove trailing and leading blanks from our string.
                IN: str - the string we want to Trim

                RETVAL: A Trimmed string!
        */
        {
                return RTrim(LTrim(str));
        }


function VerifyEmail(email) 
{
	invalidChars = " /:,;"
	if (email == "") {	// cannot be empty
	  //alert("Please enter your email address.");
	  return false
        }
	for (i=0; i<invalidChars.length; i++) 
	{	// does it contain any invalid characters?
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) 
		{
			return false
		}
  }
  atPos = email.indexOf("@",1)	// there must be one "@" symbol
  if (atPos == -1) 
  {
		return false
  }
  if (email.indexOf("@",atPos+1) != -1) 
  {	// and only one "@" symbol
		return false
  }
  periodPos = email.indexOf(".",atPos)
  if (periodPos == -1) 
  {			// and at least one "." after the "@"
		return false
  }
  if (periodPos+3 > email.length)	
  { // must be at least 2 characters after the "."
		return false
  }
  return true
}

//Check the object value is null if null return false else return true
function f_IsNullOrBlank(objID) {
    var currObj = document.getElementById(objID);
    if (txtObject != null) {
        var value = txtObject.value;
        if (value == "") {
            return false;
        }
    }
    else {
        return false;
    }
    return true;
}
function f_IsNullForObject(txtObject, message) {
    if (txtObject != null) {
        var value = txtObject.value;
        if (value == "") {
            alert(message);
            return false;
        }
    }
    else {
        return false;
    }
    return true;
}
//Check textBox value is blank
function f_IsNullForTextBox(objID, message) {
    var currObj = document.getElementById(objID);
    var result = f_IsNullForObject(currObj, message);
    if (!result) {
        currObj.focus();
        currObj.select();
        return false;
    }
    return true;
}
//Check the Number is Integer
function f_IsInteger(currValue, message) {
    var reg = /^[1-9]{1}[\d]*$/
    var result = reg.test(currValue);
    if (!isNaN(message) && !result) {
        alert(message);
        return false;
    }
    return result;
}
function f_IsIntForObject(txtObject, message) {
    if (txtObject != null) {
        return f_IsInteger(txtObject.value, message);
    }
    else {
        return false;
    }
    return true;
}
function f_IsIntForTextBox(objID, message) {
    var currObj = document.getElementById(objID);
    if (!f_IsIntForObject(currObj, message)) {
        currObj.focus();
        currObj.select();
        return false;
    }
    return true;
}
//Check the Number Is float
function f_IsFloat(currValue, Message) {
    var reg = /^[0-9]+(\.[0-9]+){0,1}$/
    var result = reg.test(currValue);
    if (isNaN(Message) && !result) {
        alert(Message);
        return false;
    }
    return result;
}
function f_IsFloatForObject(txtObject, message) {
    if (txtObject != null) {
        return f_IsFloat(txtObject.value, message);
    }
    else {
        return false;
    }
    return true;
}
function f_IsFloatForTextBox(objID, message) {
    var currObj = document.getElementById(objID);
    if (!f_IsFloatForObject(currObj, message)) {
        currObj.focus();
        currObj.select();
        return false;
    }
    return true;
}
//Check DropDownList Is select
function f_IsSelectForDDL(ddlObject, message, defalueValue) {
    var strValue = "-1";
    if (defalueValue != undefined) {
        strValue = defalueValue;
    }
    var selectIndex = ddlObject.selectedIndex;
    var value = ddlObject.options[selectIndex].value;
    if (value == strValue) {
        ddlObject.focus();
        if (message != '') {
            alert(message);
        }
        return false;
    }
    return true;
}
function f_IsSelect(ddlObject, defalueValue) {
    var strValue = "-1";
    if (defalueValue != undefined) {
        strValue = defalueValue;
    }
    var selectIndex = ddlObject.selectedIndex;
    var value = ddlObject.options[selectIndex].value;
    if (value == strValue) {
        return false;
    }
    return true;
}
function f_IsSelectForID(ddlObject, defalueValue) {
    var obj = document.getElementById(ddlObject);
    if (obj != null) {
        return (f_IsSelect(obj, defalueValue));
    }
    else {
        return false;
    }
    return true;
}
function f_IsSelectForDDLID(objID, message, defalueValue) {
    var obj = document.getElementById(objID);
    if (obj != null) {
        return (f_IsSelectForDDL(obj, message, defalueValue));
    }
    else {
        return false;
    }
    return true;
}
//Check GridView DrowDownList is select Value
function f_IsSelectForGridViewDDL(currGridView, textBoxList, MessageList, defaultValue) {
    if (currGridView == null) {
        return false;
    }
    var oInputs = currGridView.getElementsByTagName("select");
    if (textBoxList.length == MessageList.length) {
        var Result = true;
        for (var i = 0; i < oInputs.length; i++) {
            var currSelect = oInputs[i];
            for (var j = 0; j < textBoxList.length; j++) {
                if (Result) {
                    if (currSelect.id.indexOf("_" + textBoxList[j]) > -1) {
                        Result = f_IsSelectForDDL(currSelect, MessageList[j], defaultValue);
                    }
                }
            }
            if (!Result) {
                return Result;
            }
        }
    }
    else {
        return false;
    }
    return true;
}
function f_IsSelectForGridViewDDLByID(gridID, textBoxList, MessageList, defaultValue) {
    var currGridView = document.getElementById(gridID);
    return f_IsSelectForGridViewDDL(currGridView, textBoxList, MessageList, defaultValue);
}
//Email Validation 
function f_IsEmailValidate(currValue) {
    var re = /^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    return re.test(currValue);
}
function f_IsEmailValidatebyID(objID, Message) {
    var result = false;
    var currObj = document.getElementById(objID);
    if (currObj != null) {
        var objValue = currObj.value;
        result = f_IsEmailValidate(objValue);
        if (!result && (Message != undefined || Message != "")) {
            currObj.focus();
            currObj.select();
            alert(Message);
        }
        return result;
    }
    return result;
}

//Is American Phone
function f_IsUSPhone(currValue) {
    var re = /^((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/;
    return re.test(currValue);
}
function f_IsUSPhonebyID(objID, Message) {
    var result = false;
    var currObj = document.getElementById(objID);
    if (currObj != null) {
        var objValue = currObj.value;
        result = f_IsUSPhone(objValue);
        if (!result && (Message != undefined || Message != "")) {
            currObj.focus();
            currObj.select();
            alert(Message);
        }
        return result;
    }
    return result;
}
//Zip Code Validation
function f_IsUSZipCode(currValue) {
    var re = /^\d{5}(-\d{4})?/;
    return re.test(currValue);
}
function f_IsUSZipCodebyID(objID, Message) {
    var result = false;
    var currObj = document.getElementById(objID);
    if (currObj != null) {
        var objValue = currObj.value;
        result = f_IsUSZipCode(objValue);
        if (!result && (Message != undefined || Message != "")) {
            currObj.focus();
            currObj.select();
            alert(Message);
        }
        return result;
    }
    return result;
}

