// Check the length of a text area component.

var globalTextLen = 0;

function checkLen(target, size, charsLeftTarget)
{
    strLen = target.value.length
    if( globalTextLen != strLen )
    {
        globalTextLen = strLen;
        if( globalTextLen > size )
        {
            alert("This field is too long.  It contains " + strLen + " characters.");
        }
    }
    if( charsLeftTarget != null )
    {
        charsLeft = size - strLen;
        charsLeftTarget.value = charsLeft;
    }
    target.focus();
}

// Check a given value is a number.

function isNumber(inputVal, isFloat)
{
    foundDecimal = false;
    inputStr = inputVal.toString();
    lastCharPos = inputStr.length -1;
    for( var i = 0 ; i < inputStr.length ; i++ )
    {
        var ch = inputStr.charAt(i)
        if( i == 0 && ch == '-' )
        {
            continue;
        }
        if( isFloat && ch=='.' && 
            !foundDecimal && 
            i != (lastCharPos) && 
            i != 0 )
        {
            foundDecimal = true;
            continue;
        }
        if( ch < '0' || ch > '9' )
        {
            if( isFloat )
                alert("You may only enter real numbers in this field. The current value is: " + inputStr);
            else
                alert("You may only enter integer numbers in this field. The current value is: " + inputStr);
            return false;
        }
    }
    return true;
}

// Check an input component contains a number.

function numericOnly(target, isFloat)
{
    if( !isNumber(target.value, isFloat) )
    {
        target.focus()
        target.select()
    }
}

// Form resubmission prevention.

var submitted = false;

function hasSubmitted()
{
    if (submitted)
    {
        alert("A double click has been detected.  You only have to click once on a NEXT or a SUBMIT button.  Please click OK and wait for the next browser page to be loaded.");
        return false;
    }
    else
    {
        submitted = true;
        return true;
    }
}

// Wait 25 minutes before warning the user that their session will expire soon.
// They then have 5 minutes to save their work.

function sessionTimeoutWarning()
{
    setTimeout("alert('WARNING:  Your session will expire shortly.  To save your work, click on the Save, Next, or Prev buttons.')", 1500000);
}

// Print routine.

function printit()
{  
	if (window.print) 
	{
		window.print() ;  
	} 
	else 
	{
		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
		WebBrowser1.ExecWB(6, 2); //Use a 1 vs. a 2 for a prompting dialog box    
		WebBrowser1.outerHTML = "";  
	}
}

// Window popup routines

var r_names   = new Array;
var r_windows = new Array;

function openWindow(url, name)
{
    win = window.open(url, name,"directories=no,location=no,menubar=yes,resizable=yes,scrollbars=yes,toolbar=no,width=640,height=480,left=10,right=0,top=10,bottom=0");
    win.focus();
}

function r_openWindow(url, name, options)
{
  // get window by name
  for (var i=0; i<r_names.length; i++)
  {
    if (r_names[i] == name)
    {
      if (!r_windows[i].closed)
      {
        r_windows[i].focus();
        return;
      }
      else  // window was closed - reopen
      {
        var w = window.open(url, name, options);
        r_windows[i] = w;
        w.focus();
        return;
      }
    }
  }

  // new window
  var w = window.open(url, name, options);
  r_names[i] = name;
  r_windows[i] = w;
  w.focus();
}

// Called by a hyperlink submition and prevent submitting more than once
// @param url The url if the submition is a link only, null if formName not null
// @param formName The name of form to be submitted, null if url not null
// @param name The name of the window
// @param The options to open the new window
// example: Form submition: <a href="javascript:;" onClick="return r_open_submit(null, "form1", "example", "location=false");">
//          Link submition: <a href="javascript:;" onClick="return r_open_submit("next.html", null, "example", "location=false");">
var m_window = false;
function r_open_submit(url, formName, name, options)
{
   if (m_window && !m_window.closed)
	{
	    m_window.focus();
        return false;
	}else{
	    if (url == null)
	        url = "";
	    if (options == null)
	        options = "directories=no,location=no,menubar=yes,resizable=yes,scrollbars=yes,toolbar=no,width=640,height=480,left=10,right=0,top=10,bottom=0";
	    m_window = window.open(url, name, options);
        // form submition
        if (formName != null)
        {
            eval("document." + formName + ".submit()");
        }
	    return true; 
	} 
}

// Save file routine

function saveHTMLFile (filename)
{
	document.execCommand('SaveAs', null, filename)
}

// Image routines

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr;
  for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)
    x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document;
  if(d.images)
  {
    if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
    for(i=0; i<a.length; i++)
      if (a[i].indexOf("#")!=0){
        d.MM_p[j]=new Image;
        d.MM_p[j++].src=a[i];
      }
  }
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;
  if(!d) d=document;
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document;
    n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all)
    x=d.all[n];
  for (i=0;!x&&i<d.forms.length;i++)
    x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++)
    x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById)
    x=document.getElementById(n);
  return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments;
  document.MM_sr=new Array;
  for(i=0;i<(a.length-2);i+=3)
    if ((x=MM_findObj(a[i]))!=null){
      document.MM_sr[j++]=x;
      if(!x.oSrc)
        x.oSrc=x.src;
      x.src=a[i+2];
    }
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  features = features + ",left=10, right=0, top=10, bottom=0";
  window.open(theURL,winName,features);
}

function MM_openBrWindowMaximise(theURL,winName,features) { //v2.0
  var w = screen.availWidth - 10;
  var h = screen.availHeight - 100;
  features = features + ", width=" + w + ", height=" + h + ", left=0, right=0, top=0, bottom=0";
  window.open(theURL,winName,features);
}

MM_reloadPage(true);

// ===========================
// Prevents right clicking
// ===========================

IE4plus = (document.all) ? true : false;
NS4 = (document.layers) ? true : false;

function clickIE() {
  return false;
}

function clickNS(e) {
  if (e.which==2 || e.which==3) {
    return false;
  }
}
if (!IE4plus) {
  document.captureEvents(Event.MOUSEDOWN || Event.MOUSEUP);
  document.onmousedown=clickNS;
  document.onmouseup= clickNS;
  document.oncontextmenu=clickIE; // For NS 6+
} 
else {
  document.onmouseup= clickIE;
  document.oncontextmenu=clickIE;
}
