var request;
var classifiedsAddItem			= "Place an ad for free by filling out the form. Newest ads will appear at the top of the classified ads page. You can update your ad later if you need to. Your ad will remain posted for one month from the time of the last update. All fields are required except the image field. If you are posting a \"Wanted\" item rather than selling an item, leave the price field blank. <br /><br />You may use the following markup tags in your description &lt;b&gt; <b>Bold</b> &lt;/b&gt; and Line &lt;br&gt; <br />Break. <br /><br />"
var classifiedsUpdateItem		= "Your ad will stay up for one month from the time that you add it or update it. Your information is stored by cookies, anytime you remove all your cookies from your computer you will not be able to update your ad. If you do not know what a computer cookie is then you do not need to worry about it. <br /><br />You may use the following markup tags in your description &lt;b&gt; <b>Bold</b> &lt;/b&gt; and Line &lt;br&gt; <br />Break. <br /><br />"
var classifiedEmailPrivacy		= "We are sensitive to your privacy and we want you to know that your email address is never displayed or given out to anyone. We use this information so that others can contact you through our website without actually seeing your email address."
var classifiedContactPerson		= "We do not store your email address or give it out to anyone. This is so the person who owns this item can reply to your email message."

validZipRegEx = /(^\d{5}$)|(^\d{5}-\d{4}$)/
validEmailRegEx = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
validPhoneRegEx = /^\(?(\d{3})\)?\-?[\.\-\/ ]?(\d{3})[\.\-\/ ]?(\d{4})$/
validFaxRegEx = /^\(?(\d{3})\)?\-?[\.\-\/ ]?(\d{3})[\.\-\/ ]?(\d{4})$/
validAreaCode = /(^\d{3}$)/
validPhoneFirst = /(^\d{3}$)/
validPhoneLast = /(^\d{4}$)/


function getValue(name, haystack) {
	/*
	we now have a list of name=value pairs like this:
	name=value
	name=value
	name=value
	...
	*/
	// get to the beginning of the name=value we care about
	if (haystack.indexOf(name) == -1) return '';
	haystack = haystack.substring(haystack.indexOf(name), haystack.length);

	// now isolate the name=value we care about
	haystack = haystack.substring(0, haystack.indexOf("\n"));

	// now get the value
	haystack = haystack.substring(haystack.indexOf("=")+1, haystack.length);
	return haystack;

}

function getValuePart(str) {
	return str.substring(str.indexOf("@")+1, str.length);
}

function getNamePart(str) {
	return str.substring(0, str.indexOf("@"));
}

function createRequest() {
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }

  if (!request)
    alert("Error initializing XMLHttpRequest!");
}

function updateLogin() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var logged_in = getValue('logged_in', request.responseText);
			var rusty_admin = getValue('rusty_admin', request.responseText);
			if (logged_in=='true') {
				window.location = "http://www.powercurrent.com/"
			} else {
				document.getElementById("status").innerHTML = 'Login failed, please try again.';
			}
		}
	}
}

function updateSidebar() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			alert("save complete!");
		}
	}
}

function saveSidebarText(text_position, state_key, county_key, water_name_key, scope) {
	createRequest();

	var sidebar_id = text_position + "_sidebar_text";
	var sidebar_text = document.getElementById(text_position + "_sidebar_text").value;
	var params = "text_position=" + text_position + "&" +
	"state_key=" + state_key + "&" +
	"county_key=" + county_key + "&" +
	"water_name_key=" + water_name_key + "&" +
	"scope=" + scope + "&" +
	"sidebar_text=" + sidebar_text;
	var url = "/qry/qry_save_sidebar_text.php";
	request.onreadystatechange = updateSidebar;
	request.open("POST", url, true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", params.length);
	request.setRequestHeader("Connection", "close");
	request.send(params);
}

function login() {
	createRequest();
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	var params = "username=" + username + "&" +
	"password=" + password;
	var url = "/qry/qry_login.php";
	request.onreadystatechange = updateLogin;
	request.open("POST", url, true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", params.length);
	request.setRequestHeader("Connection", "close");
	request.send(params);
}

function checkContactPerson() {
	var error = 0;
	var error_message = "Please make the following corrections:\n\n";
	var email = document.frmContactPerson.email.value;
	var name = document.frmContactPerson.name.value;
	var email_message = document.frmContactPerson.email_message.value;

	if (name == "") {
		error = 1;
		error_message += "* Please enter you name.\n";
	}
	if (email == "") {
		error = 1;
		error_message += "* Please enter your email address.\n";
	}
	if (email_message == 0) {
		error = 1;
		error_message += "* Please enter a message.\n";
	}
	if (error == 1) {
		alert(error_message);
		return false;
	} else {
		return true;
	}
}

function checkClassifedInformation() {
	var error = 0;
	var error_message = "Please make the following corrections:\n\n";
	var short_description = document.frmClassified.short_description.value;
	var long_description = document.frmClassified.long_description.value;
	var price = document.frmClassified.price.value;
	/*var phone = document.frmClassified.phone.value;*/
	var email = document.frmClassified.email.value;
	var name = document.frmClassified.name.value;
	var x = document.getElementById("catSelect")
	var category_key = x.options[x.selectedIndex].value;

	if (short_description == "") {
		error = 1;
		error_message += "* Please enter a title.\n";
	}
	if (long_description == "") {
		error = 1;
		error_message += "* Please enter a description.\n";
	}
	if (category_key == 0) {
		error = 1;
		error_message += "* Please choose a category.\n";
	}
	if (price != "" && isNaN(price)) {
		error = 1;
		error_message += "* Please enter a valid price (ex: 29.22, no dollar sign).\n";
	}
	if (email == "" || !validEmailRegEx.exec(email)) {
		error = 1;
		error_message += "* Please enter a valid email address.\n";
	}
	/*if (phone == "" || !validPhoneRegEx.exec(phone)) {
		error = 1;
		error_message += "* Please enter a valid phone number.\n";
	}*/
	if (name == "") {
		error = 1;
		error_message += "* Please enter a name.\n";
	}
	if (error == 1) {
		alert(error_message);
		return false;
	} else {
		return true;
	}
}
