function popup(what) {

	showdiv(what);
	showdiv('popup');
}

function hidealldivs() {
	closepopup('loginformdiv');
	closepopup('mcalcdiv');
	closepopup('rmidiv');
	closepopup('rsdiv');
	closepopup('savelistingdiv');
	closepopup('savesearchdiv');
}

function showdiv(zdiv) {
	// alert('showdiv: ' + zdiv);
		myDiv = getRefToDiv(zdiv);
		if( myDiv.style ) { myDiv= myDiv.style; }
		myDiv.display = 'block';
}

function hidediv(zdiv) {
		myDiv = getRefToDiv(zdiv);
		if( myDiv.style ) { myDiv= myDiv.style; }
		myDiv.display = 'none';
}

function closemessage() {
	// hidediv('popup');
	hidediv('ajaxmessage');
}

function closepopup(zdiv) {
	hidediv(zdiv);
	hidediv('popup');
	hidediv('ajaxmessage');
}

function getRefToDiv(divID,oDoc) {
  if( document.getElementById ) {
    return document.getElementById(divID); }
  if( document.all ) {
    return document.all[divID]; }
  if( !oDoc ) { oDoc = document; }
  if( document.layers ) {
    if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
      //repeatedly run through all child layers
      for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
        //on success, return that layer, else return nothing
        y = getRefToDiv(divID,oDoc.layers[x].document); }
    return y; } }
  return false;
}

function showrecoverdiv() {
	showdiv('recoverpassword');
	hidediv('loginformdiv');
	hidediv('ajaxmessage');
}

function showregistration() {
	showdiv('registration');
	hidediv('loginformdiv');
	hidediv('ajaxmessage');
}

function recoverpassword() {
	// alert('recoverpassword');
	ajax_action = "recoverpassword";
	doajax('recoverpassword');
	hidediv('recoverpassword');
	showdiv('loginformdiv');
}

function showsavelisting(mlsno,logged_in) {
	document.getElementById("ajax_action").value = "showsavelisting";

	// hidealldivs();

	if (logged_in != 0) {
		showdiv('savelistingdiv');
		hidediv('loginformdiv');
	} else {
		// need to log in first... save the listing number and the action - saved in global var
		showdiv('loginformdiv');
	}
	showdiv('popup');
}

function showsavesearch(logged_in) {

	document.getElementById("ajax_action").value = "showsavesearch";

	if (logged_in != 0) {
		showdiv('savesearchdiv');
		hidediv('loginformdiv');
	} else {
		// need to log in first... save the listing number and the action - saved in global var
		showdiv('loginformdiv');
	}
	showdiv('popup');
}

function savesearch() {
	doajax('savesearch');
}

function savelisting(mlsno) {
	doajax('savelisting');
}

function deletelisting(mlsno) {
	document.getElementById("ajax_action").value = "deletelisting";
	doajax('deletelisting');
}

function processshowing() {
	ajax_action="processshowing";
	doajax("processshowing");
}

function processrmi() {
	ajax_action="processrmi";
	doajax('processrmi');

}

function processshowingrequest() {
	ajax_action = "processshowingrequest";
	// the first thing we need to do is to VALIDATE.
	myForm = getRefToDiv('showingrequest_form');
	// alert (myForm.username.value);  // - WORKS!

	valid = validateemailrequest();  // use the same as email...

	if (valid) {
		qs = "?cid=" + document.getElementById("cid").value;
		qs += "&mlsno=" + myForm.mlsno.value;
		qs += "&username=" + myForm.txtusername.value;
		qs += "&email=" + myForm.txtsenderemail.value;
		qs += "&phone=" + myForm.txtPhone.value;
		qs += "&fax=" + myForm.txtFax.value;
		qs += "&comments=" + myForm.txttext.value;
		qs += "&datetosee=" + myForm.datetosee.value;
		qs += "&timetosee=" + myForm.timetosee.value;

		// alert(qs);
		doajax('requestshowing',qs);
		document.getElementById("aa_content").scrollTop = 0;
		window.location.href = "#aa_top";
	}
}

function processemailrequest() {

	ajax_action = "processemailrequest";
	// the first thing we need to do is to VALIDATE.
	myForm = getRefToDiv('emailrequest_form');
	// alert (myForm.username.value);  // - WORKS!

	valid = validateemailrequest();
	// we have a query string... now we need to do some validation..

	if (valid) {
		// alert('valid');
		// problem... ajax doesn't come back quick enough, sometimes...
		// how to wait?

		//success = document.getElementById('ajax_success').value;
		// if success, then we can add the record!
		// we need to generate a url from the form... since there are LOTS of fields,
		// we may as well do it here.

		//if (success==1) {

		qs = "?cid=" + document.getElementById("cid").value;
		qs += "&mlsno=" + myForm.mlsno.value;
		qs += "&username=" + myForm.txtusername.value;
		qs += "&email=" + myForm.txtsenderemail.value;
		qs += "&phone=" + myForm.txtPhone.value;
		qs += "&fax=" + myForm.txtFax.value;

		if (myForm.agentstatus[1].checked) {
			qs += "&agentstatus=1";
			qs += "&agentname=" + myForm.agentname.value;
		}

		if (myForm.agentstatus[2].checked) {
			qs += "&agentstatus=2";
		}

		if (myForm.agentstatus[0].checked) {
			qs += "&agentstatus=0";
		}


		qs += "&comments=" + myForm.txttext.value;


		doajax('emailrmi',qs);
		document.getElementById("aa_content").scrollTop = 0;
		window.location.href = "#aa_top";
	}
}

function validateemailrequest() {
	var valid=true;
	var badmsg = "Invalid Registration " + "\n";
	// now the required fields...
	if (myForm.txtusername.value == "") {
		valid = false;
		badmsg += "Name required...\n";
	}
	if (myForm.txtsenderemail.value == "") {
		valid=false;
		badmsg += "Email required ...\n";
	}

/*
	if (myForm.txtPhone.value == "") {
		valid=false;
		badmsg += "Phone required ...(enter NONE if none)\n";
	}
*/
	if (!valid) {
		badmsg += "\n\n";
		badmsg += "Please correct and resubmit...";
		alert (badmsg);
	}

	return valid;
}
function sendlogin() {
	document.getElementById("ajax_status").value = 'working';
	ajax_status=0;
	doajax('sendlogin');
}

function processlogin() {
	logged_in = document.getElementById('ajax_logged_in').value;
	// alert('Logged In:' + logged_in);

	var divtoshow = document.getElementById("ajax_action").value;

	document.getElementById("ajaxout").innerHTML = "X" + logged_in + "XDivToShow: " + divtoshow;

	if (logged_in != 0) {
		// alert('showing rmidiv');
		// hide the login form and show the rmiform
		// show rmiformdiv
		document.getElementById("loginstatus").innerHTML = "Logged In";
		document.getElementById("ajaxout").innerHTML = document.getElementById("ajaxout").innerHTML + document.getElementById("loginstatus").innerHTML;
		switch (divtoshow) {
			case "showrs":
				divtoshow="rsdiv";
				break;
			case "showrmi":
				divtoshow="rmidiv";
				break;
			case "showsavelisting":
				divtoshow="savelistingdiv";
				break;
			case "showsavesearch":
				divtoshow="savesearchdiv";
				break;
			default:
				divtoshow="loginformdiv";
		}
		showdiv(divtoshow);

		// hide the loginformdiv
		hidediv('loginformdiv');
	} else {
		// otherwise, the login form should still be visible...
		// just need to show the message
		ajax_msg = document.getElementById('ajax_msg').value;
		document.getElementById("ajaxmessage").innerHTML = "<p>" + ajax_msg + "</p>";
		showdiv("ajaxmessage");
	}
}

function showrmiresults(zdiv) {
	// make the ajax div visible...
	//  alert('showrmiresults');
	success = document.getElementById('ajax_success').value;
	logged_in = document.getElementById('ajax_logged_in').value;
	// alert("Success in results: " + success);
	var id='ajaxmessage';
	var prop = 'background-color';

	// alert ("Success: " + success);
	if (success == '1') {
		// hide the div...
		hidediv(zdiv);
		var v = '#00f';
	} else {
		var v = '#f00';
	}

	// setStyleById(id,prop,v);
	document.getElementById('ajaxmessage').style['background-color'] = v;

	ajax_msg = document.getElementById('ajax_msg').value;
	document.getElementById("ajaxresults").innerHTML = "<p>" + ajax_msg + "</p>";
	showdiv("ajaxmessage");
}

// setStyleById: given an element id, style property and
// value, apply the style.
// args:
//  i - element id
//  p - property
//  v - value
//
function setStyleById(i, p, v) {
	alert("V: " + v);
	var n = document.getElementById(i);
	n.style[p] = v;
}

function sendregistration() {
		// alert("X" + document.getElementById("ajax_registered").value + "X");

	ajax_action = "register";
	// the first thing we need to do is to VALIDATE.
	myForm = getRefToDiv('newregistration');
	// alert (myForm.username.value);  // - WORKS!

	valid = validateform();
	// we have a query string... now we need to do some validation..
	if (valid) {
		// alert('valid');
		// problem... ajax doesn't come back quick enough, sometimes...
		// how to wait?

		//success = document.getElementById('ajax_success').value;
		// if success, then we can add the record!
		// we need to generate a url from the form... since there are LOTS of fields,
		// we may as well do it here.

		//if (success==1) {
		qs = "?cid=" + document.getElementById("cid").value;
		qs += "&username=" + myForm.username.value;
		qs += "&passwd=" + myForm.passwd.value;
		qs += "&firstname=" + myForm.firstname.value;
		qs += "&firstname2=" + myForm.firstname2.value;
		qs += "&lastname=" + myForm.lastname.value;
		qs += "&lastname2=" + myForm.lastname2.value;
		qs += "&address=" + myForm.address.value;
		qs += "&address2=" + myForm.address2.value;
		qs += "&city=" + myForm.city.value;
		qs +- "&state=" + myForm.state.value;
		qs += "&zip=" + myForm.zip.value;
		qs += "&homephone=" + myForm.homephone.value;
		qs += "&workphone=" + myForm.workphone.value;
		qs += "&wp_ext=" + myForm.wp_ext.value;
		qs += "&email=" + myForm.email.value;
		qs += "&comments=" + myForm.zcomments.value;

		document.getElementById("ajax_logged_in").value='0';

		doajax('addnewaccount',qs);

	}
}


function showloginstatus(logged_in) {
	var loginstatus;
	if(logged_in) {
		document.getElementById("loginstatus").innerHTML = "Logged In";
	} else {
		document.getElementById("loginstatus").innerHTML = "<em style=\"color: #c00;\">NOT</em> Logged In";
	}
}
function validateform() {
	var valid=true;
	var badmsg = "Invalid Registration " + "\n";
	if (myForm.passwd.value != myForm.passwd2.value) {
		valid=false;
		badmsg += "Passwords do not match... \n";
	}

	if (myForm.email.value != myForm.email2.value) {
		valid = false;
		badmsg += "Email addresses do not match ... \n";
	}

	// now the required fields...
	if (myForm.username.value == "") {
		valid = false;
		badmsg += "Username required...\n";
	}
	if (myForm.firstname.value == "") {
		valid=false;
		badmsg += "First Name required ...\n";
	}
	if (myForm.lastname.value == "") {
		valid=false;
		badmsg += "Last Name required ...\n";
	}
	if (myForm.address.value == "") {
		valid=false;
		badmsg += "Address required ...\n";
	}
	if (myForm.firstname.value == "") {
		valid=false;
		badmsg += "Firstname required ...\n";
	}
	if (myForm.city.value == "") {
		valid=false;
		badmsg += "City required ...\n";
	}
	if (myForm.state.value == "") {
		valid=false;
		badmsg += "State required ...\n";
	}
	if (myForm.zip.value == "") {
		valid=false;
		badmsg += "Zip Code required ...\n";
	}
	if (myForm.firstname.value == "") {
		valid=false;
		badmsg += "Firstname required ...\n";
	}
	if (myForm.homephone.value == "") {
		valid=false;
		badmsg += "Home Phone required ...\n";
	}
	if (myForm.email.value == "") {
		valid=false;
		badmsg += "Email required ...\n";
	}

	if (!valid) {
		badmsg += "\n\n";
		badmsg += "Please correct and resubmit...";
		alert (badmsg);
	}

	return valid;
}