var xmlobj;
var col_code;

function loadXMLDoc(url)
{
	if (window.XMLHttpRequest) {
		xmlobj = new XMLHttpRequest();
		xmlobj.onreadystatechange = XMLchange_col;
		xmlobj.open("GET", url, true);
		xmlobj.send(null);
	} else if (window.ActiveXObject) {
		xmlobj = new ActiveXObject("Microsoft.XMLHTTP");
		if (xmlobj) {
			xmlobj.onreadystatechange = XMLchange_col;
			xmlobj.open("GET", url, true);
			xmlobj.send();
		}
	}
}

function XMLchange_col()
{
	if (xmlobj.readyState == 4 && xmlobj.status == 200)
	{
		var cbox = document.getElementById('cbox_'+col_code);
		var cxml = xmlobj.responseXML;
		var cr = cxml.getElementsByTagName('r')[0].firstChild.data;
		var cg = cxml.getElementsByTagName('g')[0].firstChild.data;
		var cb = cxml.getElementsByTagName('b')[0].firstChild.data;
		cbox.style.backgroundColor = 'rgb('+cr+','+cg+','+cb+')';
	}
}

function initialise_products ()
{
	if (!document.getElementsByTagName) return;
	var body_forms = document.getElementById('page_body').getElementsByTagName('form');
	for (var i=0;i<body_forms.length;i++)
	{
		if (!body_forms[i].product_size) continue;
		
		body_forms[i].onmouseover = show_hide_total;
		body_forms[i].onmouseout = show_hide_total;
		
		var trs = body_forms[i].getElementsByTagName('tbody')[0].getElementsByTagName('tr');
if(trs.length > 1)
{
	trs[0].style.background = '#448';
	trs[0].style.color = '#fff';
	for (var j=0;j<trs.length;j++)
	{
		if(trs[j].currentStyle)
		{
		trs[j].style.cursor = 'hand';
		}
		else
		{
		trs[j].style.cursor = 'pointer';
		}
		trs[j].onclick = click_prod_row;
	}
}
		body_forms[i].product_size.onchange = change_size_price;
		body_forms[i].quantity.onchange = change_qty_price;

		init_colour(body_forms[i]);

		var totals = document.createElement('span');
		totals.appendChild(document.createTextNode('Total Price \£'));
		totals.appendChild(document.createTextNode('1.00'));
		totals.style.color = 'navy';
		totals.style.marginLeft = '3em';
		totals.style.display = 'none';
		
		return_qty_label(body_forms[i]).appendChild(totals);

		redraw_total(body_forms[i]);
	}
}

function get_code(the_form)
{
		var prod_code = the_form.id;
		return prod_code.substring(8,(prod_code.length));
}

function init_colour(the_form)
{
		var color_option = document.getElementById('option_col_'+get_code(the_form));
		if (color_option){
			color_option.onchange = change_colour;
		}
}

function change_colour(e)
{
	var the_form = this.parentNode.parentNode.parentNode;
	col_code = get_code(the_form);
	
	if(!document.getElementById('cbox_'+col_code))
	{
		var fProperty = (document.all) ? 'styleFloat' : 'cssFloat';
		var box=document.createElement('div');
		box.id = 'cbox_'+col_code;
		box.style.width= '50px';
		box.style.height= '50px';
		box.style.border= '1px solid #000';
		box.style[fProperty] = 'right';
		box.appendChild(document.createTextNode(' '));
		the_form.getElementsByTagName('input')[0].parentNode.insertBefore(box,the_form.getElementsByTagName('label')[0]);	
	}
	
	var id = this.value.substring(0,this.value.indexOf('_'));
	loadXMLDoc('/colours/colour_rgb?colour='+id);
}

function init_trustlogo()
{
	var tlogo = document.getElementById('trustlogo_badge');
	if (tlogo)
	{
		if(tlogo.currentStyle){tlogo.style.cursor = 'hand';}
		else{tlogo.style.cursor = 'pointer';}
		tlogo.title = 'Comodo Seal - Click To Verify';
		tlogo.onclick= function(){window.open('/help/trustlogo.html','Secure','height=300,width=400');}
	}
}

function return_qty_label(the_form)
{
	return document.getElementById('qty_'+get_code(the_form)).parentNode;
}

function redraw_total(the_form)
{
	var size_id	= 'size_'+the_form.product_size.value;
	var total_cost = the_form.quantity.value * document.getElementById(size_id).innerHTML;

	var tqty = return_qty_label(the_form);
	
	tqty.lastChild.lastChild.data = clean_price(total_cost)
}

function change_size_price(e)
{
	var cont_form	= this.parentNode.parentNode.parentNode;
	var size_id	= 'size_'+this.value;
	var size_cont	= document.getElementById(size_id);
	
	var the_rows = size_cont.parentNode.parentNode.getElementsByTagName('tr');
	var row_index = size_cont.parentNode.rowIndex - 1;
	
	redraw_prod_table(row_index,the_rows);
	redraw_total(cont_form);
}

function change_qty_price(e)
{
	redraw_total(document.getElementById('product_'+get_code(this.parentNode.parentNode.parentNode)));
}

function click_prod_row(e)
{
	var rowidx = this.rowIndex - 1;
	var cont_form = this.parentNode.parentNode.parentNode.parentNode;
	cont_form.product_size.selectedIndex = rowidx;
	
	var the_rows = this.parentNode.getElementsByTagName('tr')
	redraw_prod_table(rowidx,the_rows);
	redraw_total(cont_form);
}

function redraw_prod_table(tridx,the_rows)
{
	for (var j = 0; j < the_rows.length; j++) {
		if (j%2 == 1){
			the_rows[j].style.background = '#fff';
		}
		else{
			the_rows[j].style.background = '#ddf';
		}
		the_rows[j].style.color = '#000';
	}
	the_rows[tridx].style.color = '#fff';
	the_rows[tridx].style.background = '#448';
}

function clean_price(price)
{
	price = Math.round(price*100) /100;
	price = price.toString();
	var dec_p_loc = price.indexOf(".");
	if (dec_p_loc == - 1){
		price = price + ".00";
	}
	if (dec_p_loc == price.length - 2){
		price = price + "0";
	}
	return price;
}

function show_hide_total(e)
{
	if(!e) var e = window.event;
	
	var total_d = return_qty_label(this).lastChild
	if(e.type=='mouseover')
	{
		total_d.style.display = 'inline';
	}
	else if(e.type=='mouseout')
	{
		total_d.style.display = 'none';
	}
}

window.onload = function () {
	if (!document.getElementById) return;
	document.getElementById('searchterm').onfocus = function(){
		if (this.defaultValue==this.value)	this.value = " ";
	}
	initialise_products();
	init_trustlogo();
}
