//-------------------------------------------------------------------------------------------------
// purchase.js
//
//  
//
// Sonalksis, 2009
//-------------------------------------------------------------------------------------------------

//global variables that can be used by ALL the functions on this page.
var vatRate  = 0.15;

var inputs;
var imgFalse = 'images/check_blue_off.png';
var imgTrue  = 'images/check_blue_on.png';
var imgOff   = 'images/check_grey_on.png';

var imgBundleFalse = 'images/check_yellow_off.png';
var imgBundleTrue  = 'images/check_yellow_on.png';

var IdIndex  = { "productBox" : 0, "productType" : 1};  // enum to select id with multiple words

//runs when page loads using: <script type="text/javascript">window.onload  = init;</script>
function init()
{
	loadInputs();
	replaceChecks();
	updateTotal(prices, hasVatCharge);
}


function getIdWithIndex(element, index)
{
	var id = element.getAttribute('id');
	
	if (id != null)
	{
		var idArray = id.split(" ");
		
		if ((index >= 0) && (index < idArray.length))
		{
			return idArray[index];
		}
	}
	
	return "null";
}


function loadInputs()
{	
	var inputElements = document.getElementsByTagName('input');
	
	inputs = new Array();
	
	for ( var i = 0; i < inputElements.length; i++)
	{	
		if (getIdWithIndex(inputElements[i], IdIndex.productBox) == "productBox")
		{
			inputs.push(inputElements[i]);
		}
	}
}

function replaceChecks()
{	
	//cycle trough the input fields
	for(var i=0; i < inputs.length; i++)
	{
		//create checkbox images
		var img         = document.createElement('img');  // clickable one
		var imgDisabled = document.createElement('img');  // disabled (greyed out)
		
		imgDisabled.src = imgOff;
		
		//check if the checkbox is checked
		if(inputs[i].checked)
		{
			if (getIdWithIndex(inputs[i], IdIndex.productType) == "bundle")
			{
				img.src = imgBundleTrue;
			}
			else
			{
				img.src = imgTrue;
			}
		}
		else
		{
			if (getIdWithIndex(inputs[i], IdIndex.productType) == "bundle")
			{
				img.src = imgBundleFalse;
			}
			else
			{
				img.src = imgFalse;
			}
		}

		//set image ID
		img.id         = 'checkImage' + i;
		imgDisabled.id = 'checkDisabled' + i;
		
		//set image onclick action
		img.onclick = new Function('checkChange('+i+')');
		
		//place image in front of the checkbox
		inputs[i].parentNode.insertBefore(imgDisabled, inputs[i]);
		inputs[i].parentNode.insertBefore(img, imgDisabled);
		
		//hide the real and disabled checkboxes
		inputs[i].style.display='none';
		imgDisabled.style.display='none';
	}
}

function updateImage(index, isTrue)
{
	var img = document.getElementById('checkImage'+index);

	if (isTrue)
	{
		if (getIdWithIndex(inputs[index], IdIndex.productType) == "bundle")
		{
			img.src = imgBundleTrue;
		}
		else
		{
			img.src = imgTrue;
		}
	}
	else
	{
		if (getIdWithIndex(inputs[index], IdIndex.productType) == "bundle")
		{
			img.src = imgBundleFalse;
		}
		else
		{
			img.src = imgFalse;
		}
	}
}


//change the checkbox status and the replacement image
function checkChange(i)
{
	if (inputs[i].checked)
	{
		updateImage(i, false);
		
		inputs[i].parentNode.parentNode.style.background = 'black';
		
		inputs[i].click();  // simulate click to change 'real' checkbox :)
	}
	else
	{
		updateImage(i, true);
		
		//document.getElementById('productEntry'+i).style.background= '#343434';
		
		inputs[i].parentNode.parentNode.style.background = '#101010'; 
		
		inputs[i].click();
	}
}


function bundleEntryUpdate(boxId, webIndents, prices, hasVatCharge)
{	
	//alert("boxId: " + boxId + " webIndent: " + webIndents[boxId] + " price: " + prices[boxId]);
	
	// if checked, hide checkboxes with greater webIndents
	var n = boxId + 1;
	
	if (inputs[boxId].checked)
	{		
		while ( (webIndents[n] > webIndents[boxId]) && (n < webIndents.length) )
		{
			// clear checked subproducts
			inputs[n].checked = '';
			updateImage(n, false);
			
			// hide sub-item prices and replace their boxes with greyed ticks
			document.getElementById('checkImage'+n).style.display      = 'none';
			document.getElementById('checkDisabled'+n).style.display   = 'inline';
			document.getElementById('currencyDiv'+n).style.display     = 'none';
			document.getElementById('priceDiv'+n).style.display        = 'none';
			//document.getElementById('productEntry'+n).style.background = '#1F1F1F';
			inputs[n].parentNode.parentNode.style.background = '#101010';
			n++;
		}
	}
	else
	{
		while ( (webIndents[n] > webIndents[boxId]) && (n < webIndents.length) )
		{	
			// show sub-item prices and replace grey boxes.
			document.getElementById('checkDisabled'+n).style.display   = 'none';
			document.getElementById('checkImage'+n).style.display      = 'inline';
			document.getElementById('currencyDiv'+n).style.display     = 'inline';
			document.getElementById('priceDiv'+n).style.display        = 'inline';
			//document.getElementById('productEntry'+n).style.background = 'black';
			inputs[n].parentNode.parentNode.style.background = 'black';
			n++;
		}
	}
	
	updateTotal(prices, hasVatCharge);
}

// called by bundleEntryUpdate function AND by single entry "onclick" inputs
function updateTotal(prices, hasVatCharge)
{
	var net   = 0.00;
	var vat   = 0.00;
	var total = 0.00;
	
	for (var i = 0; i < inputs.length; i++)
	{
		if (inputs[i].checked)
		{
			net += parseFloat(prices[i]);	
		}
	}
	
	total = net;
	
	if (hasVatCharge == true)
	{
		vat   = net * vatRate;
		total = net + vat;
	}
	
	document.getElementById('netDynamic').innerHTML   = '<p class=/"small/">' + net.toFixed(2) + '</p>';
	document.getElementById('vatDynamic').innerHTML   = '<p class=/"small/">' + vat.toFixed(2) + '</p>';
	document.getElementById('totalDynamic').innerHTML = '<p class=/"small bold/">' + total.toFixed(2) + '</p>';
	
}

