﻿var newWindow;

function CalCourseRegTotal(event) 
{
    if (event != null) 
    {
        var txt = event.srcElement || event.target;
        txt.value = formatNumber(2, txt.value);
    }
    var gridRegs = document.getElementById('ctl00_sitecontent_gvRegistration');
    if (gridRegs == null)
        gridRegs = document.getElementById('gvRegistration');

    var gridProds = document.getElementById('ctl00_sitecontent_gvRegProducts');
    if (gridProds == null)
        gridProds = document.getElementById('gvRegProducts');

    var CrsRegSubTotal = 0;
    for (i = 1; i < gridRegs.rows.length - 1; i++) 
    {
        var tbl_CourseReg = gridRegs.rows[i].cells[0].getElementsByTagName('TABLE')[0];
        if (tbl_CourseReg.rows[0].cells[3].getElementsByTagName('INPUT')[0].id.indexOf("txtCourseInvAmnt") != -1) 
        {
            var CourseInvCntrl = parseFloat(tbl_CourseReg.rows[0].cells[3].getElementsByTagName('INPUT')[0].value);
            if (isNaN(CourseInvCntrl)) CourseInvCntr = 0;
            CrsRegSubTotal = CrsRegSubTotal + CourseInvCntrl;
        }
    }
    var tbl_CourseRegFoot = gridRegs.rows[gridRegs.rows.length - 1].cells[0].getElementsByTagName('TABLE')[0];
    if (tbl_CourseRegFoot.rows[0].cells[3].getElementsByTagName('INPUT')[0].id.indexOf("txtTotCourseInvAmnt") != -1) 
    {
        var CourseRegTotal = tbl_CourseRegFoot.rows[0].cells[3].getElementsByTagName('INPUT')[0];
        CourseRegTotal.value = CrsRegSubTotal.toFixed(2);
    }
    var CourseProdTotal =0
    if (gridProds != null && gridProds.rows.length > 2) 
    {
        var tbl_ProdOrdersFoot = gridProds.rows[gridProds.rows.length - 1].cells[0].getElementsByTagName('TABLE')[0];
        if (tbl_ProdOrdersFoot.rows[0].cells[4].getElementsByTagName('INPUT')[0].id.indexOf("txtTotProdOrderAmnt") != -1)
        CourseProdTotal = parseFloat(tbl_ProdOrdersFoot.rows[0].cells[4].getElementsByTagName('INPUT')[0].value);
    }
    else 
    {
        CourseProdTotal = 0;
    }
    var AuthAdjAmntCntrl = document.getElementById("ctl00_sitecontent_txtPromoAmt");
    if (AuthAdjAmntCntrl == null)
        AuthAdjAmntCntrl = document.getElementById("txtPromoAmt");
    if (AuthAdjAmntCntrl != null)
        var AuthAdjAmnt = AuthAdjAmntCntrl.value;

    var RegistrationTotal = document.getElementById("ctl00_sitecontent_txtTotInvAmnt");
    var NewRegTotal = 0;
    if (RegistrationTotal == null)
        RegistrationTotal = document.getElementById("txtTotInvAmnt");
    if (RegistrationTotal != null) 
    {
        NewRegTotal = parseFloat(CrsRegSubTotal) + parseFloat(CourseProdTotal) - parseFloat(AuthAdjAmnt);
        RegistrationTotal.value = NewRegTotal.toFixed(2);
    }
    var OldRegTotalCntrl = document.getElementById("ctl00_sitecontent_txtOrigInvAmnt");
    if (OldRegTotalCntrl == null)
        OldRegTotalCntrl = document.getElementById("txtOrigInvAmnt");
    if (OldRegTotalCntrl != null)
        var OldRegTotal = parseFloat(OldRegTotalCntrl.value);
    if (NewRegTotal < OldRegTotal)
        alert("Please refund the Course registration & Product investment manually, as it is not processed automatically by the system...");    
    return RegistrationTotal;
}

function CalProdOrders(event) {
    
    var gridProds = document.getElementById('ctl00_sitecontent_gvRegProducts');
    if (gridProds == null)
        gridProds = document.getElementById('gvRegProducts');

    var gridRegs = document.getElementById('ctl00_sitecontent_gvRegistration');
    if (gridRegs == null)
        gridRegs = document.getElementById('gvRegistration');
        
    var CrsProdsAmnt = 0;
    var TotProdQty = 0;
    for (i = 1; i < gridProds.rows.length - 1; i++) 
    {
        var tbl_ProdOrders = gridProds.rows[i].cells[0].getElementsByTagName('TABLE')[0];
        if (tbl_ProdOrders.rows[0].cells[2].getElementsByTagName('INPUT')[0].id.indexOf("txtUnitCost") != -1
            && tbl_ProdOrders.rows[0].cells[3].getElementsByTagName('INPUT')[0].id.indexOf("txtOrderQty") != -1
            ) 
        {
            var ProdQty = parseInt(tbl_ProdOrders.rows[0].cells[3].getElementsByTagName('INPUT')[0].value);
            var UnitCost = parseFloat(tbl_ProdOrders.rows[0].cells[2].getElementsByTagName('INPUT')[0].value);
            if (isNaN(ProdQty)) ProdQty = 0;
            if (isNaN(UnitCost)) UnitCost = 0;
            var ProdOrderAmnt = ProdQty * UnitCost;
            TotProdQty = TotProdQty + ProdQty;
            var ProdOrderCntrl = tbl_ProdOrders.rows[0].cells[4].getElementsByTagName('INPUT')[0];
            ProdOrderCntrl.value = ProdOrderAmnt.toFixed(2);
            CrsProdsAmnt = CrsProdsAmnt + ProdOrderAmnt
        }
    }

    var tbl_ProdOrdersFoot = gridProds.rows[gridProds.rows.length - 1].cells[0].getElementsByTagName('TABLE')[0];
    if (tbl_ProdOrdersFoot.rows[0].cells[4].getElementsByTagName('INPUT')[0].id.indexOf("txtTotProdOrderAmnt") != -1)
        tbl_ProdOrdersFoot.rows[0].cells[4].getElementsByTagName('INPUT')[0].value = CrsProdsAmnt.toFixed(2);

    if (tbl_ProdOrdersFoot.rows[0].cells[3].getElementsByTagName('INPUT')[0].id.indexOf("txtTotProdQty") != -1)
        tbl_ProdOrdersFoot.rows[0].cells[3].getElementsByTagName('INPUT')[0].value = parseInt(TotProdQty);

    var CourseRegTotal =0;
    if (gridRegs != null && gridRegs.rows.length > 2) {
        var tbl_CourseRegFoot = gridRegs.rows[gridRegs.rows.length - 1].cells[0].getElementsByTagName('TABLE')[0];
        if (tbl_CourseRegFoot.rows[0].cells[3].getElementsByTagName('INPUT')[0].id.indexOf("txtTotCourseInvAmnt") != -1) {
            CourseRegTotal = tbl_CourseRegFoot.rows[0].cells[3].getElementsByTagName('INPUT')[0].value;
        }
    }
    else
        CourseRegTotal = 0;
        
    var AuthAdjAmntCntrl = document.getElementById("ctl00_sitecontent_txtPromoAmt");
    if (AuthAdjAmntCntrl == null)
        AuthAdjAmntCntrl = document.getElementById("txtPromoAmt");
    if (AuthAdjAmntCntrl != null)
        var AuthAdjAmnt = AuthAdjAmntCntrl.value;

    var RegistrationTotal = document.getElementById("ctl00_sitecontent_txtTotInvAmnt");
    var NewRegTotal = 0;
    if (RegistrationTotal == null)
        RegistrationTotal = document.getElementById("txtTotInvAmnt");
    if (RegistrationTotal != null) {
        NewRegTotal = parseFloat(CourseRegTotal) + parseFloat(CrsProdsAmnt) - parseFloat(AuthAdjAmnt);
        RegistrationTotal.value = NewRegTotal.toFixed(2);
    }
    
    var OldRegTotalCntrl = document.getElementById("ctl00_sitecontent_txtOrigInvAmnt");
    if (OldRegTotalCntrl == null)
        OldRegTotalCntrl = document.getElementById("txtOrigInvAmnt");
    if (OldRegTotalCntrl != null)
        var OldRegTotal = parseFloat(OldRegTotalCntrl.value);

    if (NewRegTotal < OldRegTotal)
        alert("Please refund the Course registration & Product investment manually, as it is not processed automatically by the system...");
    return RegistrationTotal;
}
function CalAuthAmnt(event) {
    var AuthAmnt=0;
    if (event != null) {
        var txt = event.srcElement || event.target;
        txt.value = formatNumber(2, txt.value);
        AuthAmnt = txt.value;
    }

    var gridProds = document.getElementById('ctl00_sitecontent_gvRegProducts');
    if (gridProds == null)
        gridProds = document.getElementById('gvRegProducts');

    var gridRegs = document.getElementById('ctl00_sitecontent_gvRegistration');
    if (gridRegs == null)
        gridRegs = document.getElementById('gvRegistration');

    var CourseRegTotal = 0;
    if (gridRegs != null && gridRegs.rows.length > 2) {
        var tbl_CourseRegFoot = gridRegs.rows[gridRegs.rows.length - 1].cells[0].getElementsByTagName('TABLE')[0];
        if (tbl_CourseRegFoot.rows[0].cells[3].getElementsByTagName('INPUT')[0].id.indexOf("txtTotCourseInvAmnt") != -1)
            CourseRegTotal = tbl_CourseRegFoot.rows[0].cells[3].getElementsByTagName('INPUT')[0].value;
    }
    else
        CourseRegTotal = 0;

    var CourseProdTotal = 0;
    if (gridProds != null && gridProds.rows.length > 2) {
        var tbl_ProdOrdersFoot = gridProds.rows[gridProds.rows.length - 1].cells[0].getElementsByTagName('TABLE')[0];
        if (tbl_ProdOrdersFoot.rows[0].cells[4].getElementsByTagName('INPUT')[0].id.indexOf("txtTotProdOrderAmnt") != -1)
            CourseProdTotal = parseFloat(tbl_ProdOrdersFoot.rows[0].cells[4].getElementsByTagName('INPUT')[0].value);
    }
    else
        CourseProdTotal = 0;
        
    var RegistrationTotal = document.getElementById("ctl00_sitecontent_txtTotInvAmnt");
    var NewRegTotal = 0;
    if (RegistrationTotal == null)
        RegistrationTotal = document.getElementById("txtTotInvAmnt");
    if (RegistrationTotal != null) {
        NewRegTotal = parseFloat(CourseRegTotal) + parseFloat(CourseProdTotal) - parseFloat(AuthAmnt);
        RegistrationTotal.value = NewRegTotal.toFixed(2);
    }
    var OldRegTotalCntrl = document.getElementById("ctl00_sitecontent_txtOrigInvAmnt");
    if (OldRegTotalCntrl == null)
        OldRegTotalCntrl = document.getElementById("txtOrigInvAmnt");
    if (OldRegTotalCntrl != null)
        var OldRegTotal = parseFloat(OldRegTotalCntrl.value);

    if (NewRegTotal < OldRegTotal)
        alert("Please refund the Course registration & Product investment manually, as it is not processed automatically by the system...");
    return RegistrationTotal;
}

function formatNumber(decimalPlaces, amount) 
{
    if (amount.toString() == "") return amount;
    if (isNaN(amount)) return amount;
    amount = parseFloat(amount);
    return (amount == Math.floor(amount)) ? amount + '.' + replicate('0', decimalPlaces) : ((amount * Math.pow(10, (decimalPlaces - 1)) == Math.floor(amount * Math.pow(10, (decimalPlaces - 1)))) ? amount + replicate('0', decimalPlaces - amount.toString().split(".")[1].length) : amount);
    //return amount.toFixed(decimalPlaces);
}
function replicate(ch, count) 
{
    var str = "";
    var i = 0;
    for (i = 0; i < count; i++) 
    {
        str += ch;
    }
    return str;
}

function CheckPromoCode(event) {
    if (event != null) {
        var txt = event.srcElement || event.target;
        var PromoCode = 0;
        if (txt.value != "101" && txt.value != "202" && txt.value != "") {
            alert("Invalid code, Try again or please contact the office for the valid code and amount..");
        }
    }
}
function CheckPromoAmnt(event) {
    var AuthAmnt = 0;
    if (event != null) {
        var txt = event.srcElement || event.target;
        if (isNaN(txt.value) || txt.value == "") {
            txt.value = formatNumber(2, 0);
        }
        else {
            txt.value = formatNumber(2, txt.value);
            AuthAmnt = parseFloat(txt.value);    
        }
        var PromoCodeCntrl = document.getElementById("ctl00_sitecontent_txtPromCodeNo");
        if (PromoCodeCntrl == null)
            PromoCodeCntrl = document.getElementById("txtPromCodeNo");
        if (PromoCodeCntrl != null)
            var PromoCode = (PromoCodeCntrl.value);
        if (PromoCode != "101" && PromoCode != "202" && AuthAmnt > 0) {
            alert("Invalid code, Try again or please contact the office for the valid code and amount..");
            txt.value = formatNumber(2, 0);
        }
        else {
            var TotAmntXPromCntrl = document.getElementById("ctl00_sitecontent_txtTotalInvXProm");
            if (TotAmntXPromCntrl == null)
                TotAmntXPromCntrl = document.getElementById("txtTotalInvXProm");
            if (TotAmntXPromCntrl != null)
                var TotAmntXProm = parseFloat(TotAmntXPromCntrl.value);
            var FinalInvAmnt = TotAmntXProm - AuthAmnt;
                var TotCrsInvAmntDispCntrl = document.getElementById("ctl00_sitecontent_txtTotCourseInvDisp");
            if (TotCrsInvAmntDispCntrl == null)
                TotCrsInvAmntDispCntrl = document.getElementById("txtTotCourseInvDisp");
            
            var TotInvAmntCntrl = document.getElementById("ctl00_sitecontent_txtTotCourseInv");
            if (TotInvAmntCntrl == null)
                TotInvAmntCntrl = document.getElementById("txtTotCourseInv");
            if (TotInvAmntCntrl != null)
                TotInvAmntCntrl.value = formatNumber(2,FinalInvAmnt);
            
            if (TotCrsInvAmntDispCntrl != null) {
                TotCrsInvAmntDispCntrl.value = "$" + formatNumber(2,FinalInvAmnt);
            }
        }
    }
} 
    function checkDate(sender, args) {
        if (sender._selectedDate < new Date()) {
            alert("You cannot select a day earlier than today!");
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }
    }
    function SingleRadioChecked(spanChk) {
        var CurrentRdbID = spanChk.id;
        var gridCourses = document.getElementById('ctl00_sitecontent_gvCourses');
        if (gridCourses == null)
            gridCourses = document.getElementById('gvCourses');
        for (i = 1; i < gridCourses.rows.length; i++) {
            var tbl_Courses = gridCourses.rows[i].cells[0].getElementsByTagName('TABLE')[0];
            if (tbl_Courses.rows[0].cells[7].getElementsByTagName('INPUT')[2].id.indexOf("rdRegister") != -1) {
                var CourseRegRadio = tbl_Courses.rows[0].cells[7].getElementsByTagName('INPUT')[2];
                if (CourseRegRadio.id != CurrentRdbID && CourseRegRadio.type == "radio") {
                    if (CourseRegRadio.checked) { CourseRegRadio.checked = false; }
                }
            }
        }
    }
    function ComboExpRadioChecked(spanChk) {
        var CurrentRdbID = spanChk.id;
        var gridCourses = document.getElementById('ctl00_sitecontent_gvExp');
        if (gridCourses == null)
            gridCourses = document.getElementById('gvExp');
        for (i = 1; i < gridCourses.rows.length; i++) {
            var tbl_Courses = gridCourses.rows[i].cells[0].getElementsByTagName('TABLE')[0];
            if (tbl_Courses.rows[0].cells[6].getElementsByTagName('INPUT')[2].id.indexOf("rdRegister") != -1) {
                var CourseRegRadio = tbl_Courses.rows[0].cells[6].getElementsByTagName('INPUT')[2];
                if (CourseRegRadio.id != CurrentRdbID && CourseRegRadio.type == "radio") {
                    if (CourseRegRadio.checked) { CourseRegRadio.checked = false; }
                }
            }
        }
    }
    function ComboBCRadioChecked(spanChk) {
        var CurrentRdbID = spanChk.id;
        var gridCourses = document.getElementById('ctl00_sitecontent_gvBC');
        if (gridCourses == null)
            gridCourses = document.getElementById('gvBC');
        for (i = 1; i < gridCourses.rows.length; i++) {
            var tbl_Courses = gridCourses.rows[i].cells[0].getElementsByTagName('TABLE')[0];
            if (tbl_Courses.rows[0].cells[6].getElementsByTagName('INPUT')[2].id.indexOf("rdRegister") != -1) {
                var CourseRegRadio = tbl_Courses.rows[0].cells[6].getElementsByTagName('INPUT')[2];
                if (CourseRegRadio.id != CurrentRdbID && CourseRegRadio.type == "radio") {
                    if (CourseRegRadio.checked) { CourseRegRadio.checked = false; }
                }
            }
        }
    }
    function ComboMedRadioChecked(spanChk) {
        var CurrentRdbID = spanChk.id;
        var gridCourses = document.getElementById('ctl00_sitecontent_gvAdMed');
        if (gridCourses == null)
            gridCourses = document.getElementById('gvAdMed');
        for (i = 1; i < gridCourses.rows.length; i++) {
            var tbl_Courses = gridCourses.rows[i].cells[0].getElementsByTagName('TABLE')[0];
            if (tbl_Courses.rows[0].cells[6].getElementsByTagName('INPUT')[2].id.indexOf("rdRegister") != -1) {
                var CourseRegRadio = tbl_Courses.rows[0].cells[6].getElementsByTagName('INPUT')[2];
                if (CourseRegRadio.id != CurrentRdbID && CourseRegRadio.type == "radio") {
                    if (CourseRegRadio.checked) { CourseRegRadio.checked = false; }
                }
            }
        }
    }
    function CheckContentLengthEdit(sender, args) 
    {
        args.IsValid = (args.Value.length <= 250);
        return;
    }

    function CheckContentLength(sender, args) 
    {
        var obj = window.document.getElementById("ctl00_sitecontent_txtResourceDesc");
        alert(obj.value.Length);
        if (obj.value.Length <= 250) 
        {
            args.IsValid = true;
            return;
        }
        else {
            alert("This field allows only 250 Character at maximum, Please check the text.");
            obj.value = "";
            args.IsValid = false;
            return;
        }
    }
    function CheckFileTypeEdit(sender, args) {
        //var objFile = window.document.SBBankInfoManage.Browsefield.value;
        var obj = window.document.getElementById("ctl00_sitecontent_resUploadEdit");
        var objFile = obj.value;
        var ext = objFile.slice(objFile.lastIndexOf(".")).toLowerCase();
        if (ext == ".pdf") {
            args.IsValid = true;
            return;
        }
        else {
            alert("Only PDF documents are allowed as resource. Please upload a valid PDF document..");
            obj.value = "";
            args.IsValid = false;
            return;
        }
    }
    function CheckFileType(sender, args) {
        //var objFile = window.document.SBBankInfoManage.Browsefield.value;
        var obj = window.document.getElementById("ctl00_sitecontent_resUploadNew");
        var objFile = obj.value;
        var ext = objFile.slice(objFile.lastIndexOf(".")).toLowerCase();
        if (ext == ".pdf") {
            args.IsValid = true;
            return;
        }
        else {
            alert("Only PDF documents are allowed as resource. Please upload a valid PDF document..");
            obj.value = "";
            args.IsValid = false;
            return;
        }
    }