//Listing full view page related functions
function SubmitContactAgentForm()
{	
	var id = $(this).attr('id').replace('agentContactFormBtnCol', '');
	var uid = $('div#agentContactForm' + id + ' input#listingId').val();
	var name = $('div#agentContactForm' + id + ' input#ClientName').val();
	var email = $('div#agentContactForm' + id + ' input#Email').val();
	var contact = $('div#agentContactForm' + id + ' input#ContactNumber').val();
	var enquiry = $('div#agentContactForm' + id + ' textarea#Enquiry').val();
	var agentEmail = $('div#agentContactForm' + id + ' input#agentEmail').val();		
	
	if(name.length==0 || email.length==0 || contact.length==0 || enquiry.length==0 || agentEmail.length==0)
	{
		$('div#messageHolder' + id).html("<span class='redText'>Please fill in all required informations.</span>"); 
	}
	else
	{
		if(enquiry.length>255)
		{
			$('div#messageHolder' + id).html("<span class='redText'>Sorry, your enquiry message cannot exceed 255 characters.</span>");
			return;
		}
		//$(this).html("<img class='alignRight' src='/Images/Site/rotatingAjaxLoader.gif' alt='' style='width:16px; height:16px;' />");	
		$('tr#btnRow' + id).addClass('invisible');
		$('div#messageHolder' + id).html("<span>Sending your request, please wait...</span>"); 
		$('div#messageHolder' + id).removeClass('invisible');
		$.ajax({
		   type: "POST",
		   url: "/Async/ContactListingAgent.asp",
		   data: "uid=" + escape(uid) + "&id=" + escape(id) + "&agentEmail=" + escape(agentEmail) + "&clientName=" + escape(name) + "&email=" + escape(email) + "&contact=" + escape(contact) + "&enquiry=" + escape(enquiry),
		   success: function(msg){
		     var arrMsg = msg.split('#3#');			 
			 $('div#messageHolder'+arrMsg[0]).html(arrMsg[1]);
			 //$('div#messageHolder'+arrMsg[0]).removeClass('invisible');
			 //$('td#agentContactFormBtnCol' + id ).html("<input type='button' id='" + arrMsg[0] + "' name='send' value='send' class='formBtn alignRight last' />");
			 $('tr#btnRow' + id).removeClass('invisible');
		   }
		 });
	}
}
//function checkEntry(jqElm, divId)
//{
//	var txt = jqElm.val();
//	var lid = $('div#' + divId + ' input#listingId').val();
//	var compTxt = "Regarding ID#" + lid;
//	if(txt.indexOf(compTxt)!=0)
//	{
//		txt = txt.replace(compTxt,'');
//		txt = compTxt + ' ' ;
//		jqElm.val(txt);
//	}
//}
function AddRemoveFromWatchList()
{
	var listingId = $(this).children('div').attr('id');						
	var ajaxUrl = $(this).children('div').html().indexOf("Remove from watch list") > -1 ? 
					"/Async/WatchList.asp?act=Remove&id=" + escape(listingId) : 
					"/Async/WatchList.asp?id=" + escape(listingId);			
		
	$.ajax({url: ajaxUrl, cache: false, success: function(data){													  	
		var arrData = data.split('#3#');
		$('div#fullViewWatchListWrapper').children('div').html(arrData[1].indexOf("Remove from watch list")>-1 ? "Remove from watch list" : "Add to watch list");		
	}});	
}

var $modal;
function InitializeModalWindow()
{
	var id = $(this).attr('id').replace('frm_', '');	
	$modal = $('<div></div>')
		.html($('div#' + id).html())
		.dialog({
			autoOpen: false,
			modal:true,
			width:450,
			height:290,
			title: 'Send to a Friend',
			beforeClose : function(){$modal.dialog('destroy');},
			open : function() {
				$('.formBtn').removeShadow();
				$('.formBtn').dropShadow({right: 1, bottom: 1, opacity: 0.3, blur: 2});
			}
		});		 
	$modal.dialog('open');
}
function EmailFriend()
{
	var id = $('div.ui-dialog input#listingId').val();
	var senderName = $('div.ui-dialog input#ClientName').val();
	var recipientName = $('div.ui-dialog input#FriendName').val();
	var recipientEmail = $('div.ui-dialog input#FriendEmail').val();	
	
	$.ajax({
	   type: "POST",
	   url: "/Async/SendReferralEmail.asp",
	   data: "id=" + escape(id) + "&senderName=" + escape(senderName) + "&recipientName=" + escape(recipientName) + "&recipientEmail=" + escape(recipientEmail),
	   success: function(msg){
		 UpdateModalWindow(msg);
	   }
	 });
}
function CloseModalWindow()
{
	$modal.dialog('destroy');
	$modal.dialog('close');	
}
function UpdateModalWindow(msg)
{
	$modal.dialog('destroy');
	$modal = $("<div></div>")
		.html(msg)
		.dialog({
			autoOpen: false,
			modal:true,
			width:450,
			height:310,
			title: 'Send to a Friend',
			beforeClose : function(){$modal.dialog('destroy');}
		});
	$modal.dialog('open');
}
function ShowCalendarOptions()
{
	$(this).parent('p').next('div.AddToCalendarList').css('display', 'block');
	return false;
}
function HideCalendarOptions()
{
	$(this).parent('div.AddToCalendarList').css('display', 'none');
	return false;
}
function UpdateFrameSize()
{
	var id = $(this).attr('id');
	var height = 480;  //572     816
	var scrolling = "no";
	if(id=="rentalInv"){height = 460; scrolling = "no";} // 380
	if(id=="yourHome"){height = 490; scrolling = "no";} //602
	if(id=="community"){height = 470; scrolling = "no";}
	$('iframe#communityFrame').attr('height', height);
	$('iframe#communityFrame').attr('scrolling', scrolling);
}

//Element binding
$('td.agentContactFormCol').bind('click', SubmitContactAgentForm);
$('div#fullViewWatchListWrapper').bind('click', AddRemoveFromWatchList);
$('img#printPage').bind('click', function PrintPage(){window.print();});
$('img.popUpOpener').bind('click', InitializeModalWindow);
$('a.AddToCalendar').bind('click', ShowCalendarOptions);
$('a.cancelCal').bind('click', HideCalendarOptions);
$('ul#communityTabs li a').bind('click', UpdateFrameSize);

$(document).ready(function() {	
	$('a.fakeBtn').dropShadow({right: 1, bottom: 1, opacity: 0.3, blur: 2});
	$('.formBtn').dropShadow({right: 1, bottom: 1, opacity: 0.3, blur: 2});
});
