﻿// Scripts for ctlContents Begin //
function lbEdit_Click(sender, setWidth, xAdjust, yAdjust)
{
    var htmlid = sender.id.replace("lbEdit","divHTML");
    var divid = sender.id.replace("lbEdit","divEditor");
    var editorid = sender.id.replace("lbEdit","ftbEditor");
    
    var html = document.getElementById(htmlid);
    var div = document.getElementById(divid);
    var editor = document.getElementById(editorid);
    if(html!=null && div!=null && editor!=null)
    {
        var pos = findPos(html);
        div.style.top = pos.Y + parseInt(yAdjust);
        div.style.left = pos.X + parseInt(xAdjust);
        div.style.visibility = "visible";
        if(setWidth)
        {
            div.style.width = html.offsetWidth;
            editor.style.width = html.offsetWidth;
        }
    }
    return false;
}

function btnSave_Click(ftbid, divid, editorid)
{
    var div = document.getElementById(divid);
    var ftb = document.getElementById(ftbid);
    var editor = document.getElementById(editorid);
    if(div!=null && ftb!=null && editor!=null)
    {
        var ftb = document.getElementById(ftbid);
        div.innerHTML = ftb.value;
        editor.style.visibility = "hidden";
    }
   return false;
}

function btnCancel_Click(sender)
{
    var editorid = "";
    if(sender.id.indexOf("btnCancel") > -1)
        editorid = sender.id.replace("btnCancel","divEditor");
    else
        editorid = sender.id.replace("ibClose","divEditor");

    var editor = document.getElementById(editorid);
    if(editor!=null)
        editor.style.visibility = "hidden";
    return false;
}
// Scripts for ctlContents End //

// Scripts for ctlSection Begin //
    function SectionAction_OnMouseOver(sender)
    {
        if(sender.className.indexOf("actionMin") > -1)
            sender.className = "actionMin_hover";
        else if(sender.className.indexOf("actionMax") > -1)
            sender.className = "actionMax_hover";
        else if(sender.className.indexOf("actionClose") > -1)
            sender.className = "actionClose_hover";
    }
    function SectionAction_OnMouseOut(sender)
    {
        if(sender.className.indexOf("actionMin") > -1)
            sender.className = "actionMin";
        if(sender.className.indexOf("actionMax") > -1)
            sender.className = "actionMax";
        if(sender.className.indexOf("actionClose") > -1)
            sender.className = "actionClose";
    }
    
    // Only use display property to hide/display section
    // Other place use visibility property
    // Because display property for affect DevExpress HTML Editor display
    function SectionActionClick(sender, bodyid)
    {
        var body = document.getElementById(bodyid);
        if(body!=null)
        {
            if(sender.className.indexOf("actionMin") > -1)
            {
                sender.className = "actionMax_hover";
                //body.style.visibility = "hidden";
                body.style.display = "none";

            }
            else
            {
                sender.className = "actionMin_hover";
                //body.style.visibility = "visible";
                body.style.display = "inline";
            }
        }
    }
// Scripts for ctlSection End //

// Scripts for ctlImage Begin //
    function SetUploadPanel(sender, display, xAdjust, yAdjust)
    {
        var baseStr = "lbEdit";
        if(sender.id.indexOf("ibClose") > -1)
            baseStr = "ibClose";
        
        var txtid = sender.id.replace(baseStr,"txtPanelMode");
        var panid = sender.id.replace(baseStr,"panUpload");
    
        var pan = document.getElementById(panid);
        if(pan!=null)
        {
            pan.style["display"] = display;
            if(display == "inline")
            {
                var pos = findPos(sender);
                pan.style.top = pos.Y + parseInt(yAdjust);
                pan.style.left = pos.X + parseInt(xAdjust);
                
                var txt = document.getElementById(txtid);
                if(txt!=null) txt.value = display;
            }
        }
        return false;
    }
// Script for ctlImage End //

// Script for ctlEventPackage Begin //
function ShowPanel(sender)
{
    var panid = "";
    if(sender.id.indexOf("lbEdit")>-1)
        panid =  sender.id.replace("lbEdit","pan");
    else
        panid =  sender.id.replace("lbAdd","pan");
    
    var pan = document.getElementById(panid);
    if(pan!=null) 
    {
        pan.style.visibility = "visible";
        var pos = findPos(sender);
        pan.style.top = pos.Y;
        pan.style.left = pos.X;
    }
    var status = document.getElementById(panid + "Status");
    if(status!=null) status.value = "visible";    

    return false;
}
function ShowPanel_Adjust(sender, xAdjust, yAdjust)
{
    var panid = "";
    if(sender.id.indexOf("lbEdit")>-1)
        panid =  sender.id.replace("lbEdit","pan");
    else
        panid =  sender.id.replace("lbAdd","pan");
    
    var pan = document.getElementById(panid);
    if(pan!=null) 
    {
        pan.style.display = "inline";
        var pos = findPos(sender);
        pan.style.top = pos.Y + yAdjust;
        pan.style.left = pos.X + xAdjust;
    }
    var status = document.getElementById(panid + "Status");
    if(status!=null) status.value = "inline";    

    return false;
}

function ShowPanelAtCenter(sender)
{
    var panid = "";
    if(sender.id.indexOf("lbEdit")>-1)
        panid =  sender.id.replace("lbEdit","pan");
    else
        panid =  sender.id.replace("lbAdd","pan");
    
    var pan = document.getElementById(panid);
    if(pan!=null) 
    {
        pan.style.visibility = "visible";
        var pos = findPos(sender);
        pan.style.top = pos.Y;
        pan.style.left = pos.X - 300;
    }
    return false;
}
function HidePanel(sender)
{
    var panid = "";
    if(sender.id.indexOf("ibClose") > -1)
        panid = sender.id.replace("ibClose","pan");
    else if(sender.id.indexOf("btnSave") > -1)
        panid = sender.id.replace("btnSave","pan");
    else if(sender.id.indexOf("btnAdd") > -1)
        panid = sender.id.replace("btnAdd","pan");
    else 
        panid = sender.id.replace("btnCancel","pan");
    
    var pan = document.getElementById(panid);
    if(pan != null) pan.style.visibility = "hidden";

    var status = document.getElementById(panid + "Status");
    if(status!=null) status.value = "hidden";    

    return false;
}
function HidePanel_Adjust(sender)
{
    var panid = "";
    if(sender.id.indexOf("ibClose") > -1)
        panid = sender.id.replace("ibClose","pan");
    else if(sender.id.indexOf("btnSave") > -1)
        panid = sender.id.replace("btnSave","pan");
    else if(sender.id.indexOf("btnAdd") > -1)
        panid = sender.id.replace("btnAdd","pan");
    else 
        panid = sender.id.replace("btnCancel","pan");
    
    var pan = document.getElementById(panid);
    if(pan != null) pan.style.display = "none";

    var status = document.getElementById(panid + "Status");
    if(status!=null) status.value = "none";    

    return false;
}
function EnableTextBox(sender)
{
    var txtid = "";
    if(sender.id.indexOf("StateTax") > -1)
        txtid = sender.id.replace("chkIsStateTax","txtStateTaxPercent");
    else
        txtid = sender.id.replace("chkIsFedTax","txtFedTaxPercent");
    var txt = document.getElementById(txtid);
    if(txt != null)
    {
        txt.disabled = !sender.checked;
    }
}

function ctlEventPackageList_GetPropertiesByPackageID(packageid)
{
    var i=0;
    var arr = new Array(0,0,0,0);
    for(i=0;i<packages.length;i++)
    {
        if(packages[i][0] == packageid)
        {
            arr[0] = packages[i][1]; // price
            arr[1] = packages[i][2]; // fed amount
            arr[2] = packages[i][3]; // state amount
            arr[3] = packages[i][4]; // no covered
            arr[4] = packages[i][5];
            break;
        }
    }
    return arr;
}

function ctlEventPackageList_PackageChecked(sender)
{
    var checkid = sender.id;
    var arr = ctlEventPackageList_GetPropertiesByPackageID(checkid);
    var price = arr[0];
    var fed = arr[1];
    var state = arr[2];
    var nocovered = arr[3];
    var checked = sender.checked;

    var currentCheckName = "", otherCheckName = "";
    if(checkid.indexOf("chkMySelf") > -1) 
    {
        currentCheckName = "chkMySelf";
        otherCheckName = "chkSpouse";
    }
    else if(checkid.indexOf("chkSpouse") > -1) 
    {
        currentCheckName = "chkSpouse";
        otherCheckName = "chkMySelf";
    }
    var baseStr = checkid.replace(currentCheckName,"");

    // Get checkbox current checked status
    if(parseInt(nocovered) == parseInt(1))
    {
        // Clear all current type selections 
        ctlEventPackageList_ClearSelection(currentCheckName);
        
        // go through noCovered==2: uncheck those single selection
        ctlEventPackageList_ClearCover2Selection(otherCheckName);

        if(checked) 
        {
            // Check current checkbox
            sender.checked = true;
        }
    }
    else if(parseInt(nocovered) == parseInt(2))
    {
        // Clear all selections for both types 
        ctlEventPackageList_ClearSelection("");
        
        // Check both types
        if(checked)
        {
            var myself = document.getElementById(baseStr + currentCheckName);
            var spouse = document.getElementById(baseStr + otherCheckName);
            if(myself!=null && spouse!=null)
            {
                myself.checked = true;
                spouse.checked = true;
            }
        }
    }

    // Check total for all packages and add-ons 
    ctlEventPackage_SetTotal();
}
function ctlEventPackageList_ClearSelection(checkName)
{
    var alltxt = document.getElementsByTagName("input");
    for(var i=0;i<alltxt.length;i++)
    {
        if(alltxt.type = "checkbox")
        {
            if(checkName == "" && (alltxt[i].id.indexOf("chkMySelf") > -1 || alltxt[i].id.indexOf("chkSpouse") > -1))
                alltxt[i].checked = false;
            else if(alltxt[i].id.indexOf(checkName) > -1)
                alltxt[i].checked = false;
        }
    }
}
// Go through specific Checkboxes
function ctlEventPackageList_ClearCover2Selection(checkName)
{
    var alltxt = document.getElementsByTagName("input");
    for(var i=0;i<alltxt.length;i++)
    {
        if(alltxt.type = "checkbox")
        {
            var arr = ctlEventPackageList_GetPropertiesByPackageID(alltxt[i].id);
            if(alltxt[i].checked && alltxt[i].id.indexOf(checkName) > -1 && parseInt(arr[3]) == parseInt(2))
                alltxt[i].checked = false;
        }
    }
}

function ctlEventAddOnList_GetPropertiesByAddOnID(addonid)
{
    var i=0;
    var arr = new Array(0,0,0,0,0);
    for(i=0;i<addons.length;i++)
    {
        if(addons[i][0] == addonid)
        {
            arr[0] = addons[i][1]; // price
            arr[1] = addons[i][2]; // fed amount
            arr[2] = addons[i][3]; // state amount
            arr[3] = addons[i][4]; // AddOnPriceId | AttendeeID
            arr[4] = addons[i][5]; // RegAllowed (-1 if no limit)
            break;
        }
    }
    return arr;
}
// Change Qty for Add-On: reset line Subtotal and Whole Total
function ctlEventAddOnList_AddOnSelected(sender)
{
    // Check if have package selected first
    var packagestr = "";
    var txtpackage = document.getElementById("ctlEventPackage_txtPackageValues");
    if(txtpackage != null)
    {
        packagestr = txtpackage.value;
    }
    
    var qty = 0;
    if(isNaN(sender.value)) 
    {
        sender.value = "0";
        qty = 0;
    }
    else
    {
        qty = parseInt(sender.value);
    }
    
    var txtid = sender.id;
    var arr = ctlEventAddOnList_GetPropertiesByAddOnID(txtid);
    var price = arr[0], fed = arr[1], state = arr[2], limit = parseInt(arr[4]);
    // split attid
    var attid = "";
    var arr3 = arr[3].split("|");
    if(arr3.length == 2) attid = arr3[1];
    
    // Check if current attendee have package selected
    if(qty == 0 || (attid != "" && packagestr.indexOf(attid) > 0))
    {
        var currentTxtName = "", otherTxtName = "";
        if(txtid.indexOf("txtMySelf") > -1) 
        {
            currentTxtName = "txtMySelf";
            otherTxtName = "txtSpouse";
        }
        else 
        {
            currentTxtName = "txtSpouse";
            otherTxtName = "txtMySelf";
        }

        var otherqty = 0;
        var other = document.getElementById(txtid.replace(currentTxtName,otherTxtName));
        if(other != null)
        {
            otherqty = parseInt(other.value);
            limit = limit - otherqty;
        }
        
        // check if over limit
        if(limit >= 0 && qty > limit)
        {
            alert(addonoverlimitalert + (limit + otherqty).toString());
            qty = limit;
            sender.value = limit.toString();
        }

        var total = document.getElementById(txtid.replace(currentTxtName,"lblSubTotal"));
        if(total!=null)
        {
            var linetotal = price * qty;
            if(other!=null && parseInt(other.value) > parseInt(0))
            {
            
                arr = ctlEventAddOnList_GetPropertiesByAddOnID(other.id);
                linetotal = parseFloat(linetotal) + parseFloat(arr[0]) * parseInt(other.value);
            }
        
            // set total for current line
            total.innerHTML = FormatCurrency(linetotal, currencymark);

            // Check total for all packages and add-ons 
            ctlEventPackage_SetTotal();
        }
    }
    else
    {
        alert(addonorequirepacakge);
        sender.value = "0";
    }        
}

// Set suttotal for each packages and add-ons
// Set Subtotal, FedTaxAmount, StateTaxAmount, GrandTotal for all packages and Add-Ons
function ctlEventPackage_SetTotal()
{
    var arr = null;
    var subtotal = 0, statetotal = 0, fedtotal = 0;
    var qty = 0, total;
    // Loop packages and addons to get total
    var lbltotal = null;
    var myself, spouse;
    var alltxt = document.getElementsByTagName("input");
    
    var addonids = "", packageids = "";
    for(var i=0;i<alltxt.length;i++)
    {
        if(alltxt[i].id.indexOf("chkMySelf") > -1)
        {
            // Packages
            myself = document.getElementById(alltxt[i].id);
            spouse = document.getElementById(alltxt[i].id.replace("chkMySelf","chkSpouse"));
            lbltotal = document.getElementById(alltxt[i].id.replace("chkMySelf","lblSubTotal"));
            
            total = 0;
            if(myself!=null && myself.checked)
            {
                arr = ctlEventPackageList_GetPropertiesByPackageID(myself.id);
                total = parseFloat(arr[0]);
                subtotal = parseFloat(subtotal) + parseFloat(arr[0]);
                fedtotal = parseFloat(fedtotal) + parseFloat(arr[1]);   
                statetotal = parseFloat(statetotal) + parseFloat(arr[2]);
                
                packageids = packageids + "|||" + arr[4];
            }
            if(spouse!=null && spouse.checked)
            {
                arr = ctlEventPackageList_GetPropertiesByPackageID(spouse.id);
                total = parseFloat(total) + parseFloat(arr[0]);
                subtotal = parseFloat(subtotal) + parseFloat(arr[0]);

                if(parseInt(arr[3]) == 1)
                {
                    fedtotal = parseFloat(fedtotal) + parseFloat(arr[1]);   
                    statetotal = parseFloat(statetotal) + parseFloat(arr[2]);
                }
                packageids = packageids + "|||" + arr[4];
            }
            
            // Set Line Total
            if(lbltotal != null) lbltotal.innerHTML = FormatCurrency(total, currencymark);
        }
        else if(alltxt[i].id.indexOf("txtMySelf") > -1)
        {
            // Add-Ons
            
            // MySelf
            qty = parseInt(alltxt[i].value);
            if(qty >0)
            {
                arr = ctlEventAddOnList_GetPropertiesByAddOnID(alltxt[i].id);
                subtotal = parseFloat(subtotal) + qty * parseFloat(arr[0]);
                fedtotal = parseFloat(fedtotal) + qty * parseFloat(arr[1]);                       
                statetotal = parseFloat(statetotal) + qty * parseFloat(arr[2]);
                
                addonids = addonids + "|||" + arr[3] + "|" + qty;
            }
            
            // Spouse
            spouse = document.getElementById(alltxt[i].id.replace("txtMySelf","txtSpouse"));
            if(spouse!=null && parseInt(spouse.value) > parseInt(0))
            {
                qty = parseInt(spouse.value);
                arr = ctlEventAddOnList_GetPropertiesByAddOnID(spouse.id);
                
                subtotal = parseFloat(subtotal) + qty * parseFloat(arr[0]);
                fedtotal = parseFloat(fedtotal) + qty * parseFloat(arr[1]);                       
                statetotal = parseFloat(statetotal) + qty * parseFloat(arr[2]);
                
                addonids = addonids + "|||" + arr[3] + "|" + qty;
            }
        }
    }

    var display = "";
    if(parseFloat(subtotal) > parseFloat(0))
    {
        display = "visible";
        
        var lblSubTotal = document.getElementById(subtotalid);
        if(lblSubTotal!=null)lblSubTotal.innerHTML = FormatCurrency(subtotal, currencymark);
    }
    else
    {
        display = "hidden";
    }
    if(divSubTotal != null) divSubTotal.style.visibility = display;
    
    if(parseFloat(statetotal) > parseFloat(0))
    {
        display = "visible";
        
        var lblStateTax = document.getElementById(statetotalid);
        if(lblStateTax!=null) lblStateTax.innerHTML = FormatCurrency(statetotal, currencymark);
    }
    else
    {
        display = "hidden";
    }
    if(divState != null) divState.style.visibility = display;
    
    if(parseFloat(fedtotal) > parseFloat(0))
    {
        display = "visible";
        
        var lblFedTax = document.getElementById(fedtotalid);
        if(lblFedTax!=null) lblFedTax.innderHTML = FormatCurrency(fedtotal, currencymark);
    }
    else
        display = "hidden";
    if(divFed != null) divFed.style.visibility = "hidden";
    
    var lblGrandTotal = document.getElementById(grandtotalid);
    if(lblGrandTotal!=null) lblGrandTotal.innerHTML = FormatCurrency(subtotal + statetotal + fedtotal, currencymark);
    
    // update postback valus
    txtPackageValues.value = packageids;
    txtAddOnValues.value = addonids;    
}

// Client Side get round function
function GetRound(number)
{
    if(isNaN(number)) number = 0;
    return (Math.round(number * 100)/100).toFixed(2);
}
function FormatCurrency(number, mark)
{
    if(!isNaN(number))
        return mark + GetRound(number);
    else return mark + "0.00";
}
// Script for ctlEventPackage End //

function findPos(obj)
{
    this.X = obj.offsetLeft;
    this.Y = obj.offsetTop;
    while(obj.offsetParent)
    {
        this.X=this.X+obj.offsetParent.offsetLeft;
        this.Y=this.Y+obj.offsetParent.offsetTop;
        if(obj.offsetParent==null){break}
        else{obj=obj.offsetParent;}
    }
    return this;
}
