// JavaScript Document
function menuHover(id){
	var d=document.getElementById(id);
	if(d){	
		d.style.cursor="pointer";
		d.style.backgroundImage="url(/images/mc_button_bkg_hover.png)";
		d.style.backgroundRepeat="no-repeat";
		d.style.fontSize="13px";
	}
}   
function menuOut(id){
	var d=document.getElementById(id);
	if(d){	
		d.style.cursor="default";
		d.style.backgroundImage="url(/images/mc_button_bkg.png)";
		d.style.backgroundRepeat="no-repeat";
		d.style.fontSize="12px";
	}
} 
function vmenuHover(id){
	var d=document.getElementById(id);
	if(d){	
		d.style.cursor="pointer";
		d.style.backgroundImage="url(/images/mc_vert_button_bkg_hover.png)";
		d.style.backgroundRepeat="no-repeat";
		d.style.fontSize="13px";
	}
}   
function vmenuOut(id){
	var d=document.getElementById(id);
	if(d){	
		d.style.cursor="default";
		d.style.backgroundImage="url(/images/mc_vert_button_bkg.png)";
		d.style.backgroundRepeat="no-repeat";
		d.style.fontSize="12px";
	}
}  

function imgBtnHover(id){
	var d=document.getElementById(id);
	if(d){	
		d.style.cursor="pointer";
		d.style.backgroundImage="url(/images/mc_inq_button_hover.png)";
		d.style.backgroundRepeat="no-repeat";
		d.style.fontWeight="bold";
	}
}   
function imgBtnOut(id){
	var d=document.getElementById(id);
	if(d){	
		d.style.cursor="default";
		d.style.backgroundImage="url(/images/mc_inq_button_normal.png)";
		d.style.backgroundRepeat="no-repeat";
		d.style.fontWeight="normal";
	}
}   

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);
	}
});
FormManager=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_img");
		this.departureCal=$("dep_img");
		this.arrivalInput=$("arrivaldate");
		this.departureInput=$("departuredate");
	},
	setMandatoryFields:function(){
		this.firstNameInput=$("firstname");
		this.lastNameInput=$("lastname");
		this.emailInput=$("email");
		this.phoneInput=$("phone");
		this.guestsInput=$("numberofguests");
	},
	setLabels:function(){
		this.fnLabel=$("first_name_label");
		this.lnLabel=$("last_name_label");
		this.emLabel=$("email_address_label");
		this.phLabel=$("telephone_number_label");
		this.ngLabel=$("sleeps_number_label");
		this.arrLabel=$("arrival_date_label");
		this.depLabel=$("departure_date_label");
		this.formTitle=$("contact_frame_upper");
	},
	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 phv=this.phoneInput.getValue();
		if(phv==""){missing_fields_array.push(this.phLabel);field_names_array.push("Phone Number");}else this.resetLabel(this.phLabel);
		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){
			
			//alert(missing_fields_array.length);
			
			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 any 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);
		}
		
		//alert(checkedOK);
		
		return checkedOk;
	},
	submitForm:function(e){
		//e.stop();
		if(!this.checkForm())e.stop();
	},
	hiliteLabel:function(c){
		c.setStyle({color:"#FF0000"});
	},
	resetLabel:function(l){
		l.setStyle({color:"#002244"});
	}
});

Event.observe(document,"dom:loaded",function(){									 
	var fm=new FormManager("contact");
	ip=new imagePreloader("/images/mc_button_bkg_normal.png","/images/mc_button_bkg_hover.png","/images/mc_vert_button_bkg.png","/images/mc_vert_button_bkg_hover.png","/images/mc_inq_button_normal.png","/images/mc_inq_button_hover.png");
});

