var oXmlHttp;
var utilityId;
var isShowLoadImage;
var ajaxOutputText;
var debug = false;

function debugView(text)
{
	if(debug)
	{
		if(document.getElementById('testDiv'))
		{
			document.getElementById('testDiv').innerHTML += text; 
		}
	}
}

function itemArray(tagName, id, name)
{
	this.tagName = tagName;
	this.id = id;
	this.name = name;
}

var itemsArray = new Array();

function OnFinderChange(idName, genderOnly, showLoadImage, catId)
{
	utilityId = document.getElementById(idName);

	if (genderOnly == null)
	{
		genderOnly == false;
	}

	isShowLoadImage = ((showLoadImage != null && showLoadImage == false) ? false : true);

	try
	{
		var url = "XmlHttp.aspx?" + GetFinderQueryStringValue(genderOnly);

		// if a category need to be set on the initial load.
		if (catId != null && catId.length > 0)
		{
		    url += "&cat=" + catId;
		}

		oXmlHttp = CreateXmlHttpRequest();

		if (oXmlHttp)
		{
			oXmlHttp.onreadystatechange = GetShoeFinderXMLText;
			oXmlHttp.open("GET", url, true);
			oXmlHttp.send(null);			
		}
	}
	catch(err) {}
}

function GetFinderQueryStringValue(genderOnly)
{
	var qry = "id=" + utilityId.value;

	var idBeginWith = utilityId.id.replace("utilityId", "");
	var ddGender = document.getElementById(idBeginWith + "ddGender");
	var ddSize = document.getElementById(idBeginWith + "ddSize");
	var ddWidth = document.getElementById(idBeginWith + "ddWidth");
	var ddPrice = document.getElementById(idBeginWith + "ddPrice");
	var ddProdChildCategory = document.getElementById(idBeginWith + "ddProdChildCategory");
	var ddBrand = document.getElementById(idBeginWith + "ddBrand");
	var clearIcon = document.getElementById("clearIcon");
	var icon = document.getElementById(idBeginWith + "loaderIcon");

	if (navigator.userAgent.toLowerCase().indexOf("msie 6.0") > -1)
	{
		icon.src = "Images/LoaderIconForIE6.gif";
	}
	
	if (ddGender != null)
	{
		qry += "&gender=" + ddGender.options[ddGender.selectedIndex].value;
	}

	if (!genderOnly)
	{
		if (ddSize != null && ddSize.selectedIndex > 0)
			qry += "&size=" + ddSize.options[ddSize.selectedIndex].value;

		if (ddWidth != null && ddWidth.selectedIndex > 0)
			qry += "&width=" + ddWidth.options[ddWidth.selectedIndex].value;
			
		if (ddPrice != null && ddPrice.selectedIndex > 0)
			qry += "&price=" + ddPrice.options[ddPrice.selectedIndex].value;
			
		if (ddProdChildCategory != null && ddProdChildCategory.selectedIndex > 0)
			qry += "&cat=" + ddProdChildCategory.options[ddProdChildCategory.selectedIndex].value;
			
		if (ddBrand != null && ddBrand.selectedIndex > 0)
			qry += "&brand=" + ddBrand.options[ddBrand.selectedIndex].value;
			
		clearIcon.style.display = "block";
	}
	else
	{
		SetSelectedIndex2(ddSize, 0);
		SetSelectedIndex2(ddWidth, 0);
		SetSelectedIndex2(ddPrice, 0);
		SetSelectedIndex2(ddProdChildCategory, 0);
		SetSelectedIndex2(ddBrand, 0);
		clearIcon.style.display = "none";
	}

	if (!(genderOnly && !isShowLoadImage))	
	{
		ddGender.disabled = true; 
		ddSize.disabled = true;
		ddWidth.disabled = true;
		ddPrice.disabled = true;
		ddProdChildCategory.disabled = true;
		ddBrand.disabled = true;
	}

	if (navigator.userAgent.toLowerCase().indexOf("msie 6.0") > -1)
	{
		ddGender.style.display = "none";
		ddSize.style.display = "none";
		ddWidth.style.display = "none";
		ddPrice.style.display = "none";
		ddProdChildCategory.style.display = "none";
		ddBrand.style.display = "none";
	}

	return (qry);
}

// This was changed due to the fact that the function name is colliding
// w/the same function in the product_script.js and causing issues
//
function SetSelectedIndex2(obj, idx)
{
	obj.selectedIndex = idx;
}

function GetShoeFinderXMLText()
{
	var el = document.getElementById("LoadingImage");

	if(oXmlHttp.readyState == 4)
	{
		if(oXmlHttp.status == 200)
		{
			SetDropDownListItems(oXmlHttp.responseText);
		}

		el.style.display = "none";
	}
	else
	{
		if (isShowLoadImage)
		{
			el.style.display = "block";
		}
	}
}

function SetDropDownListItems (xmlText)
{
	var idBeginWith = utilityId.id.replace("utilityId", "");
	var ddGender = document.getElementById(idBeginWith + "ddGender");
	var ddSize = document.getElementById(idBeginWith + "ddSize");
	var ddWidth = document.getElementById(idBeginWith + "ddWidth");
	var ddPrice = document.getElementById(idBeginWith + "ddPrice");
	var ddProdChildCategory = document.getElementById(idBeginWith + "ddProdChildCategory");
	var ddBrand = document.getElementById(idBeginWith + "ddBrand");

	FillDropDownListItems(ddSize, ddSize.id.split("_")[ddSize.id.split("_").length-1], xmlText, false);
	FillDropDownListItems(ddWidth, ddWidth.id.split("_")[ddWidth.id.split("_").length-1], xmlText, false);
	FillDropDownListItems(ddPrice, ddPrice.id.split("_")[ddPrice.id.split("_").length-1], xmlText, false);
	FillDropDownListItems(ddProdChildCategory, ddProdChildCategory.id.split("_")[ddProdChildCategory.id.split("_").length-1], xmlText, true);
	FillDropDownListItems(ddBrand, ddBrand.id.split("_")[ddBrand.id.split("_").length-1], xmlText, true);

	ddGender.disabled = false;

	if (navigator.userAgent.toLowerCase().indexOf("msie 6.0") > -1)
	{
		ddGender.style.display = "block";
	}
}

function FillDropDownListItems (objDropDown, tagName, xmlText, isSave)
{
	var maxLength = 23;
	var itemPair;
	
	var selectedValue = objDropDown.options[objDropDown.selectedIndex].value;
	
	ClearDropDownListItems (objDropDown);
	
	var items = GetItemContent(tagName, xmlText);

	if (items.length > 1)
	{
		for (var i=0; i<items.length-1; i++)
		{
			itemPair = items[i].split(",");
			if (isSave) itemsArray.push(new itemArray(tagName, itemPair[0], itemPair[1]));
			
			if (itemPair[1].length > maxLength)
			{
				itemPair[1] = itemPair[1].substring(0,maxLength-3) + "...";
			}
			objDropDown.options[i+1] = new Option(trim(itemPair[1]), itemPair[0]);

			if (selectedValue == objDropDown.options[i+1].value)
			{
				objDropDown.options[i+1].selected = true;
			}
		}
	}

	objDropDown.disabled = (objDropDown.options.length == 1);

	if (objDropDown.options.length == 2)
	{
		objDropDown.options[1].selected = true;
	}

	if (navigator.userAgent.toLowerCase().indexOf("msie 6.0") > -1)
	{
		objDropDown.style.display = "block";
	}
}

function GetItemContent(tagName, xmlText)
{
	var startTag = "<" + tagName + ">";
	var endTag = "</" + tagName + ">";
	
	var retValue = xmlText.substring(xmlText.indexOf(startTag), xmlText.indexOf(endTag)).split("|");
	
	if (retValue.length > 0)
	{
		retValue[0] = retValue[0].replace(startTag, "");
	}

	return retValue;
}

function ClearDropDownListItems (objDropDown)
{
	for (var i = objDropDown.options.length-1; i >0; i--)
	{
		objDropDown.options[i] = null;
	}
	
	return objDropDown;
}

function CreateXmlHttpRequest()
{
	var newXMLhttp;

	if (window.XMLHttpRequest)
	{
		newXMLhttp = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		try
		{
			newXMLhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				newXMLhttp = ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(err)
			{
				newXMLhttp = null;
			}
		}
	} 
	else
	{
		newXMLhttp = null;
	}

	return newXMLhttp;
}

function GoSearch(idName, startWith, endWith)
{
	var SearchTerm = "";

	if (startWith == null)
	{
		startWith = "^"; // for Coremetrics
	}

	if (endWith == null)
	{
		endWith = "Footwear";
	}

	SearchTerm = startWith;

	utilityId = document.getElementById(idName);
	var idBeginWith = utilityId.id.replace("utilityId", "");

	var ddGender = document.getElementById(idBeginWith + "ddGender");
	var ddSize = document.getElementById(idBeginWith + "ddSize");
	var ddWidth = document.getElementById(idBeginWith + "ddWidth");
	var ddPrice = document.getElementById(idBeginWith + "ddPrice");
	var ddProdChildCategory = document.getElementById(idBeginWith + "ddProdChildCategory");
	var ddBrand = document.getElementById(idBeginWith + "ddBrand");

	if (utilityId.value == "210")
	{
		SearchTerm += "Clearance+";
		SetCookie('CM_Page', 'Clearance Shoe Finder', null, null);
	}
	else if (utilityId.value == "222")
	{
		SetCookie('CM_Page', 'Slipper Finder', null, null);
	}
	else
	{
		SetCookie('CM_Page', 'Shoe Finder', null, null);
	}

	if (ddGender != null)
	{
		SearchTerm += ddGender.options[ddGender.selectedIndex].text;
	}

	if (ddSize != null && ddSize.selectedIndex > 0)
		SearchTerm += "+size+" + ddSize.options[ddSize.selectedIndex].text;

	if (ddWidth != null && ddWidth.selectedIndex > 0)
		SearchTerm += "+" + ddWidth.options[ddWidth.selectedIndex].text;
		
	if (ddPrice != null && ddPrice.selectedIndex > 0)
	{
		SearchTerm += "+";
		if (ddPrice.options[ddPrice.selectedIndex].text.toLowerCase().indexOf("up") > -1)
		{
			SearchTerm += "over+";
		}
		
		SearchTerm += ddPrice.options[ddPrice.selectedIndex].text.toLowerCase().replace(" and up", "").replace("$", "") + "+dollar";
	}

	if (ddBrand != null && ddBrand.selectedIndex > 0)
	{
		//SearchTerm += "+" + ddBrand.options[ddBrand.selectedIndex].text;
		for (var i=0; i<itemsArray.length; i++)
		{
			if (itemsArray[i].tagName == "ddBrand" && itemsArray[i].id == ddBrand.options[ddBrand.selectedIndex].value)
			{
				SearchTerm += "+" + itemsArray[i].name.replace("&", "/");
				break;
			}
		}
	}

	if (ddProdChildCategory != null && ddProdChildCategory.selectedIndex > 0)
	{
		//SearchTerm += "+" + ddProdChildCategory.options[ddProdChildCategory.selectedIndex].text.replace("&", "/");
		for (var i=0; i<itemsArray.length; i++)
		{
			if (itemsArray[i].tagName == "ddProdChildCategory" && itemsArray[i].id == ddProdChildCategory.options[ddProdChildCategory.selectedIndex].value)
			{
				SearchTerm += "+" + itemsArray[i].name.replace("&", "/");
				break;
			}
		}
	}

	if (SearchTerm.toLowerCase().indexOf("shoe") == -1 && SearchTerm.toLowerCase().indexOf("slipper") == -1 && 
		SearchTerm.toLowerCase().indexOf("sandal") == -1 && SearchTerm.toLowerCase().indexOf("boot") == -1 &&
		SearchTerm.toLowerCase().indexOf("footwear") == -1)
	{
		if (endWith.length > 0)	SearchTerm += "+" + endWith;
	}

	SearchTerm = SearchTerm.replace(/ /g, '+');

	if (document.URL.indexOf("https:") == -1)
		document.location = "SearchResults.aspx?SearchTerm=" + SearchTerm;	
	else
		document.location = "http://www.footsmart.com/SearchResults.aspx?SearchTerm=" + SearchTerm;
}

function MenuOn(obj) 
{
    obj.style.width=150;
}

function MenuOut(obj)
{
    obj.style.width=135;
}

function ChangeHeightForDiv()
{
	var div = document.getElementById("shoeFinderContainer");
	if (div != null && navigator.userAgent.toLowerCase().indexOf("msie 6.0") > -1)
	{
		div.style.height = 235;
	}
}

/* Insole Finder
    6/8/2009  */



function InsoleFinder(idName) 
{


this.oXmlHttp=null;
this.utilityId = document.getElementById(idName);
this.isShowLoadImage=true;
this.ajaxOutputText="";
this.debug = false;
this.itemsArray = new Array();
}

InsoleFinder.prototype.debugView= function(text)
{
	if(debug)
	{
		if(document.getElementById('testDiv'))
		{
			document.getElementById('testDiv').innerHTML += text; 
		}
	}
}

InsoleFinder.prototype.itemArray= function(tagName, id, name)
{
	this.itemArraytagName = tagName;
	this.itemArrayid = id;
	this.itemArrayname = name;
}



InsoleFinder.prototype.OnFinderChange= function(reset)
{
	
	try
	{
		var url = "XmlHttp.aspx?" + this.GetFinderQueryStringValue(reset);

		//alert(url);
		this.oXmlHttp = this.CreateXmlHttpRequest();

		if (this.oXmlHttp)
		{
			this.oXmlHttp.onreadystatechange = function() {this.GetFinderXMLText()}.bind(this);
			this.oXmlHttp.open("GET", url, true);
			this.oXmlHttp.send(null);			
		}
	}
	catch(err) {
	//alert(err);
	}
}

InsoleFinder.prototype.GetFinderQueryStringValue= function(reset)
{
	var qry = "id=" + this.utilityId.value;
    var idBeginWith = this.utilityId.id.replace("utilityId", "");
    var productTypeRefId=document.getElementById(idBeginWith + "productTypeRefId");
    var productTypeDictId=document.getElementById(idBeginWith + "productTypeDictId");
	
	var ddType = document.getElementById(idBeginWith + "ddType");
	var ddFeature = document.getElementById(idBeginWith + "ddFeature");
	var ddFootType = document.getElementById(idBeginWith + "ddFootType");
	var ddLength = document.getElementById(idBeginWith + "ddLength");
	var ddBrand = document.getElementById(idBeginWith + "ddBrand");
	var clearIcon = document.getElementById("insoleclearIcon");
	var icon = document.getElementById(idBeginWith + "insoleloaderIcon");

	if (navigator.userAgent.toLowerCase().indexOf("msie 6.0") > -1)
	{
		icon.src = "Images/LoaderIconForIE6.gif";
	}
	
    qry += "&productTypeRefId=" + productTypeRefId.value;
    qry += "&productTypeDictId=" + productTypeDictId.value;
    qry += "&shoeTypeRefId=" + ddType.options[0].value;
	qry += "&featureRefId=" + ddFeature.options[0].value;
	qry += "&footTypeRefId=" + ddFootType.options[0].value;
	qry += "&insoleLengthRefId=" + ddLength.options[0].value;	

	if (!reset) {
	    
	    
	    
	    if (ddType != null && ddType.selectedIndex > 0)
		    qry += "&shoeType=" + ddType.options[ddType.selectedIndex].value;

	    if (ddFeature != null && ddFeature.selectedIndex > 0)
		    qry += "&feature=" + ddFeature.options[ddFeature.selectedIndex].value;
    		
	    if (ddFootType != null && ddFootType.selectedIndex > 0)
		    qry += "&footType=" + ddFootType.options[ddFootType.selectedIndex].value;
    		
	    if (ddLength != null && ddLength.selectedIndex > 0)
		    qry += "&insoleLength=" + ddLength.options[ddLength.selectedIndex].value;
    		
	    if (ddBrand != null && ddBrand.selectedIndex > 0)
		    qry += "&brand=" + ddBrand.options[ddBrand.selectedIndex].value;
    			
        clearIcon.style.display = "block";
     } else   {
        this.SetSelectedIndex(ddType, 0);
        this.SetSelectedIndex(ddFeature, 0);
        this.SetSelectedIndex(ddFootType, 0);
        this.SetSelectedIndex(ddLength, 0);
        this.SetSelectedIndex(ddBrand, 0);
		clearIcon.style.display = "none";
      }
	

	
	if (navigator.userAgent.toLowerCase().indexOf("msie 6.0") > -1)
	{
		ddType.style.display = "none";
		ddFeature.style.display = "none";
		ddFootType.style.display = "none";
		ddLength.style.display = "none";
		ddProdChildCategory.style.display = "none";
		ddBrand.style.display = "none";
	}

	return (qry);
}


InsoleFinder.prototype.SetSelectedIndex= function(obj, idx)
{
	obj.selectedIndex = idx;
}

InsoleFinder.prototype.GetFinderXMLText= function()
{
    
	var el = document.getElementById("insoleLoadingImage");

	if(this.oXmlHttp.readyState == 4)
	{
	//alert("callback status:"+this.oXmlHttp.status);
		if(this.oXmlHttp.status == 200)
		{
			this.SetDropDownListItems(this.oXmlHttp.responseText);
		}

		el.style.display = "none";
	}
	else
	{
		
        el.style.display = "block";
		
	}
}

InsoleFinder.prototype.SetDropDownListItems= function(jsontext)
{
	var idBeginWith = this.utilityId.id.replace("utilityId", "");
	var ddType = document.getElementById(idBeginWith + "ddType");
	var ddFeature = document.getElementById(idBeginWith + "ddFeature");
	var ddFootType = document.getElementById(idBeginWith + "ddFootType");
	var ddLength = document.getElementById(idBeginWith + "ddLength");
	var ddBrand = document.getElementById(idBeginWith + "ddBrand");
	

    //parses the json to fill items
    //alert(jsontext);
    var values=eval("("+jsontext+")");

	this.FillDropDownListItems(ddType, values["ref"+ddType.options[0].value]);
	this.FillDropDownListItems(ddFeature, values["ref"+ddFeature.options[0].value]);
	this.FillDropDownListItems(ddFootType, values["ref"+ddFootType.options[0].value]);
	this.FillDropDownListItems(ddLength, values["ref"+ddLength.options[0].value]);
	this.FillDropDownListItems(ddBrand, values["brands"]);

}

InsoleFinder.prototype.FillDropDownListItems= function(objDropDown, items)
{
    
	var maxLength = 23;
	
	
	var selectedValue = objDropDown.options[objDropDown.selectedIndex].value;
	
	this.ClearDropDownListItems(objDropDown);
	
	

	if (items && items.length > 0)
	{
		for (var i=0; i<items.length; i++)
		{
			
			var item=items[i].split("|",2);
			objDropDown.options[i+1] = new Option();
			objDropDown.options[i+1].value=item[0];
			objDropDown.options[i+1].text=item[1];

			if (selectedValue == objDropDown.options[i+1].value)
			{
				objDropDown.options[i+1].selected = true;
			}
		}
	}

	objDropDown.disabled = (objDropDown.options.length == 1);

	if (objDropDown.options.length == 2)
	{
		objDropDown.options[1].selected = true;
	}

	if (navigator.userAgent.toLowerCase().indexOf("msie 6.0") > -1)
	{
		objDropDown.style.display = "block";
	}
}

/*GetItemContent(tagName, xmlText)
{
	var startTag = "<" + tagName + ">";
	var endTag = "</" + tagName + ">";
	
	var retValue = xmlText.substring(xmlText.indexOf(startTag), xmlText.indexOf(endTag)).split("|");
	
	if (retValue.length > 0)
	{
		retValue[0] = retValue[0].replace(startTag, "");
	}

	return retValue;
}*/

InsoleFinder.prototype.ClearDropDownListItems= function(objDropDown)
{
	for (var i = objDropDown.options.length-1; i >0; i--)
	{
		objDropDown.options[i] = null;
	}
	
	return objDropDown;
}

InsoleFinder.prototype.CreateXmlHttpRequest= function()
{
	var newXMLhttp;

	if (window.XMLHttpRequest)
	{
		newXMLhttp = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		try
		{
			newXMLhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				newXMLhttp = ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(err)
			{
				newXMLhttp = null;
			}
		}
	} 
	else
	{
		newXMLhttp = null;
	}

	return newXMLhttp;
}

InsoleFinder.prototype.GoSearch= function(startWith, endWith)
{
	var SearchTerm = "";

	if (startWith == null)
	{
		startWith = "^^*"; // for Coremetrics
	}

	if (endWith == null)
	{
		endWith = "Insoles";
	}

	

	
	
	var idBeginWith = this.utilityId.id.replace("utilityId", "");
	var ddType = document.getElementById(idBeginWith + "ddType");
	var ddFeature = document.getElementById(idBeginWith + "ddFeature");
	var ddFootType = document.getElementById(idBeginWith + "ddFootType");
	var ddLength = document.getElementById(idBeginWith + "ddLength");
	var ddBrand = document.getElementById(idBeginWith + "ddBrand");

	
	SetCookie('CM_Page', 'Insole Finder', null, null);
	

	if (ddType != null && ddType.selectedIndex > 0)
	{
		SearchTerm += ddType.options[ddType.selectedIndex].text;
	}
	if (ddFeature != null && ddFeature.selectedIndex > 0)
	{
		SearchTerm += "+"+ddFeature.options[ddFeature.selectedIndex].text;
	}
	if (ddFootType != null && ddFootType.selectedIndex > 0)
	{
		SearchTerm += "+"+ddFootType.options[ddFootType.selectedIndex].text;
	}
	if (ddLength != null && ddLength.selectedIndex > 0)
	{
		SearchTerm += "+"+ddLength.options[ddLength.selectedIndex].text;
	}
	if (ddBrand != null && ddBrand.selectedIndex > 0)
	{
		SearchTerm += "+"+ddBrand.options[ddBrand.selectedIndex].text;
	}
	
	
    
	
	if (SearchTerm.length>0 && SearchTerm.charAt(0)=='+')
	    SearchTerm=SearchTerm.substring(1,SearchTerm.length);
	
	SearchTerm = escape(SearchTerm.replace(/ /g, '+'));

    SearchTerm = startWith + SearchTerm+ "+" + endWith;
	
	

	if (document.URL.indexOf("https:") == -1)
		document.location = "SearchResults.aspx?SearchTerm=" + SearchTerm;	
	else
		document.location = "http://www.footsmart.com/SearchResults.aspx?SearchTerm=" + SearchTerm;
}


InsoleFinder.prototype.ChangeHeightForDiv= function()
{
	var div = document.getElementById("insoleshoeFinderContainer");
	if (div != null && navigator.userAgent.toLowerCase().indexOf("msie 6.0") > -1)
	{
		div.style.height = 235;
	}
}



/* End of Insole Finder */