/**
* Fairoffersystem Application library written by Manjeet Rangi
*
// Copyright (c) 2007, 2008 Manjeet Rangi
**/

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

// Set up object
var Aviniti = new Object();

// Global app object
var theApp = new Object(); 


if(!Application) var Application = {};
Application.Agents = Class.create();


Application.Agents.prototype = {	
	initialize: function() {
    	var theApp = this; 
    	this.offerId=0;
	},
	addQuickLink: function(pageurl,pagetitle,container) {
		//add to links
		var newajax = new Ajax.Updater(
						container, 
						'/index/addquicklink', 
						{	method: 'post', 
							parameters: 'p='+ pagetitle +'&url='+ pageurl,
							evalScripts:true,						
							onLoading: function(request) 
							{
							//	showLoader(responseId)
							}
						}
					);  	 
		
	},
    RemoveQuickLink:function(pageurl,pagetitle,container)
	{
		//add to links
		var newajax = new Ajax.Updater(
						container, 
						'/index/removequicklink', 
						{	method: 'post', 
							parameters: 'p='+ pagetitle+'&url='+ pageurl,
							evalScripts:true,						
							onLoading: function(request) 
							{
							//	showLoader(responseId)
							}
						}
					);  	 
		
	},
	
	showofferForm:function(formid)
	{
		$(formid).toggle();		
		
	},
	hideForm:function(formid)
	{
		$(formid).hide();		
		
	},	
	makeOffer:function(formid,container)
	{
					
		// Make an Offer
		var newajax = new Ajax.Updater(
						container, 
						'/agents/offers/insertoffer', 
						{	method: 'post', 
							parameters: Form.serialize(formid),
							evalScripts:true,						
							onLoading: function(request) 
							{
								showLoader(container) 
							}
						}
					);  	 				
		
		return false;			
	},
	makeVisitorOffer:function(formid,container)
	{

		if($('agent_name').value.trim()=='')
		{
			alert('Agent name is required');
			return false;
		}							
		if($('agent_email').value.trim()=='')
		{
			alert('Email address is required');
			return false;
		}		
		if($('agent_phone').value.trim()=='')
		{
			alert('Phone is required');
			return false;
		}				
		if($('agent_broker').value.trim()=='')
		{
			alert('Broker Information is required');
			return false;
		}				
		
		// Make an Offer
		var newajax = new Ajax.Updater(
						container, 
						'/visitors/property/insertoffer', 
						{	method: 'post', 
							parameters: Form.serialize(formid),
							evalScripts:true,						
							onLoading: function(request) 
							{
								showLoader(container) 
							}
						}
					);  	 				
		
		return false;			
	},
	makeOthersOffer:function(formid,container)
	{
		if($('agent_name').value.trim()=='')
		{
			alert('Agent name is required');
			return false;
		}							
		if($('agent_email').value.trim()=='')
		{
			alert('Email address is required');
			return false;
		}		
		if($('agent_phone').value.trim()=='')
		{
			alert('Phone is required');
			return false;
		}						
		if($('agent_broker').value.trim()=='')
		{
			alert('Broker Information is required');
			return false;
		}				
		
		// Make an Offer
		var newajax = new Ajax.Updater(
						container,  
						'/agents/offers/othersoffer', 
						{	method: 'post', 
							parameters: Form.serialize(formid),
							evalScripts:true,						
							onLoading: function(request) 
							{
								showLoader(container) 
							}
						}
					);  	 				
		
		return false;				
		
		
		
	},
	addtoWatch:function(mls,container)
	{
		//add to watchlist
		var newajax = new Ajax.Updater(
						container, 
						'/agents/properties/addwatch', 
						{	method: 'post', 
							parameters: 'm='+ mls,
							evalScripts:true,						
							onLoading: function(request) 
							{
							//	showLoader(responseId) 
							}
						}
					);  	 		
		
	},
	removeWatch:function(mls,container)
	{
		//remove watch list
		var newajax = new Ajax.Updater(
						container, 
						'/agents/properties/removewatch', 
						{	method: 'post', 
							parameters: 'm='+ mls,
							evalScripts:true,						
							onLoading: function(request) 
							{
							//	showLoader(responseId) 
							}
						}
					);  	 				
	},
	showMessageForm:function(formid)
	{
		$(formid).toggle();
	},	
	sendMessage:function(formid,container)
	{
		// Make an Offer
		var newajax = new Ajax.Updater(
						container, 
						'/agents/index/sendmessage', 
						{	method: 'post', 
							parameters: Form.serialize(formid),
							evalScripts:true,						
							onLoading: function(request) 
							{
								showLoader(container) 
							}
						}
					);  	 				
		
		return false;			
		
	},
	showLoader:function(container)
	{
		$(container).innerHTML='<img src="/images/loader.gif" />';
	},
	checkEmail:function()
	{		
		// Make an Offer
		var newajax = new Ajax.Request(
						'/visitors/index/checkemail', 
						{	method: 'post', 
							parameters: 'email='+ $('agent_email').value,
							evalScripts:true,	
							onComplete:function(response)
							{
								if(response.responseText=='1')
								{
									$('agent_password_div').show();
								}
								else								
								{
									$('agent_password_div').hide();
								}
								$('email_check_loader').hide();
								//
							},
							onLoading: function(request) 
							{								
								$('email_check_loader').innerHTML ='Checking email address .....';
								$('email_check_loader').show();
							}
						}
					);  	 		
		
	},
	checkWatchEmail:function()
	{		
		// Make an Offer
		var newajax = new Ajax.Request(
						'/visitors/index/checkemail', 
						{	method: 'post', 
							parameters: 'email='+ $('watch_agent_email').value,
							evalScripts:true,	
							onComplete:function(response)
							{
								if(response.responseText=='1')
								{
									$('watch_agent_password_div').show();
								}
								else								
								{
									$('watch_agent_password_div').hide();
								}
								$('watch_email_check_loader').hide();
								//
							},
							onLoading: function(request) 
							{								
								$('watch_email_check_loader').innerHTML ='Checking email address .....';
								$('watch_email_check_loader').show();
							}
						}
					);  	 		
		
	},	
	addtoVisitorWatchForm:function(div_id)
	{
		$(div_id).toggle();
	},
	addtoVisitorWatch:function(formid,container)
	{

		if($('watch_agent_name').value.trim()=='')
		{
			alert('Agent name is required');
			return false;
		}							
		if($('watch_agent_email').value.trim()=='')
		{
			alert('Email address is required');
			return false;
		}		
		if($('watch_agent_phone').value.trim()=='')
		{
			alert('Phone is required');
			return false;
		}						
		if($('watch_agent_broker').value.trim()=='')
		{
			alert('Broker Information is required');
			return false;
		}				
		
		// Make an Offer
		var newajax = new Ajax.Updater(
						container, 
						'/visitors/property/addwatch', 
						{	method: 'post', 
							parameters: Form.serialize(formid),
							evalScripts:true,						
							onLoading: function(request) 
							{
								showLoader(container) 
							}
						}
					);  	 				
		
		return false;					
		
		
	},
	addtoOthersWatch:function(formid,container)
	{

		if($('watch_agent_name').value.trim()=='')
		{
			alert('Agent name is required');
			return false;
		}							
		if($('watch_agent_email').value.trim()=='')
		{
			alert('Email address is required');
			return false;
		}		
		if($('watch_agent_phone').value.trim()=='')
		{
			alert('Phone is required');
			return false;
		}						
		if($('watch_agent_broker').value.trim()=='')
		{
			alert('Broker Information is required');
			return false;
		}				
		
		// Make an Offer
		var newajax = new Ajax.Updater(
						container, 
						'/agents/offers/addOtherswatch', 
						{	method: 'post', 
							parameters: Form.serialize(formid),
							evalScripts:true,						
							onLoading: function(request) 
							{
								showLoader(container) 
							}
						}
					);  	 				
		
		return false;					
		
		
	},
	acceptOffer:function(offerId)
	{
	  if(confirm("Are you sure you want to accept this offer?"))	
	  {
	  	$('offer_comment_header_text').innerHTML = '<h2>Please explain why you are accepting this offer.</h2>';
	  	$('offer_comments').show();
	  	$('offer_comments').scrollTo();	
	  	try
	  	{  	
	  		$('txtcomments').focus();
	  	}catch(e){}
	  	
	  	$('offer_id').value=offerId;
	  	Event.observe('offer_comments_form', 'submit', function(event)
				{
					var newajax = new Ajax.Updater(
							'offer_comment_status', 
							'/agents/offers/accept', 
							{	method: 'post', 
								parameters: Form.serialize('offer_comments_form'),
								onLoading: function(request) 
								{
									$('offer_comment_status').show();
									showLoader('offer_comment_status') 
								}
							}
						);  															
					$('offer_comments_form').reset();
					$('offer_comments').hide();					
					Event.stop(event);				
				});  						
	  }
	  else
	  {
	  	$('offer_comments').hide();
	  	return false;
	  }
	  
		
	},
	rejectOffer:function(offerId)
	{
	  if(confirm("Are you sure you want to reject this offer?"))	
	  {
	  	$('offer_comment_header_text').innerHTML = '<h2>Please explain why you are rejecting this offer.</h2>';
	  	$('offer_comments').show();
	  	$('offer_comments').scrollTo();	  	
	  	try
	  	{
	  		$('txtcomments').focus();
	  	}
	  	catch(e) {}
	  	$('offer_id').value=offerId;
	  	Event.observe('offer_comments_form', 'submit', function(event)
				{
					var newajax = new Ajax.Updater(
							'offer_comment_status', 
							'/agents/offers/reject', 
							{	method: 'post', 
								parameters: Form.serialize('offer_comments_form'),
								onLoading: function(request) 
								{
									$('offer_comment_status').show();
									showLoader('offer_comment_status') 
								}
							}
						);  															
					$('offer_comments_form').reset();
					$('offer_comments').hide();					
					Event.stop(event);				
				});  						
	  }
	  else
	  {
	  	$('offer_comments').hide();
	  	return false;
	  }
	  
		
	}	
	,
	revokeOffer:function(offerId)
	{
	  if(confirm("Are you sure you want to revoke this offer? \n\nIf you click OK please give your reason(s) for revoking this offer by typing in the comment box.\n\n "))	
	  {
	  	$('offer_comment_header_text').innerHTML = '<h2>Please explain why you are revoking this offer.</h2>';
	  	$('offer_comments').show();
	  	$('offer_comments').scrollTo();
	  	try
	  	{
	  		$('txtcomments').focus();
	  	}
	  	catch(e)
	  	{
	  		
	  	}
	  	$('offer_id').value=offerId;
	  	
	  		Event.observe('offer_comments_form', 'submit', function(event)
				{
					var newajax = new Ajax.Updater(
						'offer_comment_status', 
						'/agents/offers/revoke', 
						 {	method: 'post', 
							parameters: Form.serialize('offer_comments_form'),
							onLoading: function(request) 
							{				
								$('offer_comment_status').show();
								showLoader('offer_comment_status') 
							}
						 }
						);  					
					Event.stop(event);
					$('offer_comments_form').reset();
					$('offer_comments').hide();
				});  						
	  }
	  else
	  { 
	  	$('offer_comments').hide();
	  	return false;
	  }	 					
	},
	removeInterest:function(agent_id,agent_name,mls,container)
	{
		if(confirm("Are you sure you want to remove Interest for "+ agent_name  +"?"))	
		{
			var newajax = new Ajax.Request(
				'/agents/properties/removeInterest', 
				{	method: 'post', 
					parameters: 'agent='+ agent_id +'&property='+ mls,
					evalScripts:true,	
					onComplete:function(response) 
					{
						$(container).remove();
					}
				}
			);  	 		
		}//end if
	//end function	
	},
	confirmDelete:function(url,mls_no)
	{
		if(confirm("Are you sure you want to delete this property[MLS:"+ mls_no +"]?"))
		{
			window.location.href=url;
		}
		
	},
	showTab:function(tabid)
	{
		var tabs=3;
		for(i=1;i<=tabs;i++)
		{
			$('tab'+i).hide();
			
		}
		
		$('tab'+tabid).show();
		
	},
	confirmActivate:function(pid,action,mls_no,container)
	{
		if(confirm("Are you sure you want to "+  action +"  "+ mls_no  +"?"))	
		{
			var newajax = new Ajax.Request(
				'/brokers/property/chstatus', 
				{	method: 'post', 
					parameters: 'pid='+ pid +'&property='+ mls_no,
					evalScripts:true,	
					onLoading: function(request) 
					{
						$(container).innerHTML= '<img src="/images/act-loader.gif" border="0" />';
					},
					onComplete:function(response) 
					{
						$(container).innerHTML=response.responseText;
					}
				}
			);  	 		
		}//end if						
	}
	
}
theApp = new Application.Agents();

