;var NetTalkVersion = 6.14;
// ---------------------------------------------

var cnt=0;
var tcnt=0;
var fcnt='';
var icnt='';
function countDown(){
  hh = parseInt( cnt / 3600 );
  mm = parseInt( cnt / 60 ) % 60;
  ss = cnt % 60;
    var t = hh + ":" + (mm < 10 ? "0" + mm : mm) + ":" + (ss < 10 ? "0" + ss : ss);
  jQuery('#' + icnt).html(t);
  cnt -= 1;
  if (cnt ==0){
    window.open(fcnt,'_top');
  } else {
        setTimeout("countDown();",1000);  
  }
};

function resetCountDown(){
  cnt = tcnt;
}

function startCountDown(t,f,i){
  if (t){
        tcnt = t;
    }   
    if (f){
        fcnt = f;
    }
    if (i){
        icnt = i;
    }
    cnt = tcnt;
  countDown();
};

function versionCheck(v){
  var s = v + '';
  s = s.replace('.','');
  v = Number(v);
  if (v != NetTalkVersion){
    jQuery('#_ver' + s).html('UPDATE OF WEB FOLDER REQUIRED - Try pressing Ctrl-F5. Server is on version ' + v + ' but web folder is on version ' +  NetTalkVersion);
  } else {
        jQuery('#_ver' + s).hide();
    }   
}
// ---------------------------------------------
// functions to handle busy graphic
var busyCounter=0;
jQuery(function(){
        busyCounter = 0;
        jQuery("#_busy").hide();
});

jQuery("html").bind("ajaxSend",function(){
        //jQuery("#_busy").position({my: "center", at:"center", of: window});
        jQuery("#_busy").show();
        busyCounter += 1;
        if (jQuery.browser.msie) {
                jQuery('input:radio, input:checkbox').unbind('click.iefix');
        };
});
jQuery("html").bind("ajaxComplete",function(){;
        busyCounter -= 1;
        //if (busyCounter == 0){
                jQuery("#_busy").hide();
        //};
        if (jQuery.browser.msie) {
                jQuery('input:radio, input:checkbox').bind('click.iefix',function () {
                        this.blur();
                        this.focus();
                });
        }
        
});
// ---------------------------------------------
function ntAlert(m){
  setTimeout(function() { 
        var a = jQuery(":focus");
        alert(unescape(m)); 
         jQuery(a).focus();
    }, 1);
}
// ---------------------------------------------
var hadfocus='';
var setfocus='';
function afterSv(){
  GreenAll();
  applyHTML5();
  var d = document.getElementById('alert_div');
  if (d != null){
    var i = d.innerHTML;
    var r = /(<([^>]+)>)/gi;
    i = i.replace(r, "");
    if ((i != '') && (i != '\n') && (i != '\n\n') && (i != '<br />')){
      var thissound=document.getElementById('soundfail');
      if (thissound != null){
        thissound.Play();
      }
    } else {
      var thissound=document.getElementById('soundok');
      if (thissound != null){
        thissound.Play();
      }
    }
  }
}

var tables = [];
function GreenAll(){
  for(var e = 0; e < tables.length; e++){
     tables[e].table=document.getElementById(tables[e].tid); // necessary after ajax call
     if (tables[e].table != null){
       tables[e].parseCell();
       tables[e].applyGreenBar();
     }
     tables[e].makeResizable();
     tables[e].prepColumns();
     tables[e].bind();
     //tables[e].restoreFocus();
  }
}
// -----------------------------------------------------------------------------------
// AutoTab support
// If an entry field has data-nt-autotab=1", then when the maxlength is reached focus
// automatically moves to the next field.
// -----------------------------------------------------------------------------------
jQuery.fn.focusNextInputField = function() { // this function from http://jqueryminute.com/, thanks to jdSharp.
    return this.each(function() {
        var fields = $(this).parents('form:eq(0),body').find('button,input,textarea,select').not('[readonly]');
        var index = fields.index( this );
        if ( index > -1 && ( index + 1 ) < fields.length ) {
            fields.eq( index + 1 ).focus();
        }
        return false;
    });
};

jQuery(document).ready( function(){
    jQuery("body").delegate("[data-nt-autotab=1]","keyup",function(e) {
        if ($(this).val().length == $(this).attr("maxlength")){
            if((e.which >= 32) && (e.which <= 122)){
                jQuery(':focus').focusNextInputField();
            }       
        }   
    }); 
});


// recursive function to find the first checkbox which is "inside" c.
function getCheckbox(c){
 if (c.type == 'checkbox'){
  return c;
 }
 if (c.firstChild != null){
  a = getCheckbox(c.firstChild);
  if (a != null){
  return a;
  }
 }
 while (c.nextSibling != null){
  a = getCheckbox(c.nextSibling);
  if (a != null){
   return a;
  }
 }
 return null;
}

function dsb(event,f,b,n,prid,prv){
 var i=0;
 if (n=='deleteb_btn'){
  if(confirm('Are you sure you want to delete this record?')==false){
   return false;
  }
 }
 // dont send files if form is cancelled.
 if (n=='cancel_btn'){
                jQuery(':file').remove();
 }
 // set all buttons disabled, if target of button is same frame.
 if (f.target == "" || f.target == "_self"){
         jQuery(':button').attr('disabled', 'disabled');
 }
 for (var e=0 ; e < f.elements.length; e++) {
   if (f.elements[e].name == prid){
    f.elements[e].value = prv;
    i = 1;
   }
 }
 var bid = document.createElement('INPUT');
 bid.type = 'hidden';
 bid.name = '_buttontext_';
 bid.value = $(event.target).val();
 f.appendChild(bid);

 jQuery("#_webkit_").val(Math.random());
 if ((i==0) && (prid != '')){
  var rid = document.createElement('INPUT');
  rid.type = 'hidden';
  rid.name = prid;
  rid.value = prv;
  f.appendChild(rid);
 }
 var pb = document.createElement('INPUT');
 pb.type = 'hidden';
 pb.name = 'pressedButton';
 pb.value = n;
 f.appendChild(pb);
 osf(f);
 removePlaceHolder();
 f.submit();
}

function osf(f){
    if(f.target=='' || f.target=='_self' || f.target=='_top') {
        for (var e=0 ; e < f.elements.length; e++) {
            if(f.elements[e].type=='button'){
                f.elements[e].disabled = true;
            }
        }
    }
}

function ml(ta,ml,e){
  var k;
  if(window.event){ // IE
    k = e.keyCode
  } else if(e.which){ // Netscape/Firefox/Opera/Safari
    k = e.which
  };
  if (k==8 || k==null || k > 60000){
    return true;
  }
  return (ta.value.length <= ml);
}

function firstFocus(id){
  var e;
  var t = 4000000000;
    jQuery(id + ' :input').not('[readonly],[disabled],[type="hidden"]').each(function(){
        tx = $(this).offset().top
      if (tx < t && tx != 0){
        e = this;
        t = tx;
      }
    })
    $(e).focus();
}

function nextFocus(f,pname,skipone){
  var i = 0;
  var j = 0;
  if (skipone==2){ // pname is specified control to get focus
    for (var e=0 ; e < f.elements.length; e++) {
      if(f.elements[e].name==pname){
  try{
    f.elements[e].focus();
  } catch (e) {
  }
  break;
      }
    }
  } else {
    for (var e=0 ; e < f.elements.length; e++) {
      if (i==1){
  if ((f.elements[e].type == "text") || (f.elements[e].type == "textarea") || (f.elements[e].type == "checkbox") || (f.elements[e].type == "radio") || (f.elements[e].type == "select-one")){
    //|| (f.elements[e].type == "button")
    if(f.elements[e].readOnly != true){
      if((skipone==1) && (j==0)){
        j = 1;
      } else {
        try{
    f.elements[e].focus();
        } catch (e) {
        }
        break;
      }
    }
  }
      }
      else{
  if(pname==''){
    if(f.elements[e].readOnly != true){
      try{
        f.elements[e].focus();
      } catch (e) {
      }
      break;
    }
  } else {
    if(f.elements[e].name==pname){
      i = 1;
    }
  }
      }
    }
  }
}


function removeElement(fn,dn){
 var f=document.getElementById(fn);
 var dv=document.getElementById(dn);
 var a;
 var b;
 if (dv != null){
  var divs = dv.getElementsByTagName('DIV');
  for(var e = divs.length-1; e>=0 ; e--){
   if ((divs[e].id != dn) && (divs[e].id != '')){
    removeElement(fn,divs[e].id);
   }
  }
  if (f != null){
   for(var e = f.elements.length-1; e>=0 ; e--) {
    a = f.elements[e].parentNode.id;
    b = dv.id
    if (a==b){
     try{
      dv.removeChild(f.elements[e]);
     } catch (e) {
     }
    }
   }
  }
 }
}

function FieldValue(f,e){
  var ans ='';
  var typ = f.type;
  var i = 0;
  var j = 0;
  if (typ == undefined){
    typ = f[0].type;
  }
  switch (typ){
  case "radio":
    j = f.length;
    for(i = 0; i < j; i++) {
      if(f[i].checked) {
  ans = f[i].value;
  break;
      }
    }
    break;
  case "checkbox":
    if (f.checked){
      ans = f.value;
    }
    break;
  case "select-multiple":
    j = f.length;
    for(i = 0; i < j; i++) {
      if(f.options[i].selected) {
        ans = ans + ';|;' + f.options[i].value;
    }
    }
    break;
  default: 
    if ($(f).data('luv')){
      ans = $(f).data('luv');
    } else {
      ans = f.value;
    }  
  }
  // if called as a post, do not encode & and %. If called from EIP then do.
  if ((e == 0) || (e == undefined)){
                ans = ans.replace(/%/,"%25");
                ans = ans.replace(/&/,"%26");
                ans = ans.replace(/#/,"%23");
        }
  return ans
}

function SetSessionValue(name,value){
  jQuery.get('SetSessionValue',name+'='+value+'&_ajax_=1',function(data){xmlProcess(data);});
}

function TabChanged(url,value){
  jQuery.get(url,'_tab_='+value+'&_ajax_=1',function(data){xmlProcess(data);});
}

function GetTab(name){
  jQuery.get(name,'_ajax_=1',function(data){xmlProcess(data);});
}

function xmlProcess(data){
  if (typeof(data) == 'string'){
    jQuery('html').trigger("ajaxComplete");
    return;
  }
  jQuery('response',data).each(function(i){
    var t = jQuery("response",data).get(i); // returns Element object
    var e = jQuery(t).attr("type");
    if (window.ActiveXObject) {  //for IE 
        var s = t.xml;           // IE 9 doesn't get this 
        if (s == undefined){
                    var s = (new XMLSerializer()).serializeToString(t); // but IE9 can do this, which IE7/8 can't
        }
    } else { // code for Mozilla, Firefox, Opera, etc.
        var s = (new XMLSerializer()).serializeToString(t);
    }
    if (s){
            s = s.substring(s.indexOf('>')+1,s.lastIndexOf('<'));
            if (e=='element'){
                d = jQuery(t).attr("id");
                jQuery("#"+d).replaceWith(s);
                try{jQuery("#"+d).page().removeClass("ui-page").css('border',0);} catch(e){};
            } else if (e=='script'){
                s = s.replace(/&quot;/g,'"');
                s = s.replace(/&amp;/g,"&");
                s = s.replace(/&lt;/g,"<");
                s = s.replace(/&gt;/g,">");
                try{
                eval(s);
                } catch (e){
                    try{
                    } catch (e){}   
                }
            }
        }   
  });
  afterSv();
    gradient();
    resetCountDown();
}

// SetServer
function sv(id,name,ev,val,par,sil){
 hadfocus = id;
  if(par==undefined){
  jQuery.get(name,{_event_: ev,value: val,_ajax_:1, _rnd_: Math.random()},function(data){xmlProcess(data);});
 }else{
  var parms='';
  for(var d = 2; d < arguments.length; d++){
    parms += arguments[d] + '&';
  }
  parms += '_ajax_=1&_rnd_=' + Math.random();
  jQuery.get(name,parms,function(data){xmlProcess(data);});
 }
}

//Set timer
function SetTimer(name,t,par,sil){
 if(par==undefined)  {par='fred=1'};
 if(sil==undefined)  {sil='fred=2'};
 //sv('',name,'','',par,sil);
 //setTimeout('SetTimer(\'' + name +'\','+t+',\''+par +'\',\'' + sil+'\')',t);
 setTimeout("sv('','"+name+"','','','"+par+"','"+sil+"');",t);
};

// SelectDate and ResetAfterDate called by Date Lookup button
var cr1;
var cs;
var ct;
var cb1;
var cb2;
// SelectDate
function sd(f,e,p,r,b1,b2){
 ct = document.forms[f].elements[e];
 switch (p){
 case "@D6":
 case "@D06":
  var c = new calendar6(ct);
  break;
 case "@D2":
 case "@D02":
  var c = new calendar2(ct);
  break;
 }
 c.popup();
 if (arguments.length == 4){
  cr1 = r;
  cs = 1;
 }
 if (arguments.length == 6){
  cr1 = r;
  cs = 2;
  cb1 = b1;
  cb2 = b2;
 }
}
// ResetAfterDate
function rad(){
 if (cs==1){
  sv('',cr1,1,ct.value);
  cs = 0;
 }
 if (cs==2){
  sv('',cr1,cb1,cb2,'Value='+ct.value);
  cs = 0;
 }
}

// jQuery Default Settings
jQuery.datepicker.setDefaults({
   closeText: 'Cancel',
   dateFormat: 'm/dd/yy',
   showButtonPanel: true,
   showOn: 'button',
   buttonImageOnly: true,
   buttonImage: 'styles/images/calendar.gif',
   buttonText: 'Calendar',
   constrainInput: false
});


// html5 helper-functions for browsers that don't yet support html5.
// Modernizr is used as a detector so if browser has native support these functions do nothing.
// ---------------------------------------------
function applyPlaceHolderElement(e){
 var t = e.attr('placeholder');
 var f = e.parents('form:first');
 if (e.val() === ''){
  e.val(t);
  e.css('color', '#888');
 }
 e.bind('focus.placeholder', function(event) {
  if (e.val() === t){
   e.val('');
  }
  e.css('color', '');
 });
 e.bind('blur.placeholder', function(event) {
  if (e.val() === ''){
         e.val(t);
   e.css('color', '#888');
  }
 });
 f.bind("submit.placeholder", function(event) {
  if (e.val() === t){
   e.val("");
  }
 });
};

// ---------------------------------------------
// Jan 2010 - Webkit support placeholder on a <input> but none yet on <textarea>
function applyPlaceHolder(){
  jQuery('[placeholder]').each(function(i) {
    var e = jQuery(this);
    if (!Modernizr.input.placeholder && e.type === 'INPUT'){
    // do nothing
    } else {
            if (jQuery(e).is(":focus")) {
                // do nothing
            } else {
                applyPlaceHolderElement(e);
            }   
    };
  });
};
// ---------------------------------------------
function removePlaceHolder(){
 jQuery('[placeholder]').each(function(i) {
  var e = jQuery(this);
  if (e.val() === e.attr('placeholder')){
   e.val("");
  }
 });
};
// ---------------------------------------------
function applyHTML5(){
 applyPlaceHolder();
}
// ---------------------------------------------
function bubbleStyle(div,attr,col){
    if ((attr=='background-color') && (col != 'transparent')){
        jQuery("#"+div).parent().css('background-color',col);
        jQuery("#"+div).css('background-color','transparent');
        if (Modernizr.cssgradients ==  false){
            if (window.ActiveXObject) {  //for IE
                var ua = navigator.userAgent;
                var re  = new RegExp("MSIE ([0-8]{1,}[\.0-8]{0,})");
                if (re.exec(ua) != null){
                    jQuery("#"+div).parent().each(function(){
                        this.style.filter = '"filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='+col+', endColorstr=#FFFFFFFF)"';
                    }); 
                }   
            }
        } else {
            jQuery("#"+div).parent().css('background','-webkit-gradient(linear, 0 0, 0 100%, from('+col+'), to(#FFFFFF))');
            if (jQuery("#"+div).parent().css('background') == ''){
                jQuery("#"+div).parent().css('background','-moz-linear-gradient(center bottom, #FFFFFF 0%, '+col+' 75%)');
            }
        }   
    }
}

function gradient(){
    jQuery('.nt-grad').each(function(){
        var col = jQuery(this).css('background-color');
        if (col != 'transparent'){
            if (Modernizr.cssgradients ==  false){
                if (window.ActiveXObject) {  //for IE
                    var ua = navigator.userAgent;
                    var re  = new RegExp("MSIE ([0-8]{1,}[\.0-8]{0,})");
                    if (re.exec(ua) != null){
                        this.style.filter = '"filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='+col+', endColorstr=#FFFFFFFF)"';
                    }   
                }
            } else {
                jQuery(this).css('background','-webkit-gradient(linear, 0 0, 0 100%, from('+col+'), to(#FFFFFF))');
                if (jQuery(this).css('background') == ''){
                    jQuery(this).css('background','-moz-linear-gradient(center bottom, #FFFFFF 0%, '+col+' 75%)');
                }
            }   
        }
    });
}

// ---------------------------------------------
function browseCssSupport(){
// $('body').prepend('<div id="_ntbc_" class="nt-browse-colors"></div>');
}


// ---------------------------------------------
jQuery(document).ready( function(){jQuery('.rounded').corners();});


// ---------------------------------------------
// run html5 support scripts when page opens
jQuery(document).bind('ready', function(event) {
  applyHTML5();
  gradient();
    browseCssSupport();
});
// ---------------------------------------------
// IE checkbox / radio fix.
// http://norman.walsh.name/2009/03/24/jQueryIE
$(function () {
    if ($.browser.msie) {
        jQuery('input:radio, input:checkbox').bind('click.iefix',function () {
            this.blur();
            this.focus();
        });
    }
});


// ---------------------------------------------
//  Extension of jQueryUi dialog to add a call to SetAccess
// ---------------------------------------------
(function(jQuery){
    var _init = jQuery.ui.dialog.prototype._init;
    
    //Custom Dialog Init
    jQuery.ui.dialog.prototype._init = function() {
        _init.apply(this, arguments);
        var _this=this;
    if ((this.options.addsec != '') && (this.options.addsec != undefined)){
            tb = this.uiDialogTitlebar;
            tb.append('<a href="#" id="dialog-access" class="dialog-access ui-dialog-titlebar-access ui-corner-all"><span class="ui-icon ui-icon-key"></span></a>');
            //Secwin Button
            jQuery('.dialog-access', tb).hover(function(){
                jQuery(this).addClass('ui-state-hover');
            }, function(){
                jQuery(this).removeClass('ui-state-hover');
            }).click(function(){
                ntd.push('secwinwebuseraccess','','header',1,2,null,'','','_screen_=' + _this.options.addsec);  
                return false;
            });
        }   
    };

})(jQuery); 


function swpf(id,addsec){
  jQuery('#form-access-'+id).prepend('<a href="#" id="a-form-access-'+id+'" class="nt-form-page-access ui-widget-header ui-corner-all"><span class="ui-icon ui-icon-key"></span></a>');
    jQuery('#a-form-access-'+id).hover(function(){
        jQuery(this).addClass('ui-state-hover');
    }, function(){
        jQuery(this).removeClass('ui-state-hover');
    }).click(function(){
        ntd.push('secwinwebuseraccess','','header',1,2,null,'','','_screen_=' + addsec);    
        return false;
    });
}   

function getLocation(){
  navigator.geolocation.getCurrentPosition(sendLocation,noSendLocation);
}

function sendLocation(pos){      
  jQuery.get('SetSessionValue','_Latitude_=' + pos.coords.latitude + 
                               '&_Longitude_=' + pos.coords.longitude +
                               '&_Altitude_=' + pos.coords.altitude +
                               '&_Accuracy_=' + pos.coords.accuracy +
                               '&_AltitudeAccuracy_=' + pos.coords.altitudeAccuracy +
                               '&_Heading_=' + pos.coords.heading +                               
                               '&_Speed_=' + pos.coords.speed +
                               '&_LocationUnixTime_=' + parseInt(pos.timestamp/1000) +
                               '&_LocationDate_=' + parseInt(pos.timestamp / 86400000 + 61730) +
                               '&_LocationTime_=' + parseInt((pos.timestamp % 86400000) / 10) +
                               '&_LocationError_=' +
                               '&_ajax_=1'
         ,function(data){xmlProcess(data);});
}

function noSendLocation(err){    
  switch(err.code){  
    case err.PERMISSION_DENIED: SetSessionValue('_LocationError_',err.code + '_permission_denied');  
    break;  
    case err.POSITION_UNAVAILABLE: SetSessionValue('_LocationError_',err.code + '_position_unavailable');  
    break;  
    case err.TIMEOUT: SetSessionValue('_LocationError_',err.code + '_timeout');  
    break;  
    default: SetSessionValue('_LocationError_',err.code + '_unknown');  
    break;  
  }  
};


