
var order = new Array();

function add(id)
{	
	id = stripI(id);
	
	if (order.length==0)
	{
		// show table, head/foot
		document.getElementById('order').style.visibility="visible";
		showElement('head');
		showElement('foot1');
		showElement('foot2');
	}
	
	var visible = order[id];
	if (!visible)
	{
		showElement('r'+id); // show row
	}
	
	inc(id);
	
	var quantity = document.getElementById('q'+id);
	var val = quantity.innerHTML;
	val = parseInt(val) + 1;
	quantity.innerHTML = val;
	document.getElementById('qh'+id).value = val;
	
	//document.forms['f'+id].quantity.value = val;
	
	updateTotal();
}

function remove(id)
{
	id = stripI(id);
	
	dec(id);
	
	var quantity = document.getElementById('q'+id);
	var val = quantity.innerHTML;
	val = parseInt(val) - 1;
	if (val<0)
	{
		val = 0;
	}
		
	quantity.innerHTML = val;
	document.getElementById('qh'+id).value = val;
	
	//alert(val);
	if(val <= 0)
	{
		hideElement('r'+id);
	}
	
	//document.forms['f'+id].quantity.value = val;
	
	updateTotal();
}

function stripI(id)
{
	// strip i from i30, etc.
	id = id.toString();
	id = id.substr(1,id.length);
	return id;
}

// increase part count
function inc(id)
{
	var val = parseInt(order[id]);
	if (isNaN(val))
	{
		val = 1;
	}
	else
	{
		val++;
	}
	order[id] = val;
}

// decrease part count
function dec(id)
{
	var val = parseInt(order[id]);
	if (isNaN(val))
	{
		val = 0;
	}
	else
	{
		val--;
	}
	if (val<0)
	{
		val = 0;
	}
	order[id] = val;
}

function showElement(id)
{
	var el = document.getElementById(id);
	
	// UGLY HACK to fix stupid Internet Explorer not supporting CCS1 display property table-row
	var ie = isIE();
	
	if (!ie)
	{
		el.style.display = 'table-row';
	}
	else
	{
		el.style.display = 'block'; // eat that IE
	}
}

function hideElement(id)
{
	var el = document.getElementById(id);
	el.style.display = 'none';
}

function clearAll()
{
	
	for (var i in order)
	{
		var el = document.getElementById('q'+i);
		el.innerHTML = '0';
		var el = document.getElementById('t'+i);
		el.innerHTML = '0.00';
	}
	
	setTotalCost(0);
	order = new Array();
}

function updateTotal()
{
	var totalCost = 0;
	
	for (var key in order)
	{
		var price = parseFloat(document.getElementById('p'+key).innerHTML);
		var q = parseInt(document.getElementById('q'+key).innerHTML);
		var sum = price * q;
		document.getElementById('t'+key).innerHTML = sum.toFixed(2);
		totalCost = totalCost + sum;
	}
	
	setTotalCost(totalCost.toFixed(2));
}

/*function floatFix(val)
{
	return Math.round(val*100)/100; // fix js float representation accuracy
}*/

function setTotalCost(total)
{
	// fix for IE, redo corner styling when values change (because IE does not support CSS3 border-radius yet)
	if (total == 0)
	{
		total = '0.00';
	}
	document.getElementById('totalCost_div').innerHTML = "<span id='totalCost'>"+total+"</span>";
	document.getElementById('totalCostTitle').innerHTML = "<span>Total Cost</span>";
	reStyleBottom();
}

function isIE()
{
	if (navigator.appName == 'Microsoft Internet Explorer')
	{
		return true;
	}
	else
	{
		return false;
	}
}

function sendOrder()
{
	//console.log('1');

	var data = $.toJSON(order);
	var form = document.getElementById('form_send_data');
	var hid = document.getElementById('send_data');
	if(form && hid)
	{
		hid.value = data;
		form.submit();
	}
}

function sendOneItem(id)
{
	var orderOne = new Array();
	id = stripI(id);
	orderOne[id] = order[id];
	
	var data = $.toJSON(orderOne);
	var form = document.getElementById('form_send_data');
	var hid = document.getElementById('send_data');
	if(form && hid)
	{
		hid.value = data;
		form.submit();
	}
}

function dataFromServer(data)
{
	
	console.log(data);
	window.location = "shipping1.php?id="+data;
}

function success (data, textStatus)
{
	//console.log(data+' '+textStatus);
}

function error (XMLHttpRequest, textStatus, errorThrown)
{
	//console.log('err:'+textStatus+' '+errorThrown);
}