﻿

function UpdateCartWithAJAX(astrURL, astrFieldID, astrFieldType, astrMode, astrQuantity, aintInventoryPerOrder, astrSplashFlag, aintPageMode) {
    var xmlHttp, strResponseHTML, strHasItemInCart, intRemovedValue, blnRedirect;
    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;
            }
        }
    }
    if (astrSplashFlag == '1')
    { intRemovedValue = 1; }
    else
    { intRemovedValue = 0; }
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            if (document.getElementById('divFieldResult' + astrFieldID)) {
                document.getElementById('divFieldResult' + astrFieldID).innerHTML = '';
            }
            strResponseHTML = escape(xmlHttp.responseText);
            strHasItemInCart = strResponseHTML.substr(0, 1)
            strHasError = strResponseHTML.substr(1, 1)
            strResponseHTML = strResponseHTML.substr(2, strResponseHTML.length)


            blnRedirect = false;
            //astrSplashFlag = 0 = redirect
            //astrSplashFlag = 1 = stay
            if (aintPageMode == '0' & astrSplashFlag == '1' && strHasError == '0' && (astrMode == 'UPDATE' || astrMode == 'ADD')) {
                blnRedirect = true;
            }
            if (astrSplashFlag == '0' && strHasError == '0' && (astrMode == 'REMOVE')) {
                blnRedirect = true;
            }
            if (document.getElementById('divFieldResult' + astrFieldID)) {
                if (blnRedirect == false) {
                    document.getElementById('divFieldResult' + astrFieldID).style.display = '';
                    document.getElementById('divFieldResult' + astrFieldID).innerHTML = unescape(strResponseHTML);
                    if (astrQuantity == '0' && strHasError == '0') {
                        if (astrFieldType == 'CHECKBOX') {
                            if (intRemovedValue == 1)
                            { document.getElementById('SF' + astrFieldID).checked = true; }
                            else
                            { document.getElementById('SF' + astrFieldID).checked = false; }
                        }
                        if (astrFieldType == 'QUANTITY') {
                            document.getElementById('SF' + astrFieldID).value = intRemovedValue;
                        }
                        document.getElementById('AddButton' + astrFieldID).style.display = '';
                        document.getElementById('UpdateButton' + astrFieldID).style.display = 'none';
                        document.getElementById('RemoveButton' + astrFieldID).style.display = 'none';
                    }
                    if (strHasItemInCart == '1' && astrSplashFlag == '1' && strHasError == '0') {
                        document.getElementById('divAddedToCart' + astrFieldID).style.display = '';
                    }
                    else {
                        document.getElementById('divAddedToCart' + astrFieldID).style.display = 'none';
                    }
                    if (astrMode == 'ADD' && strHasError == '0') {
                        if (astrFieldType == 'CHECKBOX') {
                            document.getElementById('SF' + astrFieldID).checked = true;
                        }
                        if (astrFieldType == 'QUANTITY') {
                            document.getElementById('SF' + astrFieldID).value = astrQuantity;
                        }
                        document.getElementById('AddButton' + astrFieldID).style.display = 'none';
                        if (aintInventoryPerOrder == 1)
                        { document.getElementById('UpdateButton' + astrFieldID).style.display = 'none'; }
                        else
                        { document.getElementById('UpdateButton' + astrFieldID).style.display = ''; }
                        document.getElementById('RemoveButton' + astrFieldID).style.display = '';
                    }
                    if (astrMode == 'UPDATE' && strHasError == '0') {
                        if (document.getElementById('SF' + astrFieldID))
                        { document.getElementById('SF' + astrFieldID).value = astrQuantity; }

                        document.getElementById('AddButton' + astrFieldID).style.display = 'none';
                        if (aintInventoryPerOrder == 1)
                        { document.getElementById('UpdateButton' + astrFieldID).style.display = 'none'; }
                        else
                        { document.getElementById('UpdateButton' + astrFieldID).style.display = ''; }
                        document.getElementById('RemoveButton' + astrFieldID).style.display = '';
                    }
                }
            }
            if (blnRedirect == true) {
                HandleRedirectAfterCartChange()
            }
        }
    }
    xmlHttp.open('GET', astrURL + '&random=' + Math.floor(Math.random() * 2000000), true);
    xmlHttp.send(null);
}

function HandleUpdate(aintFieldID, aintInventoryPerOrder, astrFieldType, astrSplashFlag, aintPageMode, astrContestID) {
    var strQuantity;

    if (document.getElementById('SF' + aintFieldID)) {
        strQuantity = document.getElementById('SF' + aintFieldID).value;
    }
    if (strQuantity == '') {
        strQuantity = '1';
    }

    UpdateCartWithAJAX('AJAXCart.aspx?mode=UPDATE&Quantity=' + strQuantity + '&FieldID=' + aintFieldID + '&contestid=' + astrContestID, aintFieldID, astrFieldType, 'UPDATE', strQuantity, aintInventoryPerOrder, astrSplashFlag, aintPageMode);

}

function AddDropdownOptionToCart(astrMainDealID) {
    var strOptionValue, intContinue;
    intContinue = 1;
    if (document.getElementById('JoinDealDropdown' + astrMainDealID)) {
        strOptionValue = document.getElementById('JoinDealDropdown' + astrMainDealID).options[document.getElementById('JoinDealDropdown' + astrMainDealID).selectedIndex].value;
        if (strOptionValue == 'SELECTONE') {
            alert('You must select an option above.');
            intContinue = 0;
        }
        if (strOptionValue == 'SOLDOUT') {
            alert('The item you have selected is no longer available.');
            intContinue = 0;
        }
        if (strOptionValue == 'COMINGSOON') {
            alert('The item you have selected is not yet available.');
            intContinue = 0;
        }
        if (strOptionValue == 'DEALOVER') {
            alert('The item you have selected is no longer available.');
            intContinue = 0;
        }
        
        if (intContinue == 1)
        { HandleAdd(astrMainDealID, document.getElementById('JoinDealDropdown' + astrMainDealID).options[document.getElementById('JoinDealDropdown' + astrMainDealID).selectedIndex].value, 0, 'QUANTITY', '1', 0); }
    }
} 

function HandleAdd(aintMainDeal, aintFieldID, aintInventoryPerOrder, astrFieldType, astrSplashFlag, aintPageMode, astrContestID) {
    var strQuantity;
    if (document.getElementById('SF' + aintFieldID)) {
        strQuantity = document.getElementById('SF' + aintFieldID).value;
    }
    if ((isNaN(strQuantity) == true) || strQuantity == '0') {
        strQuantity = '1';
    }

    UpdateCartWithAJAX('AJAXCart.aspx?mode=ADD&Quantity=' + strQuantity + '&FieldID=' + aintFieldID + '&contestid=' + astrContestID, aintMainDeal, astrFieldType, 'ADD', strQuantity, aintInventoryPerOrder, astrSplashFlag, aintPageMode);

}
function HandleRemove(aintFieldID, aintInventoryPerOrder, astrFieldType, astrSplashFlag, aintPageMode, astrContestID) {
    var strQuantity;
    strQuantity = '0';
    UpdateCartWithAJAX('AJAXCart.aspx?mode=REMOVE&Quantity=' + strQuantity + '&FieldID=' + aintFieldID + '&contestid=' + astrContestID, aintFieldID, astrFieldType, 'REMOVE', strQuantity, aintInventoryPerOrder, astrSplashFlag, aintPageMode);

}
