// JavaScript Document
// start popup/overlay code

function getMorelocal()
{
	if($('#area_id').val().match(/\d\d?\d?/))
	{
		var my_ajx_url = '/add-property/index.shtml?ajax=1&moreLocal='+$('#area_id').val();
		$.get(my_ajx_url,function(ajax_data){
				$.blockUI({message: ajax_data , css: { width:'590px' }});
				});
	}

}

function closeDialog()
{
	$.unblockUI();
	setMoreLocal('');
	return false;
}
function showScraperWarning(friendlyName) {

}

function closeSW(friendlyName) {
	var mySW = document.getElementById("scraper_warning");
	var mySWi = document.getElementById(friendlyName);

	mySW.style.display = "none";
	mySWi.style.display = "none";
}

// end popup/overlay code

/*
   Simple Image Trail script- By JavaScriptKit.com
   Visit http://www.javascriptkit.com for this script and more
   This notice must stay intact
 */

var w=1
var h=1

if (document.getElementById || document.all)
	document.write('<div id="trailimageid"><img id="ttimg" src="/images/s.gif" /></div>')

function gettrailobj()
{
	if (document.getElementById) return document.getElementById("trailimageid").style
	else if (document.all) return document.all.trailimagid.style
}

function truebody()
{
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function hidetrail()
{
	document.onmousemove=""
		document.getElementById('ttimg').src='/images/s.gif'
		gettrailobj().visibility="hidden"
		gettrailobj().left=-1000
		gettrailobj().top=0
}


function showtrail(width,height,file)
{
	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1)
	{
		w=width
			h=height

			// followmouse()

			document.getElementById('ttimg').src=file
			document.onmousemove=followmouse
			gettrailobj().visibility="visible"
			gettrailobj().width=w+"px"
			gettrailobj().height=h+"px"


	}
}


function followmouse(e)
{

	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1)
	{
		var xcoord=20
			var ycoord=0

			if (typeof e != "undefined")
			{
				xcoord+=e.pageX
					ycoord+=e.pageY
			}
			else if (typeof window.event !="undefined")
			{
				xcoord+=truebody().scrollLeft+event.clientX
					ycoord+=truebody().scrollTop+event.clientY
			}

		var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
			var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)

			if (xcoord+w+3>docwidth)
				xcoord=xcoord-w-(20*2)

					if (ycoord-truebody().scrollTop+h>truebody().clientHeight)
						ycoord=ycoord-h-0;

		gettrailobj().left=xcoord+"px"
			gettrailobj().top=ycoord+"px"

	}

}

// END

function setMoreLocal(val)
{
	$.unblockUI(); 
	$('#morelocal').val(val);

	if (val)
	{
		$('#showmorelocal').html('<p>'+val+' (<a href="javascript:getMorelocal()">edit</a>)</p>');
	}
	else
	{
		$('#showmorelocal').html('');
	}
}
// highlight a specific nav tab

function highlightTab()
{
	var page_url = window.location.href;

	if (location.pathname.length <= 1)
	{
		$("#nav1").css({backgroundImage: 'url(/images/nav-on.gif)', backgroundPosition: 'center top', backgroundRepeat: 'no-repeat'});
	}
	if (page_url.match(/developments/i))
	{
		$("#nav2").css({backgroundImage: 'url(/images/nav-on.gif)', backgroundPosition: 'center top', backgroundRepeat: 'no-repeat'});
	}
	if (page_url.match(/add-property/i))
	{
		$("#nav3").css({backgroundImage: 'url(/images/nav-on.gif)', backgroundPosition: 'center top', backgroundRepeat: 'no-repeat'});
	}
	if (page_url.match(/my-properties/i))
	{
		$("#nav4").css({backgroundImage: 'url(/images/nav-on.gif)', backgroundPosition: 'center top', backgroundRepeat: 'no-repeat'});
	}
	if (page_url.match(/branch-details/i))
	{
		$("#nav5").css({backgroundImage: 'url(/images/nav-on.gif)', backgroundPosition: 'center top', backgroundRepeat: 'no-repeat'});
	}
	if (page_url.match(/marketing/i))
	{
		$("#nav6").css({backgroundImage: 'url(/images/nav-on.gif)', backgroundPosition: 'center top', backgroundRepeat: 'no-repeat'});
	}
	if (page_url.match(/s1-recommends/i))
	{
		$("#nav7").css({backgroundImage: 'url(/images/nav-on.gif)', backgroundPosition: 'center top', backgroundRepeat: 'no-repeat'});
	}
	if (page_url.match(/contact/i))
	{
		$("#nav8").css({backgroundImage: 'url(/images/nav-on.gif)', backgroundPosition: 'center top', backgroundRepeat: 'no-repeat'});
	}
}

// END







// highlight homepage table

function highlightRows()
{
	$("#hpTable tr").hover(function(){
			$(this).addClass('whiteBG');
			},
			function(){
			$(this).removeClass('whiteBG');
			}
			);
}

// END



// highlight my properties table

function highlightRows2()
{
	$("#myPropertiesTable tr").hover(function(){
			$(this).addClass('whiteBG');
			},
			function(){
			$(this).removeClass('whiteBG');
			}
			);
}

// END



// add property details


function editPropertyDetails()
{
	var my_ajx_url = '/add-property/index.shtml?ajax=1&edit_property=1&prop_id='+$('#propid').val();
	$.blockUI({message: '<center><img src="/images/loading.gif" /></center>'  ,css: { width: '590px',color:'#fff' }});
	$.get(my_ajx_url,function(ajax_data){

			$('#addProperty').html(ajax_data);
			$.unblockUI();
			});
}





function addPhotographs()
{
	var my_ajx_url = '/add-property/photos.cgi?ajax=1&photo=1&prop_id='+$('#propid').val();
	$.blockUI({message: '<center><img src="/images/loading.gif" /></center>'  ,css: { width: '590px',color:'#000' }});
	$.get(my_ajx_url,function(ajax_data){
			$('#addProperty').html(ajax_data);
			$.unblockUI();
			});
}

function addSchedule()
{

	var my_ajx_url = '/add-property/index.shtml?ajax=1&schedule=1&prop_id='+$('#propid').val();
	$.blockUI({message: '<center><img src="/images/loading.gif" /></center>'  ,css: { width: '590px',color:'#000' }});
	$.get(my_ajx_url,function(ajax_data){
			$('#addProperty').html(ajax_data);
			$.unblockUI();
			});

	/*$("#addProperty").css({display: 'none'});
	  $("#addPhotographs").css({display: 'none'});
	  $("#addSchedule").css({display: 'block'});
	  $("#addThanks").css({display: 'none'});
	 */
}

function addPropertyDetails()
{
	$("#addProperty").css({display: 'block'});
	$("#addPhotographs").css({display: 'none'});
	$("#addSchedule").css({display: 'none'});
	$("#addThanks").css({display: 'none'});
}

function uploadProperty()
{
	$("#addProperty").css({display: 'none'});
	$("#addPhotographs").css({display: 'none'});
	$("#addSchedule").css({display: 'none'});
	$("#addThanks").css({display: 'block'});
}

// rental

function addPhotographsRental()
{
	$("#addProperty").css({display: 'none'});
	$("#addPhotographs").css({display: 'block'});
	$("#addThanks").css({display: 'none'});
}

function addPropertyDetailsRental()
{
	$("#addProperty").css({display: 'block'});
	$("#addPhotographs").css({display: 'none'});
	$("#addThanks").css({display: 'none'});
}

function uploadPropertyRental()
{
	$("#addProperty").css({display: 'none'});
	$("#addPhotographs").css({display: 'none'});
	$("#addThanks").css({display: 'block'});
}
// END

// show custom stats on homepage
function showCustom()
{
	$("#hpDropdowns").css({display: 'block'});
	$(".hdrop1").css({display: 'block'});
}

// END

// show custom stats on my properties page
function showCustom2()
{
	$("#hpDropdowns2").css({display: 'block'});
	$("#custDays").css({src : '/images/but-custom-on.gif'});
}

// END

// show stats for sale or rent on homepage
function showStatsSale()
{
	$("#stats-sale").css({display: 'block'});
	$("#stats-rent").css({display: 'none'});
	$("#issale").val("sale");

}
function showStatsRent()
{
	$("#stats-sale").css({display: 'none'});
	$("#stats-rent").css({display: 'block'});
	$("#issale").val("rent");
}
function showStatsSaleSearch()
{
	$("#issale").val("sale");

	var current_url = document.URL;
	// REGEX TO CLEAN UP THE URL PARAM
	var myurl = current_url.replace(/\&?issale=(?:sale|rent|undefined\&?)/g,'');
	myurl = myurl.replace(/\&?prop_option=[^&]+/, '');

	if(document.URL.match(/\?/))
	{
		myurl = myurl +"&issale=sale";
	}
	else
		myurl = myurl + "?issale=sale";
		
	window.location.assign(myurl);
}
function showStatsRentSearch()
{
	$("#issale").val("rent");

	var current_url = document.URL;
	var myurl = current_url.replace(/\&?issale=(?:sale|rent|undefined\&?)/g,'');
	myurl = myurl.replace(/\&?prop_option=[^&]+/, '');

	if(document.URL.match(/\?/))
	{
		myurl = myurl + "&issale=rent";
	}
	else
	{
		myurl = myurl + "?issale=rent";
	}
	window.location.assign(myurl);
}
// END
//my properties keyword search
function showKeywords()
{
	$("#myPropertiesKeywordsDiv1").css({display: 'none'});
	$("#myPropertiesKeywordsDiv2").css({display: 'block'});
}
function hideKeywords()
{
	$("#myPropertiesKeywordsDiv1").css({display: 'block'});
	$("#myPropertiesKeywordsDiv2").css({display: 'none'});
}
// END
// some values loading
function form_param()
{
	if($(document).getUrlParam("result_page"))
	{
		$("#result_page").selectOptions($(document).getUrlParam("result_page"));
	}
}
//end
function property_option_select()
{
}
function onGoPropertyClick()
{
	var myprop =  $(".checkBoxes:checked").val();
	if(!myprop)
	{
		alert('Please select a property.');
		return false;
	}
	if( $('#property_options').val() == 'archive')
	{
		
		if($('#prop_'+myprop).val() == 'archived')
		{
		  alert('This property has already been archived.');
		  return false;
		}
		if( confirm("Are you sure want to archive?"))
		{
			return true;
		}
		return false;
	}
	if( $('#property_options').val() == 'unarchive')
	{
		if($('#prop_'+myprop).val() == 'live')
		{
		  alert('This property has already been live.');
		  return false;
		}
		if( confirm("Are you sure want to restore to live?"))
		{
			return true;
		}
		return false;
	}
	if($('#property_options').val() == 'edit')
	{
		if($(".checkBoxes:checked").length > 1)
		{
			alert('You can only edit one property at a time.');
			return false;
		}
	}else
	{
		alert('Please select an option from the drop-down menu.');
		return false;
	}
} // end

function checkprice()
{
	var orgprice = $('#original_price').val().replace(/\D/g,'');
        var orgpricemod = $('#original_pricemodifier').val();
	var modified = 0;
        if(orgprice != '')
	{
	  var newprice = $('#price').val().replace(/\D/g,'');
	  if(orgprice != newprice) 
	  {
	    modified =1;
	  } 
	}
        if(orgpricemod != '')
        {
            var newpricemod = $('#price_modifier').val().replace(/\s+/g,'');
            if(orgpricemod != newpricemod)
             {
                modified =1;
             }
                
        }
        
       if(modified === 1)
       {
            if(confirm('Do you want the property to display a price change banner and push out on email alerts?'))
	    {
	        $('#disable').val('n');
            }
       }
}
//
function numberPerPage()
{
	var current_url =document.URL;
	var myurl = current_url.replace(/\&?result_page=\d?\d?&?/g,'');
	if(document.URL.match(/\?/))
	{
		myurl = myurl + "&result_page=" +  $("#result_page").val();
	}
	else
	{
		myurl = myurl + "?result_page=" +  $("#result_page").val();
	}
	window.location.assign(myurl);
}
function datepost(val)
{
	var current_url =document.URL;
	var myurl = current_url.replace(/(?:&?dateposted=(day\d)?\d?\d?(totals)?&?|&?prop_option=Please.select.....|\&?issale=(?:sale|rent|undefined)|date_range\=Go)/g,'');
	myurl = myurl.replace(/&day[12]=[0-9]+|&month[12]=[0-9]+|&year[12]=[0-9]+/g,'');
	if(document.URL.match(/\?/))
	{
		myurl = myurl + "&dateposted=" +val+"&issale="+$('#issale').val();
	}
	else
	{
		myurl = myurl + "?dateposted="+val+"&issale="+$('#issale').val();
	}
	window.location.assign(myurl);
}
function orderby(val)
{
	var current_url = document.URL;
	var myurl = current_url.replace(/\&?orderby=\*?(%2A)?(\w)+/g,'');
	if(document.URL.match(/\?/))
	{
		myurl = myurl + "&orderby=" +val;
	}
	else
	{
		myurl = myurl + "?orderby=" +val;
	}
	window.location.assign(myurl);
}
/// send point of sale
function sendpointofsale()
{
	$.get('/branch-details/index.shtml?requestSalePack=1',function(data){
			$('#pointofsale').hide();
			$('#pointsalesucess').show();
			});
}
/// export to excel link
$(document).ready(function(){
			$("#excel").click(function(){
					var current_url = document.URL;
					var new_url;
					if (current_url.match(/\?/))
					{
						new_url = current_url + "&excel=yes";
					}
					else
					{
						new_url = current_url + "?excel=yes";
					}
					window.location = new_url;
			});
});
// on load
function onload_run()
{
	highlightTab();
	highlightRows();
	highlightRows2();
	form_param();
	if($("#issale").val() == 'sale')
	{
		showStatsSale();
	}
	else
	{
		showStatsRent();             
	}
	if($('#pointsalesucess').length > 0)
	{
		$('#pointsalesucess').hide();
	}
	$("#selectAll").click(function()
	{
		var checked_status = this.checked;
		$("input[@name=props]").each(function()
		{
			this.checked = checked_status;
		});
	});
}
// onload functions
	$(document).ready(function(){
			   onload_run();
			  });

