var oFindXML = document.all.FindXML; // new ActiveXObject("Msxml2.DOMDocument"); // Create an XML object	
var oListFunc=null; // used as a function pointer

function hgEditSelectEquip(cIDField, cDisplayField, cQuery) {
	var cXML='';
	var cCreatedQuery='';
		
	try{
		var oArray = GetEquip();
			
		if (oArray!=null) {
			var cXML=oArray[1];   // Returned record's XML
				
			// First we will check to see if the default nodes will be filled
			// with the approriate data, or if the user specified a Query
			if (cQuery!='' && cQuery!=null) 
				processFindXML(cXML, cQuery);					
			else {
				// We will assume the user specified the default fields, so we 
				// will just create the query string for them					
				cCreatedQuery=cIDField + "=hgGUID;" + cDisplayField + "=serialnum";
				processFindXML(cXML, cCreatedQuery);
			}			
		}
	}catch(e){
		//do nothing
	}
		
	if (cXML!='' && cXML!=null)
		return cXML;  // return XML for selected item
	else
		return '';
}

function hgEditSelectGroup(cIDField, cDisplayField, cQuery, cGroupSecLvl, nGroupType, nLocID) {
	var cXML='';
	var cCreatedQuery='';
	
	try {		
		var oArray = GetUserGroup(cGroupSecLvl, nGroupType, false);
			
		if (oArray!=null) {
			var cXML=oArray[1];   // Returned record's XML
				
			// First we will check to see if the default nodes will be filled
			// with the approriate data, or if the user specified a Query
			if (cQuery!='' && cQuery!=null) 
				processFindXML(cXML, cQuery);					
			else {				
				// If the user specified any fields they will be filled in automatically
				if (cIDField!=null && cIDField!='' && cDisplayField!=null && cDisplayField!='') {
					// We will assume the user specified the default fields, so we 
					// will just create the query string for them					
					cCreatedQuery=cIDField + "=usergroupguid;" + cDisplayField + "=groupname";
					processFindXML(cXML, cCreatedQuery);
				}
			}			
		}
	}catch(e){
		alert(e.description);		// display error
	}
	
	
	if (cXML!='' && cXML!=null)
		return cXML;  // return XML for selected item
	else
		return '';
}

function hgEditSelectGroups(cGroupSecLvl, nGroupType, nLocID) {
	// TODO: Add support for location ID parameter	
	
	try{
		var oArray = GetUserGroup(cGroupSecLvl, nGroupType, true, cLocID);
	} catch(e) {
		alert(e.description);		//do nothing 
	}		
}

function hgEditSelectUser(cIDField, cDisplayField, cQuery, cSecLevel, cLocID) {
	// TODO: Add support for location ID parameter
	
	var cXML='';
	var cCreatedQuery='';
	
	try {
		var oArray = GetUser(cSecLevel, false, cLocID);
			
		if (oArray!=null) {
			var cXML=oArray[1];   // Returned record's XML			
			// First we will check to see if the default nodes will be filled
			// with the approriate data, or if the user specified a Query
			if (cQuery!='' && cQuery!=null) 
				processFindXML(cXML, cQuery);					
			else {
				// If the user specified any fields they will be filled in automatically
				if (cIDField!=null && cIDField!='' && cDisplayField!=null && cDisplayField!='') {
					// We will assume the user specified the default fields, so we 
					// will just create the query string for them					
					cCreatedQuery=cIDField + "=userguid;" + cDisplayField + "=username";
					processFindXML(cXML, cCreatedQuery);
				}
			}			
		}
	} catch(e) {
		alert(e.description);		// Alert user of error
	}
	
	try{
		//Some pages need to know when this has finished.
		hgEditSelectUserFinished();
	} catch(e){
	}	
	if (cXML!='' && cXML!=null)
		try{
		//Some pages need to know when a view field has been edited so the Save changes.
		EventDataChanged()
	} catch(e){
	}	
	if (cXML!='' && cXML!=null)
		return cXML;  // return XML for selected item
	else
		return '';
}

function hgEditSelectUsers(cSecLevel, cLocID) {
	// TODO: Add support for location ID parameter	
		
	try {
		GetUser(cSecLevel, true);					
	} catch(e) {
		alert(e.description);		//do nothing 
	}		
}

function hgEditSelectContact(cIDField, cDisplayField, cQuery) {
	var cXML='';
	var cCreatedQuery='';
		
	try{			
		var oArray = GetContact();
			
		if (oArray!=null) {
			var cXML=oArray[1];   // Returned record's XML				
			// First we will check to see if the default nodes will be filled
			// with the approriate data, or if the user specified a Query
			if (cQuery!='' && cQuery!=null) 
				processFindXML(cXML, cQuery);					
			else {
				// We will assume the user specified the default fields, so we 
				// will just create the query string for them					
				cCreatedQuery=cIDField + "=contactguid;" + cDisplayField + "=contactname";					
				processFindXML(cXML, cCreatedQuery);
			}			
		}
	}catch(e){
		//do nothing
	}
	if (cXML!='' && cXML!=null)
		try{
		//Some pages need to know when a view field has been edited so the Save changes.
		EventDataChanged()
	} catch(e){
	}	
	if (cXML!='' && cXML!=null)
		return cXML;  // return XML for selected item
	else
		return '';		
}

function hgEditSelectCalProc(cIDField, cDisplayField, cQuery) {
	var cXML='';
	var cCreatedQuery='';
		
	try{			
		var oArray = GetCalProc();
			
		if (oArray!=null) {
			var cXML=oArray[1];   // Returned record's XML				
			// First we will check to see if the default nodes will be filled
			// with the approriate data, or if the user specified a Query
			if (cQuery!='' && cQuery!=null) 
				processFindXML(cXML, cQuery);					
			else {
				// We will assume the user specified the default fields, so we 
				// will just create the query string for them					
				cCreatedQuery=cIDField + "=hgGUID;" + cDisplayField + "=calproc";					
				processFindXML(cXML, cCreatedQuery);
			}			
		}
	}catch(e){
		//do nothing
	}
	if (cXML!='' && cXML!=null)
		try{
		//Some pages need to know when a view field has been edited so the Save changes.
		EventDataChanged()
	} catch(e){
	}	
	if (cXML!='' && cXML!=null)
		return cXML;  // return XML for selected item
	else
		return '';
		
}

	
function hgEditSelectSupplier(cIDField, cDisplayField, cQuery) {
	var cXML='';
	var cCreatedQuery='';
		
	try{			
		var oArray = GetSupplier();
			
		if (oArray!=null) {
			var cXML=oArray[1];   // Returned record's XML				
			
			// First we will check to see if the default nodes will be filled
			// with the approriate data, or if the user specified a Query
			if (cQuery!='' && cQuery!=null) 
				processFindXML(cXML, cQuery);					
			else {				
				// We will assume the user specified the default fields, so we 
				// will just create the query string for them					
				cCreatedQuery=cIDField + "=supplierguid;" + cDisplayField + "=suppliername";									
				processFindXML(cXML, cCreatedQuery);
			}			
		}
	}catch(e){
		//do nothing
	}
	if (cXML!='' && cXML!=null)
		try{
		//Some pages need to know when a view field has been edited so the Save changes.
		EventDataChanged()
	} catch(e){
	}	
	if (cXML!='' && cXML!=null)
		return cXML;  // return XML for selected item
	else
		return '';
}

function hgEditSelectCustomer(cIDField, cDisplayField, cQuery) {
	var cXML='';
	var cCreatedQuery='';
		
	try{			
		var oArray = GetCustomer();
			
		if (oArray!=null) {
			var cXML=oArray[1];   // Returned record's XML				
			// First we will check to see if the default nodes will be filled
			// with the approriate data, or if the user specified a Query
			if (cQuery!='' && cQuery!=null) 
				processFindXML(cXML, cQuery);					
			else {
				// We will assume the user specified the default fields, so we 
				// will just create the query string for them									
				cCreatedQuery=cIDField + "=customerguid;" + cDisplayField + "=customername";					
				processFindXML(cXML, cCreatedQuery);
			}			
		}
	}catch(e){
		//do nothing
	}
	if (cXML!='' && cXML!=null)
		try{
		//Some pages need to know when a view field has been edited so the Save changes.
		EventDataChanged()
	} catch(e){
	}		
	if (cXML!='' && cXML!=null)
		return cXML;  // return XML for selected item
	else
		return '';		
}

function hgEditSelectPart(cIDField, cDisplayField, cQuery) {
	var cXML='';
	var cCreatedQuery='';
		
	try{			
		var oArray = GetPart();
			
		if (oArray!=null) {
			var cXML=oArray[1];   // Returned record's XML				
			// First we will check to see if the default nodes will be filled
			// with the approriate data, or if the user specified a Query
			if (cQuery!='' && cQuery!=null) 
				processFindXML(cXML, cQuery);					
			else {
				// We will assume the user specified the default fields, so we 
				// will just create the query string for them					
				cCreatedQuery=cIDField + "=partguid;" + cDisplayField + "=partdescription";					
				processFindXML(cXML, cCreatedQuery);
			}			
		}
	}catch(e){
		//do nothing
	}
	if (cXML!='' && cXML!=null)
		try{
		//Some pages need to know when a view field has been edited so the Save changes.
		EventDataChanged()
	} catch(e){
	}	
	if (cXML!='' && cXML!=null)
		return cXML;  // return XML for selected item
	else
		return '';		
}

function hgEditSelectDocument(cIDField, cDisplayField, cQuery){
	var cXML='';
	var cCreatedQuery='';
		
	try{			
		var oArray = GetDoc();
			
		if (oArray!=null) {
			var cXML=oArray[1];   // Returned record's XML				
			// First we will check to see if the default nodes will be filled
			// with the approriate data, or if the user specified a Query
			if (cQuery!='' && cQuery!=null) 
				processFindXML(cXML, cQuery);					
			else {
				// We will assume the user specified the default fields, so we 
				// will just create the query string for them					
				cCreatedQuery=cIDField + "=hgguid;" + cDisplayField + "=title";					
				processFindXML(cXML, cCreatedQuery);
			}			
		}
	}catch(e){
		//do nothing
	}
	if (cXML!='' && cXML!=null)
		try{
		//Some pages need to know when a view field has been edited so the Save changes.
		EventDataChanged()
	} catch(e){
	}	
	if (cXML!='' && cXML!=null)
		return cXML;  // return XML for selected item
	else
		return '';		
}

function hgEditSelectManual(cIDField, cDisplayField, cQuery){
	var cXML='';
	var cCreatedQuery='';
		
	try{			
		var oArray = GetMan();
			
		if (oArray!=null) {
			var cXML=oArray[1];   // Returned record's XML				
			// First we will check to see if the default nodes will be filled
			// with the approriate data, or if the user specified a Query
			if (cQuery!='' && cQuery!=null) 
				processFindXML(cXML, cQuery);					
			else {
				// We will assume the user specified the default fields, so we 
				// will just create the query string for them					
				cCreatedQuery=cIDField + "=manualguid;" + cDisplayField + "=manualname";					
				processFindXML(cXML, cCreatedQuery);
			}			
		}
	}catch(e){
		//do nothing
	}
	if (cXML!='' && cXML!=null)
		try{
		//Some pages need to know when a view field has been edited so the Save changes.
		EventDataChanged()
	} catch(e){
	}	
	if (cXML!='' && cXML!=null)
		return cXML;  // return XML for selected item
	else
		return '';		
}

function hgEditSelectDocTemplate(cIDField, cDisplayField, cQuery){
	var cXML='';
	var cCreatedQuery='';
		
	try{			
		var oArray = GetDocTemplate();
			
		if (oArray!=null) {
			var cXML=oArray[1];   // Returned record's XML				
			// First we will check to see if the default nodes will be filled
			// with the approriate data, or if the user specified a Query
			if (cQuery!='' && cQuery!=null) 
				processFindXML(cXML, cQuery);					
			else {
				// We will assume the user specified the default fields, so we 
				// will just create the query string for them					
				cCreatedQuery=cIDField + "=fileguid;" + cDisplayField + "=name";					
				processFindXML(cXML, cCreatedQuery);
			}			
		}
	}catch(e){
		//do nothing
	}
	if (cXML!='' && cXML!=null)
		try{
		//Some pages need to know when a view field has been edited so the Save changes.
		EventDataChanged()
	} catch(e){
	}	
	if (cXML!='' && cXML!=null)
		return cXML;  // return XML for selected item
	else
		return '';		
}


function hgEditSelectModel(cIDField, cDisplayField, cQuery){
	var cXML='';
	var cCreatedQuery='';
		
	try{			
		var oArray = GetModel();
			
		if (oArray!=null) {
			var cXML=oArray[1];   // Returned record's XML				
			// First we will check to see if the default nodes will be filled
			// with the approriate data, or if the user specified a Query
			if (cQuery!='' && cQuery!=null){
				processFindXML(cXML, cQuery);
				try{
					addToParts();
				}catch(e) { }
			}else {
				// We will assume the user specified the default fields, so we 
				// will just create the query string for them					
				cCreatedQuery=cIDField + "=model;" + cDisplayField + "=title";					
				processFindXML(cXML, cCreatedQuery);
			}			
		}
	}catch(e){
		//do nothing
	}
	if (cXML!='' && cXML!=null)
		try{
		//Some pages need to know when a view field has been edited so the Save changes.
		EventDataChanged()
	} catch(e){
	}	
	if (cXML!='' && cXML!=null)
		return cXML;  // return XML for selected item
	else
		return '';		
}

function GetModel(){
	var cURL = 'default.asp?class=CPID&method=listFind';
	return PopUp(cURL,600,600);		
}



function GetUser(cSecLvl, bMultiSel, cLocID) {

	var cMethod = 'list';
	
	// Create URL based on options
	if (bMultiSel)
		cMethod+="multi"
	
	cMethod+="find";
	
	var cURL="default.asp?class=User&Method=" + cMethod;
		
	// Append required security level if available	
	if (cSecLvl!=null)
		if (cSecLvl.length>0)
			cURL+="&securitylevel=" + cSecLvl;	
	
	return PopUp(cURL, 600, 575);
}

function GetUserGroup(cSecLvl, nGroupType, bMultiSel, cLocID, bAdmin) {

	var cMethod = 'list';
	
	// Create URL based on options
	if (bMultiSel)
		cMethod+="multi"
	
	if (nGroupType==1)
		cMethod+="user";	
	
	cMethod+="find";
	
	var cURL="default.asp?class=UserGroup&Method=" + cMethod;
		
	// Append required security level if available
	if (cSecLvl!=null)
		if (cSecLvl.length>0)
			cURL+="&securitylevel=" + cSecLvl;
			
	// Append ADMIN group presence
		if(bAdmin!=null&&bAdmin==false){
			cURL+="&showadmin=false";
		}
	
	return PopUp(cURL, 600, 575);
}

function GetEquip(bMulti){
	if (!bMulti)
		var cURL='default.asp?class=Equipment&Method=listfind';
	else
		var cURL='default.asp?class=Equipment&Method=listmultifind';
	
	return PopUp(cURL, 580, 650);
}

function GetCalProc(){
	var cURL='default.asp?class=CalProc&Method=listfind';
	return PopUp(cURL, 580, 650);
}


function GetContact(){
	var cURL='default.asp?class=contact&Method=listfind';
	return PopUp(cURL, 600, 500);
}

function GetContacts(){
	var cURL='default.asp?class=contact&method=listmultifind';
	return PopUp(cURL, 600, 600);
}

function GetSupplier(){
	var cURL='default.asp?class=Supplier&Method=listfind';
	return PopUp(cURL, 600, 575);
}

function GetSuppliers(){
	var cURL='default.asp?class=Supplier&Method=listmultifind';
	return PopUp(cURL, 600, 575);
}

function GetCustomer(){
	var cURL='default.asp?class=Customer&Method=listfind';
	return PopUp(cURL, 600, 600);
}

function GetCustomers(){
	var cURL='default.asp?class=Customer&Method=listmultifind';
	return PopUp(cURL, 600, 600);
}


function GetPart(){
	var oArray = window.showModalDialog('default.asp?class=Part&method=listfind','','resizable:yes;help:no');
	return oArray;
}

function GetParts(){
	var oArray = window.showModalDialog('default.asp?class=Part&method=listmultifind','','resizable:yes;help:no');
	return oArray;
}

function GetDoc(){
	var oArray = window.showModalDialog('default.asp?class=doc&method=listfind','','resizable:yes;help:no');
	return oArray;
}

function GetMan(){
	var oArray = window.showModalDialog('default.asp?class=manual&method=listfind','','resizable:yes;help:no');
	return oArray;
}

function GetDocTemplate(){
	var oArray = window.showModalDialog('default.asp?class=template&method=listfind','','resizable:yes;help:no');
	return oArray;
}

function GetGroups(){
    var cURL='default.asp?class=UserGroup&method=ListMultiFind';
    return PopUp(cURL, 580, 650);
}

function GetUsers(){
    var cURL='default.asp?class=User&method=ListMultiFind';
    return PopUp(cURL, 580, 650);
}

function DocumentFind(cAdditionalQueryStringParameters){
	if(cAdditionalQueryStringParameters==null) cAdditionalQueryStringParameters='';
	var cURL = 'default.asp?class=doc&method=listfind' + cAdditionalQueryStringParameters;
	var cOptions = 'dialogWidth:600px;status:no;resizable:yes;help:no';
	var oResult = window.showModalDialog(cURL,'',cOptions);
	return oResult;
}

function DocumentsFind(cAdditionalQueryStringParameters){
	if(cAdditionalQueryStringParameters==null) cAdditionalQueryStringParameters='';
	var cURL = 'default.asp?class=doc&method=listmultifind' + cAdditionalQueryStringParameters;
	var cOptions = 'dialogWidth:600px;status:no;resizable:yes;help:no';
	var oResult = window.showModalDialog(cURL,'',cOptions);
	return oResult;
}

function LocationFind(cAdditionalQueryStringParameters){
	if(cAdditionalQueryStringParameters==null) cAdditionalQueryStringParameters='';
	var cURL = 'default.asp?class=location&method=listfind' + cAdditionalQueryStringParameters;
	var cOptions = 'dialogWidth:600px;status:no;resizable:yes;help:no';
	var oResult = window.showModalDialog(cURL,'',cOptions);
	return oResult;	
}

function LocationsFind(cAdditionalQueryStringParameters){
	if(cAdditionalQueryStringParameters==null) cAdditionalQueryStringParameters='';
	var cURL = 'default.asp?class=location&method=listmultifind' + cAdditionalQueryStringParameters;
	var cOptions = 'dialogWidth:600px;status:no;resizable:yes;help:no';
	var oResult = window.showModalDialog(cURL,'',cOptions);
	return oResult;
}

function GetLocation(bMulti) {
	var cURL='';
	
	if (!bMulti)
		cURL = 'default.asp?class=location&method=listfind';
	else
		cURL = 'default.asp?class=location&method=listmultifind' + cAdditionalQueryStringParameters;
	
	var cOptions = 'dialogWidth:600px;status:no;resizable:yes;help:no';
	var oResult = window.showModalDialog(cURL,'',cOptions);
	return oResult;	
}
	
	
//*********************************************************************************/
// SELECT PARTS AFFECTED Detail WINDOW

function SelectPartAffectedDetailWindow(cCallBack,aVals,nMode,cTitle){
	var cURL='default.asp?class=PartAffected&method=DetailPage&callback=' + cCallBack + '&Mode=' + nMode + '&Title=' + cTitle;	
	var cResult=window.showModalDialog(cURL,aVals,"resizable:yes;dialogHeight:750px;dialogWidth:650px;help:no");
	if(cResult!=''){
		eval(cCallBack + '(' + cResult + ')');
	}
}	

// SEARCH FUNCTIONS

function ListFind(oFuncPointer)
{	
	oListFunc=oFuncPointer;
	this.find=displayList;
	this.getValue=getXMLValue;
	this.getXML=getXML;	
}

function displayList()
{	
	try {
		var cFindXML = oListFunc();  // call function pointer	
	
		// Initialize global XML object
		if (cFindXML!='' && cFindXML!=null)
			oFindXML.loadXML(cFindXML);
		else
			oFindXML.loadXML("");
		
	} catch(e) { 
		alert("Unable to perform search due to the following error: " + cFindXML);
		return '';
	}
	
	return cFindXML;				
}

function getXML() {		
	return oFindXML.xml;
}

function GetSelect(cSelectType, bMulti) {
	// This function accepts a select type (i.e.: Equip, Document, etc.) and provide
	// the appropriate select dialog. It will then return the list of GUID's returned
	// to the calling function. 
	
	// cSelecType (calproc | caltest | contact | customer | document | equip | group | location | part | repair | supplier | user)
	
	var oArray = new Array();
	var cGUIDList = '';
	
	switch (cSelectType.toLowerCase()) {
		case "calproc":
			oArray = GetCalProc();		
			break;
		case "caltest":
			// TODO: Add
			break;
		case "contact":
			oArray = GetContact();
			break;
		case "customer":
			oArray = GetCustomer();
			break;
		case "document":
			oArray = DocumentFind();
			break;
		case "equip":
			if (bMulti)
				oArray = GetEquip(bMulti);
			else
				cGUIDList = GetEquip();			
			break;
		case "group":
			oArray = GetGroups();
			break;
		case "location":
			if (bMulti)
				oArray = GetLocation(bMulti);
			else
				cGUIDList = GetLocation();
			break;
		case "part":
			// TODO: Add
			break;
		case "repair":
			// TODO: Add
			break;
		case "supplier":
			oArray = GetSupplier();
			break;
		case "user":
			oArray = GetUsers();
			break;
		default:
			alert("An invalid select type has been specified!");
			break;
	}
	
	if (bMulti)
		return oArray;  // return array with results
	else
		return cGUIDList;
}			

function getXMLValue(cNodeName) {			
	// Grab the desired field from the XML recordset
	try {
		if (oFindXML.xml=='' || oFindXML.xml==null)
			return "";  // no XML is available
	} catch(e) { return ""; } // an unknown error occured or no XML was available
	
	// Attempt to retrieve the specified node
	try {
		//return oFindXML.selectSingleNode('data/z:row').attributes.getNamedItem(cNodeName).text
		return NodeAttribute(oFindXML.selectSingleNode('data/z:row'),cNodeName);
	} catch(e) { alert("Unable to return XML for specified node: "+ cNodeName); }
}

function processFindXML(cSourceXML, cQueryString)
{
	var oNodeArray=null;
	var oNode=null;
	var cDestNode='';
	var cSourceNode='';
	var oSourceXML = document.all.RandomUse; // new ActiveXObject("Msxml2.DOMDocument"); // Create an XML object	
		
	try {		
	oSourceXML.loadXML(cSourceXML);				
	} catch(e) {}  // ignore errors
	// Remove trailing semicolon if present
	if (cQueryString.charAt(cQueryString.length)==";")
		cQueryString=cQueryString.substr(cQueryString,cQueryString.length-1);  // remove it
				
	// We will parse through the Query String and assign the source nodes
	// to the destination nodes in the detail recordset
		
	// We will split the query string assignments into an array
	oNodeArray = cQueryString.split(";");
	
	// Now loops through the tabs and process the includes
	for (var iCounter=0; iCounter < oNodeArray.length; iCounter++) {
		// Now split up the tab so we can grab the destination node and the source node
		try{
			oNode = oNodeArray[iCounter].split("=");  // assignments are seperated by an '=' 
			
			cDestNode = oNode[0].toLowerCase();  
			cSourceNode = oNode[1].toLowerCase();		
			// Node we will attempt to update the detail recordset				
			//document.all.DetailRecordset.selectSingleNode('record/' + cDestNode).text =  oSourceXML.selectSingleNode('data/z:row').attributes.getNamedItem(cSourceNode).text;
			var oData = oSourceXML.selectSingleNode('data/z:row');
			
			if(oData!=null)
			{
				document.all.DetailRecordset.selectSingleNode('record/' + cDestNode).text =  NodeAttribute(oData,cSourceNode);
			}
			else
			{
				document.all.DetailRecordset.selectSingleNode('record/' + cDestNode).text ='';
			}
			
		} catch(e) {
			var oField = document.all.DetailRecordset.selectSingleNode('record/' + cDestNode);
			if(oField!=null){
				oField.text = '';
			}
		}			
			
	}
		
}
