var accordion = null;
window.addEvent('domready', function() {
								 
	accordion = new Accordion('img.atStart', 'div.atStart', $('accordion'));
	
	var currentSlider = listingType;
	
	if(listingType==1) {
		$('btn-for-sale').setProperty('src', '/images/btn-for-sale-o.gif');
		$('btn-for-sale').fireEvent('click');
		accordion.display(0);
	};
	if(listingType==2) {
		$('btn-for-auction').setProperty('src', '/images/btn-for-auction-o.gif');
		$('btn-for-auction').fireEvent('click');
		accordion.display(1);
	};
	if(listingType==3) {
		$('btn-for-rent').setProperty('src', '/images/btn-for-rent-o.gif');
		$('btn-for-rent').fireEvent('click');
		accordion.display(2);
	};
	if(listingType==4) {
		$('btn-for-business').setProperty('src', '/images/btn-business-o.gif');
		$('btn-for-business').fireEvent('click');
		accordion.display(3);
	};
	
	$('btn-for-sale').addEvent('click', function(e){
		$('btn-for-sale').setProperty('src', '/images/btn-for-sale-o.gif');
		if(currentSlider=='2') { $('btn-for-auction').setProperty('src', '/images/btn-for-auction.gif'); }
		if(currentSlider=='3') { $('btn-for-rent').setProperty('src', '/images/btn-for-rent.gif'); }
		if(currentSlider=='4') { $('btn-for-business').setProperty('src', '/images/btn-business.gif'); }
		currentSlider = '1';
	});
	
	$('btn-for-auction').addEvent('click', function(e){
		$('btn-for-auction').setProperty('src', '/images/btn-for-auction-o.gif');
		if(currentSlider=='1') { $('btn-for-sale').setProperty('src', '/images/btn-for-sale.gif'); }
		if(currentSlider=='3') { $('btn-for-rent').setProperty('src', '/images/btn-for-rent.gif'); }
		if(currentSlider=='4') { $('btn-for-business').setProperty('src', '/images/btn-business.gif'); }
		currentSlider = '2';
	});
	
	$('btn-for-rent').addEvent('click', function(e){
		$('btn-for-rent').setProperty('src', '/images/btn-for-rent-o.gif');
		if(currentSlider=='1') { $('btn-for-sale').setProperty('src', '/images/btn-for-sale.gif'); }
		if(currentSlider=='2') { $('btn-for-auction').setProperty('src', '/images/btn-for-auction.gif'); }
		if(currentSlider=='4') { $('btn-for-business').setProperty('src', '/images/btn-business.gif'); }
		currentSlider = '3';
	});
	
	$('btn-for-business').addEvent('click', function(e){
		$('btn-for-business').setProperty('src', '/images/btn-business-o.gif');
		if(currentSlider=='1') { $('btn-for-sale').setProperty('src', '/images/btn-for-sale.gif');  }
		if(currentSlider=='2') { $('btn-for-auction').setProperty('src', '/images/btn-for-auction.gif'); }
		if(currentSlider=='3') { $('btn-for-rent').setProperty('src', '/images/btn-for-rent.gif');  }
		currentSlider = '4'; 
	});
	
	if($('form')&&!window.ie) {
	
		check = new FormCheck('form', {
			display : {
				fadeDuration : 500,
				tipsOffsetLeft : 130
			}
		});
	
	} else if($('form')&&window.ie) {
		// check to see is this is the search form and no agent is found
		if($('state')&&!$('agentDiv')) {
			$('form').addEvent('submit', function(e) {
				e = new Event(e);
					if(!$('state').getProperty('value')) {
						var cmd = "Please select a <strong>State</strong> to continue...";
						$('outPutCmd').toggleClass('red');
						if(!$('pTag')) {
							var p = new Element('p').injectInside('outPutCmd');
							p.setProperty('id', 'pTag');
							p.setHTML(cmd);	
						} else {
							$('pTag').setHTML(cmd);
						}
						new Fx.Scroll(window).scrollTo(0,$('outPutCmd').getCoordinates().top);
						$('state').removeClass('fieldRequired');
						$('state').addClass('fieldRequired');
						new dm_popelement({'target': 'outPutCmd','popdelay': 100,'popstyle': 'vert','closedheight': '0px','closedelay': 10000});
						return false;
					} else {
						return true;	
					};
				e.stop();
			});
		}
	}
	
});

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   };
   
function swapMenu(id)
{
	var a = document.getElementById(id);
	if(a.className=='daMenu') {
		a.className = 'daMenuOver';
	} else if(a.className=='daMenuOver') {
		a.className = 'daMenu';
	}
};

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
};

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
};

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
};

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
};

function swapThatDiv(div,id) {

	thisDiv 		= document.getElementById(div);
	thisImg 		= document.getElementById(id);
	
	var menuOn 		= '/images/menuItem_on.jpg';
	var menuOff 	= '/images/menuItem.jpg';
	
	var dynMenus 	= new Array('menu03','menu04','menu05','menu06');
	var dynImg 		= new Array('img03','img04','img05','img06');
	
	var thisDynMenu;
	var thisDynImg;
	
	if(thisDiv.style.display=='none') {
	
		thisDiv.style.display 	= 'inline';
		thisImg.src 			= menuOn;
		thisImg.width 			= '11';
		thisImg.height 			= '10';
		
		if(div=='menu03'||div=='menu04'||div=='menu05'||div=='menu06') {
		
			for(i=0;i<=dynMenus.length;i++) {
			
				if(dynMenus[i]!=div) {
				
					if(dynMenus[i]) {
					
						thisDynMenu = document.getElementById(dynMenus[i]);
						thisDynImg	= document.getElementById(dynImg[i]);
						
						thisDynMenu.style.display	= 'none';
						thisDynImg.src 				= menuOff;
						thisDynImg.width 			= '10';
						thisDynImg.height 			= '11';
					
					}
				
				}
			
			}
		
		}
		
	} else {
	
		thisDiv.style.display = 'none';
		thisImg.src = menuOff;
		thisImg.width 			= '10';
		thisImg.height 			= '11';
	
	}

};
	
function popMenu(sel,target){
	var t=document.getElementById(target);
	var gcat=sel.options[sel.selectedIndex];
	var gtypes=grouptypes[gcat.value];
	var topt=null;
	if(t){
		for(i=t.length-1;i>0;i--){
			t.remove(i);
		}
		for(j in gtypes){
			topt=document.createElement('option');
			topt.value=j;
			topt.text=gtypes[j];
			try{
				t.add(topt,null);
			}catch(e){
				t.add(topt);
			}
		}
		if( gcat.value == 0 ){
			t.disabled=true;	
		}else{
			t.disabled=false;
		}
	}
};

function checkPrice() {

	var priceMin = document.getElementById('min').value;
	var priceMax = document.getElementById('max').value;
	
	if(IsNumeric(priceMin)&&IsNumeric(priceMax)) {
		
		if((parseInt(priceMin))<(parseInt(priceMax))) {
		
			document.getElementById('price_error').style.display='none';
			document.getElementById('price_error_content').innerHTML='';
		
		} else if((parseInt(priceMin))===(parseInt(priceMax))) {
		
			document.getElementById('price_error').style.display='none';
			document.getElementById('price_error_content').innerHTML='';
		
		} else if((parseInt(priceMin))>(parseInt(priceMax))) {
		
			document.getElementById('price_error').style.display='inline';
			document.getElementById('price_error_content').innerHTML='Your Minimum price cannot be larger than your Maximum price!';
		
		} 
		
	} else {
	
		document.getElementById('price_error').style.display='none';
		document.getElementById('price_error_content').innerHTML='';
	
	}

};

function clearSuburbs(control){
var suburbText = document.getElementById('suburb');
suburbText.value = '';
for(var i = 0;i < control.length;i++){
control.options[i].selected = false;
}
control.options[0].selected = true;
};

var apaIndex = new Object;
apaIndex.populated = false;

function apaComplete () {
	if (!document.getElementById) return;
	var text   = document.getElementById('suburb');
	var select = document.getElementById('suburbs');
	if (!apaIndex.populated) apaBuildIndex();
	var suburb = text.value.match(/,*([^,]+)$/);
	if (suburb) {
	var name = suburb[1].toUpperCase().replace(/^\s*/, '').replace(/\s*$/, '');
	for (var i = apaIndex[name.charAt(0)]; i < select.options.length; i++) {
	if (select.options[i].text.toUpperCase().indexOf(name) == 0) {
	select.selectedIndex = i;
	break;
	}
	else {
	select.selectedIndex = -1;
	} } } 
};

function apaInsert () {
	if (!document.getElementById) return false;
	var text = document.getElementById('suburb');
	var select = document.getElementById('suburbs');
	text.focus();
	var suburb = select.options[select.selectedIndex];
	if (select.selectedIndex == 0) {
	text.value = '';
	return true;
	}
	var textSuburbs = text.value.split(/\s*,\s*/);
	if (!textSuburbs) return false;
	var pattern = new RegExp('\s*' + suburb.text + '\s*$');
	for (var i = 0; i < textSuburbs.length; i++) {
	if (pattern.exec(textSuburbs[i])) return false;
	}
	var newvalue = text.value.replace(
	/(^|,)([^,]*)$/,
	"$1 " + suburb.text + ', '
	);
	text.value = newvalue;
	return false; 
};

function apaBuildIndex () {
	if (!document.getElementById) return;
	var select = document.getElementById('suburbs');
	for (var i = select.options.length; i--;) {
	apaIndex[select.options[i].text.toUpperCase().charAt(0)] = i;
	} apaIndex.populated = true;
};

function apaRegion (id) {
 if (!document.getElementById) return;
 var select     = document.getElementById('suburbs');
 var regionId   = document.getElementById('region');
 var region     = apaSuburbs[id];
 apaRegionId    = id;
 regionId.value = id;
 

 if (!apaSuburbs[id]) return;
 // populate select
 // remove all optgroups from the select
 var eachGroup;
 while (eachGroup = select.firstChild) {
 select.removeChild(eachGroup);
 }
 select.length        = 0;
  while (select.options.length > 1) {
 select.options.remove(1);
 }

 addOption(allLocationLabel(region.name), id, select);

 select.selectedIndex = 0;
 for (var i = 0; i < region.places.length; i++) {
 addOption(region.places[i].name, region.places[i].name, select);
 if (region.places[i].places != null && region.places[i].places.length > 0) {
 var optGroup = document.createElement("optgroup");
 optGroup.label = 'In and around ' + unescape(region.places[i].name) + '...';
 select.appendChild(optGroup);
 for (var j = 0; j < region.places[i].places.length; j ++) {
 addOption(
 region.places[i].places[j].name,
 region.places[i].places[j].name,
 optGroup
 );
 }
 }
 }
 // populate index
 if (!apaSuburbsIndex[id]) {
 apaSuburbsIndex[id] = new Object;
 for (var i = select.options.length; i--;) {
 var text = select.options[i].text.replace(/^\*{3} All /,'').replace(/ Suburbs \*{3}$/,'');
 apaSuburbsIndex[id][text.toUpperCase().charAt(0)] = i;
 };
 apaSuburbsIndex[id].populated = true;
 };
};
 function allLocationLabel(name) {
 return '*** All ' + name + ' Suburbs ***';
};
 function addOption(optLabel, optValue, optParent) {
 var childOption = document.createElement('option');
 childOption.value = unescape(optValue);
 childOption.text  = unescape(optLabel);
 childOption.innerText = unescape(optLabel);
 optParent.appendChild(childOption);
};

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      };
	  
   return IsNumber;
   
};

function cs(f,b) { window.location.reload( false ); };
function ns(f,b) { window.location = b; };

function CreateBookmarkLink(title,url) {

if (window.sidebar) { // Mozilla Firefox Bookmark
	window.sidebar.addPanel(title, url,"");
} else if( window.external ) { // IE Favorite
	window.external.AddFavorite( url, title); }
else if(window.opera && window.print) { // Opera Hotlist
	return true; }
};
