/**
 * @author Jerry Holmes
 */

var pwtitles = null;
var pwcols = 0;
var pwdata = null;
var pwcontainer = null;
var pwstepchoices = $H();
var pwprodcol = 5;

function initProducts()
{
	
	// LOAD PRODUCT DATABASE ASYNCRONOUSLY
	
	pwcontainer = $('divProductWizard');
	var url = "http://www.thermaxxjackets.com/media/sites/23/files/products.csv";
    var http = new JKL.ParseXML.CSV( url );
    http.async(processData);
	http.parse();

}

Event.observe(window,"load",initProducts);

// PARSE CSV DATA

function processData(data)
{
	pwdata = $A(data)
	//console.dir(data)
	
	// USE FIRST ROW AS AS TITLES AND GET LENGTH
	
	pwtitles = data[0].clone();
	pwcols = pwtitles.length;
	pwdata.splice(0,1);
	pwstepchoices[-1] = pwdata;
	
	pwcontainer.update('');
	showStep(0);
}

// SHOW A STEP

function showStep(pwstep)
{
	// DELETE HIGHER STEPS
	
	//console.info("Showing step: " + pwstep)
	var hstep = pwstep;
	while(true)
	{
		var hs = $('divStep' + hstep);
		if(hs != null)
		{
			Element.remove(hs)
		}
		else
		{
			break;
		}
		hstep++;
	}
	
	// GET CHOICES
	
	choices = $A()
	pwstepchoices[pwstep-1].each(function(row){
		if (pwstep == pwprodcol) {
			choices.push({
				name: row[pwstep],
				part: row[pwstep + 1],
				list: row[pwstep + 2]
			});
		}
		else {
			choices.push(row[pwstep]);
		}
	});
	
	choices = choices.uniq();
	
	var divStep = new Element("div",{id:"divStep" + pwstep});
	pwcontainer.insert({bottom:divStep});
	divStep.insert({
			bottom: "<br/><strong>Step " + (pwstep + 1) + "</strong><hr/><br/>"
		});
	
	if(pwstep == pwprodcol)
	{
		var label = new Element("label", {
			style: "font-weight:bold"
		});
		label.update("Please choose a product:");
		divStep.insert({
			bottom: label
		});
		divStep.insert({
			bottom: "<br/>"
		});
		
		table = new Element("table",{style:"width:100%"});
		tbody = new Element("tbody");
		table.insert({bottom:tbody});
		
		select.observe("change", function(e){
			changeStep(pwstep);
		})
		
		choices.each(function(c){
			var tr = new Element("tr");
			tbody.insert({
				bottom: tr
			});
			
			var td = new Element("td");
			td.update(c.part);
			tr.insert({bottom:td});
			
			td = new Element("td");
			td.update(c.name);
			tr.insert({bottom:td});
			
			td = new Element("td");
			td.update(c.list);
			tr.insert({bottom:td});
			
			td = new Element("td");
			td.insert({bottom:
			"<form method='post' action='http://ww12.aitsafe.com/cf/add.cfm'>" +
				"<input type='hidden' value='E8305297' name='userid'/>" +
				"<input type='hidden' value='http://thermaxx.pagedragon.com/page/order-now' name='return'/>" +
				"<input type='hidden' value='" + c.name + "' name='product'/>" +
				"<input type='hidden' value='" + c.list.replace('$','').replace(',','') + "' name='price'/>" +
				"<input type='submit' value='Buy Now'/>" +
			"</form>"
			});
			tr.insert({bottom:td});
			
		});
		divStep.insert({bottom:table});
		return;
	}
	
	if (choices.length > 1) {
		var label = new Element("label", {
			style: "font-weight:bold"
		});
		label.update("Please choose: " + pwtitles[pwstep]);
		divStep.insert({
			bottom: label
		});
		divStep.insert({
			bottom: "<br/>"
		});
		if(pwtitles[pwstep] == "Thickness")
		{
			divStep.insert({
				bottom: '<p>1/2" thick Up to 300 F or 149 C<br> 1" thick Up to 450 F or 232 C<br> 2" thick Up to 600 F or 315 C<br> Above 600 F or 315 C Please call or email for personal assistance with your application.</p>'
			});
		}
		select = new Element("select", {
			id: "step" + pwstep
		})
		select.observe("change", function(e){
			changeStep(pwstep);
		})
		var option = new Element("option");
		option.update("Please Choose...&nbsp;&nbsp;");
		select.insert({
			bottom: option
		});
		choices.each(function(c){
			var option = new Element("option");
			option.update(c);
			select.insert({
				bottom: option
			});
		});
		divStep.insert({bottom:select});
		return;
	}
	if (choices.length == 1) {
		var label = new Element("label", {
			style: "font-weight:bold"
		});
		var val = choices[0];
		if(val == "")
		{
			val = "N/A";
		}
		label.update(pwtitles[pwstep] + ": " + val);
		divStep.insert({
			bottom: label
		});
		select = new Element("input", {
			id: "step" + pwstep,
			type: "hidden",
			value: choices[0]
		})
		divStep.insert({
			bottom: select
		});
		changeStep(pwstep);
		return;
	}
	
	
}

function changeStep(step)
{
	//console.info("Event for step:" + step)
	var val = $F("step" + step)
	stepdata = $A()
	pwstepchoices[step-1].each(function(row){
		if (val == row[step]) {
			stepdata.push(row);
		}
	});
	pwstepchoices[step] = stepdata;
	
	showStep(step + 1);
}
