// Order Processing Javascript Code
// Copyright 2003-2009 Warburton Technology

// Supplier currency definitions
var SupplierCurrencyCode = new Array("0","1");
var SupplierCurrencyName = new Array("GBP","USD");
var SupplierCurrencyRate = new Array(1.00,1.3333);

// Customer currency definitions
var CustomerCurrencyCode = new Array("0","1");
var CustomerCurrencyName = new Array("GBP","EUR");
var CustomerCurrencyRate = new Array(1.00,1.42);
var CustomerCurrencyDescription = new Array("UK Pounds","EU Euros");
var CustomerCurrencySymbol = new Array("GBP","EUR");

var CurrentCustomerCurrency;
var CurrencyChangeEnabled;

// Initialise the currency
initialiseCurrency();

// Initialise currency
function initialiseCurrency()
{
	var CurrencyCookie;

	// Get the customer currency cookie
	CurrencyCookie = getCustomerCurrencyCookie();

	if (CurrencyCookie == null)
	{
		// Select the default currency
		CurrencyCookie = "GBP";

		// Set the customer currency cookie
		setCustomerCurrencyCookie(CurrencyCookie);
	}

	// Set the currency
	setCustomerCurrency(CurrencyCookie);

	var CurrencyChangeCookie;

	// Get the currency change cookie
	CurrencyChangeCookie = getCurrencyChangeCookie();

	if (CurrencyChangeCookie == null)
	{
		// Select the default currency change state
		CurrencyChangeCookie = "true";

		// Set the currency change cookie
		setCurrencyChangeCookie(CurrencyChangeCookie);
	}

	// Set the currency change enabled flag
	CurrencyChangeEnabled = ((CurrencyChangeCookie == "true")?true:false);
}

// Set the current customer currency
function setCustomerCurrency(theCurrencyName)
{
	// Lookup the customer currency name
	var CurrencyIndex = lookupCustomerCurrencyName(theCurrencyName);

	// Save the current customer currency
	window.CurrentCustomerCurrency = CurrencyIndex;
}

// Change the current currency
function changeCurrency(theCurrencyName)
{
	var CurrencyCookie;

	CurrencyCookie = theCurrencyName;

	// Set the customer currency cookie
	setCustomerCurrencyCookie(CurrencyCookie);

	// Get the customer currency cookie
	CurrencyCookie = getCustomerCurrencyCookie();

	if (CurrencyCookie != theCurrencyName)
	{
		// Inform the user that they are not able to change currency with cookies disabled
		alert("You must enable cookies in your web browser in order to change the currency.");

		// Select the default currency
		CurrencyCookie = "EUR";
	}

	// Set the customer currency
	setCustomerCurrency(CurrencyCookie);
}

// Lookup the customer currency name
function lookupCustomerCurrencyName(theCurrencyName)
{
	var CurrencyIndex = -1;

	for (var index = 0; index < CustomerCurrencyName.length; index ++)
	{
		if (CustomerCurrencyName[index] == theCurrencyName)
			CurrencyIndex = index;
	}

	return CurrencyIndex;
}

// Set the customer currency cookie
function setCustomerCurrencyCookie(theCurrency)
{
	// Set the currency cookie
	setCookie("sslcurrency", theCurrency);
}

// Get the customer currency cookie
function getCustomerCurrencyCookie()
{
	// Get the currency cookie
	return getCookie("sslcurrency");
}

// Set the currency change cookie
function setCurrencyChangeCookie(theCurrencyChange)
{
	// Set the currency change cookie
	setCookie("currencychange", theCurrencyChange);
}

// Get the currency change cookie
function getCurrencyChangeCookie()
{
	// Get the currency change cookie
	return getCookie("currencychange");
}

// Disable currency changes
function disableCurrencyChanges()
{
	var CurrencyChangeCookie;

	CurrencyChangeCookie = "false";

	// Set the currency change cookie
	setCurrencyChangeCookie(CurrencyChangeCookie);

	// Save the currency change enabled flag
	window.CurrencyChangeEnabled = ((CurrencyChangeCookie == "true")?true:false);
}

function calculateRecommendedRetail(theProductPrice,theProductCurrency)
{
	var	RetailAdjustment = 0;

	// Lookup the supplier currency name
	var CurrencyIndex = lookupSupplierCurrencyName(theProductCurrency);

	RetailAdjustment = SupplierCurrencyRate[CurrencyIndex];
	
	// Calculate the retail adjusted price
	var Result = Math.round((theProductPrice / RetailAdjustment) * 100) / 100;

	return Result;
}

// Lookup the supplier currency name
function lookupSupplierCurrencyName(theCurrencyName)
{
	var CurrencyIndex = -1;

	for (var index = 0; index < SupplierCurrencyName.length; index ++)
	{
		if (SupplierCurrencyName[index] == theCurrencyName)
			CurrencyIndex = index;
	}

	return CurrencyIndex;
}

function convertCurrency(theValue,theRate)
{
	// Calulate the currency converted price
	var Result = Math.round((theValue * theRate) * 100) / 100;

	return Result;
}

function calculateDiscount(theValue,theRate)
{
	// Calculate the discount converted price
	var Result = Math.round(theValue * (100 - theRate)) / 100;

	return Result;
}

function getDecimalString(theValue)
{
	var ValueString;
	var ResultString;

	// Convert to string
	ValueString = "" + theValue;

	var index = ValueString.indexOf('.');

	if (index < 0)
	{
		ResultString = ValueString + ".00";
	}
	else
	{
		ResultString = ValueString.substring(0, index + 1);
		ResultString += ValueString.substring(index + 1, index + 3);

		if (index + 2 == ValueString.length)
			ResultString += "0";
	}

	return ResultString;
}

// Displays the product order panel table
function displayOrderPanel(ProductName,ProductPrice,ProductCurrency,ShowName,ExpandPanel)
{
	var UserID = "6631399";
	var FontSize = "10pt";

	if (!ShowName)
	{
		ExpandPanel = false;
		FontSize = "12pt";
	}

	var PanelWidth;

	if (ExpandPanel)
		PanelWidth = "100%";
	else
		PanelWidth = "1";

	document.write("<table border=\"0\" cellspacing=\"2\" cellpadding=\"1\" width=\"" + PanelWidth + "\" style=\"border-collapse: collapse; font-size: " + FontSize + "\">");
	document.write("<tr>");

	if (ShowName)
	{
		document.write("<td nowrap align=\"left\" style=\"font-weight: bold\">");
		document.write(ProductName);
		document.write("</td>");
	}

	// Calculate the retail adjusted price
	ProductPrice = calculateRecommendedRetail(ProductPrice, ProductCurrency);

	var ProductDiscount = 0;

	// Adjust the discount for specific products
	switch (ProductName)
	{
		case "MEM Key":								ProductDiscount = 10;	break;
		case "Pocket Watch B":						ProductDiscount = 10;	break;
		case "Serial Interface Board":				ProductDiscount = 30;	break;
		case "Motor Mind B":						ProductDiscount = 30;	break;

		case "CCS ICD-S40 Kit":						ProductDiscount = 20;	break;
	}

	// Apply discount to all PicBasic products
	if (ProductName.indexOf("PicBasic") != -1)
	{
		ProductDiscount = 10;
	}

	// Apply discount to all CCS C products
	if (ProductName.indexOf("CCS C") != -1)
	{
		ProductDiscount = 10;
	}

	// Apply discount to all LAB-X products
	if (ProductName.indexOf("LAB-X") != -1)
	{
		ProductDiscount = 10;
	}

	// Apply discount to all BASIC Stamp products
	if (ProductName.indexOf("BASIC Stamp") != -1)
	{
		ProductDiscount = 10;
	}

	if (ProductDiscount > 0)
	{
		// Display the product price
		document.write("<td nowrap width=\"1\" style=\"font-weight: bold\">");
		document.write("<strike>" + CustomerCurrencySymbol[CurrentCustomerCurrency] + getDecimalString(ProductPrice) + "</strike>");
		document.write("</td>");

//		// Display the discount percentage
//		document.write("<td align=\"center\" nowrap width=\"1\" style=\"font-size: 10px; color: #FF0000; line-height:100%; margin-top:-8; margin-bottom:-8\">");
//		document.write(ProductDiscount + "%<br>OFF");
//		document.write("</td>");
	}

	// Calculate the discounted product price
	ProductPrice = calculateDiscount(ProductPrice,ProductDiscount); 

	// Display the product price
	document.write("<td nowrap align=\"right\" width=\"1\" style=\"font-weight: bold; color: #CC0000\">");
	document.write(CustomerCurrencySymbol[CurrentCustomerCurrency] + getDecimalString(ProductPrice));
	document.write("</td>");

	// Display the order online button
	document.write("<td nowrap align=\"right\" width=\"1\">");
	document.write("<a class=\"button_medium\" href=\"http://ww4.aitsafe.com/cf/add.cfm?userid=" + UserID + "&product=" + ProductName + "&price=" + getDecimalString(ProductPrice) + "&qty=1&currency=" + CustomerCurrencyCode[CurrentCustomerCurrency] + "&return=" + document.URL.split("//")[1] + "\">Buy Now</a>");
	document.write("</td>");

	document.write("</tr>");
	document.write("</table>");
}

function getSelectBoxSelection(theSelectBox)
{
	var CurrenctSelection = "";

	for (index = 0; index < theSelectBox.length; index ++)
	{
		if (theSelectBox.options[index].selected)
			CurrenctSelection = theSelectBox.options[index].value;
	}

	return CurrenctSelection;
}

function getCookie(theCookieKey)
{

	var cookieValue = null;

	var startIndex = -1;
	var endIndex = -1;

	// Search for start of the cookie key
	var keyIndex = document.cookie.indexOf(theCookieKey + "=");

	if (keyIndex != -1)
	{
		// Search for the start of the cookie value
		startIndex = document.cookie.indexOf("=", keyIndex) + 1;

		// Search for the end of the cookie value
		endIndex = document.cookie.indexOf(";", startIndex);

		if (endIndex == -1)
		{
			endIndex = document.cookie.length;
		}

		// Extract the cookie value from the cookie
		cookieValue = unescape(document.cookie.substring(startIndex, endIndex));
	}

	return cookieValue;
}

function setCookie(theCookieKey, theCookieValue)
{

	if (theCookieValue != null && theCookieValue != "")
	{
		var cookieString;

		// Build the cookie string
		cookieString = theCookieKey + "=" + escape(theCookieValue) + "; path=/;";

		// Set the cookie
		document.cookie = cookieString;
	}
}
