////////////////////////////////////////////////////////////////////////////////////
// Shows loading image in center of window
//   Should be used:
//     OnSubmit of FORM element
//     OnClick of HREF element
////////////////////////////////////////////////////////////////////////////////////
function loadingwin()
{
	if (parent.menuframe) {
		if (document.all) {	// Internet Explorer
			parent.menuframe.loadingimage.style.display="inline";
		}
		else {
			parent.menuframe.document['loadingimage'].style.display="inline";
		}
	}
	else
	{
		if (parent.document.getElementById('loadingimage')) {
			parent.document.getElementById('loadingimage').style.display="inline";
		}
		else
		{
			if(typeof overlib == 'function') {
				if (document.all) {	// Internet Explorer
					var xxc = (document.body.clientWidth/2) - 70;
					var yyc = (document.body.clientHeight/2) - 120;
				} else if (document.layers) {	// Netscape
					var xxc = (window.innerWidth/2) - 70;
					var yyc = (window.innerHeight/2) - 120;
				}
				overlib('',BACKGROUND,'/eax/images/loading_eax_framed.gif',FIXX,xxc,FIXY,yyc,WIDTH,140,HEIGHT,80,STICKY);
			}
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////
// Closes window with loading image (NO LONGER USED, PROGRAMS STILL REFER TO THIS FUNCTION THOUGH)
//   Should be used:
//     OnLoad of BODY element
////////////////////////////////////////////////////////////////////////////////////
function closewin()
{
	if (parent.menuframe) {
		if (document.all) {	// Internet Explorer
			parent.menuframe.loadingimage.style.display="none";
		}
		else {
			parent.menuframe.document['loadingimage'].style.display="none";
		}
	}
	else
	{
		if (parent.document.getElementById('loadingimage')) {
			parent.document.getElementById('loadingimage').style.display="none";
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////
// Launches window in the center of the screen
//   Should be used:
//     Internal to this JS file only
////////////////////////////////////////////////////////////////////////////////////
function launchCenter(url, name, height, width) {
  var str = "height=" + height + ",innerHeight=" + height;
  str += ",width=" + width + ",innerWidth=" + width;
  if (window.screen) {
    var ah = screen.availHeight - 30;
    var aw = screen.availWidth - 10;

    var xc = (aw - width) / 2;
    var yc = (ah - height) / 2;

    str += ",left=" + xc + ",screenX=" + xc;
    str += ",top=" + yc + ",screenY=" + yc;
    
    str += ",toolbar=no,menubar=no,scrollbar=no,location=no,resizable=no"
  }
  return window.open(url, name, str);
}

////////////////////////////////////////////////////////////////////////////////////
// Launches shopping cart window from EAX program
//   Should be used:
//     If EAX programs are used to add to shopping cart
////////////////////////////////////////////////////////////////////////////////////
var openshopwin = null;
function newshopwindow(page,h,w) {
 	if (!openshopwin || openshopwin.closed) {
	  openshopwin=window.open(page,"ShopCart","toolbar=no,menubar=no,scrollbars=yes,location=no,resizable=yes,height="+h+",width="+w);
	  openshopwin.focus();
	}
	else {
	  openshopwin=window.open(page,"ShopCart","toolbar=no,menubar=no,scrollbars=yes,location=no,resizable=yes,height="+h+",width="+w);
	 openshopwin.focus();
	}
}

////////////////////////////////////////////////////////////////////////////////////
// Launches new named window (NewWindow) from EAX program
//   Should be used:
//     If EAX programs are used to open a new window
////////////////////////////////////////////////////////////////////////////////////
var newwinopen = null;
function crtnewwin(page,h,w) {
 	if (!newwinopen || newwinopen.closed) {
	  newwinopen=window.open(page,"NewWindow","status=no,toolbar=no,menubar=no,scrollbars=yes,location=no,resizable=yes,height="+h+",width="+w);
	  newwinopen.focus();
	}
	else {
	  newwinopen=window.open(page,"NewWindow","status=no,toolbar=no,menubar=no,scrollbars=yes,location=no,resizable=yes,height="+h+",width="+w);
	 newwinopen.focus();
	}
}

////////////////////////////////////////////////////////////////////////////////////
// Launches new unnamed window from EAX program
//   Should be used:
//     If EAX programs are used to open a new window
////////////////////////////////////////////////////////////////////////////////////
function crtnewblkwin(page,h,w) {
	  window.open(page,"_blank","status=no,toolbar=no,menubar=no,scrollbars=yes,location=no,resizable=yes,height="+h+",width="+w);
}

/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

/*
=============================================================
downloadcsv(url) : javascript associated with download button
=============================================================
*/

function downloadcsv(url) {
	window.open(url,"_blank","scrollbars=0,menubar=1,toolbar=0,location=0,status=0,resizable=1");
}

/*
=============================================================
AJAX
=============================================================
*/
// AJAX
var _ms_AJAX_Request_ActiveX = ""; // Holds type of ActiveX to instantiate

function AJAX_Update(url, obj, func)
{
  if (!url) return false;  // Don't run if missing the url parm. 
 
  // code for Mozilla, etc.
  if (window.XMLHttpRequest)
  {
  	var xmlhttp=new XMLHttpRequest();
  }
  // code for IE
  else if (window.ActiveXObject)
  {
      // Instantiate the latest MS ActiveX Objects
      if (_ms_AJAX_Request_ActiveX) 
			{
         xmlhttp = new ActiveXObject(_ms_AJAX_Request_ActiveX);
      } 
	  	else 
			{
	    	// loops through the various versions of XMLHTTP to ensure we're using the latest
	    	var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
                       "Microsoft.XMLHTTP"];
         	for (var i = 0; i < versions.length ; i++)
					{
         	  try 
			 		{
		   	      // try to create the object
		   	      // if it doesn't work, we'll try again
		   	      // if it does work, we'll save a reference to the proper one to speed up future instantiations
              xmlhttp  = new ActiveXObject(versions[i]);
              if (xmlhttp) 
							{
                 _ms_AJAX_Request_ActiveX = versions[i];
                 break;
              }
            }
            catch (objException) 
 			      {
                  // trap -  try next one
             } 
           }
        }
	}
	
	
    if (!xmlhttp) return false;
	 if (func) 
	  xmlhttp.onreadystatechange = function(){
	    if (xmlhttp.readyState != 4) return;
	    if (xmlhttp.status == 200)
	      func(obj, xmlhttp.responseText);
	    else 
	      alert("An error occurred " + xmlhttp.status);
	 };
    else
      xmlhttp.onreadystatechange = function() { return; }
    
	xmlhttp.open('GET', url, true);
    xmlhttp.send(null);
    if (func) {} else return xmlhttp.responseText;
    return false;
}

// Called from inside the function AJAX_Update: 
function Receive_AJAX_Response(myspan, response)
{
	myspan.innerHTML = ' ';

 	// display the span with returned html values
	myspan.innerHTML = response;
}

// Date Validation Javascript
// copyright 30th October 2004, by Stephen Chapman
// http://javascript.about.com

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function valDateFmt(datefmt) {
	myOption = -1;
	for (i=0; i<datefmt.length; i++) {
		if (datefmt[i].checked) {
			myOption = i;
		}
	}
	if (myOption == -1) {
		alert("You must select a date format");
		return ' ';
	}
	return datefmt[myOption].value;
}

function valDateRng(daterng) {
	myOption = -1;
	for (i=0; i<daterng.length; i++) {
		if (daterng[i].checked) {
			myOption = i;
		}
	}
	if (myOption == -1) {
		alert("You must select a date range");
		return ' ';
	}
	return daterng[myOption].value;
}

function stripBlanks(fld) {
	var result = "";
	var c = fld.length;
	for (i=0; i<fld.length; i++) {
		if (fld.charAt(i) != " " || c > 0) {
			result += fld.charAt(i);
			if (fld.charAt(i) != " ") {
				c = result.length;
			}
		}
	}
	return result.substr(0,c);
}

var numb = '0123456789';

function isValid(parm,val) {
	if (parm == "") 
		return true;
	for (i=0; i<parm.length; i++) {
		if (val.indexOf(parm.charAt(i),0) == -1)
			return false;
	}
	return true;
}

function isNum(parm) {
	return isValid(parm,numb);
}

var mth = new Array(' ','january','february','march','april','may','june','july','august','september','october','november','december');
var day = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function validateDate(fld,fmt,rng) {
	var dd, mm, yy;
	var today = new Date;
	var t = new Date;
	fld = stripBlanks(fld);
	if (fld == '')
		return false;
	var d1 = fld.split('\/');
	if (d1.length != 3) 
		d1 = fld.split(' ');
	if (d1.length != 3) 
		return false;
	if (fmt == 'u' || fmt == 'U') {
  		dd = d1[1];
  		mm = d1[0];
  		yy = d1[2];
  	}
	else if (fmt == 'j' || fmt == 'J') {
  		dd = d1[2];
  		mm = d1[1];
  		yy = d1[0];
  	}
	else if (fmt == 'w' || fmt == 'W') {
		dd = d1[0];
		mm = d1[1];
		yy = d1[2];
	}
	else 
		return false;

	var n = dd.lastIndexOf('st');
	if (n > -1) 
		dd = dd.substr(0,n);
	n = dd.lastIndexOf('nd');
	if (n > -1) 
		dd = dd.substr(0,n);
	n = dd.lastIndexOf('rd');
	if (n > -1) 
		dd = dd.substr(0,n);
	n = dd.lastIndexOf('th');
	if (n > -1) 
		dd = dd.substr(0,n);
	n = dd.lastIndexOf(',');
	if (n > -1) 
		dd = dd.substr(0,n);
	n = mm.lastIndexOf(',');
	if (n > -1) 
		mm = mm.substr(0,n);
	if (!isNum(dd)) 
		return false;
	if (!isNum(yy))
		return false;
	if (!isNum(mm)) {
  		var nn = mm.toLowerCase();
		for (var i=1; i < 13; i++) {
			if (nn == mth[i] ||	nn == mth[i].substr(0,3)) {
				mm = i; i = 13;
			}
  		}
	}
	if (!isNum(mm)) 
		return false;
	dd = parseFloat(dd);
	mm = parseFloat(mm);
	yy = parseFloat(yy);
	if (yy < 100)
		yy += 2000;
	if (yy < 1582 || yy > 4881)
		return false;
	if (mm == 2 && (yy%400 == 0 || (yy%4 == 0 && yy%100 != 0))) 
		day[mm-1]++;
	if (mm < 1 || mm > 12) 
		return false;
	if (dd < 1 || dd > day[mm-1]) 
		return false;
	t.setDate(dd);
	t.setMonth(mm-1);
	t.setFullYear(yy);
	if (rng == 'p' || rng == 'P') {
		if (t > today) 
			return false;
	}
	else if (rng == 'f' || rng == 'F') {
		if (t < today) 
			return false;
	}
	else if (rng != 'a' && rng != 'A') 
		return false;
	return true;
}