function textCounter( field, countfield, maxlimit ) {
  if ( field.value.length > maxlimit )
  {
    field.value = field.value.substring( 0, maxlimit );
    alert( 'Textarea value can only be ' + maxlimit + ' characters in length.' );
    return false;
  }
  else
  {
    countfield.value = maxlimit - field.value.length;
  }
}

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.disabled == false && formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 8) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a valid email (yourname@yourdomain.com) or leave the email blank.");
		formField.focus();
		result = false;
	}
   
  return result;

}

function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}

function validPrice(formField,fieldLabel,required)
{
	var tstring = "";
	var result = true;
        var string = formField.value;
        if (formField.value == "")
           return true;
  
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	string=tstring;

 	if (result)
 	{
 		if (!allDigits(string))
 		{
 			alert('The "Price" field can not include any decimals or letters.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}

function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var num = parseInt(formField.value);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var elems = formField.value.split("/");
 		
 		result = (elems.length == 3); // should be three components
 		
 		if (result)
 		{
 			var month = parseInt(elems[0]);
  			var day = parseInt(elems[1]);
 			var year = parseInt(elems[2]);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	
	return result;
}

function validLocation(formField){
  var str = formField.value
  if (str == ""){
    alert('Please select your location from the location field.');
    formField.focus();
    return false;	
  }
  else{
    return true;
  }
}

function validateForm(theForm)
{
 
  // Customize these calls for your form
  
  // Start ------->
  if (!validRequired(theForm.category,"Category")){
    return false;
  }
  
  if (theForm.price && !validPrice(theForm.price,"Price",true))
    return false;
  
  if (!validRequired(theForm.title,"Title"))
    return false;
  
  if (theForm.title){
    var textField = theForm.title.value
  }
  else{
    var textField = ""
  }
  theForm.status

  if(textField.length > 80){ 
    /* must be > 80, not == 80 or else the substring statement on the next line will cause the function to run again if the user dismisses the alert by pressing the 'enter' key rather than clicking 'OK'. */ 
    theForm.title.blur();
    /* move cursor out of form element to keep it from placing itself at position zero, causing an overwrite of the first character */
    alert("Title field is too long. Maximum 80 character limit exceeded.");
    return false;
  }
  

  if (textField.indexOf('(edit this heading)') != -1)
  { 
    theForm.title.blur();
    if (textField.indexOf('FREE:') != -1)
      alert('The heading of your ad is incomplete. Please change the (edit this heading) part with a heading that briefly describes your ad. Example: FREE: Baby Crib');
    else if (textField.indexOf('FOR-TRADE:') != -1)
      alert('The heading of your ad is incomplete. Please change the (edit this heading) part with a heading that briefly describes your ad. Example: FOR-TRADE: mountain bike for road bike');
    else
      alert('The heading of your ad is incomplete. Please change the (edit this heading) part with a heading that briefly describes your ad. Example: WANTED: Transparent Xbox 360');
    return false;
  }

  if (!validRequired(theForm.description,"Description"))
    return false;

  var textField = theForm.description

  if(textField.value.length > 2000){ 
    /* must be > 1000, not == 2000 or else the substring statement on the next line will cause the function to run again if the user dismisses the alert by pressing the 'enter' key rather than clicking 'OK'. */ 
    textField.blur() 
    /* move cursor out of form element to keep it from placing itself at position zero, causing an overwrite of the first character */
    alert("Description field is too long. Maximum 2000 character limit exceeded.") 
    return false;
  }

  if (!validRequired(theForm.phone,"Contact Phone"))
    return false;
  //alert("here");

  //if (!validRequired(theForm.email,"Contact Email"))
  //  return false;

  //if (!validEmail(theForm.email,"Contact Email",false))
  //  return false;


  if (theForm.status && !validRequired(theForm.status,"Status"))
    return false;

  if (!validRequired(theForm.password,"Password"))
    return false;

  //if (!validDate(theForm.available,"Date Available",true))
  //  return false;

  if ((theForm.password.value.substring(4,5) != 'i') && (!validLocation(theForm.location_name)))
    return false;
    
  if ((window.hasOwnProperty('checkJobWanted') == true) && (theForm.wanted_ind[0].checked == false) && (theForm.wanted_ind[1].checked == false)){
    alert('Please select one of "Job Offered" or "Job Wanted"');
    return false;
  }


  return true;
}
