/*                All content copyright © 2005,2006 Tatler Software.                   */
/*                                                                                     */ 
/*     Permission granted to use as an input control on Customer's web site when       */
/*     implemented in conjunction with QLR Manager report objects.                     */

/* The arguments passed to openCalendar() from the button or HREF used to launch it:   */
/*                                                                                     */          
/* event  = Used to capture screen position of image or button clicked to position     */
/*          popup for Netscape 4.x. Alway use event (without quotes).                  */
/* layer  = Layer id for Netscape 4.x if form inside a layer. If no layer, use an      */
/*          "" (empty string) or 0 as the argument.                                    */
/* form   = The form name containing the date input element.                           */
/* field  = The name of the element the popup calendar should read an initial value    */
/*          from (if present) or update after a date selection.                        */
/* format = The date format to read from the text input and apply when selected from   */
/*          the popup calendar.                                                        */
/*          - mmddyy                                                                   */
/*          - mmddyyyy                                                                 */ 
/*          - ddmmyy                                                                   */
/*          - ddmmyyyy                                                                 */
/*          - yyyymmdd                                                                 */ 
/* delim  = This is the expected separator used when reading the date value from       */
/*          the text input and applying the date, eg. "/","-",".", etc. It must be     */
/*          a character that is not a letter or number. If no delimiter is desired,    */
/*          use an "" (empty string) or 0 as the argument.                             */
/* title  = The value displayed in the title bar to associate the calendar with a      */
/*          specific input field. If no value is provided, the input field name will   */
/*          be used with "_" replaced with a space.                                    */
/* footer = The footer is the area beneath the calendar table that displays the date   */
/*          format and "Today" button. Any value for this argument, 1, "yes", etc.     */
/*          will display the footer. Use "" (empty string) or 0 to hide the footer.    */
/* nopad  = Any value for this argument, 1,"yes", etc. will prevent single digit days  */
/*          and months from being padded with a leading 0.  For example, 1/1/2005      */
/*          will be written to the text input instead of the default 01/01/2005.       */ 
/* copy   = Copy date value into another form element (usually a hidden element) when  */
/*          applied. This argument specifies the target form element as                */
/*          "document.form.element" and must be enclosed in quotes. For example,       */
/*          "document.subForm.start_date" may reference a hidden form element.         */
/* asdb   = Copy the displayed date value to the element defined with the "copy"       */
/*          argument in the default database format of yyyy-dd-mm. Any value for this  */
/*          argument, 1,"yes", etc. will copy the date in database format. Use ""      */
/*            (empty string) or 0 to copy the date as it is displayed.                 */ 
/*                                                                                     */
/* NOTE:  The above instructions can be removed from the production version to reduce  */
/*        download time.                                                               */

var calendars=[];

function openCalendar(event,layer,form,field,format,delim,title,footer,nopad,copy,asdb){
  var ok=document.getElementById||document.all||document.layers;
  if(!ok)return errMsg("Sorry, your browser is not capable of using this popup calendar.\nPlease consider a browser upgrade or enter the date manually.");
  var c,p,x,y,w,h,s,cd,el,id,dt,tt,fm,dl,dt,dd,mm,yy,sp;
  el=(layer)?document.layers[layer].document.forms[form].elements[field]:document.forms[form].elements[field];
  if(!el)return errMsg("The form and field arguments do not\nreference a valid text input element.");
  id=field.replace(/[^\w]/g,"").substr(0,30);
  tt=(title)?title:field.replace(/r000/i,'').replace(/_/g,' ');
  s=(el.value)?el.value.replace(/^\s+/,'').replace(/\s+$/,''):"";
  dl=delim;
  cd=new Date();
  if(s){
    fm=format.toLowerCase();
    sp=(s.match(/\D/));
    dt=getDateStr(s,fm,sp);
    dd=(dt&&dt.dd)?dt.dd:0;
    mm=(dt&&dt.mm)?dt.mm:0;
    yy=(dt)?(dt.yyyy)?(dt.yyyy.length<4)?padYear(dt.yyyy):dt.yyyy:(dt.yy)?(dt.yy.length<4)?padYear(dt.yy):dt.yy:0:0;
    if(dt&&dd&&mm&&yy){
      cd.setFullYear(yy);
      cd.setMonth(dt.mm-1);
      cd.setDate(dt.dd);
    }
  }
  c=calendars[id];
  p=event.target;
  x=(document.layers)?(event&&event.screenX)?event.screenX-150:100:null;
  y=(document.layers&&event&&event.screenY);
  y=(y)?(screen.height)?(screen.height&&(y>screen.height/2))?y-240:y+20:50:null;
  w=(c)?c.w:190;
  h=(c)?c.h:(footer&&footer!=0)?188:167;
  dt=cd.valueOf();
  calendar={p:p,x:x,y:y,w:w,h:h,dt:dt,el:el,id:id,tt:tt,ft:footer,fm:format,dl:dl,np:nopad,cp:copy,db:asdb,get:getDateStr,set:setDateStr,win:calWindow,pos:setPos};
  calendars[id]=calendar;
  calWindow(calendar);
}

function getDateStr(s,fm,dl,dt,id,np){
  var obj=new Object();
  if(dt){
    if(id)calendars[id].dt=dt;
    dt=new Date(+dt);
    var yy=dt.getFullYear();
    yy=(fm.indexOf("yyyy")==-1)?yy.toString().substr(2):yy;
    var mm=dt.getMonth()+1;
    mm=(mm<10&&!np)?'0'+mm:mm;
    var dd=dt.getDate();
    dd=(dd<10&&!np)?'0'+dd:dd;
    fm=fm.replace(/(mm+)/,mm+dl).replace(/(dd+)/,dd+dl).replace(/(yy+)/,yy+dl);
    return fm.substr(0,fm.lastIndexOf(dl));
  }
  fm=fm.replace(/(mm+)/,'$1'+dl).replace(/(dd+)/,'$1'+dl).replace(/(yy+)/,'$1'+dl);
  fm=fm.split(dl,3);
  if(dl){
    s=s.split(dl);
    for(i in fm)obj[fm[i]]=s[i];
  }
  else for(i in fm){
    obj[fm[i]]=s.substr(0,fm[i].length);
    s=s.substr(fm[i].length);
  }
  return obj;
}

function setDateStr(id){
  var cal=calendars[id];
  var hid=eval(cal.cp);
  cal.el.value=getDateStr(0,cal.fm,cal.dl,cal.dt,0,cal.np);
  if(cal.cp){
    if(hid&&cal.db)hid.value=getDateStr(0,"yyyymmdd","-",cal.dt);
    else hid.value=cal.el.value;
   }
 }
 
function errMsg(msg){
  alert(msg);
  return false;
}
 
function getPos(el){
  var offset=new Array(0,0);
  while(el){
    offset[0]+=el.offsetLeft;
    offset[1]+=el.offsetTop;
    el=el.offsetParent;
  }
  return offset;
}
 
function setPos(cal){
  if(cal){
    var xy=getPos(calFrm);
    cal.x=xy[0]+"px";
    cal.y=xy[1]+"px";
  }
}
 
function padYear(yr){
  while(yr.length<3)yr="0"+yr;return "2"+yr;
}

function calWindowORIG(cal){
  if(document.layers)window["calWin"+cal.id]=window.open(cal.p,'calWin'+cal.id,'width='+cal.w+',height='+cal.h+',status=0,resizable=1,screenX='+cal.x+',screenY='+cal.y+',left='+cal.x+',top='+cal.y);
  else{
    var calWin=frames["calWin"];
    if(!calWin||(calWin&&!calWin.calRdy))return;
    var xy=getPos(cal.el);
    calFrm=(document.getElementById)?document.getElementById("calWin"):(document.all)?document.all["calWin"]:0;
    if(calFrm&&calWin.initCal){
      calFrm.style.left=(cal&&cal.x)?cal.x:(xy[0])?xy[0]+"px":"200px";
      calFrm.style.top=(cal&&cal.y)?cal.y:(xy[1])?xy[1]+22+"px":"200px";
      calFrm.style.visibility="visible";
      setTimeout("calWin.initCal('"+cal.id+"')",1);
    }
  }
  return false;
}

function getScroll() {
  var pge = (window.pageYOffset || window.pageYOffset==0);
  var bdy = (document.body || document.documentElement);
  var xy = (pge) ? { x:window.pageXOffset,y:window.pageYOffset } : (bdy) ? { x:bdy.scrollLeft,y:bdy.scrollTop } : 0;
  return xy;
}

function calWindow(cal){
   if(document.layers)window["calWin"+cal.id]=window.open(cal.p,'calWin'+cal.id,'width='+cal.w+',height='+cal.h+',status=0,resizable=1,screenX='+cal.x+',screenY='+cal.y+',left='+cal.x+',top='+cal.y);
   else{
     var calWin=frames["calWin"];
     if(!calWin||(calWin&&!calWin.calRdy))return;
     var xy=getPos(cal.el);
     calFrm=(document.getElementById)?document.getElementById("calWin"):(document.all)?document.all["calWin"]:0;
     if(calFrm&&calWin.initCal){
       var bdy=(document.body || document.documentElement);
       var hgt=(window.innerHeight || (bdy&&bdy.offsetHeight));
       var pos=(hgt&&xy[1]);
       var flp=(pos&&(xy[1]-getScroll().y+cal.el.offsetHeight+209>hgt));
       var pgY=(pos)?(flp)?xy[1]-209:xy[1]+cal.el.offsetHeight+1:200;
       calFrm.style.left=(cal&&cal.x)?cal.x:(xy[0])?xy[0]+"px":"200px";
       calFrm.style.top=(cal&&cal.y)?cal.y:pgY+"px";
       calFrm.style.visibility="visible";
       setTimeout("calWin.initCal('"+cal.id+"')",1);
     }
   }
   return false;
 }
