var dialog_open = false;

$(function() {

	//Fix for Main Menu in IE	
	$("#navmenu-h li,#navmenu-v li").hover( function() {
		$(this).addClass("iehover");
	}, function() {
		$(this).removeClass("iehover");
	});	
});

$(function() {

	if (typeof CookiesEnabled == 'function') {
		CookiesEnabled();
	}

	if($('#LeadTelephone1') && isVisible($('#LeadTelephone1'))){
		$('#LeadTelephone1').focus();
	}
	
	if (typeof loadMaps == 'function') {
		loadMaps();
	}
});

function JsCounty(county_id, params) {
	
	var main_container = $('#MainContainer'+county_id);
	var div_container = $('#ResultsContainer'+county_id);
	var hidden = $('#OpenedCounty' + county_id);
	var cross = $('#Cross' + county_id);
	
	main_container.attr("className", "");		

	if (hidden.val() != 1) {

		div_container.html('<div style="margin-left: 100px;color:#000;"><img src="/img/ajax-loader.gif" style="margin-right:10px;margin-top:-7px;">Loading...</div>');
		main_container.show();

		var url = '/properties/search_county/?county=' + county_id + '&' + params;
		
		if ($('#CountyCheckbox'+county_id).attr("checked")){
			url += '&cas2=1';
		}	

		cross.html('<img src="/img/line.jpg" />');		
		div_container.load(url, {}, function() {});
		hidden.val(1);

	} else {
		
		main_container.hide();
		cross.html('<img src="/img/cross.jpg" />');
		hidden.val(0);
	}
}

function getOpenedCounties(element_id){

	var counties = '';
	
	if ($("#" + element_id).length > 0) {

		$("#" + element_id + " input:checkbox").each(
			function() {
				
				if (this.name == 'county'){					
					
					if($('#OpenedCounty'+this.value).val()==1){
						counties += this.value + ',';
					}	
				}	
			});

		counties = counties.substr(0, counties.length - 1);
	}
	
	return counties;	
}

function JsSearchState() {

	if(!$("#StateId")){ 
		return false;
	}	
	
	if(!$("input[@name=ltype]:checked")){
		return false;
	}	
	
	var state_id = $("#StateId").val();
	var ltype = $("input[@name=ltype]:checked").val();

	if (state_id == '') {		

		$("#StateId").focus();

	} else {

		var url = '/properties/search_list?ltype=' + ltype + '&state=' + state_id;
		$(document.location).attr("href", url);
	}
}

function JsSearchStateList(id) {
	
	//var ltype = $("input[@name=ltype_checkbox]:checked").val();
	var ltype = $("#ltype").val();	
	var reo = $("#reo").val();
	var ss = $("#ss").val();

	var url = '/properties/search_list?ltype=' + ltype + '&state=' + id + '&reo=' + reo + '&ss=' + ss;
	$(document.location).attr("href", url);
}

function JsvalorP(pestana) {
	//var pestana = pestana;
}

/*
 * Returns the number of tab selected in the form
 * 
 * */

function getSelectedTab() {

	var selected = 1;
	var count = 1;

	$('#tab-container-1-nav').find('li').each( function() {

		if ($(this).attr("class") == 'activeli') {

			selected = count;
		}

		count++;
	});

	return selected;
}

/*
 * Perform the needed actions to redirect to the url search
 * 
 * */

function JsSearchAll() {

	var selected_tab = getSelectedTab();
	var parameters = parseParameters();
	var url = buildSearchUrl(parameters, selected_tab);
	
	switch (selected_tab) {

		case 1: {
	
			// Search by Location
			if (url.length > 0) {
				$("#searching-modal").modal();
				$(document.location).attr("href", url);
			}
			break;
		}
	
		case 2: {
	
			// Search by Zip Code
			var tipo=$('#tipo').val();
			//alert(tipo);
			if(tipo==1){
				
				if (url.length > 0) {
					$("#searching-modal").modal();
					$(document.location).attr("href", url);
				}
				break;
				
				
				
				
		}else{
			
			if (url.length > 0) {
					$("#searching-modal").modal();
					var zip = parameters.property_zip;
					var ltype=$('#ltype').val();
					var reo=$('#reo').val();				
					var ss=$('#ss').val();					
					var url = '/properties/search/?ltype=' + ltype + '&reo='+ reo+ '&ss='+ ss+ '&zip='+ zip;			
					//alert(url);				
					$(document.location).attr("href", url);
				}
				break;
			
			}
			
			
			
		}

		case 3: {
	
			// Search by ID or MLS
			if ($('#PropertyId').val() != '' || $('#PropertyMlsId').val() != '') {
				$("#searching-modal").modal();
				$("#SearchByIdForm").submit();
			} else {
				alert('You must enter either the ID or MLS');
			}
			break;
		}
	}
}

/*
 * Parse form parameters to an object
 * 
 * */
 

 

function parseParameters() {

	var parameters = {};

	if ($('#state').length != 0)
		parameters.state = $("#state").val();
	else
		parameters.state = '';


	if ($("input[@name=ltype]:checked").length != 0)
		parameters.ltype = $("input[@name=ltype]:checked").val();		
		
	else
		parameters.ltype = '';	
	
	if ($("#PropertyHtype").length != 0)
		parameters.property_htype = $("#PropertyHtype").val();
	else
		parameters.property_htype = '';

	if ($("#PropertyPmin").length != 0)
		parameters.property_pmin = $("#PropertyPmin").val();
	else
		parameters.property_pmin = '';

	if ($("#PropertyPmax").length != 0)
		parameters.property_pmax = $("#PropertyPmax").val();
	else
		parameters.property_pmax = '';

	if ($("#PropertyBeds").length != 0)
		parameters.property_beds = $("#PropertyBeds").val();
	else
		parameters.property_beds = '';

	if ($("#PropertyBaths").length != 0)
		parameters.property_baths = $("#PropertyBaths").val();
	else
		parameters.property_baths = '';

	if ($("#PropertySqft").length != 0)
		parameters.property_sqft = $("#PropertySqft").val();
	else
		parameters.property_sqft = '';

	if ($("#PropertyZip").length != 0)
		parameters.property_zip = $("#PropertyZip").val();
	else
		parameters.property_zip = '';

	if ($("#PropertyPhoto").length != 0)
		parameters.property_photo = document.getElementById('PropertyPhoto').checked;
	else
		parameters.property_photo = '';
	
	if ($("#PropertyReduced").length != 0)
		parameters.property_reduced = document.getElementById('PropertyReduced').checked;
	else
		parameters.property_reduced = '';		
	
	if ($("#PropertyNew").length != 0)
		parameters.property_new = document.getElementById('PropertyNew').checked;
	else
		parameters.property_new = '';		
		
	parameters.property_ss = false;
	parameters.property_bo = false;

	switch (parameters.ltype) {

		case '3': {
			parameters.reo = true;
			break;
		}
		case '4': {
			parameters.ss = true;
			break;
		}
		default:{			
			parameters.reo = false;
			parameters.ss = false;
			
			if ($("#PropertySs").length != 0)
				parameters.property_ss = document.getElementById('PropertySs').checked;
			else
				parameters.property_ss = false;

			if ($("#PropertyBo").length != 0)
				parameters.property_bo = document.getElementById('PropertyBo').checked;
			else
				parameters.property_bo = false;
			
			break;	
		}
	}

	return parameters;
}

/*
 * Gets a list of selected counties in the form
 * 
 * */

function getSelectedCounties(element_id) {

	var counties = '';

	if ($("#" + element_id).length > 0) {

		$("#" + element_id + " input[@type=checkbox]:checked").each(
				function() {
					if (this.name == 'county')
						counties += this.value + ','
				});

		counties = counties.substr(0, counties.length - 1);
	}

	return counties;
}

/*
 * Gets a list of selected cities in the form
 * 
 * */

function getSelectedCities(element_id) {

	var cities = '';

	if ($("#" + element_id).length > 0) {

		$("#" + element_id + " input[@type=checkbox]:checked").each(
				function() {
					if (this.name == 'city')
						cities += this.value + ','
				})

		cities = cities.substr(0, cities.length - 1);
	}

	return cities;
}

/*
 * Builds the URL to do the search
 * 
 * */

function buildSearchUrl(parameters, selected_tab) {

	var url = '';

	switch (selected_tab) {

		case 1: {
	
			// Search by City/Area
			var counties = getSelectedCounties("county");
			var cities = getSelectedCities("city");
	
			if (counties.length > 0 || cities.length > 0) {
	
				url = '/properties/search?';
				
				if (counties.length > 0)
					url += 'county=' + counties + '&';
				
				if (cities.length > 0)
					url += 'city=' + cities + '&';
								
				if (parameters.ltype.length > 0){
				
					switch (parameters.ltype) {
						case '3': {
							url += 'ltype=1&reo=1&';
							break;
						}
						case '4': {
							url += 'ltype=1&ss=1&';
							break;
						}
						default:{
							url += 'ltype=' + parameters.ltype + '&';
							break;
						}
					}	
				}	
								
				if (parameters.state.length > 0)
					url += 'state=' + parameters.state + '&';
				if (parameters.property_htype.length > 0)
					url += 'htype=' + parameters.property_htype + '&';
				if (parameters.property_pmin.length > 0)
					url += 'pmin=' + parameters.property_pmin + '&';
				if (parameters.property_pmax.length > 0)
					url += 'pmax=' + parameters.property_pmax + '&';
				if (parameters.property_beds.length > 0)
					url += 'beds=' + parameters.property_beds + '&';
				if (parameters.property_baths.length > 0)
					url += 'baths=' + parameters.property_baths + '&';
				if (parameters.property_sqft.length > 0)
					url += 'sqft=' + parameters.property_sqft + '&';
				if (parameters.property_photo)
					url += 'photo=1&';
				if (parameters.property_reduced)
					url += 'reduced=1&';
				if (parameters.property_new)
					url += 'new=1&';				
				if (parameters.property_ss)
					url += 'no_ss=1&';
				if (parameters.property_bo)
					url += 'no_reo=1&';
				
			} else {
	
				alert('You must select a location');
			}
	
			break;
		}
	
		case 2: {
	
			// Search By ZipCode
			if (isValidZipString(parameters.property_zip)) {
	
				if (parameters.property_zip.length > 0) {
	
					url = '/properties/search?';

					if (parameters.ltype.length > 0){
						
						switch (parameters.ltype) {
							case '3': {
								url += 'ltype=1&reo=1&';
								break;
							}
							case '4': {
								url += 'ltype=1&ss=1&';
								break;
							}
							default:{
								url += 'ltype=' + parameters.ltype + '&';
								break;
							}
						}	
					}
					
					if (parameters.state.length > 0)
						url += 'state=' + parameters.state + '&';
					if (parameters.property_htype.length > 0)
						url += 'htype=' + parameters.property_htype + '&';
					if (parameters.property_pmin.length > 0)
						url += 'pmin=' + parameters.property_pmin + '&';
					if (parameters.property_pmax.length > 0)
						url += 'pmax=' + parameters.property_pmax + '&';
					if (parameters.property_beds.length > 0)
						url += 'beds=' + parameters.property_beds + '&';
					if (parameters.property_baths.length > 0)
						url += 'baths=' + parameters.property_baths + '&';
					if (parameters.property_sqft.length > 0)
						url += 'sqft=' + parameters.property_sqft + '&';
					if (parameters.property_zip.length > 0)
						url += 'zip=' + parameters.property_zip + '&';
					if (parameters.property_photo)
						url += 'photo=1&';
					if (parameters.property_reduced)
						url += 'reduced=1&';
					if (parameters.property_new)
						url += 'new=1&';					
					if (parameters.reo)
						url += 'reo=1&';
					if (parameters.ss)
						url += 'ss=1&';
					if (parameters.property_ss)
						url += 'no_ss=1&';
					if (parameters.property_bo)
						url += 'no_reo=1&';
					
				} else {
	
					alert('Enter a Zip Code');
				}
			} else {
	
				alert("You must enter zipcode numbers separated by commas");
			}
	
			break;
		}
	}
	
	return url;
}

/*
 * Determines if a string is a valid formated zip code
 * 
 * */

function isValidZipString(zip_code) {

	var strings = new Array();
	strings = zip_code.split(',');
	no = strings.length;

	for (x in strings) {

		var string = strings[x];
		string = string.replace(" ", "");

		if (!isNumeric(string)) {

			return false;
		}
	}

	return true;
}

/*
 * Determines if a string has only numeric values
 * 
 * */

function isNumeric(string) {

	var valid_chars = "0123456789.";
	var is_number = true;
	var char;

	for (i = 0; i < string.length && is_number == true; i++) {

		char = string.charAt(i);

		if (valid_chars.indexOf(char) == -1)
			is_number = false;
	}

	return is_number;
}

/*
 * 
 * Updates search list based on selected form params
 * 
 * */

function updatePrices(ltype){
	
	if(ltype==2){
		
		$("#PropertyPmin").html($("#PropertyRentPriceMinValues").html());
		$("#PropertyPmax").html($("#PropertyRentPriceMaxValues").html());			
	}
	else{
		
		$("#PropertyPmin").html($("#PropertySalePriceMinValues").html());
		$("#PropertyPmax").html($("#PropertySalePriceMaxValues").html());			
	}	
}


function updateList(update_prices) {	

	var opened_counties = getOpenedCounties('county');

	//Show modal window while new search list is loaded
	//$("#updating-modal").modal();	

	//Get selected params
	var parameters = parseParameters();
	var url = '/properties/search_state?';

	//Get selected localities
	var counties = getSelectedCounties("county");
	var cities = getSelectedCities("city");
	
	//Build URL 
	if (opened_counties.length > 0)
		url += 'show_counties=' + opened_counties + '&';	
	if (counties.length > 0)
		url += 'selected_counties=' + counties + '&';
	if (cities.length > 0)
		url += 'selected_cities=' + cities + '&';
	
	if (parameters.ltype.length > 0){
		
		if(update_prices){
							
			if(parameters.ltype==2){
				
				parameters.property_pmin = 1000;
				parameters.property_pmax = 0;
			}
			else{
				
				parameters.property_pmin = 75000;
				parameters.property_pmax = 0;
			}	
		}

		switch(parameters.ltype){
			
			case '1':{				
				url += 'ltype=' + parameters.ltype + '&';
				$("#ForSaleOptions").show();
				break;
			}
			case '2':{
				url += 'ltype=' + parameters.ltype + '&';
				$("#ForSaleOptions").hide();				
				break;
			}
			case '3':{
				url += 'ltype=1&reo=1&';
				$("#ForSaleOptions").hide();				
				break;
			}
			case '4':{
				url += 'ltype=&ss=1&';
				$("#ForSaleOptions").hide();				
				break;
			}			
			default: {				
				url += 'ltype=' + parameters.ltype + '&';
				$("#ForSaleOptions").hide();
				break;
			}
		}
	}	
	if (parameters.property_htype)
		url += 'htype=' + parameters.property_htype + '&';
	if (parameters.property_pmin)
		url += 'pmin=' + parameters.property_pmin + '&';
	if (parameters.property_pmax)
		url += 'pmax=' + parameters.property_pmax + '&';
	if (parameters.property_beds)
		url += 'beds=' + parameters.property_beds + '&';
	if (parameters.property_baths)
		url += 'baths=' + parameters.property_baths + '&';
	if (parameters.property_sqft)
		url += 'sqft=' + parameters.property_sqft + '&';
	if (parameters.property_photo)
		url += 'photo=1&';
	if (parameters.property_reduced)
		url += 'reduced=1&';			
	if (parameters.property_new)
		url += 'new=1&';			
	
	
	$('#tab1').html('<div class="loader" style="margin-top:10px;"></div> <div style="font-weight:bold;margin-left:180px;">Loading ...</div>');

	//Refresh search list
	$('#tab1').load(url, function() {
		//close modal window
		//$.modal.close();
	});
}

function JsSaveSearch(){
		
	if(!dialog_open) {
		
		$("#search-dialog").dialog({modal:true,title:'Save this Search',width:500});		
		dialog_open = true;
	}
	else{
		$("#search-dialog").dialog('open',{modal:true,title:'Save this Search',width:500});			           
	}
}

function JsSearchFavorite(url){

	var notify=$('#notify').attr('checked');
	var name=$("#name").val();	
	var keyid=$("#key_search").val();
	
	if(notify==true){notify=1;}
	if(notify==false){notify=0;}
	
	url=url+'&notify='+notify+'&name='+name+'&keyid='+keyid;
	
	var urla='/properties/save_search?'+url;
	divId='urlactual';
	
	$('#Res').load(urla, function() {	
		setTimeout("$('#Res').html('')",2000);							  
		setTimeout("$('#dialog').dialog('close')",2500);
		JsMessage()
	});		
}

function JsMessage(){
						
	setTimeout("$('#Message').html('Your Search has been saved'); $('#Message').show();",1500);					
}

function validateLeadInfo(url, form_id, notice_id) {

	if ($('#LeadFirstname').val() != "" && $('#LeadLastname').val() != "" && $('#LeadEmail').val() != "" && $('#LeadTelephone1').val() != "") {
		
		
		if (!$('#CaptchaValue') || $('#CaptchaValue').val() != "") {
		
			$("#saving-modal").modal();
			
			$.post(url, $('#'+form_id).serialize(), function(data){
	
					if(data==''){
						
						$("#"+notice_id).attr("class", "error-message");
						$("#saving-modal").html("<h3>Error ...</h3><p>Error saving your information. Please contact us.</p>");
						setTimeout('$.modal.close();', 3000);									
						$("#"+notice_id).html('Error. Please contact us.');
					}
					if(data=='-2'){

						if($("#captcha_image")){
						
							$("#captcha_image").html('<div class="loader"></div>');
							$("#"+notice_id).attr("class", "error-message");
							$("#"+notice_id).html('Error, image characters are incorrect.');
							$("#captcha_image").load('/captcha/image_html');
							$.modal.close();
						}	
					}
					if(data=='-1'){
					
						$("#"+notice_id).attr("class", "message");
						$("#saving-modal").html("<h3>Information Saved ...</h3><p>Your information has been saved.</p>");
						setTimeout('$.modal.close(); parent.$.fancybox.close();', 2000);
						$("#"+notice_id).html('You have scheduled a showing.');
					}
					else {
									
						$.modal.close();
					
						$.fancybox({
							type: 'iframe',
							href: '/leads/register_step2/?id='+data+'&message=true',
							padding: 10,
					        height: 570,
					        width: 600
						});										
					}	
			 });
		}
		else{
		
			$("#"+notice_id).attr("class", "error-message");
			$("#"+notice_id).html('Write the characters of the image.');
		}		
	} else {
		
		$("#"+notice_id).attr("class", "error-message");
		$("#"+notice_id).html('Please enter mandatory fields.');
	}
}

/////////////sort///////////////////////
function sortByProperties(formId, sortFieldId, directionFieldId, sortSelectId){
	
	sort = '';
	direction = '';
	sortBy = Number($('#'+sortSelectId).val());
	
	switch(sortBy){
	
		case 1:{
			sort = 'listing_price';
			direction = 'desc';
			break;
		}
		case 2:{
			sort = 'listing_price';
			direction = 'asc';
			break;
		}
		case 3:{
			sort = 'creation_date';
			direction = 'desc';
			break;
		}		
		case 4:{
			sort = 'num_beds';
			direction = 'desc';
			break;
		}
		case 5:{
			sort = 'listing_price_sqft';
			direction = 'desc';
			break;
		}			
		case 6:{
			sort = 'listing_price_sqft';
			direction = 'desc';
			break;
		}
	}
	
	if(sort!='' && direction!=''){
		
		$('#'+sortFieldId).val(sort);
		$('#'+directionFieldId).val(direction);
		$('#'+formId).submit();
	}	
}


function getPageContent(url, divId, loaderClass) {

	url += '?time=' + new Date().getTime();	
	$("#"+divId).hide().load('<div class="'+loaderClass+'"></div>',{},function () { $(this).show(500); });	
}

function addToFavorites(property_id, notice_id){
	
	var successMess = "The Property has been added to your favorites.";
	var errorMess = "Error adding the property to favorites.";
	var url = '/properties/add_favorites/id:'+property_id;

	$('#loading').html('<div class="saving">Loading...</div>');
	
	$.post(url, {}, function(data){
		
		if (data == 1) {

			$("#"+notice_id).attr("class", "message");
			$("#"+notice_id).html(successMess);
		} else {

			$("#"+notice_id).attr("class", "error-message");
			$("#"+notice_id).html(errorMess);
		}

		$('#loading').html('');		
	});
}

function isVisible(obj){
	
    if (obj == document) return true
    
    if (!obj) return false
    if (!obj.parentNode) return false
    if (obj.style) {
        if (obj.style.display == 'none') return false
        if (obj.style.visibility == 'hidden') return false
    }
    
    //Try the computed style in a standard way
    if (window.getComputedStyle) {
        var style = window.getComputedStyle(obj, "")
        if (style.display == 'none') return false
        if (style.visibility == 'hidden') return false
    }
    
    //Or get the computed style using IE's silly proprietary way
    var style = obj.currentStyle
    if (style) {
        if (style['display'] == 'none') return false
        if (style['visibility'] == 'hidden') return false
    }
    
    return isVisible(obj.parentNode)
}

function scheduleShowing(property_id, req_type, showing){

	url = '/leads/request_info/property_id:'+property_id+'/type:'+req_type;
	if(showing) url += '/showing:true/';

	$.fancybox({
		type: 'iframe',
		href: url,
		padding: 10,
        height: 650,
        width: 500
	});	
}

function showMessage(divId, content) {

	$(divId).className = 'message';

	if ($(divId) != null) {
		$(divId).innerHTML = content;
		$(divId).show();
		messageHide(divId);
	}
}

function messageHide (divId) {
	
	if ($(divId) != null) {
		new Effect.Highlight(divId, {
			startcolor :'#F39249'
		});
	}
}

function CookiesEnabled() {

	if (!navigator.cookieEnabled
			&& location.href != 'http://www.homeconnector.com/pages/enable_cookies'
			&& location.href != 'http://homec/pages/enable_cookies') {

		window.location = '/pages/enable_cookies';
	}
}

function removeFromFavorites(property_id, notice_id){
	
	var successMess = "The Property has been removed from your favorites.";
	var errorMess = "Error removing the property from favorites.";
	var url = '/properties/remove/id:'+property_id+'/type:3';
	
	$('#loading').load(url, function() {
		//close modal window
		//$.modal.close();
	});
	
	
}

function savePropertyNotes(property_id, form_id, notice_id){
	
	if ($(form_id) != null){
		formParameters = $(form_id).serialize(true);}
	else{
		formParameters = null;}
			
	var url = '/properties/save_notes/id:'+property_id;	
	var str = $.post(url, $('#'+form_id).serialize());
			
	$('#'+notice_id).show();
	$("#"+form_id).text(str);
	setTimeout("parent.$.fancybox.close();", 2000);
}

function saveLeadInformation(url, form_id, notice_id) {

	var successMess = "The info has been sent";
	var errorMess = "Error sending info, please contact us to info@homeconnector.com";

	if ($(form_id) != null)
		formParameters = $(form_id).serialize(true);
	else
		formParameters = null;
		
		$('#'+form_id).load(url, function() {
	});
}

function closeShadowBox(){
	
	parent.$.fancybox.close();
}

function numbersonly(myfield, e, dec) {
	var key;
	var keychar;

	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	keychar = String.fromCharCode(key);

	// control keys
	if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13)
			|| (key == 27))
		return true;

	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;

	// decimal point jump
	else if (dec && (keychar == ".")) {
		myfield.form.elements[dec].focus();
		return false;
	} else
		return false;
}

/*
 * This script and many more are available free online at The JavaScript
 * Source!! http://javascript.internet.com Created by: Cyanide_7 |
 */
var isNN = (navigator.appName.indexOf("Netscape") != -1);

function autoTab(input, len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode;
	var filter = (isNN) ? [ 0, 8, 9 ] : [ 0, 8, 9, 16, 17, 18, 37, 38, 39, 40,
			46 ];
	if (input.value.length >= len && !containsElement(filter, keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input) + 1) % input.form.length].focus();
	}

	function containsElement(arr, ele) {
		var found = false, index = 0;
		while (!found && index < arr.length)
			if (arr[index] == ele)
				found = true;
			else
				index++;
		return found;
	}

	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)
				index = i;
			else
				i++;
		return index;
	}
	return true;
}

function getSavedProperties(url, divId, loaderClass, typeId){

	if($('#'+divId).html()==''){
	
		$('#'+divId).html('<div class="'+loaderClass+'"></div>');
		$('#'+divId).load(url);
	}	
}

function viewPropertyPhotos(id){

	url = '/properties/view_photos_url/id:'+id;	

	$.fancybox({
		type: 'iframe',
		href: url,
		padding: 10,
        height: 450,
        width: 550
	});		
}

function JsSavedSearchesIdlead(divId, loaderClass){	
	
	var url='/properties/search_save_result';
	
	if($('#'+divId).html()==''){
	
		$('#'+divId).html('<div class="'+loaderClass+'"></div>');
		$('#'+divId).load(url);
	}	
}
function JsDeletesearch(id){ 

var answer = confirm("Do you really want to delete this sarch?")

	if (answer){
		
		var url='/properties/delete_search/id:'+id;
		var divId='resultsearchsave';
	
		$('#'+divId).html('<div class="loader"></div>');			
		$('#'+divId).load(url);		
		setTimeout("JsSavedSearchesIdlead('resultsearchsave','loader')",1500);
	}
}

function clearForm(formId) {
	
	
	$(':input','#'+formId)
	 .not(':button, :submit, :reset, :hidden')
	 .val('')
	 .removeAttr('checked')
	 .removeAttr('selected');
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g, '');
	if (isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);

	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if (cents < 10)
		cents = "0" + cents;
	
	for ( var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + ','
				+ num.substring(num.length - (4 * i + 3));
	//return (((sign) ? '' : '-') + '$' + num + '.' + cents);
	return (((sign) ? '' : '-') + '$' + num);
}

function removeFormatCurrency(num) {
	num = num.toString().replace(/\$|\,/g, '', '.');	
	return num;
}

function showMessage (divId, content){

	/*
	$(divId).className = 'message';

	if ($(divId) != null) {
		$(divId).innerHTML = content;
		$(divId).show();						
		messageHide(divId);
	}*/
}

function messageHide (divId) {
	
	/*
	if ($(divId) != null) {
		new Effect.Highlight(divId, {
			startcolor :'#F39249'
		});
		new Effect.Fade(divId, {
			duration :4.5
		});
	}
	*/
}

function saveLeadAction(url, form_id, notice_id, successMess, errorMess, show_message) {

	if($(form_id)!=null){
		formParameters = $(form_id).serialize(true);
	}	
	else{
		formParameters = null;
	}	
				
	$('#'+form_id).load(url);
}

function JsContador(inicia){	
	
	var inicia=inicia; 
	var valor=$('#contadorPhoto').val();
	var total=$('#phototal').val();	
	var mem=$('#contadorPhoto').val();
	
	valor = parseInt(valor) ;
	total = parseInt(total);
		
	if(inicia=='1'){
		
		if( valor < total){
			
			valor=parseInt(valor) +1;
			//alert(valor);
			$('#contadorPhoto').val(valor);
			var message='Photo '+valor+' to '+ total;
			$('#NumberPhoto').html(message)		
		}else {

			$('#contadorPhoto').val(1);
			var message='Photo '+1+' to '+ total;
			$('#NumberPhoto').html(message)		
		}
	}

	if(inicia=='2'){		
		
		if(valor ==1){
			
			var message='Photo '+total+' to '+ total;
			$('#contadorPhoto').val(total);				
			$('#NumberPhoto').html(message);				

		}else{	
	
			if( valor  !=1 &&  valor <= total){
				valor=parseInt(valor) -1;					
				$('#contadorPhoto').val(valor);
				var message='Photo '+valor+' to '+ total;
				$('#NumberPhoto').html(message);
			}			
		}	
	}	
}