//global variable.
var TempCurLevel;
//var SearchParams;

function createRequestObject()
{
	if (window.XMLHttpRequest)
  	{xmlhttp=new XMLHttpRequest();}
 	else if (window.ActiveXObject)
    {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
    return xmlhttp;
}

function getFormField(f, field)
{
	var formField;
	
	for (i = 0; i < f.elements.length; i++)
	{
		if (f.elements[i].name.indexOf(field) >= 0)
		{
			formField = f.elements[i];
			break;
		}
	}
	
	return formField;
}


function RemoveRows()
{
	var TblPartsBody = document.getElementById('TblPartsBody');
	
	if (TblPartsBody)
	{
		var NumRows = TblPartsBody.rows.length;
		if (NumRows > 0)
		{
			do
			{
				TblPartsBody.deleteRow(0);
			}
			while (TblPartsBody.rows.length > 0) 
		}
	}
}

function SelectParent(CurObj, PartsBrandID)
{
	
	if (PartsBrandID != 23 && parseInt(PartsBrandID)>0)
	{
		document.location = '/pages/' + ModFriendlyURL + '/viewbybrand/' + PartsBrandID  + '/default.aspx';
	}
	else
	{	
		document.location = '/pages/' + ModFriendlyURL + '/userparts/' + PartsBrandID  + '/default.aspx';
	}
}

function GetSupersessions(bCode,s,pId)
{
	var httpobj=createRequestObject();
	httpobj.abort();		
	httpobj.onreadystatechange=function()
	{
		if(httpobj.readyState==4) 
		{						
			if(httpobj.status==200)
			{
				//alert(httpobj.responseText);	
				var div = document.getElementById('superSku_' + pId);
				div.innerHTML = 	httpobj.responseText;	
				//alert(httpobj.responseText);
			}
		}
	}
	ajaxUrl = "/modules/oemparts/oemparts_getvalues.aspx";
	url = ajaxUrl + "?SearchType=supersession&BrandCode=" + bCode + "&SKU=" + s + "&PageFriendlyURL=" + PageFriendlyURL + "&ModFriendlyURL=" + ModFriendlyURL;
	//alert(url);
	//document.write(url);
	httpobj.open("GET",url, true);
	httpobj.send(null);		
}


function GenImage(AssemblyID, BrandCode, Zoom)
{
//alert("AssemblyID = " + AssemblyID + " BrandCode = " + BrandCode + " Zoom = " + Zoom);
		
		//ShowHideLoading(1);
		if(Zoom==null){
			Zoom=2;
		}
		//document.write("SearchType=getimage&ParentID=" + AssemblyID + "&BrandCode=" + BrandCode + "&Zoom=" + Zoom);
		//alert("SearchType=getimage&ParentID=" + AssemblyID + "&BrandCode=" + BrandCode + "&Zoom=" + Zoom);
		var httpobj=createRequestObject();
		httpobj.onreadystatechange=function()
		{
			if(httpobj.readyState==4) 
			{						
				if(httpobj.status==200)
				{
				
					var str = "";
					str = httpobj.responseText;
					
					//Err handling 
					var errChecker = "";
					errChecker = httpobj.responseText; 
					
					if(errChecker.indexOf("0x0214") != -1)
					{
						SetErrMsg_Detail(errChecker, "image");
						return false;
					}
					//alert("img src = " + str);
					document.getElementById("iFR").src=str;
					//ShowHideLoading(0);
					
				}
			}
		}
		
		httpobj.open("GET","/modules/oemparts/oemparts_getvalues.aspx?SearchType=getimage&ParentID=" + AssemblyID + "&BrandCode=" + BrandCode + "&Zoom=" + Zoom + "&PageFriendlyURL=" + PageFriendlyURL + "&ModFriendlyURL=" + ModFriendlyURL, true);
		httpobj.send(null);
			
	//}
	
	
}

function addAdditionalLoadEvent(func) {
	var oldonload = window.onload;
	if (window == null || window.onload == null || window.onload.length <= 0) {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func;
		}
	}
	
}

/*
-- new Brand ID Dropdown
-- Bekim - 10.15.07
*/
function GetAssemblyDDl(ParentID, CurLevel, BrandCode)
{
	
	//alert("Parent: " + ParentID + " CurLevel: " + CurLevel + " BrandCode:" + BrandCode);
	var DivDropDowns = document.getElementById('DivDropDowns');
	//alert(DivDropDowns);
	var TempNumLevels = DivDropDowns.childNodes.length;
	//alert(TempNumLevels);
	
	if (CurLevel < TempNumLevels)
	{
		RemoveDivs(DivDropDowns, CurLevel)
		if((ParentID == 0 && CurLevel == 0) || ParentID > 0)
		    PopulateBrandDDL(ParentID,CurLevel,BrandCode);
	}
	else
	{
		PopulateBrandDDL(ParentID,CurLevel,BrandCode);
	}
	
}

function PopulateBrandDDL(ParentID,CurLevel,BrandCode)
{
	//alert("PopulateBrandDDL Values: " + ParentID + " - " + CurLevel + " - " + BrandCode);
	
	//Set Root Assembly Id...this is the assemblyID that gets you to the partslist.
	var rootAssemblyId = document.getElementById("hidRootAssemblyId");
	rootAssemblyId.value = ParentID;
	
	if(isNaN(BrandCode)==false || BrandCode == "")
	{
		//alert("IN HERE");
		BrandCode=GetBrandCode();
		
		if(isNaN(BrandCode)==false || BrandCode == "")
		{	
			//alert("3rd base");
			return false;	
		}
		
	}
	
	document.getElementById("ImgLoading").style.display = "inline";	
	
	var httpobj=createRequestObject();
			httpobj.onreadystatechange=function()
			{
				if(httpobj.readyState==4)
				{						
					if(httpobj.status==200)
					{
						var Success = 0;
						var str = "";
						str = httpobj.responseXML;	
						
						//Err handling 
						var errChecker = "";
						errChecker = httpobj.responseText; 
						
						if(errChecker.indexOf("0x0214") != -1)
						{
							SetErrMsg(errChecker);
							return false;
						}
												
						if(httpobj.responseText == 'ERROR')
						{							
							document.location = PageFriendlyURL + '/' + ModFriendlyURL + '/viewbrands/' + GetBrandID()  + '/default.aspx';
						}
						else
						{
							var doc= httpobj.responseXML.documentElement;
							var parent = doc.getElementsByTagName('parent');
							var select = doc.getElementsByTagName('select');
							document.getElementById("ImgLoading").style.display = "none";							
							
							var CurDiv = document.createElement("SPAN");
							CurDiv.id = parseFloat(CurLevel) + 1;
							CurDiv.style.width = "97%";
							CurDiv.style.display = "inline";
							CurDiv.innerHTML = select[0].firstChild.nodeValue;							
							
							TempCurLevel = parseFloat(CurLevel) + 1;
							Success = 1;
							if (parent[0].firstChild.nodeValue == 1)
							{
								DivDropDowns.appendChild(CurDiv)
							}
							else
							{
								//populuate assemblies								
								var tmpCurHTML = CurDiv.innerHTML;
								CurDiv.innerHTML = 'Change Assembly: ' + tmpCurHTML;
								document.getElementById('Div_Assembly').appendChild(CurDiv);
								//PopulateParts(this.form, Assembly_ID);
								
								//grab the current drop down..
								SelectAssembly(CurLevel);								
								
								//show panels
								ShowHide('WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1_PanelSelect', 0);
								ShowHide('WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1_PanelDetail', 1);
								
								/* 
								** Put this in because of an IE bug which would keep focus on a hidden dropdown list 
								** so when you scroll the middle mouse button when the detail panel has just been
								** set to visible you would keep switching the assembly
								*/
								//var txtSKU = document.getElementById('WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1__ctl0_SearchSKU');
								var txtSKU = document.getElementById('iFR');
								txtSKU.focus();
							}
						}							
					}
				}
			}
			
			//httpobj.open("GET","/modules/oemparts/oemparts_getvalues.aspx?SearchType=getdropdown&ParentID=" + ParentID + "&CurLevel=" + CurLevel + "&BrandCode=" + BrandCode + "&BrandID=" + GetBrandID() + "&PageFriendlyURL=" + PageFriendlyURL + "&ModFriendlyURL=" + ModFriendlyURL, true);
			var url = "/modules/oemparts/oemparts_getvalues.aspx?SearchType=getdropdown&ParentID=" + ParentID + "&CurLevel=" + CurLevel + " &BrandCode=" + BrandCode + "&PageFriendlyURL=" + PageFriendlyURL + "&ModFriendlyURL=" + ModFriendlyURL;
			//alert(url);
			//document.write(url);
			//httpobj.open("GET","/modules/oemparts/oemparts_getvalues.aspx?SearchType=getdropdown&ParentID=" + ParentID + "&CurLevel=" + CurLevel + "&BrandCode=" + BrandCode + "&PageFriendlyURL=" + PageFriendlyURL + "&ModFriendlyURL=" + ModFriendlyURL, true);
			httpobj.open("GET",url, true);
			httpobj.send(null);
}


function PopulateParts(f, AssemblyID)
{
	//Get Root AssemblyId
	var rootAssemblyId = document.getElementById("hidRootAssemblyId");
		
	//alert("/modules/oemparts/oemparts_getvalues.aspx?SearchType=getparts&ParentID=" + AssemblyID + "&BrandCode=" + TempBrandID + "&PageFriendlyURL=" + PageFriendlyURL + "&ModFriendlyURL=" + ModFriendlyURL);
	GetModelName();
	ShowHideLoading(1);
	RemoveRows();
	oZoomRs();
	
	var DDlBrands = document.getElementById('WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1_DDlBrands');
	
	var TempBrandID = GetBrandCode();
	var PartsBrandID = TempBrandID;
	var tblParts_main = document.getElementById('tblParts_main');
	tblParts_main.innerHTML = "";	

	GenImage(AssemblyID, TempBrandID);
	
	var httpobj=createRequestObject();
	
		httpobj.onreadystatechange=function()
		{
			if(httpobj.readyState==4)
			{
				if(httpobj.status==200)
				{
					var str = "";
					str = httpobj.responseText;
					
				
					//Err handling 
					var errChecker = "";
					errChecker = httpobj.responseText; 					
					if(errChecker.indexOf("0x0214") != -1)
					{
						SetErrMsg_Detail(errChecker, "parts");
						return false;
					}
					
					
					if(tblParts_main)
					{
						
						tblParts_main.innerHTML = str;						
						ShowHideLoading(0);
					}
					else
					{
						
						
						ShowHideLoading(0);
					}
					
					ShowHideLoading(0);
				}
				
				ShowHideLoading(0);
			}
		}
		var url = "/modules/oemparts/oemparts_getvalues.aspx?SearchType=getparts&ParentID=" + AssemblyID + "&BrandCode=" + TempBrandID + "&PageFriendlyURL=" + PageFriendlyURL + "&ModFriendlyURL=" + ModFriendlyURL +"&rootId=" + rootAssemblyId.value;
		//alert(url);
		//document.write(url);
		httpobj.open("GET",url, true);
		httpobj.send(null);
		
	
}

function SelectAssembly(CurLevel)
{
	//alert(Assembly_ID);
	var children = document.getElementById('Div_Assembly').getElementsByTagName('select');
	var DDlAssembly =  children[0];
	//alert(DDlAssembly.id);
	
	//var DDlAssembly =  document.getElementById('DDlSelect_' + parseFloat(CurLevel + 1));
	var Debug =  document.getElementById('Debug');
	
	
	DDlAssembly.options[0].selected = true;
	
	
	//alert(DDlAssembly.options.length);							
	if (DDlAssembly.options.length > 1)
	{

		var OptionNum = 1;
		var Populated = false;
		if(Assembly_ID != '')
		{
			
			for (i=0; i <= DDlAssembly.options.length - 1; i++)
			{
				//alert(DDlAssembly.options[i].value + " - " + Assembly_ID);
				//Debug.innerHTML = Debug.innerHTML + DDlAssembly.options[i].value + " - " + Assembly_ID + "<br>";
				if(DDlAssembly.options[i].value == Assembly_ID)
				{
					
					//DDlAssembly.options[i].selected = true;
					
					PopulateParts(this.form, Assembly_ID);
					Populated = true;
					OptionNum = i;
				}
			}
			if (Populated == false)
			{
				
				PopulateParts(this.form, Assembly_ID);
			}
		}
		else
		{
			//alert(DDlAssembly.options[1].value);
			DDlAssembly.options[1].selected = true;
			
			PopulateParts(this.form, DDlAssembly.options[1].value)
		}

	}
	//alert(DDlAssembly.options[OptionNum].value);
	//alert(DDlAssembly.options[OptionNum].innerHTML);
	DDlAssembly.options[OptionNum].selected = true;
}

function RemoveDivs(ParentDiv, CurLevel)
{
	
	if (ParentDiv.childNodes.length > 0)
	{
		
		var TempNumDivs = ParentDiv.childNodes.length;
		
		for(i=CurLevel + 1;i<=TempNumDivs;i++)
		{
			
			var TempDiv = document.getElementById(i);
			
			if (TempDiv != null)
			{ParentDiv.removeChild(TempDiv)}
		}
	}
}


function ShowHide(FormID, Toshow)
{
	var elem  = document.getElementById(FormID);
	
	if (Toshow ==1)
	{elem.style.display = "inline";}
	else
	{elem.style.display = "none";}
}

function GetModelName(){
	var txtModel = document.getElementById('txtModelName');
	var labelModel = document.getElementById('WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1_lblModel');
	var DDlBrands = document.getElementById('WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1_DDlBrands');
	var w = DDlBrands.selectedIndex;
	var selected_text = DDlBrands.options[w].text;
	var Rtrn = selected_text + " : " + txtModel.value;
	
	labelModel.innerHTML = Rtrn;
	return Rtrn;
}

function Search()
{
		var BrandCode = GetSearchBrandCode();
		var txtSKU = document.getElementById('WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1__ctl0_SearchSKU');
		var txtModel = document.getElementById('WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1__ctl0_SearchModels');
		var srcParams = document.getElementById('SearchParams');
		var SKUSearch = txtSKU.value;
		var ModelSearch = txtModel.value;
		var DDlBrands = document.getElementById('WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1__ctl0_DDlBrandSearch');
		var w = DDlBrands.selectedIndex;
		var selected_text = DDlBrands.options[w].text;
		//document.write("/modules/oemparts/oemparts_getvalues.aspx?SearchType=search&SKUSearch=" + SKUSearch + "&ModelSearch=" + ModelSearch + "&BrandCode=" + BrandCode + "&PageFriendlyURL=" + PageFriendlyURL + "&ModFriendlyURL=" + ModFriendlyURL);
		//alert("/modules/oemparts/oemparts_getvalues.aspx?SearchType=search&SKUSearch=" + SKUSearch + "&ModelSearch=" + ModelSearch + "&BrandCode=" + BrandCode + "&PageFriendlyURL=" + PageFriendlyURL + "&ModFriendlyURL=" + ModFriendlyURL);
		ShowHideLoading(1);
	
		var httpobj=createRequestObject();
		httpobj.onreadystatechange=function()
		{
			if(httpobj.readyState==4) 
			{						
				if(httpobj.status==200)
				{
				
					
					var str = httpobj.responseText;
					
					if(str.indexOf("0x0214") != -1)
					{
						SetErrMsg_Detail("We're sorry, but there was a problem loading your image. Please select a new item and try again.");
						ShowHideLoading(0);
						return false
					}
					
					
					var checkIt = str.toUpperCase()
					
					if(str == "FALSE")
					{
						//alert("we found it!");
						NoResults();
						ShowHideLoading(0);
						return "";
						//break;
					}
					else if(checkIt.indexOf("ERROR: ") != -1)
					{
						//alert("ERROR!");
						ErrorResults();
						ShowHideLoading(0);
						return "";
						//break;
					}
					
					ShowHide('WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1_pnlResults', 1);
					
					
					

					srcParams.innerHTML= "<b>Active Search: </b>" + selected_text + " - " + SKUSearch + " " + ModelSearch ;
										
					ShowHide('WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1_PanelSelect', 0);
					
										
					var results = document.getElementById('results');
					//alert(results);
					results.innerHTML = str;
					
					
					
					ShowHideLoading(0);
					
				}
				
				ShowHideLoading(0);
			}
			
		}
		
		httpobj.open("GET","/modules/oemparts/oemparts_getvalues.aspx?SearchType=search&SKUSearch=" + SKUSearch + "&ModelSearch=" + ModelSearch + "&BrandCode=" + BrandCode + "&PageFriendlyURL=" + PageFriendlyURL + "&ModFriendlyURL=" + ModFriendlyURL, true);
		httpobj.send(null);
			
	//}
	
	
}

function NoResults()
{
	var NoRes  = document.getElementById("srchNoResults");
	var SkuSearch = document.getElementById("WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1__ctl0_SearchSKU");
	var SkuModel = document.getElementById("WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1__ctl0_SearchModels");
	
	NoRes.innerHTML = "Sorry, but there are no results for '" + SkuSearch.value + " " + SkuModel.value + "'";
	NoRes.style.display = "inline";
	
}

function ErrorResults()
{
	var NoRes  = document.getElementById("srchNoResults");
	var SkuSearch = document.getElementById("WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1__ctl0_SearchSKU");
	var SkuModel = document.getElementById("WMS_MasterTemplate_Container_CenterContent__ctl0__ctl0__ctl1__ctl0_SearchModels");
	
	NoRes.innerHTML = "Sorry, but there was a problem processing '" + SkuSearch.value + " " + SkuModel.value + "'. Please revise your search and try again.";
	NoRes.style.display = "inline";
	
}


function ShowHideLoading(Visible)
{
	var div  = document.getElementById("Loading");
	
	if (Visible ==1)
	{
		div.style.display = "inline";
		
		//div.style.position="absolute";
		//div.style.left="0px";
		//div.style.right="0px";
		//div.style.width='100%';
		//div.style.height='100%';
		//div.style.background="url(images/chequerboard-black.gif)";
		//div.style.zindex=999;
	}
	else
	{
		div.style.display = "none";
		
	}
	
}


function SetErrMsg(Msg)
{
	document.getElementById("ImgLoading").style.display = "none";
	var err  = document.getElementById("srchNoResults");
	err.innerHTML = Msg;
	err.style.display = "inline";

}

function SetErrMsg_Detail(Msg, Section)
{
	document.getElementById("iFR").src = "";
	ShowHideLoading(0);
	var err  = document.getElementById("errDetail");
	err.innerHTML = Msg;
	err.style.display = "inline";

}
