function addToCart(objForm, objQty, minQty, maxQty, incrementQty)
{
	if ( maxQty < 1 )
	  maxQty = objQty.value;
		
  if ( isInteger(objQty.value) && 
	     objQty.value >= minQty &&
			 objQty.value <= maxQty && 
		   objQty.value % incrementQty == 0 )
  {
	  objForm.submit();
	}
	else
	{
	  if ( !isInteger(objQty.value) )
		  alert("Quantity cannot be empty and must be an integer.");
		else if ( objQty.value < minQty )
		  alert("Quantity must be greater than or equal to " + minQty + ".");
		else if ( objQty.value > maxQty )
		  alert("Quantity cannot exceed " + maxQty + ".");
		else
		  alert("Quantity must be an increment of " + incrementQty + ", such as: " + incrementQty + ", " + (incrementQty * 2) +  ", " + (incrementQty * 3) + ", etc.");
	}
}

function keyHandler(evt, objForm)
{
  evt = (evt) ? evt : event;
  var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
  
	if ( charCode == 13 )
	{
    checkForm(objForm);
		return false;
  }
}

function addToList(strLabel, strValue, objSelect)
{
  objSelect[objSelect.length] = new Option(strLabel, strValue);
}

function removeFromList(objSelect)
{
  SelectedCounter = objSelect.length;
	
  for (i = 0; i < SelectedCounter; i++)
	{
	  if ( objSelect[i].selected )
		{
		  objSelect[i] = null;
			SelectedCounter--;
			i--;
		}
	}
}

function updateList(action, objForm, sourceID, destID, requiredValue, maxItems)
{
	maxReached = false;
	requiredValueFlag = false;

	// Default maximum items if not specified that can be added to destination list is 9999
	if ( maxItems == undefined )
	  maxItems = 9999;
		
	sourceList = objForm.elements[sourceID];
	sourceCount = sourceList.length;
	iTotalSouceItems = sourceCount;
 
	destList = objForm.elements[destID];
	destCount = destList.length;
	iTotalDestItems = destCount;

  // Possible options: addAll, add, remove, removeAll
	switch (action)
	{
	  case 'addAll': //add all
			for (i = 0; i < iTotalSouceItems; i++)
			{
				// Add items to destination starting at the next available slot so existing items
  			// are not overwritten
				destList[destCount] = new Option(sourceList[i].text, sourceList[i].value);
				
				// Move to next slot in destination
				destCount++;
			}
			
			// Remove items from source by setting each to null
			for (i = 0; i < iTotalSouceItems; i++)
			{
				sourceList[0] = null;
			}
		break;
    
		case 'add': // Add to destination
		  for (i = 0; i < sourceCount && !maxReached; i++)
			{

				if ( sourceList[i].selected  )
				{
					if ( destList.length + 1 > maxItems )
					  maxReached = true;
					else
					{
						// Add items to destination starting at the next available slot so existing items
						// are not overwritten
						destList[destCount] = new Option(sourceList[i].text, sourceList[i].value);
						
						// Remove item from source list
						sourceList[i] = null;
						
						// Move to next slot in destination
						destCount++;
						
						// Decrement item count in source list
						sourceCount--;
						
						// Decrement source list index since an item was removed
						i--;
					}
				}
			}
		break;

		case 'remove': // Remove from destination
		  for (i = 0; i < destCount; i++)
			{
			  if ( destList[i].selected )
				{
					if ( requiredValue != destList[i].text )
					{
						// Add items to source starting at the next available slot so existing items
						// are not overwritten
						sourceList[sourceCount] = new Option(destList[i].text, destList[i].value);
						
						// Remove item from destination list
						destList[i] = null;
						
						// Move to next slot in source
						sourceCount++;
						
						// Decrement item count in destination list
						destCount--;
						
						// Decrement destination list index since an item was removed
						i--;
					}
					else
					  requiredValueFlag = true;
				}
			}
		break;
    
		case 'removeAll': // remove all from destination
			for (i = 0; i < iTotalDestItems; i++)
			{
				// Add items to source starting at the next available slot so existing items
  			// are not overwritten
			  sourceList[sourceCount] = new Option(destList[i].text, destList[i].value);
				
				// Move to next slot in source
				sourceCount++;
			}

			// Remove items from destination by setting each to null
			for (i = 0; i < iTotalDestItems; i++)
			{
				destList[0] = null;
			}
		break;
  }
	
	// Alert user that the maximum was reached
	if ( maxReached )
	  alert("The maximum items that can be added are: " + maxItems);
	else if ( requiredValueFlag )
	  alert("Cannot remove: " + requiredValue + " because it is required.");
}

function searchSelectItems(objSelect, strSearch)
{
  for (i = 0; i < objSelect.length; i++)
	{
		if ( objSelect[i].value.toLowerCase() == strSearch.toLowerCase() )
		  return true; // found match
	}
	
	// No match found
	return false;
}

function showPage(pageNum, objForm)
{
	 if ( objForm.page != undefined )
	  objForm.page.selectedIndex = pageNum;
		
  objForm.submit();
}

function addToList(strLabel, strValue, objSelect)
{
  objSelect[objSelect.length] = new Option(strLabel, strValue);
}

function isNumeric(sText)
{
  var validChars = "0123456789";
  var isNumber = true;
	var decimalCount = 0;
  var ch;
  
	if ( sText.length == 0 )
	  isNumber = false;
		
  for (i = 0; i < sText.length && isNumber == true; i++) 
  { 
    ch = sText.charAt(i);
		
    if ( validChars.indexOf(ch) == -1 )
    {
			// Makre sure only 1 decimal point exists
			if ( ch == "." && decimalCount == 0 )
			  decimalCount++;
			else
        isNumber = false;
    }
  }
  
	return isNumber;
}

function formatAsCurrency(objTxtField, minNum, maxNum, defaultNum)
{	
  var errorFlag = false;
	var errMsg = "";
  var strNum = new String(objTxtField.value);
	
  // If a dollar sign already exists at the beginning of the string and has a length of at least 2
	// then see if everything after the dollar sign is a number. If it is then there's nothing
	// to do, return success
	if ( strNum.indexOf("$") == 0 && strNum.length >= 2 )
	{
	  // Strip commas and dollar sign to see if it's a number
	  if ( !isNumeric(strNum.substr(1).replace(/,/g, "")) )
		{
		  errMsg = "Please enter only digits";
			errorFlag = true;
		}
		else if ( parseFloat(strNum.substr(1).replace(/,/g, "")) < minNum || parseFloat(strNum.substr(1).replace(/,/g, "")) > maxNum )
		{
		  errMsg = "Specify a number from " + minNum.toString() + " to " + maxNum.toString();
		  errorFlag = true;
		}
		else
		{
		  var num = new NumberFormat(strNum);
	    num.setCurrency(true);
			num.setPlaces(0);
		}
	}
	else
	{
	  if ( !isNumeric(strNum.replace(/,/g, "")) )
		{
		  errMsg = "Number can only contain digits, decimal point and dollar sign";
			errorFlag = true;
		}
		else if ( parseFloat(strNum.replace(/,/g, "")) < minNum || parseFloat(strNum.replace(/,/g, "")) > maxNum )
		{
		  errMsg = "Specify a number from " + minNum.toString() + " to " + maxNum.toString();
		  errorFlag = true;
		}
		else
		{
		  var num = new NumberFormat(strNum);
	    num.setCurrency(true);
			num.setPlaces(0);
		}
	}
	
	if ( errorFlag )
	{
	  objTxtField.value = defaultNum;
		alert(objTxtField.title + ": " + errMsg);
		return false;
	}
	else
	{
	  objTxtField.value = num.toFormatted();
	  return true;
	}
}

function formatAsPercent(objTxtField, minNum, maxNum, defaultNum)
{	
  var errorFlag = false;
	var errMsg = "";
  var strNum = new String(objTxtField.value);
	
  // If a percent sign already exists at the end and the string has a length of at least 2
	// then see if everything up to the percent sign is a number. If it is then there's nothing
	// to do, return success
	if ( strNum.length == 0 )
	{
	  objTxtField.value = defaultNum;
	  return;
	}
	
	if ( strNum.indexOf("%") == strNum.length - 1 && strNum.length >= 2 )
	{
	  if ( !isNumeric(strNum.substr(0, strNum.length - 2)) )
		{
		  errMsg = "Please enter only digits";
			errorFlag = true;
		}
		else if ( parseFloat(strNum.substr(0, strNum.length - 2)) < minNum || parseFloat(strNum.substr(0, strNum.length - 2)) > maxNum )
		{
		  errMsg = "Specify a number from " + minNum.toString() + " to " + maxNum.toString();
		  errorFlag = true;
		}
	}
	else
	{
	  if ( !isNumeric(strNum) )
		{
		  errMsg = "Number can only contain digits, decimal point and percent sign";
			errorFlag = true;
		}
		else if ( parseFloat(strNum) < minNum || parseFloat(strNum) > maxNum )
		{
		  errMsg = "Specify a number from " + minNum.toString() + " to " + maxNum.toString();
		  errorFlag = true;
		}
/*	
    else if ( strNum.length == 0 )
		{
		  strNum += "0%";
		}
*/
		else
		{
		  strNum += "%";
		}
	}
	
	if ( errorFlag )
	{
	  objTxtField.value = defaultNum;
		alert(objTxtField.title + ": " + errMsg);
		return false;
	}
	else
	{
	  objTxtField.value = strNum;
	  return true;
	}
}

function formatAsInteger(objTxtField, minNum, maxNum, defaultNum, ignoreEmpty)
{	
  var errorFlag = false;
	var errMsg = "";
  var strNum = new String(objTxtField.value);
	
	// Verify field contents is not empty
	// Verify field contains digits only
	// Verify number is within limits
	// If any of these are false, set to defaultNum
  if ( strNum.length == 0 )
	{
	  objTxtField.value = defaultNum;
	  return;
	}
	
	if ( !isInteger(strNum) )
	{
	  errMsg = "Please enter only digits";
		errorFlag = true;
	}
	else if ( parseInt(strNum) < minNum || parseInt(strNum) > maxNum )
	{
	  errMsg = "Specify a number from " + minNum.toString() + " to " + maxNum.toString();
	  errorFlag = true;
	}
	
	if ( errorFlag )
	{
	  objTxtField.value = defaultNum;
		alert(objTxtField.title + ": " + errMsg);
		return false;
	}
	else
	{
	  objTxtField.value = strNum;
	  return true;
	}
}

function addDays(myDate, days)
{
  // myDate = a Date object
  // days = +/- days from date
  
  d = new Date(myDate.getTime() + days * 24 * 60 * 60 * 1000);
  
	// Under FireFox, getYear() returns 106 for 2006 for instance, so add 1900
	var year;
	if ( d.getYear() < 1900 )
	  year = d.getYear() + 1900;
	else
	  year = d.getYear();
		
  return (d.getMonth()+1) + "/" + d.getDate() + "/" + year;
}

function goPage(src)
{
  window.location.href = src;
}

function trim(s)
{
  while (s.substring(0,1) == ' ')
    s = s.substring(1,s.length);

  while (s.substring(s.length-1,s.length) == ' ')
    s = s.substring(0,s.length-1);

  return s;
}

function isUrl(s)
{
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}


function isValidEmail(str)
{
  var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
  
	if ( !str.match(re) )
    return false;
	else
    return true;
}

function validateZip(field)
{
  var valid = "0123456789-";
  var hyphencount = 0;

  if (field.length!=5 && field.length!=10)
  {
    //alert("Please enter your 5 digit or 5 digit+4 zip code.");
    return false;
  }
  
  for (var i=0; i < field.length; i++)
  {
    temp = "" + field.substring(i, i+1);
    if (temp == "-")
	  hyphencount++;
    
	if (valid.indexOf(temp) == "-1")
	{
      //alert("Invalid characters in your zip code.  Please try again.");
      return false;
    }

    if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-"))
	{
      //alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
      return false;
    }
  }
  return true;
}

function validatePhone(field)
{
	// Regex pattern for verifying a Canadian postal code
	var regex = /[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]/
	
	if ( field.value.search(regex) == -1 )
	  return false;
	else
	  return true;
}

function isInteger(s)
{
  var i;
	
	if ( s.length == 0 )
	  return false;
		
  for (i = 0; i < s.length; i++)
  {   
    // Check that current character is number
    var c = s.charAt(i)
    if ( (c < "0") || (c > "9") )
      return false;
  }
  
  // All characters are numbers.
  return true;
}

function newWindow(url, target, w, h, scroll, resizable)
{
  var win = null;
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
	var menubar = 1;
	var toolbar = 1;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += 'scrollbars=' + scroll + ',';
  settings += 'resizable=' + resizable + ',';
	settings += 'menubar=' + menubar + ',';
	settings += 'toolbar=' + toolbar;

  win = window.open(url, target, settings);

  if ( parseInt(navigator.appVersion) >= 4 )
  {
    win.window.focus();
  }
}

function clearDefault(strDefault, objField)
{
  if ( objField.value == strDefault )
    objField.value = '';
}

var myWindow;
function openCenteredWindow(url, w, h)
{
  var left = 0, top = 0;
  var width = w;
  var height = h;
	
	if ( screen.availWidth <= width )
	  width = screen.availWidth;
	else
	{
	  left = parseInt((screen.availWidth/2) - (width/2));
	}
	
	if ( screen.availHeight <= height )
	  height = screen.availHeight;
	else
	{
	  top = parseInt((screen.availHeight/2) - (height/2));
	}

  var windowFeatures = "width=" + width + ",height=" + height + 
        ",status=1,scrollbars=1,toolbar=1,resizable=1,left=" + left + ",top=" + top + 
        ",screenX=" + left + ",screenY=" + top;

  myWindow = window.open(url, "subWind", windowFeatures);
}

function checkSearchForm(objForm, objInput, defaultValue)
{
	var strErr = "";
	
	if ( trim(objInput.value).length == 0 || objInput.value == defaultValue )
		strErr += "You must enter a word or phrase to search."
	
	if ( strErr )
	{
		alert(strErr);
	}
	else
	{
		objForm.submit();
	}
}

function deleteRecord(strURL)
{
	if ( confirm('Are you sure you want to delete this?') )
	  window.location.href = strURL;

  return;
}

// strField = name of column in table to sort by
// objSortBy = hidden input field used by function to store the sort by field name
// objSortDirection = hidden input field used by function to stor the sort direction
function sortBy(strField, objSortBy, objSortDirection)
{
	objSortBy.value = strField;
	
	if ( objSortDirection.value == "ASC" )
	  objSortDirection.value = "DESC";
	else
	  objSortDirection.value = "ASC";
	
}

function checkContactForm(objForm, maxMsgLen)
{
  var strErr = "";
	var strMsg = "Please correct the following:\n";
	
	if ( trim(objForm.first_name.value).length == 0 )
	  strErr += "- First name\n";
		
	if ( trim(objForm.last_name.value).length == 0 )
	  strErr += "- Last name\n";
	
	if ( objForm.subject.selectedIndex == 0 )
	  strErr += "- Subject\n";
		
	if ( trim(objForm.email.value).length == 0 )
	  strErr += "- Email address\n";
	else if ( !isValidEmail(objForm.email.value) )
    strErr += "- Email address is not valid\n";
		
	if ( trim(objForm.message.value).length == 0 )
	  strErr += "- Message\n";
	else if ( objForm.message.value.length > maxMsgLen )
    strErr += "- Message cannot exceed " + maxMsgLen + " characters in length\n";
	
	if ( strErr )
	{
	  alert(strMsg + strErr);
		return false;
	}
	else
	{
		objForm.submit();
		return true;
	}
}

function checkForumPost(objForm)
{
  var strErr = "";
	var strMsg = "Please correct the following:\n";
	
	if ( objForm.title.value != undefined )
	{
	  if ( trim(objForm.title.value).length == 0 )
	    strErr += "- Title\n";
	}
	
	if ( trim(objForm.message.value).length == 0 )
	  strErr += "- Message\n";
	
	if ( strErr )
	{
	  alert(strMsg + strErr);
		return false;
	}
	else
	{
		objForm.submit();
		return true;
	}
}