﻿var AS_IDS="";

if(window.addEventListener)
{
  window.addEventListener("onresize",AS_WindowResize,false);
  window.addEventListener("onscroll",AS_WindowResize,false);
}
else
{
  window.attachEvent("onresize",AS_WindowResize);
  window.attachEvent("onscroll",AS_WindowResize);
}

// Displays a JavaScript enabled calendar control.
function AS_DisplayCalendar(e,id,dateTextBox,dateFormat){
  var c=document.getElementById(id+"CalendarControl");
  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;
  AS_SetDate(id,dateValue,dateTextBox,dateFormat);
  document.getElementById(id+"closeCalendar").onclick=function AS_Close(){document.getElementById(id+'CalendarControl').style.visibility='hidden';};
}
function AS_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 AS_NewMonth(){AS_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 AS_NewYear(){AS_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+"CalendarControl').style.visibility='hidden';\" >"+d.getDate()+"</span>";
      }
      var currDay=d.getDay();            
      while(d.getDay()==currDay){
        d=new Date(d-0+86400000);
      }
    }
  }  
}
function AS_ShowDateSelection(o,id,cid)
{
  if(AS_IDS!="")
  {
    AS_IDS=AS_IDS+"|";
  }
  AS_IDS=AS_IDS+o.id+"~"+id+"~"+cid;
  AS_WindowResize();
}
function AS_HideDateSelection(id,cid)
{
  var ids=AS_IDS.split("|");
  AS_IDS="";
  for(var i=0;i<ids.length;i++)
  {
    if(ids[i].indexOf(id)==-1)
    {
      if(AS_IDS!="")
      {
        AS_IDS=AS_IDS+"|";
      }
      AS_IDS=AS_IDS+ids[i];  
    }
  }  
  document.getElementById(id).style.display="none";
  document.getElementById(cid).style.display="none";
}

function AS_WindowResize()
{
  var ids=AS_IDS.split("|");
  for(var i=0; i<ids.length; i++)
  {
    if(ids[i]!="")
    {
      var objs=ids[i].split("~");
      var pos=AS_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=(ASDLG_scrollWidth())+"px";
      c.style.height=(ASDLG_scrollHeight())+"px";
      c.style.display="block";      
    }
  }
}
function AS_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 ASDLG_clientHeight() {
	return ASDLG_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function ASDLG_clientWidth() {
	return ASDLG_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function ASDLG_scrollTop() {
	return ASDLG_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function ASDLG_scrollLeft() {
	return ASDLG_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function ASDLG_scrollHeight() {	
  if(document.body){
    return ASDLG_filterResults (
      0,
	    0,
	    document.body ? document.body.scrollHeight : 0
	  );
  }
  else{
    return ASDLG_filterResults (
      0,
	    document.documentElement ? document.documentElement.scrollHeight : 0,
	    document.body ? document.body.scrollHeight : 0
	  );
  }		
}
function ASDLG_scrollWidth() {	
  if(document.body){
    return ASDLG_filterResults (
      0,
	    0,
	    document.body ? document.body.scrollWidth : 0
	  );
  }
  else{
    return ASDLG_filterResults (
      0,
	    document.documentElement ? document.documentElement.scrollWidth : 0,
	    document.body ? document.body.scrollWidth : 0
	  );
  }		
}
function ASDLG_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;
}