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 getCheckedValue(objRadio)
{
	if ( !objRadio )
	  return "";

  var radioLength = objRadio.length;
	
	if ( radioLength == undefined )
	{
	  if ( objRadio.checked )
			return objRadio.value;
		else
			return "";
	}
	
	for (var i = 0; i < radioLength; i++)
	{
		if ( objRadio[i].checked )
		{
			return objRadio[i].value;
		}
	}
	
	return "";
}


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 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 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 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.phone.value).length < 10 )
    strErr += "- Phone # not complete\n";
	
	if ( objForm.subject.selectedIndex == 0 )
	  strErr += "- Select a Subject\n";
		
	if ( trim(objForm.message.value).length == 0 )
	  strErr += "- Comment\n";
	else if ( objForm.message.value.length > maxMsgLen )
    strErr += "- Comment cannot exceed " + maxMsgLen + " characters in length\n";
	
	if ( strErr )
	{
	  alert(strMsg + strErr);
//		return false;
	}
	else
	{
		objForm.submit();
//		return true;
	}
}

function resetContactForm(objForm)
{
  objForm.visitorName.value = "";
  objForm.email.value = "";	
  objForm.phone.value = "";
  objForm.subject.selectedIndex = 0;
  objForm.message.value = "";
}

function checkLoginForm(objForm)
{
  var strErr = "";
  var strMsg = "Please correct the following:\n";
  
  if ( objForm.username.value.length == 0 )
  {	
    strErr += "- Email address\n";
  }
  else if ( !isValidEmail(objForm.username.value) )
  {
    strErr += "- Email address is not valid\n";
  }
  
  if ( objForm.password.value.length == 0 )
  {
    strErr += "- Password\n";
  }
  
  if ( strErr )
  {
    alert(strMsg + strErr);
  }
  else
  {
    objForm.submit();
  }
}

function checkCreateAccountForm(objForm)
{
  var strErr = "";
  var strMsg = "Please correct the following:\n";
  
  if ( objForm.email_address.value.length == 0 )
  {
    strErr += "- Email address\n";
  }
  else if ( !isValidEmail(objForm.email_address.value) )
  {
    strErr += "- Email address is not valid\n";
  }
  
  // Password must be at least 6 characters long
  // Password cannot contain spaces
  // Password must match verify password field
  if ( objForm.account_password.value.length < 6 )
  {
    strErr += "- Password must be at least 6 characters in length\n";
  }
  else if ( objForm.account_password.value.indexOf(" ") >= 0 )
  {
    strErr += "- Password cannot contain any spaces\n";
  }
  else if ( objForm.account_password.value != objForm.verify_password.value )
  {
    strErr += "- Password does not match Verify Password field\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 ( trim(objForm.phone1.value).length < 10 )
    strErr += "- Phone # not complete\n";
	
  if ( strErr )
  {
    alert(strMsg + strErr);
  }
  else
  {
    objForm.submit();
  }
}

function checkTrialForm(objForm)
{
  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.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.phone1.value).length < 10 )
    strErr += "- Phone # not complete\n";
	
  if ( strErr )
  {
    alert(strMsg + strErr);
  }
  else
  {
    objForm.submit();
  }
}

function checkOrderForm(objForm)
{
  var strErr = "";
  var strMsg = "Please correct the following:\n";

  if ( objForm.acceptTerms.checked == false )
    strErr += "- You must accept the user agreement before submitting the order\n";
  
  if ( strErr )
  {
    alert(strMsg + strErr);
  }
  else
  {
    objForm.submit();
  }
}

function checkResetPasswordForm(objForm)
{
  var strErr = "";
  var strMsg = "Please correct the following:\n";
  
  if ( objForm.username.value.length == 0 )
  {
    strErr += "- Email address\n";
  }
  else if ( !isValidEmail(objForm.username.value) )
  {
    strErr += "- Email address is not valid\n";
  }
  
  if ( strErr )
  {
    alert(strMsg + strErr);
  }
  else
  {
    objForm.submit();
  }
}
