
var xmlHttp
function showProduct(catId, venId, rowId ) {
    xmlHttp = GetXmlHttpObject();
    var url = "../../search/showItems?catId=" + catId + "&venId=" + venId + "&rowId=" + rowId;
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = updateItemsPage;
    xmlHttp.send(null);
}

      shopingCardPopup = function() {
        $("#sidebarcontainer_sell").hide();
        $("#sellDiv").click(function(){
          $(this).siblings("#sidebarcontainer_sell").slideToggle(300);
        });
        $("#sidebarcontainer_buy").hide();
        $("#buyDiv").click(function(){
          $(this).siblings("#sidebarcontainer_buy").slideToggle(300);
        });
      }
      
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 updateItemsPage() {

    if (xmlHttp.readyState==4 && xmlHttp.status==200) {
        var response = xmlHttp.responseText;
        responseText = response.toString();
        responseArray = responseText.split("#");
        var preValue = document.getElementById(responseArray[0]).innerHTML;
        addElement(responseArray[0], responseArray[1]);
        document.getElementById(responseArray[0]).onclick= new Function();
        document.getElementById("td_"+responseArray[0]).style.backgroundColor = "#ffffff";
    }
}

function addElement(divValue, divText) {
    var ni = document.getElementById(divValue);
    var newdiv = document.createElement('div');
    var divIdName = divValue+'Div';
    newdiv.setAttribute('id',divIdName);
    newdiv.innerHTML = divText;
    ni.appendChild(newdiv);
}



moveSuggest = function(catId, productName) {
    var searchOpt = document.getElementById("searchForm_searchOption").value;
    moveToProduct(catId,productName,searchOpt);
}

moveToProduct = function(catId, productName, variantType) {
    window.location="/search/searchByProductNameAndCategory?catId=" + catId + "&productName=" + productName + "&variantType=" + variantType;
}

// Below codes for Search Suggestions List....
function suggestWords(e) {
    var characterCode; //literal character code will be stored in this variable
    if(e && e.which){ //if which property of event object is supported (NN4)
        e = e
        characterCode = e.which //character code is contained in NN4's which property
    }
    else{
        e = event
        characterCode = e.keyCode //character code is contained in IE's keyCode property
    }
    if (characterCode == 13) {
		if (results && results.hasActivatedResultAndVersion()) {
			results.goToActivatedVersion();
			return false;
		} else {
			var searchForm_searchString = document.getElementById("searchForm_searchString").value;
			var searchOption = document.getElementById("searchForm_searchOption").value;
			window.location ="/search/searchProductByProductName?productName=" + searchForm_searchString + "&variantType=" + searchOption;
		}
    } else if (characterCode == 37 || characterCode == 38 || characterCode == 39 || characterCode == 40) {
		if (document.all) {
			keyPressEvent(e);
		}
		return;
	} else {
		var typedText = document.getElementById("searchForm_searchString").value;

        if (typedText.length > 2){
            xmlHttp = GetXmlHttpObject();
            var url = "../../search/searchSuggestWords?typedWord=" + typedText;
            xmlHttp.open("GET", url, true);
            xmlHttp.onreadystatechange = showSuggestList;
            xmlHttp.send(null);
        }
        else {
            document.getElementById("suggestDiv").innerHTML = "";
        }
    }
}

var results;

function showSuggestList() {
    if (xmlHttp.readyState==4 && xmlHttp.status==200) {
        var response = xmlHttp.responseText;
        responseText = response.toString();

        document.getElementById("suggestDiv").innerHTML = responseText;
		
		
		results = new Results();	
		
        document.getElementById("suggestDiv").style.display = "block";
        setFunction();
    }
}

function copyTxt(wrd) {
    document.getElementById("searchForm_searchString").value = wrd;
    document.getElementById("suggestDiv").innerHTML = "";
    document.getElementById("suggestDiv").style.display = "none";
}

keyPressEvent = function(e) {
    var code;
    //var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
	switch (code) {
		case 27:
			document.getElementById("suggestDiv").innerHTML = "";
			document.getElementById("suggestDiv").style.display = "none";
			window.onkeypress = setDummyFunction;
			break;
		case 38: // Up Key pressed
			results.up();
			break;
		case 40: // Down Key pressed
			results.down();
			break;
		case 39: // Right Key pressed
			results.right();
			if (e.preventDefault) e.preventDefault(); // IE
			else e.returnValue = false; // Firefox, Safari, Opera
			break;
		case 37: // Left Key pressed
			results.left();
			if (e.preventDefault) e.preventDefault(); // IE
			else e.returnValue = false; // Firefox, Safari, Opera
			break;
	}
}

onBlurEvent = function() {
    document.getElementById("suggestDiv").innerHTML = "";
    document.getElementById("suggestDiv").style.display = "none";
    window.onkeypress = setDummyFunction;
}

setFunction = function() {
	window.onkeypress = keyPressEvent;
	$("span#suggestDiv > ul > li").each(function(index) {
		$(this).mouseover(function() {
			results.setActiveResultWithoutVersion(index);
		});
		var links = $("a", this);
		for (var i = 0; i < links.length; i++) {
			$(links[i]).mouseover(function() {
				results.getActiveResult().setActiveVersion(i);
			});
		}
	});
}

setDummyFunction = function() {}

buyPrice = function() {
    document.listSearchForm.action = "../search/searchAdvancedList";
    document.listSearchForm.variant.value = "buylist_variant";
    document.listSearchForm.submit();
}

sellPrice = function() {
    document.listSearchForm.action = "../search/searchAdvancedList";
    document.listSearchForm.variant.value = "variants";
    document.listSearchForm.submit();
}


var topName = "";
selectCatIdFilteringByProductNameSecond = function(catId, ProductName, setPrice, setQty) {
    var searchOpt = document.getElementById("searchForm_searchOption").value;
    document.hiddenForm.catId.value = catId;
    topName = "centerpart_" + ProductName;
    xmlHttp = GetXmlHttpObject();
    var url = "../../search/searchCatIdFilteringByProductName?catId=" + catId + "&productName=" + ProductName + "&variantType=" + searchOpt + "&price=" + setPrice + "&qty=" + setQty;
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = newClientList;
    xmlHttp.send(null);
}

newClientList = function() {
    if (xmlHttp.readyState==4 && xmlHttp.status==200) {
        var response = xmlHttp.responseText;
        document.getElementById(topName).innerHTML = response;
        vendorDetails();
        changeCategory();
        changeText();
        nothingfound();
    }
}


var innerName = "";

selectCatIdFilteringByProductNameAndClient = function(catId, ProductName, clientId) {
    var searchOpt = document.getElementById("searchForm_searchOption").value;
    document.hiddenForm.catId.value = catId;
    innerName = "green_box_" + ProductName + "_"  + clientId;
    xmlHttp = GetXmlHttpObject();
    var url = "../../search/searchCatIdFilteringByProductNameAndClient?catId=" + catId + "&productName=" + ProductName + "&clientId=" + clientId + "&variantType=" + searchOpt;
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = newProductListBasedOnCatagory;
    xmlHttp.send(null);
}

newProductListBasedOnCatagory = function() {   
    if (xmlHttp.readyState==4 && xmlHttp.status==200) {
        var response = xmlHttp.responseText;
        document.getElementById(innerName).innerHTML = response;
        //changeText();
    }
}


changeText = function() {
    if (document.getElementById("searchForm_searchOption").value == "buylist_variant") {
        //document.hiddenForm.variantType.value = "buylist_variant";
        $('.max_price').html("min Price");
        $('.min_qty').html("max Qty");
        $('.low_price').html("High Price");
    }
    else {
        //document.hiddenForm.variantType.value = "variants";
        $('.max_price').html("max Price");
        $('.min_qty').html("min Qty");
        $('.low_price').html("Low Price");
    }
}

// Partial search results object
function Results() {
	var activePos = -1;
	var results = new Array();
	
	$("span#suggestDiv > ul > li").each(function() {
		if ($("a", this).length > 0 && $("b", this).length > 0) {
			results.push(new Result(this));
		}
	});
	
	this.setActiveResult = function(position) {
		if (position < results.length) {
			if (activePos != -1) {
				results[activePos].setActive(false);
			}
			activePos = position;
			if (activePos != -1) {
				results[activePos].setActive(true);
			}
		}
	}

	this.setActiveResultWithoutVersion = function(position) {
		if (position < results.length) {
			if (activePos != -1) {
				results[activePos].getElement().css("background-color", "#FFFFFF");
				if (activePos != position) {
					results[activePos].clearActiveVersion();
				}
			}
			activePos = position;
			results[activePos].setActiveWithoutVersion();
		}
	}
	
	this.getActiveResult = function() {
		return results[activePos];
	}
	
	this.clearActiveResult = function() {
		for (var i = 0; i < results.length; i++) {
			results[i].clearActiveVersion();
			results[i].getElement().css("background-color", "#FFFFFF");
		}
		activePos = -1;
	}
	
	this.clickResult = function() {
		if (activePos != -1) {
			results[activePos].clickVersion();
		}
	}
	
	this.length = function() {
		return results.length;
	}
	
	this.left = function() {
		results[activePos].prevVersion();
	}
	
	this.right = function() {
		results[activePos].nextVersion();
	}
	
	this.up = function() {
		if (activePos != -1 && (activePos - 1) >= -1) {
			this.setActiveResult(activePos - 1);
		}
	}
	
	this.down = function() {
		if ((activePos + 1) < results.length) {
			this.setActiveResult(activePos + 1);
		}		
	}
	
	this.hasActivatedResultAndVersion = function() {
		if (activePos != -1) {
			return results[activePos].hasActivatedVersion();
		}
		return false;
	}
	
	this.goToActivatedVersion = function() {
		if (activePos != -1) {
			results[activePos].goToActiveVersion();
		}
	}
}

// Partial search result object
function Result(element) {
	var e = $(element);
	var activePos = -1;
	var versions = new Array();
	
	$("a", element).each(function(index)  {
		versions.push($(this));
		$(this).mouseover(function () {
			results.getActiveResult().setActiveVersion(index);
		});
	});
	
	this.setActive = function(state) {
		if (state) {
			e.css("background-color", "#F7D780");
			this.setActiveVersion(0);
		} else {
			e.css("background-color", "#FFFFFF");
			this.clearActiveVersion();
		}
	}
	
	this.setActiveWithoutVersion = function() {
		for (var i = 0; i < versions.length; i++) {
			versions[i].css("background-color", "#F7D780");
		}
		activePos = -1;
		e.css("background-color", "#F7D780");
	}

	this.setActiveVersion = function(position) {
		if (position < versions.length) {
			for (var i = 0; i < versions.length; i++) {
				versions[i].css("background-color", "#F7D780");
			}
			activePos = position;
			versions[activePos].css("background-color", "#FFFFFF");
		}
	}
	
	this.clearActiveVersion = function() {
		for (var i = 0; i < versions.length; i++) {
			versions[i].css("background-color", "#FFFFFF");
		}
	}
	
	this.clickVersion = function() {
		if (activePos != -1) {
			versions[activePos].click();
		}
	}
	
	this.getElement = function() {
		return e;
	}
	
	this.nextVersion = function() {
		if (activePos + 1 < versions.length) {
			this.setActiveVersion(activePos + 1);
		}
	}
	
	this.prevVersion = function() {
		if (activePos - 1 >= 0) {
			this.setActiveVersion(activePos - 1);
		}
	}

	this.hasActivatedVersion = function() {
		for (var i = 0; i < versions.length; i++) {
			if (versions[i].css("background-color") == "#FFFFFF" || versions[i].css("background-color") == "#ffffff" || versions[i].css("background-color") == "rgb(255, 255, 255)") {
				return true;
			}
		}
		return false;
	}
	
	this.goToActiveVersion = function() {
		eval(versions[activePos].attr("href"));
	}
}
