function focusAndSelect(object)
{
	object.focus();
    object.select();
    return true;
}//focusAndSelect
//
// Trim space from a string.
//
function trim(inputString) 
{
   // Removes leading and trailing spaces from the passed string. Also
   // removes consecutive spaces and replaces it with one space.
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
}//trim 
//
// Return the the left most part of string of length lngNumberCharacters.
//
function left(strString,lngNumberCharacters)
{
	var lngLength=0;
	var SubString="";
	// Make sure the number of characters is not greater than the string length
	if (strString.length < lngNumberCharacters)
	{
		lngLength = strString.length;
	}
	else
	{
		lngLength = lngNumberCharacters;	
	}	
	/* check to see if the first part of the strings are the same */
	for (var i=0; i < lngLength; i++)
	{
		SubString = SubString + strString.charAt(i);
	}
	return SubString;	
}//left

function over_effect(e,state){
	if (document.all)
	{
		source4=event.srcElement;
	}
	else 
	{
		if (document.getElementById)
		{
			source4=e.target
		}
	}
	if (source4.className=="menulines")
	{
		source4.style.borderStyle=state;
	}
	else
	{
		while(source4.tagName!="TABLE")
		{
			source4=document.getElementById? source4.parentNode : source4.parentElement
			if (source4.className=="menulines")
			{	
				source4.style.borderStyle=state;
			}
		}
	}
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		nav_01_over = newImage("images/btnNavHomeUp.gif");
		nav_02_over = newImage("images/btnNavContactUsUp.gif");
		nav_03_over = newImage("images/btnNavNewsUp.gif");
		nav_04_over = newImage("images/btnNavAcoutUsUp.gif");
		nav_05_over = newImage("images/btnNavMyProfileUp.gif");
		nav_06_over = newImage("images/btnNavReturnsUp.gif");
		nav_07_over = newImage("images/btnNavOrderHistoryUp.gif");
		nav_08_over = newImage("images/btnNavShoppingCartUp.gif");
		nav_09_over = newImage("images/btnNavLogoutUp.gif");
		preloadFlag = true;
	}
}
//
// Routine to populate color/size with commodity code selection.
//
function PopulateColorSizeOnCommCode()
{
	var lngSelected = 0;
	//
	//  Check to see if the color field exists before attempting to set it.
	//
	if (document.frmAddItem && !document.frmAddItem.txtColor)
	{
		// Do nothing if color is undefined
	}
	else
	{
		lngSelected = document.frmAddItem.txtHiddenColor[document.frmAddItem.txtCommCode.selectedIndex].value;
		/* Set the size based on the selected commodity code */
		for (var jx = 0; jx < document.frmAddItem.txtColor.length; jx++)
		{
			/* Select an item if it has already been set */
			if (document.frmAddItem.txtColor.options[jx].value == lngSelected)
			{
				document.frmAddItem.txtColor.selectedIndex = jx;
			}
		}
	} 
	//
	//  Check to see if the size field exists before attempting to set it.
	//
	if (document.frmAddItem && !document.frmAddItem.txtSize)
	{
		// Do nothing if size is undefined
	}
	else
	{
		lngSelected = document.frmAddItem.txtHiddenSize[document.frmAddItem.txtCommCode.selectedIndex].value;
		/* Set the size based on the selected commodity code */
		for (var jx = 0; jx < document.frmAddItem.txtSize.length; jx++)
		{
			/* Select an item if it has already been set */
			if (document.frmAddItem.txtSize.options[jx].value == lngSelected)
			{
				document.frmAddItem.txtSize.selectedIndex = jx;
			}
		}
	} 
}//PopulateColorSizeCommCode		
// 
// Routine to populate the commodity code based on the color size selection
//
function PopulateCommCodeOnColorSize()
{
	var lngSelected1 = 0;
	var lngSelected2 = 0;
	//
	//  Check to see if the color field exists before attempting to set it.
	//
	if (document.frmAddItem && !document.frmAddItem.txtColor)
	{
		// Do nothing if color is undefined
		lngSelected1 = 0;
	}
	else
	{
		// Get the selected color.
		lngSelected1 = document.frmAddItem.txtColor[document.frmAddItem.txtColor.selectedIndex].value;
	} 
	//
	//  Check to see if the size field exists before attempting to set it.
	//
	if (document.frmAddItem && !document.frmAddItem.txtSize)
	{
		// Do nothing if size is undefined
		lngSelected2=0;
	}
	else
	{
		// Get the selected size.
		lngSelected2 = document.frmAddItem.txtSize[document.frmAddItem.txtSize.selectedIndex].value;
	} 
	/* Set the size based on the selected commodity code */
	for (var jx = 0; jx < document.frmAddItem.txtCommCode.length; jx++)
	{
		/* Select an item it matches on size and color (if color or size do not exist then should match up on zero) */
		if (document.frmAddItem.txtHiddenColor[jx].value == lngSelected1 && document.frmAddItem.txtHiddenSize[jx].value == lngSelected2)
		{
			document.frmAddItem.txtCommCode.selectedIndex = jx;
		}
	}
}//PopulateCommCodeOnColorSize

function IgnoreEvents(e)
{
  return false
}

function ShowScrollableWindow(strPageName,strWidth, strHeight, strArgs)
{
  var strArg;
  var strRetValue;
  if (window.showModalDialog)
  {
	strArg="dialogWidth=" + strWidth + "px;dialogHeight=" + strHeight+ "px;status=no";
    strRetValue=window.showModalDialog(strPageName,strArgs,strArg)
  }
  else
  {
	strArg="dependent=yes,width=" + strWidth + ",height=" + strHeight
    window.top.captureEvents (Event.CLICK|Event.FOCUS)
    window.top.onclick=IgnoreEvents
    window.top.onfocus=HandleFocus 
    winModalWindow = 
    window.open (strPageName,"ModalChild",strArg)
    winModalWindow.focus()
  }
  return strRetValue;
}
 
function HandleFocus()
{
  if (winModalWindow)
  {
    if (!winModalWindow.closed)
    {
      winModalWindow.focus()
    }
    else
    {
      window.top.releaseEvents (Event.CLICK|Event.FOCUS)
    }
  }
  return false
}
//
//  Validate an integer
//
function isInteger(value) 
{
    if (value == "")  { return false }
    start = 0;
    for (i=start; i<value.length; i++)
    {
      if (value.charAt(i) < "0") { return false; }
      if (value.charAt(i) > "9") { return false; }
    }
    return true;
}//IsInteger
//
// Date add function.
//
function DateAdd(dtiStartDate, numDays, numMonths, numYears)
{
	//
	// Convert the string representation of the date to a date.
	//
	var startDate = new Date(dtiStartDate);
	var returnDate = new Date(startDate.getTime());
	var yearsToAdd = numYears;
	
	var month = returnDate.getMonth()	+ numMonths;
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
	//
	// Return a string representation of the date.
	//
	return (returnDate.getMonth()+1) + "/" + returnDate.getDate() + "/" + returnDate.getFullYear();
}//DateAdd
//
// We needed a new date validation routine that would simply pass the error 
// message back intead of presenting it directly.
//
function ValidateDateNew(varDateToCheck, varType) 
{
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	var strMessage = "";
		
	/* is the format ik? */
	var matchArray = varDateToCheck.match(datePat); 
	
	if (matchArray == null) 
	{
		strMessage =  varType + " date is not in a valid format. Please use the MM/DD/YYYY format.";
		return strMessage;
	}	
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	
	/* check month range */
	if (month < 1 || month > 12) 
	{
		strMessage = varType + " month must be between 1 and 12.";
		return strMessage;
	}
	if (day < 1 || day > 31) 
	{
		strMessage = varType + " day must be between 1 and 31.";
		return strMessage;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		strMessage = varType + " month "+month+" doesn't have 31 days!";
		return strMessage;
	}
	if (month == 2) 
	{ // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) 
		{
			strMessage = "February " + year + " doesn't have " + day + " days!";
			return strMessage;
		}
	}
	return strMessage;		
}//ValidateDateNew 

function ValidateDate(varDateToCheck, varType) {
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	
	/* is the format ik? */
	var matchArray = varDateToCheck.match(datePat); 
	
	if (matchArray == null) 
	{
		alert( varType + " date is not in a valid format. Please use the MM/DD/YYYY format.");
		return false;
	}	
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	
	/* check month range */
	if (month < 1 || month > 12) 
	{
		alert(varType + " month must be between 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31) 
	{
		alert(varType + " day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		alert(varType + " month "+month+" doesn't have 31 days!");
		return false;
	}
	if (month == 2) 
	{ // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) 
		{
			alert("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}
	return true;		
}//ValidateDate  
//
// Display any errors issued by the update data routine.
//
function DisplayErrors(strError)
{
	if (strError != "")
	{
		alert(strError);
	}	
}//DisplayErrors
//
// Find the first instance of the search character within a string starting at character intStart
//
function instr0(intStart,strString,strSearchCriteria)
{
	var lngLength=0;
	var SubString="";
	/* check to see if the first part of the strings are the same */
	for (var i=intStart; i < strString.length; i++)
	{
		if (strString.charAt(i) == strSearchCriteria.charAt(0))
		{
			return parseInt(i);
		}	
	}
	return 0
}//instr	
//
// If the user does not input slashes in the date, then attempt to auto format the result.
// Note:  For this to work the user must enter the data as 06/03/1970, not 6/3/1970.
//
function MaskDate(objDate)
{
	var intPos1=1;
	var intPos3=1;
	
	if(document.all)
	{
		//
		// Do not allow any key when the shift key is pressed.
		//
		if (event.shiftKey)
		{
			return false;
		}	
		else
		{
			// Allow the tab key to continue to work.
			if(event.keyCode==9) return true;
			// Reset content if the backspace key is pressed.
			if(event.keyCode==8) return true;	
			// Reset content if the delete key is pressed.
			if(event.keyCode==46) return true;
			// Allow numbers 0 to 9 on the normal keyboard and the "/" character to pass.
			// Also allow numbers 0 to 9 on the numeric keypad.
			if((event.keyCode>47 && event.keyCode<58) || event.keyCode==191 || (event.keyCode>95 && event.keyCode<106))
			{
				// If the user manually inserts a "/" then exit auto format.
				if (event.keyCode == 191)
				{
					return true;			
				}
				// If the position of the first slash is not at 2, then do not attempt auto format.
				intPos1 = instr0(1,objDate.value,"/")
				if(intPos1 != 2 && intPos1 != 0)
				{
					return true;
				}	
				// If the 2nd slash is not at position 5 do not attemp auto format.
				intPos3 = instr0(4,objDate.value,"/")
				if(intPos3 != 5 && intPos3 != 0)
				{
					return true;
				}
				// Auto format the data when appropriate.
				if((objDate.value.length==2 || objDate.value.length==5) && event.keyCode!=8)
			    {
					objDate.value = objDate.value + "/";
				}
				// Auto format the data when appropriate.
				if((objDate.value.length==6 && (event.keyCode==48)))
			    {
					objDate.value = left(objDate.value,5) + "/20" + right(objDate.value,1);
				}
				return true;
			}
			else
			{
				return false;
			}	
		}	
	}
	else
	{
		var pattern = /^\d*$/;
		var text = objDate.value;
		if(text.search(pattern)<0 && text.length>0)
		{
			objDate = text.substr(0,text.length-2);
			return false;
		}
	return true;
	}
}//MaskDate

function ShowWindow(strPageName,strWidth, strHeight, strArgs)
{
  var strArg;
  var strRetValue;
  if (window.showModalDialog)
  {
	strArg="dialogWidth=" + strWidth + "px;dialogHeight=" + strHeight+ "px;scroll=no;status=no";
    strRetValue=window.showModalDialog(strPageName,strArgs,strArg)
  }
  else
  {
	strArg="dependent=yes,width=" + strWidth + ",height=" + strHeight
    window.top.captureEvents (Event.CLICK|Event.FOCUS)
    window.top.onclick=IgnoreEvents
    window.top.onfocus=HandleFocus 
    winModalWindow = 
    window.open (strPageName,"ModalChild",strArg)
    winModalWindow.focus()
  }
  return strRetValue;
}
