var xmlHttp
var topTagId = "";

function GetXmlHttpObject() {
    var xmlHttp=null;
    try  {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)  {
        // Internet Explorer
        try {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function submitHiddenForm() {
    xmlHttp = GetXmlHttpObject();
 //   alert("before ajax");

    var url = "/search/searchFiltering";
    //url="testAction"
    var paramStr = collectUrlString();
    //xmlHttp.open("POST", url, true);
    //xmlHttp.send(paramStr);
    xmlHttp.open("GET", url+"?"+paramStr, true);
    xmlHttp.send(null);

    xmlHttp.onreadystatechange = resultDisplay;
//alert("Going Ajax ... ");

}

resultDisplay = function() {
    if (xmlHttp.readyState==4 && xmlHttp.status==200) {
        var response = xmlHttp.responseText;
        //alert(response);
        document.getElementById("centerpart_"+topTagId).innerHTML = response;
        vendorDetails();
        nothingfound();
    }
}

alphaSelect = function(alpha) {
  document.location = "/search/searchAtoZFiltering?atoz=" + alpha;
}

colorSelect = function(color) {
  document.location = "/search/searchColorFiltering?color=" + color;
}

raritySelect = function(rarity) {
  document.location = "/search/searchRarityFiltering?rarity=" + rarity;
}

langSelect = function(lang) {
  document.location = "/search/searchLangFiltering?lang=" + lang;
}

allSelect = function() {
	document.location = "/search/searchAllFiltering";
}

categorySelect = function(imgNo, formNo) {
    if (document.hiddenForm.frmNoValue.value != formNo ) {
        document.hiddenForm.price.value = "";
        document.hiddenForm.qty.value = "";
        document.hiddenForm.feedback.value = "";
        document.hiddenForm.frmNoValue.value = "";
        document.hiddenForm.catId.value = "";
    }
    document.hiddenForm.catId.value = imgNo;
    document.hiddenForm.frmNoValue.value = formNo;
    submitHiddenForm();
}

feedBackSelect = function(imgNo, formNo, productName, catId) {
    topTagId = productName;

    if (document.hiddenForm.frmNoValue.value != formNo ) {
        document.hiddenForm.productName.value = "";
        document.hiddenForm.price.value = "";
        document.hiddenForm.qty.value = "";
        document.hiddenForm.feedback.value = "";
        document.hiddenForm.frmNoValue.value = "";
    //document.hiddenForm.catId.value = "";
        
    }
    
    document.hiddenForm.catId.value = catId;
    document.hiddenForm.productName.value = productName;
    document.hiddenForm.feedback.value = imgNo;
    document.hiddenForm.frmNoValue.value = formNo;
    submitHiddenForm();
}

priceTyped = function(price, formNo, productName, catId) {
    topTagId = productName;
    if (document.hiddenForm.frmNoValue.value != formNo ) {
        document.hiddenForm.productName.value  = "";
        document.hiddenForm.price.value = "";
        document.hiddenForm.qty.value = "";
        document.hiddenForm.feedback.value = "";
        document.hiddenForm.frmNoValue.value = "";
    //document.hiddenForm.catId.value = "";
        
    }
    
    document.hiddenForm.catId.value = catId;
    document.hiddenForm.productName.value = productName;
    document.hiddenForm.price.value = price;
    document.hiddenForm.frmNoValue.value = formNo;
    submitHiddenForm();
}

qtyTyped = function(qty, formNo, productName, catId) {
    topTagId = productName;
    if (document.hiddenForm.frmNoValue.value != formNo ) {
        document.hiddenForm.productName.value = "";
        document.hiddenForm.price.value = "";
        document.hiddenForm.qty.value = "";
        document.hiddenForm.feedback.value = "";
        document.hiddenForm.frmNoValue.value = "";
    //document.hiddenForm.catId.value = "";
        
    }
    
    document.hiddenForm.catId.value = catId;
    document.hiddenForm.productName.value = productName;
    document.hiddenForm.qty.value = qty;
    document.hiddenForm.frmNoValue.value = formNo;
  //  alert("first");
    submitHiddenForm();
}

collectUrlString = function() {
    var tmpStr = "";
    tmpStr = tmpStr + "variantType=" + document.hiddenForm.variantType.value;
    tmpStr = tmpStr + "&feedback=" + document.hiddenForm.feedback.value;
    tmpStr = tmpStr + "&price=" + document.hiddenForm.price.value;
    tmpStr = tmpStr + "&qty=" + document.hiddenForm.qty.value;
    tmpStr = tmpStr + "&catId=" + document.hiddenForm.catId.value;
    tmpStr = tmpStr + "&productName=" + document.hiddenForm.productName.value;
    return tmpStr;
}

gotoShoppingCard = function(prodVarClientId, qty) {
    xmlHttp = GetXmlHttpObject();
    var url = "/product/addCart?prodVarClientId=" + prodVarClientId + "&qty=" + qty;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    xmlHttp.onreadystatechange = shoppingCardDisplay;
}

shoppingCardDisplay = function() {
    if (xmlHttp.readyState==4 && xmlHttp.status==200) {
        replaceCartWithResponse();
				quickExpandCurrentCart();  ///////////
    }
}

function replaceCartWithResponse() {
	var response = xmlHttp.responseText;
  document.getElementById("cartDiv").innerHTML = response;
}

checkoutFunction = function(clientName, variantType) {
    xmlHttp = GetXmlHttpObject();
    var url = "/product/checkOut?clientName=" + clientName + "&variantType=" + variantType;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    xmlHttp.onreadystatechange = processCheckout;
}

function processCheckout() {
	if (xmlHttp.readyState==4 && xmlHttp.status==200) {
    replaceCartWithResponse();
    quickExpandBothCarts();  ///////////
    window.open("/product/cart_redirect");
  }
}

removeProduct = function(productName, clientName, variantType) {
    xmlHttp = GetXmlHttpObject();
    var url = "/product/clearProductFromCart?clientName=" + clientName + "&productName=" + productName + "&variantType=" + variantType;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    xmlHttp.onreadystatechange = shoppingCardDisplay;
}

clearCart = function(clientName, varType) {
    xmlHttp = GetXmlHttpObject();
    var url = "/product/clearCart?clientName=" + clientName + "&variantType=" + varType;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    xmlHttp.onreadystatechange = shoppingCardDisplay;
}

itemsInCart = function() {
    var cnt = 0;
    var anchortags=document.getElementsByTagName("div");
    for(i=0;i<anchortags.length;i++) {
        var currentTag=anchortags[i];
        if(currentTag.className=="product_name_flat") {
            cnt++;
        }
    }
    if (cnt) {
        if ((document.getElementById("sidebarcontainer_buy")) instanceof Object) {
            document.getElementById("sidebarcontainer_buy").style.display = "block";
            document.getElementById("sidebarcontainer_sell").style.display = "block";
        }
    }
    else {
        if ((document.getElementById("sidebarcontainer_buy")) instanceof Object) {
            document.getElementById("sidebarcontainer_buy").style.display = "none";
            document.getElementById("sidebarcontainer_sell").style.display = "none";
        }
    }
}

resetProduct = function(productName, catId) {
    
    topTagId = productName;

    document.hiddenForm.catId.value = catId;
    document.hiddenForm.productName.value = productName;
    document.hiddenForm.qty.value = "0";
    document.hiddenForm.price.value = "0";
    document.hiddenForm.feedback.value = "1";
    submitHiddenForm();
}


newResetProduct = function(productName, catId) {
    topTagId = productName;

    document.hiddenForm.catId.value = catId;
    document.hiddenForm.productName.value = productName;
    document.hiddenForm.qty.value = "0";
    document.hiddenForm.price.value = "0";
    document.hiddenForm.feedback.value = "1";
    //newSubmitHiddenForm();
}

newSubmitHiddenForm = function() {
    xmlHttp = GetXmlHttpObject();
    var url = "/search/searchFiltering";
    var paramStr = collectUrlString();
    xmlHttp.open("GET", url+"?"+paramStr, true);
    xmlHttp.send(null);
    xmlHttp.onreadystatechange = newResultDisplay;
}

newResultDisplay = function() {
    if (xmlHttp.readyState==4 && xmlHttp.status==200) {
        var response = xmlHttp.responseText;
        document.getElementById("centerpart_"+topTagId).innerHTML = response;
      //  alert("before vendorDetails...")
        vendorDetails();
        nothingfound();
    }
}

