function ChangeMainProdPic(picID, title, topMargin)
{
	if(picID > 0)
	{
		// update the picture
		var mainPic = $('#product #mainImage > img');
		mainPic.css('margin-top', topMargin > 0 ? topMargin + 'px' : '');
		mainPic.attr('src', '/skuPics/' + picID + '_m.jpg');
		
		// update the link
		var picLink = $('#product #mainImage a');
		picLink.attr('href', '/skuPics/' + picID + '_l.jpg');
		picLink.attr('title', title);
		
		//document.getElementById('prodDetailsPicsLarge').style.backgroundImage = 'url(/skuPics/' + picID + '_m.jpg)';
		//document.getElementById('enlargePic').href = '/skuPics/' + picID + '_l.jpg';
	}
	return false;
}

var currentTabID = 0;
function SwitchProdTab(tabID)
{
	currentTabID = tabID;
	for(i = 0; i <= 3; i++)
	{
		var e = $('tabID' + i);
		if(e)
		{
			if(i != tabID)
			{
				e.style.backgroundImage = 'url(/images/prodTabOff.jpg)';
				e.style.fontWeight = 'normal';
				e.className = 'none';
			}
			else
			{
				e.style.backgroundImage = 'url(/images/prodTabOn.jpg)';
				e.style.fontWeight = 'bold';
				$('prodTextMainScroll').innerHTML = tabTextContent[i];
			}
		}
	}
	return false;
}


function TabOver(tabID, OnOff)
{
	if(OnOff == 'On' || currentTabID != tabID)
	{
		var e = $('tabID' + tabID);
		e.style.backgroundImage = 'url(/images/prodTab' + OnOff + '.jpg)';
	}
}


function previewStarRating(name, rating)
{
	// get all the images
	var images = document.getElementById(name+"_Container").getElementsByTagName("IMG");
	
	for(var i = 1; i <= images.length; i++)
		images[i-1].src="/images/star-"+(i <= rating ? "on" : "off")+".gif";
}

function resetStarRating(name)
{
	// get the selected rating
	var rating = document.getElementById(name).value;
	
	previewStarRating(name, rating);
}

function setStarRating(name, rating)
{
	// update the hidden input
	document.getElementById(name).value = rating;
	
	previewStarRating(name, rating);
}

function createViewAllForm(optionName, selectedManufacturers, selectedSubcategories, selectedPriceRanges, query, order, mode)
{
	var title = '';
	var options = [];
	
	switch(optionName)
	{
		case 'manufacturer':
			options = manufacturers;
			title = 'Select Manufacturers';
			break;
		case 'subcategories':
			options = subcategories;
			title = 'Select Sub Categories';
			break;
	}
	
	var totalOptions = options.length;
	
	var optionsPerColumn = Math.max(10, Math.ceil(totalOptions / 2));
	
	// build the html, each column is a list with each item in the column a list item
	var html = '<form method="post">\n\
	<input type="hidden" name="priceRanges" value="' + selectedPriceRanges + '" />\n\
	<input type="hidden" name="query" value="' + query + '" />\n\
	<input type="hidden" name="orderID" value="' + order + '" />\n\
	<input type="hidden" name="modeID" value="' + mode + '" />';
	
	if(optionName != 'manufacturers')
		html += '<input type="hidden" name="manufacturer" value="' + selectedManufacturers + '" />';
	
	if(optionName != 'subcategories')
		html += '<input type="hidden" name="subcategories" value="' + selectedSubcategories + '" />';
	
	var option;
	var optionCount = 0;
	
	html += '<ul>';
	
	for(var i in options)
	{
		if(optionCount == optionsPerColumn)
		{
			html += '</ul><ul>';
			optionCount = 0;
		}
		
		option = options[i];
		
		html += '<li><label><input type="checkbox" name="' + optionName + '[]" value="' + option.ID + '"' + (option.Selected == true ? ' checked="checked"' : '') + ' />' + option.Name + ' (' + option.Count + ')</label></li>';
		
		optionCount++;
	}
	
	html += '</ul></form>';
	
	viewAllDialog.dialog({
			'title': title
		})
		.html(html)
		.dialog('open');
}

function EmbedFlash(url)
{
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="100%" height="100%" id="banner" align="middle" >');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="');
	document.write(url);
	document.write('" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="bgcolor" value="#000000" />');
	document.write('<embed src="');
	document.write(url);
	document.write('" quality="high" bgcolor="#000000" width="100%" height="100%" name="middle" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer"></embed>');
	document.write('</object>');
}

function updateCounty(countryCode, fieldPrefix)
{	
	var textBox = $('#' + fieldPrefix + '_County_Input');
	var selectBox = $('#' + fieldPrefix + '_County_Select');
		
	if(countryCode == 'US')
	{
		// get the current value in the text box		
		var textBoxValue = textBox.val();
		
		textBoxValue = textBoxValue.toLowerCase();
		
		// try and set the select box to the text value entered
		$('#' + fieldPrefix + '_County_Select option').each(function() {
			var op = $(this);
			
			if((op.text()).toLowerCase() == textBoxValue)
				op.attr('selected', true);
			else
				op.removeAttr('selected');
		});
		
		// hide and disable the text box and show and enable the select
		textBox.attr('disabled', true);
		textBox.css('display', 'none');
		
		selectBox.removeAttr('disabled');
		selectBox.css('display', '');
	}
	else
	{
		// check the text box is hidden
		if(textBox.css('display') == 'none')
		{
			// clear the current value
			textBox.val('');
			
			selectBox.attr('disabled', true);
			selectBox.css('display', 'none');
			
			textBox.removeAttr('disabled');
			textBox.css('display', '');
		}
	}
}

function LimitSelection(checkbox, name)
{
	$checkbox = $(checkbox);
	
	if($checkbox.is(':checked'))
	{
		$('input[name^="' + name + '"]').attr('checked', false);
		
		$checkbox.attr('checked', true);
	}
}

function EnableDisableField(checkbox, enable, fieldId)
{
	$checkbox = $(checkbox);
	
	if(!enable || !$checkbox.is(':checked'))
		$('#' + fieldId).val('');
	
	var enableField = false;
	
	if(enable)
	{
		if($checkbox.is(':checked'))
			enableField = true;
	}
	
	$('#' + fieldId).attr('disabled', !enableField);
}

function AddRepairItem(repairID, isLabour)
{	
	var url = 'repair-invoice-add-line.php?rid=' + repairID + (isLabour ? '&labour=1' : '');
	var height = isLabour ? 230 : 260;
	
	window.open(url, 'addline', 'status=0,toolbar=0,location=0,menubar=0,resizable=0,scrollbars=0,height=' + height + ',width=310');
}

function GetFromPopup(transactionItem)
{
	var repairItem = parseInt(transactionItem.repairItem);
	var qty = parseInt(transactionItem.qty);
	var cost = parseFloat(transactionItem.cost);
	var costPrice = parseFloat(transactionItem.costPrice);
	
	if(isNaN(repairItem) || isNaN(qty) || isNaN(cost) || isNaN(costPrice))
		return;
	
	var total = qty * cost;

	// add the row
	var html = '<tr itemNumber=\"' + transactionItem.repairItem + '\">\
		<td><input type="hidden" name="itemNumber[]" value="' + transactionItem.repairItem + '" />&nbsp;</td>\
		<td><input type="hidden" name="qty[]" value="' + qty + '" />' + qty + '</td>\
		<td><input type="hidden" name="name[]" value="' + transactionItem.description + '" />' + transactionItem.description + '</td>\
		<td>&nbsp;</td>\
		<td><input type="hidden" name="unitCost[]" value="' + cost + '" /><input type="hidden" name="unitCostPrice[]" value="' + costPrice + '" />' + cost.toFixed(2) + '</td>\
		<td class="lineTotal" labour="' + (transactionItem.isLabour ? '1' : '0') + '"><input type="hidden" name="labour[]" value="' + (transactionItem.isLabour ? '1' : '0') + '" />' + total.toFixed(2) + '</td>\
		<td><a href="#nogo" class="remove"><img src="../images/delete.png" alt="Remove" title="Remove" style="float:left; height:16px; width:16px;" /></a></td>\
	</tr>';
	
	$(html).insertBefore('#invoiceTable tr.itemTotal[itemNumber="' + repairItem + '"]');
	
	UpdateTotals();
}

function UpdateTotals()
{
	var itemTotal = new Array();
	
	for(var i = 0; i < totalItems; i++)
		itemTotal[i] = 0;
	
	var labourTotal = 0;
	var partsTotal = 0;
	var total;
	
	$('#invoiceTable tr td.lineTotal').each(function(index) {
		$td = $(this);
		
		total = parseFloat($td.text());

		if(isNaN(total))
			total = 0;
		
		var itemNumber = parseInt($td.parent('tr').attr('itemNumber'));
		
		if(!isNaN(itemNumber))
		{
			itemNumber--;
			
			itemTotal[itemNumber] += total;
		}
		
		if($td.attr('labour') == '1')
			labourTotal += total;
		else
			partsTotal += total;
	});
	
	// update item totals
	for(var i = 0; i < totalItems; i++)
		$('#invoiceTable tr.itemTotal[itemNumber="' + (i+1) + '"] td.itemTotalCell').html(itemTotal[i].toFixed(2));
	
	// update parts/labour totals
	$('#invoiceTable .total #labourTotal').html(labourTotal.toFixed(2));
	$('#invoiceTable .total #partsTotal').html(partsTotal.toFixed(2));
	
	// get the carriage value
	var carriage = parseFloat($('#invoiceTable .total #carriage input').val());
	if(isNaN(carriage))
		carriage = 0;
	
	var subTotal = (Math.round(labourTotal * 100) + Math.round(partsTotal * 100) + Math.round(carriage * 100)) / 100;	
	$('#invoiceTable .total #invoiceSubTotal').html(subTotal.toFixed(2));
	
	var vat = subTotal * 0.2;
	$('#invoiceTable .total #vat').html(vat.toFixed(2));
	
	// update the invoice total
	var invoiceTotal = subTotal + vat;
	
	$('#invoiceTable .total #invoiceTotal').html(invoiceTotal.toFixed(2));
}
