//-----------------
// Global Variables
//-----------------
var gsProductNo;   //Used to remember the Product Number for its Enlarged Picture to be displayed.

//-----------------
// Functions
//-----------------

/**
 * Add an item to the PayPal shopping cart.
 *
 * Any HTML page that calls this function must define a standard PayPal Add Item To Cart form.  By convention, this form should
 * be named "PayPalAddItem".
 *
 * Parameters:
 * sFormName_ 	- string - the name of the PayPal add item form defined within the calling HTML page.
 * sItemNumber_ - string - the SI part number to be added to the cart.  This number should have all punctuation removed from it.
 *                         For example, part number "240-351" should be passed as "240351".
 */
function paypalAddItemToCart(sFormName_, sItemNumber_)
{
	var oForm;
	var oItem;
	var sItemNo = "_" + sItemNumber_;
	
	// Make sure item is defined within the price table...
	if (typeof(gaPriceTable[sItemNo]) == "undefined")
	{
		alert("WebSite Programming Error - Item Not Found\n\n" + 
				"URL:\t\t" + document.URL + "\n" +
				"Item Number:\t" + sItemNumber_ + "\n\n" +
				"Please Contact Colorex Chemical!");
		return;
	}
	oItem = gaPriceTable[sItemNo];		// Reference the item being added to the cart

	// Make sure the PayPal form is defined
	oForm = document.getElementById(sFormName_);
	if (oForm == null)
	{
		alert("WebSite Programming Error - PayPal Form Error - Form Not Found\n\n" + 
				"URL:\t" + document.URL + "\n\n" +
				"Please Contact Colorex Chemical!");
		return;
	}

	// Make sure that PayPal form contains the necessary form variables...
	if ((typeof(oForm.cmd) == "undefined") ||
		(typeof(oForm.add) == "undefined") ||	
		(typeof(oForm.business) == "undefined") ||	
		(typeof(oForm.no_note) == "undefined") ||	
		(typeof(oForm.lc) == "undefined") ||	
		(typeof(oForm.currency_code) == "undefined") ||	
		(typeof(oForm.item_number) == "undefined") ||	
		(typeof(oForm.item_name) == "undefined") ||	
		(typeof(oForm.amount) == "undefined"))
	{
		alert("WebSite Programming Error - PayPal Form Error - Missing Fields\n\n" + 
				"URL:\t" + document.URL + "\n\n" +
				"Please Contact Scientific Instruments!");
		return;
	}		
	
	// Set the item to be added within the form, and submit the form...
	oForm.item_number.value = oItem[0];
	oForm.item_name.value = oItem[1];
	oForm.amount.value = oItem[2];
	oForm.submit();
}

/*
 * Creates the Print Preview Window.
 *
 * Parameters:
 * None
 */
function launchPrintPreviewWindow()
{
	var oNewWindow;	
	// Creates actual print preview window.
	oNewWindow = window.open("PrintPreviewTemplate.htm", "PrintPreview_Win", "width=800, height=600 scrollbars=1");
}

/*
 * Initializes the contents of the Print Preview Window.
 * !!!Pulls!!! contents onto the Print Preview Window.
 *
 * Parameters:
 * None
 */
function initPrintPreviewWindow()
{
	var oMainWindow;
	var oMainWindowDiv;
	var oPrintPreviewWindowDiv;
	var sContent;
	
	oMainWindow = window.opener;
	// Checks if the previous window that opened the Print Preview Window exists.
	// Decides what the contents should be and where to get them from.
	if (oMainWindow == null)
	{
		window.document.title = "Print Preview Window";
		sContent = '<br>...Main Document Content Displayed Here...<br>';
	}
	else
	{
		window.document.title = oMainWindow.document.title;
		oMainWindowDiv = oMainWindow.document.getElementById('contentDiv');
		if(oMainWindowDiv == null)
		{
			oMainWindowDiv = oMainWindow.document.getElementById('contentDivSpecial');
		}
		sContent = oMainWindowDiv.innerHTML;
	}
	
	// Puts the contents on the Print Preview Page
	oPrintPreviewWindowDiv = window.document.getElementById('contentDiv');
	oPrintPreviewWindowDiv.innerHTML = sContent;
	
	// Checks if the window that the Print Print Preview Window exists.
	// Decides whether to delete the links after the first horizontal line.
	if (oMainWindow != null)
	{
		oPrintPreviewWindowDiv = window.document.getElementById('navTrail');
		oPrintPreviewWindowDiv.innerHTML = '';
	}
	
	window.focus();
}

/*
 * Creates the EnlargedViewWindow.
 *
 * Parameters:
 * String sProductNumber - contains a string that is the product number of an SII product.
 */
function launchEnlargedViewWindow(sProductNumber)
{
	var oNewWindow;
	gsProductNo = '_' + sProductNumber;  // Puts the number in its correct form.
	
	// Make sure product is defined within the picture table...
	if (typeof(gaPictureTable[gsProductNo]) == "undefined")
	{
		alert("WebSite Programming Error - Picture Not Found\n\n" + 
				"URL:\t\t" + document.URL + "\n" +
				"Product Number:\t" + gsProductNo + "\n\n" +
				"Please Contact Colorex Chemical!");
		return;
	}

	// Creates actual Enlarged View Window.
	oNewWindow = window.open("EnlargedViewTemplate.htm", "EnlargedView", "width=800, height=600 scrollbars=0");
}

/*
 * Initializes the contents of the Enlarged View Window.
 * !!!Pulls!!! contents onto the Enlarged View Window.
 *
 * Parameters:
 * None
 */
function initEnlargedViewWindow()
{
	var oMainWindow;
	var oPrintPreviewWindowDiv;
	var oProduct;
	var sPictureSrc;
	var sContent;
	
	oMainWindow = window.opener;
	
	// Checks if the previous window that opened the Enlarged View Window exists.
	// Decides what the contents should be and where to get them from.
	if (oMainWindow == null)
	{
		window.document.title = "Enlarged View Window";
		sContent = '<br>...Enlarged Picture Displayed Here...<br>';
	}
	else
	{
		// Make sure item is defined within the picture table...
		if (typeof(gaPictureTable[oMainWindow.gsProductNo]) == "undefined")
		{
			alert("WebSite Programming Error - Picture Not Found\n\n" + 
					"URL:\t\t" + document.URL + "\n" +
					"Product Number:\t" + oMainWindow.gsProductNo + "\n\n" +
					"Please Contact Colorex Chemical!");
			return;
		}
		
		oProduct = gaPictureTable[oMainWindow.gsProductNo];  // Reference to the product being enlarged.
		window.document.title = oProduct[1];  // Sets the Title based on the product information.
		sPictureSrc = oProduct[0];  // Sets the source of the picture based on the product information.
		sContent = '<img src="' + sPictureSrc + '" border="0">'; //All images are made to 507-height
	}
	
	// Puts the contents on the Enlarged View Window Page
	oPrintPreviewWindowDiv = window.document.getElementById('contentDiv');
	oPrintPreviewWindowDiv.innerHTML = sContent;
	
	window.focus();
}

function enablePriceList(product, sTitle_)
{
	var sHtml;
	var sProdKey;
	var sPartNumber;
	var sQuantity;
	var sPrice;
	var oProduct;
	var iIndex = 1;

	if(getCookie("CCCCookie") == "ViewPriceList")
	{
		sHtml = '<span style="font-weight: bold;">' + sTitle_ + '</span>';
		sHtml += '<table class="pricetable" cellSpacing=0 cellPadding=2 >';
		sHtml += '	<tr>';
		sHtml += '		<TH class="pricetable" align="center" width="19%">Part<br>Number</td>';
		sHtml += '		<TH class="pricetable" align="center">Quantity</td>';
		sHtml += '		<TH class="pricetable" align="center" width="20%">Price</td>';
		sHtml += '	</tr>';
		sProdKey = '_' + product + '1';
		
		while(typeof(gaPriceTable[sProdKey]) != "undefined")
		{
			oProduct = gaPriceTable[sProdKey];
			sPartNumber = oProduct[0];
			sQuantity = oProduct[1];
			sPrice = oProduct[2];
			
			sHtml += '	<!-- ' + sProdKey + ' -->';
			sHtml += '	<tr valign="top">';
			sHtml += '		<td class="pricetable">' + sPartNumber + '</td>';
			sHtml += '		<td class="pricetable">' + sQuantity + '</td>';
			sHtml += '		<td class="pricetable" align="right">';
			sHtml += '			' + sPrice + '&nbsp;<br>';
			sHtml += '			<a href="javascript: void (0)" onclick="javascript:paypalAddItemToCart(\'PayPalAddItem\', \'' +sProdKey + '\');">';
			sHtml += '				<img src="images/add_to_cart.gif" border="0" alt="Add Item To Cart">';
			sHtml += '			</a>';
			sHtml += '		</td>';
			sHtml += '	</tr>';
			iIndex += 1;
			sProdKey = '_' + product + iIndex;
			
		}
		
		sHtml += '</table>';
		
		window.document.getElementById('PriceTable').innerHTML = sHtml;
	}
}

function setCookie (name, value, expires)
{
	document.cookie = name + "=" + escape(value) + ((expires) ? ("; expires=" + expires.toGMTString()) : "");
}

function getCookie(name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if(document.cookie.substring(i, j) == arg)
		{
			return getCookieVal(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if(i == 0)
		{
			break;
		}
	}
	return null;
}

function getCookieVal(offset)
{
	var endstr = document.cookie.indexOf(";", offset);
	if(endstr == -1)
	{
		endstr = document.cookie.length
	}
	return unescape(document.cookie.substring(offset, endstr));
}

function cookieScenario()
{
	var expires = new Date()
	expires.setTime(expires.getTime() + (100*365*24*60*60*1000))
	setCookie("CCCCookie", "ViewPriceList", expires);
	var myCookie = getCookie("CCCCookie");
	alert((myCookie) ? ("The cookie was set.\nYou can now view prices anywhere on\nwww.colorexchemical.com") : ("Website Programming Error - Cookie Not Set\n\n" +
	"URL:\t" + document.URL + "\n\n" +
	"Please Contact Colorex Chemical!")); 
}

