﻿function ParseNumber(field)
{
    var nNum = null;

    var txt = (field) ? field.value : "";

    if (txt && txt != "") 
    {
        if(typeof(txt) != "String")
        {
            txt = "" + txt;
        }

        nNum = parseInt(txt);

        if (isNaN(nNum))
        {
            nNum = 0;
        }

        field.value = nNum;
    }
}

function AddToCart(nProductID)
{
    var pid = parseInt(nProductID + "");
    var qty = 0;

    if (document.getElementById("atcqty_" + pid))
    {
        qty = parseInt(document.getElementById("atcqty_" + pid).value);

        var bDoAdd = true;
        if (qty <= 0)
        {
            if (confirm("Specifying '0' will remove the selected product from your shopping cart. Do you wish to continue with this action?") != true)
            {
                bDoAdd = false;
            }
        }

        if(bDoAdd == true)
        {
            if (isNaN(pid) == false) 
            {
                var params = new Array();
                    params[0] = "pid=" + pid;
                    params[1] = "qty=" + qty;

                var func = function () 
                {
                    if (arguments[0] != null) 
                    {
                        if (arguments[0].readyState == 4) 
                        {
                            if (arguments[0].status == 200) 
                            {
                                if (arguments[0].responseText == "success") 
                                {
                                    if (qty > 0)
                                    {
                                        alert("Successully added to shopping cart!");
                                    }
                                    else
                                    {
                                        alert("Successully removed from shopping cart!");
                                    }
                                }
                                else 
                                {
                                    alert("This product could not be added to your shopping cart at this time!");
                                }
                            }
                        }
                    }
                };

                var oReq = DoAjax("ajax/addtocart.aspx", params, true, func);
            }
        }
    }
}

function newXhrObj()
{
    var xmlHttp = null;

    try
    {
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try 
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) 
            {
                xmlHttp = null;
            }
        }
    }

    return xmlHttp;
}

function DoAjax(url, arr, async, rsc) 
{
    var successful = false;
    var sResponse = "";

    var httpRequest = null;
    httpRequest = newXhrObj();

    if (!httpRequest) 
    {
        alert('This browser does not support this feature.');
        return false;
    }

    // If the browser is Internet Explorer or Async is true
    if (oInfo.engine == "MSIE" || async == true) 
    {
        httpRequest.onreadystatechange = function () 
        {
            if (rsc && typeof (rsc) == "function") 
            {
                rsc(httpRequest);
            }

            if (httpRequest.readyState == 4) 
            {
                if (httpRequest.status == 200) 
                {
                    successful = true;

                    sResponse = httpRequest.responseText;
                }
                else if (httpRequest.status == 0) {
                }
                else 
                {
                    alert('HTTP Error: ' + httpRequest.status);
                }
            }
        }
    }

    var timestamp = new Date().getTime();

    var params = "";
    if (arr != null) 
    {
        for (var i = 0; i < arr.length; i++) 
        {
            if (arr[0] != "" && arr[0] != null) 
            {
                if (i > 0) 
                {
                    params += "&";
                }

                params += arr[i];
            }
        }
    }

    // Posts Asynchronously
    if (async == true) 
    {
        httpRequest.open("POST", url, true);
    }
    else // Posts Synchronously
    {
        httpRequest.open("POST", url, false);
    }

    if (oInfo.engine != "MSIE") { httpRequest.setRequestHeader("Last-Modified", timestamp); }
    httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    if (oInfo.browser != "Chrome") { httpRequest.setRequestHeader("Content-length", params.length); }
    httpRequest.send(params);

    // If the browser is not Internet Explorer i.e. FireFox and we are fetching the data Synchronously
    if (oInfo.engine != "MSIE" && async != true) 
    {
        if (httpRequest.readyState == 4) 
        {
            if (rsc && typeof (rsc) == "function")
            {
                rsc(httpRequest);
            }

            if (httpRequest.status == 200) 
            {
                successful = true;

                sResponse = httpRequest.responseText;
            }
            else if (httpRequest.status == 0) 
            {
            }
            else 
            {
                alert('HTTP Error: ' + httpRequest.status);
            }
        }
    }

    if (async != true) 
    {
        httpRequest = null;
        return sResponse;
    }
    else 
    {
        return httpRequest;
    }
}

