//
//

var AccommodationEnquiryHandler = Class.create(EnquiryHandler,{
																		 
    Launch: function($super){
		var loc = window.location.toString();
		this.isFPA = (loc.indexOf('fpa')!=-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){
			this.AddElement({tag: 'div', wrapper: true, properties: {className:'enquiry_form_section'}},
						    {tag: 'label', properties: {innerHTML:'<strong>Multiple enquiry cart:</strong><br />'}},
						    {tag: 'input', properties: {type:'checkbox',disabled:true}},
							{tag: 'span', properties: {innerHTML:"&nbsp;Tick boxes below"}});
		}
		
		this.AddElement({tag: 'div', wrapper: true, properties: {className:'enquiry_form_section'}},
						{tag: 'label', properties: {innerHTML:'Email address:<br />'}},
						{tag: 'input', properties: {id:'email',type:'text',size:'25',maxlength:'50',value:'Enter email'}});
		
		
		this.AddElement({tag: 'div', wrapper: true, properties: {className:'enquiry_form_section'}},
						{tag: 'label', properties: {innerHTML:'Arrival date:<br />'}},
						{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()) }});

		
		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:<br />'}},
						{tag: 'span', properties: { innerHTML: DH.getNumSelect('pax',["1","2","3","4","5","6","7","8","9","10","11",
																					  "12","13","14","15","16","17","18","19","20","21+"]) }});
		

		this.AddElement({tag: 'div', wrapper: true, properties: {className:'enquiry_form_section'}},
						{tag: 'label', properties: {innerHTML:'<br />'}},
						{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'));
		for(var i=0;i<els.length;i++){
   			if(els[i].id.indexOf('include_')!=-1) els[i].observe('change',this.HandleInclude);
		}
	},
	
	HandleInclude: function(e){
		var el = e.target;
		if(el.checked) FormFactory.forms[0].AddToEnquiry(el.id.slice(8));
		else FormFactory.forms[0].RemoveFromEnquiry(el.id.slice(8));
	},
	
	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){
		var dates = FormFactory.forms[0].CheckAndGetDates();
		if(dates==null) return;
		$('submitButton').disabled = true;
	    var div = new Element('div',{}), s = div.style, html;
		s.position = "absolute";
		s.width = "400px";
		s.background = "#f6e1fe";
		s.color = "#42525f";
		s.fontSize = "12px";
		s.padding = "10px";
		s.textAlign = "left";
		s.border = "3px solid #42525f";
		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)-250+"px" : Event.pointerX(e)-470+"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>Name: </label><br /><input style='color:#42525f;' type='text' size='45' maxlength='40' id='enq_name' /><br />";
		html += "<label>Phone (with international code): </label><br /><input type='text' style='color:#42525f;' size='30' maxlength='25' id='enq_phone' value='+' />&nbsp;";
		html += "<input type='button' style='font-size:11px;' value='Send' onclick='FormFactory.forms[0].Submit();' />";
		html += "<input type='button' style='font-size:11px;' value='Cancel' onclick='FormFactory.forms[0].Cancel();' />";
		div.innerHTML = html;
		fadeIn($('getName'),3);
	},
	
	Cancel: function(){
		$('submitButton').disabled = false;
	    fadeOut($('getName'),3);
		setTimeout("$('getName').parentNode.removeChild($('getName'));",1500);
	},
	
	Submit: function(){
		var name = $('enq_name').value, phone = $('enq_phone').value, loc = window.location.toString();
		var falseNames = ['James Bond','Spiderman','Superman','Batman','Richard Smith','Gavin Fielding'];
		var found = false;
		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();
		if(dates==null) return;
		var adate = dates[0], ddate = dates[1];
		$('submitButton').disabled = true;
		
		var pax = $('pax').value;
		var email = $('email').value;
		var url = loc.split('accommodationchamonix.com');
		var url = url[0]+"accommodationchamonix.com/cadenquiry";
		var params = "arrival_date="+adate;
		params += "&departure_date="+ddate;
		params += "&pax="+pax;
		params += "&name="+encodeURIComponent(name);
		params += "&phone="+encodeURIComponent(phone);
		params += "&email="+email;
		//params += "&sending_url="+window.location;
		params += "&rental_units=";
				
		if(this.isFPA){ // it's an fpa ad, so only one property
		    loc = loc.split('fpa/');
		    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);
		}
		fadeOut($('getName'));
		setTimeout("$('getName').parentNode.removeChild($('getName'));",1500);
		new Ajax.Request(url,{method:'post',parameters:params,onSuccess:FormFactory.forms[0].HandleResponse});
	}
	
});
