
var _values;// = ["one","two","three","four","five","fireman","firstplace","first place","place", "oneton"];

var _lastValue='';
function replaceText(box) {
	var _count = 0; // number of different strings pattern was found in.
	var _index = -1; // index in the array of the item found.
	var _str = ""; // the new value in the text-box
	var _tempArray = new Array();
	if (box.value) _str = box.value;
	if (_lastValue == _str) return; // nothing need be done
	if (_lastValue.length>_str.length) {_lastValue=_str;return;} // deleted a char
	for (i in _values) {
	// is this new value in one of the items in the array?
	// (it must start at the begining of the item'string)
		if (_values[i].indexOf(_str) == 0) {
			_tempArray[_tempArray.length] = _values[i];
			_count++;
			_index = i;
		}
	}
	if (_index >= 0 && _index < _values.length && _count>0) {
	if (_count == 1) { //was 1
		_str = _values[_index];
	} else {
		_str = _values[_index].substring(0,lenToUse(_tempArray));
	}
	box.value=_str;
	_lastValue = _str;
	}
}
// this returns an integer that represents the length of the given strings in _index where all parameters are
// equal up to 1 -> first char's match only. 0 -> no match.

function lenToUse(_list) {
  var mismatchFound = false;
  var _return = 0;
  var _shortestLength = 99;
  for (i=0;i<_list.length;i++) {
    // find out the shortest string's length
	if (_list[i].length < _shortestLength)
	  _shortestLength = _list[i].length;
  }
  for (i=0;i<_shortestLength;i++) {
    // see if each charater in all _list's strings are equal intil their are
	// not - then that is the return value.
	_matchStr = _list[0].substring(i,i+1);
	for (j=1;j<_list.length;j++) {
	  if (_list[j].substring(i,i+1) != _matchStr) {
		if (!mismatchFound) // only track upto the first time a mismatch is found.
		  _return = i-1; // last round was succesful.
	    mismatchFound = true;
	  }
	}
    if (!mismatchFound) {
      _return = i;
    }
  }
  return (_return+1); // this makes the return value start at 1 for first position
}

//function compute(e) {
//  setTimeout('replaceText()',30); // 30 msecs later...
//}


function checkValidEmail(adr,dispWarning){
	
	var myAtSymbolAt = adr.indexOf('@');
	var myLastDotAt = adr.lastIndexOf('.');	
	var myLength = adr.length;
	
	if(myLength <1){
		if (dispWarning) alert("Email address is required!");
		return false;
	}

	if (myAtSymbolAt < 1 ){
		if (dispWarning) alert("Email address needs an '@' sign! Please correct this.");
		return false;
	}

	if (myLastDotAt < myAtSymbolAt){
		if (dispWarning) alert("Invalid email address! Please use format jane@doe.com.");
		return false;
	}

	if (myLength - myLastDotAt <= 2){
		if (dispWarning) alert("Invalid email address! Please use format jane@doe.com.");
		return false;
	}
	return true;
}

function numMask()
 {
	var sMask = "01234567890";
	var KeyTyped = String.fromCharCode(window.event.keyCode);
	var srcObject = window.event.srcElement;

	if (window.event.keyCode != 8)
	{
		if (sMask.indexOf(KeyTyped.toString()) == -1)
		{
		 		window.event.keyCode = 0;
				_ret = false;
		}
	}	 

   frigger = document.getElementById(window.event.srcElement.id);
 }

function moneyMask()
 {
	var sMask = "01234567890.$";
	var KeyTyped = String.fromCharCode(window.event.keyCode);
	var srcObject = window.event.srcElement;

	if (window.event.keyCode != 8)
	{
		if (sMask.indexOf(KeyTyped.toString()) == -1)
		{
		 		window.event.keyCode = 0;
				_ret = false;
		}
	}	 

   frigger = document.getElementById(window.event.srcElement.id);
 }


function phoneMask()
 {
	var sMask = "01234567890";
	var KeyTyped = String.fromCharCode(window.event.keyCode);
	var srcObject = window.event.srcElement;
	//alert(window.event.keyCode);
	if (window.event.keyCode != 8)
	{
	 if (sMask.indexOf(KeyTyped.toString()) == -1)
		{
		 		window.event.keyCode = 0;
				_ret = false;
		}
	}	 

   frigger = document.getElementById(window.event.srcElement.id);
   keyCount = frigger.value.length;
    var tmpStr = "(";
    
    keyEntered = KeyTyped;
    keyCount++;
    switch (keyCount)
    {
    case 2:
		tmpStr += srcObject.value;
		srcObject.value = tmpStr;
		break;
    case 5:
		srcObject.value += ")";
		break;
	case 9:
		srcObject.value += "-";
		break;
	}
 }
 	
function toggle_image_root(name, path)
{
	document[name].src = 'media/navigation/nav_' + path + '.gif';
}

function toggle_image(name, path)
{
	document[name].src = '../media/navigation/nav_' + path + '.gif';
}

function togglerandom_image(name, path)
{
	document[name].src = '../media/' + path + '.gif';
}

function formatMoney(value) 
{
	result = "$"+Math.floor(value)+".";
	var cents = 100*(value-Math.floor(value))+0.5;
	result += Math.floor(cents/10);
	result += Math.floor(cents%10);
	return result;
}

function AddOther0(SelectBox)
{
	var OtherText;
	OtherText = prompt("Describe the OTHER option.","")
	if ((OtherText != "") && (OtherText != null))
	{
		SelectBox.options[SelectBox.options.length]=new Option(OtherText, "0");
		SelectBox.selectedIndex = SelectBox.options.length-1;
	}
	return true;
}

function AddOther(SelectBox)
{
	var OtherText;
	OtherText = prompt("Describe the OTHER option.","")
	if ((OtherText != "") && (OtherText != null))
	{
		SelectBox.options[SelectBox.options.length]=new Option(OtherText, OtherText);
		SelectBox.selectedIndex = SelectBox.options.length-1;
	}
	return true;
}

function newScrollWindow(URL, width, height)
{
	window.open(URL, "Sub", "scrollbars=yes,width=" + width + ",height=" + height + ",menubar=no,toolbar=no,titlebar=no,status=no");
}


function newWindow(URL, width, height)
{
	window.open(URL, "Sub", "scrollbars=no,width=" + width + ",height=" + height + ",menubar=no,toolbar=no,titlebar=no,status=no");
	return true;
}

function newScrollWindow(URL, width, height)
{
	
	var d;
	d = new Date();
	window.open(URL, "Sub" + d.getSeconds(), "scrollbars=yes,width=" + width + ",height=" + height + ",menubar=no,toolbar=no,titlebar=no,status=no");
	return true;
}

function toggle_display(img, block)
{
	var imgSrc = String(img.src);
	if (imgSrc.indexOf("/media/icons/plus.gif") != -1)
	{
		block.style.display = "block";
		img.src = "/media/icons/minus.gif";
	}
	else
	{
		block.style.display = "none";
		img.src = "/media/icons/plus.gif";
	}
}
function addItemToCart(id) 
{
	window.location='/account/cart.asp?txtLastPage=/specials.asp&addspecial=' + id; 
	return(false);
}
function addPartCookie(PartID, bState)
{
	var sParts				= "";
	
	sParts					= getCookie("RFQ_Parts") + "";
	sParts					= sParts.replace("+", "");
	PartID					= PartID.replace("+", "");
	
	
	if(PartID.substring(PartID.length-1) == ":")
		PartID += "0";
		
	//alert(sParts + "<==>" + PartID);
	
	if (sParts == null)		sParts = "";
	
	if (bState)
	{
		sParts				= sParts.replace("," + PartID + ",", "");
		sParts				= sParts + "," + PartID + ",";
	}
	else
	{
		sParts				= sParts.replace("," + PartID + ",", "") + "";	
	}
	
	if (sParts == ",")		sParts = "";

	//Count Parts
	var aryParts;
	var rfq_count			= 0;
	
	sParts					= sParts.replace("null", "");
	
	aryParts				= sParts.split(",,");

	//alert(sParts);

	//document.all.rfqcart_count.innerHTML = aryParts.length;
	
	setCookie("RFQ_Parts", sParts + "");

}
function checkParts()
{
	var i;
	var sParts				= "";
	var tmpValue			= "";
	
	sParts					= getCookie("RFQ_Parts") + "";
	
	//alert(sParts);
	
	if (sParts == null)		sParts = "";
	
	for (i = 0; i < document.all.tags("input").length; i++)
	{
		obj = document.all.tags("input")[i];
		if (obj.type == "checkbox" && obj.name == "partnums")
		{
			tmpValue			= "";
			tmpValue			= obj.value;
			tmpValue			= tmpValue.substring(0, tmpValue.lastIndexOf(":"));
		
			//alert(sParts);
			//alert(tmpValue);
					
			if (sParts.indexOf("," + tmpValue + ",") != -1)
			{
				obj.checked	= true;
			}
		}
	}
}
function setCookie(name, value) 
{
	/*alert ('here1 ' + sParts);
	var myCookie = new Cookie();
	myCookie.Create("RFQ_Parts", sParts + "");
	myCookie.Save();
	//alert ('here2');*/
  var curCookie = name + "=" + escape(value) + "; path=/";

  //var curCookie = name + "=" + escape(value);
  document.cookie = curCookie;
}

function getCookie(name) 
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	
	if (begin == -1) 
	{
		begin = dc.indexOf(prefix);
	
		if (begin != 0) return null;
	} 
	else
		begin += 2;
		
	var end = document.cookie.indexOf(";", begin);
	
	if (end == -1)
		end = dc.length;
		
	return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name) 
{
	if (getCookie(name)) 
	{
		document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function fixDate(date) 
{
  var base = new Date(0);
  var skew = base.getTime();

  if (skew > 0)
    date.setTime(date.getTime() - skew);
}


function validateForm()
{
	
	var bOK			= true;
	var sMessage	= "All required field must be completed before continuing:\n";
	
	var aryElmTags	= new Array( "input", "select", "textarea" );
	
	for(var j=0; j<aryElmTags.length; j++)
	{
		var oElms		= document.getElementsByTagName( aryElmTags[j] );
		
		for(var i=0; i<oElms.length; i++)
		{
			var oElm	= oElms[i];
			
			//try
			//{
				//bOK = bOK && validateField(oElm);
				if(!validateField(oElm))
				{
					bOK = false;
				}
			//}
			//catch(e){}
		}
	}
	
	var oElm1 = document.getElementById("errorMsg");
	
	if(oElm1)
	{
		if(!bOK)
		{
			oElm1.style.display		= "";
		}
		else
		{
			oElm1.style.display		= "none";
		}
	}
	
	return bOK;
}
function validateField(oElm)
{
	var bOK			= true;

	if(oElm.getAttribute("required") == "true" && oElm.style.display != "none")
	{
		var oElm2	= document.getElementById( oElm.id + "_error" );
		
		if(oElm.value == "")
		{
			bOK	= false;
			oElm.className	= "error";
			
			if(oElm2)
			{
				oElm2.className	= "errorBar";
			}
		}
		else if(oElm.name.indexOf("email") >= 0 && (oElm.value.indexOf("@") == -1 || oElm.value.indexOf(".") == -1))
		{
			bOK	= false;
			oElm.className	= "error";
			
			if(oElm2)
			{
				oElm2.className	= "errorBar";
			}
		}
		else
		{
			oElm.className	= "";
			
			if(oElm2)
			{
				oElm2.className	= "Bar";
			}
		}
	}
	
	
	return bOK;
}