﻿function emptyList( box ) {
	// Set each option to null thus removing it
	while ( box.options.length ) box.options[0] = null;
}
function onCategoryChange(){
    var catDDown = document.getElementById("categoryDDown")
    var posDDown = document.getElementById("positionDDown") 
    if(catDDown.selectedIndex == 0){
        posDDown.selectedIndex = 0;
        posDDown.disabled = true;
        disableGoBtn()
        return;
    }
    setPositionDDown();
    posDDown.disabled = false;
    disableGoBtn()
}
function onPositionChange(){
   var posDDown = document.getElementById("positionDDown")
   if(posDDown.value != ""){
        enableGoBtn()
   }else{
    disableGoBtn()
   }
}

function setupDDowns(){
    if(shopInfo == undefined )
        throw "shopInfo is undefined";
    var catDDown = document.getElementById("categoryDDown")
    
    var indexer=0;
    catDDown.options[indexer++] = new Option(industryWord,"")
    for(var i in shopInfo){
        catDDown.options[indexer++] = new Option(shopInfo[i].name,shopInfo[i].id)
    }
    catDDown.selectedIndex = 0;
    setPositionDDown();
}

function setPositionDDown(){
    var catDDown = document.getElementById("categoryDDown")
    var catID = catDDown.options[catDDown.selectedIndex].value;
    var posDDown = document.getElementById("positionDDown")
    emptyList(posDDown);
    posDDown.options[0] = new Option(positionWord,"");
    if(catID == ""){
       posDDown.disabled = true;
       return; 
    }
    //alert(shopInfo[catID].products)
    for( var i=0; i < shopInfo[catID].products.length; i++){
        var product = shopInfo[catID].products[i];
        posDDown.options[i+1] = new Option(product.name,product.id)
    }
    posDDown.selectedIndex = 0;
}

function enableGoBtn(){
    var catID = document.getElementById("categoryDDown").value;
    var posID = document.getElementById("positionDDown").value;
    var anchor = document.getElementById("goBtnAnchor"); 
    anchor.href = getShopURL(shopDomain ,posID,catID);
    anchor.parentNode.style.backgroundImage="url(images/jtpBanner/goBtnClean.gif)"
}

function disableGoBtn(){
    var anchor = document.getElementById("goBtnAnchor"); 
    anchor.href = "";
    anchor.parentNode.style.backgroundImage="url(images/jtpBanner/goBtnClean_disabled.gif)"
}

function getShopURL(domain,posID,catID){
    return shopDomain +"showproduct.aspx?ProductID="+posID+"&CategoryID="+catID ;
}





