﻿var		g_PartsUrlBase ="Parts.asmx/";

var		g_PartsListArray;							// array of Part objects

var		pl_table, pl_table_body;				// references to the parts list table
var		pl_is_multi_line_display;
var		pl_0result_download_message;

// Populate the parts list table
function	PopulatePlTable( table_body ) {

	for( var i = 0; i < g_PartsListArray.length; i++ ) {
		var	row			= document.createElement( "TR" );
		var	button		= CreateButton("Add", AddToBasketCallback);
		var	cell		= CreateCell( button );
		var currPart	= g_PartsListArray[ i ];

		if( pl_is_multi_line_display )
			cell.rowSpan = 2;

		row.appendChild( cell );

		if( currPart.m_bEssential )
			row.className = "ess";

		cell = CreateCell( currPart.m_PartCode );
		row.appendChild( cell );

		cell = CreateCell( FormatPrice( currPart.m_Price, g_CurrencySymbol ) );
		cell.className = "ptPrice"

		row.appendChild( cell );

		if( !pl_is_multi_line_display ) {
			if( currPart.m_Url ) {
				var	link = document.createElement( "A" );
				link.href = "javascript:ShowImage( 'data/images/" + currPart.m_Url + "' );";
				link.innerHTML = currPart.m_Description;

				row.appendChild( CreateCell( link ) );
			} else
				row.appendChild( CreateCell( currPart.m_Description ) );

			table_body.appendChild( row );
		} else {
			table_body.appendChild( row );

			var	cell;

			row = document.createElement( "TR" );

			if( currPart.m_Url ) {
				var	link = document.createElement( "A" );
				link.href = "javascript:ShowImage( 'data/images/" + currPart.m_Url + "' );";
				link.innerHTML = currPart.m_Description;

				cell = CreateCell( link );
			} else
				cell = CreateCell( currPart.m_Description );

			cell.colSpan = 2;
			cell.className = "pbr";
			row.appendChild( cell );

			table_body.appendChild( row );
		}
	}
}

// Function to be called when parts list has been downloaded
function	GotPartsList() {
	DeleteTableBody( pl_table );
	if( g_PartsListArray.length == 0 )
//		alert( pl_0result_download_message );
		;
	else
		PopulatePlTable( pl_table_body );
}

// Generate parts list from Xml document return by WebApplication
function GetPartsListFromXmlDoc( XmlDoc ) {
	var	list = new Array();
	var	nodeArray = XmlDoc.lastChild.childNodes;

	for(var i = 0; i < nodeArray.length; i++ ) {
		if( nodeArray[ i ].nodeName == "anyType" ) {
			var item = new Part();
			item.ReadFromXmlAnyTypeNode( nodeArray[i] );
			list.push( item );
		}
	}
	return	list;
}

// Construct an appropriate get request url for a part search based on partCode, possibly restricted to a model
function	FormatPartsSearch( part_code, model )
{
	if( model && model.length > 0 )
		return g_PartsUrlBase + "ByPartCodeForModel?PartCode=" + escape(part_code) + "&Model=" + escape(model);
	return g_PartsUrlBase + "ByPartCode?PartCode=" + escape(part_code);
}

function	FormatPartsSearchByPartType( partTypeId, model ) {
	return	g_PartsUrlBase + "PartsOfType?PartTypeId=" + partTypeId + "&ModelCode=" + model;
}

// Construct an appropriate get request url for a part search by description, possibly restricted to a model
function	FormatPartsSearchByDescription( description, model ) {
	if( model && model.length > 0 )
		return g_PartsUrlBase + "ByDescriptionForModel?Description=" + escape(description) + "&Model=" + escape(model);
	return g_PartsUrlBase + "ByDescription?Description=" + escape(description);
}

// Initialise parts table code
function	PartTableInit( is_multi_line, message ) {
	g_PartsListArray = new Array();

	pl_table = document.getElementById( "PartsTable" );
	pl_table_body = pl_table.tBodies[ 0 ];

	pl_is_multi_line_display	= is_multi_line;
	pl_0result_download_message	= message;

	StartSynchronousDownload( "WebSpares.asmx/GetCurrencySymbol", null);
	g_CurrencySymbol = GetXmlStringResult( g_XmlRequester.responseXML );
}
