// JavaScript Document
SpamController=Class.create({
	initialize:function(spam){
		this.spammed=spam;
		if(this.spammed!=0)this.alertSpam();
	},
	alertSpam:function(){
		var spam_msg="Dear Visitor,";
		spam_msg+="\n\nSome fields in the submitted form contain URLs, HTTP and/or FTP links or characters used by spam robots.\n";
		spam_msg+="We can't insert the provided data into our database.\nPlease do not use \"line feed\" or ";
		spam_msg+="\"carriage return\" in single line fields or text containing \"http://\", \"ftp://\" or HTML markup tags.";
		spam_msg+="\n\nThank You.";
		alert(spam_msg);
	}
});
ICFormManager=Class.create({
	initialize:function(form){
		this.form=$(form);
		this.formSubmitHandler=this.submitForm.bindAsEventListener(this);
		this.form.observe("submit",this.formSubmitHandler);
		this.setCalendarElements();
		this.setMandatoryFields();
		this.setLabels();
		this.setHandlers();
		this.startObservers();
	},
	setCalendarElements:function(){		
		this.arrivalCal=$("arr_cal");
		this.departureCal=$("dep_cal");
		this.arrivalInput=$("arrivaldate");
		this.departureInput=$("departuredate");
	},
	setMandatoryFields:function(){
		this.firstNameInput=$("firstname");
		this.lastNameInput=$("lastname");
		this.emailInput=$("email");
		this.guestsInput=$("numberofguests");
		this.bedroomsInput=$("numberofbedrooms");
	},
	setLabels:function(){
		this.fnLabel=$("fname_label");
		this.lnLabel=$("lname_label");
		this.emLabel=$("email_label");
		this.ngLabel=$("guests_label");
		this.nbLabel=$("bedrooms_label");
		this.arrLabel=$("arrival_label");
		this.depLabel=$("departure_label");
		this.formTitle=$("instant_contact_title");
	},
	setHandlers:function(){
		this.arrivalCalHoverHandler=this.hoverArrival.bind(this);
		this.arrivalCalOutHandler=this.outArrival.bind(this);
		this.arrivalCalClickHandler=this.clickArrival.bind(this);
		this.departureCalHoverHandler=this.hoverDeparture.bind(this);
		this.departureCalOutHandler=this.outDeparture.bind(this);
		this.departureCalClickHandler=this.clickDeparture.bind(this);
		this.arrivalInputClickHandler=this.clickArrival.bind(this);
		this.departureInputClickHandler=this.clickDeparture.bind(this);
	},
	startObservers:function(){
		this.arrivalCal.observe("mouseover",this.arrivalCalHoverHandler);
		this.arrivalCal.observe("mouseout",this.arrivalCalOutHandler);
		this.arrivalCal.observe("click",this.arrivalCalClickHandler);
		this.departureCal.observe("mouseover",this.departureCalHoverHandler);
		this.departureCal.observe("mouseout",this.departureCalOutHandler);
		this.departureCal.observe("click",this.departureCalClickHandler);
		this.arrivalInput.observe("click",this.arrivalCalClickHandler);
		this.departureInput.observe("click",this.departureCalClickHandler);
	},
	hoverArrival:function(){
		this.arrivalCal.setStyle({border:"1px solid #006699"});
	},
	outArrival:function(){
		this.arrivalCal.setStyle({border:"0px"});
	},
	clickArrival:function(){
		new XDCalendar("arrivaldate",{arrival:true});
	},
	hoverDeparture:function(){
		this.departureCal.setStyle({border:"1px solid #006699"});
	},
	outDeparture:function(){
		this.departureCal.setStyle({border:"0px"});
	},
	clickDeparture:function(){
		new XDCalendar("departuredate",{});
	},
	_oldIE:function(){
		return(Prototype.Browser.IE&&parseFloat(navigator.appVersion.split(';')[1].strip().split(' ')[1])<=6)?true:false;
	},
	isEmailValid:function(){
		var filter=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		return(filter.test(this.emailInput.getValue()))?true:false;
	},
	controlDates:function(arr,dep){
		if(arr==""||dep=="")return 0;
		var dateFlag=1;
		this.dateStr="";
		var date1=new Date(arr);
		var date2=new Date(dep);
		if((date1.getTime())>(date2.getTime())){
			this.dateStr="Departure Date should not be anterior to arrival!";
			dateFlag=2;
		}else if((date1.getTime())==(date2.getTime())){
			thisdateStr="Departure and Arrival Dates are the same!";
			dateFlag=2;
		}
		if(dateFlag==2)alert(this.dateStr);
		return dateFlag;
	},
	checkForm:function(){
		var checkedOk=true;
		var emailEmpty=false;
		var fields="";
		var field_names_array=new Array();
		var missing_fields_array=new Array();
		var fnv=this.firstNameInput.getValue();
		if(fnv==""){missing_fields_array.push(this.fnLabel);field_names_array.push("First Name");}else this.resetLabel(this.fnLabel);
		var lnv=this.lastNameInput.getValue();
		if(lnv==""){missing_fields_array.push(this.lnLabel);field_names_array.push("Last Name");}else this.resetLabel(this.lnLabel);
		var emv=this.emailInput.getValue();
		if(emv==""){missing_fields_array.push(this.emLabel);field_names_array.push("Email Address");emailEmpty=true;}else this.resetLabel(this.emLabel);
		var nbv=this.bedroomsInput.getValue();
		if(nbv==""){missing_fields_array.push(this.nbLabel);field_names_array.push("Number of Bedrooms");}else this.resetLabel(this.nbLabel);
		var ngv=this.guestsInput.getValue();
		if(ngv==""){missing_fields_array.push(this.ngLabel);field_names_array.push("Number of Guests");}else this.resetLabel(this.ngLabel);
		var arv=this.arrivalInput.getValue();
		if(arv==""){missing_fields_array.push(this.arrLabel);field_names_array.push("Arrival Date");}else this.resetLabel(this.arrLabel);
		var dev=this.departureInput.getValue();
		if(dev==""){missing_fields_array.push(this.depLabel);field_names_array.push("Departure Date");}else this.resetLabel(this.depLabel);
		for(var i=0,l=missing_fields_array.length;i<l;i++){this.hiliteLabel(missing_fields_array[i]);}
		for(var i=0,l=field_names_array.length;i<l;i++){fields+=" - "+field_names_array[i]+"\n";}
		if(missing_fields_array.length>0){
			checkedOk=false;
			this.formTitle.update("<u><b><i>Thank You</i></b> for completing the <span style=\"color:red;\">missing fields.</span>...</u>");
			var alert_str="Please, we would invite you to complete the missing fields (highlighted in red):\n";
			alert_str+=fields+"to facilitate the handling of your enquiry.\n\nThank You.";
			alert(alert_str);
		}
		if(!emailEmpty){
			if(!this.isEmailValid()){
				checkedOk=false;
				this.hiliteLabel(this.emLabel);
				var alert_str="Your Email Address is not valid!\nPlease provide us with a valid email address so that ";
				alert_str+="we can get back to you.\nThank You.";alert(alert_str);
			}else this.resetLabel(this.emLabel);
		}
		var dateFlag=this.controlDates(arv,dev);
		if(dateFlag==2){
			checkedOk=false;
			this.hiliteLabel(this.arrLabel);
			this.hiliteLabel(this.depLabel);
		}else if(dateFlag==1){
			this.resetLabel(this.arrLabel);
			this.resetLabel(this.depLabel);
		}
		return checkedOk;
	},
	submitForm:function(e){
		if(!this.checkForm())e.stop();
	},
	hiliteLabel:function(c){
		c.setStyle({color:"#FF0000"});
	},
	resetLabel:function(l){
		l.setStyle({color:"#002244"});
	}
});
function flHoverHandler(e){
	var div,inLabel,outLabel;
	switch(e.element().identify()){
		case "min_studio":
			inLabel=document.getElementById("studios_fl_label");
			outLabel=document.getElementById("fl_studios_label");
			$("fl_studios_area").setStyle({left:"2px",top:"2px"});
			break;
		case "min_1br":
			inLabel=document.getElementById("onebr_fl_label");
			outLabel=document.getElementById("fl_1br_label");
			$("fl_1br_area").setStyle({left:"2px",top:"2px"});
			break;
		case "min_2br":
			inLabel=document.getElementById("twobr_fl_label");
			outLabel=document.getElementById("fl_2br_label");
			$("fl_2br_area").setStyle({left:"2px",top:"2px"});
			break;
		case "min_3br":
			inLabel=document.getElementById("threebr_fl_label");
			outLabel=document.getElementById("fl_3br_label");
			$("fl_3br_area").setStyle({left:"2px",top:"2px"});
			break;
		case "min_villa":
			inLabel=document.getElementById("villa_fl_label");
			outLabel=document.getElementById("fl_villa_label");
			$("fl_villa_area").setStyle({left:"2px",top:"2px"});
			break;
		case "min_yacht":
			inLabel=document.getElementById("yacht_fl_label");
			outLabel=document.getElementById("fl_yacht_label");
			$("fl_yacht_area").setStyle({left:"2px",top:"2px"});
			break;
	}
	inLabel.style.color="#0000FF";
	inLabel.style.fontWeight="bold";
	outLabel.style.color="#0000FF";
	outLabel.style.fontWeight="bold";
}
function flOutHandler(e){
	var el=e.element();
	var inLabel,outLabel;
	switch(el.identify()){
		case "min_studio":
			inLabel=document.getElementById("studios_fl_label");
			outLabel=document.getElementById("fl_studios_label");
			$("fl_studios_area").setStyle({left:"0px",top:"0px"});
			break;
		case "min_1br":
			inLabel=document.getElementById("onebr_fl_label");
			outLabel=document.getElementById("fl_1br_label");
			$("fl_1br_area").setStyle({left:"0px",top:"0px"});
			break;
		case "min_2br":
			inLabel=document.getElementById("twobr_fl_label");
			outLabel=document.getElementById("fl_2br_label");
			$("fl_2br_area").setStyle({left:"0px",top:"0px"});
			break;
		case "min_3br":
			inLabel=document.getElementById("threebr_fl_label");
			outLabel=document.getElementById("fl_3br_label");
			$("fl_3br_area").setStyle({left:"0px",top:"0px"});
			break;
		case "min_villa":
			inLabel=document.getElementById("villa_fl_label");
			outLabel=document.getElementById("fl_villa_label");
			$("fl_villa_area").setStyle({left:"0px",top:"0px"});
			break;
		case "min_yacht":
			inLabel=document.getElementById("yacht_fl_label");
			outLabel=document.getElementById("fl_yacht_label");
			$("fl_yacht_area").setStyle({left:"0px",top:"0px"});
			break;
	}
	inLabel.style.color="#002244";
	inLabel.style.fontWeight="normal";
	outLabel.style.color="#002244";
	outLabel.style.fontWeight="normal";
}

Event.observe(document,"dom:loaded",function(){									 
	ip=new imagePreloader("/images/mr_pri_button_hover.png","/images/mr_sec_button_hover.png","/images/mr_ter_button_hover.png","/images/mr_logo_hover.png");
	var fm=new ICFormManager("instant_contact");
	["min_studio","min_1br","min_2br","min_3br","min_villa","min_yacht"].each(function(fl){
		$(fl).observe("mouseover",flHoverHandler.bindAsEventListener());
		$(fl).observe("mouseout",flOutHandler.bindAsEventListener());
	});
});
