//
//

var AccommodationEnquiryHandler = Class.create(EnquiryHandler,{
																		 
    Launch: function($super){
		var loc = window.location.toString();
		this.isFPA = (loc.indexOf('fpa')!=-1 || loc.indexOf('property')!=-1);
		var run = false, els = $('Content').descendants();
		els.each(function(el){
			    if(el.tagName=='INPUT'&&el.id.indexOf('include')!=-1) run = true;
			});
		if(this.isFPA) run = true;
		if(run!=true){
		    this.container.parentNode.removeChild(this.container);
			return;
		}
	    $super();
		this.included = [];
	},
	    
	Render: function(){
		this.today = new Date();
		this.included = [];
		this.els = [];
		
			
		if(!this.isFPA){
			
			if(this.noPrompt!=true) this.ShowPrompt();
		
			this.AddElement({tag: 'div', wrapper: true, properties: {className:'enquiry_form_section'}},
						    {tag: 'label', properties: {innerHTML:'<strong>Enquiry:</strong>'}}/*,
						    {tag: 'input', properties: {type:'checkbox',checked:true,disabled:true}},
							{tag: 'span', properties: {innerHTML:"&nbsp;Tick boxes"}}*/);
		}
		
		this.AddElement({tag: 'div', wrapper: true, properties: {className:'enquiry_form_section'}},
						{tag: 'label', properties: {innerHTML:'Email:'}},
						{tag: 'input', properties: {id:'email',type:'text',size:'20',maxlength:'50',value:'Enter email'}});
		
		
		this.AddElement({tag: 'div', wrapper: true, properties: {className:'enquiry_form_section'}},
						{tag: 'label', properties: {innerHTML:'Arrival:'}},
						{tag: 'span', wrapper: true, properties: {id:"arr_select"}},
						{tag: 'span', properties: { innerHTML: DH.getDateSelect('arr',this.today.getDate(),(this.today.getMonth()+1),this.today.getYear()) }},
						{tag: 'label', properties: {innerHTML:'Flexible date?', style: "margin-left: 5px;"}},
						{tag: 'input', properties: {type:'checkbox',id:'flexible'}});

		
		this.AddElement({tag: 'div', wrapper: true, properties: {className:'enquiry_form_section'}},
						{tag: 'label', properties: {innerHTML:'Nights:'}},
						{tag: 'span', properties: { innerHTML: DH.getNumSelect('nights',['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15+']) }});
		
		
		/*this.AddElement({tag: 'div', wrapper: true, properties: {className:'enquiry_form_section'}},
						{tag: 'label', properties: {innerHTML:'Dates Flexible:<br />'}},
						{tag: 'input', properties: {type:'checkbox',id:'flexible'}});
		*/
		/*
		this.AddElement({tag: 'div', wrapper: true, properties: {className:'enquiry_form_section'}},
						{tag: 'label', properties: {innerHTML:'Departure date:<br />'}},
						{tag: 'span', wrapper: true, properties: {id:"dep_select"}},
						{tag: 'span', properties: { innerHTML: DH.getDateSelect('dep',this.today.getDate(),(this.today.getMonth()+1),this.today.getYear()) }});
		*/
		
		 this.AddElement({tag: 'div', wrapper: true, properties: {className:'enquiry_form_section'}},
						{tag: 'label', properties: {innerHTML:'People:'}},
						{tag: 'span', properties: { innerHTML: DH.getNumSelect('pax',["1","2","3","4","6","8+"]) }});
		 
		 
		

		this.AddElement({tag: 'div', wrapper: true, properties: {className:'enquiry_form_section'}},
						{tag: 'label', properties: {innerHTML:''}},
						{tag: 'input', properties: { type: 'button', id: 'submitButton', value: 'Submit' }});
		
		this.Write();
	},
	
	SetHandlers: function(){
		$('email').observe('focus',clearField);
		$('email').observe('blur',resetField);
	    $('arr_month').observe('change',DH.MonthChange);
		//$('dep_month').observe('change',DH.MonthChange);
		$('arr_year').observe('change',DH.YearChange);
		//$('dep_year').observe('change',DH.YearChange);
		$('submitButton').observe('click',this.GetName);
		var els = $A($('Content').getElementsByTagName('input'));
		this.els = els.findAll(function(element){ return element.id.indexOf('include_')!=-1; });
		for(var i=0;i<els.length;i++){
   			if(els[i].id.indexOf('include_')!=-1) {
				els[i].observe('click',this.HandleInclude);
				//els[i].parentNode.style.padding = '1px 3px 1px 3px';
				els[i].disabled = false;
				var parent = $(els[i].parentNode.parentNode.parentNode);
				parent.observe('mouseover',this.HandleMouseOver);
				parent.observe('mouseout',this.ResetMouseOver);
			}
		}
	},
	
	ResetMouseOver: function(){
	    
	},
	
	HandleMouseOver: function(e){
	    var el = e.target;
		el.cleanWhitespace();
		var els = el.descendants();
		var element = els.find(function(elem){ return elem.name });
		if(element&&!Effect.Queue.effects.length){
			new Effect.Highlight(element.parentNode,{duration:1.3,startcolor:'#ff0000'});
			element.$flashed = true;
		}
	},
	
	ShowPrompt: function(){
	  var div = new Element('div',{id:'enquiryPrompt'});
	  $('Content').insert(div);
	  fadeIn(div,3);
	  div.observe('click',this.HideEnquiryPrompt);
	  setTimeout("FormFactory.forms[0].HideEnquiryPrompt();",12000);
	},
	
	HideEnquiryPrompt: function(){
      if(FormFactory.forms[0].promptHidden == true) return;
	  FormFactory.forms[0].promptHidden = true;
	  var el = $('enquiryPrompt'), t = 250;
	  //slideUp(el,0,true,50);
	  //squishUp(el,0,false,30);
	  var x = (document.all)? -255 : -280;
	  var y = (document.all)? -103 : -128;
	  new Effect.Move(el,{duration:.5,x:x,y:y});
	  setTimeout("new Effect.Shrink($('enquiryPrompt'),{duration:1.2});",500);
	  setTimeout("$('"+FormFactory.forms[0].els[0].id+"').checked=true",2200);
	  setTimeout("$('"+FormFactory.forms[0].els[0].id+"').checked=false",2900);
	  // setTimeout("$('enquiryPrompt').parentNode.removeChild($('enquiryPrompt'));",2000);
	},
	
	HandleInclude: function(e){
		var el = e.target;
		var num = el.id.slice(8);
		var container = $('advert_'+el.name);
		if(el.checked) {
			FormFactory.forms[0].AddToEnquiry(num);
			var h = (document.all)? 30:23;
			//slideUp(container,h);
		} else {
			FormFactory.forms[0].RemoveFromEnquiry(num);
			//slideDown(container);
		}
	},
	
	AddToEnquiry: function(id){
		var exists = false;
	    this.included.each(function(el){
			    if(el==id) exists = true;
			});
		if(exists==false) this.included.push(id);
	},
	
	RemoveFromEnquiry: function(id){
	    var a = [];
		this.included.each(function(el){
			    if(el!=id) a.push(el);
			});
		this.included = a;
	},
	
	GetName: function(e){
		if(!$('masker')){
		 // $(document.body).insert('<div id="masker"></div>');
		}
		
		var dates = FormFactory.forms[0].CheckAndGetDates(false); // set arg to true to check departure date
		if(dates==null) return;
		$('submitButton').disabled = true;
	    var div = new Element('div',{}), s = div.style, html;
		s.position = "absolute";
		s.width = "500px";
		s.background = "#eee";
		s.color = "#000";
		s.fontSize = "12px";
		s.padding = "20px";
		s.textAlign = "left";
		s.border = "5px solid #222";
		s.top = (FormFactory.forms[0].isFPA)? Event.pointerY(e)-50+"px" : Event.pointerY(e)-20+"px";
		s.left = (FormFactory.forms[0].isFPA)? Event.pointerX(e)-350+"px" : Event.pointerX(e)-570+"px";
		FormFactory.forms[0].clickY = s.top;
		FormFactory.forms[0].clickX = s.left;
		div.id = 'getName';
		$('SiteWrapper').appendChild(div);
		html = "<strong>Please enter your name and phone number to complete your enquiry</strong><br />";
		html += "<label>First name: </label><br /><input style='color:#42525f;' type='text' size='45' maxlength='40' id='enq_first_name' /><br />";
		html += "<label>Last name: </label><br /><input style='color:#42525f;' type='text' size='45' maxlength='40' id='enq_last_name' /><br />";
		html += "<label>Phone (with international code): </label><br /><input type='text' style='color:#42525f;' size='30' maxlength='25' id='enq_phone' value='+' /><br />";
		html += "<label>Specific Requirements: </label><br /><textarea style='color:#42525f;' rows='6' cols='50' id='enq_requirements'/></textarea><br />";
		html += "<input type='button' style='font-size:11px;' value='Send' id='nameSubmit' onclick='FormFactory.forms[0].Submit();' />";
		html += "<input type='button' style='font-size:11px;' value='Cancel' onclick='FormFactory.forms[0].Cancel();' />";
		div.innerHTML = html;
		$('container').setOpacity(.6);
		new Effect.Appear($('getName'),{duration:.5});
		//fadeIn($('masker'),.2);
		setTimeout("$('enq_first_name').focus();",1000);
	},
	
	Cancel: function(){
		$('submitButton').disabled = false;
	    //fadeOut($('getName'),3);
		//setTimeout("$('getName').parentNode.removeChild($('getName'));",1500);
		this.doNotConfirm = true;
		$('enq_phone').value = "+ none given";
		$('enq_first_name').value = "none ";
		$('enq_last_name').value = "given";
		this.Submit();
	},
	
	Submit: function(){
		var name = $('enq_first_name').value + " " + $('enq_last_name').value, phone = $('enq_phone').value, loc = window.location.toString();
		var first_name = $('enq_first_name').value;
		var last_name = $('enq_last_name').value;
		var falseNames = ['James Bond','Spiderman','Superman','Batman','Richard Smith'];
		var found = false;
		var requirements = $('enq_requirements').value;
		var nights = $('nights').value;
		var flexible = $('flexible').checked? 'on' : 'off';
		falseNames.each(function(el){
		        if(name.indexOf(el)!=-1){
		            alert("Hmmm. I don't think you're really "+name+". Please try again.");	
			        found = true;
		        }	
			});
		if(found==true) return;
		if(name.length<3||phone.length<8||(phone.indexOf('00')==-1&&phone.indexOf('+')==-1)){
		    alert( "There was a problem with your input. Please check you have included the\ninternational dialing code (starting with 00 or +) with your phone number" );	
			return;
		}
		var dates = FormFactory.forms[0].CheckAndGetDates(false); // set arg to true to check departure date
		if(dates==null) return;
		var adate = dates[0], ddate = dates[1];
		$('submitButton').disabled = true;
		$('nameSubmit').disabled = true;
		$('getName').innerHTML = 'sending...';
		var pax = $('pax').value;
		var email = $('email').value;
		var url = loc.split('chamonix.net');
		var url = url[0]+"chamonix.net/cadenquiry";
		var params = "arrival_date="+adate;
		//params += "&departure_date="+ddate; // uncomment to put departure date back in
		params += "&pax="+pax;
		params += "&name="+encodeURIComponent(name);
		params += "&first_name="+encodeURIComponent(first_name);
		params += "&last_name="+encodeURIComponent(last_name);
		params += "&phone="+encodeURIComponent(phone);
		params += "&requirements="+encodeURIComponent(requirements);
		params += "&nights="+nights;
		params += "&flexible="+flexible;
		params += "&email="+email;
		params += "&sending_url="+window.location;
		params += "&rental_units=";
				
		if(this.isFPA){ // it's an fpa ad, so only one property and get it from the url
		    loc = loc.split('property/');
		    params += loc[1];
		} else { // cad ads
		    for(var i=0;i<this.included.length;i++){
		        params += this.included[i]+",";
		    }
			params = params.slice(0,params.length-1);
		}
		setTimeout("new Effect.Fade($('getName'),{duration:.5});",500);
		$('container').setOpacity(1);
		//setTimeout("fadeOut($('masker'),1);",1000);
		setTimeout("$('getName').remove();",1500);
		new Ajax.Request(url,{method:'post',parameters:params,onSuccess:FormFactory.forms[0].HandleResponse});
	}
	
});
