//--------------------------------------------------------------------------------------
search_settings = new Array(-1,-1,-1);

function cSearchObj() {
	this.ProductLine = new Array();
	this.ModelName = new Array();
	this.DocumentType = new Array();
	this.DocumentTypeStr = new Array();
	this.ModelYear = new Array();
	this.Results = new Array();		// search results array of cPersonIDObj
	this.ProcurementList = new Array();
	this.CurrentState = "ready";
	this.ListWin = null;
	this.TimeoutID = -1;
	
	this.DocumentTypeStr["SM"]	= "Service Manual"
	this.DocumentTypeStr["OM"]	= "Owner's Manual"
	this.DocumentTypeStr["OSM"]	= "Owner's Service Manual"
	this.DocumentTypeStr["AM"]	= "Assembly Manual/Bulletin"
	this.DocumentTypeStr["RR"]	= "Ready Reference"
	this.DocumentTypeStr["WD"]	= "Wiring Diagram"
	this.DocumentTypeStr["US"]	= "Update Seminars"
	this.DocumentTypeStr["PM"]	= "Warranty Policy Manual"	//"Procedure Manual"
	this.DocumentTypeStr["TC"]	= "Tool Catalog"
	this.DocumentTypeStr["MI"]	= "Model ID Guide"
	this.DocumentTypeStr["SO"]	= "Service Ops Guide"
	this.DocumentTypeStr["SS"]	= "Service Support Guide"
	this.DocumentTypeStr["CSI"]	= "CSI Loyalty Guide"
	this.DocumentTypeStr["DWP"]	= "Delivered With Pride Manual"
	this.DocumentTypeStr["UK"]	= "UK"
	
	this.DoCommand = fSearchDoCommand;
	this.Init = fSearchInit;			// get Product Line and Document Type menu options
	this.InitDone = fSearchInitDone;	// initialize first two dropdown menus
	this.GetModelYear = fSearchGetModelYear;
	this.GetModelYearDone = fSearchGetModelYearDone;	
	this.GetModel = fSearchGetModel;
	this.GetModelDone = fSearchGetModelDone;		
	//this.SearchDocs = fSearchDocs;
	this.SearchTechManuals = fSearchTechManuals;
	this.SearchGeneralRef = fSearchGeneralRef;
	this.SearchDocsDone = fSearchDocsDone;
	this.WriteList = fSearchWriteList;
	this.Clear = fSearchClear;
	this.ShowDocIndex = fSearchShow;
	this.AddToList = fSearchAddToList;
	this.AddToListDone = fSearchAddToListDone;
	this.ShowProcurementList = fSearchShowProcList;
	this.UpdateProcurementList = fSearchUpdateProcList;
	this.Closing = fSearchClosing;
	this.ResetTimeout = fSearchResetTimeout;
	this.ClearTimeout = fSearchClearTimeout;
}

function cResultsObj(pModelYr, pModelYrEnd, pModel, pMarketName, pPartNum, pRetailPrice, pFileAssemPath, pDocTypeStr, pDisplayMethod, pDocType, pPrice, pPages, pBOID) {
	this.MarketName = pMarketName;
	this.PartNum = (typeof pPartNum != "undefined") ? pPartNum : "";
	this.RetailPrice = (typeof pRetailPrice != "undefined") ? pRetailPrice : "";
	this.FileAssemPath = (typeof pFileAssemPath != "undefined") ? pFileAssemPath : "";
	this.DocType = pDocTypeStr;
	this.DocTypeCode = pDocType;
	this.DisplayMethod = pDisplayMethod;
	this.Price = pPrice;
	this.Pages = pPages;
	this.BOID = pBOID;
	
	/*  Formulate document titles from model year, model, and marketing name:
	ModelYear-ModelYearEnd  ModelName (DocTitle)
	1.	If ModelYearEnd is null, omit the -
	2.	If ModelYear is 0 or null, omit the entire year part.
	3.	If ModelName is null, omit the () around the document title and omit the entire year part.
	Some different example with the document type are:
	2000-2007 DR-Z400/E                                Service Manual
	2002-2007 LT-A400/F (Eiger)                        Service Manual
	2008 LT-A750X (King Quad)                          Service Manual  
	2008 GZ250                                         Assembly Manual
	2001 Technical Update Seminar Manual               Update Seminars           
	Wiring Diagram Manual – 2000                       Wiring Diagram
	Service Operations Guide                           Service Operations
	*/
	switch (pDocType) {
		case "RR":
		case "WD":
		case "US":
			this.DocTitle = pMarketName;
			break;
			
		default:
			this.DocTitle = "";
			if (pModelYr != "0" && pModelYr != "") {
				this.DocTitle += pModelYr;
				if (parseInt(pModelYrEnd,10) > parseInt(pModelYr,10)) {
					this.DocTitle += "-" + pModelYrEnd;
				}
			}
			if (pModel != "" && typeof pModel != "undefined") {
				this.DocTitle += " " + pModel;
			}
			if (pMarketName != "" && typeof pMarketName != "undefined") {
				if (this.DocTitle.length > 0) {
					this.DocTitle += " (" + pMarketName + ")";
				} else {
					this.DocTitle = pMarketName;
				}
			}
			break;
	}
}

function fSearchDoCommand(pCmd, pArgs) {
	switch(pCmd.toLowerCase()) {
		case "onload":
			//fInitBrowserFlags();
			pgLoaded = true;
			switchNavIndicators(-1);
			this.Init();
			//document.techManuals.productLine.focus();
			//this.ResetTimeout();
			break;
				
		case "search":
			if (this.CurrentState == "ready") this.Search();
			break;
				
		case "clear":
			if (this.CurrentState == "ready") this.Clear();
			break;
			
		case "show":
			this.ShowDocIndex(pArgs);
			break;
	}
}

function fSearchInit() {
	//document.getElementById("procListCount").innerHTML = this.ProcurementList.length;
	document.techManuals.searchType.value = "Init";
	document.techManuals.submit();
}

function fSearchInitDone(pForm) {
	var f, vIndex;
	var vStatus = new String(pForm.status1.value);
	if (vStatus != "") {
		alert(vStatus);
		return;
	}
	var f = document.techManuals;
	this.ProductLine = fGetArrayData(pForm.productLine.value);
	fClearSelectOptions(f.productLine);
	vIndex = 0;
	f.productLine.options[vIndex++] = new Option("Select a Vehicle Type...", "", false, false);
	if (this.ProductLine.length > 0) 
	{
		
		if( /^\d+$/.test(search_settings[0]) )
		{
			// search_settings are set with numeric values (by return 2 shopping)
			for (var i=0; i < this.ProductLine.length; i++) 
			{
				plTxt = this.ProductLine[i];
				if( plTxt == "Vintage" ) plTxt = "Vintage (1989 or earlier)";

				if( vIndex != search_settings[0] )
				{
					f.productLine.options[vIndex++] = new Option(plTxt, this.ProductLine[i], false, false);
				}
				else
				{
					f.productLine.options[vIndex++] = new Option(plTxt, this.ProductLine[i], true, true);
				}
			}
		}
		else
		{
			// search_settings are set with string values (by external links)
			for (var i=0; i < this.ProductLine.length; i++) 
			{
				plTxt = this.ProductLine[i];
				if( plTxt == "Vintage" ) plTxt = "Vintage (1989 or earlier)";

				if( this.ProductLine[i] != search_settings[0] )
				{
					f.productLine.options[vIndex++] = new Option(plTxt, this.ProductLine[i], false, false);
				}
				else
				{
					f.productLine.options[vIndex++] = new Option(plTxt, this.ProductLine[i], true, true);
				}
			}
		}
	}

	f.productLine.style.visibility = "visible";
	document.getElementById("productLine_lbl").style.visibility = "visible";

	fClearSelectOptions(f.modelYear);
	f.modelYear.disabled = true;
	f.modelYear.style.visibility = "hidden";

	fClearSelectOptions(f.modelName);	
	f.modelName.disabled = true;
	f.modelName.style.visibility = "hidden";
	
	if( search_settings[0] != -1 )
	{
		gSearch.GetModelYear()	
	}
	
}
function fSearchGetModelYear() {
	var f = document.techManuals;
	var menuValue = fGetMenuValue(f.productLine);
	if (menuValue != "") {
		search_settings[0] = f.productLine.selectedIndex;
		switchNavIndicators(menuValue);
		f.searchType.value = "GetModelYear";
		f.submit();
	} else {
		// If selected nothing disable the next two menus
		search_settings[0] = -1;
		search_settings[1] = -1;
		search_settings[2] = -1;		
		fClearSelectOptions(f.modelYear);
		fClearSelectOptions(f.modelName);
		f.modelYear.disabled = true;
		f.modelYear.style.visibility = "hidden";
		f.modelName.disabled = true;
		f.modelName.style.visibility = "hidden";
	}
}
function fSearchGetModelYearDone(pForm) {
	this.ModelYear = fGetArrayData(pForm.modelYear.value);
	var f = document.techManuals;
	fClearSelectOptions(f.modelYear);
	fClearSelectOptions(f.modelName);
	vIndex = 0;
	f.modelYear.options[vIndex++] = new Option("Select a Model Year...", "", false, false);
	if (this.ModelYear.length > 0) 
	{
		if( /^\d{1,3}$/.test(search_settings[1]) )
		{
			// search_settings are set with numeric values (by return 2 shopping)
			for (var i=this.ModelYear.length-1; i > 0; i--) 
			{
				if( vIndex != search_settings[1] )
				{
					f.modelYear.options[vIndex++] = new Option(this.ModelYear[i], this.ModelYear[i], false, false);
				}
				else
				{
					f.modelYear.options[vIndex++] = new Option(this.ModelYear[i], this.ModelYear[i], true, true);
				}
			}
		}
		else
		{
			// search_settings have string values (set by external link)
			for (var i=this.ModelYear.length-1; i > 0; i--) 
			{
				if( this.ModelYear[i] != search_settings[1] )
				{
					f.modelYear.options[vIndex++] = new Option(this.ModelYear[i], this.ModelYear[i], false, false);
				}
				else
				{
					f.modelYear.options[vIndex++] = new Option(this.ModelYear[i], this.ModelYear[i], true, true);
				}
			}
		}
	}
	f.modelYear.disabled = false;
	f.modelYear.style.visibility = "visible";
	document.getElementById("modelYear_lbl").style.visibility = "visible";
	document.getElementById("productLine_lbl").style.color = "#cccccc";
	
	fClearSelectOptions(f.modelName);	
	f.modelName.disabled = true;
	f.modelName.style.visibility = "hidden";
	
	if( search_settings[1] != -1 )
	{
		gSearch.GetModel()	
	}
	
}
function fSearchGetModel() {
	var f = document.techManuals;
	if (fGetMenuValue(f.modelYear) != "") {
		search_settings[1] = f.modelYear.selectedIndex;
		f.searchType.value = "GetModel";
		f.submit();
	} else {
		// If selected nothing disable the next menu
		search_settings[1] = -1;
		search_settings[2] = -1;		
		fClearSelectOptions(f.modelName);
		f.modelName.disabled = true;
		f.modelName.style.visibility = "hidden";
	}	
}
function fSearchGetModelDone(pForm) {
	this.ModelName = fGetArrayData(pForm.modelName.value);
	var vMktName = fGetArrayData(pForm.mktName.value);
	var vScModelName = fGetArrayData(pForm.scModelName.value);

	var f = document.techManuals;
	
	fClearSelectOptions(f.modelName);

	vIndex = 0;
	f.modelName.options[vIndex++] = new Option("Select a Model...", "", false, false);
	f.modelName.options[vIndex++] = new Option("All", "", false, false);
	if (this.ModelName.length > 0) 
	{
		if( /^\d{1,3}$/.test(search_settings[2]) )
		{
			// search_settings are set with numeric values (by return 2 shopping)
			for (var i=0; i < this.ModelName.length; i++) 
			{
				var vStr = (vMktName[i] == "") ? this.ModelName[i] : vMktName[i] + " (" + this.ModelName[i] + ")";
				if( vIndex != search_settings[2] )
				{
					f.modelName.options[vIndex++] = new Option(vStr, this.ModelName[i], false, false);
				}
				else
				{
					f.modelName.options[vIndex++] = new Option(vStr, this.ModelName[i], true, true);
				}
			}
		}
		else
		{
			// search_settings have string values (by external link)
			for (var i=0; i < this.ModelName.length; i++) 
			{
				var vStr = (vMktName[i] == "") ? this.ModelName[i] : vMktName[i] + " (" + this.ModelName[i] + ")";
				if( vScModelName[i] != search_settings[2] )
				{
					f.modelName.options[vIndex++] = new Option(vStr, this.ModelName[i], false, false);
				}
				else
				{
					f.modelName.options[vIndex++] = new Option(vStr, this.ModelName[i], true, true);
				}
			}			
		}
	}	
	f.modelName.style.visibility = "visible";
	f.modelName.disabled = false;
	document.getElementById("modelName_lbl").style.visibility = "visible";
	document.getElementById("modelYear_lbl").style.color = "#cccccc";

	if( search_settings[2] != -1 )
	{
		gSearch.SearchTechManuals();
	}
}
function fSearchTechManuals() {
	var f = document.techManuals;
	if( f.modelName.selectedIndex > 0 ) search_settings[2] = f.modelName.selectedIndex;
	if (fGetMenuValue(f.modelYear) != "" && fGetMenuValue(f.productLine) != "") {
		f.searchType.value = "technicalSearch";
		document.getElementById("searchResultsDiv").style.display = "block";
		f.submit();	
	} else {
		alert("Please select Product Line and Model Year.");
	}
}
function fSearchGeneralRef() {
	var f = document.generalRef;
	if (fGetMenuValue(f.documentType) != "") {
		f.submit();	
	} else {
		alert("Please select Document Type.");
	}	
}
function fSearchDocsDone(pForm) {
	top.vCurrentState = "ready";
	// check status
	var vStatus = new String(pForm.status1.value);
	if (vStatus != "") {
		fClearWaitCursor(document);
		alert(vStatus);
		return;
	}
	// clear old search results
	this.Results = new Array();
	// initialize new search results
	var vModelYr = fGetArrayData(pForm.modelYear.value);
	var vModelYrEnd = fGetArrayData(pForm.modelYearEnd.value);
	var vModel = fGetArrayData(pForm.modelName.value);
	var vMarketName = fGetArrayData(pForm.docName.value);
	var vPartNum = fGetArrayData(pForm.partNum.value);
	var vFileAssemPath = fGetArrayData(pForm.fileAssemPath.value);
	var vDocType = fGetArrayData(pForm.docType.value);
	var vDisplayMethod = fGetArrayData(pForm.displayMethod.value);
	var vPrice = fGetArrayData(pForm.price.value);
	var vPages = fGetArrayData(pForm.pages.value);
	var vBOID = fGetArrayData(pForm.BOID.value);
//alert("doc types = " + vDocType);

	for (var i=0; i<vModelYr.length; i++) {
		vDocumentType = (typeof vDocType[i] != "undefined") ? this.DocumentTypeStr[vDocType[i]] : "";
		this.Results[i] = new cResultsObj(vModelYr[i], 
										  vModelYrEnd[i], 
										  vModel[i], 
										  vMarketName[i],
										  vPartNum[i],
										  "",
										  vFileAssemPath[i],
										  vDocumentType,
										  vDisplayMethod[i],
										  vDocType[i],
										  vPrice[i],
										  vPages[i],
										  vBOID[i]
										  );
	}
	this.WriteList();
	/*if (vTotalRowCount > vReturnedRowCount) {
		alert("First " + vReturnedRowCount + " of " + vTotalRowCount + ".\n\nYou may wish to refine your search and try again.");
	}
	if (vTotalRowCount < 0) {
		alert("No matches.");
	}*/
}

function fSearchWriteList() {
	var vPartNo, vAddToListBtn
	var vHTMLStr = "";
	var vLastColWidth = (this.Results.length <= 6) ? "40" : "23";
	vHTMLStr += "<table border='0' cellspacing='0' cellpadding='2' width='100%'>";
				 /* "<tr>"+
					"<td></td>"+
					"<td width='250'></td>"+
					"<td width='100' align='center'></td>"+
					"<td width='100' align='center'></td>"+
					"<td width='200' align='center'></td>"+
				  "</tr>" */
	for (var i=0; i<this.Results.length; i++) {
		var vRowClass = (i%2 == 0)?"a":"b";
		vPartNo = "";
		vAddToListBtn = "";
		if (this.Results[i].DocTypeCode != "AM" ) {
			vPartNo = this.Results[i].PartNum.substr(0,15);
			vAddToListBtn = "<img src='graphics/btnAdd.gif' " +
								"width='93' height='17' border='0' " +
								"onMouseOver='this.src=\"graphics/btnAdd-Hi.gif\"' " +
								"onMouseOut='this.src=\"graphics/btnAdd.gif\"'>";
		}
		
		vHTMLStr += "<tr>" +
					"<td align='right' class='srRow_" + vRowClass + "'>" +
						"<!--<img src='graphics/vidas/acrobat_logo.gif' width='29' height='31' border='0'>-->" +						
					"</td>" +
                	"<td class='srRow_" + vRowClass + "' align='left'>" +
						"<b>" + this.Results[i].DocTitle + "</b>" +
						"<br>" + this.Results[i].DocType + " - " +
						"$" + pennies_2_dollars(this.Results[i].Price);

		if( this.Results[i].Pages > 0 )
		{
			vHTMLStr += " (" + this.Results[i].Pages + "+ pages)";
		}
		
		vHTMLStr += "</td>";
					
					
		vHTMLStr += "<td class='srRow_"+vRowClass+"' align='center'>";
						if( vAddToListBtn != "" )
						{
							vHTMLStr += "<a href='cart.add.asp?boid=" + this.Results[i].BOID +"&rtn=" 
																	+ search_settings.join("|") + "'>" +
											vAddToListBtn + 
										"</a>"
						}
						else
						{
							vHTMLStr += "&nbsp;";
						}
		vHTMLStr += "</td>";
		
		if( false )
		{
			// taken out for a contractual reason
			vHTMLStr += "<td class='srRow_"+vRowClass+"' align='center'>";					
						if( this.Results[i].DocType.toLowerCase() == "owner's manual" ) 
						{
							vHTMLStr += "" +
							"<a href='javascript:;' onclick='gSearch.DoCommand(\"Show\","+i+");return false;'>" +
								"<img src='graphics/btnView.gif' width='87' height='17' border='0' " +
										"onMouseOver='this.src=\"graphics/btnView-Hi.gif\"' " +
										"onMouseOut='this.src=\"graphics/btnView.gif\"'>" +
							"</a>";
						}
						else
						{
							vHTMLStr += "&nbsp;";
						}
			vHTMLStr +=	"</td>";
		}
		vHTMLStr += "</tr>";
	}
	vHTMLStr += "</table>";
	var vObj = document.getElementById("searchResults");
	vObj.innerHTML = vHTMLStr;
}

function fSearchClear() {
	var vObj = document.getElementById("searchResults");
	vObj.innerHTML = "&nbsp;";
}

function fSearchShow(pIndex) {
	// http://stage.suzukiservicepro.com/Motorcycle/POD/ShowPDF.htm?title=GSX1300R&file=99500-39350-03E_TXT_index.htm
	var vPath = "http://" + location.hostname + this.Results[pIndex].FileAssemPath;
	if (vPath.charAt(vPath.length-1) != "/") vPath += "/";
	
	/*switch (this.Results[pIndex].DisplayMethod) {
		case "H":	// html index and document set
		case "I": 	// html index and document set
			var vURL = vPath + "ShowPDF.htm" +
					"?title=" + escape(this.Results[pIndex].DocTitle) +
					"&file=" + escape(this.Results[pIndex].PartNum) +
					"&doctype=" + escape(this.Results[pIndex].DocType);
			break;
			
		case "P":
			var vURL = vPath + escape(this.Results[pIndex].PartNum) + "/" + escape(this.Results[pIndex].PartNum) + ".PDF";
			break;
			
		default:
			alert("Unsupported display method: " + this.Results[pIndex].DisplayMethod);
			break;
	}*/
	
	switch (this.Results[pIndex].DocTypeCode) {
		// Single PDFs are grouped in directories named by document type
		// 	Example path: [document root]/AM/99505-47020-03E.pdf
		case "AM":
		case "DWP":
		case "OM":
		case "PM":
		case "SO":
		case "SS":
		case "TC":
		case "WD":
			var vURL = vPath + 
						this.Results[pIndex].DocTypeCode + "/" + 
						escape(this.Results[pIndex].PartNum) + ".PDF";
			break;
		
		// Directories and HTML (index and multiple pdfs) are in subdirectories named by partnumber, grouped in directories named by document type
		// 	Example path: [document root]/SM/99505-47020-03E/99505-47020-03E.pdf		
		case "MI": 
		case "RR":
		case "SM":
		case "OSM":
		case "US":
			var vURL = vPath + "ShowPDF.htm" +
					"?title=" + escape(this.Results[pIndex].DocTitle) +
					"&file=" + escape(this.Results[pIndex].PartNum) +
					"&subdir=" + this.Results[pIndex].DocTypeCode +
					"&doctype=" + escape(this.Results[pIndex].DocType);		
			break;
		
		default:
			alert("Display method unknown.");
			break;
						
	}	
	//alert(vURL);
	var vWin = fWindowOpen(vURL, "PODDigitalLibIndex", -1, -1, false, "");
	try {
		vWin.focus();
	} catch(er) {
	}
}

function fSearchAddToList(pIndex) {
	for (var i=0; i < this.ProcurementList.length; i++) {
		if (this.ProcurementList[i] == this.Results[pIndex].PartNum) {
			alert("Item has already been added to the list.");
			return;
		}
	}
	document.addToListForm.partNumber.value = this.Results[pIndex].PartNum;
	document.addToListForm.submit();
}

function fSearchAddToListDone(pForm) {
	var vCount = parseInt(pForm.numItems.value, 10);
	document.getElementById("procListCount").innerHTML = vCount + " " + ((vCount == 1)?"Item":"Items");
	this.ProcurementList = pForm.listItems.value.split(",");
}

function fSearchShowProcList() {
	this.ListWin = fWindowOpen("ProcList.asp", "ProcList", 1018, 660, false, "");
	try {
		this.ListWin.focus();
	} catch(er) {
	}	
}

function fSearchUpdateProcList(pNewList) {
	this.ProcurementList = new Array();
	for (var i=0; i < pNewList.length; i++) {
		this.ProcurementList[i] = pNewList[i];
	}
	document.getElementById("procListCount").innerHTML = pNewList.length + " " + ((pNewList.length == 1)?"Item":"Items");
}

function fSearchClosing() {
	try {
		if (this.ListWin) {
			this.ListWin.close();
		}
	} catch (er) {
	}	
}

function fSearchResetTimeout() {
	// Set timer to automatically request a page every 15 minutes to prevent timeout
	this.ClearTimeout();
	var obj = document.getElementById("timeoutBlocker");
	if (obj.src != "TimeoutBlocker.asp") {
		// initial load
		obj.src = "TimeoutBlocker.asp";
	} else {
		// reload
		var vDoc = (obj.contentDocument) ? obj.contentDocument : obj.Document;
		vDoc.location.reload(true);
	}
	this.TimeoutID = setTimeout("gSearch.ResetTimeout()", 900000);
}

function fSearchClearTimeout() {
	if (this.TimeoutID != -1) {
		clearTimeout(this.TimeoutID);
		this.TimeoutID = -1;
	}
}

function pennies_2_dollars(p)
{
	return( (p/100).toFixed(2));
}
