function poptastic(url){
	newwindow=window.open(url,'name', 'height=500,width=400,location=false');
	if (window.focus) {newwindow.focus()}
	}

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
			{
			alert("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)) {
			alert("The email address you have entered is invalid.");
			globalvar = e;
			setTimeout("globalvar.focus();", 1);
			setTimeout("globalvar.select();", 1);
		}
	}
}

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 getdoc(serverPage, id, objID) {
		
	var date = new Date();
	var timestamp = date.getTime();
	url = serverPage + "?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);
	}


function makerequest(serverPage, objID) {
		
	var date = new Date();
	var timestamp = date.getTime();
	url = serverPage + "?time=" +timestamp;

	var obj = document.getElementById(objID);
//	xmlhttp.open("GET", serverPage);
	xmlhttp.open("GET", url);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
			}
		}
	xmlhttp.send(null);
	}

//Movein calendar functions

function getcalendar(objID){
	
	var obj = document.getElementById(objID);
	obj.style.visibility = "visible";
	}

function hidecalendar(){
	var obj = document.getElementById('moveincalendar');
	obj.style.visibility = "hidden";
	}

function createDate(e){

	url = "getrealdate.php?thisday=" + e;
	xmlhttp.open("GET", url);
	xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("newcontact").calendardate.value = xmlhttp.responseText;
			document.getElementById("newcontact").hiddendate.value = e;
			hidecalendar();
			}
		}
	xmlhttp.send(null);
	}

/*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 Apartment Information*/
function validatecontact (thevalue, thename){
	var nowcont = true;
	if (thename == "fname"){
		if (trim (thevalue) == ""){
			document.getElementById("error").innerHTML = "Please tell us your first name";
			document.getElementById("newcontact").fname.focus();
			nowcont = false;
			}
		}
	if (nowcont == true){
		if (thename == "lname"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please tell us your last name.";
				document.getElementById("newcontact").lname.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "address"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please tell us your street address.";
				document.getElementById("newcontact").address.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "city"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please tell us the city your live in.";
				document.getElementById("newcontact").city.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "state"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please tell us your state.";
				document.getElementById("newcontact").state.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "zip"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please tell us your zip code.";
				document.getElementById("newcontact").zip.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "emailaddr"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please tell us your email address.";
				document.getElementById("newcontact").address.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "dphone"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please give a phone number where we can reach you.";
				document.getElementById("newcontact").dphone.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "floorplan"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please choose a floorplan.";
				document.getElementById("newcontact").floorplan.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "hiddendate"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please choose a move-in date.";
				document.getElementById("newcontact").calndardate.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "numres"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please tell us how many residents.";
				document.getElementById("newcontact").numres.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "pets"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please tell us if you have any pets.";
				document.getElementById("newcontact").pets.focus();
				nowcont = false;
				}
			}
		}
	return nowcont;
	}

/*Validates Maintenance Request Information*/
function validateservice (thevalue, thename){
	var nowcont = true;
	if (thename == "maintOcc"){
		if (trim (thevalue) == ""){
			document.getElementById("error").innerHTML = "Please tell us your full name";
			document.getElementById("service").maintOcc.focus();
			nowcont = false;
			}
		}
	if (nowcont == true){
		if (thename == "maintEmail"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please tell us your email address.";
				document.getElementById("service").maintEmail.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "maintUnit"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please tell us your unit number.";
				document.getElementById("service").maintUnit.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "maintPhone"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please tell us a phone number where we can reach you.";
				document.getElementById("service").maintPhone.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "maintReq"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please describe the request.";
				document.getElementById("service").maintReq.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "maintPerm"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Please let us know whether we have your permission to enter if you are not home.";
				document.getElementById("service").maintPerm.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "maintNotice"){
			if (trim (thevalue) == ""){
				document.getElementById("error").innerHTML = "Do we have permission to waive the 24 hour notice?";
				document.getElementById("service").maintNotice.focus();
				nowcont = false;
				}
			}
		}
	return nowcont;
	}




