/*
	Functions Javascript for the Expo Wizard
	Developed by Kevin Lunham for
	Matinée Technical Services
	6/12/2002
*/

//product type matrix
tMatrix = new Array();
tMatrix[0] = new Array('','ATEX (Europe)','NEC (North America)','GLOBAL OEM (NEC and ATEX)','IEC (International)');
tMatrix[1] = new Array('Zone 1','ax','n','x','ax');
tMatrix[2] = new Array('Zone 2','ax','n','x','ax');
tMatrix[3] = new Array('Class I Div 1','n','xy','x','ax');
tMatrix[4] = new Array('Class I Div 2','n','z','x','ax');
tMatrix[5] = new Array('Class II Div 1 or 2 (dust)','n','d','x','ax');
tMatrix[6] = new Array('Dust (European)','d','n','d','d');

//volume matrix
vMatrix = new Array();
vMatrix[0] = new Array('0.5','1.0','2.0','4.0','Other');
vMatrix[0].t = 'Metric';
vMatrix[1] = new Array('15','25','50','100','Other');
vMatrix[1].t = 'US Measurements';

//create wizard vars
var err = '', reg, haz, Atype, Aunit=0, AunitType, Avol, Atime, Aflow, Avol2=0, Aamps='', Avolts='', Amount='', Amount2='';
var resultsAll = new Array(), results = new Array(); resultsAdv = new Array();
var frame = 'formsFrame.';

//used to find objects within the framsets
function objFinder(i){
	var o,r=new Object();
	o=eval(frame+"document");
	for (x=0;x<o.forms.length;x++){if (o.forms[x].name==i){r = o.forms[x];}
	for (y=0;y<o.forms[x].length;y++){if (o.forms[x][y].name==i){r = o.forms[x][y];}}}return r;
}

//used onLoad of form 1 to set the form values intially and on amends
function setWiz1(){
	var obj;
	obj = objFinder('regulations');
	for (x=0;x<tMatrix[0].length;x++)
	{
		if (tMatrix[0][x]==reg)
		{
			obj[x].selected = true;
		}
	}
	obj = objFinder('hazard');
	for (x=0;x<tMatrix.length;x++)
	{
		if (tMatrix[x][0]==haz)
		{
			obj[x].selected = true;
		}
	}
	obj = objFinder('units');
	obj[Aunit].selected = true;
	setVol();
	obj = objFinder('time');
	for (x=0;x<obj.length;x++)
	{
		if (obj[x].value==Atime)
		{
			obj[x].selected = true;
		}
	}

}

//used onLoad of form 2 to set the form values intially and on amends
function setWiz2(){
	var obj;
	obj = objFinder('amps');
	for (x=0;x<obj.length;x++)
	{
		if (obj[x].value==Aamps)
		{
			obj[x].selected = true;
		}
	}
	obj = objFinder('volts');
	for (x=0;x<obj.length;x++)
	{
		if (obj[x].value==Avolts)
		{
			obj[x].selected = true;
		}
	}
	obj = objFinder('mount');
	for (x=0;x<obj.length;x++)
	{
		if (obj[x].value==Amount)
		{
			obj[x].selected = true;
		}
	}
}

//sets the volume drop down to correct units (uses the volume matrix)
function setVol(){
	var obj,v,t; 
	obj = objFinder('units');
	v = obj[obj.selectedIndex].value;
	Aunit=v;
	AunitType=vMatrix[v].t;
	obj = objFinder('volume');
	for(x=0;x<vMatrix[v].length;x++){
		obj.options[x].value = vMatrix[v][x];
		t = vMatrix[v][x];
		if (v==0){t+=" m3"}else{t+=" cu.ft."}
		obj.options[x].text = t;
		if (vMatrix[v][x]==Avol2)
		{
			obj[x].selected = true;
		}
	}
}

//function to calculate the type
function atribType(){
	var r,h,obj,t='n';
	obj=objFinder('regulations');r=obj[obj.selectedIndex].value;
	obj=objFinder('hazard');h=obj[obj.selectedIndex].value;
	if (r<1){err += 'You must select a Regulation Board\n';} 
	if (h<1){err += 'You must select a Hazard Classification\n';}
	if (err.length<1){t = tMatrix[h][r];}
	if (t=='n'&&err.length<1){err += 'You cannot select this Regulation Board and Hazard Classification combination.\nRegulation Board: ' + tMatrix[0][r] + '\nHazard Classification: ' + tMatrix[h][0] + '\n';}
	reg=tMatrix[0][r];haz=tMatrix[h][0];Atype=t;
}

//flow calculation
function atribFlow(){
	var obj,Q,v,t;
	obj = objFinder('volume');
	v = obj[obj.selectedIndex].value;
	Avol2 = v;
	obj = objFinder('time');
	t = obj[obj.selectedIndex].value;
	if (Aunit>0){v=v*0.028316847;}
	Q = v*1000*5/t
	Avol = v;
	Atime = t;
	Aflow = Q;
}
//function used on the submit buttons on both forms
function attribs(adv){

	if (adv)
	{
		//if advanced then sets the variables and runs the finderAdv() function
		var obj;
		obj = objFinder('amps');
		Aamps = obj[obj.selectedIndex].value;
		obj = objFinder('volts');
		Avolts = obj[obj.selectedIndex].value;
		obj = objFinder('mount');
		Amount = obj[obj.selectedIndex].value;
		if (Amount=='BP')
		{
			Amount2 = 'SS';
		}else{
			Amount2 = '';
		}
		//alert(Amount);
		finderAdv();
	}else{
		//else sets the main attributes and then calculates flow
		atribType();
		atribFlow();
		//return any errors
		if (err.length>1){alert(err);err=''}else{productFinder();}
	}
}

//used to find the initial results list of products from form 1 attributes
function productFinder(){

	var tempAccept = false;
	//set resultsAll array to 0
	resultsAll.length = 0;
	
	//loop through all the products
	for (x=0;x<pArray.length;x++)
	{
		//set the tempAccept value to false
		tempAccept = false;
		//check the type
		if (Atype.search(pArray[x][5])!=-1)
		{
			var tempFlow = 250;
			if ((Aflow*2)>tempFlow)
			{
				tempFlow = (Aflow*2);
			}
			
			//check the flow
			if (Aflow<pArray[x][6]&&tempFlow>pArray[x][6])
			{
				tempAccept = true;
			}
		}
		
		//add the item to the resultsAll if true
		if (tempAccept)
		{
			resultsAll[resultsAll.length] = pArray[x];
		}
	}
	
	deliverResults(resultsAll);
	formsFrame.document.location = 'form2.htm';
}

//used to find advanced product list (only searches the results list not the master list)
function finderAdv(){
	var a, temp=new Array();
	resultsAdv.length=0;
	for (x=0;x<resultsAll.length;x++)
	{
		a = false;
		if (Aamps==resultsAll[x][9]||Aamps=='')
		{
			resultsAdv[resultsAdv.length] = resultsAll[x];
		}
	}
	for (x=0;x<resultsAdv.length;x++)
	{
		a = false;
		if (Avolts==resultsAdv[x][10]||Avolts=='')
		{
			temp[temp.length] = resultsAdv[x];
		}
	}
	resultsAdv.length=0;
	for (x=0;x<temp.length;x++)
	{
		a = false;
		if (Amount==temp[x][7]||Amount==''||Amount2==temp[x][7])
		{
			resultsAdv[resultsAdv.length] = temp[x];
		}
	}

	deliverResults(resultsAdv);
}

//function to order results by name then rate
function nameRate(a, b) 
{ 
   if(a[0] < b[0]) 
      return -1 
   if(a[0] > b[0]) 
      return 1 
   if(a[0] == b[0]) {
	if(a[8] < b[8]) 
      return -1 
    if(a[8] > b[8]) 
      return 1 
	return 0 
   }
   return 0 
}
   
//function to order results by rate then name
function rateName(a, b) 
{ 
   if(a[8] < b[8]) 
      return -1 
   if(a[8] > b[8]) 
      return 1 
   if(a[8] == b[8]) {
	if(a[0] < b[0]) 
      return -1 
    if(a[0] > b[0]) 
      return 1 
	return 0 
   }
   return 0 
}

//function to deliver results
//NOTE MAY WANT TO CUT SOME CODE SO AS NOT TO SHOW THE COMPLETE DETAILS
function deliverResults(r){
	var str = '',tempTitle = '';

	//set results length to 0
	results.length = 0;

	if (r.length>0)
	{	
		//first sort by name the reuslts
		r.sort(nameRate);
		for (x=0;x<r.length;x++)
		{
			if (tempTitle != r[x][0])
			{
				results[results.length] = r[x];
			}
			tempTitle = r[x][0];
		}

		var	loopTotal = 10;
		if (results.length<10)
		{
			loopTotal = results.length;
		}
		results.sort(rateName);
	}

	//deliver compelte list
	str = "<table width=90% border=1 align=center>";
	str += "<tr><td colspan=2><h2>Variables List</h2></td></tr>";
	str += "<tr><td valign='top'>Regulations</td><td valign='top'>"+reg+"</td></tr>";
	str += "<tr><td valign='top'>Hazard Class</td><td valign='top'>"+haz+"</td></tr>";
	str += "<tr><td valign='top'>Purge Type</td><td valign='top'>"+Atype+"</td></tr>";
	str += "<tr><td valign='top'>Units</td><td valign='top'>"+AunitType+"</td></tr>";
	str += "<tr><td valign='top'>Volume</td><td valign='top'>"+Avol+" m3</td></tr>";
	str += "<tr><td valign='top'>Purge Time</td><td valign='top'>"+Atime+"</td></tr>";
	str += "<tr><td valign='top'>Calculated Flow</td><td valign='top'>"+Aflow+"</td></tr>";
	str += "<tr><td valign='top'>Isolated Current</td><td valign='top'>"+Aamps+"</td></tr>";
	str += "<tr><td valign='top'>Supply Voltage</td><td valign='top'>"+Avolts+"</td></tr>";
	str += "<tr><td valign='top'>Mounting Type</td><td valign='top'>"+Amount+"</td></tr>";
	str += "</table><br>";

	str += "<table width=90% border=1 align=center>";
	str += "<tr><td colspan=2><h2>Complete Results</h2></td></tr>";
	str += "<tr><td valign='top'>Product</td><td valign='top'>Rating</td></tr>";
	for (x=0;x<resultsAll.length;x++)
	{
		str += "<tr><td valign='top'>"+resultsAll[x][0]+"</td><td valign='top'>"+resultsAll[x][8]+"</td></tr>";
	}

	str += "</table><br>";

	str += "<table width=90% border=1 align=center>";
	str += "<tr><td colspan=2><h2>Advanced Results</h2></td></tr>";
	str += "<tr><td valign='top'>Product</td><td valign='top'>Rating</td></tr>";
	for (x=0;x<resultsAdv.length;x++)
	{
		str += "<tr><td valign='top'>"+resultsAdv[x][0]+"</td><td valign='top'>"+resultsAdv[x][8]+"</td></tr>";
	}

	str += "</table>";

	varsFrame.document.write(str)
	varsFrame.document.close();

	//show complete r list
	//this is where the users view of the results are shown
	str = "<html><body class='bodydarker'>"
	str += "<link href='../stylesheets/wizard.css' rel='stylesheet' type='text/css'>"
	str += "<table width=90% border=0 align=center cellpadding='2' cellspacing='0'>";

	//
	str += "<tr><td colspan='2'><TABLE height='100%' cellSpacing=0 cellPadding=0 width='300' border=0>";
	str += "<TR><TD valign=middle bgColor=#3A479A height=21 width=21><IMG height=21 hspace=0 src='../assets/Images/titleplus.gif' width=21 border=0></td>";
	str += "<td class='title' valign=middle bgColor=#3A479A>&nbsp;&nbsp;&nbsp;&nbsp;Product Selection Results</TD></TR></TABLE></td></tr>";
	//

	str += "<tr><td valign='top'' colspan='2'><img src='../images/spacer.gif' height='4' width='3'></td></tr>";
	str += "<tr><td valign='top'' colspan='2'><p>The following products meet your selection criteria and are listed in order of applicability. Click on each selected product for more information.</p></td></tr>";
	str += "<tr><td valign='top'' colspan='2'><img src='../images/spacer.gif' height='4' width='3'></td></tr>";

	str += "<tr><td valign='top' class='whitecell'width='190'><b>Product</b></td><td valign='top' class='whitecell' width='110'><b>Product No</b></td></tr>";

	for (x=0;x<loopTotal;x++)
	{
		str += "<tr><td valign='top' class='whitecell'><a href='javaScript:parent.showProd("+x+");'>"+results[x][1]+"</a></td><td valign='top' class='whitecell'><a href='javaScript:parent.showProd("+x+");'>"+results[x][0]+"</a></td></tr>";
	}
	str += "</table><br>";

	

	str += "<table width=90% border=0 align=center>";
	
	str += "<tr><td valign='top'' colspan='2'><p>To view, amend or refine your selection please click step 1 or 2 below.</p></td></tr>";
	str += "<tr><td valign='top'' colspan='2'><img src='../images/spacer.gif' height='4' width='3'></td></tr>";

	str += "<tr><td valign='top'><a href='form1.htm' target='formsFrame'><img src='wizard-step1.gif' border='0'></a></td></tr>";
	str += "<tr><td valign='top'><a href='form2.htm' target='formsFrame'><img src='wizard-step2.gif' border='0'></a></td></tr>";
	str += "</table><br></body</html>";

	resultsFrame.document.write(str);
	resultsFrame.document.close();
}

//function used to show product info in detail
function showProd(x){
	//this is where the product info is shown
	//you will have to chage this to show the design
	str = "<html><body>"
	str += "<link href='../stylesheets/wizard.css' rel='stylesheet' type='text/css'>"
	str += "<table width=90% border=0 align=center cellpadding='2' cellspacing='0'>";
	str += "<tr><td colspan='2'><TABLE height='100%' cellSpacing=0 cellPadding=0 width='100%' border=0>";
	str += "<TR><TD valign=top bgColor=#B2B2B2 height=21 width=21><IMG height=21 hspace=0 src='../assets/Images/titleplus.gif' width=21 border=0></td>";
	str += "<td class='title' valign=middle bgColor=#3A479A><table width='100%'><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td class='title'>"+results[x][1]+"</td></tr></table></TD></TR></TABLE></td></tr>";
	
	str += "<tr><td valign='top'' colspan='2'><img src='../images/spacer.gif' height='4' width='3'></td></tr>";
	str += "<tr><td valign='top' colspan='2'>"+results[x][2]+"</td></tr>";
	str += "<tr><td valign='top'' colspan='2'><img src='../images/spacer.gif' height='4' width='3'></td></tr>";
	
	str += "<tr><td valign='top' width='50%' class='title' bgColor=#3A479A>Product No</td><td valign='top' class='whitecell'>"+results[x][0]+"</td></tr>";
	str += "<tr><td valign='top'' colspan='2'><img src='../images/spacer.gif' height='4' width='3'></td></tr>";

	//code for image
	if (results[x][3]=='none'||results[x][3]=='')
	{
		var imgCode = "";
	}else{
		var imgCode = "<img src='productInfo/"+results[x][3]+"' width=50 height=50>"
	}

	//code for purge time calculation
	var calCode = "<a href='javaScript:parent.actualTime("+x+");'><img src='calc_actual.gif' border='0'></a><br><img src='../images/spacer.gif' height='4' width='3'><br>";

	//code for document links
	if (results[x][4]=='none')
	{
		var docCode = "";
	}else{
		var docCode = "<a href='productInfo/"+results[x][4]+"' target='blank'><img src='view_pdf.gif' border='0'></a><br><img src='../images/spacer.gif' height='4' width='3'>";
	}

	str += "<tr><td valign='top'>" + imgCode + "</td><td valign='top'>"+ calCode + docCode +"</td></tr>";
	
	str += "<tr><td valign='top'' colspan='2'><img src='../images/spacer.gif' height='4' width='3'></td></tr>";
	str += "<tr><td valign='top' colspan='2' width='50%' class='title' bgColor=#3A479A>Product Detail:</td></tr>";
	str += "<tr><td valign='top'' colspan='2'><img src='../images/spacer.gif' height='4' width='3'></td></tr>";
	
	str += "<tr><td valign='top' class='lightcell'>Regulations</td><td valign='top' valign='top' class='whitecell'>"+results[x][11]+"</td></tr>";
	str += "<tr><td valign='top' class='lightcell'>Hazard Classifications</td><td valign='top' valign='top' class='whitecell'>"+results[x][12]+" m3</td></tr>";
	str += "<tr><td valign='top' class='lightcell'>Max Flow Rate</td><td valign='top' valign='top' class='whitecell'>"+results[x][13]+"</td></tr>";
	str += "<tr><td valign='top' class='lightcell'>Mounting</td><td valign='top' valign='top' class='whitecell'>"+results[x][14]+"</td></tr>";
	str += "<tr><td valign='top' class='lightcell'>Isolated Currents</td><td valign='top' valign='top' class='whitecell'>"+results[x][15]+"</td></tr>";
	str += "<tr><td valign='top' class='lightcell'>Supply Voltages</td><td valign='top' valign='top' class='whitecell'>"+results[x][16]+"</td></tr>";

	str += "<tr><td valign='top'' colspan='2'><img src='../images/spacer.gif' height='4' width='3'></td></tr>";
	str += "<tr><td valign='top' colspan='2' width='50%' class='title' bgColor=#3A479A>Your Selection:</td></tr>";
	str += "<tr><td valign='top'' colspan='2'><img src='../images/spacer.gif' height='4' width='3'></td></tr>";


	str += "<tr><td valign='top' class='lightcell'>Regulation</td><td valign='top' valign='top' class='whitecell'>"+reg+"</td></tr>";
	str += "<tr><td valign='top' class='lightcell'>Hazard Classification</td><td valign='top' valign='top' class='whitecell'>"+haz+"</td></tr>";

	str += "<tr><td valign='top' class='lightcell'>Messurement Type</td><td valign='top' valign='top' class='whitecell'>"+AunitType+"</td></tr>";
	if (AunitType == 'Metric')
	{
		volType = "m<sup>3</sup>"
	}else{
		volType = "cu.ft."
	}
	str += "<tr><td valign='top' class='lightcell'>Enclosure Volume</td><td valign='top' valign='top' class='whitecell'>"+Avol2+" " + volType +"</td></tr>";
	str += "<tr><td valign='top' class='lightcell'>Purge Time</td><td valign='top' valign='top' class='whitecell'>"+Atime+" mins</td></tr>";

	switch (Amount)
	{
	case '':
		mountType = '';
		break;
	case 'SS':
		mountType = 'Stainless Steel Enclosure';
		break;
	case 'BP':
		mountType = 'Direct Mount External';
		break;
	case 'PM':
		mountType = 'Panel Mounted';
		break;
	}
	str += "<tr><td valign='top' class='lightcell'>Mounting</td><td valign='top' valign='top' class='whitecell'>"+mountType+"</td></tr>";
	str += "<tr><td valign='top' class='lightcell'>Isolated Current</td><td valign='top valign='top' class='whitecell'>"+Avolts+"</td></tr>";
	str += "<tr><td valign='top' class='lightcell'>Supply Voltage</td><td valign='top' valign='top' class='whitecell'>"+Aamps+"</td></tr>";


	str += "</table><br></body></html>";

	formsFrame.document.write(str);
	formsFrame.document.close();
}
//function to show actual purge time from volume and flow rate.
function actualTime(prod)
{
	//get volume
	var v = Avol;
	//get product max flow
	var Q = results[prod][6];
	//calculated purge time
	var t = v*1000*5/Q;
	//round value to be mins
	t = parseInt(t);

	str = "<html><body onLoad='window.focus();'>"
	str += "<link href='../stylesheets/wizard.css' rel='stylesheet' type='text/css'>"
	str += "<table width=90% border=0 align=center cellpadding='2' cellspacing='0'>";
	str += "<tr><td colspan='2'><TABLE height='100%' cellSpacing=0 cellPadding=0 width='100%' border=0>";
	str += "<TR><TD valign=top bgColor=#B2B2B2 height=21 width=21><IMG height=21 hspace=0 src='../assets/Images/titleplus.gif' width=21 border=0></td>";
	str += "<td class='title' valign=middle bgColor=#3A479A><table width='100%'><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td class='title'>Calculated Purge Time</td></tr></table></TD></TR></TABLE><br></td></tr>";


	str += "<tr><td valign='top' class='whitecell'>"

	str += "<table width=100% border=0 align=center cellspacing=2>";
	str += "<tr><td valign='top' colspan='2'><div align='center'><b>Estimated Purge Time "+t+" mins</b></div></td></tr>";
	str += "<tr><td valign='top'>&nbsp;</td><td valign='top'>&nbsp;</td></tr>";
	
	str += "<tr><td valign='top'><b>Purge Time Formula</b></td><td valign='top'>time = (volume * 5000)/flow rate</td></tr>";
	str += "<tr><td><b>Volume</b></td><td>"+Avol+" m<sup>3</sup></td></tr>";
	str += "<tr><td><b>Flow Rate</b></td><td>"+Q+" m/sec</td></tr>";

	
	
	str += "</table>";

	str += "</td></tr>";
	str += "</table></body></html>";


	


	pager("blank.htm");

	window.popUp.document.write(str)
	window.popUp.document.close();
	
}

function pager(p){

		if (p!="blank.htm")
		{
			p = "../HTML/" + p;
		}
		var w = 300;
		var h = 350;

		var popleft=(window.screen.width/2 - w/2);
		var poptop=(window.screen.height/2-h/2)	;
		window.popUp = window.open(p,"NewWindow","scrollbars=AUTO,width="+w+",height="+h+",left="+popleft+",top="+poptop);	
		window.popUp.focus();
}