function IsInvalidEmailAdress(textObject, isMandatory, message)
{
    if ((isMandatory == true) ||
        (isMandatory == false && textObject.value.length > 0)) {
        if (CheckEmailAdress(textObject.value) == false)
        {
            alert (message);
            textObject.select();
            textObject.focus();
            return true;
        }
    }
    return false;
}

function IsInvalidNumber(textObject, minval, maxval, message)
{
    // Check if the string is empty
    if (IsEmptyString(textObject.value))
    {
        alert (message);
        textObject.focus();
        textObject.select();
        return true;
    }

    // if it is not a number
    if (isNaN(textObject.value))
    {
        alert (message);
        textObject.focus();
        textObject.select();
        return true;
    }

    var val = parseFloat(textObject.value);
    if (val < minval || val > maxval)
    {
        alert (message);
        textObject.focus();
        textObject.select();
        return true;
    }

    return false;
}

function CheckEmailAdress(email)
{
    var atrate="@";
    var dot=".";
    var lessthan="<";
    var greaterthan=">";

    var pos_lessthan = email.indexOf(lessthan);
    var pos_greaterthan = email.indexOf(greaterthan);
    if (pos_lessthan != -1 && pos_greaterthan != -1) {
        email = email.substring(pos_lessthan + 1, pos_greaterthan);
    }

    var pos_atrate = email.indexOf(atrate);
    var str_length = email.length;
    var pos_dot = email.indexOf(dot);


    if (email.indexOf(atrate) == -1) {
        return false;
    }

    if (email.indexOf(atrate) == -1 || email.indexOf(atrate) == 0 || email.indexOf(atrate) == str_length) {
        return false;
    }

    if (email.indexOf(dot) == -1 || email.indexOf(dot) == 0 || email.indexOf(dot) == str_length) {
        return false;
    }

    if (email.indexOf(atrate,(pos_atrate + 1)) != -1) {
        return false;
    }

    if (email.substring(pos_atrate - 1,pos_atrate) == dot || email.substring(pos_atrate + 1,pos_atrate + 2) == dot) {
        return false;
    }

    if (email.indexOf(dot,(pos_atrate + 2)) == -1) {
        return false;
    }

    if (email.indexOf(" ") != -1) {
        return false;
    }

    return true;
}

function IsInvalidInput(textObject, message)
{
    // Check if the string is empty
    if (IsEmptyString(textObject.value))
    {
        alert (message);
        textObject.focus();
        textObject.select();
        return true;
    }

    return false;
}

function IsEmptyString(string_in)
{
    // Trim the string to remove space character
    string_in = TrimAll(string_in);

    if ((string_in.length == 0) || (string_in == null)) {
        return true;
    }

    return false;
}

function TrimAll(string_in)
{
    // Check front of the string
    while (string_in.substring(0,1) == " ") {
        string_in = string_in.substring(1, string_in.length);
    }

    // Check end of the string
    while (string_in.substring(string_in.length-1, string_in.length) == " ") {
        string_in = string_in.substring(0, string_in.length-1);
    }

    return string_in;
}

// Combo box releted functions
/******************************************************************************
** Name			: cmbAddOption()
** Descriptor	: Adds an selected option to the give list.
******************************************************************************/
function cmbAddOption(theCmb, theText, theValue)
{
    var itemFound = 0;

    for (var i = 0; i < theCmb.length; i++) {
        if (theCmb.options[i].value == theValue) {
            itemFound = 1;
            theCmb.options[i].selected = true;
        }
    }

    if (!itemFound) {
        theCmb.options[theCmb.length] = new Option(theText, theValue);
    }
}

/******************************************************************************
** Name			: cmbDeleteOption()
** Descriptor	: Deletes an given option from combo box.
******************************************************************************/
function cmbDeleteOption(theCmb, theIndex)
{
    var theCmbLength = theCmb.length;

    if (theCmbLength > 0) {
        theCmb.options[theIndex] = null;
    }
}

/******************************************************************************
** Name			: cmbDeleteAllOptions()
** Descriptor	: Deletes all the option from combo box.
******************************************************************************/
function cmbDeleteAllOptions(theCmb)
{
    var theCmbLength = theCmb.length;

    // Find the selected item
    for(var i = theCmbLength - 1; i >= 0; i--) {
        // Delete the selected text/values at last.
        cmbDeleteOption(theCmb, i);
    }
}
