﻿if (typeof (DD_belatedPNG) != undefined) {
    // Add selectors for elements which need a background png fix
    var png_fixes = [
     'img',
     'div.message',
     'div.message p'
    ];
    DD_belatedPNG.fix(png_fixes.join(','));
}

function ajax_Initialise() {
    var xmlHttp;

    try {
        // Firefox, Opera 8.0+, Safari    
        xmlHttp = new XMLHttpRequest();
    }

    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }

    return xmlHttp;
}


function ajaxUpdateShoppingBasket(IDDataContent_19_product, quantity, profit_this_item_div) {
    var response, xmlHttp, hasUpdated;
    var bonus_boxes, profit_this_item, total_boxes, total_cost, total_profit;
    $.ajax({
        type: "POST",
        url: "/ajax_UpdateShoppingBasket.aspx?IDDataContent_19_product=" + IDDataContent_19_product + "&quantity=" + quantity + "&shopper=" + document.aspnetForm.shopper.value,
        data: "",//"IDDataContent_19_product=" + IDDataContent_19_product + "&quantity=" + quantity + "&shopper=" + document.aspnetForm.shopper.value,
        success: function (html) {
            var qs = new QueryString(html);
            bonus_boxes = qs.get("bonus_boxes");
            total_boxes = qs.get("total_boxes");
            total_profit = qs.get("total_profit");
            total_cost = qs.get("total_cost");
            profit_this_item = qs.get("profit_this_item");
            document.getElementById('bonus_boxes').innerHTML = "Bonus Boxes <span class=\"purple\">" + bonus_boxes + "</span>";
            document.getElementById('total_boxes').innerHTML = "Items in basket <span class=\"purple\">" + total_boxes + "</span>";
            document.getElementById('total_boxes_1').innerHTML = total_boxes;
            document.getElementById('total_profit').innerHTML = '$' + parseFloat(total_profit).toFixed(2);
            document.getElementById('total_cost').innerHTML = '$' + parseFloat(total_cost).toFixed(2);
            document.getElementById(profit_this_item_div).innerHTML = '$' + parseFloat(profit_this_item).toFixed(2);
            document.getElementById('no_boxes').value = total_boxes;

            if (document.getElementById('bonus_boxes_base')) {
                document.getElementById('bonus_boxes_base').innerHTML = bonus_boxes;
            }
        }
    });
}


//    xmlHttp = ajax_Initialise();

//    xmlHttp.open("GET", "/ajax_UpdateShoppingBasket.aspx?IDDataContent_19_product=" + IDDataContent_19_product + "&quantity=" + quantity + "&shopper=" + document.aspnetForm.shopper.value, true);

//    xmlHttp.onreadystatechange = function () {
//      if (xmlHttp.readyState == 4) {
//        response = xmlHttp.responseText;
//        var qs = new QueryString(response);

//        bonus_boxes = qs.get("bonus_boxes");
//        total_boxes = qs.get("total_boxes");
//        total_profit = qs.get("total_profit");
//        total_cost = qs.get("total_cost");
//        profit_this_item = qs.get("profit_this_item");

//        document.getElementById('bonus_boxes').innerHTML = "Bonus Boxes <span class=\"purple\">" + bonus_boxes + "</span>";
//        document.getElementById('total_boxes').innerHTML = "Items in basket <span class=\"purple\">" + total_boxes + "</span>";
//        document.getElementById('total_boxes_1').innerHTML = total_boxes;
//        document.getElementById('total_profit').innerHTML = '$' + parseFloat(total_profit).toFixed(2);
//        document.getElementById('total_cost').innerHTML = '$' + parseFloat(total_cost).toFixed(2);
//        document.getElementById(profit_this_item_div).innerHTML = '$' + parseFloat(profit_this_item).toFixed(2);
//        document.getElementById('no_boxes').value = total_boxes;

//        if (document.getElementById('bonus_boxes_base')) {
//          document.getElementById('bonus_boxes_base').innerHTML = bonus_boxes;
//        }

//        //alert('success - finished!');
//      }
//else {
//var str = "/ajax_UpdateShoppingBasket.aspx?IDDataContent_19_product=" + IDDataContent_19_product + "&quantity=" + quantity + "&shopper=" + document.aspnetForm.shopper.value;
//alert(str);

//document.write("<a href=\"" + str + "\">The link " + str + "</a>")
//alert('fail');
//}

// }
//xmlHttp.send(null);
//}

function ajaxUpdateShoppingBasketProduct(IDDataContent_19_product, quantity) {
    var response, xmlHttp;
    var bonus_boxes, total_boxes;

    xmlHttp = ajax_Initialise();
    //alert(              "/ajax_UpdateShoppingBasket.aspx?IDDataContent_19_product=" + IDDataContent_19_product + "&quantity=" + quantity + "&shopper=" + document.cadbury.shopper.value);
    xmlHttp.open("GET", "/ajax_UpdateShoppingBasket.aspx?IDDataContent_19_product=" + IDDataContent_19_product + "&quantity=" + quantity + "&shopper=" + document.cadbury.shopper.value, true);

    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState == 4) {
            response = xmlHttp.responseText;

            var qs = new QueryString(response);

            bonus_boxes = qs.get("bonus_boxes");
            total_boxes = qs.get("total_boxes");

            document.getElementById('bonus_boxes').innerHTML = "Bonus Boxes <span class=\"purple\">" + bonus_boxes + "</span>";
            document.getElementById('total_boxes').innerHTML = "Items in basket <span class=\"purple\">" + total_boxes + "</span>";

        }
    }
    xmlHttp.send(null);

}

function Alerts(quantity) {
    if (quantity == 0)
        alert('This item has been removed from your order');
    else if (quantity == 1)
        alert('1 box has been added to your order');
    else if (quantity >= 1)
        alert(parseInt(quantity) + ' boxes have been added to your order');
}

function ShowQuantity(quantity) {
    var html;
    if (quantity == 0) {
        html = "<p>This item has been removed from your order</p>";
    }
    else {
        $(".message").addClass("thanks");
        var text = quantity > 1 ? "boxes" : "box";
        html = "<p><strong class=\"amount\">" + quantity + "</strong>" + text + " have been added to your order</p>";
    }
    showModal(html);
}

function showModal(html) {
    var modal = $(".message");
    modal.html(html);
    modal.css("top", ($(window).height() - modal.height()) / 2 + $(window).scrollTop() + "px");
    modal.fadeIn('slow');
    setTimeout(function () {
        modal.fadeOut(0);
        if (modal.hasClass("thanks"))
            modal.removeClass("thanks");
    }, 2000);
}

function CheckCookie(form) {
    form.cookieexists.value = 'true';
}

function CheckLength(ok, form, name, length) {
    if (ok) {
        if (form.value.length < length) {
            alert(name);
            form.focus();
            ok = false;
        }
    }
    return ok;
}


function ContinueYourOrder(form) {
    form.submit();
}


function GetValue(input) {
    var value;

    value = -1;

    if (!isInteger(input.value)) {
        showModal("<p>You must enter a number</p>");
        input.focus();
    }
    else {
        value = parseInt(input.value);

        if (value < 0) {
            showModal("<p>You must enter a number greater than 0</p>");
            input.focus();
        }
    }

    return value;
}

function OnAddToBasket(form, input, IDDataContent_19_product) {
    var value;

    value = GetValue(input);

    if (value != -1) {
        form.products_added_to_basket.value = IDDataContent_19_product + '=' + input.value;
        form.submit();
    }
}


function OnAttend(form) {
    if (form.receive_email.value == '') {
        form.receive_email.value = 'open';
        document.getElementById('details').style.display = 'block';
    }
    else {
        form.receive_email.value = '';
        document.getElementById('details').style.display = 'none';
    }
}


function OnChangeProductQuantityOrderForm(input, IDDataContent_19_product, profit_this_item) {
    var no, value;
    quantity = GetValue(input);

    //alert(1);

    if (quantity != -1) {
        ajaxUpdateShoppingBasket(IDDataContent_19_product, parseInt(quantity), profit_this_item);
        //ShowQuantity(quantity);   09/2011
    }
}


function OnChangeProductQuantityProducts(input, IDDataContent_19_product) {
    var value;

    quantity = GetValue(input);

    if (quantity != -1) {
        ajaxUpdateShoppingBasketProduct(IDDataContent_19_product, parseInt(quantity));
        //ShowQuantity(quantity); 09/2011
    }
}


function OnCompetition(form) {
    var ok;

    ok = true;

    ok = OnTextBox(ok, form.first, "First Name", 100, false);
    ok = OnTextBox(ok, form.last, "Last Name", 100, false);
    ok = OnEmail(ok, form.email);
    ok = OnTextBox(ok, form.details, "Details", 10000, false);
    ok = OnCheckbox(ok, form.terms, "You must accept the competition Terms & Conditions");

    return ok;
}


function OnChangeDetails(form) {
    var ok;

    ok = true;

    if (!form.ctl00$ContentPlaceHolder1$unsubscribe.checked) {
        ok = OnTextBox(ok, form.c10, "First Name", 100, false);
        //ok = OnTextBox(ok, form.c11, "Last Name", 100, false);
        ok = OnTextBox(ok, form.c22, "Username", 100, false);
        ok = OnTextBox(ok, form.c23, "Password", 100, false);
        ok = OnTextBox(ok, form.password1, "Confirm Password", 100, false);

        if (ok && form.c23.value.length < 6) {
            ok = false;
            alert('The password must be at least 6 characters long!');
            form.c23.focus();
        }

        ok = OnTextBox(ok, form.password1, "Password", 100, false);

        if (ok && form.c23.value != form.password1.value) {
            ok = false;
            alert('The passwords do not match');
            form.c23.focus();
        }
        ok = OnTextBox(ok, form.c3, "Organisation Name", 100, false);
        ok = OnSelect(ok, form.c5, "Organisation Type")
        ok = OnEmail(ok, form.c14);
        ok = OnCheckbox(ok, form.terms, "You must accept the Terms & Conditions and Privacy Policy");
    }

    return ok;
}


function OnJoin(form) {
    var ok;
    ok = true;

    ok = OnTextBox(ok, form.c10, "First Name", 100, false);
    //ok = OnTextBox(ok, form.c11, "Last Name", 100, false);
    ok = OnTextBox(ok, form.c22, "Username", 100, false);
    ok = OnTextBox(ok, form.c23, "Password", 100, false);

    ok = OnTextBox(ok, form.password1, "Confirm Password", 100, false);

    if (ok && form.c23.value.length < 6) {
        ok = false;
        alert('The password must be at least 6 characters long!');
        form.c23.focus();
    }

    if (ok && form.c23.value != form.password1.value) {
        ok = false;
        alert('The passwords do not match');
        form.c23.focus();
    }

    ok = OnTextBox(ok, form.c3, "Organisation Name", 100, false);
    ok = OnSelect(ok, form.c5, "Organisation Type")
    ok = OnEmail(ok, form.c14);
    ok = OnCheckbox(ok, form.terms, "You must accept the Terms & Conditions and Privacy Policy");

    return ok;
}


function OnLogin(form) {
    var ok;

    ok = true;

    ok = OnTextBox(ok, form.username, "Email", 100, false);

    return ok;
}


function OnLoginJoin(form) {
    var ok;

    ok = true;

    ok = OnTextBox(ok, form.ctl00$ContentPlaceHolder1$txtUsername, "Username", 100, false);
    ok = OnTextBox(ok, form.ctl00$ContentPlaceHolder1$txtPassword, "Password", 100, false);

    return ok;
}



function OnOrderConfirmation(form) {
    var ok, date;

    ok = true;
    //ok = OnDate(ok, form.c1_day, form.c1_month, form.c1_year, "Date of Order");
    //ok = CompareDate(ok, form.c1_day, form.c1_month, form.c1_year, "Order Date cannot be in the past");
    ok = OnTextBox(ok, form.c3, "Organisation Name", 100, false);
    //ok = OnTextBox(ok, form.c4, "Alternate Organisation Name", 100, false);
    ok = OnSelect(ok, form.c5, "Organisation Type");
    ok = OnTextBox(ok, form.c6, "Street Address", 1000, false);
    ok = OnTextBox(ok, form.c7, "Suburb", 100, false);
    ok = OnDigit(ok, form.c8, "Postcode");
    ok = OnSelect(ok, form.c9, "State");
    ok = OnTextBox(ok, form.c10, "Primary Contact First Name", 100, false);
    ok = OnTextBox(ok, form.c11, "Primary Contact Second Name", 100, false);
    ok = OnTextBox(ok, form.c12, "Position", 100, false);

    ok = OnDigit(ok, form.c13, "Business hours phone number");
    ok = CheckLength(ok, form.c13, "Phone number has to be at least 8 digits long", 8);

    ok = OnEmail(ok, form.c14);
    ok = OnTextBox(ok, form.c15, "Secondary Contact First Name", 100, false)
    ok = OnTextBox(ok, form.c35, "Secondary Contact Last Name", 100, false)

    ok = OnDigit(ok, form.c16, "Secondary business hours phone number");
    ok = CheckLength(ok, form.c16, "Phone number has to be at least 8 digits long", 8);

    //    if (form.MailingList.checked) {
    //        CopyAddress(form);
    //    }

    ok = OnDate(ok, form.c18_day, form.c18_month, form.c18_year, "Date of Delivery")
    ok = OnSelect(ok, form.c19, "How did you hear about Cadbury Fundraiser");

    if (form.receive_email.value.length > 0) {
        ok = OnTextBox(ok, form.c22, "Username", 100, false);
        ok = OnTextBox(ok, form.c23, "Password", 100, false);
        if (form.c23.value != form.confirm.value && ok) {
            ok = false;
            alert('Password must be the same as confirm password!');
            form.c23.focus();
        }
        else if (!form.agree.checked && ok) {
            ok = false;
            alert('You must agree the Cadbury I confirm I have read and accepted the Cadbury PRIVACY POLICY and TERMS OF USE');
            form.agree.focus();
        }
    }

    if (ok) {
        //storeUser(form);
        form.action = 'ordering-details.aspx?submit_order_details=1';
        form.submit();
    }
}

//function CopyAddress(form) {
//    if (form.MailingList.checked) {
//        form.c17.value = form.c6.value;
//        form.c24.value = form.c7.value;
//        form.c25.value = form.c8.value;
//        form.c26.value = form.c9.value;
//    }
//    else {
//        form.c17.value = "";
//        form.c24.value = "";
//        form.c25.value = "";
//        form.c26.value = "";
//    }
//}

function OnPayment(form) {

    if (form.terms.checked == true)
        window.location = 'https://www.ippayments.com.au/portal/payment/index.aspx?aid=1481118';
    else
        alert('You must accept the Payment Terms!');

}

function OnDate(ok, day, month, year, name) {
    if (ok) {
        if (day.value.length > 0 || month.value.length > 0 || year.value.length > 0) {
            if (day.value.length > 0 && month.value.length > 0 && year.value.length > 0) {
                ok = CheckFuture(ok, day, month, year, "You cannot enter a past date")
            }
            else {
                ok = false;
                day.focus();
                alert(name + ' is not completed');
            }
        }
    }
    return ok;
}

function CheckFuture(ok, day, month, year, name) {
    var myDate = new Date()
    myDate.setFullYear(year.value, month.value - 1, day.value)
    var today = new Date()
    if (myDate < today) {
        ok = false;
        day.focus();
        alert(name);
    }
    return ok;
}

function OnOrderingDetails(form) {
    var isValid = false;
    if (CheckOrderQuantity(form)) {
        if (form.terms.checked == true) {
            // window.location = 'Ordering-Login';
            isValid = true;
        }
        else
        //alert('You cannot proceed as you have not read and accepted the Cadbury Fundraiser Purchase Terms!');
            showModal("<p>Please tick the checkbox and accept the Cadbury Fundraiser Purchase Terms to continue.</p>");
    }
    else
    //alert('You cannot proceed as you have not added at least 15 boxes or 1 10kg Block box to your order!');
        showModal("<p>Please add at least 15 boxes or 1&nbsp;&nbsp;10kg Block box or 1&nbsp;&nbsp;4.5Kg Toblerone to your order.</p>");
    return isValid;
}


function CheckOrderQuantity(form) {
    var ok;
    ok = false;
    var total_boxes;

    /*
    xmlHttp = ajax_Initialise();

    xmlHttp.open("GET", "/ajax_UpdateShoppingBasket.aspx?noupdate=true", true);
    
    xmlHttp.onreadystatechange = function () {
    if (xmlHttp.readyState == 4) {
    response = xmlHttp.responseText;
    var qs = new QueryString(response);
    alert(response);
    total_boxes = qs.get("total_boxes");
       
    if (document.getElementById('bonus_boxes_base')) {
    document.getElementById('bonus_boxes_base').innerHTML = bonus_boxes;
    }
    }
    }
    xmlHttp.send(null);
    //alert(total_boxes);
    */
    //if (form.no_boxes.value >= 15 || form.product_34.value >= 1 || form.product_37.value >= 2 || (form.product_37.value == 1 && form.no_boxes.value >= 14))
    if (form.no_boxes.value >= 15 || form.product_51.value >= 1 || form.product_64.value >=1)
    //if (form.no_boxes.value >= 15 || form.product_51.value >= 1)
        ok = true;
    return ok;
}

function OnReminder(form) {
    var ok;

    ok = true;

    //validation required here...
    alert("OnReminder validation required");

    if (ok)
        form.submit();
}

function OnUnsubscribe(form) {
    var ok;

    ok = true;
    ok = OnEmail(ok, form.email);

    if (ok)
        form.submit();
}

function OnSubmitFeedback(form) {
    var ok;

    ok = true;
    ok = OnTextBox(ok, form.first, "First Name", 100, false);
    ok = OnEmail(ok, form.email);
    ok = OnTextBox(ok, form.comment, "Comment", 4000, false);

    if (ok) {
        if (form.address.value.length > 225) {
            alert('You can only enter 225 characters for the address!');
            ok = false;
        }
    }


    if (ok) {
        if (form.comment.value.length > 1400) {
            alert('You can only enter 1400 characters for the question or comment!');
            ok = false;
        }
    }

    //  if (ok)
    //  {
    //    if(form.comment.value.match(/[^a-zA-Z0-9~'!@#$%^\*\(\)_\+:\?;,\.\/"'\[\]\\\{\}\|\r\n]/g) != null)
    //    {
    //      alert('You can only enter the characters a-z, A-Z, 0-9, ~, \', !, @, # $,%,^,*, (, ), _, +, :, ?, ;, ., \, /," [, ], {, }, | in the question or comment!');
    //      ok = false;
    //    }
    //  }  

    if (form.terms.checked == false && ok) {
        ok = false;
        alert('You must accept the Terms of Use of this website.');
    }



    return ok;
}


function setCookie(c_name, value, expiredays) {
    var exdate = new Date()
    exdate.setDate(exdate.getDate() + expiredays)
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
}

function storeUser(form) {
    setCookie("cadbury_c2", form.c2.value, 2)
    setCookie("cadbury_c3", form.c3.value, 2)
    setCookie("cadbury_c4", form.c4.value, 2)
    setCookie("cadbury_c5", form.c5.value, 2)
    setCookie("cadbury_c6", form.c6.value, 2)
    setCookie("cadbury_c7", form.c7.value, 2)
    setCookie("cadbury_c8", form.c8.value, 2)
    setCookie("cadbury_c9", form.c9.value, 2)
    setCookie("cadbury_c10", form.c10.value, 2)
    setCookie("cadbury_c11", form.c11.value, 2)
    setCookie("cadbury_c12", form.c12.value, 2)
    setCookie("cadbury_c13", form.c13.value, 2)
    setCookie("cadbury_c14", form.c14.value, 2)
    setCookie("cadbury_c15", form.c15.value, 2)
    setCookie("cadbury_c16", form.c16.value, 2)
    setCookie("cadbury_c17", form.c17.value, 2)
    setCookie("cadbury_c19", form.c19.value, 2)
}

function stripSpaces(element) {
    element.value = element.value.replace(/[^0-9]/g, "");
}
