﻿var CAL_IDS="";

if(window.addEventListener)
{
  window.addEventListener("onresize",CAL_WindowResize,false);
  window.addEventListener("onscroll",CAL_WindowResize,false);
}
else
{
  window.attachEvent("onresize",CAL_WindowResize);
  window.attachEvent("onscroll",CAL_WindowResize);
}

// Displays a JavaScript enabled calendar control.
function CAL_DisplayCalendar(e,id,dateTextBox,dateFormat){
  var c=document.getElementById(id+"CalendarRangeControl");
  document.body.appendChild(c);
  c.style.left=(e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)) - 5);
  c.style.top=(e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop)) - 5);
  c.style.visibility="visible";
  var dateValue=document.getElementById(dateTextBox).value;
  CAL_SetDate(id,dateValue,dateTextBox,dateFormat);
  document.getElementById(id+"closeCalendar").onclick=function CAL_Close(){document.getElementById(id+'CalendarRangeControl').style.visibility='hidden';};
}
function CAL_SetDate(id,dateValue,dateTextBox,dateFormat){
  var separator=dateFormat=="dd/MM/yyyy"?"/":" ";
  var d; 
  if(dateValue==""){
    d=new Date()
    var months=dateFormat=="dd/MM/yyyy"?new Array("01","02","03","04","05","06","07","08","09","10","11","12"):new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
    dateValue=(d.getDate().toString().length==1?"0"+d.getDate():d.getDate())+separator+months[d.getMonth()]+separator+d.getFullYear();    
  }    
  var dateParts=dateValue.split(separator);  
  var monthDd=document.getElementById(id+"CalendarMonth");
  var i=0;
  while(i<monthDd.options.length){ 
    if( (dateFormat=="dd/MM/yyyy" && monthDd.options[i].value == dateParts[1]) ||
        (dateFormat!="dd/MM/yyyy" && monthDd.options[i].innerHTML == dateParts[1]) ){
      monthDd.options[i].selected=true;
      i=monthDd.options.length;
    }
    else{
      i++;
    }
  }
  
  monthDd.onchange=function CAL_NewMonth(){CAL_SetDate(id,dateParts[0]+separator+(dateFormat=="dd/MM/yyyy"?monthDd.value:monthDd.options[monthDd.selectedIndex].innerHTML)+separator+dateParts[2],dateTextBox,dateFormat);}
  var yearDd=document.getElementById(id+"CalendarYear");
  i=0;
  while(i<yearDd.options.length){
    if(yearDd.options[i].innerHTML==dateParts[2]){
      yearDd.selectedIndex=i;
      i=yearDd.options.length;
    }
    else{
      i++;
    }
  }      
  dateParts[2]=yearDd.value;
  yearDd.onchange=function CAL_NewYear(){CAL_SetDate(id,dateParts[0]+separator+dateParts[1]+separator+yearDd.value,dateTextBox,dateFormat);}
  var tbody=document.getElementById(id+"DateRange");
  for(i=tbody.childNodes.length-1;i>=0;i--){
    tbody.removeChild(tbody.childNodes[i]);
  }
  d=new Date(dateParts[2], monthDd.selectedIndex, 1);
  d=new Date(d-86400000*d.getDay());    
  while((d.getMonth()<=monthDd.selectedIndex&&d.getFullYear()==dateParts[2])||(d.getMonth()==11&&monthDd.selectedIndex==0)){
    var row=tbody.insertRow(-1);
    for(i=0;i<7;i++){
      var cell=row.insertCell(-1);
      var cellClass;
      if(d.getDate()==dateParts[0]&&d.getMonth()==monthDd.selectedIndex){
        cellClass="CurrentDay";        
      }
      else{
        if(d.getDay()==0||d.getDay()==6){
          cellClass="WeekEnd";
        }
        else{
          cellClass="WeekDay";
        }          
      } 
      cell.className=cellClass;
      cell.i_value=cellClass;
      if(d.getMonth()==monthDd.selectedIndex){      
        cell.onmouseover=function Change(){this.className='DayOver';}; 
        cell.onmouseout=function Change(){this.className=this.i_value;};
        cell.innerHTML="<span onclick=\"document.getElementById('"+dateTextBox+"').value='"+(d.getDate().toString().length<2?"0"+d.getDate().toString():d.getDate().toString())+separator+dateParts[1]+separator+dateParts[2]+"';document.getElementById('"+id+"CalendarRangeControl').style.visibility='hidden';\" >"+d.getDate()+"</span>";
      }
      var currDay=d.getDay();            
      while(d.getDay()==currDay){
        d=new Date(d-0+86400000);
      }
    }
  }  
}
function CAL_ShowDateSelection(o,id,cid)
{
  if(CAL_IDS!="")
  {
    CAL_IDS=CAL_IDS+"|";
  }
  CAL_IDS=CAL_IDS+o.id+"~"+id+"~"+cid;
  CAL_WindowResize();
}
function CAL_HideDateSelection(id,cid)
{
  var ids=CAL_IDS.split("|");
  CAL_IDS="";
  for(var i=0;i<ids.length;i++)
  {
    if(ids[i].indexOf(id)==-1)
    {
      if(CAL_IDS!="")
      {
        CAL_IDS=CAL_IDS+"|";
      }
      CAL_IDS=CAL_IDS+ids[i];  
    }
  }  
  document.getElementById(id).style.display="none";
  document.getElementById(cid).style.display="none";
}

function CAL_WindowResize()
{
  var ids=CAL_IDS.split("|");
  for(var i=0; i<ids.length; i++)
  {
    if(ids[i]!="")
    {
      var objs=ids[i].split("~");
      var pos=CAL_findPos(document.getElementById(objs[0]));
      var sel=document.getElementById(objs[1]);
      sel.style.left=pos[0];  
      sel.style.top=pos[1];
      sel.style.display="block";
      var c=document.getElementById(objs[2]); 
      document.body.appendChild(c);
      c.style.width=(CALDLG_scrollWidth())+"px";
      c.style.height=(CALDLG_scrollHeight())+"px";
      c.style.display="block";      
    }
  }
}
function CAL_findPos(obj) {
	var curleft=0;
	var curtop=0;
  if(obj.offsetParent){
    do{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
    }while(obj=obj.offsetParent);
  }
  return [curleft,curtop];
}
function CALDLG_clientHeight() {
	return CALDLG_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function CALDLG_clientWidth() {
	return CALDLG_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function CALDLG_scrollTop() {
	return CALDLG_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function CALDLG_scrollLeft() {
	return CALDLG_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function CALDLG_scrollHeight() {	
  if(document.body){
    return CALDLG_filterResults (
      0,
	    0,
	    document.body ? document.body.scrollHeight : 0
	  );
  }
  else{
    return CALDLG_filterResults (
      0,
	    document.documentElement ? document.documentElement.scrollHeight : 0,
	    document.body ? document.body.scrollHeight : 0
	  );
  }		
}
function CALDLG_scrollWidth() {	
  if(document.body){
    return CALDLG_filterResults (
      0,
	    0,
	    document.body ? document.body.scrollWidth : 0
	  );
  }
  else{
    return CALDLG_filterResults (
      0,
	    document.documentElement ? document.documentElement.scrollWidth : 0,
	    document.body ? document.body.scrollWidth : 0
	  );
  }		
}
function CALDLG_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}



