var http_request = false;
var div_hbody = document.getElementById("hbody"); 
var img_path = "http://www.ergo4me.com/chair_selector/gallery/";
var	div_content = document.getElementById("div_content");
var	div_result = document.getElementById("div_result");
var	div_final = document.getElementById("div_final");
var div_cont = document.getElementById("prod-cont");
var div_options = document.getElementById("alloptions");
var chair_options = document.getElementById("options_chair");
var arm_options = document.getElementById("options_arm");
var span_proID =  document.getElementById("proID");
var img_pro = document.getElementById("img_pro"); 
var div_alt = document.getElementById("alt_prodImages"); 
var img_alt1 = document.getElementById("alt_1"); 
var img_alt2 = document.getElementById("alt_2"); 
var img_alt3 = document.getElementById("alt_3"); 
var img_alt4 = document.getElementById("alt_4"); 
var div_chair_options = document.getElementById("chair_options"); 
var div_arm_options = document.getElementById("arm_options"); 
var img_desc = document.getElementById("img_desc");
var div_hb = document.getElementById("hb"); 
var frm_main = document.getElementById("frm_main");

function makeRequest(url, parameters) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	//http_request.onreadystatechange = alertContents;
	//http_request.open('GET', url, true);
	//http_request.send(null);

	  http_request.onreadystatechange = alertContents;
	  http_request.open('POST', url, true);
	  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	  http_request.setRequestHeader("Content-length", parameters.length);
	  http_request.setRequestHeader("Connection", "close");
	  http_request.send(parameters);
}

function alertContents() {
	if (http_request.readyState == 4) {
		if (http_request.responseText.indexOf('Error -')==-1)
		{
			if (http_request.responseText=="done!")
			{
				//show_final();
				document.location.href= "./chairselectorfinal.php";
			}
			else
			{
				div_content.style.display = "none";
				place_pro(http_request.responseText);
				div_result.style.display = "block";
			}
		}
		else
		{
				alert (http_request.responseText);
		}
	}
}

function do_search(objID) {

		tmp_data = hbcode();

		var data ="";
		if (objID=="")
		{
			data = "height=" + frm_main.height.value + "&weight=" + frm_main.weight.value + "&inseam=" + frm_main.inseam.value +
							"&headrest=" + frm_main.headrest.value + "&seat=" + frm_main.seat.value + "&bdop=" + tmp_data;
		}
		else
		{
			data = "proID=" + objID;
		}
		makeRequest('chair_selector/data.php',data );
}

function show_search() {
	div_content.style.display = "block";
	div_result.style.display = "none";
	document.getElementById('img_arm').src = img_path + "arms/0.gif";
	img_pro.src = img_path+"0.jpg";
	img_alt1.src = img_path + "small/00.jpg";img_alt1.name = 0;
	img_alt2.src = img_path + "small/00.jpg";img_alt2.name = 0;
	img_alt3.src = img_path + "small/00.jpg";img_alt3.name = 0;
	img_alt4.src = img_path + "small/00.jpg";img_alt4.name = 0;
	img_pro.title = "";	
}

function do_changearm(sel)
{
	document.getElementById('img_arm').src = img_path + "arms/" + sel.value + ".gif";
	document.getElementById(sel.options[sel.selectedIndex].id.replace('-','')).checked = true;
	gen_options_code(div_arm_options, arm_options);
}

function gen_options_code(div_options,span_options)
{
	if (document.all)
	{
		var curr_options= div_options.all.tags('INPUT');
	}
	else
	{
		var curr_options= div_options.getElementsByTagName('INPUT');
	}
	var tmp_code = '';
	for (i=0;i<curr_options.length;i++)
	{	
			if (curr_options[i].checked)
			{
				if (tmp_code=='')
				{
					tmp_code = tmp_code + curr_options[i].value;
				}
				else
				{
					tmp_code = tmp_code + '-' + curr_options[i].value;
				}
			}
	}
	span_options.innerHTML = tmp_code;	
}

function swap_pro(objName)
{
	if (objName!=0)
	{
		img_pro.src = img_path+"0.jpg";
		img_alt1.src = img_path + "small/00.jpg";img_alt1.name = 0;
		img_alt2.src = img_path + "small/00.jpg";img_alt2.name = 0;
		img_alt3.src = img_path + "small/00.jpg";img_alt3.name = 0;
		img_alt4.src = img_path + "small/00.jpg";img_alt4.name = 0;
		do_search(objName);
	}
}

function place_pro(pro_id)
{
	var str_return = pro_id;
	str_return = str_return.split(':');
	str_main = str_return[0].split(',');
	img_pro.src = img_path + str_main[0] + ".jpg";
	img_pro.title = str_main[1];
	if (str_return[0]==0)
	{
		span_proID.innerHTML = "N/A";
	}
	else
	{
		span_proID.innerHTML = str_main[0];
	}
	if (str_return[1])
	{
		str_main = str_return[1].split(',');
		img_alt1.src = img_path + "small/" + str_main[0] + ".jpg";img_alt1.name = str_main[4];
		img_alt2.src = img_path + "small/" + str_main[1] + ".jpg";img_alt2.name = str_main[5];
		img_alt3.src = img_path + "small/" + str_main[2] + ".jpg";img_alt3.name = str_main[6];
		img_alt4.src = img_path + "small/" + str_main[3] + ".jpg";img_alt4.name = str_main[7];
	}

	if (str_return[3])
	{
		if (document.all)
		{
			var curr_options= div_chair_options.all.tags('INPUT');
		}
		else
		{
			var curr_options= div_chair_options.getElementsByTagName('INPUT');
		}
		for (i=0;i<curr_options.length;i++)
		{	
			curr_options[i].checked=false;
		}

		str_main = str_return[2].split(',');
		for (itm in str_main)
		{
				if (document.getElementById("opt_"+str_main[itm]))
				{
						document.getElementById("opt_"+str_main[itm]).checked = true;
				}
		}

		str_main = str_return[3].split(',');
		document.getElementById("armopt_"+str_main[0]).checked = true;
		change_arm(document.getElementById("armopt_"+str_main[0]));
		gen_options_code(div_chair_options, chair_options);
	}
}

function show_details()
{
	if (div_options.style.display=="none")
	{
		div_options.style.display="block";
	}
	else
	{
		div_options.style.display="none";
	}
}

function activate_app()
{
	document.getElementById("btn_submit").disabled=false;
	document.getElementById("btn_submit_up").disabled=false;
	document.getElementById("btn_reset").disabled=false;
}

function send_req()
{

	if (trim(frm_main.name.value)=="" && trim(frm_main.email.value)=="" && trim(frm_main.phone.value)=="")
	{
			alert("Please enter at least a name and email or phone number");
			frm_main.name.focus();
			return false;
	}
	else if (trim(frm_main.name.value)=="")
	{
			alert("Please enter your name.");
			frm_main.name.focus();
			return false;
	}
	else if (trim(frm_main.email.value)=="" && trim(frm_main.phone.value)=="")
	{
			alert("Please enter a phone or email.");
			frm_main.email.focus();
			return false;
	}

document.getElementById("dt_submit").disabled=true;
	data = "comp=1&compdata" + 
	"=" + create_opts(0) +
	":" + encodeURI(span_proID.innerHTML) +
	":" + create_opts(1) +
	":" + create_opts(2) +
	":" + frm_main.height.value +
	":" + frm_main.weight.value +
	":" + frm_main.seat.value +
	":" + frm_main.headrest.value +
	":" + frm_main.inseam.value + 
	":" + encodeURI(frm_main.name.value) + 
	":" + encodeURI(frm_main.email.value) + 
	":" + encodeURI(frm_main.phone.value); 
	makeRequest('chair_selector/data.php',data );
}

function change_arm(opt)
{
	document.getElementById('-' + opt.id).selected=true;
	document.getElementById('img_arm').src = img_path + "arms/" + document.getElementById('-' + opt.id).value + ".gif";
	gen_options_code(div_arm_options,arm_options);
}

function create_opts(typ)
{
	switch (typ)
	{
		case 0:
			return hbcode();
		case 1:
			return optcode(div_chair_options);
		case 2:
			return optcode(div_arm_options);
	}
}

function hbcode()
{
			if (document.all)
			{
				var curr_options= div_hb.all.tags('INPUT');
			}
			else
			{
				var curr_options= div_hb.getElementsByTagName('INPUT');
			}
			
			var tmp_data = '0';
			for (i=0;i<curr_options.length;i++)
			{	
				if (curr_options[i].checked){
					tmp_data = tmp_data + ',' + curr_options[i].id.replace('bd_','');
				}
			}
			return tmp_data;
}

function optcode(div_options)
{
	if (document.all)
	{
		var curr_options= div_options.all.tags('INPUT');
	}
	else
	{
		var curr_options= div_options.getElementsByTagName('INPUT');
	}
	var tmp_code = '0';
	for (i=0;i<curr_options.length;i++)
	{	
			if (curr_options[i].checked)
			{
				tmp_code = tmp_code + ',' + curr_options[i].id.replace('opt_','').replace('arm','');
			}
	}
	return tmp_code;
}

function show_final()
{
	div_cont.style.display = "none";
	div_result.style.display = "none";
	div_final.style.display = "block";
	div_content.style.display = "block";
}

function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
function trim( value ) {
	return LTrim(RTrim(value));
}
function dynpop(frm) {
	div_hbody.style.display = "none";
	document.getElementById("popdesc1").style.display="none";
	document.getElementById("popdesc2").style.display="none";
	document.getElementById("popdesc3").style.display="none";
	document.getElementById("popdesc"+frm).style.display="block";
}
function dynpopclose() {
	div_hbody.style.display = "block";
	document.getElementById("popdesc1").style.display="none";
	document.getElementById("popdesc2").style.display="none";
	document.getElementById("popdesc3").style.display="none";
}