﻿// JScript File
function CheckAll(aspCheckBoxID, checkVal) {
    for(i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]

        if (elm.type == 'checkbox') {
            if (String(elm.id).substring(elm.id.length - aspCheckBoxID.length, elm.id.length) == aspCheckBoxID) {
                elm.checked = checkVal
            }
        }
    }
}

function checkSubmit(strCode, strDesc) {
    if (document.getElementById(strCode).value == '')
    {
        window.alert("Please enter Code.");
        document.getElementById(strCode).focus();
        return false;
    } else if (document.getElementById(strDesc).value == '') {
        window.alert("Please enter Description.");
        document.getElementById(strDesc).focus();
        return false;
    } else if (confirm("Please double check the details before submitting your data, if you have not do so, please click 'Cancel', otherwise please click 'OK' to continue.")) {
        return true;
    } else {
        return false;
    } 
}

function Search(CtrlName,nPage,nTitle,nDetails)
{
    ChildWindow = window.open(nPage + '&CtrlName=' + CtrlName, nTitle, nDetails);
}


function openWindow(theURL,winName,features) {
	if (winName.window) winName.focus;
	popupWin = window.open(theURL,winName,features)
	popupWin.focus;
}

    
function checkConfirm(checkItem) {
    var totalrecord = 0;
    
    for(i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]

        if (elm.type == 'checkbox') {
            if (String(elm.id).substring(elm.id.length - checkItem.length, elm.id.length) == checkItem) {
                if (elm.checked == true) {
                    totalrecord = totalrecord + 1
                }
            }
        }
    }
    
    if (totalrecord > 0) {
        if (confirm("Are you sure want to delete the record(s)?")) {
            return true;
        } else {
            return false;
        } 
    } else {
        alert("Please select at least one record to delete");
        
        return false;
    }
}

function checkRemove() {
    if (confirm("Are you sure want to remove the Display Image?")) {
        return true;
    } else {
        return false;
    } 
}

function CheckPhoneNo()
{
    if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode == 43) || (event.keyCode == 45))
    {
        return true;
    }
    else
    {
        return false;
    }
}


function CheckDouble()
{
    if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode == 46))
    {
        return true;
    }
    else
    {
        return false;
    }
}


function CheckNumeric(CtrlName)
{
    if(isNaN(document.getElementById(CtrlName).value))
    {
        window.alert("Invalid value");
        document.getElementById(CtrlName).focus();
    }
}

function openWindow(theURL,winName,features) {
	if (winName.window) winName.focus;
	popupWin = window.open(theURL,winName,features)
	popupWin.focus;
}


function checkUser(strTransaction, strUserName, strName, strLevel, strPassword, strConfirmPassword, strCurrectPassword, strEmail) {
    if (strTransaction == 'N') {
        if (document.getElementById(strUserName).value == '')
        {
            window.alert("Please enter User Name.");
            document.getElementById(strUserName).focus();
            return false;
        } 
    }
    
    if (strTransaction == 'N' || strTransaction == 'PL' || strTransaction == 'EPL') {
        if (document.getElementById(strName).value == '') 
        {
            window.alert("Please enter Name.");
            document.getElementById(strName).focus();
            return false;
        } 
        
        if (document.getElementById(strEmail).value == '') 
        {
            window.alert("Please enter Email.");
            document.getElementById(strEmail).focus();
            return false;
        } 
    }
    
    if (strTransaction == 'N' || strTransaction == 'PL') {
        if (document.getElementById(strLevel).value == '') 
        {
            window.alert("Please select User Level.");
            document.getElementById(strLevel).focus();
            return false;
        } 
    }
    
    if (strTransaction == 'P') {
        if (document.getElementById(strCurrectPassword).value == '') 
        {
            window.alert("Please enter Current Password.");
            document.getElementById(strCurrectPassword).focus();
            return false;
        }   
    }
    
    if (strTransaction == 'N' || strTransaction == 'P') {
        if (document.getElementById(strPassword).value == '') 
        {
            window.alert("Please enter New Password.");
            document.getElementById(strPassword).focus();
            return false;
        } 
        
        if (strTransaction == 'P') {
            if (document.getElementById(strPassword).value == document.getElementById(strCurrectPassword).value) {
                window.alert("New Password cannot same with Current Password.");
                document.getElementById(strPassword).focus();
                return false;
            } 
        }
        
        if (document.getElementById(strConfirmPassword).value == '') 
        
        {
            window.alert("Please enter Confirm Password.");
            document.getElementById(strConfirmPassword).focus();
            return false;
        } 
        
        if (document.getElementById(strConfirmPassword).value != document.getElementById(strPassword).value) {
            window.alert("Confirm Password different with New Password.");
            document.getElementById(strConfirmPassword).focus();
            return false;
        } 
    }
    
    if (confirm("Please double check the details before submitting your data, if you have not do so, please click 'Cancel', otherwise please click 'OK' to continue.")) {
        return true;
    } else {
        return false;
    } 
}

function checkSearch(strCriteria, strType) {
    if (document.getElementById(strType).value != 'A') {
        if (document.getElementById(strCriteria) != null) {
            if (document.getElementById(strCriteria).value == '')
            {
                window.alert("Please enter Search Criteria.");
                document.getElementById(strCriteria).focus();
                return false;
            }
        }
    } 
}


function checkSearchDate(strCriteria, strType, strFrom, strTo) {
    if (document.getElementById(strType).value != 'A') {
        if (document.getElementById(strCriteria) != null) {
            if (document.getElementById(strCriteria).value == '') {
                window.alert("Please enter Search Criteria.");
                document.getElementById(strCriteria).focus();
                return false;
            }
        } else if (document.getElementById(strType).value == 'D' || document.getElementById(strType).value == 'SD' || document.getElementById(strType).value == 'ED' || document.getElementById(strType).value == 'CD') {
            if (document.getElementById(strFrom).value == '') {
                window.alert("Please enter Search Criteria.");
                document.getElementById(strFrom).focus();
                return false;
            } 
            
            if (isDate(document.getElementById(strFrom).value) == false) {
                document.getElementById(strFrom).focus();
                return false;
            }
            
            
            if (document.getElementById(strTo).value != '') {
                if (isDate(document.getElementById(strTo).value) == false) {
                    document.getElementById(strTo).focus();
                    return false;
                }
            }
            
            if (document.getElementById(strFrom).value != '' && document.getElementById(strTo).value != '') {
                if (checkDate(document.getElementById(strFrom).value, document.getElementById(strTo).value) == false) {
                    window.alert("To date must occur after From date.");
                    document.getElementById(strTo).focus();
                    return false;
                }
            }
        }
    } 
}


function checkReset(checkItem) {
    var totalrecord = 0;
    
    for(i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]

        if (elm.type == 'checkbox') {
            if (String(elm.id).substring(elm.id.length - checkItem.length, elm.id.length) == checkItem) {
                if (elm.checked == true) {
                    totalrecord = totalrecord + 1
                }
            }
        }
    }
    
    if (totalrecord > 0) {
        if (confirm("Are you sure want to reset the password(s)?")) {
            return true;
        } else {
            return false;
        } 
    } else {
        alert("Please select at least one record to reset the password");
        
        return false;
    }
}


function checkCustomer(strCompanyName, strContactPerson, strAddress, strState, strPostcode, strCountry, strContactNo) {
    if (document.getElementById(strCompanyName).value == '')
        {
            window.alert("Please enter Company Name.");
            document.getElementById(strCompanyName).focus();
            return false;
        } 
    
    if (document.getElementById(strContactPerson).value == '') 
        {
            window.alert("Please enter Contact Person.");
            document.getElementById(strContactPerson).focus();
            return false;
        } 
        
    if (document.getElementById(strAddress).value == '') 
    {
        window.alert("Please enter Address.");
        document.getElementById(strAddress).focus();
        return false;
    } 
        
   if (document.getElementById(strState).value == '') 
        {
            window.alert("Please select State.");
            document.getElementById(strState).focus();
            return false;
        }   
    
    
//    if (document.getElementById(strPostcode).value == '') 
//        {
//            window.alert("Please enter Postcode.");
//            document.getElementById(strPostcode).focus();
//            return false;
//        } 
        
    if (document.getElementById(strCountry).value == '')     
        {
            window.alert("Please select Country.");
            document.getElementById(strCountry).focus();
            return false;
        } 
        
    if (document.getElementById(strContactNo).value == '') {
        window.alert("Please enter Contact No.");
        document.getElementById(strContactNo).focus();
        return false;
    } 
    
    if (confirm("Please double check the details before submitting your data, if you have not do so, please click 'Cancel', otherwise please click 'OK' to continue.")) {
        return true;
    } else {
        return false;
    } 
}

function checkNews(strTitle, strMessage, strStartDate, strEndDate) {
    updateRTE('rte3');
        
    if (document.getElementById(strTitle).value == '')
    {
        window.alert("Please enter Title.");
        document.getElementById(strTitle).focus();
        return false;
    } 
    
    if (document.getElementById('hdn' + strMessage).value == '') 
    {
        window.alert("Please enter Message.");
        return false;
    } 
    
    if (document.getElementById(strStartDate).value == '') 
    {
        window.alert("Please enter Start Date.");
        document.getElementById(strStartDate).focus();
        return false;
    } 
            
    if (isDate(document.getElementById(strStartDate).value) == false) 
    {
        document.getElementById(strStartDate).focus();
        return false;
    }
    
    if (document.getElementById(strEndDate).value == '') 
    {
        window.alert("Please enter Expired Date.");
        document.getElementById(strEndDate).focus();
        return false;
    } 
            
    if (isDate(document.getElementById(strEndDate).value) == false) 
    {
        document.getElementById(strEndDate).focus();
        return false;
    }
            
    if (document.getElementById(strStartDate).value != '' && document.getElementById(strEndDate).value != '') 
    {
        if (checkDate(document.getElementById(strStartDate).value, document.getElementById(strEndDate).value) == false) {
            window.alert("Expired date must occur after Start date.");
            document.getElementById(strEndDate).focus();
            return false;
        }
    }
            
    if (confirm("Please double check the details before submitting your data, if you have not do so, please click 'Cancel', otherwise please click 'OK' to continue.")) {
        return true;
    } else {
        return false;
    }
}

function SearchCalender(CtrlName,nPage,nTitle,nDetails)
{
    ChildWindow = window.open(nPage + '?CtrlName=' + CtrlName, nTitle, nDetails);
}

function checkPassRecovery(strType, strCompanyCode, strUserName, strEmail) {
    if (strType == 'U') {
        if (document.getElementById(strCompanyCode).value == '')
        {
            window.alert("Please enter Company Code.");
            document.getElementById(strCompanyCode).focus();
            return false;
        }
    }
    
    if (document.getElementById(strUserName).value == '') {
        window.alert("Please enter Username.");
        document.getElementById(strUserName).focus();
        return false;
    } else if (document.getElementById(strEmail).value == '') {
        window.alert("Please enter Email.");
        document.getElementById(strEmail).focus();
        return false;
    } else if (confirm("Please double check the details before submitting your data, if you have not do so, please click 'Cancel', otherwise please click 'OK' to continue.")) {
        return true;
    } else {
        return false;
    } 
}


function checkLogin(strType, strCompanyCode, strUserName, strPassword) {
    if (strType == 'U') {
        if (document.getElementById(strCompanyCode).value == '')
        {
            window.alert("Please enter Company Code.");
            document.getElementById(strCompanyCode).focus();
            return false;
        }
    }
    
    if (document.getElementById(strUserName).value == '') {
        window.alert("Please enter Username.");
        document.getElementById(strUserName).focus();
        return false;
    } else if (document.getElementById(strPassword).value == '') {
        window.alert("Please enter Password.");
        document.getElementById(strPassword).focus();
        return false;
    } else {
        return true;
    } 
}

function checkChangePass(strNew, strConfirm) {
    if (document.getElementById(strNew).value == '') 
    {
        window.alert("Please enter New Password.");
        document.getElementById(strNew).focus();
        return false;
    } else if (document.getElementById(strConfirm).value == '') {
        window.alert("Please enter Confirm Password.");
        document.getElementById(strConfirm).focus();
        return false;
    } else if (document.getElementById(strNew).value != document.getElementById(strConfirm).value) {
        window.alert("Confirm Password different with New Password.");
        document.getElementById(strConfirm).focus();
        return false;
    } else {
        return true;
    }
} 

function checkNewMS(strType, strCategory) {
    if (document.getElementById(strCategory).value == '') 
    {
        window.alert("Please select Category.");
        document.getElementById(strCategory).focus();
        return false;
    } else if (confirm("Please double check the details before submitting your data, if you have not do so, please click 'Cancel', otherwise please click 'OK' to continue.")) {
        updateRTE('rte1');
        
        return true;
    } else {
        return false;
    }
} 


function checkEmail(strType, strTitle) {
    if (document.getElementById(strType).value == '') 
    {
        window.alert("Please select Sent To.");
        document.getElementById(strType).focus();
        return false;
    } else if (document.getElementById(strTitle).value == '') {
        window.alert("Please enter Subject.");
        document.getElementById(strTitle).focus();
        return false;
    } else if (confirm("Please double check the details before submitting your data, if you have not do so, please click 'Cancel', otherwise please click 'OK' to continue.")) {
        updateRTE('rte4');
        
        return true;
    } else {
        return false;
    }
} 


function checkForward(strUser) {
    if (document.getElementById(strUser).value == '') 
    {
        window.alert("Please select Sent To.");
        document.getElementById(strUser).focus();
        return false;
    } else if (confirm("Please double check the details before submitting your data, if you have not do so, please click 'Cancel', otherwise please click 'OK' to continue.")) {
        updateRTE('rte5');
        
        return true;
    } else {
        return false;
    }
} 


function checkUpdateMS(strSystem, strSection, strStatus, strType, strNumber) {
    if (strSection == '2') {
        if (strSystem == 'IMS') {
            if (document.getElementById(strNumber) != null) {
                if (document.getElementById(strNumber).value != '') {
                    if (document.getElementById(strType).value == '') {
                        window.alert("Please select Man-Day type.");
                        document.getElementById(strType).focus();
                        return false;
                    } 
                } if (document.getElementById(strType).value != '' && document.getElementById(strType).value != 'F') {
                    if (document.getElementById(strNumber).value == '') {
                        window.alert("Please enter Man-Day.");
                        document.getElementById(strNumber).focus();
                        return false;
                    } 
                }
            }
        } else if (strSystem == 'RMS') {
            if (document.getElementById(strNumber) != null) {
                if (document.getElementById(strNumber).value != '') {
                    if (document.getElementById(strType).value == '') {
                        window.alert("Please select Man-Day type.");
                        document.getElementById(strType).focus();
                        return false;
                    } 
                } if (document.getElementById(strType).value != '' && document.getElementById(strType).value != 'F') {
                    if (document.getElementById(strNumber).value == '') {
                        window.alert("Please enter Man-Day.");
                        document.getElementById(strNumber).focus();
                        return false;
                    } 
                }
            } 
        } 
    } 
    
    updateRTE('rte2');
    return true;
} 


function checkClose() {
    if (confirm("Are you sure want to close the ticket?")) {
        return true;
    } else {
        return false;
    }
} 


function isDate(dateStr) {
    var datePat = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        alert("Please enter date as yyyy-mm-dd.");
        return false;
    }

    year = matchArray[1]; // p@rse date into variables
    month = matchArray[3];
    day = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("Month "+month+" doesn`t have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        
        if (day > 29 || (day==29 && !isleap)) {
            alert("February " + year + " doesn`t have " + day + " days!");
            return false;
        }
    }
    
    return true; // date is valid
}

function checkDate(strFromDate, strToDate) {
    if (strFromDate > strToDate) {
        return false;
    } else {
        return true;
    }
}