/* search for particular ads javascript */

// set the timer id
var timerID = null;

function ShowAdDetail(URL)
{
	// show the ajax throbber
	ShowThrobber();

	// handles the on success state
	var handlerfunc = function(t)
	{			
		setTimeout(function() 
				{
				// hide the ajax throbber
				HideThrobber();
				}, 500);
	}

	// update the ad details div with the required data..
	new Ajax.Updater('ad_details', URL, {evalScripts:true,onSuccess:handlerfunc});
}

// Display the postal address details if required
function ShowOrHideDiv(DivName, ShowDiv)
{
	//	var ShowDiv = document.getElementById(CheckBoxName).checked;

	// if the postal details are required - display
	if (ShowDiv=='Show')
	{
		Effect.BlindDown(DivName, {duration: 0.5});
		document.getElementById(DivName.concat('_show_button')).style.display = 'none';
		document.getElementById(DivName.concat('_hide_button')).style.display = 'inline';
	}
	else // otherwise - hide
	{
		Effect.BlindUp(DivName, {duration: 0.5});
		document.getElementById(DivName.concat('_show_button')).style.display = 'inline';
		document.getElementById(DivName.concat('_hide_button')).style.display = 'none';
	}
}


// display the required details in the region search form area
function UpdateRegionSearch(element, DivName, URL)
{
	// show the ajax throbber
	ShowThrobber();

	// handles the on success state
	var handlerfunc = function(t)
	{			
		setTimeout(function() 
				{
				// hide the ajax throbber
				HideThrobber();
				}, 500);
	}

	// get the value for the current element..
	var RegionValue = element.value;

	// append the value to the end of the url..
	URL += '&SearchType='.concat(RegionValue);

	// update the div
	new Ajax.Updater(DivName, URL, {evalScripts:true, onSuccess:handlerfunc});
}


// display the required details in the sub regions search form area
function UpdateRegionLocationDiv(Div, URL)
{
	// set the vars..
	var State = '';
	var Subdivision = '';
	var Suburb = '';
	var DisplaySuburb = '';

	// get the state, region and location values from the list..
	var LocationDiv = document.getElementById(Div);
	//see if we need to display the display suburb

	var BoolDisplaySuburb = "1";
	var bool_display_suburb = document.getElementById('bool_display_suburb');
	if (bool_display_suburb && bool_display_suburb.value == "")
	{
		BoolDisplaySuburb = "0";
	}
	
	//form type
	var FormType = (document.getElementById('form_type') ? document.getElementById('form_type').value : '');

	// get all the select elements within this div..
	var DropBoxes = LocationDiv.getElementsByTagName('select');

	// iterate through the drop boxes and get the values..
	for (i = 0; i < DropBoxes.length; i++)
	{
		if (DropBoxes[i].id == Div.concat('_state'))
			State = DropBoxes[i].value;
		else if (DropBoxes[i].id == Div.concat('_subdivisions')) {
			Subdivision = DropBoxes[i].value;
		} else if (DropBoxes[i].id == Div.concat('_suburbs')) {
			Suburb = DropBoxes[i].value;
		} else if (DropBoxes[i].id == Div.concat('_display_suburbs'))
			DisplaySuburb = DropBoxes[i].value; 
	}

	// append state, region and location values to the url..
	URL += '&State=' + State + '&Subdivision=' + Subdivision + '&Suburb=' + Suburb + '&DisplaySuburb=' + DisplaySuburb + '&BoolDisplaySuburb=' + BoolDisplaySuburb + '&FormType=' + FormType;
	
	//  update the div..
	UpdateDiv(Div, URL);
}



// Show the photo in the frame..
function ShowPhoto(URL)
{
	// stop the slide show if there is one
	if (timerID)
		clearTimeout(timerID);

	// update the photo section..
	new Ajax.Updater('car_images', URL, {evalScripts:true});
}

// start the slide show
function StartSlideShow(URL)
{
	// goto the next slide
	NextSlide(URL);
}

// stop the slide show
function StopSlideShow(URL)
{
	// update the photo section..
	if (timerID)
		clearTimeout(timerID);

	// update the ajax with the new item..
	new Ajax.Updater('car_images', URL, {evalScripts:true});
}

// goto the next slide in the slideshow
function NextSlide(URL)
{
	// update the photo section.. after 3 seconds
	timerID = setTimeout(function() 
			{
			new Ajax.Updater('car_images', URL, {evalScripts:true});
			}, 3000);	
}

// updates the watchlist
function UpdateWatchlist(URL)
{	
	// show the ajax throbber
	ShowThrobber();

	// handles the on success state
	var handlerfunc = function(t)
	{			
		setTimeout(function() 
				{
				// hide the ajax throbber
				HideThrobber();
				}, 500);
	}

	// run the xmlhttp request and get the data..
	new Ajax.Updater('user_short_list', URL, {onSuccess:handlerfunc});
}

// updates the comparelist
function UpdateComparelist(URL)
{
	// show the ajax throbber
	ShowThrobber();

	// handles the on success state
	var handlerfunc = function(t)
	{
		setTimeout(function()
				{
				// hide the ajax throbber
				HideThrobber();
				}, 500);
	}

	// run the xmlhttp request and get the data..
	new Ajax.Updater('user_compare_list', URL, {onSuccess:handlerfunc});
}



// updates the browsing shortlist
function UpdateBrowseShortlist(DivID, URL)
{	
	// show the ajax throbber
	ShowThrobber();

	// handles the on success state
	var handlerfunc = function(t)
	{			
		setTimeout(function() 
				{
				// hide the ajax throbber
				HideThrobber();
				}, 500);
	}

	// run the xmlhttp request and get the data..
	new Ajax.Updater(DivID, URL, {onSuccess:handlerfunc});
}
/*
   This has been move to comparelist.js
// updates the browsing comparelist
function UpdateBrowseComparelist(DivID, AdvertID, URL, UpdateURL)
{	
// show the ajax throbber
ShowThrobber();

// handles the on success state
var handlerfunc = function(t)
{			
setTimeout(function() 
{
// hide the ajax throbber
HideThrobber();

// now foreach compare div on the page.. update the details..
var CompareDivs = getElementsByClassName(document, "div", "compare_list_option");

// iterate and set the compare options..
for (var i = 0; i <= (CompareDivs.length - 1); i++)
{
// get the advert id for the div..
var CompareID = CompareDivs[i].id;
var CompareParts = CompareID.toString().split('_');		
new Ajax.Updater(CompareID, UpdateURL.concat('&AdvertID=').concat(CompareParts[3]), {});
}

}, 500);
}

// run the xmlhttp request and get the data..
new Ajax.Updater(DivID.concat('_').concat(AdvertID), URL, {onSuccess:handlerfunc});
}
 */

function GetNextResult(URL)
{	
	// show the ajax throbber
	ShowThrobber();

	// handles the on success state
	var handlerfunc = function(t)
	{			
		setTimeout(function() 
		{
			// hide the ajax throbber
			HideThrobber();
		}, 500);
	}

	// get the ad details
	new Ajax.Updater('all_details', URL, {evalScripts:true,onSuccess:handlerfunc});
}
GetSortedResults = function(DivName, URL)
{
	var OrderBy = document.getElementById('order_by').value;
	var OrderType = document.getElementById('order_type').value;
	var ItemsPerPage = document.getElementById('items_per_page').value;
	URL += "&OrderBy="+OrderBy+"&OrderType="+OrderType+"&PerPage="+ItemsPerPage+"&Reloading=1";
	new Ajax.Updater(DivName, URL, {evalScripts:true});
	updatePageLinks( OrderBy, OrderType, URL );
	updatePrevNext( OrderBy, OrderType, URL );
	updatePerPage( OrderBy, OrderType );
};
function updateLink( link, orderBy, orderType ) {

	var getOrderBy=/OrderBy=([^&]+)/;
	var regExpBy = new RegExp( getOrderBy );	

	var getOrderType=/OrderType=([^&]+)/;
	var regExpType = new RegExp( getOrderType );	

	// does the link have these parts?
	if (regExpBy.test(link)) {
		link = link.replace( getOrderBy, 'OrderBy='+orderBy )
	}
	if (regExpType.test(link)) {
		link = link.replace( getOrderType, 'OrderType='+orderType )
	}
	return link;
}
function updatePerPage( orderBy, orderType ) {
	// maintain the sortOrder in the hidden OrderBy and OrderType fields
	// change the values in the hidden fields
	// so when sort order is changed, the hidden fields
	// are also changed
	document.num_per_page_form.ppOrderBy.value=orderBy;
	document.num_per_page_form.ppOrderType.value=orderType;
}
function updatePageLinks( OrderBy, OrderType, URL )
{
	// to update OrderBy and OrderType:
	// get the element values from NavigationBar
	// rewrite the page number links
	var NavBar = document.getElementById('navigation_bar');

	var PageNumbers = document.getElementById('page_selection');
	var pageLinkSet = PageNumbers.childNodes;
	for ( var v=0; v<pageLinkSet.length; v++ ) {
		link = pageLinkSet[v];
		if ( link.nodeType==1 ) {
			if (link.href) {
				link.href = updateLink(link.href, OrderBy, OrderType );
			}
		}
	}
}
function updatePrevNext( OrderBy, OrderType, URL ) {
	// do the same thing with links inside the Prev/Next selectors
	// and links around the icon 
	var NavBar = document.getElementById('navigation_bar');
	var LRLinks = document.getElementsByName('PagerLinks');
	for( var v=0; v<LRLinks.length; v++ ) {
		if ( LRLinks[v].href ) {
			LRLinks[v].href = updateLink( LRLinks[0].href, OrderBy, OrderType );
		}
	}
}

function MapUpdate( contents ) {
	//alert( 'MapUpdate:'+URL );
	var gm=document.getElementById('resultMapDisplay');
	//contents='This Content is overwritten - by what?';
	gm.innerHTML=contents;
}

GetPerPageResults = function(DivName, URL)
{
	var OrderBy = document.getElementById('order_by').value;
	var OrderType = document.getElementById('order_type').value;
	var ItemsPerPage = document.getElementById('items_per_page').value;
	URL += "&OrderBy="+OrderBy+"&OrderType="+OrderType+"&PerPage="+ItemsPerPage;
	
	new Ajax.Updater(DivName, URL, {evalScripts:true});
}

// Updates the search form action so that we can go to various pages based on the category the user chooses
function UpdateSearchFormAction (Element, URLPrefix) {
	
	var QuickSearchForm = document.getElementById('QuickSearch');
	var URL = URLPrefix;

	if (!Element)
		return;

	// send the url based on the element value
	if (Element.value == '1')
	{
		URL += 'buy/residential/results/';
	}
	else if (Element.value == '2')
	{
		URL += 'rent/residential/results/';
	}
	else if (Element.value == '3')
	{
		URL += 'buy/rural/results/';
	}
	else if (Element.value == '4')
	{
		URL += 'rent/rural/results/';
	}
	else if (Element.value == '5')
	{
		URL += 'buy/commercial/results/';
	}
	else if (Element.value == '6')
	{
		URL += 'rent/commercial/results/';
	}
	else if (Element.value == '7')
	{
		URL += 'buy/business/results/';
	}
	else if (Element.value == '8')
	{
		URL += 'share/results/';
	}
	else if (Element.value == '9')
	{
		URL += 'new_properties/results/';
	}
	else
	{
		URL += 'buy/residential/results/';
	}
	
	// update the action attribute of the form
	QuickSearchForm.setAttribute('action', URL);
	
	//if (document.getElementById('AdvancedSearch'))
	{
		//AdvancedSearch.setAttribute('action', URL);
	}
}

function SubmitAdvancedSearch (URLPrefix) 
{
	var URL = URLPrefix;
	
	var Element=document.getElementById("primary_search_category_id");
	
	// send the url based on the element value
	if (Element.value == '1')
	{
		URL += 'buy/residential/results/';
	}
	else if (Element.value == '2')
	{
		URL += 'rent/residential/results/';
	}
	else if (Element.value == '3')
	{
		URL += 'buy/rural/results/';
	}
	else if (Element.value == '4')
	{
		URL += 'rent/rural/results/';
	}
	else if (Element.value == '5')
	{
		URL += 'buy/commercial/results/';
	}
	else if (Element.value == '6')
	{
		URL += 'rent/commercial/results/';
	}
	else if (Element.value == '7')
	{
		URL += 'buy/business/results/';
	}
	else if (Element.value == '8')
	{
		URL += 'share/results/';
	}
	else if (Element.value == '9')
	{
		URL += 'new_properties/results/';
	}
	else
	{
		URL += 'buy/residential/results/';
	}
	
	// update the action attribute of the form
	document.AdvancedSearch.action = URL;
}


// ClearSearch - function to clear the quick search form...
function ClearSearch(URL)
{
	new Ajax.Updater('QuickSearchForm', URL, {evalScripts:true});
}

// to change parameter sets from SearchFormData[name]= format to name= format
function ExtractName(FormParam) 
{
	var a = FormParam.replace(/SearchFormData\[/,"");
	a = a.replace(/\]/,"");
	return a;
}
// a version of AdvancedSearch that extracts GET parameter names to make more SEO friendly URLs
function AdvancedSearchGet(URLPrefix) 
{
	var elem = document.getElementById('QuickSearch').elements;
	//var URL = document.getElementById('QuickSearch').action+"?";
	var URL = URLPrefix+"index.htm?Action=classified_search&";
	// split the elem.name string to extract the content of [ ]
	var name = '';
	for(i=0; i< elem.length; i++)
	{
		//  if(elem[i].name.indexOf("SearchFormData") >= 0)
		//we use a hidden field for last_7_days checkbox and we should only send the value if checkbox was checked
		// extract the name from the [ ]
		name = ExtractName( elem[i].name )
		if (elem[i].type != "checkbox")
		{
			//suburb select is multiple so we need to send all the selected suburbs
			if (elem[i].id == "quick_search_region_location_search_suburbs")
			{
				var contor = 0;
				for (var j = 0; j < elem[i].options.length; j++)
				{
					if (elem[i].options[j].selected)
					{
						URL += "suburb["+j+"]="+elem[i].options[j].value+"&";
						contor++;
					}
				}
			}
			else 
			{
				//remove the % and # from the url
				if (elem[i].value == "%" || elem[i].value == "Keywords,Postcode or Property Id")
					elem[i].value = "";
					
				URL += name+"="+elem[i].value+"&";
			}
		}
		else 
		{
			if (elem[i].checked)
			{
				URL += elem[i].name+"="+elem[i].value+"&";
			}
		}
	}
	window.document.location = URL;
}

function AdvancedSearch(URLPrefix)
{
	var elem = document.getElementById('QuickSearch').elements;
	//var URL = document.getElementById('QuickSearch').action+"?";
	var URL = URLPrefix+"index.htm?Action=classified_search&";
	for(i=0; i< elem.length; i++)
	{
		//  if(elem[i].name.indexOf("SearchFormData") >= 0)
		//we use a hidden field for last_7_days checkbox and we should only send the value if checkbox was checked
		if (elem[i].type != "checkbox")
		{
			//suburb select is multiple so we need to send all the selected suburbs
			if (elem[i].id == "quick_search_region_location_search_suburbs")
			{
				var contor = 0;
				for (var j = 0; j < elem[i].options.length; j++)
				{
					if (elem[i].options[j].selected)
					{
						URL += "SearchFormData[suburb]"+"["+contor+"]"+"="+elem[i].options[j].value+"&";
						contor++;
					}
				}
			}
			else 
			{
				//remove the % and # from the url
				if (elem[i].value == "%" || elem[i].value == "Keywords,Postcode or Property Id")
					elem[i].value = "";
					
				URL += elem[i].name+"="+elem[i].value+"&";
			}
		}
		else 
		{
			if (elem[i].checked)
			{
				URL += elem[i].name+"="+elem[i].value+"&";
			}
		}
	}
	window.document.location = URL;

}

// function to save the current search
function SaveThisSearch(SearchID)
{
	var URL = "/index.htm?Action=my_searches_save&SearchID="+SearchID;
	new Ajax.Updater('save_this_search', URL, {evalScripts:true});
}

// function to save the current search
function DeleteSavedSearch(SearchID)
{	
	var URL = "/index.htm?Action=my_searches_delete&SearchID="+SearchID;
	new Ajax.Updater('my_saved_searches', URL, {evalScripts:true});
}

// function to release the parked ads
function ReleaseAd(AdvertID)
{	
	var URL = "/index.htm?Action=admin_parked_ads_release&AdvertID="+AdvertID;
	new Ajax.Updater('parked_ads_list', URL, {evalScripts:true});
}

function PriceTypeChanged()
{
    PriceTypeSelect = document.getElementById('price_type');
//alert( "price type change:" + PriceTypeSelect.value);
    if (PriceTypeSelect.value == "Auction")
    {
        if (document.getElementById('auction_date_wrapper') != null)
        {
            document.getElementById('auction_date_wrapper').style.display = "inline";
            document.getElementById('tender_close_date_wrapper').style.display = "none";
        }
    }
    else
    {
        if (PriceTypeSelect.value == "Tender")
        {
            if (document.getElementById('tender_close_date_wrapper') != null)
            {
               document.getElementById('tender_close_date_wrapper').style.display = "inline";
               document.getElementById('auction_date_wrapper').style.display = "none";
            }
        }
        else
        {
            if (document.getElementById('auction_date_wrapper') != null)
            {
               document.getElementById('tender_close_date_wrapper').style.display = "none";
               document.getElementById('auction_date_wrapper').style.display = "none";
            }
        }
    }
}


// Hide the keyword search label
function HideKeywordSearchLabel(element)
{
	if (element.value == 'Keywords,Postcode or Property Id')
	{
		element.value = '';
	}	
}

// Show the keyword search label
function ShowKeywordSearchLabel(element)
{
	if (element.value == '')
	{
		element.value = 'Keywords,Postcode or Property Id';
	}	
}


/* end of file */
