﻿var xmlHttpObj;
var QUEUE = new Array();
var METHOD = new Array();
var QUEUE_BUSY = false;
var queueWatcher = setInterval(execAJAX, 100);
var LAST_RESPONSE_STATUS = 200;

function queueRequest(request, method)
{
    QUEUE.push(request);
    METHOD.push(method);  
}

function execAJAX()
{
    var request;

    if(!QUEUE_BUSY && QUEUE.length > 0)
    {
        QUEUE_BUSY = true;
        request = QUEUE.shift();
        
        xmlHttpObj = getXmlHttpObject();
        xmlHttpObj.onreadystatechange = stateChanged;
        xmlHttpObj.open('GET', request, true);
        xmlHttpObj.send(null);    
    }
}

function getXmlHttpObject()
{ 
    var objXMLHttp = null;

    if (window.XMLHttpRequest)
        objXMLHttp = new XMLHttpRequest();
    else if (window.ActiveXObject)
        objXMLHttp = new ActiveXObject('Microsoft.XMLHTTP');
    
    return objXMLHttp;
}

function stateChanged() 
{
    var json;
    var method;
    
    if (xmlHttpObj.readyState == 4 || xmlHttpObj.readyState == 'complete')
    {
        json = xmlHttpObj.responseText;
        method = METHOD.shift();
        LAST_RESPONSE_STATUS = xmlHttpObj.status;
        switch(method)
        {
            case 'updateOrderFormInfo':
                eval(method + '(\'' + json + '\')');
                break;
            case 'showMoreInfo':
                eval(method + '(\'' + json + '\')');
                break;
            case 'showBuyOnline':
                eval(method + '(\'' + json + '\')');
                break;
            default:
                eval(method + '(' + json + ')');
                break;
        }

        QUEUE_BUSY = false;
    } 
}

function updatePageMenu(json)
{
    PAGES = json;
    PAGE_COUNT = PAGES.pages.length;
}

function updateRunDates(json)
{
    RUNDATES = json;
}

function updateCategoryMenu(json)
{
    CATEGORYS = json;
}

function updateBrandMenu(json)
{
    BRANDS = json;
}

function updatePromotionMenu(json)
{
    PROMOTIONS = json;
}

// MODIFIED: 03/26/08 4:43PM - changed the TOC to TOCS to be consistant with 
// BRANDS and CATEGORYS
function updateTOCMenu(json)
{
    TOCS = json;
}

function updateFullImageData(json)
{
    FULL_IMAGE = json;
}

function updateFacingImageData(json)
{
    FACING_IMAGE = json;
}

function updateIndexImageData(json)
{
    INDEX_IMAGE = json;
}

function updateZoomImageData(json) {
}

function updateProductDetailData(json)
{
	if( LAST_RESPONSE_STATUS == 500 )
		PRODUCT_DETAIL = 'ERROR';
	else
		PRODUCT_DETAIL = json;
}

function updateStoreInfo(json)
{
    DEALER_LOCATION = json;
}

function updatePageViewMenu()
{
    var html = '';
    var itemCount = 0;

    // make sure the oPUBINFO object if available
    if (typeof(oPUBINFO) == 'undefined')
    {
        setTimeout('updatePageViewMenu()', 50);
        return;
    }
    
    html += '{';
    html += '	"pageview":';
    html += '	{';
    html += '		"submenus":false,';
    html += '		"menuitems":';
    html += '		[';
    
    if( oPUBINFO.PublicationType.image.full.Use )
    {
        html += '			{"name":"' + FULL + '", "url": "VIEW=\\\'FULL\\\'; doNav();"}';
        itemCount += 1;
    }

    if ( oPUBINFO.PublicationType.image.facing.Use )
    {
        if (itemCount > 0)
            html += ',';

        html += '			{"name":"' + FACING + '", "url": "VIEW=\\\'FACING\\\'; doNav();"}';
        itemCount += 1;
    }

    if ( oPUBINFO.PublicationType.image.index.Use )
    {
        if (itemCount > 0)
            html += ',';

        html += '			{"name":"' + INDEX + '", "url": "VIEW=\\\'INDEX\\\'; doNav();"}';
    }
    
    html += '		]';
    html += '	}';
    html += '}';
    
    PAGEVIEW = eval('(' + html + ')');    
}

function updatePriceRangeMenu()
{
    var html = '';
    var itemCount = 0;

    // make sure the oPUBINFO object if available
    if (typeof(oPUBINFO) == 'undefined')
    {
        setTimeout('updatePriceRangeMenu()', 50);
        return;
    }
    
    html += '{';
    html += '	"priceranges":';
    html += '	{';
    html += '		"submenus":false,';
    html += '		"menuitems":';
    html += '		[';
    
    var low = '';
    var high = '';
    for( var i = 0 ; i < oPUBINFO.PublicationType.menu.pricerangemenu.ranges.length ; i++ )
    {
		low = oPUBINFO.PublicationType.menu.pricerangemenu.ranges[i].low;
		high = oPUBINFO.PublicationType.menu.pricerangemenu.ranges[i].high 
		
//		if( high == '-1' )
//			low += 'char(107)';
		
		html += '			{"brandid":"' + i + '"';
		html += ',"name":"';
		if (LANGUAGE.toUpperCase() == "FR")
		    html += low.toString().replace(".",",") + " $";
		else
		    html += "$" + low;



		if (high != '-1') 
		{
		    html += ' - ';
		    if (LANGUAGE.toUpperCase() == "FR")
		        html += high.toString().replace(".", ",") + ' $';
            else
                html += "$" + high;
		}
		else
		    html += '+';
		
		html += '"}';		
		
		if( i != oPUBINFO.PublicationType.menu.pricerangemenu.ranges.length - 1 )
			html += ',\n';
    }
    
    html += '		]';
    html += '	}';
    html += '}';
    
    PRICERANGES = eval('(' + html + ')');    
}

function updateOrderFormInfo( OrderFormPDF )
{
	ORDERFORMNAME = OrderFormPDF;
}

function updateProductInfo(json)
{
    PRODUCTS = json;
}

function updateCorrectionNoticeData(json)
{
    NOTICES = json;
}

function updateCouponData(json) {
    COUPONS = json;
}