/*
master/chatfilterjs
Copyright 2022 Profile Your City, Inc.
*/

var ida = '';
var idcc = '00000000_00000000';
var chat_closed = false;
//localStorage.setItem('chatinit','false');
var hide_the_chat = false;

chatDataOut = [];
adminChatDataOut = [];

//SCROLL TO LATEST ON FIRST LOAD
document.addEventListener("DOMContentLoaded", function () {

	 newStateCheck(function(currentState){
		 	
	 	if (currentState == 'agent') { 
	 		//console.log('agent chat 31:'+currentState);
	 		chatUpdateItems();
	 	} else {
	 	    //console.log('AUTO chat 34:'+currentState);
	 		aChatUpdateItems();
			if(hide_the_chat) {
				$('#'+ida+'chatbox').hide();
				//console.log('43');	
			}
	 	}
		
	 });
	 
 	$('.closechat').click(function(){
 		$('#'+ida+'chatbox').hide();
 		chat_closed = true;
 		//console.log('chat close 1: '+chat_closed);
 		$.get('https://jasonsellsbellaterra.com/'+ida+'chat-down', function(data) {
 		});
		localStorage.setItem('chat_is_closed','1');
 	}); 
	
 	$('.closechat2').click(function(){
 		$('#'+ida+'chatbox').hide();
 		chat_closed = true;
 		//console.log('chat close 1: '+chat_closed);
 		$.get('https://jasonsellsbellaterra.com/'+ida+'chat-down', function(data) {
 		});
		localStorage.setItem('chat_is_closed','1');
 	});


});
	
//HANDLE ENTER KEY
$('#'+ida+'postchat').keypress(function (e) {
 var key = e.which;

 if(key == 13)  // the enter key code
  {
	//  alert('pressed enter');
	//console.log('pressed enter');
	
	 $('#'+ida+'chatsubmit').text("Sending");
	  
	   newStateCheck(function(currentState){
   		if(currentState == 'agent') { //agent chat
   			//console.log('agent chat 63');
   			//$('#chatsubmit').text("Sending");
			$('#'+ida+'chatmessage').val($('input[name='+ida+'message]').val());
			$('#'+ida+'chatsubmit').click();
			
   			$.get('https://jasonsellsbellaterra.com/'+ida+'chat-up?time=1730208043', function(data) {
   			});
			localStorage.setItem('chat_is_closed','0');
   		} else { //auto chat bot
   			//console.log('AUTO chat 72');
   			$('#'+ida+'chatsubmit').click();
   		}
	   });
	   
	    $('#'+ida+'chatsubmit').text("Send");
	   
    
    return false;  
  }
});   

if ( !Storage.prototype.setObject ) {
    Storage.prototype.setObject = function(key, value) {
        this.setItem(key, JSON.stringify(value));
    }
}

if ( !Storage.prototype.getObject ) {
    Storage.prototype.getObject = function(key) {
        var value = this.getItem(key);
        return value && JSON.parse(value);
    }
}

//SAY HI ON OPEN
$('#'+ida+'chatinit').click(function(){     
	
	//$('#'+ida+'chatbox').show();
	localStorage.setItem('chat_is_closed','0');
	
    newStateCheck(function(currentState){
		
		if(currentState == 'agent') { //agent chat
			//console.log('agent chat 104');
			chatUpdateItems();
		} else { //auto chat bot
			//console.log('AUTO chat 107');
			
						aGuestMenuMessage();
					 	
			aChatUpdateItems(); 
		
		}

    });
	
	$.get('https://jasonsellsbellaterra.com/'+ida+'chat-up?time=1730208043', function(data) {
	});

}); 

//no chat yet prompt
hasdata = localStorage.getObject('chatData');
if (isObject(hasdata)) {
	if (hasdata.length < 1) {
		//console.log('has data empty');
	    			$('#chatcount').text("Let's Chat!");
	    					aGuestMenuMessage();
			} else {
	    		$('#chatcount').text("Let's Chat!");
			 
				//console.log('no dl has data');
				if(!hide_the_chat) {
					//$('#'+ida+'chatbox').show();
					localStorage.setItem('chat_is_closed','0');
					//console.log('175');
			    } else {
					//$('#'+ida+'chatbox').hide();
					localStorage.setItem('chat_is_closed','1');
					//console.log('178');
				}
				    		//console.log('has data full');
	}
} else {
	//console.log('has data no object');
	 
		//console.log('no dl first load');
		if(!hide_the_chat) {
			//$('#'+ida+'chatbox').show();
			localStorage.setItem('chat_is_closed','0');
			//console.log('190');
	    } else {
			//$('#'+ida+'chatbox').hide();
			localStorage.setItem('chat_is_closed','1');
			//console.log('193');
		}
	    	$('#chatcount').text("Let's Chat!");
    			aGuestMenuMessage();
	}

// queries
// current_url = window.location.href;

mls_listing_id = '';
type = '';
//closedchatmls = localStorage.getItem('closedchatmls');
showchat = true;

if(ida == '' && mls_listing_id != '') { 	// && (closedchatmls != mls_listing_id)

	listing_quest = "What do you think of this property? Click an option: <br><a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val(\'I Like It MLS#:"+mls_listing_id+"\');document.getElementById(\'chatsubmit\').click();\">I Like It</a><br><a href=\"#\" class=\"btn btn-round btn-warning btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val(\'Close but no\');document.getElementById(\'chatsubmit\').click();\">Close but no</a><br><a href=\"#\" class=\"btn btn-round btn-info btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val(\'Not what I want\');document.getElementById(\'chatsubmit\').click();\">Not what I want</a>";

	aChatMessage(listing_quest,$('input[name='+ida+'user_id]').val(),$('input[name='+ida+'agent_id]').val(),'Automatic Helper','agent');
	
	if(chat_closed != true) {
		//console.log('chat_closed false');
		var chat_closed = false;
		openChatOnScrollUp(true);
	} else {
		//console.log('chat_closed' + chat_closed);
		var chat_closed = true;
		openChatOnScrollUp(false);
	}

	
} 

current_url = window.location.href;
if (current_url.match(/(results\?)/gmi)) {
	//console.log('on results');
		  //console.log('guest results');	
	 aChatFilter('triggergaa');
	 if(!hide_the_chat) {
	    //$('#'+ida+'chatbox').show();
		 localStorage.setItem('chat_is_closed','0');
	    //console.log('265');
     } else {
		//$('#'+ida+'chatbox').hide();
		 localStorage.setItem('chat_is_closed','1');
		//console.log('268');
	  }
	 } 

if (current_url.match(/(\?logout)/gmi)) {
	//alert('cleared!');
	localStorage.clear();
}

if (current_url.match(/auth\/login/gmi)) {
	//console.log('on auth/login');
var x = window.matchMedia("(max-width: 700px)")
var toHide = hideChatIf(x)
	if(toHide) {
		//$('#'+ida+'chatbox').hide();
		localStorage.setItem('chat_is_closed','1');
	} else {
		if(!hide_the_chat) {
		    //$('#'+ida+'chatbox').show();
			localStorage.setItem('chat_is_closed','0');
		    //console.log('287');
	    } else {
			//$('#'+ida+'chatbox').hide();
			localStorage.setItem('chat_is_closed','1');
			//console.log('290');
		}
	}
}

//SAVE CHAT response DATA
$('#'+ida+'chatsubmit').click(function(){ 

	newStateCheck(function(currentState){
	 	if(currentState == 'agent') {
 				
			 var tm = $('input[name=message]').val();
	 
			  if(tm != '')  {
				  
				  $('#'+ida+'chatsubmit').text("Sending");
    
			  $.ajax({
			      url: 'https://jasonsellsbellaterra.com/newuchat',
			      type: "post",
			      data: {'message':$('input[name=message]').val(),
				  	'user_id':$('input[name=user_id]').val(), 
				  	'agent_id':$('input[name=agent_id]').val(), 
					'username':$('input[name=username]').val(), 
					'code':$('input[name=code]').val(), 
					'from':$('input[name=from]').val(), 
				  	'_token': $('input[name=_token]').val()},
			      success: function(data){
				//	  alert('chat clicked');
					chatUpdateItems(true);
					$('#postchat')[0].reset();
					$('.chat-input').val('');
					$(".chat-input").focus();
					
			        $('.conversation-list').scrollTo('100%', '100%', {
			            easing: 'swing'
			        });
					
			      }
			    });     

				}
		
	 	} else { //auto chat bot
			
		//$('#chatsubmit').text("Sending");
 		
		 var message = $('input[name='+ida+'message]').val();
		 var mrm = message.toLowerCase();

		 if (mrm == 'restart' || mrm == 'do over' || mrm == 'done' || mrm == 'stop' || mrm == 'end' || mrm == 'quit' || mrm == 'go to bed' || mrm == 'go to sleep' || mrm == 'get some rest' ) {
 
			 aChatUpdateItems();
			 aChatFilter(mrm);
 
		 } else {
	  		 aChatMessage($('input[name='+ida+'message]').val(),$('input[name='+ida+'user_id]').val(),$('input[name='+ida+'agent_id]').val(),$('input[name='+ida+'username]').val(),$('input[name='+ida+'from]').val());

	 var pendinganswer = localStorage.getItem('await');
	 var pendingvalid = localStorage.getItem('await_valid');
	 var qindex = localStorage.getItem('question_index');
	 

	 if (localStorage.getItem('series_name') != null  && pendinganswer != null && pendinganswer != '' && pendingvalid != null && pendingvalid != '') {
	 	localStorage.setItem(pendinganswer, $('input[name='+ida+'message]').val()); // set question name answer
	
		valid_arr = aChatValid($('input[name='+ida+'message]').val(),pendingvalid);

		if (typeof valid_arr[0].isvalid !== 'undefined' && valid_arr[0].isvalid) {
		
			var tttime = MysqlDateNow();
			//alert(tttime);
			//go to next 
			localStorage.setItem('await','');
			localStorage.setItem('await_valid','');
			localStorage.setItem('answer_time_'+qindex,tttime);
			aChatSeries(true,$('input[name='+ida+'message]').val());
			
		} else {
		 	//send validation error
			aChatMessage(valid_arr[0].message,$('input[name='+ida+'user_id]').val(),$('input[name='+ida+'agent_id]').val(),'Automatic Helper','agent');

		}
	 } 
	 
/*	if(pendinganswer == 'cht_max_price') {
		const changeSelected = (e) => {
		  const $select = document.querySelector('#sonmaxprice');
		  $select.value = localStorage.getItem('cht_min_price');
		};
	}*/

	 aChatUpdateItems();
	 aChatFilter(mrm);
 
	}

	 }
	 
		 $('#'+ida+'chatmessage').val('');//reset input message box
		 
		 $('#'+ida+'chatsubmit').text("Send");
		 
	});
	
 });  
 
 
   function aGuestMenuMessage() {
	 
  	var series_json = {
  		"series_name": "hello",
  		"series": false,
  		"questions": [
  			{
  				"name": "cht_hello",
  				  				"question": "Hi! Searching has never been so easy! Click an option:<br>&bull; <a href=\"#\" style=\"text-decoration:underline;\" onclick=\"$('#chatmessage').val('Find a Property');document.getElementById('chatsubmit').click();\">Find a Property</a><br>&bull; <a href=\"#\" style=\"text-decoration:underline;\" onclick=\"$('#chatmessage').val('Create a Property Alert');document.getElementById('chatsubmit').click();\">Create a Property Alert</a><br>&bull; <a href=\"#\" style=\"text-decoration:underline;\" onclick=\"$('#chatmessage').val('Create An Account');document.getElementById('chatsubmit').click();\">Create An Account</a><br>&bull; <a href=\"#\" style=\"text-decoration:underline;\" onclick=\"$('#chatmessage').val('Log Into My Account');document.getElementById('chatsubmit').click();\">Log Into My Account</a><br>&bull; <a href=\"#\" style=\"text-decoration:underline;\" onclick=\"$(\'#chatmessage\').val(\'Chat with Live Agent\');document.getElementById(\'chatsubmit\').click();\">Chat with Live Agent</a><br>&bull; <a href=\"#\" style=\"text-decoration:underline;\" onclick=\"$('#chatmessage').val('Contact An Agent');document.getElementById('chatsubmit').click();\">Call/Email An Agent</a>",
  				  				"validation": "none",
  				"answer": ""
  			}
  		]
  	};
		
  	localStorage.setObject('seriesData', series_json);	

  	aFlushSeries();					
  	aChatSeries(true,'');
	
 }
 function aChatFilter(mrm) { 
	
 	//console.log('Guest chat');
	
 	current_url = window.location.href;
	//console.log('Guest chat current_url:'+current_url);
	
	
 if (mrm == 'hi' || mrm == 'hi!' |mrm == 'hello' || mrm == 'hola' || mrm == 'anyone there?' || mrm == 'anybody there?' || mrm == 'help' || mrm == 'need help' || mrm == 'i need help' || mrm == 'help me' || mrm == 'start') {  
 	aGuestMenuMessage();

 } else if (mrm.match(/(talk to|speak to|live|real|actual)(.*?)(person|agent|broker|real estate agent|realtor|human)/gmi) || mrm == 'realtor' || mrm.match(/(interested|looking for|available|wondering|call me)/gmi)) {
  	 
  	var series_json = {
  		"series_name": "guest get live agent",
  		"series": true,
		"redirect": current_url,
  		"questions": [
 			{
 				"name": "cht_client_email",
 				"question": "&#128512; I can help with that! What's your email address?",
 				"validation": "email",
 				"answer": ""
 			},
 			{
 				"name": "cht_client_name",
 				"question": "What's your name?",
 				//"condition": "nouser",
 				"validation": "notempty",
 				"answer": ""
 			}
  		]
  	};
		
  	localStorage.setObject('seriesData', series_json);	
  	aFlushSeries();					
  	aChatSeries(true,'');
	 
} else if (mrm.match(/contact(.*?)(agent|realtor|company|broker)/gmi)) { //Contact My Agent 	
	 	var agent_name = 'Jason Pavich';
 	var agent_email = 'Jason@JasonPavich.com';
 	var agent_phone = '239-229-7483';
			
 	var series_json = {
 		"series_name": "find agent",
 		"series": false,
 		"questions": [
 			{
 				"name": "cht_reply",
 				 				"question": "OK, you want to contact an agent. &#128512;<br>Your Agent: "+agent_name+"<br>Call: <a href=\"tel:1"+agent_phone+"\">"+agent_phone+"</a><br>Email: <a href=\"mailto:"+agent_email+"\">"+agent_email+"</a><br><a href=\"/contact\">or Click here to contact an agent.</a>. Type HELP or <a href=\"#\" onclick=\"$(\'#chatmessage\').val(\'HELP\');document.getElementById(\'chatsubmit\').click();\" style=\"text-decoration:underline;\">Click HELP</a> to see these options again. &#128512;",
 				 				"validation": "",
 				"answer": ""
 			}
 		]
 	};
		
 	localStorage.setObject('seriesData', series_json);	
 	aFlushSeries();					
 	aChatSeries(true,'');
	
	
 } else if (mrm.match(/(list|advertise)(.*?)(property|listing|house)/gmi)) { 	
 	var series_json = {
 		"series_name": "list property",
 		"series": false,
 		"questions": [
 			{
 				"name": "cht_reply",
 				"question": "Click below to watch video and learn how to add your property to our site:<br><iframe width=\"100%\" src=\"https://www.youtube.com/embed/DVmwt6RHxrE\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen></iframe><a href=\"/list-your-property\"  style=\"text-decoration:underline;\">Then click here and enter the details of your property now.</a>. Type HELP or <a href=\"#\" onclick=\"$(\'#chatmessage\').val(\'HELP\');document.getElementById(\'chatsubmit\').click();\" style=\"text-decoration:underline;\">Click HELP</a> to see these options again. &#128512;",
 				"validation": "",
 				"answer": ""
 			}
 		]
 	};
		
 	localStorage.setObject('seriesData', series_json);	
 	aFlushSeries();					
 	aChatSeries(true,'');
	
 } else if (mrm.match(/(create|make|start)(.*?)account/gmi) || mrm == 'signup' || mrm == 'sign up' || mrm == 'log in' || mrm == 'login') { 	
 	var series_json = {
 		"series_name": "guest create account",
 		"series": true,
		"redirect": current_url,
 		"questions": [
 			{
 				"name": "cht_client_email",
 				"question": "&#128512; I can help with that! What's your email address?",
 				"validation": "email",
 				"answer": ""
 			},
 			{
 				"name": "cht_client_name",
 				"question": "What's your name?",
 				//"condition": "nouser",
 				"validation": "notempty",
 				"answer": ""
 			}
 		]
 	};
		
 	localStorage.setObject('seriesData', series_json);	
 	aFlushSeries();					
 	aChatSeries(true,'');
	
	
 } else if (mrm.match(/(set up|setup|create|email|add)(.*?)(alert|update|property alert|property update)/gmi)) { 	
 	var series_json = {
 		"series_name": "guest property alert",
 		"series": true,
		"redirect": current_url,
 		"questions": [
 			{
 				"name": "cht_ptype",
 				"question": "One of our most popular requests &#128512;! What type of property are you searching for? Click an option: <br><a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val(\'Single Family\');document.getElementById(\'chatsubmit\').click();\">Single Family</a><br><a href=\"#\" class=\"btn btn-round btn-primary btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val(\'Condominiums\');document.getElementById(\'chatsubmit\').click();\">Condominiums</a><br><a href=\"#\" class=\"btn btn-round btn-success btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val(\'Lots or Land\');document.getElementById(\'chatsubmit\').click();\">Lots or Land</a>",
 				"validation": "limit:single family|condominiums|lots or land",
 				"answer": ""
 			},
 			{
 				"name": "cht_min_price",
 				"question": "What's your minimum price? (type for example $1, 10K or 100,000) or choose then click Save Choice:<br><select id=\"sonminprice\" onchange=\"$(\'#chatmessage\').val(this.value);\"><option value=\"1000\">$1,000</option><option value=\"5000\">$5,000</option><option value=\"10000\">$10,000</option><option value=\"25000\">$25,000</option><option value=\"50000\">$50,000</option><option value=\"100000\">$100,000</option><option value=\"150000\">$150,000</option><option value=\"200000\">$200,000</option><option value=\"250000\">$250,000</option><option value=\"300000\">$300,000</option><option value=\"350000\">$350,000</option><option value=\"400000\">$400,000</option><option value=\"450000\">$450,000</option><option value=\"500000\">$500,000</option><option value=\"750000\">$750,000</option><option value=\"1000000\">$1,000,000</option><option value=\"2000000\">$2,000,000</option><option value=\"3000000\">$3,000,000</option><option value=\"4000000\">$4,000,000</option><option value=\"5000000\">$5,000,000</option><option value=\"10000000\">$10,000,000</option></select> <a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val($(\'#sonminprice\').val());document.getElementById(\'chatsubmit\').click();\">Save Choice</a>",
 				"validation": "numeric",
 				"answer": ""
 			},
 			{
 				"name": "cht_max_price",
 				"question": "What's your maximum price? (type for example: 250K or 250,000) or choose then click Save Choice:<br> <select id=\"sonmaxprice\" onchange=\"$(\'#chatmessage\').val(this.value);\"><option value=\"99999999999\">No Maximum</option><option value=\"100000000\">$100,000,000</option><option value=\"10000000\">$10,000,000</option><option value=\"5000000\">$5,000,000</option><option value=\"4000000\">$4,000,000</option><option value=\"3000000\">$3,000,000</option><option value=\"2000000\">$2,000,000</option><option value=\"1000000\">$1,000,000</option><option value=\"750000\">$750,000</option><option value=\"500000\">$500,000</option><option value=\"450000\">$450,000</option><option value=\"400000\">$400,000</option><option value=\"350000\">$350,000</option><option value=\"300000\">$300,000</option><option value=\"250000\">$250,000</option><option value=\"200000\">$200,000</option><option value=\"150000\">$150,000</option><option value=\"100000\">$100,000</option><option value=\"50000\">$50,000</option><option value=\"25000\">$25,000</option><option value=\"10000\">$10,000</option><option value=\"5000\">$5,000</option></select> <a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val($(\'#sonmaxprice\').val());document.getElementById(\'chatsubmit\').click();\">Save Choice</a>",
 				"validation": "numeric",
 				"answer": ""
 			},
 			{
 				"name": "cht_min_beds",
 				"question": "How many bedrooms minimum? (type for example: 3 or three) or choose then click Save Choice:<br><select id=\"sonbedrooms\" onchange=\"$(\'#chatmessage\').val(this.value);\"><option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"6\">6</option><option value=\"7\">7</option><option value=\"8\">8</option><option value=\"9\">9</option><option value=\"10\">10</option></select> <a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val($(\'#sonbedrooms\').val());document.getElementById(\'chatsubmit\').click();\">Save Choice</a>",
 				"validation": "numeric",
 				"condition": "cht_ptype:single family|condominiums",
 				"answer": ""
 			},
 			{
 				"name": "cht_min_baths",
 				"question": "How many bathrooms minimum? (type for example: 2 or two) or choose then click Save Choice:<br><select id=\"sonbathsfull\" onchange=\"$(\'#chatmessage\').val(this.value);\"><option value=\"0\">0</option><option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"6\">6</option><option value=\"7\">7</option><option value=\"8\">8</option><option value=\"9\">9</option><option value=\"10\">10</option></select> <a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val($(\'#sonbathsfull\').val());document.getElementById(\'chatsubmit\').click();\">Save Choice</a>",
 				"validation": "numeric",
 				"condition": "cht_ptype:single family|condominiums",
 				"answer": ""
 			}
 			 			,
 			{
 				"name": "cht_city",
 				"question": "Which city should it search? " + buildMultiSelectCity(),
 				"validation": "none",
 				"answer": ""
 			},
			{
				"name": "cht_community_name",
				"question": "Do you want to look in a specific community? <span id='bmsc'></span>",
				"validation": "none",
				"answer": ""
			},
			{
				"name": "cht_subdivision",
				"question": "Should I search a specific subdivision?  <span id='bmss'></span>",
				"validation": "none",
				"answer": ""
			},
 			 			{
 				"name": "cht_min_acres",
 				"question": "How many acres minimum? (type for example: 3 or three) or choose then click Save Choice:<br><select id=\"sonacres\" onchange=\"$(\'#chatmessage\').val(this.value);\"><option value=\"0\">Under 1 Acre</option><option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"6\">6</option><option value=\"7\">7</option><option value=\"8\">8</option><option value=\"9\">9</option><option value=\"10\">10</option><option value=\"15\">15</option><option value=\"20\">20</option><option value=\"30\">30</option><option value=\"40\">40</option><option value=\"50\">50</option><option value=\"100\">100</option></select> <a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val($(\'#sonacres\').val());document.getElementById(\'chatsubmit\').click();\">Save Choice</a>",
 				"validation": "numeric",
 				"condition": "cht_ptype:lots or land",
 				"answer": ""
 			},
 			{
 				"name": "cht_pool",
 				"question": "Only include properties with private pools? <a href=\"#\" class=\"btn btn-round btn-success btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val(\'Yes\');document.getElementById(\'chatsubmit\').click();\">Yes</a> <a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val(\'No\');document.getElementById(\'chatsubmit\').click();\">No</a>",
 				"condition": "cht_ptype:single family",
 				"validation": "yesno",
 				"answer": ""
 			},
 			{
 				"name": "cht_client_email",
 				"question": "What's your email address?",
 				"validation": "email",
 				"answer": ""
 			},
 			{
 				"name": "cht_client_name",
 				"question": "What's your name?",
 				//"condition": "nouser",
 				"validation": "notempty",
 				"answer": ""
 			}
 		]
 	};

 	localStorage.setObject('seriesData', series_json);	
 	aFlushSeries();					
 	aChatSeries(true,'');
	
 } else if (mrm == 'triggergaa') {  
 
var series_json = {
	"series_name": "results guest add alert",
	"series": true,
	"current_url": current_url,
	"questions": [
		{
			"name": "cht_client_email",
			"question": "Want to receive new alerts for properties matching this search?<br><br> &#128512; I can help with that! <br><br>What email address should we send your alerts to?",
			"validation": "email",
			"answer": ""
		},
		{
			"name": "cht_client_name",
			"question": "What's your name?",
			//"condition": "nouser",
			"validation": "notempty",
			"answer": ""
		}
	]
};
	
localStorage.setObject('seriesData', series_json);	
aFlushSeries();					
aChatSeries(true,'');
 
 
} else if (mrm.match(/find(.*?)(property|land|listing|house|home)/gmi)) { 	
 	var series_json = {
 		"series_name": "guest find property",
 		"series": true,
 		"questions": [
 			{
 				"name": "cht_ptype",
 				"question": "I can help find what you need &#128512;! What type of property are you searching for? Click an option: <br><a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val(\'Single Family\');document.getElementById(\'chatsubmit\').click();\">Single Family</a><br><a href=\"#\" class=\"btn btn-round btn-primary btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val(\'Condominiums\');document.getElementById(\'chatsubmit\').click();\">Condominiums</a><br><a href=\"#\" class=\"btn btn-round btn-success btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val(\'Lots or Land\');document.getElementById(\'chatsubmit\').click();\">Lots or Land</a>",
 				"validation": "limit:single family|condominiums|lots or land",
 				"answer": ""
 			},
 			{
 				"name": "cht_min_price",
 				"question": "What's your minimum price? (type for example $1, 10K or 100,000) or choose then click Save Choice:<br><select id=\"sonminprice\" onchange=\"$(\'#chatmessage\').val(this.value);\"><option value=\"1000\">$1,000</option><option value=\"5000\">$5,000</option><option value=\"10000\">$10,000</option><option value=\"25000\">$25,000</option><option value=\"50000\">$50,000</option><option value=\"100000\">$100,000</option><option value=\"150000\">$150,000</option><option value=\"200000\">$200,000</option><option value=\"250000\">$250,000</option><option value=\"300000\">$300,000</option><option value=\"350000\">$350,000</option><option value=\"400000\">$400,000</option><option value=\"450000\">$450,000</option><option value=\"500000\">$500,000</option><option value=\"750000\">$750,000</option><option value=\"1000000\">$1,000,000</option><option value=\"2000000\">$2,000,000</option><option value=\"3000000\">$3,000,000</option><option value=\"4000000\">$4,000,000</option><option value=\"5000000\">$5,000,000</option><option value=\"10000000\">$10,000,000</option></select> <a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val($(\'#sonminprice\').val());document.getElementById(\'chatsubmit\').click();\">Save Choice</a>",
 				"validation": "numeric",
 				"answer": ""
 			},
 			{
 				"name": "cht_max_price",				
 				"question": "What's your maximum price? (type for example: 250K or 250,000) or choose then click Save Choice:<br><select id=\"sonmaxprice\" onchange=\"$(\'#chatmessage\').val(this.value);\"><option value=\"99999999999\">No Maximum</option><option value=\"100000000\">$100,000,000</option><option value=\"10000000\">$10,000,000</option><option value=\"5000000\">$5,000,000</option><option value=\"4000000\">$4,000,000</option><option value=\"3000000\">$3,000,000</option><option value=\"2000000\">$2,000,000</option><option value=\"1000000\">$1,000,000</option><option value=\"750000\">$750,000</option><option value=\"500000\">$500,000</option><option value=\"450000\">$450,000</option><option value=\"400000\">$400,000</option><option value=\"350000\">$350,000</option><option value=\"300000\">$300,000</option><option value=\"250000\">$250,000</option><option value=\"200000\">$200,000</option><option value=\"150000\">$150,000</option><option value=\"100000\">$100,000</option><option value=\"50000\">$50,000</option><option value=\"25000\">$25,000</option><option value=\"10000\">$10,000</option><option value=\"5000\">$5,000</option></select> <a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val($(\'#sonmaxprice\').val());document.getElementById(\'chatsubmit\').click();\">Save Choice</a>",
 				"validation": "numeric",
 				"answer": ""
 			},
 			{
 				"name": "cht_min_beds",
 				"question": "How many bedrooms minimum? (type for example: 3 or three) or choose then click Save Choice:<br><select id=\"sonbedrooms\" onchange=\"$(\'#chatmessage\').val(this.value);\"><option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"6\">6</option><option value=\"7\">7</option><option value=\"8\">8</option><option value=\"9\">9</option><option value=\"10\">10</option></select> <a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val($(\'#sonbedrooms\').val());document.getElementById(\'chatsubmit\').click();\">Save Choice</a>",
 				"validation": "numeric",
 				"condition": "cht_ptype:single family|condominiums",
 				"answer": ""
 			},
 			{
 				"name": "cht_min_baths",
 				"question": "How many bathrooms minimum? (type for example: 2 or two) or choose then click Save Choice:<br><select id=\"sonbathsfull\" onchange=\"$(\'#chatmessage\').val(this.value);\"><option value=\"0\">0</option><option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"6\">6</option><option value=\"7\">7</option><option value=\"8\">8</option><option value=\"9\">9</option><option value=\"10\">10</option></select> <a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val($(\'#sonbathsfull\').val());document.getElementById(\'chatsubmit\').click();\">Save Choice</a>",
 				"validation": "numeric",
 				"condition": "cht_ptype:single family|condominiums",
 				"answer": ""
 			}
 			 			,
 			{
 				"name": "cht_city",
 				"question": "Which city should it search?" + buildMultiSelectCity(),
 				"validation": "none",
 				"answer": ""
 			},
 			{
 				"name": "cht_community_name",
 				"question": "Do you want to look in a specific community? <span id='bmsc'></span>",
 				"validation": "none",
 				"answer": ""
 			},
 			{
 				"name": "cht_subdivision",
 				"question": "Should I search a specific subdivision?  <span id='bmss'></span>",
 				"validation": "none",
 				"answer": ""
 			},
 			 			{
 				"name": "cht_min_acres",
 				"question": "How many acres minimum? (type for example: 3 or three) or choose then click Save Choice:<br><select id=\"sonacres\" onchange=\"$(\'#chatmessage\').val(this.value);\"><option value=\"0\">Under 1 Acre</option><option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"6\">6</option><option value=\"7\">7</option><option value=\"8\">8</option><option value=\"9\">9</option><option value=\"10\">10</option><option value=\"15\">15</option><option value=\"20\">20</option><option value=\"30\">30</option><option value=\"40\">40</option><option value=\"50\">50</option><option value=\"100\">100</option></select> <a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val($(\'#sonacres\').val());document.getElementById(\'chatsubmit\').click();\">Save Choice</a>",
 				"validation": "numeric",
 				"condition": "cht_ptype:lots or land",
 				"answer": ""
 			},
 			{
 				"name": "cht_pool",
 				"question": "Only include properties with private pools? <a href=\"#\" class=\"btn btn-round btn-success btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val(\'Yes\');document.getElementById(\'chatsubmit\').click();\">Yes</a> <a href=\"#\" class=\"btn btn-round btn-danger btn-xs\" style=\"padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px;height:unset;\" onclick=\"$(\'#chatmessage\').val(\'No\');document.getElementById(\'chatsubmit\').click();\">No</a>",
 				"condition": "cht_ptype:single family",
 				"validation": "yesno",
 				"answer": ""
 			}
 		]
 	};

 	localStorage.setObject('seriesData', series_json);	
 	aFlushSeries();					
 	aChatSeries(true,'');
	
 } else if (mrm.match(/(current)(.*?)(link|url)/gmi)) { 	
 	var series_json = {
 		"series_name": "current link",
 		"series": false,
 		"questions": [
 			{
 				"name": "cht_reply",
 				"question": "You are on: "+current_url+" Type HELP or <a href=\"#\" onclick=\"$(\'#chatmessage\').val(\'HELP\');document.getElementById(\'chatsubmit\').click();\" style=\"text-decoration:underline;\">Click HELP</a> to see these options again. &#128512;",
 				"validation": "",
 				"answer": ""
 			}
 		]
 	};
		
 	localStorage.setObject('seriesData', series_json);	
 	aFlushSeries();					
 	aChatSeries(true,'');
	
 } else if (mrm.match(/not what i want/gmi)) { 	
 	var series_json = {
 		"series_name": "guest not what i want",
 		"series": true,
		"redirect": current_url,
 		"passthrough": [
 			{
 				"mls_listing_id": "",
 				"type": ""
 			}
 		],
 		"questions": [
 			{
 				"name": "cht_what_you_think",
 				"question": "What needs to be different about this property?",
 				"validation": "notempty",
 				"answer": ""
 			},
 			{
 				"name": "cht_client_email",
 				"question": "&#128512 We have powerful free tools available to help you find exactly the property you need. <br><br>To get access to the tools and get started now, What's your email address?",
 				"validation": "email", 
 				"answer": ""
 			},
 			{
 				"name": "cht_client_name",
 				"question": "What's your name?",
 				"validation": "notempty",
 				"answer": ""
 			}
 		]
 	};
		
 	localStorage.setObject('seriesData', series_json);	
 	aFlushSeries();					
 	aChatSeries(true,'');
	
 } else if (mrm.match(/close but no/gmi)) { 	
 		
 	var series_json = {
 		"series_name": "guest close but no",
 		"series": true,
		"redirect": current_url,
 		"passthrough": [
 			{
 				"mls_listing_id": "",
 				"type": ""
 			}
 		],
 		"questions": [
 			{
 				"name": "cht_what_you_think",
 				"question": "What needs to be different about this property to make it perfect?",
 				"validation": "notempty",
 				"answer": ""
 			},
 			{
 				"name": "cht_client_email",
 				"question": "&#128512 We have powerful free tools available to help you find exactly the property you need. <br><br>To get access to the tools and get started now, What's your email address?",
 				"validation": "email", 
 				"answer": ""
 			},
 			{
 				"name": "cht_client_name",
 				"question": "What's your name?",
 				"validation": "notempty",
 				"answer": ""
 			}
 		]
 	};
		
 	localStorage.setObject('seriesData', series_json);	
 	aFlushSeries();					
 	aChatSeries(true,'');
	
 } else if (mrm.match(/i like it/gmi)) { 	
 		
 	var series_json = {
 		"series_name": "guest i like it",
 		"series": true,
		"redirect": current_url,
 		"passthrough": [
 			{
 				"mls_listing_id": "",
 				"type": ""
 			}
 		],
 		"questions": [
 			{
 				"name": "cht_client_email",
 				"question": "Glad you found something you like, let&rsquo;s add it to your favorites!<br><br>To get started, What's your email address?",
 				"validation": "email",
 				"answer": ""
 			},
 			{
 				"name": "cht_client_name",
 				"question": "What's your name?",
 				"validation": "notempty",
 				"answer": ""
 			}
 		]
 	};
		
 	localStorage.setObject('seriesData', series_json);	
 	aFlushSeries();					
 	aChatSeries(true,'');
	
 } else if (mrm == 'log into my account' ||mrm == 'sign into my account' ||  mrm == 'log in' || mrm == 'login' || mrm == 'signin' || mrm == 'sign in' || mrm == 'sing in' || mrm == 'singin') { 
 	var series_json = {
 		"series_name": "sign in",
 		"series": false,
 		"questions": [
 			{
 				"name": "cht_signin",
 				"question": "<a href=\"/auth/login\" style=\"text-decoration:underline;\">Click here to log in now.</a> Forgot your password? <a href=\"/password/email\" style=\"text-decoration:underline;\">Click here to reset your password.</a> Type HELP or <a href=\"#\" onclick=\"$(\'#chatmessage\').val(\'HELP\');document.getElementById(\'chatsubmit\').click();\" style=\"text-decoration:underline;\">Click HELP</a> to see options again.",
 				"validation": "none",
 				"answer": ""
 			}
 		]
 	};
		
 	localStorage.setObject('seriesData', series_json);	

 	aFlushSeries();					
 	aChatSeries(true,'');

 } else if (mrm.match(/Hi |Hello |can I |when will |when can |when I |where is |where does |where can |what is |what can |why is |why are |how are |how is |how do /gmi)) { 	
 	var series_json = {
 		"series_name": "guest get live agent catchall",
 		"series": true,
		"redirect": current_url,
 		"questions": [
 			{
 				"name": "cht_client_email",
 				"question": "I'm just a humble bot &#128512; But I can help with that! What's your email address?",
 				"validation": "email",
 				"answer": ""
 			},
 			{
 				"name": "cht_client_name",
 				"question": "What's your name?",
 				//"condition": "nouser",
 				"validation": "notempty",
 				"answer": ""
 			}
 		]
 	};
		
 	localStorage.setObject('seriesData', series_json);	
 	aFlushSeries();					
 	aChatSeries(true,'');
	
	
 }
 } //func

  
/*if(hide_the_chat) {
	$('#'+ida+'chatbox').hide();
	//console.log('1412');	
}*/
if(localStorage.getItem('chat_is_closed') === '1') {
	console.log('chat_is_closed');
	$('#'+ida+'chatbox').hide();
} else {
	console.log('chat_not_closed');
	//$('#'+ida+'chatbox').show();
}