var sURL = "ajax_query.php";
var sURLJSON = "ajax_json_query.php";
var oCurrentTooltip = "";
var sOldClass;
var iAjaxPopup = 0;
var IE4 = (document.all) ? true : false;
var ajax_tooltip_MSIE = false;
var oContent = "";
var iCurrPage;
var iNextPage;
var iPrevPage;
var iMaxPage;
var bItemContent = false;
var iBalloonTimeout = 0;
var iCurrID;
var bGoodbyeMsg = false;

if(navigator.userAgent.indexOf('MSIE')>=0)ajax_tooltip_MSIE=true;

function checkNumber(event) {
    //var oDbug = document.getElementById('debug');
    //oDbug.innerHTML = event.keyCode;
    
    if(checkNormalEvent(event)) {
        return true;
    }
    
    if((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) {
    return true; }
    else {
        return false;
    }
}

function checkNormalEvent(event) {
    
    if(event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 67 || event.keyCode == 88) {
        return true;
    }
    else {
        return false;
    }
}

function toCapitalize(sString) {
        val = sString.toLowerCase();
        newVal = '';
        val = val.split(' ');
        for(var c=0; c < val.length; c++) {
                newVal += val[c].substring(0,1).toUpperCase() +
val[c].substring(1,val[c].length) + ' ';
        }
        return newVal;
};

function closePopup(sDiv) {
    iAjaxPopup = 0;
    hidelayer(sDiv);

};

function cancelTimeout() {
     if(iBalloonTimeout) {
        clearTimeout(iBalloonTimeout);
    }
};

function updateCart(iRow,sInputID,sRowTotal,sSubtotal) {
    var d = new Date();
    var timeSerial = d.getTime();
    var iQty = document.getElementById(sInputID).value;
    if(!iQty) {
        iQty = 1;
        document.getElementById(sInputID).value = iQty;
    }
    var oXmlHttp = zXmlHttp.createRequest();
    var sQuery = "updatecart=1" + "&id=" + iRow + "&qty=" + iQty;
    sQuery += "&serial=" + timeSerial;
    
    oXmlHttp.open("GET",sURL + "?" + sQuery, true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {            
                sRawData = oXmlHttp.responseText;
                if(sRawData != "NO") {
                    var fData = sRawData.split(':');
                    document.getElementById(sRowTotal).innerHTML = fData[0];
                    document.getElementById(sSubtotal).innerHTML = fData[1];
                }
            }
        }   
    };
    oXmlHttp.send(null);    
};

function updateShipping(sSelectID,sVarID) {
    var d = new Date();
    var timeSerial = d.getTime();
    var fValue = document.getElementById(sSelectID).value;
    var oXmlHttp = zXmlHttp.createRequest();
    var sQuery = "updateship=1" + "&value=" + fValue;
    sQuery += "&serial=" + timeSerial;
    
    oXmlHttp.open("GET",sURL + "?" + sQuery, true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {            
                sRawData = oXmlHttp.responseText;
            }
        }   
    };
    oXmlHttp.send(null);    
};


function replaceDiv(sDiv,sTemplate) {
    var d = new Date();
    var timeSerial = d.getTime();

    var oXmlHttp = zXmlHttp.createRequest();
    var sQuery = "replace_div=1" + "&tpl=" + sTemplate;
    sQuery += "&serial=" + timeSerial;
    
    oXmlHttp.open("GET",sURL + "?" + sQuery, true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {            
                sRawData = oXmlHttp.responseText;
                if(sRawData != "NO") {
                    oDiv = document.getElementById(sDiv);
                    oDiv.innerHTML = sRawData;
                }
            }
        }   
    };
    oXmlHttp.send(null);    
};

function hideTooltip() {
    hidelayer("toolTip","block");    
};

function showTooltip(sSection,sID,oEvent) {
   
    var sDiv = "toolTip";
    var d = new Date();
    var timeSerial = d.getTime();
    var oDiv = document.getElementById(sDiv);
    if(!oDiv) {
        oDiv = document.createElement('DIV');
		oDiv.id = sDiv;	
		document.body.appendChild(oDiv);   
    }
    
    oDiv.innerHTML = '<div id="toolTip" style="padding: 25px;"><img src="../images/indicator_white.gif" style="vertical-align: middle; padding-right: 10px;"> Please Wait </div>';
    oDiv.style.left = (oEvent.clientX - 400) + "px";
    oDiv.style.top = (oEvent.clientY + 25) + "px";
    showlayer(sDiv);

    var oXmlHttp = zXmlHttp.createRequest();
    var sQuery = "tooltip=1" + "&id=" + sID + "&section=" + sSection;
    sQuery += "&serial=" + timeSerial;
    
    oXmlHttp.open("GET",sURL + "?" + sQuery, true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {            
                sRawData = oXmlHttp.responseText;
                if(sRawData != "NO") {
                    hidelayer(sDiv,"block");
                    oDiv.innerHTML = sRawData;
                    
                    if(ajax_tooltip_MSIE) {
                        fix_MSIE_div_bug(sDiv);
                    }
                    showlayer(sDiv);
                }
                else {
                    hidelayer(sDiv,"block");
                }
            }
        }   
    };
    oXmlHttp.send(null);
};

function showAjaxPopup(sID,sRequest,sDiv) {
   
    if(iAjaxPopup) {
        return;
    }
    
    var d = new Date();
    var timeSerial = d.getTime();
    var oDiv = document.getElementById(sDiv);
    if(!oDiv) {
        oDiv = document.createElement('DIV');
		oDiv.id = sDiv;	
		document.body.appendChild(oDiv);   
    }
    
    oDiv.innerHTML = '<div style="padding: 25px;"><img src="../images/indicator_white.gif" style="vertical-align: middle; padding-right: 10px;"> Please Wait </div>';
    centerLayer(oDiv);
    showlayer(sDiv);
    
    var oXmlHttp = zXmlHttp.createRequest();
    var sQuery = sRequest + "=1" + "&id=" + sID + "&req=" + escape(sRequest);
    sQuery += "&serial=" + timeSerial;
    
    oXmlHttp.open("GET",sURL + "?" + sQuery, true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {            
                sRawData = oXmlHttp.responseText;
                if(sRawData != "NO") {
                    hidelayer(sDiv);
                    oDiv.innerHTML = sRawData;
                    iAjaxPopup = 1;
                    centerLayer(oDiv);
                    if(ajax_tooltip_MSIE) {
                        fix_MSIE_div_bug(sDiv);
                    }
                    showlayer(sDiv);
                }
            }
        }   
    };
    oXmlHttp.send(null);    
};

function fix_MSIE_div_bug(sDiv) {

    /* Create iframe object for MSIE in order to make the div cover select boxes */
    var ajaxIframeID = sDiv + "_iframe";
    var ajax_iframe = document.getElementById(ajaxIframeID);
    var oDiv = document.getElementById(sDiv);

    if(!ajax_iframe) {	
    	ajax_iframe = document.createElement('<IFRAME frameborder="0">');
    	ajax_iframe.style.position = 'absolute';
    	ajax_iframe.border='0';
    	ajax_iframe.frameborder='0';
    	ajax_iframe.style.backgroundColor='#000';
    	ajax_iframe.src = 'blank.php';
    	ajax_iframe.style.left = '0px';
    	ajax_iframe.style.top = '0px';
    	ajax_iframe.style.display = 'none';
    	ajax_iframe.id = ajaxIframeID;
    	oDiv.appendChild(ajax_iframe);
    }
    var zIndex = parseInt(oDiv.style.zIndex) - 5;
    ajax_iframe.style.zIndex = zIndex;
    ajax_iframe.style.width = oDiv.style.top;
    ajax_iframe.style.width = oDiv.style.left;
	ajax_iframe.style.width = oDiv.clientWidth + 'px';
	ajax_iframe.style.height = oDiv.clientHeight + 'px';
	ajax_iframe.style.display = 'block';
  
};

function geoCodeRequest(sAddress,sCity,sState,sZipCode) {

    var oXmlHttp = zXmlHttp.createRequest();
    var oAddress = document.getElementById(sAddress);
    var oCity = document.getElementById(sCity);
    var oState = document.getElementById(sState);
    var oZipCode = document.getElementById(sZipCode);
    var oLatitude = document.getElementById('latitude');
    var oLongitude = document.getElementById('longitude');
    var oGcLat = document.getElementById('gclat');
    var oGcLong = document.getElementById('gclong');
    
    var oData = {
        street: oAddress.value,
        city: oCity.value,
        state: oState.value,
        zip: oZipCode.value
    };
        
    oXmlHttp.open("POST","yahooGeocode.php", true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {         
                var aGeocode = JSON.parse(oXmlHttp.responseText);
                /*
                var oDebug = document.getElementById('debug');
                oDebug.innerHTML = "<p>" + aGeocode.Address + "<br />" + aGeocode.City + ", " + aGeocode.State + " " + aGeocode.Zip + "</p>\nLat: " + aGeocode.Latitude + "<br />Lng: " + aGeocode.Longitude + "<br />";
                */
                if(aGeocode.balloon) {
                    x_offset_tooltip = -10;
                    y_offset_tooltip = -10;
                    ajax_showTooltip(aGeocode.balloon,oAddress);
                }
                else {
                    oAddress.value = toCapitalize(aGeocode.Address);
                    oZipCode.value = aGeocode.Zip;
                    oGcLat.value = aGeocode.Latitude;
                    oLatitude.value = aGeocode.Latitude;
                    oGcLong.value = aGeocode.Longitude;
                    oLongitude.value = aGeocode.Longitude;
                }
                /* centerLayer(oDebug);
                showlayer('debug');*/
            }
        }
    };
    oXmlHttp.send(JSON.stringify(oData));
};

function leftArrowMart(iPromoID,sDiv) {
    
    if(iCurrPage == 1) {
        return;
    }
    var newPage = (iCurrPage - 1);
    loadMartResult(newPage,iPromoID,sDiv,'r');
};

function rightArrowMart(iPromoID,sDiv) {
    
    if(iCurrPage == iMaxPage) {
        return;
    }
    var newPage = (iCurrPage + 1);
    loadMartResult(newPage,iPromoID,sDiv,'l');
};

function leftArrowBuyOnline(iOnlineID,sDiv) {
    
    if(iCurrPage == 1) {
        return;
    }
    var newPage = (iCurrPage - 1);
    loadBuyOnlineResult(newPage,iOnlineID,sDiv,'r');   
    
};

function rightArrowBuyOnline(iOnlineID,sDiv) {
    
    if(iCurrPage == iMaxPage) {
        return;
    }
    var newPage = (iCurrPage + 1);
    loadBuyOnlineResult(newPage,iOnlineID,sDiv,'l');   
   
};

function leftArrowSearch(iSearchID,sDiv) {
    
    if(iCurrPage == 1) {
        return;
    }
    var newPage = (iCurrPage - 1);
    loadSearchResult(newPage,iSearchID,sDiv,'r');   
    
};

function rightArrowSearch(iSearchID,sDiv) {
    
    if(iCurrPage == iMaxPage) {
        return;
    }
    var newPage = (iCurrPage + 1);
    loadSearchResult(newPage,iSearchID,sDiv,'l');   
   
};

function leftArrow(iCollectionID,sDiv) {
    
    if(iCurrPage == 1) {
        return;
    }
    var newPage = (iCurrPage - 1);
    loadThumbnail(newPage,iCollectionID,sDiv,'r');   
    
};

function rightArrow(iCollectionID,sDiv) {
    
    if(iCurrPage == iMaxPage) {
        return;
    }
    var newPage = (iCurrPage + 1);
    loadThumbnail(newPage,iCollectionID,sDiv,'l');   
   
};

function loadThumbnail(iPageID,iCollectionID,sDiv,cDirection) {
       
    var d = new Date();
    var timeSerial = d.getTime();
    
    var oXmlHttp = zXmlHttp.createRequest();    
    var oDiv = document.getElementById(sDiv);
    var sQuery = "req=loadthumb&id=" + iCollectionID + "&pg=" + iPageID;
    sQuery += "&serial=" + timeSerial;
    
    oXmlHttp.open("GET",sURL + "?" + sQuery, true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {            
                sRawData = oXmlHttp.responseText;             
                if(sRawData != "NO") {
                    aRS = JSON.parse(sRawData);
                    iCurrPage = parseInt(aRS.curr);
                    iNextPage = parseInt(aRS.next);
                    iPrevPage = parseInt(aRS.prev);
                    iMaxPage = parseInt(aRS.max);
                    var oPagerDiv = document.getElementById('pagerNav');
                    oPagerDiv.innerHTML = aRS.pager;
                    if(!bSliderContent) {
                        initializeSlider(sDiv,'list',aRS.content);
                    }
                    else {
                        slideLayer(sDiv,'list',aRS.content,cDirection);
                    }
                    postLoad();
                }
            }
        }   
    };
    oXmlHttp.send(null);     
};

function updatePhoto(sOptionID,sPhotoID) {

    var d = new Date();
    var timeSerial = d.getTime();
    var iRowID = document.getElementById(sOptionID).value;
    var oXmlHttp = zXmlHttp.createRequest();    
    var oDiv = document.getElementById(sPhotoID);
    var sQuery = "req=martPhoto&id=" + "&id=" + iRowID;
    sQuery += "&serial=" + timeSerial;
    
    oXmlHttp.open("GET",sURL + "?" + sQuery, true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {            
                sRawData = oXmlHttp.responseText;             
                if(sRawData != "NO") {
                    oDiv.src=sRawData;
                }
            }
        }   
    };
    oXmlHttp.send(null);     
};

function loadMartResult(iPageID,iPromoID,sDiv,cDirection) {
       
    var d = new Date();
    var timeSerial = d.getTime();
    
    var oXmlHttp = zXmlHttp.createRequest();    
    var oDiv = document.getElementById(sDiv);
    var sQuery = "req=loadmartresult&id=" + "&pg=" + iPageID + "&pid=" + iPromoID;
    sQuery += "&serial=" + timeSerial;
    
    oXmlHttp.open("GET",sURL + "?" + sQuery, true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {            
                sRawData = oXmlHttp.responseText;             
                if(sRawData != "NO") {
                    aRS = JSON.parse(sRawData);
                    iCurrPage = parseInt(aRS.curr);
                    iNextPage = parseInt(aRS.next);
                    iPrevPage = parseInt(aRS.prev);
                    iMaxPage = parseInt(aRS.max);
                    var oPagerDiv = document.getElementById('pagerNav');
                    oPagerDiv.innerHTML = aRS.pager;
                    if(!bSliderContent) {
                        initializeSlider(sDiv,'list',aRS.content);
                    }
                    else {
                        slideLayer(sDiv,'list',aRS.content,cDirection);
                    }
                    postLoad();
                }
            }
        }   
    };
    oXmlHttp.send(null);     
};

function loadBuyOnlineResult(iPageID,iOnlineID,sDiv,cDirection) {
       
    var d = new Date();
    var timeSerial = d.getTime();
    
    var oXmlHttp = zXmlHttp.createRequest();    
    var oDiv = document.getElementById(sDiv);
    var sQuery = "req=loadbuyonlineresult&bolid=" + iOnlineID + "&pg=" + iPageID;
    sQuery += "&serial=" + timeSerial;
    
    oXmlHttp.open("GET",sURL + "?" + sQuery, true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {            
                sRawData = oXmlHttp.responseText;             
                if(sRawData != "NO") {
                    aRS = JSON.parse(sRawData);
                    iCurrPage = parseInt(aRS.curr);
                    iNextPage = parseInt(aRS.next);
                    iPrevPage = parseInt(aRS.prev);
                    iMaxPage = parseInt(aRS.max);
                    var oPagerDiv = document.getElementById('pagerNav');
                    oPagerDiv.innerHTML = aRS.pager;
                    if(!bSliderContent) {
                        initializeSlider(sDiv,'list',aRS.content);
                    }
                    else {
                        slideLayer(sDiv,'list',aRS.content,cDirection);
                    }
                    postLoad();
                }
            }
        }   
    };
    oXmlHttp.send(null);     
};

function loadSearchResult(iPageID,iSearchID,sDiv,cDirection) {
       
    var d = new Date();
    var timeSerial = d.getTime();
    
    var oXmlHttp = zXmlHttp.createRequest();    
    var oDiv = document.getElementById(sDiv);
    var sQuery = "req=loadsearchresult&id=" + iSearchID + "&pg=" + iPageID;
    sQuery += "&serial=" + timeSerial;
    
    oXmlHttp.open("GET",sURL + "?" + sQuery, true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {            
                sRawData = oXmlHttp.responseText;             
                if(sRawData != "NO") {
                    aRS = JSON.parse(sRawData);
                    iCurrPage = parseInt(aRS.curr);
                    iNextPage = parseInt(aRS.next);
                    iPrevPage = parseInt(aRS.prev);
                    iMaxPage = parseInt(aRS.max);
                    var oPagerDiv = document.getElementById('pagerNav');
                    oPagerDiv.innerHTML = aRS.pager;
                    if(!bSliderContent) {
                        initializeSlider(sDiv,'list',aRS.content);
                    }
                    else {
                        slideLayer(sDiv,'list',aRS.content,cDirection);
                    }
                    postLoad();
                }
            }
        }   
    };
    oXmlHttp.send(null);     
};

function postLoad() {
    
    if(bSliderComplete == false) {
        setTimeout("postLoad('')",100);
        return;
    }
    
    var oLeftArrow = document.getElementById('leftArrow');
    var oRightArrow = document.getElementById('rightArrow');
    if(!oLeftArrow || !oRightArrow) {
        return;
    }
    
    if(iCurrPage == 1) {
        oLeftArrow.src = "images/arrow-l3.gif";
    }
    else if(iCurrPage > 1) {
        oLeftArrow.src = "images/arrow-l1.gif";
    }
    if(iCurrPage < iMaxPage) {
        oRightArrow.src = "images/arrow-r1.gif";   
    }
    else if(iCurrPage == iMaxPage) {
        oRightArrow.src = "images/arrow-r3.gif";
    }
};

function loadItemInfo(sCellID,iItemID,y_offset) {
    if(iCurrID != iItemID) {
        ajax_hideTooltip();
    }
    
    if(bItemContent) {
        return;
    }
    else {
        iBalloonTimeout = setTimeout("_loadInfo('" + sCellID + "','" + iItemID + "','" + y_offset + "')",750);
    }
};

function _loadInfo(sCellID,iItemID,y_offset) {
   
    iCurrID = iItemID;
    
    var d = new Date();
    var timeSerial = d.getTime();
        
    var oXmlHttp = zXmlHttp.createRequest();
    var sQuery = "req=iteminfo" + "&id=" + iItemID;
    sQuery += "&serial=" + timeSerial;
    
    oXmlHttp.open("GET",sURL + "?" + sQuery, true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {            
                sRawData = oXmlHttp.responseText;
                if(sRawData != "NO") {
                    var oCell = document.getElementById(sCellID);
                    ajax_showTooltip(sRawData,oCell,y_offset);
                    bItemContent = true;
                }
            }
        }   
    };
    oXmlHttp.send(null);
};

function retailerLogout(sDivContainer,sDiv) {
    
    var sRawData;
    var d = new Date();
    var timeSerial = d.getTime();
        
    var oXmlHttp = zXmlHttp.createRequest();
    var sQuery = "req=logout";
    sQuery += "&serial=" + timeSerial;
    
    oXmlHttp.open("GET",sURL + "?" + sQuery, true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {            
                sRawData = oXmlHttp.responseText;
                if(sRawData != "NO") {
                    hideLogin();
                }
            }
        }   
    };
    oXmlHttp.send(null);  
};

function hideLogin() {
    
    if(bGoodbyeMsg == false && bSliderComplete) {
        simpleSlideRight('loginStatusContainer','loginstatus'); 
    }
    else if(bGoodbyeMsg == true && bSliderComplete) {
        var oDiv = document.getElementById('loginstatus');
        oDiv.innerHTML = "You are logged out.  Good Bye!";
        oDiv.style.left = "0px";
        setTimeout("simpleSlideRight('loginStatusContainer','loginstatus');",3000);
        bGoodbyeMsg = false;
        return;
    }
    setTimeout("hideLogin()",500);
};

function editCategory(sCollectionID,sID,sFertile) {
   
    if(iAjaxPopup) {
        return;
    }
    var sDiv = 'ajaxPopup';
    var d = new Date();
    var timeSerial = d.getTime();
    var oDiv = document.getElementById(sDiv);
    if(!oDiv) {
        oDiv = document.createElement('DIV');
		oDiv.id = sDiv;	
		document.body.appendChild(oDiv);   
    }
    
    oDiv.innerHTML = '<div style="padding: 25px;"><img src="../images/indicator_white.gif" style="vertical-align: middle; padding-right: 10px;"> Please Wait </div>';
    centerLayer(oDiv);
    showlayer(sDiv);
    
    var oXmlHttp = zXmlHttp.createRequest();
    var sQuery = "ecat=1" + "&id=" + sID + "&cid=" + sCollectionID + "&child=" + sFertile;
    sQuery += "&serial=" + timeSerial;
    
    oXmlHttp.open("GET",sURL + "?" + sQuery, true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {            
                sRawData = oXmlHttp.responseText;
                if(sRawData != "NO") {
                    hidelayer(sDiv);
                    oDiv.innerHTML = sRawData;
                    tabberAutomatic(tabberOptions);
                    iAjaxPopup = 1;
                    centerLayer(oDiv);
                    if(ajax_tooltip_MSIE) {
                        fix_MSIE_div_bug(sDiv);
                    }
                    showlayer(sDiv);
                }
            }
        }   
    };
    oXmlHttp.send(null);    
};