var is_gecko = /gecko/i.test(navigator.userAgent);
var is_ie    = /MSIE/.test(navigator.userAgent);

function currencyFormat(fld, milSep, decSep, e)
{
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;

	// Check codes to see if a special key has been pressed
	if(checkCodes(e))
	{
		return true;
	}
	
	var position = doGetCaretPosition(fld);
	
	// Get rid of any selected text
	fld = removeSelection(fld);

	// Get key value from key code
	key = String.fromCharCode(whichCode);  

	// IF VALUE IS NOT A NUMARIC, THEN RETURN FALSE
	if (strCheck.indexOf(key) == -1)
		return false;

	//ALLOCATE LENTH OF FIELD TO LEN
	len = fld.value.length;

	//FOR EACH CHARACTER IN LEN DO THE FOLLOWING
	for(i = 0; i < len; i++)
	{
		//IF CURRENT CARACTER IS NOT 0, THEN BREAK.
		if (fld.value.charAt(i) != '0' && fld.value.charAt(i) != '$')
			break;
	}
		
	//MAKE AUX NULL
	aux = '';
	
	//LENGTH VARIABLE CONTRINUES FROM PREVIOUS
	for(; i < len; i++)
	{
		if(i == position)
		{
			aux += key;
		}
		
		//ONLY IF ITS A VALID CHARACTER THEN, ADD TO AUX
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) 
		{	
			aux += fld.value.charAt(i);
		}
	}
	
	if(position >= len)
	{
		//ADD THE KEY JUST PRESSED TO THE VALID STRING
		aux += key;
	}
	
	//LEN IS THE LENTH OF THE VALID STRING
	len = aux.length;
	
	//IF LENTH = 0 THEN FIELD VALUS IS NOTIHNG
	if (len == 0) fld.value = '';

	//IF LENTH IS GREATER THEN 0, DO THE FOLLOWING
	if (len > 0) 
	{
		//INITIIZE AUX 2 - WILL BE USED AS A STRING BUILDER
		aux2 = '';
	
		//
		for (j = 0, i = len - 1; i >= 0;   i--) 
		{
			if (j == 3) 
			{
				aux2 += milSep;
				j = 0;
			}
		
			aux2 += aux.charAt(i);
			j++;
		}
	
		//INITIZE THE TEXT BOX
		fld.value = '$';
	
		//PLACE THE LENTH OF THE NEW STRING INTO LEN2
		len2 = aux2.length;
	
	
		for (i = len2 - 1; i >= 0; i--)
		{
			fld.value += aux2.charAt(i);
		}
	}
	
	doSetCaretPosition(fld, fld.value.length);
	
	return false;
}

function currencyDoubleFormat(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;

	//Check codes to see if a special key has been pressed	
	if(checkCodes(e))
		return true;
		
	var position = doGetCaretPosition(fld);
	
	// Get rid of any selected text
	fld = removeSelection(fld);

	// Get key value from key code
	key = String.fromCharCode(whichCode);  

	// IF VALUE IS NOT A NUMARIC, THEN RETURN FALSE
	if (strCheck.indexOf(key) == -1) return false;  

	//ALLOCATE LENTH OF FIELD TO LEN
	len = fld.value.length;

	//FOR EACH CHARACTER IN LEN DO THE FOLLOWING
	for(i = 0; i < len; i++)
		//IF CURRENT CARACTER IS NOT 0, THEN BREAK.
		if (fld.value.charAt(i) != '0' && fld.value.charAt(i) != '$') break; 
			//MAKE AUX NULL
			aux = '';	
			
			
	//LENGTH VARIABLE CONTRINUES FROM PREVIOUS
	for(; i < len; i++)
	{
		if(i == position)
		{
			aux += key;
		}
		
		//ONLY IF ITS A VALID CHARACTER THEN, ADD TO AUX
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) 
			aux += fld.value.charAt(i);
	}
	
	if(position >= len)
	{
		//ADD THE KEY JUST PRESSED TO THE VALID STRING
		aux += key;
	}
	
	//LEN IS THE LENTH OF THE VALID STRING
	len = aux.length;
	
	//IF LENTH = 0 THEN FIELD VALUS IS NOTIHNG
	if (len == 0) fld.value = '';
	
	if (len == 1) fld.value = '$' + '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '$' + '0'+ decSep + aux;

	//IF LENTH IS GREATER THEN 0, DO THE FOLLOWING
	if (len > 2) 
	{
		//INITIIZE AUX 2 - WILL BE USED AS A STRING BUILDER
		aux2 = '';
	
		//
		for (j = 0, i = len - 3; i >= 0;   i--) 
		{
			aux2 += aux.charAt(i);
		}
	
		//INITIZE THE TEXT BOX
		fld.value = '$';
	
		//PLACE THE LENTH OF THE NEW STRING INTO LEN2
		len2 = aux2.length;
	
	
		for (i = len2 - 1; i >= 0; i--)
			fld.value += aux2.charAt(i);
			
		fld.value += decSep + aux.substr(len - 2, len);
	}
	
	doSetCaretPosition(fld, fld.value.length);
	
	return false;
}


function ConvertIntToNumberField(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;

	//Check codes to see if a special key has been pressed	
	if(checkCodes(e))
		return true;
		
	var position = doGetCaretPosition(fld);
	
	// Get rid of any selected text
	fld = removeSelection(fld);

	// Get key value from key code
	key = String.fromCharCode(whichCode);  

	// IF VALUE IS NOT A NUMARIC, THEN RETURN FALSE
	if (strCheck.indexOf(key) == -1) return false;  

	//ALLOCATE LENTH OF FIELD TO LEN
	len = fld.value.length;

	//FOR EACH CHARACTER IN LEN DO THE FOLLOWING
	for(i = 0; i < len; i++)
		//IF CURRENT CARACTER IS NOT 0, THEN BREAK.
		if (fld.value.charAt(i) != '0' && fld.value.charAt(i) != '$') break; 
			//MAKE AUX NULL
			aux = '';	
			
			
	//LENGTH VARIABLE CONTRINUES FROM PREVIOUS
	for(; i < len; i++)
	{
		if(i == position)
		{
			aux += key;
		}
		
		//ONLY IF ITS A VALID CHARACTER THEN, ADD TO AUX
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) 
			aux += fld.value.charAt(i);
	}
	
	if(position >= len)
	{
		//ADD THE KEY JUST PRESSED TO THE VALID STRING
		aux += key;
	}
	
	//LEN IS THE LENTH OF THE VALID STRING
	len = aux.length;
	
	//IF LENTH = 0 THEN FIELD VALUS IS NOTIHNG
	if (len == 0) fld.value = '';
	
	//IF LENTH IS GREATER THEN 0, DO THE FOLLOWING
	if (len > 0) 
	{
		//INITIIZE AUX 2 - WILL BE USED AS A STRING BUILDER
		aux2 = '';
	
		//
		for (j = 0, i = len - 1; i >= 0;   i--) 
		{
			if (j == 3) 
			{
				aux2 += milSep;
				j = 0;
			}
		
			aux2 += aux.charAt(i);
			j++;
		}
	
		//INITIZE THE TEXT BOX
		fld.value = '';
	
		//PLACE THE LENTH OF THE NEW STRING INTO LEN2
		len2 = aux2.length;
		
		var new_position = 0;
		var count = 0;
		
		for (i = len2 - 1; i >= 0; i--)
		{
			fld.value += aux2.charAt(i);
		}
	}
	
	doSetCaretPosition(fld, fld.value.length);
	
	return false;
}




function checkCodes(e)
{
	// RETURN TRUE IF CTRL + A PRESSED, CTRL + C, CTRL + V, CTRL + X, OR CTRL + Z IS PRESSED
	// 97 is a, 99 is c, 118 is v, 120 is x
	
	if(e.ctrlKey)
	{
		if(e.which == 97 || e.which == 99 || e.which == 118 || e.which == 120 || e.which == 122)
			return true;
	}

	// RETURN TRUE IF THE USER PRESSED BACKSPACE, DELETE, TAB, OR ENTER, LEFT ARROW, OR RIGHT ARROW
	// 8 is backspace, 46 is delete, 9 is tab, 13 is enter, 37 is left arrow, 39 is right arrow
	if(e.keyCode == 8 || e.keyCode == 46 || e.keyCode == 9 || e.keyCode == 13 || e.keyCode == 37 || 
		e.keyCode == 39)
	{
		return true;
	}
}

//From http://www.webmasterworld.com/forum91/5005.htm
function removeSelection(fld)
{
	var begin, end, part1, part2;
	var end;
	
	begin = getSelectionStart(fld);
	end = getSelectionEnd(fld);
	
	if(end != 0)
	{
		part1 = fld.value.substr(0, begin);
		part2 = fld.value.substr(end);
	
		fld.value = part1 + '' + part2;
	}
	
	return fld;
}

function getSelectionStart(input)
{
	var selection;
	
	if (is_gecko)
		return input.selectionStart;
	else
	{
		if (window.getSelection)
		{ 
			selection = window.getSelection(); 
		}
		else if (document.getSelection)
		{ 
			selection = document.getSelection(); 
		}
		else if (document.selection)
		{ 
			selection = document.selection.createRange().text; 
		} 
		
		return startPos = input.value.indexOf(selection);
	}
}

function getSelectionEnd(input)
{
	var selectin;
	
	if (is_gecko)
		return input.selectionEnd;
	else
	{
		if (window.getSelection)
		{ 
			selection = window.getSelection(); 
		}
		else if (document.getSelection)
		{ 
			selection = document.getSelection(); 
		}
		else if (document.selection)
		{ 
			selection = document.selection.createRange().text; 
		} 
		
		var startPos = input.value.indexOf(selection); 
		
		return endPos = input.value.indexOf(selection) + selection.length;
	}
}

function doGetCaretPosition (oField)
{
	// Initialize
	var iCaretPos = 0;

	// IE Support
	if (document.selection)
	{
		// Set focus on the element
		oField.focus ();
  
		// To get cursor position, get empty selection range
		var oSel = document.selection.createRange ();
  
		// Move selection start to 0 position
		oSel.moveStart ('character', -oField.value.length);
  
		// The caret position is selection length
		iCaretPos = oSel.text.length;
	}

	// Firefox support
	else if (oField.selectionStart || oField.selectionStart == '0')
		iCaretPos = oField.selectionStart;

	// Return results
	return (iCaretPos);
}
   
function doSetCaretPosition (oField, iCaretPos)
{
	// IE Support
	if (document.selection)
	{

		// Set focus on the element
		oField.focus ();
  
		// Create empty selection range
		var oSel = document.selection.createRange ();
  
		// Move selection start and end to 0 position
		oSel.moveStart ('character', -oField.value.length);
  
		// Move selection start and end to desired position
		oSel.moveStart ('character', iCaretPos);
		oSel.moveEnd ('character', 0);
		oSel.select ();
	}

	// Firefox support
	else if (oField.selectionStart || oField.selectionStart == '0')
	{
		oField.selectionStart = iCaretPos;
		oField.selectionEnd = iCaretPos;
		oField.focus ();
	}
}

function ClrField(field)
{
	if(field.value == "Enter email address")
	{
		field.value='';
	}
	
	if(field.value == "Enter zip code")
	{
		field.value='';
	}
}

//DEFAULT PHONE LENGTH FIELD
var phone_field_length=0;

function TabNext(obj,event,len,next_field) 
{
	//IF THE EVENT WAS DOWN
	if (event == "down") 
	{
		//phone_field_length=obj.value.length;
	}
	else if (event == "up") 
	{
		//IF THE EVEN IS UP
		if (obj.value.length != phone_field_length) 
		{
			//PUT LENGTH OF OBJECT INTO PHONE LENGTH FIELD
			phone_field_length=obj.value.length;
			
			//USE LENGTH PASSED BY USER TO DETERMINE IF MATCHES.
			if (phone_field_length == len) 
			{
				//alert('tet this');
				//IF YES, THEN PASS THE FIELD TO THE NEXT TEXT BOX
				next_field.focus();
			}
		}
	}
}

function fnTrapKD(btn, event)
{
   if (navigator.appName.indexOf ("Microsoft") !=-1) 
   { 
	    if (document.all)
	    {
		    if (event.keyCode == 13)
		    {
			    event.returnValue=false;
			    event.cancel = true;
			    btn.click();
		    }
	    }
	    else if (document.getElementById)
	    {
		    if (event.which == 13)
		    {
			    event.returnValue=false;
			    event.cancel = true;
			    btn.click();
		    }
	    }
	    else if(document.layers)
	    {
		    if(event.which == 13)
		    {
			    event.returnValue=false;
			    event.cancel = true;
			    btn.click();
		    }
	    }
   }
    else 
    { 
      if (event.keyCode == 13) 
      { 
          var newEvent = document.createEvent("KeyEvents"); 
          newEvent.initKeyEvent("keypress", true, true,document.defaultView, event.ctrlKey, event.altKey, event.shiftKey, event.metaKey, 0, btn.click()); 
          event.preventDefault(); 
          event.target.dispatchEvent(newEvent); 
      } 
   } 
}


menu_status = new Array(); 



function SessionTimeoutWarning(blRgstrd)
{
//	var intInrvl = ((1000*60)*20);
	var intInrvl = ((1000*60)*30);
	
	//setTimeOut(displayTimeoutAlert(blRgstrd), 20000);
	window.setTimeout('displayTimeoutAlert('+blRgstrd+')', intInrvl);
}

function displayTimeoutAlert(rgstrd)
{
	//OBJECT WHICH HOLDS THE MESSAGE BASED ON PARAMETER 
	var strMsg = '';
	
	//PUT MESSAGE IN OBJECT BASED ON IF USER IS REGISED OR NOT REGISTERED BY HAS ITEM IN SESSION
	if(rgstrd)
		strMsg = 'For the safety of your account, your session will expire in 10 minutes due to inactivity.  Unless you reset the session time, you will be logged out of the system.  Would you like to reset your session?';
	else
		strMsg = 'Your session will expire in 10 minutes due to inactivity.  Unless you reset the session time, all items in your shopping cart and wish list will be disposed.  Would you like to reset your session?';
	
	//IF USERS DECIDES TO REFRESH SESSION, THEN PAGE IS POSTED BACK
	if(confirm(strMsg)==true)
		__doPostBack('','');		
}


function ClrField(field)
{
	if(field.value == "Enter email address")
	{
		field.value='';
	}
	
	if(field.value == "Enter zip code")
	{
		field.value='';
	}
}

//DEFAULT PHONE LENGTH FIELD
var phone_field_length=0;

function TabNext(obj,event,len,next_field) 
{
	//IF THE EVENT WAS DOWN
	if (event == "down") 
	{
		//phone_field_length=obj.value.length;
	}
	else if (event == "up") 
	{
		//IF THE EVEN IS UP
		if (obj.value.length != phone_field_length) 
		{
			//PUT LENGTH OF OBJECT INTO PHONE LENGTH FIELD
			phone_field_length=obj.value.length;
			
			//USE LENGTH PASSED BY USER TO DETERMINE IF MATCHES.
			if (phone_field_length == len) 
			{
				//alert('tet this');
				//IF YES, THEN PASS THE FIELD TO THE NEXT TEXT BOX
				next_field.focus();
			}
		}
	}
}

//function fnTrapKD(btn, event)
//{
//	if (document.all)
//	{
//		if (event.keyCode == 13)
//		{
//			event.returnValue=false;
//			event.cancel = true;
//			btn.click();
//		}
//	}
//	else if (document.getElementById)
//	{
//		if (event.which == 13)
//		{
//			event.returnValue=false;
//			event.cancel = true;
//			btn.click();
//		}
//	}
//	else if(document.layers)
//	{
//		if(event.which == 13)
//		{
//			event.returnValue=false;
//			event.cancel = true;
//			btn.click();
//		}
//	}
//}

//menu_status = new Array(); 

function autotab(original,destination)
{
  if (document.getElementById(original).getAttribute && document.getElementById(original).value.length == document.getElementById(original).getAttribute("maxlength"))
  {
      document.getElementById(destination).focus()
  }
}

function showHide(theid,image)
{
	if (document.getElementById) 
	{
	    if(menu_status[theid] != 'show') 
		{
			document.getElementById(theid).className = 'show';
			menu_status[theid] = 'show';
			image.src = 'images/misc/minus.gif';
		}
		else
		{
			document.getElementById(theid).className = 'hide';
			menu_status[theid] = 'hide';
			image.src = 'images/misc/plus.gif';
		}
	}
}


function ShowEmail(theID)
{
  document.getElementById(theID).style.display = '';
  return false;
}


function HideEmail(theID)
{
  document.getElementById(theID).style.display = 'none';
  return false;
}



//function SessionTimeoutWarning(blRgstrd)
//{
//	var intInrvl = ((1000*60)*20);
////	var intInrvl = ((1000*60));
//	
//	//setTimeOut(displayTimeoutAlert(blRgstrd), 20000);
//	window.setTimeout('displayTimeoutAlert('+blRgstrd+')', intInrvl);
//}

//function displayTimeoutAlert(rgstrd)
//{
//	//OBJECT WHICH HOLDS THE MESSAGE BASED ON PARAMETER 
//	var strMsg = '';
//	
//	//PUT MESSAGE IN OBJECT BASED ON IF USER IS REGISED OR NOT REGISTERED BY HAS ITEM IN SESSION
//	if(rgstrd)
//		strMsg = 'For the safety of your account, your session will expire in 10 minutes due to inactivity.  Unless you reset the session time, you will be logged out of the system.  Would you like to reset your session?';
//	else
//		strMsg = 'Your session will expire in 10 minutes due to inactivity.  Unless you reset the session time, all items in your shopping cart and wish list will be disposed.  Would you like to reset your session?';
//	
//	//IF USERS DECIDES TO REFRESH SESSION, THEN PAGE IS POSTED BACK
//	if(confirm(strMsg)==true)
//		__doPostBack('','');		
//}
