// required to be declared up here...
var slideshow_status = 0;

function highlight_on(row,color){
  row.style.backgroundColor = color;
}
function highlight_off(row,color){
  row.style.backgroundColor = color;
}
function winpopopen(psrc,width,height){
winpop=window.open(basedir + "winpop.php?pic="+psrc+"&width="+width+"&height="+height,"ConsoleWindow","toolbar=no,,width="+(width+10)+",height="+(height+35)+",directories=no,status=no,scrollbars=no,resize=no,menubar=no");
winpop.focus();
}
function help(help_file){
winpop=window.open("help/"+help_file,"ConsoleWindow","toolbar=no,,width=400,height=250,directories=no,status=no,scrollbars=yes,resize=no,menubar=no");
winpop.focus();
}
function register(){
winpop=window.open("register.php","ConsoleWindow","toolbar=no,,width=400,height=450,directories=no,status=no,scrollbars=yes,resize=no,menubar=no");
winpop.focus();
}


function mouseon(cell){
  if(document.all){
  cell.style.background = '#0066CC';
  }
}
function mouseout(cell){
  if(document.all){
  cell.style.background = '';
  }  
}

//-----------FUNCTIONS TO CONTROL THE MOVEMENT OF THE SEARCH DIV----------

x1=-270
x=0
y=0
var direction;
function search_move(x,y){
		if (document.all){
			document.all['search_form'].style.pixelLeft=x;
			//document.all['search_form'].style.pixelTop =y;
		}
}

function search_moveit(dir){
	if (document.all){
		
		sf = document.all.search_form;
		hd = document.all.hider;
		hd.style.visibility = 'visible'
		direction = dir;
		search_mover()
	}else if (document.layers){
		
		hd = document.layers.hider
		sf = hd.document.layers.search_form
		if (dir == 'in'){
			hd.visibility = 'visible'
			sf.visibility = 'visible'
			sf.left = 0;
		}else{
			hd.visibility = 'hidden'
			sf.visibility = 'hidden'
		}
	}else  if (document.getElementById){
		hd = document.getElementById("hider");
		sf = document.getElementById("search_form");
		if (dir == 'in'){
			sf.style.left = 0;
			hd.style.visibility = 'visible'
			sf.style.visibility = 'visible'
		}else{
			hd.style.visibility = 'hidden'
			sf.style.visibility = 'hidden'
		}		
	}
}

function search_mover(){
	if (direction == 'in'){
		if(x1 < 15){
			search_move(x1,y)
			x1 = x1 + 15;
			setTimeout('search_mover()',2)
		}else{
			search_finished()
		}
	}else{
		if(x1 > -300){
			x1 = x1 - 15;
			search_move(x1,y)
			setTimeout('search_mover()',2)
		}else{
			search_finished()
		}
	}
}

function search_finished(){
	start_clouds();
	if (direction == 'out'){
		document.all.hider.style.visibility = 'hidden'
	}
}
//--------------------------------------------------------------
function mouseover_thumb(id){
	clearTimeout(t);
	document.images.photo.src = p[id].src
}

function mouseout_thumb(id){
	if (slideshow_status == 1){
		runSlideShow();
	}
}

function runSlideShow(){
	if (document.all){
		document.images.photo.style.filter="blendTrans(duration=2)"
		document.images.photo.style.filter="blendTrans(duration=crossFadeDuration)"
		document.images.photo.filters.blendTrans.Apply()      
	}
	if (c.length > 0){
		this_stock_id = id[j]
		cap = document.getElementById("slideshow_caption");
		cap.style.visibility = "visible";
		cap.innerHTML= "Click here for more information about this featured "+c[j]+ " property";
		cap.onclick = function (){ document.location = property_detail_page + "?stock_id="+this_stock_id };
	}
	
	document.images.photo.src = p[j].src
	if (document.all){
		document.images.photo.filters.blendTrans.Play()
	}
	
	j = j + 1
	pl = p.length;
	if (j > (pl-1)) j=0
	clearTimeout(t);
	t = setTimeout('runSlideShow()', slideShowSpeed)

}

function slideshowStop(){
	if (slideshow_enabled == 0) return;
	if (t) clearTimeout(t);
	slideshow_status = 0;
}

function slideshowStart(){
	if (slideshow_enabled == 0) return;
	if (slideshow_status != 1){
		slideshow_status = 1;
		runSlideShow();
	}
}

function slideshowHelp(){
	if (slideshow_enabled == 0) return;
	if (help == 0){
		help = 1;
		slideshowStop();
		document.images.photo.src = imghelp.src;
	}else{
		help = 0;
		slideshowStart();
	}
}

cookieName = "PROPERTYLIST";

function savelist(property_id){
	property_id = parseInt(property_id);
	oldcookie= getCookie(cookieName);
	if (oldcookie == null) {
		oldcookie = "";
	}else{
		oldcookie = oldcookie + ",";
	}
	newcookie = oldcookie + property_id;
	newcookieBits = newcookie.split(",");
	newcookie = removeDuplicates(newcookieBits).join(",");
	setCookie(cookieName, newcookie, 100);
	alert("This property has been added to your favourites");
}

function removeDuplicates(a){
  var seen = new Array();
  var new_array = new Array();
  b=0;
  for (var i = 0; i < a.length; ++i) {
    if (!seen[a[i]]) {
	  	seen[a[i]] = true;
		new_array[b] = a[i];
		b++;
	 }
  } 
	return new_array;
}

function removeSingleListing(property_id){
	var arr = new Array;
	b=0;
	cookieBits = getCookie(cookieName).split(",");
	for (var i = 0; i < cookieBits.length; i++){
		if (property_id != cookieBits[i]){
			arr[b] = cookieBits[i]
			b++;
		}
	}
	newcookie = arr.join(",");
	setCookie(cookieName, newcookie, 100);
	alert("This property has been removed from your favourites list");
	document.location.reload();
}

 function setCookie (name, value, days) {
	var expires = new Date(); 
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	//var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	expires.setTime(expires.getTime() + days*24*60*60*1000); 
	
	document.cookie = name + "=" + escape (value) +
	  ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	  ((path == null) ? "" : ("; path=" + path)) +
	  ((domain == null) ? "" : ("; domain=" + domain)) +
	  ((secure == true) ? "; secure" : "");
 }

function getCookie (name) {
var dc = document.cookie;
var cname = name + "=";
var clen = dc.length;
var cbegin = 0;

while (cbegin < clen) { 
var vbegin = cbegin + cname.length;

if (dc.substring(cbegin, vbegin) == cname) { 
var vend = dc.indexOf (";", vbegin);
if (vend == -1) vend = clen;

return unescape(dc.substring(vbegin, vend));
}

cbegin = dc.indexOf(" ", cbegin) + 1;

if (cbegin== 0) break;
}
return null;
}

function showlist(){
	alert (getCookie(cookieName));
}

function deletelist(){
	DeleteCookie(cookieName);
	document.location.reload();
	
}

function DeleteCookie (name) {  
var exp = new Date();  
exp.setTime (exp.getTime() - 1);  
var cval = getCookie (name);  
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

// end of cookie functions

function printable(stock_id){
	printWin = window.open("printable.php?stock_id="+stock_id,"PrintableVersion","toolbar=no,width=680,height=480,directories=no,status=no,scrollbars=yes,resize=no,menubar=no");
	printWin.focus();
}

function favourites_printable(stock_id){
	printWin = 	window.open("favourites_printable.php","PrintableVersion","toolbar=no,width=640,height=480,directories=no,status=no,scrollbars=yes,resize=no,menubar=no");
	printWin.focus();
}

function rental_printable(stock_id){
	printWin = window.open("rental_printable.php?stock_id="+stock_id,"PrintableVersion","toolbar=no,width=640,height=480,directories=no,status=no,scrollbars=yes,resize=no,menubar=no");
	printWin.focus();
}

function isemail(email_addr){
  var addressPattern = 
    /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,10}$/;
  return addressPattern.test(email_addr);	
}

function validate(frm){
	var errors;
	var error_text = "";
	for (field in val){
		if (frm[field].style){
			frmstyle = frm[field].style
		}else{
			frmstyle = frm[field];
		}
		field_val = frm[field].value
		if (val[field] == "date"){
			if (!is_date(field_val)){	
				errors = 1;
				error_text += "\n-Dates must have the format 'dd/mm/yyyy'"
				frmstyle.backgroundColor = highlight;
			}else{
				frmstyle.backgroundColor = "";
			}
		}else if (val[field] == "email"){
			if (!isemail(field_val)){	
				errors = 1;
				error_text += "\n-The email address does not appear to be valid"
				frmstyle.backgroundColor = highlight;
			}else{
				frmstyle.backgroundColor = "";
			}
		}else{
			if (field_val.length < val[field]) {
				errors = 1;
				frmstyle.backgroundColor = highlight;
			}else{
				frmstyle.backgroundColor = "";
			}
		}
	}
	if (errors){
		alert ("ALERT:\n\nYou have not completed all of the required fields in the form. \nThe required fields have been highlighted for you\n\nPlease complete these now\n" + error_text)
		return false;
	}
}

function is_date (string) {
  var datePattern = 
    /^[0-9]{1,2}\/[0-9]{1,2}\/([0-9]{2}|[0-9]{4})$/;
  return datePattern.test(string);
}
  function restoreStyles(){
    inputList = document.getElementsByTagName("INPUT");
    for(i=0;i<inputList.length;i++)
      inputList[i].style.backgroundColor = "";
    selectList = document.getElementsByTagName("SELECT");
    for(i=0;i<selectList.length;i++)
      selectList[i].style.backgroundColor = "";
  }


var val = Array();
var highlight = "#FFCC99";

function check_contact(theform){
	error = ""
	if (!theform.name.value) error += "Please enter your name\n";
	if (!theform.phone.value) error += "Please enter your phone number\n";
	if (!theform.email.value) error += "Please enter your email address\n";
	if (!theform.message.value) error += "Please enter a message\n";
	
	if (error){
		alert ("Error\n\n" + error);
		return false;
	}else{
		return true;
	}
}

function set_body_bg(){
	if (document.body.clientWidth > 990){
		document.body.className = "wideBodyBg";
	}else{
		document.body.className = "defaultBodyBg";		
	}
}
function body_onload(){
	set_body_bg();
	if (slideshow_status == 1){
		runSlideShow();
	}
}

function populate(parentSel,parentSelVal,childSel,arrChildren,selectedVal) {
	//alert(var_dump(selectedVal));
	// next two lines are specific - rest is generic
	if (parentSel){
		if (parentSel.id == "region_id")
			depopulate(document.getElementById('realenz_suburb_no'))
	}
	
	if (parentSel)
		parentSelVal = parseInt(parentSel.options[parentSel.options.selectedIndex].value);
	
	if (parentSelVal) {
		childSel.disabled = false;
		childSelLength = arrChildren[parentSelVal].length;
		childSel.options.length = childSelLength + 1;
		childSel.options[0] = new Option("All","");
		arrChildrenElements = new Array();
		count = 0;
		x="";
		for (i=0; i < childSelLength; i++) {
			arrChildrenElements = arrChildren[parentSelVal][i].split("|");
			childSel.options[i+1] = new Option(arrChildrenElements[1],arrChildrenElements[0]);
			if (selectedVal){
				if (in_array(arrChildrenElements[0],selectedVal)){
					childSel.options[i+1].selected = true;
				}
			}
		}	
	} else {
		depopulate(childSel)
	}
	//alert(count);
}

function depopulate(sel){
	sel.options.length = 1;
	sel.disabled = true;
}

function in_array(needle,haystack){
	var retVal = false;
	for (z in haystack){
		if (needle == haystack[z])
			retVal = true;
	}
	return retVal
}
	
function var_dump(arr){
	var out = "";
	if (!arr)
		return false;
	for (b in arr){
		out += b + " = " + arr[b] + "\n";
	}
	return out;
}

var popUpWin = 0;
function open_multi_suburbs(){
  if(popUpWin) 
    if(!popUpWin.closed) popUpWin.close();
	selected_suburbs = document.forms["search01"].multi_subs.value;
	val_region_id = document.getElementById("region_id").value;
  popUpWin = open(basedir + 'select_suburbs.php?region_id='+val_region_id+"&selected_suburbs="+selected_suburbs, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width=520,height=330,left=100, top=100');
  popUpWin.focus();
}

function add_multi_subs(subs){
	if (subs){
		of = document.forms["search01"];
		of.multi_subs.value = subs;
		suburbSel = of.suburb_id;
		suburbSel.options.length = 1;
		suburbSel.options[0] = new Option("MULTIPLE","");
	}
}

function select_no_subs(){
	document.forms["search01"].multi_subs.value = "";
}