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 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(2);
		}
	}
	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(2);
		}
	}
	
	if ( errorFlag )
	{
	  objTxtField.value = defaultNum;
		alert(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(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,resizable=0,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.visitorName.value).length == 0 )
	  strErr += "- Name\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);
	}
	else
	{
		objForm.submit();
	}
}

function checkQuoteForm(objForm, maxMsgLen)
{
  var strErr = "";
	var strMsg = "Please correct the following:\n";
	
	
	if ( trim(objForm.name.value).length == 0 )
	  strErr += "- Name\n";
		
	if ( trim(objForm.email_address.value).length == 0 )
	  strErr += "- Email address\n";
	else if ( !isValidEmail(objForm.email_address.value) )
    strErr += "- Email address is not valid\n";
	/*
	if ( trim(objForm.phone.value).length == 0 )
	  strErr += "- Phone\n";

	if ( trim(objForm.company.value).length == 0 )
	  strErr += "- Company\n";
	
	if ( trim(objForm.event_title.value).length == 0 )
	  strErr += "- Event Title\n";
	
	if ( trim(objForm.event_date.value).length == 0 )
	  strErr += "- Event Date\n";
	else if ( !isDate(objForm.event_date.value, "M/d/yyyy") )      
	  strErr += "- Event Date expected format: mm/dd/yyyy\n";
	
	if ( trim(objForm.event_time.value).length == 0 )
	  strErr += "- Event Time\n";
	
	if ( trim(objForm.duration_of_speech.value).length == 0 )
	  strErr += "- Duration of Speech\n";
	
	if ( objForm.type_of_speech.selectedIndex == 0 )
		strErr += "- Type of Speech\n";
	
	if ( trim(objForm.budget.value).length == 0 )
	  strErr += "- Budget\n";
	
	nonProfit = false;
	for (i = 0; i < objForm.non_profit.length; i++)
	{
    if (objForm.non_profit[i].checked == true)
		{	
      nonProfit = true;
			break;
		}
	}
	
	if ( !nonProfit )
	  strErr += "- Non-profit Organization?\n";
	
	
	coloradoConnection = false;
	for (i = 0; i < objForm.colorado_connection.length; i++)
	{
    if (objForm.colorado_connection[i].checked == true)
		{	
      coloradoConnection = true;
			break;
		}
	}
	
	if ( !coloradoConnection )
	  strErr += "- Colorado Connection?\n";
	
	
	productSales = false;
	for (i = 0; i < objForm.product_sales.length; i++)
	{
    if (objForm.product_sales[i].checked == true)
		{	
      productSales = true;
			break;
		}
	}
	
	if ( !productSales )
	  strErr += "- Product Sales Allowed?\n";
		
	if ( objForm.additional_info.value.length > maxMsgLen )
    strErr += "- Additional Information cannot exceed " + maxMsgLen + " characters in length\n";
	*/
	if ( strErr )
	{
	  alert(strMsg + strErr);
		return false;
	}
	else
	{
		objForm.submit();
		return true;
	}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}