var selectedcheckin = new Date();
var checkinoffset = 1;				
var yearoffset = 1;
var now = new Date();
var calinselect;

var invalids="/:,;!~$";			//lists the characters that shouldn't be included 

function isValid(string){
	for(var i=0; i<string.length; i++){
	  if(invalids.indexOf(string.charAt(i)) != -1)
		return false;
	}
	return true;
}
function validateMailFm(){

	var errmsg = "";
	
	// Check Name
	var tmpname = document.mailform.fname.value;
	if(tmpname=="") errmsg += "Please provide your name\n";
	if(!isValid(tmpname)) errmsg += "\"" + invalids + "\" characters are invalid for name";
	// Check email
	var tmpemail=document.mailform.femail.value;
	if (tmpemail=="") errmsg += "Please provide your email address\n";
	if(!isValid(tmpemail)) errmsg += "The email you provided is not in valid format\n";
	if(tmpemail.indexOf("@") == -1) errmsg += "The valid email format is name@domain.ext \n";
	if(tmpemail.indexOf(".") == -1) ;
	// Check Subject
	var tmpsub = document.mailform.fsubject.value;
	if(tmpsub=="") errmsg += "Please provide the subject of your inquiry\n";
	if(!isValid(tmpsub)) errmsg += "\"" + invalids + "\" characters are invalid in subject field";
	// Check message
	var tmpmsg = document.mailform.fmessage.value;
	if(tmpmsg=="") errmsg += "Please provide the message of your inquiry\n";
	//if(!isValid(tmpmsg)) errmsg += "Please remove \"" + invalids + "\" characters";
	
	// Display error message if any	
	if(errmsg!=""){
		alert(errmsg);
		return false;
	}else{
		document.mailform.submit();
	}
}

function validatebookhotel(){

	var errmsg = "";
	
	// Check Name
	var tmpname = document.hotelbook.fname.value;
	if(tmpname=="") errmsg += "Please provide your name\n";
	if(!isValid(tmpname)) errmsg += "\"" + invalids + "\" characters are invalid for name";
	// Check email
	var tmpemail=document.hotelbook.femail.value;
	if (tmpemail=="") errmsg += "Please provide your email address\n";
	if(!isValid(tmpemail)) errmsg += "The email you provided is not in valid format\n";
	if(tmpemail.indexOf("@") == -1) errmsg += "The valid email format is name@domain.ext \n";
	if(tmpemail.indexOf(".") == -1) ;
	// Check dates
	var tmpsub = document.hotelbook.checkindate.value;
	if(tmpsub=="") errmsg += "Please provide the check-in date\n";
	if(!isValid(tmpsub)) errmsg += "\"" + invalids + "\" characters are invalid in check-in date field";
	// Check dates
	var tmpsub = document.hotelbook.checkoutdate.value;
	if(tmpsub=="") errmsg += "Please provide the check-out date\n";
	if(!isValid(tmpsub)) errmsg += "\"" + invalids + "\" characters are invalid in check-out date field";
	// Check no of people
	var tmpsub = document.hotelbook.ppl.value;
	if(tmpsub=="") errmsg += "Please provide the number of people\n";
	if(!isValid(tmpsub)) errmsg += "\"" + invalids + "\" characters are invalid in number of people field";
	// Check message
	var tmpmsg = document.hotelbook.fmessage.value;
	if(tmpmsg=="") errmsg += "Please provide the message of your inquiry\n";
	//if(!isValid(tmpmsg)) errmsg += "Please remove \"" + invalids + "\" characters";
	
	// Display error message if any	
	if(errmsg!=""){
		alert(errmsg);
		return false;
	}else{
		document.mailform.submit();
	}
}


function validateguestbook()
{
	var errmsg = "";
	
	// Check Name
	var tmpname = document.guestbook.gbname.value;
	if(tmpname=="") errmsg += "Please provide your name\n";
	var tmpemail = document.guestbook.gbemail.value;
	if(tmpemail=="") document.guestbook.gbemail.value = " ";
	var tmpwebsite = document.guestbook.gbwebsite.value;
	if(tmpwebsite=="") document.guestbook.gbwebsite.value = " ";
	var tmpmsg = document.guestbook.gbmsg.value;
	if(tmpmsg=="")  errmsg += "Please write some message\n";

	// Display error message if any	
	if(errmsg!=""){
		alert(errmsg);
		return false;
	}else{
		document.guestbook.submit();
	}

}

var win;
function popup()
{
	var feature = "toolbar=0,status=0,directories=0,scrollbars=1,resizable=1,menubar=0,left=0,top=0";
	if(win && !win.closed) win.close();
	win=window.open("bcondition.html","con",feature+",width=500,height=400");
	win.focus();
}


// check if user has provided checkin date
function checkin(){
	if(document.hotelbook.checkindate.value == ""){
		calout.disabledDatesExpression=""; 		// clear the disable dates of checkout cal. i.e. all dates are enabled
		calout.addDisabledDates(null,formatDate(now,"MMM dd, yyyy"));
		calout.addDisabledDates(formatDate(now,"MMM dd, yyyy"),null);
	}		
}

// edit or change this function to specify behavior of the checkin date calendar
function checkinclick(calitem){
	calitem.disabledDatesExpression=""; 		// clear the disable dates of checkout cal. i.e. all dates are enabled
 	calinselect=true; 
	if(document.getElementsByName("hotelbook").length > 0) {
		document.hotelbook.checkoutdate.value="";
	}
	return false;
}


// edit or change this function to specify behavior of the checkout date calendar
function checkoutclick(){
	calinselect=false;							// clear the flag that checkin cal is selected
}

// this function is included in the CalendarPopup.js to set the checkout date according
// to the checkin date selected
function setcheckoutdate(checkoutcal,day,nightobj){
	if(calinselect==true){						// check if checkin cal is selected
		// disable dates in checkout calendar according to the checkin date selected
		checkoutcal.addDisabledDates(null,formatDate(day,"MMM dd, yyyy"));	
		selectedcheckin = day;
	}else{
		// if checkout cal is selected, calculate the number of nights and display it
		var nights = day - selectedcheckin;
		if(nightobj){
			nightobj.value = Math.round(nights/86400000);
		}
	}
}

// This function is called when number of nights text field is changed
// the checkout date will be changed according to the number of nights
function updatecheckoutdate(coobj, nobj){
	// Check if number of nights entered is number and greater than 0
	// reset the number to 1 if it isn't
	if((nobj.value<1)|| (isNaN(nobj.value))){
		nobj.value=1;
	}
	nobj.value = Math.round(nobj.value);
	var a = new Date();
	a.setTime(selectedcheckin.getTime()+60000*60*24*nobj.value);
	coobj.value=formatDate(a,"MMM dd, yyyy");
}


