function buildCal(m, y, cM, cH, cDW, cD, brdr, close, divId, editId, iframeId){
var mn=['January','February','March','April','May','June','July','August','September','October','November','December'];
var dim=[31,0,31,30,31,30,31,31,30,31,30,31];

var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st

var todaydate=new Date() //DD added
var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added

dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
var t='<table class="'+cM+'" cols="7" cellpadding="0" border="'+brdr+'" cellspacing="0"><tr align="center">';
t+='<td colspan="1" align="left" class="'+cH+'"><a href="javascript:prevMonth(\''+m+'\',\''+y+'\',\''+divId+'\',\''+editId+'\',\''+iframeId+'\');">&#60;&#60;&nbsp;</a></td><td colspan="5" align="center" class="'+cH+'">'+mn[m-1]+' - '+y+'</td><td colspan="1" align="right" class="'+cH+'"><a href="javascript:nextMonth(\''+m+'\',\''+y+'\',\''+divId+'\',\''+editId+'\',\''+iframeId+'\');">&#62;&#62;&nbsp;</a></td></tr><tr align="center">';

for(s=0;s<7;s++)t+='<td class="'+cDW+'">'+"SMTWTFS".substr(s,1)+'</td>';
t+='</tr><tr align="center">';
for(i=1;i<=42;i++){
var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&nbsp;';
var href='<a href="javascript:selectDate(\''+m+'\',\''+x+'\',\''+y+'\',\''+divId+'\',\''+editId+'\');" onclick="selectDate(\''+m+'\',\''+x+'\',\''+y+'\',\''+divId+'\',\''+editId+'\',\''+iframeId+'\');">';


if (x>0) {
	var currDate = new Date();
	currDate.setFullYear(y);
	currDate.setMonth(m-1);
	currDate.setDate(x);
	
	if (x==scanfortoday) //DD added
		x='<span id="today">'+href+x+'</span>'; //DD added
	else if(currDate>todaydate) {
		x=href+x;
	}
	
	t+='<td class="'+cD+'">'+x+'</a></td>';
}
else
	t+='<td class="'+cD+'">'+x+'</a></td>';
if(((i)%7==0)&&(i<36))t+='</tr><tr align="center">';
}
return t+='</tr><tr><td colspan="7" align="center" class="'+close+'"><a href="javascript:hideCal(\''+divId+'\',\''+iframeId+'\');">Close</a></td></tr></table>';
}

function popCal(divId, editId, iframeId) {
	var m = new Date().getMonth() + 1;
	var y = new Date().getFullYear();
			
	if(document.getElementById(editId)) {
		var date = parseDate(document.getElementById(editId).value);
		//alert(date);
		if(date) {
			m = date.getMonth()+1;
			y = date.getFullYear();
		}
	}
	drawCal(m, y, divId, editId, iframeId);
	popIframe(iframeId);
}

function popIframe(iframeId) {
	var iframe = document.getElementById(iframeId);
	iframe.style.display = 'inline';
	iframe.style.position = 'absolute';
	iframe.style.zIndex=99;
	//iframe.style.
}

function hideIframe(iframeId) {
	if(document.getElementById(iframeId)) {
		var iframe = document.getElementById(iframeId);
		iframe.style.display = 'none';
	}
}

function drawCal(m, y, divId, editId, iframeId) {
	if(document.getElementById(divId)) {		
		var div = document.getElementById(divId);
		div.style.display='inline';
		div.style.position='absolute';
		div.style.zIndex=100;
		div.innerHTML=buildCal(m, y, "main", "month", "daysofweek", "days", 0, "close", divId, editId, iframeId);
	}
}

function hideCal(divId, iframeId) {
	if(document.getElementById(divId)) {
		var div = document.getElementById(divId);
		div.style.display='none';
		div.innerHTML='';
		
		hideIframe(iframeId);
	}

}

function selectDate(m, d, y, divId, editId, iframeId) {
	document.getElementById(editId).value=m+'/'+d+'/'+y;
	hideCal(divId, iframeId);
}

function prevMonth(m, y, divId, editId, iframeId) {
	if(m==1) {
		m=12;
		y--;
	}
	else {
		m--;
	}
	drawCal(m, y, divId, editId, iframeId);
}

function nextMonth(m, y, divId, editId, iframeId) {
	if(m==12) {
		m=1;
		y++;
	}
	else {
		m++;
	}
	drawCal(m, y, divId, editId, iframeId);
}

function validateDate(date) {
	var dateArray = date.split('/');
}

function parseDate(date) {
	if(Date.parse(date)) {
		var parsedDate = new Date();
		parsedDate.setTime(Date.parse(date));
		return parsedDate;
	}
	else
		return null;
}

var FULL_NAME_REQUIRED = "Full Name is required.";
var EMAIL_REQUIRED = "Email is required.";
var EMAIL_INVALID_FORMAT = "Email has an invalid format.";
var EMAIL_CONFIRMATION_REQUIRED = "Email Confirmation is required.";
var EMAIL_CONFIRMATION_INVALID_FORMAT = "Email Confirmation has an invalid format.";
var EMAIL_CONFIRMATION_NOT_MATCH = "Email and Email Confirm do not match.";
var CONTACT_NUMBER_REQUIRED = "Contact Number is required.";
var CHECK_IN_INVALID = "Invalid check-in date.";
var CHECK_OUT_INVALID = "Invalid check-out date.";
var CHECK_OUT_NOT_GREATER = "Check-out date must be later than the check-in date.";
var ROOM_TYPE_NOT_SELECTED = "Please select a room type you would like to reserve.";

function validateReservation(formId) {
	var error = '';
	
	//Create regex objects
	var reEmail = new RegExp('^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$');
	
	var fullName = document.getElementById('fullName').value;
    var roomType = document.getElementById('roomtype').value;

    if(fullName == null)
		error += FULL_NAME_REQUIRED + '\n';
	else if(fullName != null && fullName == '')
		error += FULL_NAME_REQUIRED + '\n';
		
	var email = document.getElementById('email').value;
	var emailConfirm = document.getElementById('emailConfirm').value;
	
	if(email == null)
		error += EMAIL_REQUIRED + '\n';
	else if(email != null && email == '')
		error += EMAIL_REQUIRED + '\n';
	else if(!reEmail.test(email))
		error += EMAIL_INVALID_FORMAT + '\n';
	
	if(emailConfirm == null)
		error += EMAIL_CONFIRMATION_REQUIRED + '\n';
	else if(emailConfirm != null && emailConfirm == '')
		error += EMAIL_CONFIRMATION_REQUIRED + '\n';
	else if(!reEmail.test(emailConfirm))
		error += EMAIL_CONFIRMATION_INVALID_FORMAT + '\n';
		
	if(email!=emailConfirm)
		error += EMAIL_CONFIRMATION_NOT_MATCH + '\n';
	
	var contactNumber = document.getElementById('contactNumber').value;
	
	if(contactNumber == null)
		error += CONTACT_NUMBER_REQUIRED + '\n';
	else if(contactNumber != null && contactNumber == '')
		error += CONTACT_NUMBER_REQUIRED + '\n';
	
	var checkIn = parseDate(document.getElementById('checkIn').value);
	var checkOut = parseDate(document.getElementById('checkOut').value);
	
	if(!checkIn)
		error += CHECK_IN_INVALID + '\n';
	if(!checkOut)
		error += CHECK_OUT_INVALID + '\n';
	if(checkIn>=checkOut)
		error += CHECK_OUT_NOT_GREATER + '\n';

    if(roomType == "NOT")
		error += ROOM_TYPE_NOT_SELECTED + '\n';

    if(error!='')
		alert(error);
	else
		if(document.getElementById(formId))
			document.getElementById(formId).submit();
}
