function get_firstchild(n) {
  x=n.firstChild;
  while (x.nodeType!=1) {
    x=x.nextSibling;
  }
  return x;
}

var oldMenu  = 0;
var currSrc  = 0;
var currMenu = 0;
var currTip  = 0;
function menuShow(menu, src) {
  if (menu!=currMenu) {menuHide(oldMenu)};
  if (menu!=0 && !window.document.all) {menu = window.document.getElementById(menu)};
  if (menu!=0 && window.document.all) {menu = window.document.all(menu)};
  if (!window.document.all) {src = window.document.getElementById(src)};
  if (window.document.all) {src = window.document.all(src)};
  if (menu!=0 && menu!=currMenu) {
    menu.style.visibility='visible';
    menu.style.display='block';
    menu.style.width='auto';
    src.className='mDropped';
    currSrc=src;
    src2=src.offsetParent;
    if (src2!=null) {
      menu.style.top=screen_Top(src2)+src2.clientHeight;
      menu.style.left=screen_Left(src2);
    }
    if (navigator.userAgent.indexOf("Mac") != -1 &&
        typeof document.body.topMargin != "undefined") {
      menu.style.top=screen_Top(src)+14;
    }
    if (getcookie()=="") {
      menu.style.top=$('mainMenu').cumulativeOffset()[1]+30;
      menu.style.left=screen_Left(src2)+6;
    }
    if ( (getcookie()=='"/css2/lowres.css"') || (getcookie()=='"/css2/projection.css"') || (navigator.userAgent.indexOf("MSIE 5") != -1) || (navigator.userAgent.indexOf("MSIE 6") != -1) )  {
      menu.style.top=$('mainMenu').cumulativeOffset()[1]+28;
      menu.style.left=screen_Left(src2)+6;
    }
    if (Get_Cookie('smallHeader')) {
      menu.style.top=$('mainMenu').cumulativeOffset()[1]+28;
    }
    if (menu.offsetLeft+menu.clientWidth>winWidth()) {
       menu.style.left=(winWidth()-menu.clientWidth-18)+'px';
       menu.firstChild.style.textAlign='right';
       menu.className=menu.className+' right';
    }
    if (!window.document.all) {SubMenu=get_firstchild(window.document.getElementById('mainSubMenu'))};
    if (window.document.all) {SubMenu=get_firstchild(window.document.all('mainSubMenu'))};
    /*SubMenu.style.visibility='hidden';*/
    if (!window.document.all) {MainMenu=get_firstchild(window.document.getElementById('mainMenu'))};
    if (window.document.all) {MainMenu=get_firstchild(window.document.all('mainMenu'))};
    /*MainMenu.className=MainMenu.className+' down';*/
    oldMenu = menu;
    currMenu = menu;
  }
}
function menuHide(menu) {
  if (menu!=0) {menu.style.visibility='hidden'; currSrc.className='mlink'; currMenu=0; currSrc=0};
  if (menu!=0 && !window.document.all) {SubMenu=get_firstchild(window.document.getElementById('mainSubMenu'))};
  if (menu!=0 && window.document.all) {SubMenu=get_firstchild(window.document.all('mainSubMenu'))};
  if (menu!=0) {SubMenu.style.visibility='visible'};
  if (menu!=0 && !window.document.all) {MainMenu=get_firstchild(window.document.getElementById('mainMenu'))};
  if (menu!=0 && window.document.all) {MainMenu=get_firstchild(window.document.all('mainMenu'))};
  if (menu!=0) {MainMenu.className=MainMenu.className.replace(' down','')};
}
function screen_Left(obj) {
  var res = 0;
  while ( (obj!=null) && (obj.tagName!='BODY') ) {
    res += obj.offsetLeft;
    obj = obj.offsetParent;
  }
  return res; 
}
function screen_Top(obj) {
  var res = 0;
  while ( (obj!=null) && (obj.tagName!='BODY') ) {
    res += obj.offsetTop;
    obj = obj.offsetParent;
  }
  return res;
}
function min(a,b) {
  if (a<b) {return a;};
  return b;
}
function tipShow(src) {
  if (currTip != src) {
    if (!window.document.all) {tip=window.document.getElementById('tipPopUp')};
    if (window.document.all) {tip=window.document.all('tipPopUp')};
    if (src.title=='') {src.title=src.tipText}
    else {src.tipText = src.title};
    tip.innerHTML='<div class=tipInner>'+src.title+'</div>';
    src.title='';
    tip.style.top=(screen_Top(src)-tip.firstChild.clientHeight)+'px'; 
    tip.style.left=min(screen_Left(src)+24, document.body.offsetWidth-130)+'px';
    tip.style.display='block';
    currTip = src;
  }
}
function tipHide() {
  if (!window.document.all) {tip=window.document.getElementById('tipPopUp')};
  if (window.document.all) {tip=window.document.all('tipPopUp')};
  tip.style.display='none';
  currTip = 0;
}
function disable_button(button, msg) {
    button.title = 'Please wait... (If not responding, refresh page to reset)';
    button.value = msg;
    button.innerHTML = msg;
    button.dynamicDisabled = 1;
    button.disabled = 1;
    if (window.addEventListener) {window.addEventListener('pageshow', pageShowHandler, false)};
    if (navigator.userAgent.indexOf("MSIE 9") == -1) {button.form.submit()};
}
function disable_link(link) {
    href = link.href;
    link.title = 'Please wait... (If not responding, refresh page to reset)';
    window.location.href = href;
    link.href = 'javascript:void(0)';
    link.style.color = 'silver';
    link.style.cursor = 'progress';
}
function pageShowHandler(event) {
    if (event.persisted) {
        var arrElements = document.getElementsByTagName('button');
        for (var i=0; i<arrElements.length;i++) {
            var btn = arrElements[i];
            if (btn.dynamicDisabled && btn.dynamicDisabled==1) {
                btn.dynamicDisabled=0;
                btn.disabled = false;
                btn.title = '';
                btn.value = 'Save Changes';
                btn.innerHTML = 'Save Changes';
            }                
        }
        window.removeEventListener('pageshow',pageShowHandler,false);
    }
}
var X = new RegExp('X', 'g');
function payrollMenu(id, org) {
s = "<a id=open class='slink noWrap' href='X/payroll/open' accesskey='1'><u>1</u>.&nbsp;Open</a><br> <a id=enter class='slink noWrap' href='X/payroll/enter' accesskey='2'><u>2</u>.&nbsp;Enter</a><br> <a id=pay_reports class='slink noWrap' href='X/payroll/pay_reports' accesskey='3'><u>3</u>.&nbsp;Print</a><br> <a id=confirm class='slink noWrap' href='X/payroll/confirm' accesskey=''4'><u>4</u>.&nbsp;Confirm</a>";
return s.replace(X, org);
}
function peopleMenu(id, org) {
s = "<a id=active class=slink href='X/people?view=Active'>View&nbsp;Current&nbsp;People</a><br> <a id=all class=slink href='X/people?view=all'>All&nbsp;People</a><br> <a id=inactive class=slink href='X/people?view=Inactive'>Inactive&nbsp;People</a><br> <a id=requests class=slink href='X/people?view=requests'>Leave&nbsp;Requests</a><br> <a id=add class=slink href='X/people/addPerson'>Add&nbsp;a&nbsp;New&nbsp;Person</a>";
return s.replace(X, org);
}
function taxMenu(id, org) {
s = "<a id=options class=slink href='X/tax/'>Tax&nbsp;Options</a><br> <a id=ir345 class=slink href='X/tax/ir345'>Remittance</a><br> <a id=ked class=slink href='X/tax/ked'>KiwiSaver</a><br> <a id=ir348 class=slink href='X/tax/ir348'>Monthly&nbsp;Schedule</a><br> <a id=tax_history class=slink href='X/tax/history'>History</a><br> <a id=openBal class=slink href='X/tax/openingBalances'>Opening&nbsp;Balances</a><br>";
return s.replace(X, org);
}
function taxMenu_au(id, org) {
s = "<a id=options class=slink href='X/tax/'>Tax&nbsp;Options</a><br> <a id=iactivity class=slink href='X/tax/activity'>Activity&nbsp;Statement</a><br> <a id=tax_history class=slink href='X/tax/history'>History</a><br> <a id=openBal class=slink href='X/tax/openingBalances'>Opening&nbsp;Balances</a><br>";
return s.replace(X, org);
}
function reportMenu(id, org) {
s = "<a id=financial_rpt class=slink href='X/payroll/report/?category=financial'>Financial</a><br> <a id=history_rpt class=slink href='X/payroll/report/?category=history'>History</a><br> <a id=leave_rpt class=slink href='X/payroll/report/?category=leave'>Leave</a><br> <a id=payroll_rpt class=slink href='X/payroll/report/?category=payroll'>Payroll</a><br> <a id=personal_rpt class=slink href='X/payroll/report/?category=personal'>Personal</a><br> <a id=setup_rpt class=slink href='X/payroll/report/?category=setup'>Setup</a><br> <a id=tax_rpt class=slink href='X/payroll/report/?category=tax'>Tax</a><br class=notFullscreen> <a id=library class='slink notFullscreen' href='X/payroll/report/'>All&nbsp;Reports</a><br class=download_centre> <a id=download_centre class='slink download_centre' href='X/payroll/report/download'>Downloads</a>";
return s.replace(X, org);
}
function setupMenu(id, org) {
s = "<a id=organisation_html class=slink href='X/setup/'>Organisation</a><br> <a id=password class=slink href='X/setup/password'>Password</a><br> <a id=element class=slink href='X/setup/payment'>Pay&nbsp;Elements</a><br> <a id=cost_centre class='slink noWrap' href='X/setup/cost_centre'>Costing/GL</a><br> <a id=userfield class=slink href='X/setup/userfield'>User&nbsp;Field</a><br> <a id=reportingGroup class=slink href='X/setup/reportingGroup'>Reporting</a><br> <a id=interfaces class=slink href='X/setup/interfaces'>Interfaces</a><br>";
return s.replace(X, org);
}
function helpMenu(id, org) {
t = "<a class='slink pointer' id=hideTips1 href='javascript:{hideTips()}'>Hide Tips</a>";
t = t + "<a class='slink showTips' id=showTips1 href='javascript:{showTips()}'>Show Tips</a>";
s = "<a class=slink id=howTo href='X/help/how'>How&nbsp;do&nbsp;I?...</a><br> <a class=slink id=h_leave href='X/help/payroll/timesheet/leave'>Record&nbsp;Leave</a><br> <a class=slink id=h_enter href='X/help/payroll/process/enter'>Alter&nbsp;Timesheets</a><br> <a class=slink id=h_finish href='X/help/people/finish'>Process&nbsp;Final&nbsp;Pays</a><br> <a class=slink id=h_people href='X/help/people/'>Add/Alter&nbsp;People</a><br> <a class=slink id=h_process href='X/help/payroll/process/'>Run&nbsp;Pays</a><br> <a class=slink id=h_helpdesk href='X/help/company/helpdesk'>Contact&nbsp;Us</a> "+t+"<br class=notFullscreen> <strong><a class=slink id=help3 href='X/help/'>Get&nbsp;More&nbsp;<u>H</u>elp</a></strong><br>";
return s.replace(X, org);
}
function kH(e) {
var pK = e? e.which: window.event.keyCode;
if (pK != 33) {return pK};
q=document.getElementById('q');
q.focus(); 
q.value='! '
if (q.setSelectionRange) {q.setSelectionRange(2,2)};
return false;
}
document.onkeypress = kH;
if (document.layers) document.captureEvents(Event.KEYPRESS);

function getcookie() {
 var cookiestring = "" + document.cookie;
 var index1       = cookiestring.indexOf('selected_stylesheet');
 if (index1==-1) return ""; 
 var index2       = cookiestring.indexOf(';',index1);
 if (index2==-1) index2 = cookiestring.length; 
 return unescape(cookiestring.substring(index1 + 'selected_stylesheet'.length+1, index2));
}
function winWidth() { 
    var moz = (document.getElementById && !document.all) ? 1 : 0;
    return (moz) ? window.innerWidth : document.body.clientWidth; 
}  
function winHeight() { 
    var moz = (document.getElementById && !document.all) ? 1 : 0;
    return (moz) ? window.innerHeight : ( (document.documentElement.clientHeight)?document.documentElement.clientHeight:document.body.clientHeight ); 
}  
function set_stylesheet() {
  if ((navigator.userAgent.indexOf("iPhone") != -1) || (navigator.userAgent.indexOf("iPod") != -1) || (navigator.userAgent.indexOf("iPad") != -1) || (navigator.userAgent.indexOf("Kindle") != -1)) {
      return;
  }
  if ( (navigator.userAgent.indexOf("MSIE 5") != -1) || (navigator.userAgent.indexOf("MSIE 6") != -1) )  {
      return;
  }
  if ( (winWidth()<640) && (winWidth()>1) ) {
    if (getcookie()!='"/css2/handheld.css"') {
      window.location.href='/css/set_handheld?referer='+escape(window.location.href);
    }
    return;
  } 
  if ( (winHeight()<600) && (winHeight()>1) ) {
    if (getcookie()!='"/css2/lowres.css"') {
      window.location.href='/css/set_lowres?referer='+escape(window.location.href);
    }
    return;
  }
  if ( (winHeight()>=620) && (getcookie()!="") ) {
    window.location.href='/css/set_standard?referer='+escape(window.location.href);
    return;
  }
}
window.onresize = set_stylesheet;

function dateKeyPress(field, event) {
  if(window.event) { keynum = event.keyCode}
  else if(event.which) { keynum = event.which }
  if (field.value=="") {date = Date.today() }
  else {date = Date.parse(field.value)}
  if (keynum==40 && event.shiftKey) {
    field.value = date.add({months:1}).toString('dd-MMM-yyyy');
  }
  else if (keynum==38 && event.shiftKey) {
    field.value = date.add({months:-1}).toString('dd-MMM-yyyy');
  }
  else if (keynum==40) {
    field.value = date.add({days:1}).toString('dd-MMM-yyyy');
  }
  else if (keynum==38) {
    field.value = date.add({days:-1}).toString('dd-MMM-yyyy');
  }
}

function tag(t) {return "<"+t+">"+this+"</"+t+">";}
function tag(t,nam,val) 
  {return ("<"+t+">"+this+"</"+t+">").replace(/<([^>]*)([^$]*)/i, "<$1 "+nam+"='"+val+"'$2");}
function att(nam, val) 
  {return this.replace(/<([^>]*)([^$]*)/i, "<$1 "+nam+"='"+val+"'$2");}
var monthName = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
function isToday() {
  with (new Date())
    return (this.getFullYear()==getFullYear()) && 
     (this.getMonth()==getMonth()) && 
     (this.getDate()==getDate());
}
String.prototype.tag=tag;
Number.prototype.tag=tag;
String.prototype.att=att;
Number.prototype.att=att;
Date.prototype.isToday=isToday;
function formatDay(yy, mm, dd, fld) {
  with (new Date(yy,mm,dd)) {
    var result = getDate().toString().link("javascript:{setField('"+fld+"','"+dd+'-'+monthName[mm]+'-'+yy+"')}").tag("span class=day", "id", "d"+yy+"-"+(mm+1)+"-"+dd).tag("TD", "style", "font-size:smaller");
    if (isToday())
      result = getDate().toString().link("javascript:{setField('"+fld+"','"+dd+'-'+monthName[mm]+'-'+yy+"')}").tag("span title='Today'", "class", "today").tag("TD", "style", "font-size:smaller");
    else if ((getDay()==0) || (getDay()==6))
      result = getDate().toString().link("javascript:{setField('"+fld+"','"+dd+'-'+monthName[mm]+'-'+yy+"')}").tag("span class='day weekend'", "id", "d"+yy+"-"+(mm+1)+"-"+dd).tag("TD class=weekend", "style", "font-size:smaller");
    if (getDay()==6)
      result += "<TR align='right'>";
  }
  return result;
}
function setField(fld, val) {
  if (!window.document.all){fld = window.document.getElementById(fld)};
  if (window.document.all) {fld = window.document.all(fld)};
  fld.value=val;
  fld.onchange();
}
function gotoToday(tag, fld) {
  if (typeof(fld)=='undefined') {fld='date'}
  with (new Date())
    calendar(tag, getFullYear(), getMonth(), fld);
}
function calendar(tag, year, mth, fld) {
try {
  first = new Date(year, mth, 1);
  year = first.getFullYear();
  mth = first.getMonth();
  var day = first.getDay();
  var numDays = new Date(year, mth+1, 0).getDate();
  var hdr = "<".link("javascript:calendar(document.getElementById('"+tag.id+"'),"+year+","+(mth-1)+",'"+fld+"')").att("title","Previous Month").tag("TH class=cal");
  hdr+= (monthName[mth]+" "+year).tag("TH class=cal","colspan","5").att("style","text-align:center");
  hdr+= ">".link("javascript:calendar(document.getElementById('"+tag.id+"'),"+year+","+(mth+1)+",'"+fld+"')").att("title","Next Month").tag("TH class=cal");
  var wk = "<TH class='cal weekend'>Su<TH class=cal>Mo<TH class=cal>Tu<TH class=cal>We<TH class=cal>Th<TH class=cal>Fr<TH class='cal weekend'>Sa";
  var bdy = "";
  for (i = 0; i++ < day; bdy += "<TD>");
  for (i = 0; i++ < numDays; bdy += formatDay(year, mth, i, fld)); 
  tag.innerHTML = (hdr.tag("TR") + wk.tag("TR","align","right") + bdy.tag("TR","align","right")).tag("table","class","calendar").att("cellspacing","0");
  tag.the_year = year;
  tag.the_month = mth;
}
catch (e) {return;}
}
function Set_Cookie( name, value, expires, path, domain, secure ) {
var today = new Date();
today.setTime( today.getTime() );
if ( expires ) {
  expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name + "=" +escape( value ) +
 ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
 ( ( path ) ? ";path=" + path : "" ) + 
 ( ( domain ) ? ";domain=" + domain : "" ) +
 ( ( secure ) ? ";secure" : "" );
}

function Delete_Cookie( name, path, domain ) {
  if ( Get_Cookie( name ) ) 
    document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function Get_Cookie( check_name ) {
var a_all_cookies = document.cookie.split( ';' );
var a_temp_cookie = '';
var cookie_name = '';
var cookie_value = '';
var b_cookie_found = false; 
for ( i = 0; i < a_all_cookies.length; i++ ) {
  a_temp_cookie = a_all_cookies[i].split( '=' );
  cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
  if ( cookie_name == check_name ) {
    b_cookie_found = true;
    if ( a_temp_cookie.length > 1 ) {
        cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
    }
    return cookie_value;
    break;
  }
  a_temp_cookie = null;
  cookie_name = '';
}
if ( !b_cookie_found ) {
  return null;
}
}
function hideTips() {
  menuShow(0);
  Effect.multiple($$('.circle'), Effect.Fade);
  Effect.multiple($$('.pointer'), Effect.Fade);
  Effect.multiple($$('.tipBox'), Effect.Fade); 
  $('hideTips1').style.display = 'none';
  $('showTips1').style.display = 'inline';
  Set_Cookie('hideTips', '1', 180, '/', '', '');
}
function showTips() {
  Delete_Cookie('hideTips', '/', '');
  var today = new Date();
  today.setTime( today.getTime() );
  window.document.location.search='showTips='+today.getTime();
}
function switchHeader() {
  if ( Get_Cookie('smallHeader') ) {
    Delete_Cookie('smallHeader', '/', '');
  }
  else {
    Set_Cookie('smallHeader', '1', 9999, '/', '', 0);
  }
  var today = new Date();
  today.setTime( today.getTime() );
  window.document.location.search='header='+today.getTime();
}

