/*
	
	DATE_FORMAT = "MM/dd/yyyy"
	DEFAULT_CHECKIN_OFF_NUM = "3"
	The variabel DATE_FORMAT is need to be defined, cause it used by the validate funtion. It is defined in the common.js file.
	
	DEFAULT_DURATION  is need to auto set the check out date after the check in date changed

	This function is used to set the field's value. If you need to change the date field's format, you need to modify this function.
	function setDate(month,day,year,fieldName)

	The function getDateFromFormat(), formatDate() which are called in this file is definded the data.js, please make the date.js is included first.
    The function populateFormElement is from common.js
	The function showWeek is from common.js
	
*/

var IE=GECKO=MAC=OPERA=KHTML=NS=false;
var ua=navigator.userAgent;
IE=(ua.indexOf('MSIE')!=-1) ? parseFloat(ua.substring(ua.indexOf('MSIE')+5,ua.indexOf('MSIE')+8)) : false;
if (ua.indexOf('Gecko')!=-1) GECKO=true;
if (ua.indexOf('Mac')!=-1) MAC=true;
if (ua.indexOf('Opera')!=-1) {IE=GECKO=false;OPERA=true;}
if (ua.indexOf('KHTML')!=-1) {IE=GECKO=false;KHTML=true;}
if (ua.indexOf('Netscape')!=-1&&GECKO) NS=true;

var iframeMenu=(IE>=5) ? true : false;
var divMenu=(GECKO||OPERA||KHTML) ? true : false;
var menuStates=new Object();
var menuParent=new Object();
var timeout;

function addMenu(id) {
  menuStates[id]=new Object();
  menuStates[id]['on']=true;
  if (iframeMenu) newIframe(id);
  if (divMenu) newDiv(id);
  if (!wrapper(id)) return;
  if (MAC&&IE) {
    thisMenu(id).style.visibility="visible";
    resizeMenu(id);
    moveMenu(id);
    thisMenu(id).style.visibility="hidden";
  }
}

//function fadeMenu(id) { stopFade(); if (menuParent[id]) return; timeout=setTimeout("hideAll()",1500); }
function fadeMenu(id)
{
	stopFade();
	if (menuParent[id])
	{
		return
	}
	timeout=setTimeout("hideAll()",5000); 
	}


function hideAll(oId) {
  stopFade();
  for (var id in menuStates) {
    if (thisMenu(id)&&oId!=id&&id!=menuParent[oId]&&(menuStates[id]['on']||!oId)) {
      thisMenu(id).style.visibility="hidden";
      if (iframeMenu&&menuParent[id]&&thisOpener(id+'Open',menuParent[id]))
        thisOpener(id+'Open',menuParent[id]).className="top_sub";
      if (divMenu&&menuParent[id]&&thisMenu(id+'Open'))
        thisMenu(id+'Open').className="top_sub";
      menuStates[id]['on']=false;
    }
  }
}

function insertHTML(menu,html) 
{
    menu.innerHTML=html;
}





function insertMenu(mId,t,r,b,l) {
  if (divMenu) {
    var html='<div valign="bottom" style="position: absolute; visibility: hidden;';
    if (t !=-1&&r !=-1&&b !=-1&&l !=-1) html+=' margin: '+t+'px '+r+'px '+b+'px '+l+'px;';
    return html+='" id="'+mId+'"></div>';
  } else {
    var html='<iframe style="position: absolute; visibility: hidden;';
    if (t !=-1&&r !=-1&&b !=-1&&l !=-1) html+=' margin: '+t+'px '+r+'px '+b+'px '+l+'px;';
    return html+='" id="'+mId+'" frameborder="0" scrolling="no"></iframe>';
  }
}


function linkOff(item) {
  var id=item['id'].replace(/Open/,'');
  if (menuStates[id]) if (menuStates[id]['on']) return;
  item.className='top_sub';
}

function linkOn(item) { stopFade(); item.className='top_sub_hover'; }

function moveMenu(id) {
  if (!menuParent[id]) return;
  var mParent=thisMenu(menuParent[id]);
  var doc=(divMenu) ? thisDoc(id+"Open") : thisOpener(id+"Open",menuParent[id]);
  var ie_gecko_left=(IE>=5.5) ? -3 : (OPERA) ? 3 : -1;
  var ie_top=(IE>=5.5) ? -7 : (GECKO) ? -1 : (OPERA) ? 3 : -6;
  if (MAC&&IE) { ie_top=3; ie_gecko_left=3; }
  var winHeight=document.body.clientHeight+document.body.scrollTop;
  var top=(KHTML||OPERA||GECKO||IE>=5.5||(MAC&&IE>=5)) ?
        mParent.offsetTop+doc.offsetTop+ie_top :
        mParent.top+ie_top+doc.offsetTop+document.body.scrollTop+1;
  var fromBottom=(top+thisMenu(id).offsetHeight>winHeight) ?
        winHeight-thisMenu(id).offsetHeight-1 : top;
  thisMenu(id).style.top=(fromBottom<1+document.body.scrollTop) ?
    1+document.body.scrollTop : fromBottom;
  thisMenu(id).style.left=(KHTML||OPERA||GECKO||IE>=5.5||(MAC&&IE>=5)) ?
    mParent.offsetLeft+ie_gecko_left+mParent.offsetWidth :
    mParent.left-1+mParent.offsetWidth;
}

function newDiv(id) {
  insertHTML(thisMenu(id),'<table border="0" cellpadding="0" cellspacing="0"><tr><td><div id="menuWrapper'+id+'" style="border-style:solid; border-color:#000; background: #FFF; border-width:1px;"></div></td></tr></table>');
}

function newIframe(id) {
  if (IE) thisDoc(id).createStyleSheet(mainStyleSheet.src);

  var ss=(!IE) ? '<link rel="stylesheet" href="'+mainStyleSheet.src+'" type="text/css">' : '';
//  setTimeout("test()",5000);

  var mMar=(MAC&&IE==5) ? ' margin:-4px 0px 0px-4px;' : '';
  thisDoc(id).body.text="#000000";
  insertHTML(thisDoc(id).body,ss+'<table border="0" cellpadding="0" cellspacing="0" style="position: absolute; left: 0; top: 0;"><tr><td><div id="menuWrapper" style="border-style:solid; border-color:#000; background: #FFF; border-width:1px;'+mMar+'">Menu Loading...</div></td></tr></table>');
}


function pointerType() { return (GECKO) ? 'pointer' : 'hand'; }

function resizeMenu(id) {
  var oId=(thisMenu(id+"_Cell")||false);
  if (oId&&IE) if (oId.offsetWidth>thisMenu(id).width+3) addMenu(id);
  thisMenu(id).width=wrapper(id).offsetWidth;
  thisMenu(id).height=wrapper(id).offsetHeight;
  thisMenu(id).style.zIndex=(menuParent[id]) ? 1001 : 1000;
  thisMenu(id).style.filter="progid:DXImageTransform.Microsoft.Shadow(color='#999999',Direction=135,Strength=2)";
}

function showMenu(id) {
  if (!menuStates[id]) 
    {
      addMenu(id);

    }

  menuStates[id]['on']=true;
  stopFade();
  hideAll(id);
  resizeMenu(id);
  moveMenu(id);
  if (menuParent[id]&&menuStates[id]['on']) {
    if (iframeMenu) thisOpener(id+'Open',menuParent[id]).className="top_sub_hover";
    if (divMenu) thisMenu(id+'Open').className="top_sub_hover";
  }
  thisMenu(id).style.visibility='visible';
}



function stopFade() { clearTimeout(timeout); }

function thisDoc(id) { return (IE&&iframeMenu&&document.frames(id)) ? document.frames(id).document :
         (iframeMenu&&GECKO) ? thisMenu(id).contentDocument : thisMenu(id);}

function thisMenu(id) { return document.getElementById(id); }

function thisOpener(id,parent) { return (thisDoc(parent)) ? thisDoc(parent).getElementById(id) : thisDoc(id); }

function wrapper(id) { return (iframeMenu) ? thisOpener('menuWrapper',id) : thisMenu('menuWrapper'+id); }

document.onmouseup=hideAll;
//another one
var days=new Array("Su","Mo","Tu","We","Th","Fr","Sa");
var months=new Array('','January','February','March','April','May','June','July','August','September','October','November','December');
var years=new Array('2004','2005');

var selectedMonth=new Array('','','','','','','','','','','','','');
var selectedYear=new Array('','');

var monthDays=new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
var todaysDate=new myGetDate();
var calendarOpen=new Object();

function myGetDate(month,day,year) {
  if (day&&year) {month--; var rawDate=new Date(year,month,day);}
  else {var rawDate=new Date();}
  this.year=rawDate.getFullYear();
  this.month=rawDate.getMonth();this.month++;
  this.day=rawDate.getDate();
  this.dow=rawDate.getDay();
  this.date=parseInt(this.year.toString()+makeDouble(this.month.toString())+makeDouble(this.day.toString()));
  this.monthDays=monthDays;this.monthDays[2]=(this.year%4)?28:29;
  this.prev_month=(this.month==1)?12:(this.month-1);
  this.prev_year=(this.month==1)?(this.year-1):this.year;
  this.next_month=(this.month==12)?1:(this.month+1);
  this.next_year=(this.month==12)?(this.year+1):this.year;
  return this;
}

function parseDate(testDate) {
  testDate = testDate.replace(/\W|\s+/g, ',');
  var month,day,year;
  var dateArray=new Array();dateArray=testDate.split(',');
  for (var i=0;i<dateArray.length;i++) {dateArray[i]=parseFloat(dateArray[i]);}
  if (dateArray[2]<30) {year=parseInt(dateArray[2])+2000;}
  else if (dateArray[2]>30&&dateArray[2]<100) {year=parseInt(dateArray[2])+1900;}
  else {year=dateArray[2];}
  month=dateArray[0];
  if (month>12) month=0;
  if (month) {
    day=parseInt(dateArray[1]);
    if (parseInt(day)>monthDays[month]) {day=monthDays[month];}
  }
  if (month&&day&&year) {return myGetDate(month,day,year);}
  else {return myGetDate();}
}

function makeDouble(number) {
  if ((number.toString()).length>=2) {return number;}
  else if (number) {var numString = number.toString();return('0'+numString);}
  return number;
}
function changeMonth(theElement,theCalID,month,day,year,fieldName,inDayID, outDayID,update,jump)
{
	theMonth=theElement.options[theElement.selectedIndex].value
	parent.drawCalendar(theCalID,theMonth, 1,year, fieldName,inDayID,outDayID,0,1)
}
function changeYear(theElement,theCalID,month,day,year,fieldName,inDayID,outDayID,update,jump)
{
	theYear=theElement.options[theElement.selectedIndex].value
	parent.drawCalendar(theCalID,month, 1,theYear, fieldName,inDayID,outDayID,0,1)
}

function drawCalendar(theCalID,month,day,year,fieldName, inDayID,outDayID, update,jump) {

  field = eval(fieldName)
  var calID=theCalID;
  var formDate=parseDate(field.value);
  if (formDate.date!=todaysDate.date&&!update&&!jump) {var selectedDate=formDate;}
  else {var selectedDate=new myGetDate(month,day,year);}
  if (!jump) {
	 setDate(selectedDate.month,selectedDate.day,selectedDate.year,fieldName, inDayID, outDayID);}
 
  monthDate=new myGetDate(selectedDate.month,1,selectedDate.year);
  if (!divMenu&&!iframeMenu) return; 
  var calHtml = '<style type="text/css">.calgray {font-family: verdana, arial, sans-serif; font-size: 10px; color: #CCC;}.small {font-family: verdana, arial, sans-serif; font-size: 10px;}.cal {font-family: arial, verdana;font-size: 10px;}</style>';
  
  calHtml += ' <table border="1" class="calbg" cellpadding="2" cellspacing="0"  width="150" onmouseover="parent.stopFade()" onmouseout="parent.fadeMenu(\''+calID+'\')">';
  calHtml += '  <tr valign="top">';
  calHtml += '   <td width="100%" >';
  
  len = selectedMonth.length
 for (i=0;i<len ;i++ )
  {
	  selectedMonth[i]="";
  }
  selectedMonth[selectedDate.month]='selected';  
  calHtml += '&nbsp;<select bgcolor="#FFFFFF"  name="month" onchange="parent.changeMonth(this, \''+calID +'\',' +selectedDate.month+', 1, '+selectedDate.year+', \''+fieldName +'\',0,1);"><option value=1 '+selectedMonth[1]+' >'+months[1]+'<option value=2 '+selectedMonth[2]+' >'+months[2]+'<option value=3  '+selectedMonth[3]+' >'+months[3]+'<option value=4 '+selectedMonth[4]+' >'+ months[4]+'<option value=5 '+selectedMonth[5]+' >'+months[5]+'<option value=6  '+selectedMonth[6]+' >'+months[6]+'<option value=7 '+selectedMonth[7]+' >'+months[7]+'<option value=8 '+selectedMonth[8]+' >'+months[8]+'<option value=9 '+selectedMonth[9]+' >'+months[9]+'<option value=10 '+selectedMonth[10]+' >'+months[10]+'<option value=11  '+selectedMonth[11]+' >'+months[11]+'<option value=12  '+selectedMonth[12]+' >'+months[12]+'</select>';
  calHtml += '&nbsp;&nbsp';
  calHtml += '<select name="year" onchange="parent.changeYear(this, \''+calID +'\',' +selectedDate.month+', 1, '+selectedDate.year+', \''+fieldName +'\',0,1);">';
  
 len = years.length

 selectedYear[selectedDate.year]='selected';  

 //alert(selectedYear[selectedDate.year])
 for (i=0;i<len ;i++ )
  {
	 selected="";
	if(selectedDate.year == years[i]) selected="selected"
	 
	calHtml  +='<option value='+ years[i]+' ' + selected + ' >'+years[i];
  }
  calHtml += '</select>';
  calHtml += '    <table border="0" cellspacing="0" cellpadding="0" width="100%">';
  calHtml += '<tr><td colspan=3 class=small height=5></td></tr>'
  calHtml += '     <tr valign="top">';
  calHtml += '      <td>';
  if (selectedDate.date<=todaysDate.date) {
    calHtml += '     <img id="t" name="t" src="'+spacer.src+'" width="37" height="13" border="0" /></td>';
  }
  else {
    calHtml += '     <a href="JavaScript:void(0)" onClick="parent.drawCalendar(\''+calID +'\',' +selectedDate.prev_month+', 1, '+selectedDate.prev_year+', \''+fieldName +'\''+ ',\''+inDayID+'\','+ '\''+outDayID+'\', 0,1);"><img src="'+cal_last.src+'" id="cal_last" name="cal_last" width="37" height="13" border="0" /></a></td>';
  }
  calHtml += '      <td class="small" width="100%" align="center" valign="middle" nowrap="nowrap">';
  calHtml += '&nbsp;<b>'+months[selectedDate.month]+' '+selectedDate.year+'</b>&nbsp;</td>';
  calHtml += '      <td>';
  calHtml += '       <a href="JavaScript:void(0)" onClick="parent.drawCalendar(\''+calID +'\',' +selectedDate.next_month+', 1, '+selectedDate.next_year+', \''+fieldName  +'\''+ ',\''+inDayID+'\','+ '\''+outDayID+'\',0,1);"><img src="'+cal_next.src+'" id="cal_next" name="cal_next" width="37" height="13" border="0" /></a></td>';
  calHtml += '     </tr>';
  calHtml += '    </table>';
  calHtml += '    <table border="0" class="calNextMonth" cellspacing="0" cellpadding="3" width="100%">';
  calHtml += '     <tr class="cal" valign="top">';
  // Days of the Week column titles
  for (var i=0;i<days.length;i++) {
    calHtml += '      <td width="14%" valign="top" align="center">'+days[i]+'</td>';
  }
  calHtml += '     </tr>';
  var weekNum = 1;
  var rowNum = 0;
  calHtml += '     <tr id="week"'+weekNum+'" class="cal" valign="top">';
  // Previous Months Days
  calHtml += getPrevMonthDays(monthDate, selectedDate, '\'parent.'+fieldName+ '\'');
  // This Months Days
  for (var thisDay=1;thisDay<=selectedDate.monthDays[selectedDate.month];thisDay++) {
    var dateString=parseInt(selectedDate.year.toString()+makeDouble(selectedDate.month.toString())+makeDouble(thisDay.toString()));
    if (((thisDay-1)+monthDate.dow)%7==0&&((thisDay-1)+monthDate.dow)!=0) {
      calHtml += '     </tr>';
      weekNum++;
      calHtml += '     <tr id="week"'+weekNum+'" class="cal" valign="top">';
    }
    // Calendar Days Display
    if (dateString==formDate.date) {
      rowNum = weekNum;
      calHtml += '      <td width="14%" align="center" class="calactivedate"><a style="text-decoration: none;" href="JavaScript:void(0)" onClick="parent.drawCalendar(\''+calID +'\',' +selectedDate.month+', '+thisDay+', '+selectedDate.year+', \'parent.'+ fieldName  +'\''+ ',\''+inDayID+'\','+ '\''+outDayID+'\',0,1);"><b>'+makeDouble(thisDay)+'</b></a>';
    }
    else if (dateString<todaysDate.date) {
      calHtml += '      <td width="14%" class="calgray" align="center" style="text-decoration: line-through;">'+thisDay;
    }
    else {
       calHtml += '      <td width="14%" align="center"><a style="text-decoration: none;" href="JavaScript:void(0)" onClick="parent.drawCalendar(\''+calID +'\',' +selectedDate.month+', '+thisDay+', '+selectedDate.year+', \'parent.'+fieldName +'\''+ ',\''+inDayID+'\',\''+ outDayID + '\',1); parent.hideAll();">'+makeDouble(thisDay)+'</a>';
    }
    calHtml += '      </td>';
	//alert(calHtml);
  }
  // Next Months Days
  calHtml += getTailMonthDays(monthDate,selectedDate,"\'parent."+fieldName+"\'");
  calHtml += '     </tr>';
  calHtml += '     <tr class="cal" valign="top">';
  calHtml += '      <td colspan="7" width="100%" align="center" class="calbg">';
  calHtml += '       <a href="JavaScript:void(0)" onClick="parent.hideAll();parent.calendarOpen[\''+calID+'\']=\'off\';">Cancel</a></td>';
  calHtml += '     </tr>';
  calHtml += '    </table>';
  calHtml += '   </td>';
  calHtml += '  </tr>';
  calHtml += ' </table>';
//  calHtml += '<script'+'t> parent.populateCal(' + selectedDate.month +','+selectedDate.year+')</scrip'+'t>';
  if( !menuStates[calID] ) addMenu( calID );
  insertHTML(wrapper(calID),calHtml);
  wrapper(calID).style.border='0px';
  showMenu(calID);
}



function setDate(month,day,year,elementName, inDayID, outDayID) {
  field =eval(elementName)
  field.value=makeDouble((month))+'/'+makeDouble(day)+'/'+year;
  tmp = new Array();
  tmp = elementName.split(".");
  len = tmp.length
   formName=tmp[len-2];
  fieldName=tmp[len-1];
  if (fieldName=="CheckInDate")
  {

	var theCheckInDate = new Date (getDateFromFormat(field.value,DATE_FORMAT))
	var checkOutDateStr = formatDate( new Date(theCheckInDate.getTime() + DEFAULT_DURATION*24*60*60*1000), DATE_FORMAT)
    var checkOutArray = checkOutDateStr.split("/")

	populateFormElement(eval("document." + formName + ".doa_mm"), month)
	populateFormElement(eval("document." + formName + ".doa_dd"), day)
	populateFormElement(eval("document." + formName + ".doa_yy"), year)

	populateFormElement(eval("document." + formName + ".dod_mm"), checkOutArray[0])
	populateFormElement(eval("document." + formName + ".dod_dd"), checkOutArray[1])
	populateFormElement(eval("document." + formName + ".dod_yy"), checkOutArray[2])
	checkOutDateField = eval("document."+formName +".CheckOutDate");
	checkOutDateField.value=  checkOutDateStr;
	showWeek(elementName, inDayID)
	showWeek("document."+formName +".CheckOutDate", outDayID)
	
  }
  else
	{
		populateFormElement(eval("document." + formName + ".dod_mm"), month)
		populateFormElement(eval("document." + formName + ".dod_dd"), day)
		populateFormElement(eval("document." + formName + ".dod_yy"), year)
		
		showWeek(elementName, outDayID)

	}
}


function getPrevMonthDays(monthDate,selectedDate,id) {
  var calHtml='';
  var days=monthDays[selectedDate.prev_month];
  var day;
  for (var i=monthDate.dow-1;i>=0;i--) {
    day = days-i;
    calHtml += '      <td width="14%" bgcolor="#FFFFFF" class="calgray" align="center">'+day+'</td>';
  }
  return calHtml;
}

function getTailMonthDays(monthDate,selectedDate,id) {
  var calHtml='';
  var tailDays=7-((parseInt(selectedDate.monthDays[selectedDate.month])+parseInt(monthDate.dow))%7);
  if (tailDays>=7) tailDays=0;
  for (var day=1; day<=tailDays;day++) {
    calHtml += '      <td width="14%" bgcolor="#FFFFFF" class="calgray" align="center">'+makeDouble(day)+'</td>';
  }
  return calHtml;
}



