function element(divID) {
		return document.getElementById(divID);
}

function checkFields() {
	var msg = "The following fields are required: \n";
	
	if (document.adminForm.mls.value == "") {
		msg = msg + " - MLS # \n";
	}

	if (document.adminForm.price.value == "") {
		msg = msg + " - Price \n";
	}
	
	if (document.adminForm.bedrooms.value == "") {
		msg = msg + " - Bed Rooms \n";
	}
	
	if (document.adminForm.bathrooms.value == "") {
		msg = msg + " - Bath Rooms \n";
	}
	
	if (document.adminForm.city.value == "") {
		msg = msg + " - City \n";
	}
	
	if (document.adminForm.state.value == "") {
		msg = msg + " - State \n";
	}
	
	if (document.adminForm.zip.value == "") {
		msg = msg + " - Zip Code \n";
	}
	
	if (msg == "The following fields are required: \n") {
		document.adminForm.submit();
		element('add-form').style.marginBottom = "0px";
		element('add-form').style.marginTop = "0px";
		setTimeout("element('add-form').innerHTML = '<div id=\"loading\"><p><img src=\"images/loading.gif\" />Adding listing please wait!</p></div>'", 500);
	} else {
		alert(msg);
		return false;
	}
}

function emailList() {
	var msg = "Good";
	
	if (document.mailing.email.value == "") {
		msg = "Please input an e-mail address!\n";
	}
	
	if (msg == "Good") {
		document.adminForm.submit();
	} else {
		alert(msg);
		return false;
	}
}

function swapPhoto(elementID,imgs,photo) {
	element(elementID).innerHTML = "<a href=\"javascript:showPhoto('pictures/" + photo + "');\"><span>Click thumbnail for larger photo.</span><img src=\"" + imgs + "\" alt=\"Listing Photo\" /></a>";	
}

function showPhoto(photoImg) {
		if (window.ActiveXObject) {
			element('blackbg').style.width = element('body').clientWidth + "px";
			if (screen.height < element('body').clientHeight) {
				var divHeight = element('body').clientHeight;
			} else {
				var divHeight = screen.height;
			}
			element('blackbg').style.height = divHeight + "px";
		} else {
			element('blackbg').style.width = window.innerWidth + "px";
			element('blackbg').style.height = window.innerHeight + "px";
		}
		
		element('blackbg').style.display = "block";
		element('large-photo').style.display = "block";
		element('large-photo').innerHTML = "<a href=\"javascript:closePhoto();\"><img src=\"" + photoImg + "\" alt=\"Listing Photo\" /></a><p><a href=\"javascript:closePhoto();\">Close [X]</a></a>";
}

function closePhoto() {
		element('blackbg').style.display = "none";
		element('large-photo').style.display = "none";
}

function removePhoto(photo,rowID,photoNum) {
	c = confirm("Are you sure you want to remove the photo?");
	if(c == true) {
		ajax('removephoto.php','photo=' + photo + '&rowID=' + rowID + '&photoNum=' + photoNum,'','POST','0');
	}
	
	setTimeout(window.location.reload(), 500);
}

var xmlhttp;
function ajax(url,eventVar,elementID,request,sChange) {
	
	methreq = request;
	element_id = elementID
	
	if (window.XMLHttpRequest) {
  		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
	}
	
	if (xmlhttp) {
		if (sChange == '1') {
			xmlhttp.onreadystatechange=check_state;
		}
		switch(methreq) {
			case "POST":
				method = "POST";
				break;
			case "GET":
				method = "GET";
				break;
		}
		xmlhttp.open(method,url,true);
		if(method == "POST"){
			xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		}		
		xmlhttp.send(eventVar);
	}
}

function check_state() {
	if (xmlhttp.readyState==4&&xmlhttp.responseText) {
		if (xmlhttp.status==200) {
			element(element_id).innerHTML = xmlhttp.responseText;
		}
	}
}

function listingRemove(listingID,divID) {
	c = confirm("Are you sure you want to remove this listing?");
	if (c == true) {
		ajax('remove.php','id=' + listingID,divID,'POST','1');
	}
}

function cryptPass(obj) {
	document.loginForm.hidepass.value = obj;
}

function showHelp() {
	if (element('help').style.display == "none" || element('help').style.display == "") {
		element('help').style.display = "block";
	} else {
		element('help').style.display = "none";
	}
}
