/*    ****************************************************************************
*
*  Permission to use, copy, modify, distribute and sell this software (or
*  code file) and its documentation for any purpose is STRICTLY PROHIBITED.
*  This file is provided "as is" without expressed or implied warranty.
*
* **************************************************************************
*
*  Copyright(c) 2010 Compunix, LLC.
*  Please visit http://www.compunix.us for licensing information.
*
*  THE ABOVE NOTICE MUST REMAIN INTACT.
*
* ************************************************************************** */
function SwitchTab(tab) {

    document.getElementById('buy').className = 'tabbed_content tabs-hide';
    document.getElementById('orderinfo').className = 'tabbed_content tabs-hide';

    if (tab == 'tab_1') //buy
    {
        document.getElementById('tab_2').className = '';
        document.getElementById('buy').className = 'tabbed_content';
    }
    if (tab == 'tab_2') //details(orderinfo)
    {
        document.getElementById('tab_1').className = '';
        document.getElementById('orderinfo').className = 'tabbed_content';
    }

    document.getElementById(tab).className = 'tabs-selected';



    // tabbed_content tabs-hide
}

function getOffset(el) {
    var _x = 0;
    var _y = 0;
    while (el && !isNaN(el.offsetLeft) && !isNaN(el.offsetTop)) {
        //        _x += el.offsetLeft - el.scrollLeft;         
        //        _y += el.offsetTop - el.scrollTop;      
        _x += el.offsetLeft;
        _y += el.offsetTop;
        el = el.offsetParent;
    }
    return { top: _y, left: _x };
}

function getAbsoluteOffset(obj) {
    var _x = 0;
    var _y = 0;
    //obj = obj.parentNode;
    _x += obj.offsetLeft;
    _y += obj.offsetTop;
    if (obj.offsetParent) {
        do {
            _x += obj.offsetLeft;
            _y += obj.offsetTop;

        } while (obj = obj.offsetParent);
    }
    return { top: _y, left: _x };
}

function fillQuickViewBox(productID) {
    var xmlHttp;

    var x = getOffset(document.getElementById('quickviewbtn' + productID)).top;

    ;
    document.getElementById('quickview-popup').style.top = x - 120;

    // create XMLHttpRequest object
    // if IE6 or IE5, create ActiveXObject

    // XMLHttpRequest
    if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
    // ActiveXObject
    else if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    // No browser support
    else {
        alert("Your browser does not support XMLHttpRequest or ActiveXObject");
    }


    xmlHttp.onreadystatechange = function() {

        if (xmlHttp.readyState == 1) {
            document.getElementById('Section' + sectionID).innerHTML = "Loading...";
            //document.getElementById('quickview-popup').innerHTML = "Loading...";
        }
        if (xmlHttp.readyState == 2) {
            document.getElementById('Section' + sectionID).innerHTML = "Loading...";
            //document.getElementById('quickview-popup').innerHTML = "Loading...";
        }
        if (xmlHttp.readyState == 3) {
            document.getElementById('Section' + sectionID).innerHTML = "Loading...";
            //document.getElementById('quickview-popup').innerHTML = "Loading...";
        }

        // if request is complete
        if (xmlHttp.readyState == 4) {
            // document.getElementById('AwaitingSectionContent').style.display = 'none'; 
            // document.getElementById('SectionsContent').style.display = 'block';    

            var startTag = 'Section_BeginContent';
            var endTag = 'Section_EndContent';

            var str = xmlHttp.responseText;

            // alert(str);                        

            var starting = str.indexOf(startTag);
            var ending = str.indexOf(endTag);

            var innerHtmlString = str.substring(starting + startTag.length, ending);
            //innerHtmlString = innerHtmlString.replace('class="AddToCartButton"', 'class="AddToCartButton" id="AddToCartButton"').replace('class="AddToWishButton"', 'class="AddToWishButton" id="AddToWishButton"');
            // id="AddToCartButton_1_1" -> id="AddToCartButton"
            //var startidx = innerHtmlString.indexOf('id="AddToCartButton',0) + 19; 
            //var underbaridx = innerHtmlString.indexOf('_',startidx);
            //var idlength = innerHtmlString.indexOf('"',underbaridx) - underbaridx;
            //innerHtmlString = innerHtmlString.replace(innerHtmlString.substr(startidx,idlength),'');
            // AddtoWishButton
            //startidx = innerHtmlString.indexOf('id="AddToWishButton',0) + 19; 
            //underbaridx = innerHtmlString.indexOf('_',startidx);
            //idlength = innerHtmlString.indexOf('"',underbaridx) - underbaridx;
            //innerHtmlString = innerHtmlString.replace(innerHtmlString.substr(startidx,idlength),'');

            //alert(innerHtmlString);


            document.getElementById('quickview-popup').style.display = "block";
            document.getElementById('quickview-popup').innerHTML = innerHtmlString;
            //alert(innerHtmlString);          
            //document.getElementById('AJAXATC').innerHTML = newhtml;
            EvalJSBlocks(innerHtmlString);

            //alert(document.getElementById('AddToCartButton').onclick.toString());
            document.getElementById('AddToCartButton').onclick = function() { document.getElementById('AddToCartButton').parentNode.submit(); };

            if (document.getElementById('AddToWishButton')) document.getElementById('AddToWishButton').onclick = function() { document.getElementById('IsWishList').value = '1'; document.getElementById('AddToWishButton').parentNode.submit(); };
            //alert(document.getElementById('AddToCartButton').onclick.toString());




            // document.getElementById("section" + sectionID +"ProductID").value = productID;

        }
    }

    // SERVER REQUEST
    // Post, server side script, asynchronously
    var url = "quickproductview.aspx";
    var params = "prodID=" + productID;
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send(params);

}

function checkAttributesCount(productID, pcolors, psizes) {
    pcolorsarr = pcolors.split(',');
    psizesarr = psizes.split(',');

    if (pcolorsarr.length > 0 || psizesarr.length > 1) {
        fillQuickAttributeViewBox(productID);
    } else {
        return false;
    }
}


function fillQuickAttributeViewBox(productID) {
    var xmlHttp;

    var x = getAbsoluteOffset(document.getElementById('ProductPic' + productID)).top;
    var y = getAbsoluteOffset(document.getElementById('ProductPic' + productID)).left;

    document.getElementById('quickattribute-popup').innerHtml = "";
    document.getElementById('quickattribute-popup').style.top = (x+150) + "px" ;
    document.getElementById('quickattribute-popup').style.left = (y+50) + "px" ;    

    // create XMLHttpRequest object
    // if IE6 or IE5, create ActiveXObject

    // XMLHttpRequest
    if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
    // ActiveXObject
    else if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    // No browser support
    else {
        alert("Your browser does not support XMLHttpRequest or ActiveXObject");
    }


    xmlHttp.onreadystatechange = function() {

        if (xmlHttp.readyState == 1) {
            //document.getElementById('quickview-popup').innerHTML = "Loading...";
        }
        if (xmlHttp.readyState == 2) {
            //document.getElementById('quickview-popup').innerHTML = "Loading...";
        }
        if (xmlHttp.readyState == 3) {
            //document.getElementById('quickview-popup').innerHTML = "Loading...";
        }

        // if request is complete
        if (xmlHttp.readyState == 4) {
            // document.getElementById('AwaitingSectionContent').style.display = 'none'; 
            // document.getElementById('SectionsContent').style.display = 'block';    

            var startTag = 'Section_BeginContent';
            var endTag = 'Section_EndContent';

            var str = xmlHttp.responseText;

                          

            var starting = str.indexOf(startTag);
            var ending = str.indexOf(endTag);

            var innerHtmlString = str.substring(starting + startTag.length, ending);

            //alert(innerHtmlString);

            document.getElementById('quickattribute-popup').style.display = "block";
            document.getElementById('quickattribute-popup').innerHTML = innerHtmlString;

            EvalJSBlocks(innerHtmlString);

        }
    }

    // SERVER REQUEST
    // Post, server side script, asynchronously
    var url = "quickattributeview.aspx";
    var params = "prodID=" + productID;
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send(params);

}




function showQuickViewBtn(productID) {
    document.getElementById('quickviewbtn' + productID).style.display = "block";
}

function hideQuickViewBtn(productID) {
    document.getElementById('quickviewbtn' + productID).style.display = "none";
}

function hideQuickViewPopUp() {
    document.getElementById('quickview-popup').style.display = "none";
}

function hideQuickAttributeViewPopUp() {
    document.getElementById('quickattribute-popup').style.display = "none";
}

function EvalJSBlocks(gen_html) {
    var stripped_html = gen_html;
    var js_start = 0;
    do {
        js_start = stripped_html.toLowerCase().indexOf('<' + 'script', 0);
        if (js_start > -1) {
            var js_start2 = stripped_html.indexOf('>', js_start) + 1;
            var js_end = stripped_html.toLowerCase().indexOf('<' + '/' + 'script' + '>', js_start);
            var js_end2 = stripped_html.indexOf('>', js_end) + 1;
            var gen_js = stripped_html.substring(js_start2, js_end);
            stripped_html = stripped_html.substring(0, js_start) + stripped_html.substring(js_end2, stripped_html.length);
            gen_js = gen_js.replace('<' + '!--', '');
            gen_js = gen_js.replace('/' + '/--' + '>', '');
            eval(gen_js);
        }
    }
    while (js_start > -1);

    return stripped_html;
}


