function InitPage() {

	document.onclick = function (event) {
		if ($('searchoptionspopup') != null) {
			if ($("searchoptionspopup").style.display != "none") {
				$("searchoptionspopup").hide()
			}
		}
	};
	if ($('searchoptionspopup') != null) { 
		$('searchoptionspopup').observe('click', stopPropagation);
	}
	//if ($('searchbarcontainer') != null) {
	//	$('searchbarcontainer').observe('click', stopPropagation);
	//}
	
	
	var menubackground = null;
	
	menubackground = '<table id="lettingssubbackground" style="height: '+(15*20+40)+'px" class="nlsmenubackground2"><tbody><tr><td/></tr></tbody></table>';
    Element.insert($("actmn_lettingssub"), menubackground);
	
	//var menubackground = '<table id="lettingsagbackground" style="height: '+(numLettings1*20+40)+'px" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    //Element.insert($("actmn_lettingsag"), menubackground);
    
    //menubackground = '<table id="lettingshmbackground" style="height: '+(numLettings2*20+40)+'px" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    //Element.insert($("actmn_lettingshp"), menubackground);
    
    //menubackground = '<table id="lettingsnzbackground" style="height: '+(numLettings3*20+40)+'px" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    //Element.insert($("actmn_lettingsqz"), menubackground);
    
    menubackground = '<table id="lettingsbackground" style="height: '+(10*20+40)+'px" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    Element.insert($("actmn_lettings"), menubackground);
    
    //menubackground = '<table id="lettingssubbackground" style="height: '+(3*20+40)+'px; left: -6px;" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    //Element.insert($("actmn_lettingssub"), menubackground);
    
    menubackground = '<table id="salesbackground" style="height: '+(saleMenuItems*20+40)+'px" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    Element.insert($("actmn_sales"), menubackground);

    menubackground = '<table id="salessubbackground" style="height: '+(2*20+40)+'px" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    Element.insert($("actmn_salessub"), menubackground);
    
    menubackground = '<table id="didsburyareasbackground" style="height: '+(7*20+40)+'px" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    Element.insert($("actmn_didsburyareas"), menubackground);
    
    menubackground = '<table id="heatonmoorbackground" style="height: '+(6*20+40)+'px" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    Element.insert($("actmn_heatonmoorareas"), menubackground);
    
    
    menubackground = '<table id="heatonmoorbackground" style="height: '+(10*20+40)+'px; left: -6px;" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    Element.insert($("actmn_heatonmoor"), menubackground);
    
    menubackground = '<table id="didsburybackground" style="height: '+(8*20+40)+'px; left: -6px;" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    Element.insert($("actmn_didsbury"), menubackground);
    
 //   menubackground = '<table id="lettingsbranchesbackground" style="height: '+(4*20+40)+'px; left: -6px;" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
  //  Element.insert($("actmn_lettingsbranches"), menubackground);
        
    
    

    menubackground = '<table id="landlordsbackground" style="height: '+(4*20+40)+'px" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    Element.insert($("actmn_landlords"), menubackground);
    menubackground = '<table id="newtenantsbackground" style="height: '+(2*20+40)+'px" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    Element.insert($("actmn_newtenants"), menubackground);
    
    menubackground = '<table id="tenantstoolboxbackground" style="height: '+(3*20+40)+'px" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    Element.insert($("actmn_tenantstoolbox"), menubackground);
    
    menubackground = '<table id="studentsbackground" style="height: '+(2*20+40)+'px" class="nlsmenubackground"><tbody><tr><td/></tr></tbody></table>';
    Element.insert($("actmn_students"), menubackground);
}

function validateViewing(form) {
	
	if (form.name.value == "") {
		alert("Please enter a name!");
		return false;
	}
		
	if (form.tel.value == "") {
		alert("Please enter a telephone number!");
		return false;
	}
}

function urlRedirect(url) {
    setTimeout("top.location.href = '" + url +"'",1);
}

function changeStatsPage(sortby, orderby) {
	urlRedirect('/staff/property_statistics.php?'+'sortby='+sortby+'&orderby='+orderby);
}

function toggleDisplay(elementId) {
	if ($(elementId).style.display == "none") {
		$(elementId).show();
	}
	else {
		$(elementId).hide();
	}
}

function togglePopupDisplay(elementId, source) {
	if ($(elementId).style.display == "none") {
		$(elementId).show();
		$(source).addClassName("highlightgold");
	}
	else {
		$(elementId).hide();
		$(source).removeClassName("highlightgold");
	}
}

var homefor = "sale";
function SelectHomeFor(selected, popup) {
	if (homefor != selected) {
		RemoveButtonHighlight(homefor);
		
		homefor = selected;
		
		HighlightButton(homefor);
				
		if (homefor == "rent") {
			if (!popup) {
				$("searchformcontainer").className = "yellowboxbackground";
			}
			else {
				$("searchformcontainer").className = "biggersearchpopup";
			}
			$("renttip").show();
			$("rentrow").show();
			$("pricerowrent").show();
			$("pricerowsale").hide();
			$("pricerowstudent").hide();
			$("rentlocations").show();
			$("salelocations").hide();
			$("studentlocations").hide();
		}
		else {
			if (!popup) {
				$("searchformcontainer").className = "yellowboxbackground";
			}
			else {
				$("searchformcontainer").className = "searchpopup";
			}
			$("renttip").hide();
			$("rentrow").hide();
			
			if (homefor == "student") {
				$("pricerowstudent").show();
				$("pricerowrent").hide();
				$("pricerowsale").hide();
				$("studentlocations").show();
				$("salelocations").hide();
				$("rentlocations").hide();
			}
			else {
				$("pricerowrent").hide();
				$("pricerowstudent").hide();
				$("pricerowsale").show();
				$("rentlocations").hide();
				$("salelocations").show();
				$("studentlocations").hide();
			}
		}
	}
}

var displaytype = "list";
function SelectDisplayType(selected) {

	if(displaytype != selected) {
		RemoveButtonHighlight(displaytype);
		
		displaytype = selected;
		
		HighlightButton(displaytype);
	}
}

var saleAreas = new Array();
var rentAreas = new Array();
var studentAreas = new Array();

function addSaleArea(areaID) {
	if (areaID == 0) {
		if (saleAreas.indexOf(0) == -1) {
			$("allsaleareas").className = "location areaselected";
			saleAreas.push(0);
		}
		else {
	    	saleAreas = saleAreas.without(0);
	    	$("allsaleareas").className = "location";
	    }
	}
	else {
		if (saleAreas.indexOf(areaID) == -1) {
			if ($("sarea"+areaID) != null) {
				$("sarea"+areaID).className = "location areaselected";
				saleAreas.push(areaID);
			}
	    }
	    else {
	    	saleAreas = saleAreas.without(areaID);
	    	$("sarea"+areaID).className = "location";
	    }
    }
}

function addRentArea(areaID) {
	
	if (areaID == 0) {
		if (rentAreas.indexOf(0) == -1) {
			$("allrentareas").className = "location areaselected";
			rentAreas.push(0);
		}
		else {
	    	rentAreas = rentAreas.without(0);
	    	$("allrentareas").className = "location";
	    }
	}
	else {
		if (rentAreas.indexOf(areaID) == -1) {
			if ($("rarea"+areaID) != null) {
				rentAreas.push(areaID);
			    $("rarea"+areaID).className = "location areaselected";
			}
	    }
	    else {
	    	rentAreas = rentAreas.without(areaID);
	    	$("rarea"+areaID).className = "location";
	    }
    }
}

function addStudentArea(areaID) {
	if (areaID == 0) {
		if (studentAreas.indexOf(0) == -1) {
			$("allstudentareas").className = "location areaselected";
			studentAreas.push(0);
		}
		else {
	    	studentAreas = studentAreas.without(0);
	    	$("allstudentareas").className = "location";
	    }
	}
	else {
		if (studentAreas.indexOf(areaID) == -1) {
			if ($("starea"+areaID) != null) {
				studentAreas.push(areaID);
			    $("starea"+areaID).className = "location areaselected";
			}
	    }
	    else {
	    	studentAreas = studentAreas.without(areaID);
	    	$("starea"+areaID).className = "location";
	    }
    }
}

function getAreas(areas) {
	var list = "";
	
	
	areas.each( function (area, index) {
		if (index == 0)
			list += area;
		else 
			list += ","+area;
	});
	
	return list;
}

function DoSearch() {

	var saletype = (homefor == "sale") ? 0 : 1;

	var minprice = 0;
	var maxprice = 0;
	
	var area_id = 0;
	switch (homefor) {
		case "sale" : 
			area_id = getAreas(saleAreas); 
			minprice = $("minpricesale").getValue();
			maxprice = $("maxpricesale").getValue();
			break;
		case "rent" : 
			area_id = getAreas(rentAreas); 
			minprice = $("minpricerent").getValue();
			maxprice = $("maxpricerent").getValue();
			break;
		case "student" : 
			area_id = getAreas(studentAreas); 
			minprice = $("minpricestudent").getValue();
			maxprice = $("maxpricestudent").getValue();
			break;
	}
	
	var branch = "main";
	if ($("branch") != null) {
		branch = $("branch").getValue();
	}
	
	var beds = $("beds").getValue();
	var type = $("type").getValue();
	var searchtype = (homefor == "student") ? 0 : 1; 
	var parking = 9;
	if (homefor == "rent") {
		if ($("parkingyes").checked) {
			parking = 1;
		}
		else if ($("parkingno").checked) {
			parking = 0;
		}
	}
	
	var url = "";
	
	if (displaytype == "list") {
		url += "/propertysearch.php?";
		
		url += "area_id="+area_id;
		url += "&minprice="+minprice;
		url += "&maxprice="+maxprice;
		url += "&beds="+beds;
		url += "&type="+type;
		url += "&saletype="+saletype;
		url += "&searchtype="+searchtype;		
		url += "&parking="+parking;
		url += "&branch="+branch;
		location.href = url;
	}
	else {
	
		if (saletype == 0) {
			url += "/search_map.html";
		}
		else {
			if (searchtype == 0) {
				url += "/search_map_student.html";
			}
			else {
				url += "/search_map_lettings.html";
			}
		}
		location.href = url;
	}
}

function DoQuickSearch() {

	if ($("proptypesale").checked) {
		var saletype = 0;
	}
	else if ($("proptyperent").checked) {
		var saletype = 1;
	}
	else {
		var saletype = 2;
	}	
	
	var searchtext = $("searchinput").getValue();
	
	var url = "/propertysearch.php?searchtext="+searchtext;
	
	if (saletype != 2)
		url += "&saletype="+saletype;
		
	location.href = url;
}

function HighlightButton(name) {
	if ($(name+"button") != null) {		
		var left = $(name+"button").getElementsByClassName("greybuttonleft");
		var middle = $(name+"button").getElementsByClassName("greybuttonmiddle");
		var right = $(name+"button").getElementsByClassName("greybuttonright");
	
		if (left.length > 0) {
			left[0].className = "orangebuttonleft";
			middle[0].className = "orangebuttonmiddle";
			right[0].className = "orangebuttonright";
		}
	}
}

function RemoveButtonHighlight(name) {		
	if ($(name+"button") != null) {		
		var left = $(name+"button").getElementsByClassName("orangebuttonleft");
		var middle = $(name+"button").getElementsByClassName("orangebuttonmiddle");
		var right = $(name+"button").getElementsByClassName("orangebuttonright");
	
		if (left.length > 0) {
			left[0].className = "greybuttonleft";
			middle[0].className = "greybuttonmiddle";
			right[0].className = "greybuttonright";
		}
	}
}

var infoShowing = false;
function ToggleMoreInfo() {
	if (!infoShowing) {
		$("moreinfopin").className = "pinexpanded";
		$("moreinfo").show();
		infoShowing = true;
	}
	else {
		$("moreinfopin").className = "pincollapsed";
		$("moreinfo").hide();
		infoShowing = false;
	}
}



function stopPropagation(event) {
	event.stopPropagation();
}

function checkEnter(e)
{

	if (e != null) {
		var keynum;
		var keychar;
		var numcheck;
		
		if(window.event) // IE
		  {
		  keynum = e.keyCode;
		  }
		else if(e.which) // Netscape/Firefox/Opera
		  {
		  keynum = e.which;
		  }
		  
		if (keynum == 13) {
			return true
		}
		else {
			return false;
		}
	}
}

var noticesearch = 0;
function DoneNotice() {
	var url = '/noticesearch.php';
	// notice the use of a proxy to circumvent the Same Origin Policy.
	
	new Ajax.Request(url, {
	  method: 'get',
	  onSuccess: function(transport) {
	  	noticesearch = 1;
	  }
	});
}


function alpha(e) {
	var k;
	document.all ? k = e.keyCode : k = e.which;
	
	return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8);
}

function numbersonly(myfield, e, dec)
{
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
	    (key==9) || (key==13) || (key==27) )
	   return true;
	
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;
	
	// decimal point jump
	else if (dec && (keychar == "."))
	   {
	   myfield.form.elements[dec].focus();
	   return false;
	   }
	else
	   return false;
}

function popup(mylink, windowname)
{
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
	   href=mylink;
	else
	   href=mylink.href;
	window.open(href, windowname, 'width=600,height=700,scrollbars=yes');
	return false;
}