function checkphone(e){
	var goodphone="";
	var phone=String(e.value);
	phone=phone.replace(/[^0-9]/g,'');
	var vlen=phone.length;

	if (phone)
		{

		if (vlen=='10' && !isNaN(phone))
			{
			goodphone="("+phone.slice(0,3)+") "+phone.slice(3,6)+"-"+phone.slice(6);
			e.value=goodphone;
			}
		else
			{
			document.getElementById("error").innerHTML = "The phone number you have entered is invalid. Please enter only 10 numbers including the area code.";
			globalvar = e;
			setTimeout("globalvar.focus();", 1);
			setTimeout("globalvar.select();", 1);
			}
		}
	else
		{
		e.value=phone;
		}
	}
	
function checkcontactphone(e){
	var goodphone="";
	var phone=String(e.value);
	phone=phone.replace(/[^0-9]/g,'');
	var vlen=phone.length;

	if (phone)
		{

		if (vlen=='10' && !isNaN(phone))
			{
			goodphone="("+phone.slice(0,3)+") "+phone.slice(3,6)+"-"+phone.slice(6);
			e.value=goodphone;
			}
		else
			{
			document.getElementById("contacterror").innerHTML = "The phone number you have entered is invalid. Please enter only 10 numbers including the area code.";
			globalvar = e;
			setTimeout("globalvar.focus();", 1);
			setTimeout("globalvar.select();", 1);
			}
		}
	else
		{
		e.value=phone;
		}
	}
		
function checkemail(e){
	var email=String(e.value);
	if (email){
		if(!email.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)) {
			document.getElementById("error").innerHTML = "Please enter a valid email address.";
			globalvar = e;
			setTimeout("globalvar.focus();", 1);
			setTimeout("globalvar.select();", 1);
			}
		}
	}

function checkcontactemail(e){
	var email=String(e.value);
	if (email){
		if(!email.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)) {
			document.getElementById("contacterror").innerHTML = "Please enter a valid email address.";
			globalvar = e;
			setTimeout("globalvar.focus();", 1);
			setTimeout("globalvar.select();", 1);
			}
		}
	}
	
function checkLoginEmail(e){
	var email=String(e.value);
	if (email){
		if(!email.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)) {
			document.getElementById("loginError").innerHTML = "Please enter a valid email address.";
			globalvar = e;
			setTimeout("globalvar.focus();", 1);
			setTimeout("globalvar.select();", 1);
			}
		}
	}
		
/*Ajax*/
var xmlhttp = false;

//Check if we are using IE.
try 
	{
	//If the Javascript version is greater than 5.
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
catch (e) 
	{
	//If not, then use the older active x object.
	try 
		{
		//If we are using Internet Explorer.
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
	catch (E) 
		{
		//Else we must be using a non-IE browser.
		xmlhttp = false;
		}
	}

//If we are using a non-IE browser, create a javascript instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
	xmlhttp = new XMLHttpRequest();
	}
	
function makerequest(serverPage, objID) {
		
	var date = new Date();
	var timestamp = date.getTime();
	url = serverPage + "?timestamp=" + timestamp;

	var obj = document.getElementById(objID);
	xmlhttp.open("GET", url);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
			}
		}
	xmlhttp.send(null);
	}
	
function getdoc(serverPage, id, objID) {
		
	var date = new Date();
	var timestamp = date.getTime();
	url = serverPage + "?timestamp=" + timestamp +"&id=" + id;

	var obj = document.getElementById(objID);
	xmlhttp.open("GET", url);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
			}
		}
	xmlhttp.send(null);
	}
	
/*Gets requested page with element id*/
function showevent(serverPage, id, objID) {
	var date = new Date();
	var timestamp = date.getTime();
	url = serverPage + "?timestamp=" + timestamp +"&id=" + id;
	newObj = objID + id;
	var obj = document.getElementById(newObj);
	document.getElementById(newObj).style.visibility = "visible";
	xmlhttp.open("GET", url);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
			}
		}
	xmlhttp.send(null);
	}
	
function clearevent(id, objID){
	newObj = objID + id;
	document.getElementById(newObj).style.visibility = "hidden";
	}
	
/*Form functions must include submitevent, getformvalues, validateevent, trim and process*/
/*getformvalues & trim never change*/

function getformvalues (fobj, valfunc){
	var str = "";
	aok = true;
	var val;
	//Run through a list of all objects contained within the form.
	for(var i = 0; i < fobj.elements.length; i++){
		if(valfunc) {
			if (aok == true){
				val = valfunc (fobj.elements[i].value,fobj.elements[i].name); 
					if (val == false){
						aok = false;
					}
				}
			}
		str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
		}
	//Then return the string values.
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	return str;
	}

function trim(inputString){
	// Removes leading and trailing spaces from the passed string. Also removes
	// consecutive spaces and replaces it with one space. If something besides
	// a string is passed in (null, custom object, etc.) then return the input.
	if (typeof inputString != "string") { 
		return inputString; 
		}
	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
	} 
	// Ends the "trim" function


var aok;

function submitform (theform, serverPage, objID, valfunc){
	var file = serverPage;
	var str = getformvalues(theform,valfunc);
	//If the validation is ok.
	if (aok == true){
		obj = document.getElementById(objID);
		processform (serverPage, obj, "post", str);
		}
	}

function processform (serverPage, obj, getOrPost, str){
	//Get an XMLHttpRequest object for use.
	//xmlhttp = getxmlhttp ();
	if (getOrPost == "get"){
			xmlhttp.open("GET", serverPage);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					document.getElementById("wrapper").innerHTML = xmlhttp.responseText;
					}
				}
			xmlhttp.send(null);
			} 
		else {
			xmlhttp.open("POST", serverPage, true);
			xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					obj.innerHTML = xmlhttp.responseText;
					}
				}
			xmlhttp.send(str);
			}
		}
		
/*Validates Lost Password*/
function validatelostpassword (thevalue, thename){
	var nowcont = true;
	if (thename == "emailaddr"){
		if (trim (thevalue) == ""){
			document.getElementById("error").innerHTML = "Please enter your email address";
			document.getElementById("lostpw").emailaddr.focus();
			nowcont = false;
			}
		}
	return nowcont;
	}
		
/*Validates Registration*/
function validateregistration (thevalue, thename){
	var nowcont = true;
	if (thename == "fname"){
		if (trim (thevalue) == ""){
			document.getElementById("error").innerHTML = "Please enter your first name";
			document.getElementById("form").fname.focus();
			nowcont = false;
			}
		}
	if (nowcont == true){
		if (thename == "lname"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please enter your last name.";
				document.getElementById("form").lname.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "community"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please select your community.";
				document.getElementById("form").community.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "street"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please enter your street address.";
				document.getElementById("form").street.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "city"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please enter your city.";
				document.getElementById("form").city.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "state"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please choose a state.";
				document.getElementById("form").state.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "zipcode"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please enter your zipcode.";
				document.getElementById("form").zipcode.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "phone"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please enter a phone number.";
				document.getElementById("form").phone.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "emailaddr"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please enter an email address.  This will be your login.";
				document.getElementById("form").emailaddr.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "pw1"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please enter a password.";
				document.getElementById("form").password.focus();
				nowcont = false;
				}
			}
		}
	return nowcont;
	}
	
/*Validates Registration Update*/
function validateregistrationupdate (thevalue, thename){
	var nowcont = true;
	if (thename == "fname"){
		if (trim (thevalue) == ""){
			document.getElementById("error1").innerHTML = "Please enter your first name";
			document.getElementById("form1").fname.focus();
			nowcont = false;
			}
		}
	if (nowcont == true){
		if (thename == "lname"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please enter your last name.";
				document.getElementById("form1").lname.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "community"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please select your community.";
				document.getElementById("form1").community.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "street"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please enter your street address.";
				document.getElementById("form1").street.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "city"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please enter your city.";
				document.getElementById("form1").city.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "state"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please choose a state.";
				document.getElementById("form1").state.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "zipcode"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please enter your zipcode.";
				document.getElementById("form1").zipcode.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "phone"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please enter a phone number.";
				document.getElementById("form1").phone.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "emailaddr"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please enter an email address.  This will be your login.";
				document.getElementById("form1").emailaddr.focus();
				nowcont = false;
				}
			}
		}
	return nowcont;
	}

	
/*Validates Contact Form*/
function validatecontact (thevalue, thename){
	var nowcont = true;
	if (thename == "contact"){
		if (trim (thevalue) == ""){
			document.getElementById("contacterror").innerHTML = "Please select a contact";
			document.getElementById("contactform").contact.focus();
			nowcont = false;
			}
		}
	if (nowcont == true){
		if (thename == "yourname"){
			if (trim (thevalue) == ""){
				document.getElementById("contacterror").innerHTML = "Please tell us your name.";
				document.getElementById("contactform").yourname.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "youremail"){
			if (trim (thevalue) == ""){
				document.getElementById("contacterror").innerHTML = "Please enter your email address.";
				document.getElementById("contactform").youremail.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "yourphone"){
			if (trim (thevalue) == ""){
				document.getElementById("contacterror").innerHTML = "Please enter your phone number.";
				document.getElementById("contactform").yourphone.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "comments"){
			if (trim (thevalue) == ""){
				document.getElementById("contacterror").innerHTML = "Please comment.";
				document.getElementById("contactform").comments.focus();
				nowcont = false;
				}
			}
		}
	return nowcont;
	}
	
/*Validates Classified Ad*/
function validateclassified (thevalue, thename){
	var nowcont = true;
	if (thename == "title"){
		if (trim (thevalue) == ""){
			document.getElementById("error").innerHTML = "Please enter a title for your classified";
			document.getElementById("form").title.focus();
			nowcont = false;
			}
		}
	if (nowcont == true){
		if (thename == "description"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please enter a description.";
				document.getElementById("form").description.focus();
				nowcont = false;
				}
			}
		}
	return nowcont;
	}
	
/*Validates Password Update*/
function validatepasswordupdate (thevalue, thename){
	var nowcont = true;
	if (thename == "pass"){
		if (trim (thevalue) == ""){
			document.getElementById("error2").innerHTML = "Please enter your new password";
			document.getElementById("form2").pass.focus();
			nowcont = false;
			}
		}
	if (nowcont == true){
		if (thename == "pass2"){
			if (trim (thevalue) == ""){
				document.getElementById("error2").innerHTML = "Please confirm your password";
				document.getElementById("form2").pass2.focus();
				nowcont = false;
				}
			}
		}
	return nowcont;
	}
