// Homepage Search Form
//-----------------------------------------------------------
function SubmitHomepageSearchForm() {
	var regionId = $('select#region').val();
	if(regionId > 0) $('input#regionName').val($("select#region option:selected").text());		
	var cityId = $('select#city').val();
	if(cityId > 0) $('input#cityName').val($("select#city option:selected").text());		
	var suburbIds = $('select#suburb').val();
	var suburbNames = '';
	if(suburbIds != '') 
	{
		$("select#suburb option:selected").each(function () {
			if($(this).val()!=0)
			{
        		suburbNames += suburbNames == '' ? $(this).text() : ',' + $(this).text();
			}
        });
		$('input#suburbNames').val(suburbNames);	
	}	
	document.forms['homepageSearchForm'].submit();
}
// Side Search Form
//-----------------------------------------------------------
function ToggleSearchForm()
{
	if(!$(this).hasClass('activeTab'))
	{
		$('div.forSaleTab span').removeClass('activeTab');
		$('div.rentTab span').removeClass('activeTab');	
		$(this).addClass('activeTab');		
		$('table#forSaleBlock').css('display', $(this).html()=='To Rent' ? 'none' : 'block');
		$('table#rentBlock').css('display', $(this).html()=='To Rent' ? 'block' : 'none');		
		$('select#residentialCategory').css('display', $(this).html()=='To Rent' ? 'none' : '');		
		$('select#rentalCategory').css('display', $(this).html()=='To Rent' ? '' : 'none');		
		$('input#group').val($(this).html()=='To Rent' ? 'rent' : 'forSale');	
		$('input#type').val($(this).html()=='To Rent' ? 'Rental' : 'Residential');
		$('form#searchForm').attr('action', $(this).html()=='To Rent' ? 'Rental_Property' : 'Residential_Property');
	}
}
function ToggleCommercialSearchForm()
{
	if(!$(this).hasClass('activeTab'))
	{
		$('div.saleTab span').removeClass('activeTab');
		$('div.leaseTab span').removeClass('activeTab');	
		$(this).addClass('activeTab');		
		$('table#forSaleBlock').css('display', $(this).html()=='Leases' ? 'none' : 'block');
		$('table#leaseBlock').css('display', $(this).html()=='Leases' ? 'block' : 'none');			
		$('input#group').val($(this).html()=='Leases' ? 'lease' : 'forSale');		
	}
}
function ResetSearchForm()
{
	$('input#keyword').val('Keyword or ID#');		
	$('input#sfRegionName').val('');
	$('input#sfCityName').val('');
	$('input#sfSuburbNames').val('');
	$('input#sfCategoryName').val('');
	$('input#sfSubCategoryName').val('');
	$('form#searchForm select').children().each(
		function(){
			if($(this).attr('value')=='0' || $(this).attr('value') == '')
			{
				$(this).attr('selected','selected'); // The Retarded IE needed this!!!
			}
			if($(this).attr('selected')) $(this).attr('selected','');
	});
	$('div#citySelectWrapper').html("<select name='city' id='city' class='wide' onchange='UpdateSuburbOptions($(this));'><option value='0'>All cities</option></select>");	
	$('div#suburbSelectWrapper').html("<select name='suburb' id='suburb' class='wide' onchange='UpdateSuburbName($(this));'><option value='0'>All suburbs</option></select>");
	$('div#businessSubCategoryWrapper').html("<select name='businessSubCategory' id='businessSubCategory' class='wide' onchange='UpdateCategoryName('sc', $(this));'>" +
			"<option value='0'>Any sub category</option></select>");	
}

function ClearKeywordField()
{
	//On Focus
	$(this).val($(this).val().toLowerCase()=='keyword or id#' ? '' : $(this).val());	
}
function RestoreKeywordField()
{
	//On Blur
	$(this).val($(this).val().length > 0 ? $(this).val() : "Keyword or ID#");
}
function UpdateBusinessSubCategory()
{	
	var catId = $(this).val();
	$('input#sfSubCategoryName').val('');
	if(catId > 0) $('input#sfCategoryName').val($(this).children("option:selected").text());
	var ajaxUrl = "/Async/BusinessSubCategoryGenerator.asp?prId=" + escape(catId);
	$.ajax({url: ajaxUrl, cache: false, success: function(data){		
		$('div#businessSubCategoryWrapper').html(data);
	}});
}
function UpdateCityOptions()
{
	var regionId = $(this).val();
	$('input#sfRegionName').val('');
	$('input#sfCityName').val('');
	$('input#sfSuburbNames').val('');
	if(regionId > 0) $('input#sfRegionName').val($(this).children("option:selected").text());
	$('div#suburbSelectWrapper').html("<select name='suburb' id='suburb' class='wide' onchange='UpdateSuburbName($(this));'><option value='0'>All suburbs</option></select>");	
	//$('div#citySelectWrapper').html("<img src='/Images/Site/ImageGallery/spacer.gif' alt='' />");
	//$('div#citySelectWrapper').addClass('loading');
	var ajaxUrl = "/Async/CitySuburbOptionGenerator.asp?sbst=city&prId=" + escape(regionId);	
	$.ajax({url: ajaxUrl, cache: false, success: function(data){
		//$('div#citySelectWrapper').removeClass('loading');	
		$('div#citySelectWrapper').html(data);
	}});
}
function UpdateSuburbOptions(jqElm)
{	
	var cityId = jqElm.val();
	$('input#sfCityName').val('');
	$('input#sfSuburbNames').val('');
	if(cityId > 0) $('input#sfCityName').val(jqElm.children("option:selected").text());	
	var ajaxUrl = "/Async/CitySuburbOptionGenerator.asp?sbst=suburb&prId=" + escape(cityId);	
	$.ajax({url: ajaxUrl, cache: false, success: function(data){	
		$('div#suburbSelectWrapper').html(data);
	}});
}
function UpdateCategoryName(target, jqElm)
{
	var id = jqElm.val();
	if(target=='pc')
	{
		$('input#sfCategoryName').val(''); 
		if(id > 0) $('input#sfCategoryName').val(jqElm.children("option:selected").text());	
	}
	else
	{
		$('input#sfSubCategoryName').val(''); 
		if(id > 0) $('input#sfSubCategoryName').val(jqElm.children("option:selected").text());
	}
}
function UpdateSuburbName(jqElm)
{
	var id = jqElm.val();
	$('input#sfSuburbNames').val('');
	if(id > 0) $('input#sfSuburbNames').val(jqElm.children("option:selected").text());	
}

function BookAnAppointment()
{
	var id = $('div#bookAnAppoinmentForm input#listingId').val();
	var clientName = $('div#bookAnAppoinmentForm input#ClientName').val();
	var clientPhone = $('div#bookAnAppoinmentForm input#ClientPhoneNumber').val();
	var clientEmail = $('div#bookAnAppoinmentForm input#ClientEmail').val();
	$('tr#btnRow').addClass('invisible');
	$('div#bookAnAppointmentMsgWrapper').html("<span>Sending your request, please wait...</span>");
	
	$.ajax({
	   type: "POST",
	   url: "/Async/SendBookAnAppointmentEmail.asp",
	   data: "id=" + escape(id) + "&clientName=" + escape(clientName) + "&clientPhone=" + escape(clientPhone) + "&clientEmail=" + escape(clientEmail),
	   success: function(msg){
		   	$('tr#btnRow').removeClass('invisible');
		   	$('div#bookAnAppointmentMsgWrapper').html(msg);
			$('div#bookAnAppointmentMsgWrapper').removeClass();			
	   }
	 });
}
function ResetBookAnAppointmentForm()
{
	var clientName = $('div#bookAnAppoinmentForm input#ClientName').val('');
	var clientPhone = $('div#bookAnAppoinmentForm input#ClientPhoneNumber').val('');
	var clientEmail = $('div#bookAnAppoinmentForm input#ClientEmail').val('');
	$('tr#btnRow').removeClass('invisible');
	$('div#bookAnAppointmentMsgWrapper').addClass('invisible');	
}

// Full view listing
//-----------------------------------------------------------
function collapseExpandSection(jQElm, elId)
{
	$('#'+elId).css('display', $('#'+elId).css('display')=='block' ? 'none' : 'block');
	jQElm.html($('#'+elId).css('display') == 'block' ? 'Collapse This Section' : 'Expand This Section');
}
function CollapseExpandDescription(jqElm)
{
	var txt = jqElm.html();
	jqElm.html($('div#fullDesc').html());
	$('div#fullDesc').html(txt);
}

function ToggleCommunityTab()
{
	$('ul#communityTabs li').removeClass('uRhere');
	$(this).addClass('uRhere');
	applyDropShadows();
	setTimeout('applyDropShadows();',500);
	setTimeout('applyDropShadows();',1000);
}

function applyDropShadows() {
	$('.dropShadow').remove();
	$('#horisontalNav li.uRhere').dropShadow({right: 1, bottom: 1, opacity: 0.3, blur: 2});
	$('#franchiseNav li.uRhere').dropShadow({right: 1, bottom: 1, opacity: 0.3, blur: 2});
	$('a.fakeBtn').dropShadow({right: 1, bottom: 1, opacity: 0.3, blur: 2});
	$('.formBtn').dropShadow({right: 1, bottom: 1, opacity: 0.3, blur: 2});
	$('.corporateBanner').dropShadow({right: 1, bottom: 1, opacity: 0.5, blur: 2});
	$('.franchiseBanner').dropShadow({right: 1, bottom: 1, opacity: 0.5, blur: 2});
	$('#ContactFormControl_btnSend').dropShadow({right: 1, bottom: 1, opacity: 0.3, blur: 2});
	$('#rentSaleTab .rentTab span, #rentSaleTab .forSaleTab span, #leaseSaleTab .saleTab span, #leaseSaleTab .leaseTab span').bind('click',function() {
		$('.formBtn').removeShadow();
		$('.formBtn').dropShadow({right: 1, bottom: 1, opacity: 0.3, blur: 2});
	});	
}

// Element Binding
$(document).ready(function() {
 	$('div.forSaleTab span').bind('click', ToggleSearchForm);
	$('div.rentTab span').bind('click', ToggleSearchForm);	
	$('div.saleTab span').bind('click', ToggleCommercialSearchForm);
	$('div.leaseTab span').bind('click', ToggleCommercialSearchForm);	
	$('input#keyword').bind('blur', RestoreKeywordField);
	$('input#keyword').bind('focus', ClearKeywordField);
	$('ul#communityTabs li').bind('click', ToggleCommunityTab);		
	$('form#searchForm select#region').bind('change', UpdateCityOptions);		
	$('form#searchForm select#businessCategory').bind('change', UpdateBusinessSubCategory);		
	$('span#resetButton').bind('click', ResetSearchForm);
	$('span#resetButton2').bind('click', ResetSearchForm);
	$('span#resetButton3').bind('click', ResetSearchForm);
	//$('div.bookAnAppointment').bind('click', DisplayBookAnAppointmentForm); //Search result page
	//$('p.bookAnAppointmentWide').bind('click', DisplayBookAnAppointmentForm); //Full view page
	
	$('a.hasToolTip').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		fade: 250
	});

	// drop shadows
	applyDropShadows();
});
