var targetTextBoxForCalenderDate = '';
var MainCalender = document.getElementById("cal_popup");
function getObj(objID) {
    if (document.getElementById) {return document.getElementById(objID);}
    else if (document.all) {return document.all[objID];}
    else if (document.layers) {return document.layers[objID];}
}

function checkClick(e) {
  e?evt=e:evt=event;
  CSE=evt.target?evt.target:evt.srcElement;
  if (getObj('fc'))
    if (!isChild(CSE,getObj('fc')))
      getObj('fc').style.display='none';
}

function isChild(s,d) {
  while(s) {
    if (s==d) 
      return true;
    s=s.parentNode;
  }
  return false;
}

function Left(obj)
{
  var curleft = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curleft += obj.offsetLeft
      obj = obj.offsetParent;
    }
  }
  else if (obj.x)
    curleft += obj.x;
  return curleft;
}

function Top(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
  
	return curtop;
}

document.write('<div id ="cal_popup" class="cal_popup" style="display:none;"><iframe style="position:absolute;width:1000px;height:1000px;" frameborder="0"></iframe><table cellspacing="0" cellpadding="0" style="position:relative;"><tr><td><table id="fc" class="fc" style="display:none;" cellpadding=2>');
document.write('<tr><td class="LeftMonth" onclick="csubm()">&nbsp;</td><td colspan=5 id="mns" class="MonthName" align="center"></td><td align="right" class="RightMonth" onclick="caddm()">&nbsp;</td></tr>');
document.write('<tr><td align=center class="DayName">S</td><td align=center class="DayName">M</td><td align=center class="DayName">T</td><td align=center class="DayName">W</td><td align=center class="DayName">T</td><td align=center class="DayName">F</td><td align=center class="DayName">S</td></tr>');

for(var kk=1;kk<=6;kk++) {
  document.write('<tr>');
  for(var tt=1;tt<=7;tt++) {
    num=7 * (kk-1) - (-tt);
    document.write('<td id="v' + num + '">&nbsp;</td>');
  }
  document.write('</tr>');
}

document.write('</table></td></tr></table></div>');
document.all?document.attachEvent('onclick',checkClick):document.addEventListener('click',checkClick,false);


// Calendar script
var now = new Date;
var scfd=now.getDate();
var cfd=now.getDate();
var sccm=now.getMonth();
var sccy=now.getFullYear();
var ccm=now.getMonth();
var ccy=now.getFullYear();

var updobj;

function lcs(ielem,topPos,leftPos) {
	updobj=ielem;
	// ~ getObj('fc').style.left=Left(ielem)+"px";
	// ~ getObj('fc').style.top=Top(ielem)+ielem.offsetHeight+"px";
	getObj('fc').style.left="0px";
	getObj('fc').style.top="0px";
	
	if (parseInt(leftPos) < 0) {
	    	document.getElementById("cal_popup").style.left= Left(ielem)+"px";    
	}
	else {
	    document.getElementById("cal_popup").style.left = leftPos+"px";
	}
	
	if (topPos < 0) {
	    document.getElementById("cal_popup").style.top= Top(ielem)+ielem.offsetHeight+"px";
	}
	else {
	    document.getElementById("cal_popup").style.top= topPos+"px";
	}
	
	
	getObj('fc').style.display='';
	getObj('cal_popup').style.display='';
 
	// First check date is valid
	// curdt=ielem.value;
  
	// curdt =
	// document.getElementById("day").value+"/"+(document.getElementById("month").value*1)+"/"+document.getElementById("year").value;
	curdt = document.getElementById("txtCheckin").value;
	curdtarr=curdt.split('/');
	isdt=true;
 
	for(var k=0;k<curdtarr.length;k++) {
		if (isNaN(curdtarr[k]))
			isdt=false;
	}

	if (isdt&(curdtarr.length==3)) {
		ccm=curdtarr[1]-1;
		ccy=curdtarr[2];
		prepcalendar(curdtarr[0],curdtarr[1]-1,curdtarr[2]);
	}
}

function staticCalendar(element) {
	alert(document.getElementsByClassName("fc"));
}

function lcs_new(ielem, targetTextBox) {
	
  targetTextBoxForCalenderDate = targetTextBox;
  updobj=ielem;
  // ~ getObj('fc').style.left=Left(ielem)+"px";
  // ~ getObj('fc').style.top=Top(ielem)+ielem.offsetHeight+"px";
  getObj('fc').style.left="0px";
  getObj('fc').style.top="0px";
  document.getElementById("cal_popup").style.left=Left(ielem)+"px";
  document.getElementById("cal_popup").style.top=Top(ielem)+ielem.offsetHeight+"px";
  getObj('fc').style.display='';
  getObj('cal_popup').style.display='';
    
  
  // First check date is valid
  // curdt=ielem.value;
    
  // curdt =
  // document.getElementById("day").value+"/"+(document.getElementById("month").value*1)+"/"+document.getElementById("year").value;
  curdt = document.getElementById(targetTextBoxForCalenderDate).value;
    
  curdtarr=curdt.split('/');
  isdt=true;
  
  for(var k=0;k<curdtarr.length;k++) {
    if (isNaN(curdtarr[k]))
      isdt=false;
  }
  
  if (isdt&(curdtarr.length==3)) {
	  ccm=curdtarr[1]-1;
	  ccy=curdtarr[2];
	  prepcalendar(curdtarr[0],curdtarr[1]-1,curdtarr[2]);
  }
}


function evtTgt(e)
{
  var el;
  if(e.target)el=e.target;
  else if(e.srcElement)el=e.srcElement;
  if(el.nodeType==3)el=el.parentNode; // defeat Safari bug
  return el;
}
function EvtObj(e){if(!e)e=window.event;return e;}
function cs_over(e) {
  evtTgt(EvtObj(e)).style.background='#FFCC66';
}
function cs_out(e) {
  evtTgt(EvtObj(e)).style.background='#C4D3EA';
}
function cs_click(e) {
	// ~
	// updobj.value=calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)];
	times = calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)].split('/');
    
	if (times[0] < 10) times[0] = "0"+times[0];
	/*
	 * for (var idx=0;idx<document.getElementById('day').options.length;idx++) {
	 * if (times[0]==document.getElementById('day').options[idx].value) {
	 * document.getElementById('day').selectedIndex=idx; } }
	 * 
	 * if (times[1] < 10) times[1] = "0"+times[1]; for (var idx=0;idx<document.getElementById('month').options.length;idx++) {
	 * if (times[1]==document.getElementById('month').options[idx].value) {
	 * document.getElementById('month').selectedIndex=idx; } }
	 * 
	 * for (var idx=0;idx<document.getElementById('year').options.length;idx++) {
	 * if (times[2]==document.getElementById('year').options[idx].value) {
	 * document.getElementById('year').selectedIndex=idx; } }
	 */

	if(parseInt(times[1])<10) {
		times[1]='0'+times[1];
	}
	// TA: --------------------------------
	// This change is useful when lcs_new() is called, instead of lcs(), so as
	// to use Calender on multiple textboxes.
	// The id of the textbox must be passed through lcs_new(), which will set
	// 'targetTextBoxForCalenderDate'.
	// Otherwise with lcs(), the only text box which can be used with calendar
	// should have id "txtCheckin".
	// document.getElementById("txtCheckin").value=times[0]+'/'+times[1]+'/'+times[2]
	

	if('' != targetTextBoxForCalenderDate) {
	    document.getElementById(targetTextBoxForCalenderDate).value=times[0]+'/'+times[1]+'/'+times[2];
	    alert(targetTextBoxForCalenderDate);
	    eventAdd = document.getElementById(targetTextBoxForCalenderDate); 
	}
	else {
	    document.getElementById("txtCheckin").value=times[0]+'/'+times[1]+'/'+times[2];
	    eventAdd  = document.getElementById("txtCheckin");
	}
	
	if (document.createEvent) { 
	    // alert("FF");
	    myEvent = document.createEvent('HTMLEvents');
	    myEvent.initEvent('change', true, true);
	    eventAdd.dispatchEvent(myEvent); 
	}  
	else if (document.createEventObject) { 
	    // alert("IE"); 
	    myEvent = document.createEventObject(); 
	    eventAdd.fireEvent('onchange', myEvent); 
	}
	
	
	
	// TA: --------------------------------
	
	getObj('fc').style.display='none';
	getObj('cal_popup').style.display='none';
}

var mn=new Array('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC');
var mnn=new Array('31','28','31','30','31','30','31','31','30','31','30','31');
var mnl=new Array('31','29','31','30','31','30','31','31','30','31','30','31');
var calvalarr=new Array(42);

function f_cps(obj) {
	obj.className = 'Active';
}

function f_cpps(obj) {
	obj.className = 'Deactive';
}

function f_hds(obj) {
	obj.className = 'Selected';
}

// day selected
function prepcalendar(hd,cm,cy) {
  now=new Date();
  now.setDate(now.getDate() + 1);
  sd=now.getDate();
  td=new Date();
  td.setDate(1);
  td.setFullYear(cy);
  td.setMonth(cm);
  cd=td.getDay();
  getObj('mns').innerHTML=mn[cm]+ ' ' + cy;
  marr=((cy%4)==0)?mnl:mnn;
  for(var d=1;d<=42;d++) {
    f_cps(getObj('v'+parseInt(d)));
    if ((d >= (cd -(-1))) && (d<=cd-(-marr[cm]))) {
      dip=((d-cd < sd)&&(cm==sccm)&&(cy==sccy));
      htd=((hd!='')&&(d-cd==hd));
      if (dip)
        f_cpps(getObj('v'+parseInt(d)));
      else if (htd)
        f_hds(getObj('v'+parseInt(d)));
      else
        f_cps(getObj('v'+parseInt(d)));

      //getObj('v'+parseInt(d)).onmouseover=(dip)?null:cs_over;
      //getObj('v'+parseInt(d)).onmouseout=(dip)?null:cs_out;
      getObj('v'+parseInt(d)).onclick=(dip)?null:cs_click;
      
      getObj('v'+parseInt(d)).innerHTML=d-cd;	
      calvalarr[d]=''+(d-cd)+'/'+(cm-(-1))+'/'+cy;
          // ~ day = (d-cd);
          // ~ month = (cm-(-1));
          // ~ year = cy;
    }
    else {
      getObj('v'+d).innerHTML='&nbsp;';
      getObj('v'+parseInt(d)).onmouseover=null;
      getObj('v'+parseInt(d)).onmouseout=null;
      getObj('v'+parseInt(d)).style.cursor='default';
      }
  }
}

prepcalendar('',ccm,ccy);
// getObj('fc'+cc).style.visibility='hidden';

function caddm() {
  marr=((ccy%4)==0)?mnl:mnn;
  
  ccm+=1;
  if (ccm>=12) {
    ccm=0;
    ccy++;
  }
  cdayf();
  prepcalendar('',ccm,ccy);
}

function csubm() {
  marr=((ccy%4)==0)?mnl:mnn;
  
  ccm-=1;
  if (ccm<0) {
    ccm=11;
    ccy--;
  }
  cdayf();
  prepcalendar('',ccm,ccy);
}

function cdayf() {
if ((ccy>sccy)|((ccy==sccy)&&(ccm>=sccm)))
  return;
else {
  ccy=sccy;
  ccm=sccm;
  cfd=scfd;
  }
}
