/*
 *
 * (c) Copyright 2007 Hen's Teeth Network, Inc. All rights reserved.
 * Redistribution without prior written permission is strictly prohibited.
 * Contact Art Zemon www.hens-teeth.net
 *
 */

var lastSearchString = "";

function clearColor()
{
	var textColor = document.getElementById('textColor');
    var colorSelectionDIV = document.getElementById('colorSelectionDIV');
	var addButton = document.getElementById('add');
	var hiddenSKU = document.getElementById('key');
	
	hiddenSKU.value = "GELSHEET";
    colorSelectionDIV.innerHTML = '';
	textColor.value = "";
	lastSearchString = "";
	addButton.disabled = true;
	return true;
}

function checkQuantity()
{
	var addButton = document.getElementById('add');
	var strQty = trim(document.getElementById('qty').value);
	
	if ('0' == strQty || '' == strQty)
	{	
		addButton.disabled = true;
	}
	else
	{
		var textColor = document.getElementById('textColor').value;
		if ( 0 <= textColor.search(/-/))
		{
			addButton.disabled = false;
		}
		else
		{
			addButton.disabled = true;
		}
	}
}

function setOpt0()
{
	var opt0Value = document.getElementById('opt0');
	var selectVendor = document.getElementById('selectVendor').value;
	
	switch (trim(selectVendor))
	{
		case 'Gam':
			opt0Value.value = 4;
			break;
		case 'Lee':
			opt0Value.value = 5;
			break;
		case 'Rosco_E_Colour':
			opt0Value.value = 1;
			break;
		case 'Rosco_Cinegel':
			opt0Value.value = 2;
			break;
		case 'Rosco_Supergel':
			opt0Value.value = 3;
			break;
		default:
			opt0Value.value = 0;
			break;
	}
}

function selectItem(id, name, series, color, vendor, price)
{
	var textColor = document.getElementById('textColor');
	var custTextField = document.getElementById('custtext0');
	var justPrice = document.getElementById('price');
	var addButton = document.getElementById('add');
	var hiddenSKU = document.getElementById('key');

    textColor.value = vendor + ' ' + id + ' ' +  name + '-' +  series;
    lastSearchString = textColor.value;
	custTextField.value = textColor.value;

	justPrice.value 	= price;
	hiddenSKU.value = "GELSHEET";
	
	/* Change SKU based on selection */
	switch (trim(vendor))
	{
		case 'Gam':
			hiddenSKU.value = hiddenSKU.value + "-GAM" + id;
			break;
			
		case 'Lee':
			hiddenSKU.value = hiddenSKU.value + "-LEE" + id;		
			break;
			
		case 'Rosco_E_Colour':
			hiddenSKU.value = hiddenSKU.value + "-ECO" + id;		
			break;

		case 'Rosco_Cinegel':
			hiddenSKU.value = hiddenSKU.value + "-RCG" + id;		
			break;

		case 'Rosco_Supergel':
			hiddenSKU.value = hiddenSKU.value + "-RSG" + id;		
			break;
			
		case 'Rosco':
			hiddenSKU.value = hiddenSKU.value + "-ROS" + id;		
			break;
	}
	
	
    var colorSelectionDIV = document.getElementById('colorSelectionDIV');
    colorSelectionDIV.innerHTML = '<table width="100" height="100" border="1"><tr><td bgcolor="' +  color + '">&nbsp;</td></tr></table>';
	
	addButton.disabled = false;
	checkQuantity();
}

function successHandler(o)
{	
	if(o.responseXML !== undefined)
	{
		var i;
		var colorSelectionDIV = document.getElementById('colorSelectionDIV');
		var products = o.responseXML.getElementsByTagName('product');
		
	
		if (0 == products.length)
		{
			colorSelectionDIV.innerHTML = "No results found. Please try again.";
			return;
		}

		var strDisplay = "<table width='100%' border='0' cellpadding='2' cellspacing='0'>";

		for (i = 0; i < products.length; i++)
		{
			var id 			= products[i].getElementsByTagName('id')[0].childNodes[0].nodeValue;
			var name 		= products[i].getElementsByTagName('name')[0].childNodes[0].nodeValue;
			var rgbcolor 	= products[i].getElementsByTagName('rgbcolor')[0].childNodes[0].nodeValue;
			var series	 	= products[i].getElementsByTagName('series')[0].childNodes[0].nodeValue;
			var	trans 		= products[i].getElementsByTagName('trans')[0].childNodes[0].nodeValue;
			if ("N" == trans) { trans = ""; }
			var	note 		= products[i].getElementsByTagName('note')[0].childNodes[0].nodeValue;
			if ("N" == note) { note = ""; }
			var vendor		= products[i].getElementsByTagName('vendor')[0].childNodes[0].nodeValue;
			var price		= products[i].getElementsByTagName('price')[0].childNodes[0].nodeValue;
			
			var selectParams = "selectItem('" + id + "','" + name + "','" + series + "','" + rgbcolor + "','" + vendor + "','" + price + "');";
			
			strDisplay += '<tr valign="top" onmouseover="this.bgColor=\'#CCCCCC\';" onMouseOut="this.bgColor=\'#FFFFFF\';"> \
							<td > \
								<table class="listRow" border="0" cellspacing="2" cellpadding="2" onclick="' + selectParams + '"> \
									<tr> \
										<td width="50px" class="list">' + id + '</td> \
										<td width="50px" rowspan="3" class="list" bgcolor="' + rgbcolor + '">&nbsp;</td> \
										<td class="list"><strong>' + name + '</strong></td> \
										<td class="list">' + series + '</td> \
									</tr> \
									<tr> \
										<td class="list">&nbsp;</td> \
										<td class="list" colspan="2">Transmission: ' + trans + '</td> \
									</tr> \
									<tr> \
										<td class="list">&nbsp;</td> \
										<td class="list" colspan="2">' + note + '</td> \
									</tr> \
								</table> \
							</td> \
						 </tr>';
		}
		strDisplay += '</table>';
		
		colorSelectionDIV.innerHTML = strDisplay;
	}


}

function failureHandler(o){
	var colorSelectionDIV = document.getElementById('colorSelectionDIV');
	colorSelectionDIV.innerHTML =  o.status + " " + o.statusText;
}

function lookupColor()
 {
 	var selectVendor = document.getElementById('selectVendor').value;
	var textColor = document.getElementById('textColor').value;
	var colorSelectionDIV = document.getElementById('colorSelectionDIV');
	var addButton = document.getElementById('add');
	
	addButton.disabled = true;
	
	// Only search on 1 character if it is a number
	if (1 == textColor.length && -1 == textColor.search(/^[0-9]$/))
	{
		// Return if the 1 character is a letter
		return;
	}

	colorSelectionDIV.innerHTML = "Searching...";
	// alert(selectVendor + " " + textColor);
	var entryPoint = '/HTN/lib/gelColor.php';
	var queryString = encodeURI('?v=' + selectVendor + '&q=' + textColor + '&t=1');
	var sUrl = entryPoint + queryString;
	// alert(sUrl);

	if ("" == textColor)
	{
		colorSelectionDIV.innerHTML = "";
	}
	else
	{
		var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, { success:successHandler, failure:failureHandler });
	}

}


function myColorSelectInterval()
{
	var timeInterval = 1000;
	var textColor = document.getElementById('textColor').value;
	var colorSelectionDIV = document.getElementById('colorSelectionDIV');
	
	if (textColor.length < 1)
	{	
	    colorSelectionDIV.innerHTML = '';
		return;
	}
	
	if (-1 != textColor.search('-')) // then it is a result string
	{
	    // Since we're displaying the color swatch 
		// We don't need to clear the recion
		// colorSelectionDIV.innerHTML = '';
		return;
	}
	
	if (lastSearchString != textColor)
	{
		lastSearchString = textColor;
		lookupColor();
		return;
	}	
}

function validateForm()
{
	var textColor = document.getElementById('textColor').value;

	if ('' == trim(textColor) || -1 == textColor.search('-'))
	{
	    colorSelectionDIV.innerHTML = '';
		return false;
	}
	else
	{
		return true;
	}
}


function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}