/*
  $Id: ccc_javascript.js,v 1.4 2005/06/17 17:20:50 stephen Exp $

  Copyright (c) 2005 SNJ Computers

  Released under the GNU General Public License
*/
function ccc_popup(url)
{
	window.open(url, 'popupWindow',
					'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width=700, height=450, screenX=150, screenY=150, top=150, left=150');
}

function buildImage(imageName, separator)
{
	if (!imageName)
	{
		var link = '<img src="images/no_image.gif" border="0">' + separator;
	}
	else
		var link = '<a href="'+arguments[2]+'"><img src="' + imageName + '" border="0"></a>' + separator;
	return link;
}

function createHiddenField(name, value)
{
	var redone = 0;
	try
	{
		kids = document.forms.builds.childNodes;
		for (var i = 0; i < kids.length; i++)
		{
			if (kids[i].id == name)
			{
				kids[i].value = value;
				redone = 1;
			}
		}
		if (redone == 0)
		{
			throw '';
		}
	}
	catch (e)
	{
		var input = document.createElement('INPUT');
		input.setAttribute('type', 'hidden');
		input.setAttribute('name', name);
		input.setAttribute('id', name);
		input.setAttribute('value', value);
		document.forms.builds.appendChild(input);
	}
}

function build_table_content(name)
{
	var table_content = '';
	ElementType = document.getElementById(name).getAttribute('type');
	switch (ElementType)
	{
		case 'drop':
			var text = document.getElementById(name).options[document.getElementById(name).options.selectedIndex].getAttribute('text');
			table_content += text + "<br>";
			break;
		case 'radio':
		case 'checkbox':
			elements = document.getElementsByName(name + '[]');
			if (elements)
			{
				for (f = 0; f < elements.length; f++)
				{
					if (elements[f].checked)
					{
						table_content += elements[f].getAttribute('text') + "<br>";
					}
				}
			}
			break;
	}
	return table_content;
}

function build_table()
{
	var table = '';
	for (a = 0; a < Computers.length; a++)
	{
		try
		{
			table += '<b>' + Computers[a][0] + ':</b><br>' + build_table_content(Computers[a][1]);
		}
		catch (e)
		{
		}
	}
	return table;
}

function update_total(totalTrans)
{
	var total = 0;
	var Selects = document.builds.getElementsByTagName("select");
	for (d = 0; d < Selects.length; d++)
	{
		_selected = Selects[d].options.selectedIndex;
		if (Selects[d].disabled == false)
		{
			total += Math.abs(Selects[d].options[_selected].getAttribute('price'));
		}
	}
	var Selects = document.builds.getElementsByTagName("input");
	for (e = 0; e < Selects.length; e++)
	{
		if (Selects[e].disabled == false && Selects[e].checked == true)
		{
			total += Math.abs(Selects[e].getAttribute('price'));
		}
	}
	if (totalTrans > 0)
	{
		total += Math.abs(totalTrans);
	}
	if (showSubTotal == 'true')
	{
		if (document.getElementById('SubTotal_Top'))
		{
			document.getElementById('SubTotal_Top').innerHTML = symbol_left + custRound(total, false) + symbol_right;
		}
		if (document.getElementById('SubTotal_Bottom'))
		{
			if (document.getElementById('SubTotal_Top'))
			{
				document.getElementById('SubTotal_Bottom').innerHTML = document.getElementById('SubTotal_Top').innerHTML;
			}
			else
			{
				document.getElementById('SubTotal_Bottom').innerHTML = symbol_left + custRound(total, false) + symbol_right;
			}
		}
	}
	if (baseProductPrice > 0)
	{
		createHiddenField('basePrice', baseProductPrice);
		if (showBase == 'true')
		{
			if (document.getElementById('Base_Top'))
			{
				document.getElementById('Base_Top').innerHTML = symbol_left + custRound(baseProductPrice, false) + symbol_right;
			}
			if (document.getElementById('Base_Bottom'))
			{
				if (document.getElementById('Base_Top'))
				{
					document.getElementById('Base_Bottom').innerHTML = document.getElementById('Base_Top').innerHTML;
				}
				else
				{
					document.getElementById('Base_Bottom').innerHTML = symbol_left + custRound(baseProductPrice, false) + symbol_right;
				}
			}
		}
		total += Math.abs(baseProductPrice);
	}
	if (buildPrice >= 0)
	{
		if (buildPrice <= 0)
		{
			buildPriceShown = textFree;
		}
		else
		{
			buildPriceShown = symbol_left + custRound(buildPrice, false) + symbol_right;
			createHiddenField('buildPrice', buildPrice);
			total += Math.abs(buildPrice);
		}
		if (showBuild == 'true')
		{
			if (document.getElementById('Build_Top'))
			{
				document.getElementById('Build_Top').innerHTML = buildPriceShown;
			}
			if (document.getElementById('Build_Bottom'))
			{
				if (document.getElementById('Build_Top'))
				{
					document.getElementById('Build_Bottom').innerHTML = document.getElementById('Build_Top').innerHTML;
				}
				else
				{
					document.getElementById('Build_Bottom').innerHTML = buildPriceShown;
				}
			}
		}
	}
	if (useCommission == 'true')
	{
		if (commissionRate <= 0)
		{
			commissionShown = textNone;
		}
		else
		{
			commissionTotal = Math.abs((total * commissionRate) / 100);
			commissionShown = symbol_left + custRound(commissionTotal, false) + symbol_right;
			createHiddenField('commissionTotal', commissionTotal);
			total += commissionTotal;
		}
		if (showCommission == 'true')
		{
			if (document.getElementById('Commission_Top'))
			{
				document.getElementById('Commission_Top').innerHTML = commissionShown;
			}
			if (document.getElementById('Commission_Bottom'))
			{
				if (document.getElementById('Commission_Top'))
				{
					document.getElementById('Commission_Bottom').innerHTML = document.getElementById('Commission_Top').innerHTML;
				}
				else
				{
					document.getElementById('Commission_Bottom').innerHTML = commissionShown;
				}
			}
		}
	}
	if (showTotal == 'true')
	{
		if (document.getElementById('Total_Top'))
		{
			document.getElementById('Total_Top').innerHTML = symbol_left + custRound(total, false) + symbol_right;
		}
		if (document.getElementById('Total_Bottom'))
		{
			if (document.getElementById('Total_Top'))
			{
				document.getElementById('Total_Bottom').innerHTML = document.getElementById('Total_Top').innerHTML;
			}
			else
			{
				document.getElementById('Total_Bottom').innerHTML = symbol_left + custRound(total, false) + symbol_right;
			}
		}
	}
	if (showCurrentProducts == 'true')
	{
		if (document.getElementById('sysdesc'))
		{
			document.getElementById('sysdesc').innerHTML = build_table();
		}
	}
}

function custRound(anynum, calcTax)
{
	anynum = eval(anynum * currency_value);
	decimal_places = decimal_places;
	switch (decimal_places)
	{
		case '1':
			decimal_places = 10;
			break;
		case '2':
			decimal_places = 100;
			break;
		case '3':
			decimal_places = 1000;
			break;
		case '4':
			decimal_places = 10000;
			break;
		case '5':
			decimal_places = 100000;
			break;
		default:
			decimal_places = 100;
			break;
	}
	if (priceWithTax == 'true' && calcTax == true)
	{
		taxTotal = Math.abs((anynum * tax_rate) / 100);
		if (showTaxes == 'true')
		{
			document.getElementById('Taxes_Top').innerHTML = Math.abs((Math.round(taxTotal * decimal_places) / decimal_places));
			document.getElementById('Taxes_Bottom').innerHTML = document.getElementById('Taxes_Top').innerHTML;
		}
		anynum += taxTotal;
	}
	workNum = Math.abs((Math.round(anynum * decimal_places) / decimal_places));
	workStr = "" + workNum;
	if (workStr.indexOf('.') == -1)
	{
		workStr += '.00';
	}
	dStr = workStr.substr(0, workStr.indexOf('.'));
	dNum = dStr - 0;
	pStr = workStr.substr(workStr.indexOf('.'));
	while (pStr.length < 3)
	{
		pStr += "0";
	}
	pStr = pStr.replace('.', decimal_point);
	if (dNum >= 1000)
	{
		dLen = dStr.length;
		dStr = parseInt("" + (dNum / 1000)) + thousands_point + dStr.substring(dLen - 3, dLen);
	}
	if (dNum >= 1000000)
	{
		dLen = dStr.length;
		dStr = parseInt("" + (dNum / 1000000)) + thousands_point + dStr.substring(dLen - 7, dLen);
	}
	retval = dStr + pStr;
	return retval;
}

function showimage_drop(box_name, initLoad)
{
	if (box_name)
	{
		var _box = document.getElementById(box_name);
		var selectedBox = _box.options[_box.options.selectedIndex];
		var selectedModel = selectedBox.getAttribute('value');
		var selectedPrice = selectedBox.getAttribute('price');
		if (selectedModel == '')
		{
			document.getElementById('link_' + box_name).innerHTML = '';
		}
		else
		{
			document.getElementById('link_' + box_name).innerHTML = '<label onmouseover="this.style.cursor=\'hand\'"; onclick="javascript:ccc_popup(\'ccc_more_info.php?path=' + selectedModel + '\')">'
																						  + textMoreInfo + '</label>';
		}
		if (showImages == 'true')
		{
			document.getElementById('image_' + box_name).innerHTML = buildImage(selectedBox.getAttribute('image'), '<br>');
		}
		if (showPriceDiffs == 'true')
		{
			for (g = 0; g < _box.length; g++)
			{
				workingBox = _box.options[g];
				workingBoxPrice = workingBox.getAttribute('price');
				if (workingBoxPrice > 0)
				{
					var productName = new String(workingBox.innerHTML);
					if (productName.indexOf('[+]') != -1)
					{
						productName = productName.substr(0, productName.indexOf('[+]'));
					}
					if (productName.indexOf('[-]') != -1)
					{
						productName = productName.substr(0, productName.indexOf('[-]'));
					}
					var productPriceDiff = (workingBoxPrice - selectedPrice);
					if (productPriceDiff == 0)
					{
						var PlusMinus = '';
						productPriceDiff = '';
					}
					else
					{
						if (productPriceDiff > 0)
						{
							var PlusMinus = '[+]';
						}
						else
						{
							var PlusMinus = '[-]';
						}
						productPriceDiff = symbol_left + custRound(Math.abs(productPriceDiff), true) + symbol_right;
					}
					workingBox.innerHTML = '';
					workingBox.innerHTML = productName + PlusMinus + ' ' + productPriceDiff;
				}
			}
		}
		if (!initLoad)
		{
			update_total();
		}
	}
}

function showimage_checkbox(box_name, initLoad)
{
	if (box_name)
	{
		if (showPriceDiffs == 'true')
		{
			spans = document.getElementsByName('price_' + box_name);
			if (spans.length <= 0)
			{
				spans = document.getElementsByName('price_' + box_name + '[]');
			}
		}
		buttons = document.getElementsByName(box_name + '[]');
		if (showPriceDiffs == 'true')
		{
			if (buttons[0].getAttribute('type') == 'radio')
			{
				var selectedPrice = 0;
				for (f = 0; f < buttons.length; f++)
				{
					if (buttons[f].checked)
					{
						selectedPrice = Math.abs(buttons[f].getAttribute('price'));
						break;
					}
				}
			}
		}
		var selectedImages = '';
		for (h = 0; h < buttons.length; h++)
		{
			if (showPriceDiffs == 'true')
			{
				var workingSpan = spans[h];
			}
			var workingButton = buttons[h];
			if (workingButton.checked)
			{
				if (showPriceDiffs == 'true')
				{
					workingSpan.innerHTML = '<b>' + textIncluded + '</b>';
				}
				if (showImages == 'true')
				{
					selectedImages += buildImage(workingButton.getAttribute('image'), '', workingButton.getAttribute('large_image'));
				}
			}
			else
			{
				if (showPriceDiffs == 'true')
				{
					var workingButtonPrice = Math.abs(workingButton.getAttribute('price'));
					if (workingButton.getAttribute('type') == 'checkbox')
					{
						plus_minus = '[+]';
					}
					else
					{
						workingButtonPrice = (workingButtonPrice - selectedPrice);
						if (workingButtonPrice < 0)
						{
							plus_minus = '[-]';
						}
						else
						{
							plus_minus = '[+]';
						}
					}
					if (useCommission == 'true')
					{
						workingButtonPrice += Math.abs((workingButtonPrice * commissionRate) / 100);
					}
					workingSpan.innerHTML = plus_minus + symbol_left + custRound(Math.abs(workingButtonPrice), true) + symbol_right;
				}
			}
		}
		if (showImages == 'true')
		{
			if (selectedImages != '')
			{
				document.getElementById('image_' + box_name).innerHTML = selectedImages;
			}
			else
			{
				document.getElementById('image_' + box_name).innerHTML = '';
			}
		}
		if (!initLoad)
		{
			update_total();
		}
		try {
			var node = document.getElementById('image_' + box_name).firstChild;
			if (node)
			{
				if (node.nodeName == 'A')
					tb_init(document.getElementById('image_' + box_name).firstChild);
			}
		} catch (err){};
	}
}
