﻿var cs = 'ctl00_ContentPlaceHolder1_';

function checkTime(obj) {
    var val = obj.value;

    if (val != "") {
        if (val.indexOf(".") < 0) {
            window.alert("Your time does not contain a '.'\r\n\r\nThe format should be H.MM\r\n\r\nPlease check");
        }

        if (val.length < 4 || val.length > 5) {
            window.alert("Your time is not of the correct format.\r\n\r\nThe format should be H.MM\r\n\r\nPlease check");
        }
    }    
}


function RemoveTooltip(obj) {
    obj.title = obj.value;
}

function GetOptionsDeposit() {
    var qty1 = gf("Option1Qty");
    var qty2 = gf("Option2Qty");
    var qty3 = gf("Option3Qty");
    var qty4 = gf("Option4Qty");
    var item1 = gf("Option1");
    var item2 = gf("Option2");
    var item3 = gf("Option3");
    var item4 = gf("Option4");

    var medal1 = gf("Medal1");
    var medal2 = gf("Medal2");
    var medal3 = gf("Medal3");
    var medal4 = gf("Medal4");

    var s = item1.value + "@" + qty1.value + "#";
    s = s + item2.value + "@" + qty2.value + "#";
    s = s + item3.value + "@" + qty3.value + "#";
    s = s + item4.value + "@" + qty4.value + "?";
    if (medal1 != null) {
        s = s + medal1.value + "#";
        s = s + medal2.value + "#";
        s = s + medal3.value + "#";
        s = s + medal4.value;
    }


    // call the webmethod
    PageMethods.GetOptionsDeposit(s, OnCallGetOptionsDepositComplete,OnCallGetOptionsDepositError, "OptionsDeposit");
}

// Callback function on complete
// First argument is always "result" if server side code returns void 
// then this value will be null
// Second argument is usercontext control pass at the time of call
// Third argument is methodName (server side function name) 
function OnCallGetOptionsDepositComplete(result, txtresult, methodName) {
    smf(txtresult, result);
    RecalcPrice();
}

// Callback function on error
// First argument is always "error" if server side code throws any exception
// Second argument is usercontext control pass at the time of call
// Third argument is methodName (server side function name) 
// In this example the methodName will be "Sum"
function OnCallGetOptionsDepositError(error, userContext, methodName) {
    if (error !== null) {}
}


function GetDateContractOut(val) {
    if (val != "") {
        var behavior = $find('DpDateContractOut');
        if (behavior)
            behavior.populate(val);
    } 
}

function GetEventDate(val, obj) {
    var behavior = $find('DpEventDate');
    if (behavior)
        behavior.populate(val);
}

function GetOptionCost(val, obj) {

    if (val == "16") { //"Laser & Strobe Light-show Upgrade"
        sdd("DiscoSize", "Laser & Strobe Light-show");
    } 
    
    if (val == " -- Not selected --") {
        sdd("Option" + obj + "Qty", "--");
        RecalcOptions();
    }
    

    
    var behavior = $find('dp' + obj);
    if (behavior)
        behavior.populate(val);

    // set focus to the cost box so that when the user moves away it recalcs
    var f = gf("Option" + obj + "Cost");
    f.focus();
}

function SetFloorLevel(val) {

    var floorLevel = val.value;

    if (floorLevel == "Upstairs" || floorLevel == "Downstairs") {
        smf("FloorCharge", "10");
    }
    else {
        smf("FloorCharge", "0");    
    }

    RecalcPrice();
}

function RecalcOptions() {

    var qty1 = gf("Option1Qty");
    var qty2 = gf("Option2Qty");
    var qty3 = gf("Option3Qty");
    var qty4 = gf("Option4Qty");
    var options = 0;

    if (qty1.value != "--") {
        var price1 = gmf("Option1Cost");
        options = parseFloat(options) + (qty1.value * price1);
    }
    if (qty2.value != "--") {
        var price2 = gmf("Option2Cost");
        options = parseFloat(options) + (qty2.value * price2);
    }
    if (qty3.value != "--") {
        var price3 = gmf("Option3Cost");
        options = parseFloat(options) + (qty3.value * price3);
    }
    if (qty4.value != "--") {
        var price4 = gmf("Option4Cost");
        options = parseFloat(options) + (qty4.value * price4);
    }

    var medal1 = gf("Medal1");
    var medal2 = gf("Medal2");
    var medal3 = gf("Medal3");
    var medal4 = gf("Medal4");

    var medal = 0;
    var count = 0;

    if (medal1 != null) {
        if (medal1.value != " -- Not selected --") count++;
        if (medal2.value != " -- Not selected --") count++;
        if (medal3.value != " -- Not selected --") count++;
        if (medal4.value != " -- Not selected --") count++;

        if (count > 1)
            medal = ((parseInt(count) - 1) * 5);
    }

    // take the options figure and add it to that
    smf("Options", parseFloat(options) + parseFloat(medal));

    GetOptionsDeposit();
}



function DiscountReason_OnClick() {

    var dr = document.getElementById(cs + "DiscountReason");

    if (dr != null) {
        var discount = gmf("Discount");
        if (dr.value == "Customer Questionnaire") {
            smf("Discount", "10"); 
        } else if (dr.value == "Venue Confirmation"){
            smf("Discount", "20"); 
        }
    }

    RecalcPrice();
}

function RecalcPrice() {
    var basicPrice = gmf("BasicPrice");
    var options = gmf("Options");
    var floorCharge = gmf("FloorCharge");
    var discount = gmf("Discount");

    var deposit1 = gmf("Amount1");
    var deposit2 = gmf("Amount2");
    
    var discountOffDeposit = document.getElementById(cs + "DiscountOffDeposit").checked;    
    var isCorporate = document.getElementById(cs + "IsCorporate").checked;
    
    // work out the subtotal
    var subTotal = parseFloat(basicPrice) + parseFloat(options) + parseFloat( floorCharge) - parseFloat( discount);

    // if it's corporate then add the vat
    var vat = 0;
    if (isCorporate)
        vat = (subTotal * 0.15).toFixed(2);

    // now make the total
    var totalCost = (parseFloat(subTotal) + parseFloat(vat)).toFixed(2);

    // now the deposit column
    var basicDeposit = parseFloat(gmf("BasicDeposit"));
    var optionsDeposit = parseFloat(gmf("OptionsDeposit"));

    var totalDeposit = basicDeposit + optionsDeposit;

    if (discountOffDeposit)    
        totalDeposit = totalDeposit - parseFloat(discount);
    
    // now the balance to pays
    var balance1 = totalCost - deposit1;
    var balance2 = totalCost - deposit1 - deposit2;
    
    // set the fields on the form

    smf("BasicPrice", basicPrice);
    smf("Options", options);
    smf("FloorCharge", floorCharge);
    smf("Discount", discount);
    smf("SubTotal", subTotal);
    smf("Vat", vat);

    smf("Total", totalCost);
    
    smf("BasicDeposit", basicDeposit);
    smf("OptionsDeposit", optionsDeposit);
    smf("TotalDeposit", totalDeposit);

    smf("Balance1", balance1);

    if (balance2 < balance1)
        smf("Balance2", balance2);
}

function gf(str){
    var f = document.getElementById(cs + str);
    return f;
}

// set money field
function smf(str, val) {

    var f = document.getElementById(cs + str);

    if (f == null) return;
    if (f.value != null)
        f.value = "£" + parseFloat(val).toFixed(2);

    if (f.textContent != null)
        f.textContent = "£" + parseFloat(val).toFixed(2);

    if (f.defaultValue != null)
        f.defaultValue = "£" + parseFloat(val).toFixed(2);

    if (f.innerText != null)
        f.innerText = "£" + parseFloat(val).toFixed(2);
}

// set field
function sf(str, val) {

    var f = document.getElementById(cs + str);
    
    if (f.value != null)
        f.value = val;
    
    if(f.textContent != null)
        f.textContent = val;

    if (f.defaultValue != null)
        f.defaultValue = val;

    if (f.innerText != null)
        f.innerText = val;
}

// set dd
function sdd(str, val) {

    var f = document.getElementById(cs + str);
    
    if (f.value != null)
        f.value = val;
    
    if(f.textContent != null)
        f.textContent = val;

    if (f.defaultValue != null)
        f.defaultValue = val;

}

// get money field to decimal
function gmf(str) {

    var f = document.getElementById(cs + str);

    if (f != null && f.value != "") {
        f = f.value.replace('£', '');
        return parseFloat(f).toFixed(2);
    } else
        return 0;
}


function ShowEmailLinks(val) {

    var o = document.getElementById(cs + "EmailLink");
    if (val.value == "")
        o.style.display = "none";
    else
        o.style.display = "block";
}

// format currency
function fc(o) {
    var num = o.value;
    num = num.toString().replace(/\£|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
        num.substring(num.length - (4 * i + 3));
    o.value = (((sign) ? '' : '-') + '£' + num + '.' + cents);
}

function fd(o) {
    FormatDateMask(o);
}

function FormatDateMask(o) {
    if (o.value.length == 8 || o.value.length == 6) {
        var p1 = o.value.substring(0, 2);
        var p2 = o.value.substring(2, 4);
        var p3 = o.value.substring(4);

        try {
            if (o.value.length == 8) {
                if (p3.substring(0,2) == "20")
                    o.value = p1 + "/" + p2 + "/" + p3;
                else
                    window.alert("Please input date in the following format:\r\n\r\nddmmyy");
            }
            else if (o.value.length == 6) {
                o.value = p1 + "/" + p2 + "/20" + p3;
            }
            else {
                window.alert("Please input date in the following format:\r\n\r\nddmmyy");
            }
        }
        catch(err)
        {
            window.alert("Please input date in the following format:\r\n\r\nddmmyy");
        }
         
    }
}

function SetContactOnDay(val)
{
    var cod = document.getElementById(cs + "HomeMobile2ContactName");
    var title = document.getElementById(cs + "CTitle");
    var firstname = document.getElementById(cs + "FirstName");
    var lastname = document.getElementById(cs + "LastName");
    
    if (val.checked)
    {
        var newVal = title.value + ' ' + firstname.value + ' ' + lastname.value;
        cod.value = newVal;
    }
}

function FormatPhone(val)
{
    if (val.value != "")
    {
        var length = val.value.length;

        if (length > 7)
        {
            var endOf = val.value.substring(length - 7);
            var startOf = val.value.substring(0, length-7);

            val.value = "(" + startOf.trim() + ") " + endOf.trim();
        }
    }    
}

function CloseReceipt()
{
    var sendreceipt = document.getElementById("SendReceipt");
    if (sendreceipt != null)
        sendreceipt.style.display = "none";
        
    var sendinvoice = document.getElementById("SendInvoice");    
    if (sendinvoice != null)
        sendinvoice.style.display = "block";
}

function UpdateInvites(val)
{
    var invites = document.getElementById(cs + "Invites");
    invites.value = val.value;                 
}

