﻿var __dialogs=new Array();
var __CALENDAR_DLG;
var __CALENDAR_TARGET;
var __CALENDAR_TEMPLATE;
var __CALENDAR_CURR_DATE;
var __CALENDAR_INCLUDE_TIME;
var __CALENDAR_HOURS;
var __CALENDAR_MINUTES;
var __CALENDAR_SECONDS;

if(window.addEventListener)
{
  window.addEventListener("onresize",__WindowResize,false);
  window.addEventListener("onscroll",__WindowResize,false);
}
else
{
  window.attachEvent("onresize",__WindowResize);
  window.attachEvent("onscroll",__WindowResize);
}
window.onresize=__WindowResize;
window.onscroll=__WindowResize;

function __ShowCalendar(e,ctlId,template)
{
  __CALENDAR_TARGET=ctlId;
  __CALENDAR_TEMPLATE=template;
  __CALENDAR_DLG=__CreateDialog("CalendarControl","&#160;",false);
  __CALENDAR_DLG.className="Dialog Calendar";
  __CALENDAR_DLG.setAttribute("positioning","IGNORE");
  var id=__CALENDAR_DLG.id;
  __ShowDialog(__CALENDAR_DLG);
  __CALENDAR_DLG.style.left=(e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)) - 5);
  __CALENDAR_DLG.style.top=(e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop)) - 5);
  var date=__GetValue(ctlId);
  __CALENDAR_HOURS=null;
  __CALENDAR_MINUTES=null;
  __CALENDAR_SECONDS=null;
  __CALENDAR_CURR_DATE=__ParseDate(date);
  __RenderCalendar(date,"day");
}
function __RenderCalendar(date,mode)
{
  __CALENDAR_INCLUDE_TIME=false;
  var dateObj=__ParseDate(date);
  var monthName=(dateObj.getMonth()==0?"January":dateObj.getMonth()==1?"February":dateObj.getMonth()==2?"March":dateObj.getMonth()==3?"April":dateObj.getMonth()==4?"May":dateObj.getMonth()==5?"June":dateObj.getMonth()==6?"July":dateObj.getMonth()==7?"August":dateObj.getMonth()==8?"September":dateObj.getMonth()==9?"October":dateObj.getMonth()==10?"November":"December");
  var lastDay=32 - new Date(dateObj.getFullYear(),dateObj.getMonth(),32).getDate();
  var prevLastDay=32 - new Date((dateObj.getMonth()==0?dateObj.getFullYear()-1:dateObj.getFullYear()),(dateObj.getMonth()==0?11:dateObj.getMonth()-1),32).getDate();
  var nextLastDay=32 - new Date((dateObj.getMonth()==11?dateObj.getFullYear()+1:dateObj.getFullYear()),(dateObj.getMonth()==11?0:dateObj.getMonth()+1),32).getDate();
  var monthStartObj=new Date();
  monthStartObj.setFullYear(dateObj.getFullYear(), dateObj.getMonth(), 1);
  var calendarInfo=__LoadXml("<calendar />");
  var paramNames=new Array();
  var paramVals=new Array();
  paramNames.push("seconds");
  paramVals.push(dateObj.getSeconds().toString());
  paramNames.push("minutes");
  paramVals.push(dateObj.getMinutes().toString());
  paramNames.push("hours");
  paramVals.push(dateObj.getHours().toString());
  paramNames.push("day");
  paramVals.push((monthStartObj.getDay()==0?6:monthStartObj.getDay()-1).toString());
  paramNames.push("date");
  paramVals.push(dateObj.getDate().toString());
  paramNames.push("month");
  paramVals.push(dateObj.getMonth().toString());
  paramNames.push("monthName");
  paramVals.push(monthName);
  paramNames.push("year");
  paramVals.push(dateObj.getFullYear().toString());
  paramNames.push("lastDay");
  paramVals.push(lastDay.toString());
  paramNames.push("prevLastDay");
  paramVals.push(prevLastDay.toString());
  paramNames.push("nextLastDay");
  paramVals.push(nextLastDay.toString());
  paramNames.push("currSelectedDay");
  paramVals.push(__CALENDAR_CURR_DATE.getDate().toString());
  paramNames.push("includeTime");
  paramVals.push(__CALENDAR_INCLUDE_TIME.toString());
  paramNames.push("mode");
  paramVals.push(mode);
  __Transform(document.getElementById("content_"+__CALENDAR_DLG.id),calendarInfo,__RequestPath+"/DesktopModules/OALUtils/XSLT/Calendar.xsl",paramNames,paramVals);
}
function __ValidateTimePeriod(ctlId,max)
{
  var val=__GetNumericValue(ctlId);
  if(val<0)
  {
    val=0;
  }
  else
  {
    if(val>max)
    {
      val=max;
    }
  }
  val=val.toString();
  if(val.length==1)
  {
    val="0"+val;
  }
  switch(ctlId)
  {
    case("HoursTextBox"):
      __CALENDAR_HOURS=val;
      break;
    case("MinutesTextBox"):
      __CALENDAR_MINUTES=val;
      break;
    default:
      __CALENDAR_SECONDS=val;
      break;
  }
  __SetValue(ctlId,val);
}
function __SetDate(date)
{
  var dateObj=__ParseDate(date);
  var dateText;
  if(__CALENDAR_TEMPLATE.indexOf("/")>-1)
  {
    // dd/mm/yyyy or similar.
    var templateParts=__CALENDAR_TEMPLATE.split("/");
    var dateParts;
    var timeParts;
    if(templateParts.indexOf(" ")>-1)
    {
      dateParts=templateParts.split(" ")[0].split("/");
      timeParts=templateParts.split(" ")[1].split(":");
    }
    else
    {
      dateParts=templateParts.split("/");
      timeParts=null;
    }
    if(templateParts[0].length==2)
    {
      dateText=(dateObj.getDate()<10?"0":"");
    }
    dateText+=dateObj.getDate().toString()+"/";
    if(templateParts[1].length==2)
    {
      dateText+=((dateObj.getMonth()+1)<10?"0":"");
    }
    dateText+=(dateObj.getMonth()+1).toString()+"/";
    var yearTemplate;
    if(templateParts[2].indexOf(" ")>-1)
    {
      yearTemplate=templateParts[2].split(" ")[0];
    }
    else
    {
      yearTemplate=templateParts[2];
    }
    if(yearTemplate.length==2)
    {
      dateText+=dateObj.getFullYear().toString().substr(2,2);
    }
    else
    {
      dateText+=dateObj.getFullYear().toString();
    }
    if(timeParts!=null)
    {
      dateText+=" "+(dateObj.getHours()<10?"0":"")+dateObj.getHours().toString()+":"+(dateObj.getMinutes()<10?"0":"")+dateObj.getMinutes().toString();
      if(timeParts.length>2)
      {
        dateText+=":"+(dateObj.getSeconds()<10?"0":"")+dateObj.getSeconds().toString();
      }
    }
  }
  else
  {
    var templateParts=__CALENDAR_TEMPLATE.split(" ");
    if(templateParts[0].length==2)
    {
      dateText=(dateObj.getDate()<10?"0":"");
    }
    dateText+=dateObj.getDate().toString()+" ";
    var m=dateObj.getMonth();
    if(templateParts[1].length==3)
    {
      dateText+=(m==0?"Jan":m==1?"Feb":m==2?"Mar":m==3?"Apr":m==4?"May":m==5?"Jun":m==6?"Jul":m==7?"Aug":m==8?"Sep":m==9?"Oct":m==10?"Nov":m==11?"Dec":"")+" ";
    }
    else
    {
      dateText+=(m==0?"January":m==1?"February":m==2?"March":m==3?"April":m==4?"May":m==5?"June":m==6?"July":m==7?"August":m==8?"September":m==9?"October":m==10?"November":m==11?"December":"")+" ";
    }
    if(templateParts[2].length==2)
    {
      dateText+=dateObj.getFullYear().toString().substr(2,2);
    }
    else
    {
      dateText+=dateObj.getFullYear().toString();
    }
    if(templateParts.length>3)
    {
      var timeParts=templateParts[3].split(":");
      dateText+=" "+(dateObj.getHours()<10?"0":"")+dateObj.getHours().toString()+":"+(dateObj.getMinutes()<10?"0":"")+dateObj.getMinutes().toString();
      if(timeParts.length>2)
      {
        dateText+=":"+(dateObj.getSeconds()<10?"0":"")+dateObj.getSeconds().toString();
      }
    }
  }
  document.getElementById(__CALENDAR_TARGET).value=dateText;
  __DestroyDialog(__CALENDAR_DLG);
}
function __ParseDate(date)
{
  var dateObj=new Date();
  try
  {
    var day;
    var month;
    var year;
    var date=date.toLowerCase();
    if(date.indexOf("/")>=1)
    {
      // dd/MM/yyyy or dd/MM/yy
      var dateParts;
      var timeParts;
      if(date.indexOf(" ")>-1)
      {
        dateParts=date.split(" ")[0].split("/");
        timeParts=date.split(" ")[1].split(":");
      }
      else
      {
        dateParts=date.split("/");
        timeParts=null;
      }
      day=dateParts[0]*1;
      month=(dateParts[1]*1)-1;
      if(dateParts[2].length>2)
      {
        year=dateParts[2]*1;
      }
      else
      {
        if((dateParts[2]*1)>=0&&(dateParts[2]*1)<=49)
        {
          year=(dateObj.getFullYear().toString().substr(0,2)+dateParts[2])*1;
        }
        else
        {
          year=((dateObj.getFullYear()-100).toString().substr(0,2)+dateParts[2])*1;
        }
      }
      if(timeParts!=null)
      {
        __CALENDAR_INCLUDE_TIME=true;
        if(__CALENDAR_HOURS==null)
        {
          __CALENDAR_HOURS=timeParts[0]*1;
          __CALENDAR_MINUTES=timeParts[1]*1;
          __CALENDAR_SECONDS=(timeParts.length>2?timeParts[2]*1:0);
        }
      }
    }
    else
    {
      // dd MMMM yyyy or dd MMM yyyy
      var dateParts=date.split(" ");
      day=dateParts[0]*1;
      var m=dateParts[1].substr(0,3);
      month=(m=="jan"?0:m=="feb"?1:m=="mar"?2:m=="apr"?3:m=="may"?4:m=="jun"?5:m=="jul"?6:m=="aug"?7:m=="sep"?8:m=="oct"?9:m=="nov"?10:m=="dec"?11:-1);
      if(dateParts[2].length>2)
      {
        year=dateParts[2]*1;
      }
      else
      {
        if((dateParts[2]*1)>=0&&(dateParts[2]*1)<=49)
        {
          year=(dateObj.getFullYear().toString().substr(0,2)+dateParts[2])*1;
        }
        else
        {
          year=((dateObj.getFullYear()-100).toString().substr(0,2)+dateParts[2])*1;
        }
      }
      if(dateParts.length>3)
      {
        __CALENDAR_INCLUDE_TIME=true;
        if(__CALENDAR_HOURS==null)
        {
          var timeParts=dateParts[3].split(":");
          __CALENDAR_HOURS=timeParts[0]*1;
          __CALENDAR_MINUTES=timeParts[1]*1;
          __CALENDAR_SECONDS=(timeParts.length>2?timeParts[2]*1:0);
        }
      }
    }
    dateObj.setFullYear(year,month,day);
    if(__CALENDAR_INCLUDE_TIME)
    {
      dateObj.setHours(__CALENDAR_HOURS,__CALENDAR_MINUTES,__CALENDAR_SECONDS);
    }
  }
  catch(e){}
  return(dateObj);
}

function __WindowResize()
{
  var backdrop=document.getElementById("__DialogBackdrop");  
  if(backdrop!=null&&backdrop.style.display=="block"){
    backdrop.style.width=(__ScrollWidth())+"px";
    backdrop.style.height=(__ScrollHeight())+"px";
  }
  for(var i=0;i<__dialogs.length;i++)
  {
    var dialog=document.getElementById(__dialogs[i]);      
    if(dialog!=null)
    {
      if(dialog.getAttribute("positioning")==null||dialog.getAttribute("positioning")!="IGNORE")
      {
        dialog.style.top=((__ClientHeight()-dialog.offsetHeight)/2+__ScrollTop())+"px";    
        dialog.style.left=((__ClientWidth()-dialog.offsetWidth)/2+__ScrollLeft())+"px";
      }      
    }
  } 
  __PositionWaitScreen();
}
function __CreateDialog(id,title,includeTitle)
{
  if(includeTitle==null)
  {
    includeTitle=true;
  }
  var dialog;
  try
  {
    var prefix=0;
    var newId=id;
    while(document.getElementById(newId)!=null)
    {
      prefix++;
      newId=id+prefix;
    }
    dialog=document.createElement("div");
    dialog.className="Dialog";
    var dialogInner=document.createElement("div");
    dialogInner.className="DialogInner";
    if(includeTitle)
    {
      var titleBar=document.createElement("div");
      var titleSpan=document.createElement("span");
      titleSpan.id="title_"+newId;
      titleSpan.className="TitleBar";
      titleSpan.innerHTML=title;
    }
    var content=document.createElement("div");
    content.id="content_"+newId;
    content.className="ContentContainer Form";
    if(includeTitle)
    {
      titleBar.appendChild(titleSpan);
      dialogInner.appendChild(titleBar);
    }
    dialogInner.appendChild(content);
    dialog.appendChild(dialogInner);    
    dialog.id=newId;
    document.body.appendChild(dialog);
  }
  catch(e)
  {
    dialog=null;
  }
  return(dialog);
}
function __DestroyDialog(dialog)
{
  if(dialog!=null)
  {
    if(dialog.style.visibility=="visible")
    {
      __HideDialog(dialog);
    }
    if(dialog.parentNode!=null)
    {
      dialog.parentNode.removeChild(dialog);
      dialog.innerHTML="";
    }
    dialog=null;
  }
}
function __ShowDialog(dialog)
{
  if(dialog!=null&&(dialog.style.visibility=="hidden"||dialog.style.visibility==""))
  {
    try
    {
      __dialogs.push(dialog.id);
      if(dialog.parentNode==null)
      {
        document.body.appendChild(dialog);
      }            
      dialog.style.visibility="visible";
      dialog.style.zIndex=500+__dialogs.length;      
      __CreateBackdrop();
    }
    catch(e){}
  }
}
function __CreateBackdrop()
{
  var backdrop=document.getElementById("__DialogBackdrop");
  if(backdrop==null)
  {
    backdrop=document.createElement("div");
    backdrop.id="__DialogBackdrop";
    backdrop.className="ScreenCover";
    document.body.appendChild(backdrop);
  }
  backdrop.style.display="block";
  backdrop.style.zIndex=499+__dialogs.length;
  __WindowResize();
}
function __HideDialog(dialog)
{
  if(dialog!=null)
  { 
    dialog.style.visibility="hidden";
    __dialogs.pop(dialog.id);
    var backdrop=document.getElementById("__DialogBackdrop")
    if(backdrop!=null)
    {
      if(__dialogs.length==0)
      {
        backdrop.style.display="none";
      }
      else
      {
        backdrop.style.zIndex=499+__dialogs.length;
      }
    }
  }
}
function __ScrollHeight() {	
  if(document.body){
    return __FilterResults (
      0,
	    0,
	    document.body ? document.body.scrollHeight : 0
	  );
  }
  else{
    return __FilterResults (
      0,
	    document.documentElement ? document.documentElement.scrollHeight : 0,
	    document.body ? document.body.scrollHeight : 0
	  );
  }		
}
function __ScrollWidth() {	
  if(document.body){
    return __FilterResults (
      0,
	    0,
	    document.body ? document.body.scrollWidth : 0
	  );
  }
  else{
    return __FilterResults (
      0,
	    document.documentElement ? document.documentElement.scrollWidth : 0,
	    document.body ? document.body.scrollWidth : 0
	  );
  }		
}
function __ClientHeight() {
	return __FilterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function __ClientWidth() {
	return __FilterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function __ScrollTop() {
	return __FilterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function __ScrollLeft() {
	return __FilterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function __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;
}
function __FindPosX(obj)
{
  var curleft = 0;
  if(obj.offsetParent)
  {
    while(1)
    {
      curleft += obj.offsetLeft;
      if(!obj.offsetParent)
      {
        break;
      }
      obj = obj.offsetParent;
    }
  }  
  else
  {
    if(obj.x)
    {
      curleft += obj.x;
    }
  }   
  return curleft;
}
function __FindPosY(obj)
{
  var curtop = 0;
  if(obj.offsetParent)
  {
    while(1)
    {
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
      {
        break;
      }
      obj = obj.offsetParent;
    }
  }
  else
  {
    if(obj.y)
    {
      curtop += obj.y;
    }
  }
  return curtop;
}

function getXY(obj)
{
  var curleft = 0;
  var curtop = obj.offsetHeight + 5;
  var border;
  if (obj.offsetParent)
  {
    do
    {
      // XXX: If the element is position: relative we have to add borderWidth
      if (getStyle(obj, 'position') == 'relative')
      {
        if (border = _pub.getStyle(obj, 'border-top-width')) curtop += parseInt(border);
        if (border = _pub.getStyle(obj, 'border-left-width')) curleft += parseInt(border);
      }
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    }
    while (obj = obj.offsetParent)
  }
  else if (obj.x)
  {
    curleft += obj.x;
    curtop += obj.y;
  }
  return {'x': curleft, 'y': curtop};
}
function getStyle(obj, styleProp)
{
  if (obj.currentStyle)
    return obj.currentStyle[styleProp];
  else if (window.getComputedStyle)
    return document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleProp);
}
