// JavaScript Document

var newWindow;

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function openDetail(hotel_id) {
	MM_goToURL('parent','./?detail&h='+hotel_id);
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  newWindow = window.open(theURL,winName,features);
}

function checkDate() {
	today = new Date();
	inMonth = MM_findObj('inmonth');
	inDay = MM_findObj('inday');
	inYear = MM_findObj('inyear');
	outMonth = MM_findObj('outmonth');
	outDay = MM_findObj('outday');
	outYear = MM_findObj('outyear');
	inDate = new Date(inYear[inYear.selectedIndex].value, inMonth[inMonth.selectedIndex].value - 1, inDay[inDay.selectedIndex].value);
	outDate = new Date(outYear[outYear.selectedIndex].value, outMonth[outMonth.selectedIndex].value - 1, outDay[outDay.selectedIndex].value);
//	alert(inDate.getMonth()+"-"+inDate.getDate()+"-"+inDate.getFullYear());
	if (inDate < today) {
		inDate = today;
	}
	if (outDate <= inDate) {
		outDate = new Date(inDate.getTime() + 2*24*60*60*1000);
	}
	setIn(inDate);
	setOut(outDate);
}

function calClick(calName, theMonth, theDay, theYear) {
	calOff(calName);
	var newDate = new Date(theYear, theMonth, theDay);
	if (calName == "smallCalIn") {
		setIn(newDate);
	} else {
		setOut(newDate);
	}
	checkDate();
}

function setIn(inDate) {
	today = new Date();
	inMonth = MM_findObj('inmonth');
	inDay = MM_findObj('inday');
	inYear = MM_findObj('inyear');
	//alert(inDate.getMonth()+"-"+inDate.getDate()+"-"+inDate.getFullYear());
	inMonth.selectedIndex = inDate.getMonth();
	inDay.selectedIndex = inDate.getDate()-1;
	inYear.selectedIndex = inDate.getFullYear() - today.getFullYear();
}

function setOut(outDate) {
	today = new Date();
	outMonth = MM_findObj('outmonth');
	outDay = MM_findObj('outday');
	outYear = MM_findObj('outyear');
	// alert(outDate.getMonth()+"-"+outDate.getDate()+"-"+outDate.getFullYear());
	outMonth.selectedIndex = outDate.getMonth();
	outDay.selectedIndex = outDate.getDate()-1;
	outYear.selectedIndex = outDate.getFullYear() - today.getFullYear();
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function calOff(calName) {
	var smallCal = MM_findObj(calName);
	smallCal.style.visibility = "hidden";
}

function toggleSmallCal(calName) {
	var smallCal = MM_findObj(calName);
	if (smallCal.style.visibility == "visible") {
		smallCal.style.visibility = "hidden";
	} else {
		inMonth = MM_findObj('inmonth');
		inYear = MM_findObj('inyear');
		printCal(inMonth[inMonth.selectedIndex].value - 1, inYear[inYear.selectedIndex].value, calName);
		smallCal.style.visibility = "visible";
	}
}


function printCal(theMonth, theYear, calName) {
	theMonths = new Array();
	theMonths[0] = "January";
	theMonths[1] = "February";
	theMonths[2] = "March";
	theMonths[3] = "April";
	theMonths[4] = "May";
	theMonths[5] = "June";
	theMonths[6] = "July";
	theMonths[7] = "August";
	theMonths[8] = "September";
	theMonths[9] = "October";
	theMonths[10] = "November";
	theMonths[11] = "December";
	theFirst = new Date(theYear, theMonth, 1);
	dayCount = 1 - theFirst.getDay();
	tmp = new Date(theYear, theMonth + 1, 1);
	theLast = new Date(tmp.getTime() - 24*60*60*1000);
	dayMax = theLast.getDate();
	
	today = new Date();
	previousMonth = new Date(theYear, theMonth - 1, 1);
	nextMonth = new Date(theYear, theMonth + 1, 1);
	
	var calString = "";

	calString += "<table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
	calString += "	<tr>";
	calString += "		<th width=\"1%\">";
	if (theFirst > today) {  
		calString += "<a href=\"#\" onClick=\"printCal("+previousMonth.getMonth()+", "+previousMonth.getFullYear()+", '"+calName+"')\">&lt;</a>";
	} else {
		calString += "&nbsp;";
	}
	calString += "		</th>";
	calString += "		<th width=\"98%\">"+theMonths[theFirst.getMonth()]+" "+theFirst.getFullYear()+"</th>";
	calString += "		<th width=\"1%\">";
	if (nextMonth.getFullYear() - today.getFullYear() < 2) {
		calString += "		<a href=\"#\" onClick=\"printCal("+nextMonth.getMonth()+", "+nextMonth.getFullYear()+", '"+calName+"')\">&gt;</a>";
	} else {
		calString += "&nbsp;";
	}
	calString += "		</th>";
	calString += "	</tr>";
	calString += "</table>";
	
	calString += "<table width=\"100%\"  border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
	calString += "	<tr>";
	calString += "		<th width=\"15%\">S</th>";
	calString += "		<th width=\"14%\">M</th>";
	calString += "		<th width=\"14%\">T</th>";
	calString += "		<th width=\"14%\">W</th>";
	calString += "		<th width=\"14%\">T</th>";
	calString += "		<th width=\"14%\">F</th>";
	calString += "		<th width=\"15%\">S</th>";
	calString += "	</tr>";
	while (dayCount < dayMax) {
		calString += "	<tr>";
		for (var i = 0; i < 7; i ++) {
			
			if (dayCount < 1 || dayCount > dayMax) {
				calString += "		<td class=\"noday\">&nbsp;</td>";
			} else {
				if (theFirst.getMonth() == today.getMonth() && theFirst.getFullYear() == today.getFullYear()) {
					if (dayCount < today.getDate()) {
						calString += "		<td class=\"invalidday\">"+dayCount+"</td>";
					} else if (dayCount == today.getDate()) {
						calString += "		<td class=\"today\"><a href=\"#\" onClick=\"calClick('"+calName+"', "+theFirst.getMonth()+", "+dayCount+", "+theFirst.getFullYear()+")\">"+dayCount+"</a></td>";
					} else {
						calString += "		<td class=\"day\"><a href=\"#\" onClick=\"calClick('"+calName+"', "+theFirst.getMonth()+", "+dayCount+", "+theFirst.getFullYear()+")\">"+dayCount+"</a></td>";
					}
				} else {
					calString += "		<td class=\"day\"><a href=\"#\" onClick=\"calClick('"+calName+"', "+theFirst.getMonth()+", "+dayCount+", "+theFirst.getFullYear()+")\">"+dayCount+"</a></td>";
				}
			}
			dayCount ++;
		}
		calString += "	</tr>";
	}
	calString += "		<tr>\n<td class=\"footer\" colspan=\"7\"><a href=\"#\"  onClick=\"calOff('"+calName+"')\">Close</a></td></tr>";
	calString += "</table>";
	
	var smallCal = MM_findObj(calName);
	smallCal.innerHTML = calString;
}