var v_mths=['January','February','March','April','May','June','July','August','September','October','November','December'];
var v_mths_abbr=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var v_dim = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var v_dys=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
var v_date=new Date();
var v_ibox;
var v_table;
var v_stoyr;
var v_stomth;
var v_stoday;
var v_stomthname;

document.write('<table id="cal_table1" class="calpopup" style="text-align: center;">');
document.write('<tr>');
document.write('<td style="cursor: pointer; color: #4b6ca2; font-size: 11px; font-weight: bold; font-family: tahoma;" onclick="pyr();"><<</td>');
document.write('<td colspan="5" id="cyr1" class="nicetext" >&nbsp;</td>');
document.write('<td style="cursor: pointer; color: #4b6ca2; font-size: 11px; font-weight: bold; font-family: tahoma;" onclick="nyr();">>></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td style="cursor: pointer; color: #4b6ca2; font-size: 11px; font-weight: bold; font-family: tahoma;" onclick="pmth();"><</td>');
document.write('<td id="cmth1" class="nicetext" colspan="5"></td>');
document.write('<td style="cursor: pointer; color: #4b6ca2; font-size: 11px; font-weight: bold; font-family: tahoma;" onclick="nmth();">></td>');
document.write('</tr>');
document.write('<tr class="caldays">');
document.write('<td>Su</td>');
document.write('<td>Mo</td>');
document.write('<td>Tu</td>');
document.write('<td>We</td>');
document.write('<td>Th</td>');
document.write('<td>Fr</td>');
document.write('<td>Sa</td>');
document.write('</tr>');

	for (i=0; i<42; i++) {
		if (i%7==0) {
			document.write('<tr class="caltds nicetext">');
		}
		document.write('<td id="d'+i+'" onclick="setday(this.id); document.getElementById(\'sdate\').style.display=\'\';" onmouseover="change_bg(this.id);" onmouseout="clear_bg(this.id)" >&nbsp;</td>');
		if ((i+1)%7==0) {
			document.write('</tr>');
		}
	}

document.write('<tr>');
document.write('<td colspan="7" style="background-color: #4b6ca2; text-align: right;"><img src="images/close.gif" style="cursor: pointer;" onclick="hide_cal(\'cal_table1\');"></td>');
document.write('</tr>');
document.write('</table>');

function show_cal(selbox,boss,the_table,adj){

	var v_left=0;
	var v_top=0;

	var v_iframe=document.getElementById('srchiframe');
	v_iframe.contentWindow.document.getElementById('sdate').style.display='none';
	v_iframe.contentWindow.document.getElementById('nights').style.display='none';
	v_iframe.contentWindow.document.getElementById('days').style.display='none';

	if (v_iframe.offsetParent) {
		do {
//			alert(boss.id + ":" + boss.offsetLeft);
			v_left+=v_iframe.offsetLeft;
			v_top+=v_iframe.offsetTop;
		} while (v_iframe=v_iframe.offsetParent);
	} else {
			if (v_iframe.y) {
				v_top+=v_iframe.y;
			}
		}
	v_ibox=selbox;
	v_table=the_table;
// Calculate the xy coordinates of the calendar container element 
//	alert(document.body.innerHTML);
	if (boss.offsetParent) {
		do {
			v_left+=boss.offsetLeft;
			v_top+=boss.offsetTop;
		} while (boss=boss.offsetParent);
	} else {
			if (boss.y) {
				v_top+=boss.y;
			}
		}
	var caltab=document.getElementById(the_table);
	if (caltab) {
		caltab.style.left=parseInt(v_left+0) + 'px';
		caltab.style.top=parseInt(v_top-2) + 'px';
		caltab.style.display='block';
	}

	if (!(v_ibox.options[v_ibox.selectedIndex].text=="- CLICK TO SELECT -")) {
		var v_dtarr=(v_ibox.options[v_ibox.selectedIndex].text).split("-");
		v_stoday=v_dtarr[0];
		for (i=0;i<12;i++) {
			if (v_dtarr[1]==v_mths_abbr[i]) {
				v_stomth=i;
				v_stomthname=v_mths[i];
				i=12;
			}
		}
		v_stoyr=v_dtarr[2];
//		alert(v_stoyr + ":" + v_stomth + ":" + v_stoday);
		v_date=new Date(v_stoyr,v_stomth,v_stoday);
	}

	pop_cal();
//	v_ibox.style.display='none';
//	v_ibox.style.display='';
}

function pop_cal(){
// Set the calendar month to that of the container element, where it exists 
/*
//	if (boss.value) {
//		var v_darr=(boss.value).split("-");
*/

// Populate the year td
	var the_td=document.getElementById("cyr1");
	the_td.innerHTML=v_date.getFullYear();
// Populate the month td
	var the_td=document.getElementById("cmth1");
	the_td.innerHTML=v_mths[v_date.getMonth()];
	
// get first day of month
  var v_day1 = new Date(v_date.getFullYear(), v_date.getMonth(), 1);
  var v_mthstart = v_day1.getDay();
  
// find number of days in month
  var v_mthlen = v_dim[v_date.getMonth()];
  
// compensate for leap year
  if (v_date.getMonth() == 1) {
    if((v_date.getFullYear() % 4 == 0 && v_date.getFullYear() % 100 != 0) || v_date.getFullYear() % 400 == 0){
      v_mthlen = 29;
    }
  }

// clear down the table cells  
  for (i=0; i<42; i++) {
		var the_td=document.getElementById("d" + i);
		the_td.innerHTML="&nbsp;";
		the_td.style.background="#ccc";
		the_td.style.fontSize="11";
		the_td.style.fontWeight="normal";		
		the_td.style.cursor = 'default';
		the_td.onclick=function(){}
		the_td.onmouseover=function(){}
		the_td.onmouseout=function(){}
	}

	now = new Date();
	now = new Date(now.getFullYear(), now.getMonth(), now.getDate());

  for (i=v_mthstart;i<v_mthstart+v_mthlen;i++) {
		var the_td=document.getElementById("d" + i);
		the_td.innerHTML=i-v_mthstart+1;
		the_td.style.background="#fff";
		the_td.style.cursor = 'pointer';
		var caldate = new Date(v_date.getFullYear(), v_date.getMonth(), i-v_mthstart+1);
		if (caldate < now) {
//			the_td.innerHTML = '&nbsp;';
			the_td.style.background="#ccc";
			the_td.style.cursor = 'default';
		} else {
				the_td.onclick=function(){setday(this.id);}
				the_td.onmouseover=function(){change_bg(this.id);}
				the_td.onmouseout=function(){clear_bg(this.id);}
			}
		if (v_stoday) {
			if (v_stoday==the_td.innerHTML && v_stomthname==v_mths[v_date.getMonth()] && v_stoyr==v_date.getFullYear()) {
				the_td.style.fontSize="14";
				the_td.style.fontWeight="bold";		
			}
		}
	}
	
//	alert(v_mthstart + ":" + v_mthlen);
}

function nmth(){
	v_date=new Date(v_date.getMonth()<11 ? v_date.getFullYear() : (v_date.getFullYear())+1 , v_date.getMonth()<11 ? (v_date.getMonth())+1 : 0, 1);
	pop_cal();
}

function pmth(){
	now = new Date();
	now = new Date(now.getFullYear(), now.getMonth(), 1);
	v_datesto=new Date(v_date.getMonth()>0 ? v_date.getFullYear() : (v_date.getFullYear())-1 , v_date.getMonth()>0 ? (v_date.getMonth())-1 : 11, 1);
	if (v_datesto >= now) {
		v_date = v_datesto;
	}
	pop_cal();
}

function nyr(){
	v_date=new Date(v_date.getFullYear()+1,v_date.getMonth(),1);
	pop_cal();
}

function pyr(){
	now = new Date();
	if (v_date.getFullYear() > now.getFullYear()) {
		v_date=new Date(v_date.getFullYear()-1,v_date.getMonth(),1);
		now = new Date();
		now = new Date(now.getFullYear(), now.getMonth(), 1);
		if (now > v_date) {
			v_date = now;
		}
		pop_cal();
	}
}

function setday(td_id){
	var the_td=document.getElementById(td_id);
	if (!(the_td.innerHTML=="&nbsp;")) {
		v_date=new Date(v_date.getFullYear(),v_date.getMonth(),the_td.innerHTML);
		v_ibox.options[v_ibox.selectedIndex].value=v_date.getDate() + "-" + v_mths_abbr[v_date.getMonth()] + "-" + v_date.getFullYear();
		v_ibox.options[v_ibox.selectedIndex].text=v_date.getDate() + "-" + v_mths_abbr[v_date.getMonth()] + "-" + v_date.getFullYear();
		hide_cal(v_table);
	}
}

function change_bg(td_id){
	var the_td=document.getElementById(td_id);
	if (!(the_td.innerHTML=="&nbsp;")) {
		the_td.style.fontSize="14";
		the_td.style.fontWeight="bold";
	}
}

function clear_bg(td_id){
	var the_td=document.getElementById(td_id);
	if (!(the_td.innerHTML=="&nbsp;")) {
		the_td.style.fontSize="11";
		the_td.style.fontWeight="normal";
	}
}

function hide_cal(the_table){
	var caltab=document.getElementById(the_table);
	if (caltab) {
		caltab.style.display='none';
//		v_ibox.style.display='';
	}
	var v_iframe=document.getElementById('srchiframe');
	v_iframe.contentWindow.document.getElementById('sdate').style.display='';
	v_iframe.contentWindow.document.getElementById('nights').style.display='';
	v_iframe.contentWindow.document.getElementById('days').style.display='';
}
function sC(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function gC(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
