var defaultEmptyOK = false

function loadChecks(propTypes){
	if(propTypes){
		sectionHideBed(propTypes)
	}
}

function sectionHideBed(propTypes) {
	var chosenpropType = propTypes.options[propTypes.selectedIndex].value;
	//alert(chosenpropType)
	if(chosenpropType=='Section'){
		document.getElementById('lowerPropForm').style.display='none'
	}else{
		document.getElementById('lowerPropForm').style.display='block'
	}
}

function landToggle1(){
		document.propertyform.landsize_m2.value=''
		document.propertyform.landsize_acre.value=''
}
function landToggle2(){
		document.propertyform.landsize_ha.value=''
		document.propertyform.landsize_m2.value=''
}
function landToggle3(){
		document.propertyform.landsize_ha.value=''
		document.propertyform.landsize_acre.value=''
}


function toggleLeaseOff()
{//alert(document.getElementById('leaseRow1').style.display)
		document.getElementById('leaseRows').style.display='none'
}


function toggleLeaseOn()
{//alert(document.getElementById('leaseRow1').style.display)
		document.getElementById('leaseRows').style.display='block'
}


function crossOrStrataCommentsOff(){
		document.getElementById('crossOrStrataComments').style.display='none'
}

function crossOrStrataCommentsOn(){
		document.getElementById('crossOrStrataComments').style.display='block'
}


function clearCrossOrStrata(){
//clears all fields relating to strata or cross leasing if user choosesfreehold ot leasehold options
		document.propertyform.cross_or_strata[0].checked=false 
		document.propertyform.cross_or_strata[1].checked=false
		document.propertyform.crosslease_or_strata_comments.value='' 
}

function clearFreeOrLeasehold(){
//clears all fields relating to strata or cross leasing if user choosesfreehold ot leasehold options
		document.propertyform.freehold_or_leasehold[0].checked=false 
		document.propertyform.freehold_or_leasehold[1].checked=false
		toggleLeaseOff();
}

function clearSwimmingText(){
	document.propertyform.swimming_descript.value=''
}

function checkSwimNoChecked(){
	if(document.propertyform.swimming[1].checked==true){
		alert("You cannot fill the swimming pool description \n field as you have ticked 'no' for swimming pool.")
		clearSwimmingText();
	} 	
}


function doupload(){
	document.getElementById('pleaseWait').style.display='block'
	setTimeout('document.images["pbar"].src = "/images/wait.gif"', 200);
	document.upload_form.submit()
}


function clearAgeRadios(propform){
		propform.structure_age[0].checked=false 
		propform.structure_age[1].checked=false 
}

function clearAgeRightInput(propform){
		propform.structure_age_above_1year.value=''
}

function clearFloorRadios(propform){
		propform.storeys[0].checked=false 
		propform.storeys[1].checked=false 
}

function clearFloorRightInput(propform){
		propform.storeys_other.value=''
}

function clearNoGarage(propform){
	propform.garagenone.checked=false 

}

function clearGarageItems(propform){
	propform.garagesingle.checked=false
	propform.garagedouble.checked=false
	propform.garagetandem.checked=false
	propform.garagemorethantwo.checked=false
	propform.garagecarport.checked=false
	propform.garageinternalaccess.checked=false
	propform.garagelockup.checked=false
	propform.garagenone.checked=false
}

function clearLevels1(propform){
	propform.level_below_road.checked=false
	propform.level_with_road.checked=false
}
function clearLevels2(propform){
	propform.level_above_ground.checked=false
	propform.level_with_road.checked=false
}
function clearLevels3(propform){
	propform.level_above_ground.checked=false
	propform.level_below_road.checked=false
}

function alertNonnumeric(targetField){
	if( (!isInteger(targetField.value) || targetField.value==0)   && targetField.value!=''  ){
		alert("Please enter a number greater than zero, or leave this field empty")
		targetField.focus()
		targetField.focus()
		exit
	}
}

function alertNonnumeric2(targetField){
	if(!isInteger(targetField.value) ){
		alert("Please enter a number greater than zero, or leave this field empty")
		targetField.focus()
		targetField.focus()
		exit
	}
}

//*******************************************************************************
// Validation functions

var defaultEmptyOK = false

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}
function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}
var whitespace = " \t\n\r";
// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// Removes all characters which appear in string bag from string s.

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

// Removes all whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripWhitespace (s)

{   return stripCharsInBag (s, whitespace)
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}



function trim (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


function checkDelPage(pageid){
	if (confirm("Are you sure you want to delete this entire page and all its contents?"))
	{
		location.href='nz_info.php?task=del_page&pageid='+pageid
	}
	else{
	return false
	}
}

function checkDelSub(subid,pageid){
	if (confirm("Are you sure you want to delete this subsection?"))
	{
		location.href='nz_info.php?task=del_subhead&subid='+subid+'&pageid=pageid'
	}

}


//**************************************************************************************************************


function sendXMLHttpRequest(url,list) {
//alert(list)
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
		if(list=="suburb")
           	req.onreadystatechange = processReqChangeSuburb;
		else
			req.onreadystatechange = processReqChangeArea;
        req.open("GET", url, true);
        req.send(null);

    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
		//alert("oo")
        if (req) {
			if(list=="suburb"){
            	req.onreadystatechange = processReqChangeSuburb;}
			else{
				req.onreadystatechange = processReqChangeArea;}
            req.open("GET", url, true);
            req.send();
        }
    }
}


function processReqChangeSuburb() 
{ 
    if (req.readyState == 4) {
        // only if "OK"
		
        if (req.status == 200) {
            // ...processing statements go here...
			//alert(req.responseText)
			document.getElementById("SuburbSelectList").innerHTML=req.responseText
        } else {
            alert("There was a problem retrieving the data:\n" + req.statusText);
        }
    }
}

function processReqChangeArea() 
{ 
    if (req.readyState == 4) {
        // only if "OK"
		
        if (req.status == 200) {
            // ...processing statements go here...
			//alert(req.responseText)
			document.getElementById("AreaSelectList").innerHTML=req.responseText
        } else {
            alert("There was a problem retrieving the data:\n" + req.statusText);
        }
    }
}


function confirmAction(textStr,targetUrl){
	proceed=window.confirm(textStr)
	if(proceed==true)
		window.location=targetUrl
}

//newsletter
//************************************************

function validateNews(thisform){

	if( thisform.title.value==""){
		alert("Please enter a title")
		return false
	}
	
		if(thisform.lettercontent.value==""){
		alert("Please enter newsletter content")
		return false
	}
	
	return true
	
}
function checkNonEmptyPrvw(nid,target){
	if( isEmpty(nid)){
		alert("First choose a newsletter to preview")
	}
	else
	{
		location.href="/admin/admin_newsletter.php?nid="+nid+"&task=showprvw&targ="+target
	}
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


function checkDelLetter(nid,targ){

	if( isEmpty(nid)){
		alert("First choose a newsletter to delete")
	}

	else if (confirm("Are you sure you want to delete this newsletter?"))
	{
		location.href="/admin/admin_newsletter.php?nid="+nid+"&task=delletter&targ="+targ
	}
}




function checkSendNews(thisform){
	if (confirm("The newsletter is about to be sent to all your subscribers. Do you want to proceed and send the newsletter?"))
	{
		thisform.submit()
	}
	return false
}


function confirmAction(url,question){
	if (confirm(question))
	{
		window.location.href=url
	}
	else
	return false

}

function confirmAction2(question){
	if (confirm(question))
	return true
	else
	return false

}


function MM_openBrWindow(theURL,winName)
 { //v2.0
   var newWindow = window.open(theURL,winName,'width=900,height=800,top=555,left=60,resizable=1,scrollbars=1');
   newWindow.focus();
}

function help(theURL,winName)
 { //v2.0
   var newWindow = window.open(theURL,winName,'width=300,height=250,top=60,left=60,resizable=1,scrollbars=1');
   newWindow.focus();
}



//************************************************

var msgs=new Array(
'Are you sure you want to delete this REA/REAmc and all their properties, images and enquiry records?'

)


var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{	// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 	// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


function validateEnquiry(){

	//alert(document.enqForm.enq_name.value)
	var errMsg=''
	if(isEmpty(trim(document.enqForm.enq_name.value))){
		errMsg+="Please enter a name.<br>"
	}

	if( isEmail(document.enqForm.enq_email.value)==false){
		errMsg+="Please enter a valid email address.<br>"
	}
	
	/*if(isEmpty(trim(document.enqForm.enq_phone.value))){
		errMsg+="Please enter a phone number.<br>"
	}*/
	
	if(isEmpty(trim(document.enqForm.enq_question.value))){
		errMsg+="Please enter your enquiry question.<br>"
	}
		
	if(isEmpty(errMsg)){
		return true
	}else{
		document.getElementById('errMsg').innerHTML=errMsg
		errMsg=''
		return false
	}
	//trim (document)
	//isEmpty(s)
}

function validateBrokerEnquiry(){

	//alert(document.enqForm.enq_name.value)
	var errMsg=''
	if(isEmpty(document.brokerEnqForm.enq_name.value)){
		errMsg+="Please enter a name.<br>"
	}
	

	if( isEmail(document.brokerEnqForm.enq_email.value)==false &&
		document.brokerEnqForm.enq_phone.value==''){
		errMsg+="Please enter either a valid email or phone number.<br>"
	}
	
	if(isEmpty(document.brokerEnqForm.enq_question.value)){
		errMsg+="Please enter your enquiry question.<br>"
	}
		
	if(isEmpty(errMsg)){
		return true
	}else{
		document.getElementById('errMsg').innerHTML=errMsg
		errMsg=''
		return false
	}
	//trim (document)
	//isEmpty(s)
}

//**********************************************************************************
//   FADERS
//

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}



//Loan Calculator ******************************

function showpay() {
 if ((document.calc.loan.value == null || document.calc.loan.value.length == 0) ||
     (document.calc.months.value == null || document.calc.months.value.length == 0)
||
     (document.calc.rate.value == null || document.calc.rate.value.length == 0))
 { document.calc.pay.value = "Incomplete data";
 }
 else
 {
 var princ = document.calc.loan.value;
 var term  = document.calc.months.value;
 var intr   = document.calc.rate.value / 1200;
 total = princ * intr / (1 - (Math.pow(1/(1 + intr), term)));
 document.calc.pay.value = round(total,2)
 }

// payment = principle * monthly interest/(1 - (1/(1+MonthlyInterest)*Months))

}

function round(number,X) {
// rounds number to X decimal places, defaults to 2
    X = (!X ? 2 : X);
    return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}



function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    if (isWhitespace(s)) return false;
    var i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


function checkEmail (theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false)) 
       //return warnInvalid (theField, iEmail);
	   return false
    else return true;
}


function goAdClick(goUrl,adid){

window.open('go.php?goUrl=' + goUrl +'&adid='+adid,'newWin','resizable=1,status=1,menubar=1,toolbar=1,scrollbars=1,location=1,directories=1,width=350,height=350,top=60,left=60')

}


function isURL(urlStr){

if(urlStr==''){
	alert("Please enter a URL in the format: www.websitename.com")
	return false
}

}




		






