$(document).ready(function(){
	var url_property_detail		 = escape(get_url());
	  /*  - Popup "Contact us" Layer: start */ 
	var fcp_contact_form_id			= 'fcp_contact_us_form';
	var fcp_first_name_id			= 'fcp_first_name';
	var fcp_last_name_id			= 'fcp_last_name';
	var fcp_email_address_id 		= 'fcp_email_address';
	var fcp_phone_number_id			= 'fcp_phone_number';
	var fcp_comment_id				= 'fcp_comment';
	var fcp_remember_id				= 'fcp_remember';
	var fcp_submit_id 				= 'fcp_submit';
	var fcp_close_btn_id		= 'fcp_close_btn';
	var fcp_confirmation_message_id	= 'fcp_confirmation_message';
	var fcp_error_message_id   		= 'fcp_error_message';
	

	var fcp_first_name_input	  	=  $("#" + fcp_first_name_id);
	var fcp_last_name_input			=  $("#" + fcp_last_name_id);
	var fcp_email_address_input		=  $("#" + fcp_email_address_id);
	var fcp_phone_number_input		=  $("#" + fcp_phone_number_id);
	var fcp_comment_input			=  $("#" + fcp_comment_id);
	var fcp_remember_input			=  $("#" + fcp_remember_id);
	var fcp_submit_input			=  $("#" + fcp_submit_id);
	var fcp_close_btn				=  $("#" + fcp_close_btn_id);
	var fcp_error_message_holder 	=  $("#" + fcp_error_message_id);
	
	var mls_num_property_detail		= "";
	var listing_source_propertyd	= "";
	
	fcp_default_values = {"fcp_first_name":"first name", "fcp_last_name":"last name", "fcp_email_address":"email address", "fcp_phone_number":"phone number"};
	
	fcp_first_name_input.bind("keypress", function(e) {
		return validate_key_pressed(e,1);
	  });
	
	fcp_first_name_input.bind("focus", function() {
		clear_error_msg(fcp_error_message_holder);
		clear_txt_input($(this), fcp_default_values[$(this).attr('id')]);
 	 });
	
	fcp_first_name_input.bind("blur", function() {
		fill_txt_input($(this), fcp_default_values[$(this).attr('id')]);
  	});
	
	fcp_last_name_input.bind("keypress", function(e) {
		return validate_key_pressed(e,1);
	  });
	
	fcp_last_name_input.bind("focus", function() {
		clear_error_msg(fcp_error_message_holder);
		clear_txt_input($(this), fcp_default_values[$(this).attr('id')]);
 	 });
	
	fcp_last_name_input.bind("blur", function() {
		fill_txt_input($(this), fcp_default_values[$(this).attr('id')]);
  	});
	
	fcp_email_address_input.bind("keypress", function(e) {
		return validate_key_pressed(e,2);
	  });
	  
	 fcp_email_address_input.bind("focus", function() {
		clear_error_msg(fcp_error_message_holder);
		clear_txt_input($(this), fcp_default_values[$(this).attr('id')]);
 	 });
	
	fcp_email_address_input.bind("blur", function() {
		fill_txt_input($(this), fcp_default_values[$(this).attr('id')]);
  	});
	
	fcp_phone_number_input.bind("keypress", function(e) {
		return validate_key_pressed(e,10);
	  });
	
	fcp_phone_number_input.bind("focus", function() {
		clear_error_msg(fcp_error_message_holder);
		clear_txt_input($(this), fcp_default_values[$(this).attr('id')]);
 	 });
	
	fcp_phone_number_input.bind("blur", function() {
		fill_txt_input($(this), fcp_default_values[$(this).attr('id')]);
  	});
	
	fcp_close_btn.bind("click", function(){
		 hide_contact_popup();
	});
	
	fcp_submit_input.bind("click", function(){
		if(check_form_contact_info(fcp_contact_form_id, fcp_email_address_id, fcp_phone_number_id, fcp_error_message_holder, fcp_default_values)){
				if(fcp_comment_input.val() == fcp_default_values['fcp_comment']){ var fcp_comment_value = '';}else{var fcp_comment_value = fcp_comment_input.val();}
				request_sendemail_contact(fcp_first_name_input.val(), fcp_last_name_input.val(), fcp_email_address_input.val(), fcp_phone_number_input.val(), fcp_comment_value, fcp_remember_input.attr('checked') , fcp_error_message_holder, url_property_detail);
			}
	});
	fcp_submit_input.hover(function(){
			$(this).attr("src", "/images/email_this_listing/btn_submit_on.png");
		  }, function() {
			 $(this).attr("src", "/images/email_this_listing/btn_submit.png");										  
	});
	  /*  - Popup "Contact us" Layer: end */ 
	  
	  /*  Popup "Email this search: start" */
	var email_this_search_id    = 'li_email_this_search_link';
	var email_this_search_popup = $("#" + email_this_search_id);
	
	//var url_property_detail	  = escape(get_url());
	var fes_form_id			  = 'send_email_this_search_form';
	var fes_yourname_id		  = 'fes_yourname';
	var fes_email_address_id  = 'fes_email_address';
	var fes_recipientname_id  = 'fes_recipientname';
	var fes_recipientemail_id = 'fes_recipientemail';
	var fes_submit_id	      = 'fes_submit';
	var fes_sendcopy_id		  = 'fes_sendcopy';
	var fes_comment_id		  = 'fes_comment';
	var email_search_error_message_id = 'email_search_error_message';
	var se_close_email_this_search_id = 'se_close_email_this_search_popup';
	
	
	var fes_yourname_input	  	 =  $("#" + fes_yourname_id);
	var fes_email_address_input  =  $("#" + fes_email_address_id);
	var fes_recipientname_input  =  $("#" + fes_recipientname_id);
	var fes_recipientemail_input =  $("#" + fes_recipientemail_id);
	var fes_sendcopy_input 		 =  $("#" + fes_sendcopy_id);
	var fes_submit_input 		 =  $("#" + fes_submit_id);
	var fes_comment_input		 =  $("#" + fes_comment_id);
	var email_search_error_msg_holder = $("#" + email_search_error_message_id);
	var se_close_email_this_search_btn=$("#" + se_close_email_this_search_id);
	
	fes_default_values = {"fes_yourname":"your name", "fes_email_address":"your email address", "fes_recipientname":"recipients name", "fes_recipientemail":"recipients email address", "fes_comment":"I thought you might be interested in this search on easteggrealty.com."};
	
	fes_yourname_input.bind("keypress", function(e) {
		return validate_key_pressed(e,1);
	  });
	
	fes_yourname_input.bind("focus", function() {
		clear_error_msg(email_search_error_msg_holder);
		clear_txt_input($(this), fes_default_values[$(this).attr('id')]);
 	 });
	
	fes_yourname_input.bind("blur", function() {
		fill_txt_input($(this), fes_default_values[$(this).attr('id')]);
		
  	});
	
	fes_email_address_input.bind("keypress", function(e) {
		return validate_key_pressed(e,2);
	  });
	  
	 fes_email_address_input.bind("focus", function() {
		clear_error_msg(email_search_error_msg_holder);
		clear_txt_input($(this), fes_default_values[$(this).attr('id')]);
 	 });
	
	fes_email_address_input.bind("blur", function() {
		fill_txt_input($(this), fes_default_values[$(this).attr('id')]);
		
  	});

	fes_recipientname_input.bind("keypress", function(e) {
		return validate_key_pressed(e,1);
	  });
	
	fes_recipientname_input.bind("focus", function() {
		clear_error_msg(email_search_error_msg_holder);
		clear_txt_input($(this), fes_default_values[$(this).attr('id')]);
 	 });
	
	fes_recipientname_input.bind("blur", function() {
		fill_txt_input($(this), fes_default_values[$(this).attr('id')]);
  	});
	
	 fes_recipientemail_input.bind("keypress", function(e) {
		return validate_key_pressed(e,2);
	  });
	  
	 fes_recipientemail_input.bind("focus", function() {
		clear_error_msg(email_search_error_msg_holder);
		clear_txt_input($(this), fes_default_values[$(this).attr('id')]);
 	 });
	
	fes_recipientemail_input.bind("blur", function() {
		fill_txt_input($(this), fes_default_values[$(this).attr('id')]);
		
  	});
	
	fes_comment_input.bind("focus", function() {
		clear_error_msg(email_search_error_msg_holder);
		clear_txt_input($(this), fes_default_values[$(this).attr('id')]);
 	 });
	
	fes_comment_input.bind("blur", function() {
		fill_txt_input($(this), fes_default_values[$(this).attr('id')]);
		
  	});
	
	
	fes_submit_input.bind("click", function(){
		if(check_form_send_email_info(fes_form_id, fes_yourname_id, fes_email_address_id, fes_recipientname_id, fes_recipientemail_id, email_search_error_msg_holder, fes_default_values)){
			if(fes_comment_input.val() == fes_default_values['fes_comment']){ var fes_comment_value = '';}else{var fes_comment_value = fes_comment_input.val();}
			request_sendemail_this_search(fes_yourname_input.val(), fes_email_address_input.val(), fes_recipientname_input.val(), fes_recipientemail_input.val(), fes_comment_value, fes_sendcopy_input.attr('checked') , email_search_error_msg_holder, url_property_detail);
		}
	});
	
	email_this_search_popup.bind("click", function(){
		show_email_this_search_popup(false);
	});
	
	$('#ny_email_this_search_link').bind("click", function(){
		show_email_this_search_popup(false);
	});
	
	se_close_email_this_search_btn.bind("click", function(){
		 hide_send_email_this_search_popup();
	});
    /*  Popup "Email this search: end" */
	
	/*  Popup "Email this listing: start" */
	var fse_form_id			  = 'send_email_this_listing_form';
	var fse_yourname_id		  = 'fse_yourname';
	var fse_email_address_id  = 'fse_email_address';
	var fse_recipientname_id  = 'fse_recipientname';
	var fse_recipientemail_id = 'fse_recipientemail';
	var fse_submit_id	      = 'fse_submit';
	var fse_sendcopy_id		  = 'fse_sendcopy';
	var fse_comment_id		  = 'fse_comment';
	var send_email_error_message_id = 'send_email_error_message';
	var se_close_popup_id    = 'se_close_popup';
	
	
	var fse_yourname_input	  	 =  $("#" + fse_yourname_id);
	var fse_email_address_input  =  $("#" + fse_email_address_id);
	var fse_recipientname_input  =  $("#" + fse_recipientname_id);
	var fse_recipientemail_input =  $("#" + fse_recipientemail_id);
	var fse_sendcopy_input 		 =  $("#" + fse_sendcopy_id);
	var fse_submit_input 		 =  $("#" + fse_submit_id);
	var fse_comment_input		 =  $("#" + fse_comment_id);
	var send_email_error_msg_holder = $("#" + send_email_error_message_id);
	
	var se_close_popup_btn			= $("#" + se_close_popup_id);
	
	fse_default_values = {"fse_yourname":"your name", "fse_email_address":"your email address", "fse_recipientname":"recipients name", "fse_recipientemail":"recipients email address", "fse_comment":"I thought you might be interested in this listing on easteggrealty.com."};

	
	fse_yourname_input.bind("keypress", function(e) {
		return validate_key_pressed(e,1);
	  });
	
	fse_yourname_input.bind("focus", function() {
		clear_error_msg(send_email_error_msg_holder);
		clear_txt_input($(this), fse_default_values[$(this).attr('id')]);
 	 });
	
	fse_yourname_input.bind("blur", function() {
		fill_txt_input($(this), fse_default_values[$(this).attr('id')]);
		
  	});
	
	
	fse_email_address_input.bind("keypress", function(e) {
		return validate_key_pressed(e,2);
	  });
	  
	 fse_email_address_input.bind("focus", function() {
		clear_error_msg(send_email_error_msg_holder);
		clear_txt_input($(this), fse_default_values[$(this).attr('id')]);
 	 });
	
	fse_email_address_input.bind("blur", function() {
		fill_txt_input($(this), fse_default_values[$(this).attr('id')]);
		
  	});
	
	
	fse_recipientname_input.bind("keypress", function(e) {
		return validate_key_pressed(e,1);
	  });
	
	fse_recipientname_input.bind("focus", function() {
		clear_error_msg(send_email_error_msg_holder);
		clear_txt_input($(this), fse_default_values[$(this).attr('id')]);
 	 });
	
	fse_recipientname_input.bind("blur", function() {
		fill_txt_input($(this), fse_default_values[$(this).attr('id')]);
  	});
	
	 fse_recipientemail_input.bind("keypress", function(e) {
		return validate_key_pressed(e,2);
	  });
	  
	 fse_recipientemail_input.bind("focus", function() {
		clear_error_msg(send_email_error_msg_holder);
		clear_txt_input($(this), fse_default_values[$(this).attr('id')]);
 	 });
	
	fse_recipientemail_input.bind("blur", function() {
		fill_txt_input($(this), fse_default_values[$(this).attr('id')]);
		
  	});
	
	fse_comment_input.bind("focus", function() {
		clear_error_msg(send_email_error_msg_holder);
		clear_txt_input($(this), fse_default_values[$(this).attr('id')]);
 	 });
	
	fse_comment_input.bind("blur", function() {
		fill_txt_input($(this), fse_default_values[$(this).attr('id')]);
		
  	});
	
	
	fse_submit_input.bind("click", function(){
		if(check_form_send_email_info(fse_form_id, fse_yourname_id, fse_email_address_id, fse_recipientname_id, fse_recipientemail_id, send_email_error_msg_holder, fse_default_values)){
			if(fse_comment_input.val() == fse_default_values['fse_comment']){ var fse_comment_value = '';}else{var fse_comment_value = fse_comment_input.val();}
			request_sendemail_this_listing(fse_yourname_input.val(), fse_email_address_input.val(), fse_recipientname_input.val(), fse_recipientemail_input.val(), fse_comment_value, fse_sendcopy_input.attr('checked') , send_email_error_msg_holder, url_property_detail);
		}
	});
	
	fse_submit_input.hover(function(){
			$(this).attr("src", "/images/user/btn_submit_on.png");
		  }, function() {
			 $(this).attr("src", "/images/user/btn_submit.png");										  
		});
	
	se_close_popup_btn.bind("click", function(){
		 hide_email_this_listing_popup();
	});
	
	/*  Popup "Email this listing: end" */
	
});

/* Basic send email: Contact us layer*/
function show_contact_popup(mls_num, listing_source, show_thanku_msg){
	var layer_contact_id = 'contact_popup_container';
	var thanku_msg_holder = $('#fcp_confirmation_message');
	if(show_thanku_msg){ 
		thanku_msg_holder.show(); 
	}else{ 
		thanku_msg_holder.hide();
		$('#fcp_contact_us_form').show();
		$('#fcp_description').show();
	}
	show_layer(layer_contact_id);	 
	mls_num_property_detail 	= mls_num;
	listing_source_propertyd 	= listing_source; 
}

function hide_contact_popup(){
	var layer_email_this_search_id = 'contact_popup_container';
 	hide_layer(layer_email_this_search_id);
}



function request_sendemail_contact(first_name_value, last_name_value, email_address_value, phone_number_value, comment_value, remember_value , error_msg_holder, url_property_detail){
	var url = '/templates/includes/send_email_request_handler.xpnd';
	var url = url + '?type=2';
	var url = url + '&first_name=' + first_name_value;
    var url = url + '&last_name=' + last_name_value;
	var url = url + '&email_address=' + email_address_value;
	var url = url + '&phone_number=' + phone_number_value;
	var url = url + '&email_comment=' + comment_value;
	var url = url + '&remember=' + remember_value;
	var url = url + '&url_property_detail=' + url_property_detail;
	var url = url + '&mls_num=' + mls_num_property_detail;
	var url = url + '&listing_source=' + listing_source_propertyd;
	
	
	 $.getJSON(url, function(result){
	    if(result[0] == 0){
		  display_error(0, result[1], error_msg_holder);
	    }
	    else{
		 display_message_thank_you_contact(result[2]);
	    }
    });
}

function display_message_thank_you_contact(message_result){
	var thank_you_holder = $('#fcp_confirmation_message');
	thank_you_holder.attr("innerHTML", message_result);
	$('#fcp_contact_us_form').hide();
	$('#fcp_description').hide();
	thank_you_holder.show();
}



function check_form_contact_info(contact_form_id, email_address_id, phone_number_id, error_msg_holder, default_values){
	var result = false;
	var fcp_email_address_input = $("#" + email_address_id);
	var fcp_email_address_value = fcp_email_address_input.val();	
	var fcp_phone_number_input = $("#" + phone_number_id);
	var fcp_phone_number_value = fcp_phone_number_input.val();	
	
	if(form_sendemail_values_entered(contact_form_id, default_values)){
		  if(email_address_valid(fcp_email_address_value)){
		  	if(phone_number_valid(fcp_phone_number_value)){
		   		result =true;
			}else{
				display_error(0, '*please enter a valid phone number', error_msg_holder);
			}
		  }
		  else{
		    display_error(0, '*please enter a valid email address', error_msg_holder);
		  }
	}
	else{
	  display_error(1, '', error_msg_holder);
	}
	return result;

}


/* Basic send email: Email this search layer*/
function show_email_this_search_popup(show_thanku_msg){
	var layer_email_this_search_id = 'email_this_search_popup_container';
	var thanku_msg_holder = $('#es_confirmation_message');
	if(show_thanku_msg){ 
		thanku_msg_holder.show(); 
	}else{ 
		thanku_msg_holder.hide();
		$('#send_email_this_search_form').show();
		$('#es_description').show();
	}
	show_layer(layer_email_this_search_id);	  
}

function hide_send_email_this_search_popup(){
	var layer_email_this_search_id = 'email_this_search_popup_container';
 	hide_layer(layer_email_this_search_id);
}



function request_sendemail_this_search(your_name, your_email_addr, recipient_name, recipient_email, email_comment, sendcopyemail, error_msg_holder, url_property_detail ){
	var url = '/templates/includes/send_email_request_handler.xpnd';
	var url = url + '?type=3';
	var url = url + '&your_name=' + your_name;
    var url = url + '&your_email_addr=' + your_email_addr;
	var url = url + '&recipient_name=' + recipient_name;
	var url = url + '&recipient_email=' + recipient_email;
	var url = url + '&email_comment=' + email_comment;
	var url = url + '&sendcopyemail=' + sendcopyemail;
	var url = url + '&url_property_detail=' + url_property_detail;
	 $.getJSON(url, function(result){
	    if(result[0] == 0){
		  display_error(0, result[1], error_msg_holder);
	    }
	    else{
		 display_msg_thankyou_email_search(result[2]);
	    }
    });
}/*end request_send email this search*/

function display_msg_thankyou_email_search(message_result){
	var thank_you_holder = $('#es_confirmation_message');
	thank_you_holder.attr("innerHTML", message_result);
	$('#send_email_this_search_form').hide();
	$('#es_description').hide();
	thank_you_holder.show();
}

/* Basic send email: Email this listing layer*/
function show_email_this_listing_popup(mls_num, show_thanku_msg){
	var layer_email_this_search_id = 'email_this_listing_popup_container';
	var thanku_msg_holder = $('#se_confirmation_message');
	if(show_thanku_msg){ 
		thanku_msg_holder.show(); 
	}else{ 
		thanku_msg_holder.hide();
		$('#send_email_this_listing_form').show();
		$('#se_description').show();
	}
	show_layer(layer_email_this_search_id);	 
	mls_num_property_detail =mls_num;
	
}

function hide_email_this_listing_popup(){
	$('#email_this_listing_popup_container').hide();
}

function request_sendemail_this_listing(your_name, your_email_addr, recipient_name, recipient_email, email_comment, sendcopyemail, error_msg_holder, url_property_detail ){
	
	var url = '/templates/includes/send_email_request_handler.xpnd';
	var url = url + '?type=1';
	var url = url + '&your_name=' + your_name;
    var url = url + '&your_email_addr=' + your_email_addr;
	var url = url + '&recipient_name=' + recipient_name;
	var url = url + '&recipient_email=' + recipient_email;
	var url = url + '&email_comment=' + email_comment;
	var url = url + '&sendcopyemail=' + sendcopyemail;
	var url = url + '&url_property_detail=' + url_property_detail;
	var url = url + '&mls_num=' + mls_num_property_detail;
	
	
	 $.getJSON(url, function(result){
	    if(result[0] == 0){
		  display_error(0, result[1], error_msg_holder);
	    }
	    else{
		 display_message_thank_you_email_this_listing(result[2]);
	    }
    });
}/*end request_sendemail*/



function display_message_thank_you_email_this_listing(message_result){
	var thank_you_holder=$('#se_confirmation_message');
	thank_you_holder.attr("innerHTML", message_result);
	$('#send_email_this_listing_form').hide();
	$('#se_description').hide();
	thank_you_holder.show();
}


function check_form_send_email_info(form_id, yourname_id, email_address_id, recipientname_id, recipientemail_id, error_msg_holder, default_values){
	var result = false;
	var email_address_input = $("#" + email_address_id);
	var recipientemail_input = $("#" + recipientemail_id);
	var email_address_value = email_address_input.val();
	var recipientemail_value = recipientemail_input.val();
	
	
	if(form_sendemail_values_entered(form_id, default_values)){
		  if(email_address_valid(email_address_value) && email_address_valid(recipientemail_value)){
		   	result =true;
		  }
		  else{
		    display_error(0, '*please enter a valid email address', error_msg_holder);
		  }
	}
	else{
	  display_error(1, '', error_msg_holder);
	}
	return result;

}

/* Global functions to email this listing, email this search and contact us */
//returns "true" if all the values have been entered; "false" otherwise
function form_sendemail_values_entered(form_id, default_values){
  var result = true;
  var text_inputs = $("#" + form_id + " input[type!=image]");
  text_inputs.each(function() {
	if($(this).attr('id').indexOf('comment')== -1){
	  if(($(this).val() == '') || ($(this).val() == default_values[$(this).attr('id')])){
	    result = false;
	  }
	}
  });
  return result;
}
