﻿

//New added at 30-12-2009
function IsNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}




function getKeyCode(e) {
    if (window.event)
        return window.event.keyCode;
    else if (e)
        return e.which;
    else
        return null;
}


function keyRestrict(e, validchars) {
    var key = '', keychar = '';
    key = getKeyCode(e);
    if (key == null) return true;
    keychar = String.fromCharCode(key);
    keychar = keychar.toLowerCase();
    validchars = validchars.toLowerCase();
    if (validchars.indexOf(keychar) != -1)
        return true;
    if (key == null || key == 0 || key == 8 || key == 9 || key == 13 || key == 27)
        return true;
    return false;
}


function VisibleTxtByCbo(sCboName, sTxtName, iSelectedIndexShowTxt) {
    var cbo = document.getElementById(sCboName);
    var txt = document.getElementById(sTxtName);

    if (cbo.selectedIndex == iSelectedIndexShowTxt)
        txt.style.display = "inline";
    else
        txt.style.display = "none";

}

function VisibleOtherByCbo(sCboName, sTxtName, iSelectedIndexShowTxt) {
    var cbo = document.getElementById(sCboName);
    var txt = document.getElementById(sTxtName);

    if (cbo.selectedIndex == iSelectedIndexShowTxt)
        txt.style.visibility = "visible";
    else
        txt.style.visibility = "hidden";

}



//New functions added at 4-9-2009
function SwapImage(sImgID/*String*/, sImgFilePath/*String*/) {
    var img = document.getElementById(sImgID);
    img.src = sImgFilePath;
}

function MyAlert(sPromptMsg, sPromptMsg_GB, sPromptMsg_EN) {
    if (sGlobal_Lang == 'BIG5')
        alert(sPromptMsg);
    else if (sGlobal_Lang == 'GB')
        alert(sPromptMsg_GB);
    else
        alert(sPromptMsg_EN);
}

//To check whether the textbox has been inputted or not, if not then prompt a message
/*
function CheckTextBoxInput(sTxtFieldName, sPromptMsg) {
var txt = document.getElementById(sTxtFieldName);
if ((txt.style.display != 'none') && (trim(txt.value) == '')) {
alert(sPromptMsg);
txt.focus();
return false;
}
return true;
}
*/
function CheckTextBoxInput(sTxtFieldName, sPromptMsg, sPromptMsg_GB, sPromptMsg_EN) {
    var txt = document.getElementById(sTxtFieldName);
    if ((txt.style.display != 'none') && (trim(txt.value) == '')) {
        MyAlert(sPromptMsg, sPromptMsg_GB, sPromptMsg_EN);
        txt.focus();
        return false;
    }
    return true;
}


function CheckTextBoxesInput(sTxtFieldName, sTxtFieldName2, sPromptMsg, sPromptMsg_GB, sPromptMsg_EN) {
    var txt = document.getElementById(sTxtFieldName);
    var txt2 = document.getElementById(sTxtFieldName2);
    if ((trim(txt.value) == '') && (trim(txt2.value) == '')) {
        MyAlert(sPromptMsg, sPromptMsg_GB, sPromptMsg_EN);
        txt.focus();
        return false;
    }
    return true;
}



//New added at 30-12-2009
function CheckTextBoxIsNumeric(sTxtFieldName, sPromptMsg, sPromptMsg_GB, sPromptMsg_EN) {
    var txt = document.getElementById(sTxtFieldName);
    var sValue = trim(txt.value);
    if ((txt.style.display != 'none') && (sValue != '') && (!IsNumeric(sValue))) {
        MyAlert(sPromptMsg, sPromptMsg_GB, sPromptMsg_EN);
        txt.focus();
        return false;
    }
    return true;
}



function CheckEmailFormat(sTxtFieldName, sPromptMsg, sPromptMsg_GB, sPromptMsg_EN) {
    var txt = document.getElementById(sTxtFieldName);
    if ((trim(txt.value).indexOf("@") == -1)) {
        MyAlert(sPromptMsg, sPromptMsg_GB, sPromptMsg_EN);
        txt.focus();
        return false;
    }
    return true;
}


/*
function CheckComboBoxInput(sTxtFieldName, sPromptMsg) {
var cbo = document.getElementById(sTxtFieldName);
if (cbo.selectedIndex == 0) {
alert(sPromptMsg);
cbo.focus();
return false;
}

return true;
}
*/

function CheckComboBoxInput(sTxtFieldName, sPromptMsg, sPromptMsg_GB, sPromptMsg_EN) {
    var cbo = document.getElementById(sTxtFieldName);
    if (cbo.selectedIndex == 0) {
        MyAlert(sPromptMsg, sPromptMsg_GB, sPromptMsg_EN);
        cbo.focus();
        return false;
    }

    return true;
}


/*
function CheckRadioInput(sTxtFieldName, sPromptMsg) {
var cbo = document.getElementById(sTxtFieldName);
if (!cbo.checked) {
alert(sPromptMsg);
cbo.focus();
return false;
}

return true;
}
*/
function CheckRadioInput(sTxtFieldName, sPromptMsg, sPromptMsg_GB, sPromptMsg_EN) {
    var cbo = document.getElementById(sTxtFieldName);
    if (!cbo.checked) {
        MyAlert(sPromptMsg, sPromptMsg_GB, sPromptMsg_EN);
        cbo.focus();
        return false;
    }

    return true;
}

function CheckRadioBoxesInput(sTxtFieldNames /*String[]*/, sPromptMsg, sPromptMsg_GB, sPromptMsg_EN) {

    for (var i = 0; i < sTxtFieldNames.length; i++) {
        var cbo = document.getElementById(sTxtFieldNames[i]);
        if (cbo.checked)
            return true;
    }

    MyAlert(sPromptMsg, sPromptMsg_GB, sPromptMsg_EN);
    return false;
}




//To check whether the date is valid or not
function CheckDate(iDay, iMonth, iYear) {
    iMonth = iMonth - 1;

    var sDateStr = iDay + '-' + (iMonth + 1) + '-' + iYear;

    var d = new Date();
    d.setFullYear(iYear, iMonth, iDay);

    if (d.getMonth() != iMonth) {
        //alert ('The ' + sDateStr + ' is NOT a valid date.');
        //return false;
        return null;
    } else {
        //return true;
        return d;
    }
}


function ltrim(instr) {
    var isDone = false;
    while (!isDone) if (instr.charAt(0) == ' ') instr = instr.substring(1); else isDone = true;
    return instr;
}


function rtrim(instr) {
    var isDone = false;
    while (!isDone) if (instr.charAt(instr.length - 1) == ' ') instr = instr.substring(0, instr.length - 1); else isDone = true;
    return instr;
}


function trim(instr) {
    instr = ltrim(instr);
    instr = rtrim(instr);
    return instr;
}


