// ----------------------------------------------
// Preload Images for navigation
// John Kepler
// ----------------------------------------------

function preloadImages() {
	aImageSources = new Array (
	"/mha/images/home_over.gif", 
	"/mha/images/aboutMHA_over.gif", 
	"/mha/images/pressreleases_over.gif", 
	"/mha/images/hospitalinfo_over.gif", 
	"/mha/images/resources_over.gif", 
	"/mha/images/reports_over.gif", 
	"/mha/images/orderforms_over.gif", 
	"/mha/images/faq_over.gif", 
	"/mha/images/employment_over.gif", 
	"/mha/images/links_over.gif", 
	"/mha/images/education_over.gif", 
	"/mha/images/keystonecenter_over.gif", 
	"/mha/images/hospitalsact_over.gif", 
	"/mha/images/hospitalservices_over.gif", 
	"/mha/images/insurance_over.gif", 
	"/mha/images/membersonly_over.gif", 
	"/mha/images/current_over.gif", 
	"/mha/images/advisory_over.gif", 
	"/mha/images/monday_over.gif", 
	"/mha/images/advocacy_over.gif", 
	"/mha/images/feedback_over.gif", 
	"/mha/images/printpage_over.gif", 
	"/mha/images/send_over.gif", 
	"/mha/images/bookmarkus_over.gif", 
	"/mha/images/topassociation_over.gif",
	"/mha/images/topeducation_over.gif",
	"/mha/images/tophospitalserv_over.gif",
	"/mha/images/topinsurance_over.gif"
	)
		
	aImageList = new Array ();

    for (counter in aImageSources) {
        aImageList[counter] = new Image();
        aImageList[counter].src = aImageSources[counter];
    }
}

// ######################################################################
// BookmarkPage() 
// Call with: BookmarkPage(document.domain,document.URL,document.title)
//   determines which action is appropriate automatic bookmark for IE
//   or dialog prompt for Netscape or Macintosh
// John Kepler
// March 16, 2004
// ######################################################################
// 

function BookmarkPage(bmdomain,bmurl,bmtitle) {
	TextPrompt = 'to bookmark this page.';
	if (bmurl.indexOf("DesktopServlet") > 0 ) { 
		bmurl = "http://www.mha.org"; 
		TextPrompt = 'to bookmark this page, and change the link to "' + bmurl + '".';
	}

	if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion >= "4.0") {
		// WinIE supports the method
		window.external.AddFavorite(bmurl,bmtitle);
	} else {
		// Other browsers DO NOT support the method
		var isMac = navigator.userAgent.indexOf("Mac") != -1
		if (isMac) {
			// mac browser - all
			TextPrompt = 'Please use Command-D ' + TextPrompt;
			alert(TextPrompt);
		} else {
			// win browser - non-IE
			TextPrompt = 'Please use Control-Shift-D ' + TextPrompt;
			alert(TextPrompt);
		}
	}
}

// ----------------------------------------------
// Open Login Window
// John Kepler
// ----------------------------------------------
function loginNowLink() {  
	window.open('/login/hcportal', '', 'resizable=yes, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, width=292, height=312');
	return false;
}

// ----------------------------------------------
// Open Logout Window
// John Kepler
// ----------------------------------------------
function logoutNowLink() {  
//	return false;
}



// ----------------------------------------------
// Email this page
// John Kepler
// ----------------------------------------------

function emailThisPage(whereTo) {
	if (whereTo.indexOf("DesktopServlet") > 0 ) { whereTo = "http://www.mha.org"; }
	var myURL = "/mha/jsp/sendtoafriend.jsp?myURL=" +  whereTo;
	window.open(myURL, '', 'resizable=yes, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, width=540, height=610');
	//return false;
}

// ----------------------------------------------
// cssPrintPreview
// John Kepler
// Open This Window in Print Preview Mode
// ----------------------------------------------
function cssPrintPreview(myWin) {  
	//window.open(myWin, this.target, '');
	//setActiveStyleSheet('print display', 0); 
	//parent.print();
	window.print();
	//return myWin;
}

//reloads the window if Nav4 resized
function MM_reloadPage(init) {  
	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) history.go(0);
}

//Login validation function FORM VALIDATION FUNCTION
function objLogin(obj)
{
        //validate user name
        if(obj.userid.value == "")
        {
                alert("Please enter your username");
                obj.userid.focus();
                return false;
        }

        //validate password
        if(obj.password.value == "")
        {
                alert("Please enter your password");
                obj.password.focus();
                return false;
        }
        return true;
}

// ----------------------------------------------
// Button rollover - uses DOM
// John Kepler
// ----------------------------------------------

function buttonRollover(thisObj,mouse_state) {
	
	//don't do anything if can't access DOM
	//if (!document.getElementById()) return false;
	var thisElement = document.getElementById(thisObj);
	var thisElementSrc = thisElement.src;
	//var newElementSrc = thisElementSrc.replace('_up.gif','');
	
	
	//alert(newElementSrc + "_up.gif");
	
	if (mouse_state == "over") {
		var newElementSrc = thisElementSrc.slice(0,(thisElementSrc.indexOf('_up.gif')));
		thisElement.src = newElementSrc + "_over.gif";
	} else {
		var newElementSrc = thisElementSrc.slice(0,(thisElementSrc.indexOf('_over.gif')));
		thisElement.src = newElementSrc + "_up.gif";
	}

}

// ----------------------------------------------
// StyleSwitcher functions written by Paul Sowden
// http://www.idontsmoke.co.uk/ss/
// - - - - - - - - - - - - - - - - - - - - - - -
// For the details, visit ALA:
// http://www.alistapart.com/stories/alternate/
//
// nifty StyleSwitcher scripts modified by Aaron Jones 10/10/2002
// modified by jkepler@aha.org
// ----------------------------------------------

//window.onload = function(e) {
//  var cookie = readCookie("style");
//  var title = cookie ? cookie : getPreferredStyleSheet();
//  setActiveStyleSheet(title);
//}

//window.onunload = function(e) {
//  var title = getActiveStyleSheet();
//  createCookie("style", title, 365);
//}

function getActiveStyleSheet() {
var i, a;
 for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
  if(a.getAttribute("rel").indexOf("style") != -1
  && a.getAttribute("title")
  && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function setActiveStyleSheet(title, reset) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (reset == 1) {
	  createCookie("wstyle", title, 365);
  }
}

function setStyle() {
	var style = readCookie("wstyle");
	if (style != null) {
		setActiveStyleSheet(style, 0);
	}
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

// ----------------------------------------------


// ----------------------------------------------
// emailCheck V1.1.3
// andeep V. Tamhankar (stamhankar@hotmail.com) 
// http://javascript.internet.com
// modified by jkepler@aha.org
// ----------------------------------------------

function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

//alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
//alert("Ths username contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
//alert("Ths domain name contains invalid characters.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

//alert("The username doesn't seem to be valid.");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
//alert("Destination IP address is invalid!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
//alert("The domain name does not seem to be valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
//alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
//alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}

// ----------------------------------------------

// ----------------------------------------------
// LUHN Credit Card Ckeck
// Simon Tneoh (tneohcb@pc.jaring.my) 
// http://javascript.internet.com
// modified by jkepler@aha.org
// ----------------------------------------------

/* var Cards = new makeArray(8); */
/* Cards[0] = new CardType("MasterCard", "51,52,53,54,55", "16"); */
/* var MasterCard = Cards[0]; */
/* Cards[1] = new CardType("VisaCard", "4", "13,16"); */
/* var VisaCard = Cards[1]; */
/* Cards[2] = new CardType("AmExCard", "34,37", "15"); */
/* var AmExCard = Cards[2]; */
/* Cards[3] = new CardType("DinersClubCard", "30,36,38", "14"); */
/* var DinersClubCard = Cards[3]; */
/* Cards[4] = new CardType("DiscoverCard", "6011", "16"); */
/* var DiscoverCard = Cards[4]; */
/* Cards[5] = new CardType("enRouteCard", "2014,2149", "15"); */
/* var enRouteCard = Cards[5]; */
/* Cards[6] = new CardType("JCBCard", "3088,3096,3112,3158,3337,3528", "16"); */
/* var JCBCard = Cards[6]; */
/* var LuhnCheckSum = Cards[7] = new CardType(); */

/*************************************************************************\
CheckCardNumber(form)
function called when users click the "check" button.
\*************************************************************************/
/* function CheckCardNumber(myyear,mymonth) { */
/* var tmpmonth = mymonth; */
/* var tmpyear = "20" + myyear; */
/*  */
/* if (!(new CardType()).isExpiryDate(tmpyear, tmpmonth)) { */
/* alert("This credit card appears to ha already expired."); */
/* return false; */
/* } */
/*  */
/* card = form.CardType.options[form.CardType.selectedIndex].value; */
/* var retval = eval(card + ".checkCardNumber(\"" + form.CardNumber.value + */
/* "\", " + tmpyear + ", " + tmpmonth + ");"); */
/* cardname = ""; */
/* if (retval) */
/*  */


// comment this out if used on an order form
//alert("This card number appears to be valid.");


/* else { */
/* // The cardnumber has the valid luhn checksum, but we want to know which */
/* // cardtype it belongs to. */
/* for (var n = 0; n < Cards.size; n++) { */
/* if (Cards[n].checkCardNumber(form.CardNumber.value, tmpyear, tmpmonth)) { */
/* cardname = Cards[n].getCardType(); */
/* break; */
/*    } */
/* } */
/* if (cardname.length > 0) { */
/* alert("This looks like a " + cardname + " number, not a " + card + " number."); */
/* } */
/* else { */
/* alert("This card number is not valid."); */
/*       } */
/*    } */
/* } */
/*************************************************************************\
Object CardType([String cardtype, String rules, String len, int year, 
                                        int month])
cardtype    : type of card, eg: MasterCard, Visa, etc.
rules       : rules of the cardnumber, eg: "4", "6011", "34,37".
len         : valid length of cardnumber, eg: "16,19", "13,16".
year        : year of expiry date.
month       : month of expiry date.
eg:
var VisaCard = new CardType("Visa", "4", "16");
var AmExCard = new CardType("AmEx", "34,37", "15");
\*************************************************************************/
function CardType() {
var n;
var argv = CardType.arguments;
var argc = CardType.arguments.length;

this.objname = "object CardType";

var tmpcardtype = (argc > 0) ? argv[0] : "CardObject";
var tmprules = (argc > 1) ? argv[1] : "0,1,2,3,4,5,6,7,8,9";
var tmplen = (argc > 2) ? argv[2] : "13,14,15,16,19";

this.setCardNumber = setCardNumber;  // set CardNumber method.
this.setCardType = setCardType;  // setCardType method.
this.setLen = setLen;  // setLen method.
this.setRules = setRules;  // setRules method.
this.setExpiryDate = setExpiryDate;  // setExpiryDate method.

this.setCardType(tmpcardtype);
this.setLen(tmplen);
this.setRules(tmprules);
if (argc > 4)
this.setExpiryDate(argv[3], argv[4]);

this.checkCardNumber = checkCardNumber;  // checkCardNumber method.
this.getExpiryDate = getExpiryDate;  // getExpiryDate method.
this.getCardType = getCardType;  // getCardType method.
this.isCardNumber = isCardNumber;  // isCardNumber method.
this.isExpiryDate = isExpiryDate;  // isExpiryDate method.
this.luhnCheck = luhnCheck;// luhnCheck method.
return this;
}

/*************************************************************************\
boolean checkCardNumber([String cardnumber, int year, int month])
return true if cardnumber pass the luhncheck and the expiry date is
valid, else return false.
\*************************************************************************/
function checkCardNumber() {
var argv = checkCardNumber.arguments;
var argc = checkCardNumber.arguments.length;
var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
var year = (argc > 1) ? argv[1] : this.year;
var month = (argc > 2) ? argv[2] : this.month;

this.setCardNumber(cardnumber);
this.setExpiryDate(year, month);

if (!this.isCardNumber())
return false;
if (!this.isExpiryDate())
return false;

return true;
}
/*************************************************************************\
String getCardType()
return the cardtype.
\*************************************************************************/
function getCardType() {
return this.cardtype;
}
/*************************************************************************\
String getExpiryDate()
return the expiry date.
\*************************************************************************/
function getExpiryDate() {
return this.month + "/" + this.year;
}
/*************************************************************************\
boolean isCardNumber([String cardnumber])
return true if cardnumber pass the luhncheck and the rules, else return
false.
\*************************************************************************/
function isCardNumber() {
var argv = isCardNumber.arguments;
var argc = isCardNumber.arguments.length;
var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
if (!this.luhnCheck())
return false;

for (var n = 0; n < this.len.size; n++)
if (cardnumber.toString().length == this.len[n]) {
for (var m = 0; m < this.rules.size; m++) {
var headdigit = cardnumber.substring(0, this.rules[m].toString().length);
if (headdigit == this.rules[m])
return true;
}
return false;
}
return false;
}

/*************************************************************************\
boolean isExpiryDate([int year, int month])
return true if the date is a valid expiry date,
else return false.
\*************************************************************************/
function isExpiryDate() {
var argv = isExpiryDate.arguments;
var argc = isExpiryDate.arguments.length;

year = argc > 0 ? argv[0] : this.year;
month = argc > 1 ? argv[1] : this.month;

if (!isNum(year+""))
return false;
if (!isNum(month+""))
return false;
today = new Date();
expiry = new Date(year, month);
if (today.getTime() > expiry.getTime())
return false;
else
return true;
}

/*************************************************************************\
boolean isNum(String argvalue)
return true if argvalue contains only numeric characters,
else return false.
\*************************************************************************/
function isNum(argvalue) {
argvalue = argvalue.toString();

if (argvalue.length == 0)
return false;

for (var n = 0; n < argvalue.length; n++)
if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
return false;

return true;
}

/*************************************************************************\
boolean luhnCheck([String CardNumber])
return true if CardNumber pass the luhn check else return false.
Reference: http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
\*************************************************************************/
function luhnCheck() {
var argv = luhnCheck.arguments;
var argc = luhnCheck.arguments.length;

var CardNumber = argc > 0 ? argv[0] : this.cardnumber;

if (! isNum(CardNumber)) {
return false;
  }

var no_digit = CardNumber.length;
var oddoeven = no_digit & 1;
var sum = 0;

for (var count = 0; count < no_digit; count++) {
var digit = parseInt(CardNumber.charAt(count));
if (!((count & 1) ^ oddoeven)) {
digit *= 2;
if (digit > 9)
digit -= 9;
}
sum += digit;
}
if (sum % 10 == 0)
return true;
else
return false;
}

/*************************************************************************\
ArrayObject makeArray(int size)
return the array object in the size specified.
\*************************************************************************/
function makeArray(size) {
this.size = size;
return this;
}

/*************************************************************************\
CardType setCardNumber(cardnumber)
return the CardType object.
\*************************************************************************/
function setCardNumber(cardnumber) {
this.cardnumber = cardnumber;
return this;
}

/*************************************************************************\
CardType setCardType(cardtype)
return the CardType object.
\*************************************************************************/
function setCardType(cardtype) {
this.cardtype = cardtype;
return this;
}

/*************************************************************************\
CardType setExpiryDate(year, month)
return the CardType object.
\*************************************************************************/
function setExpiryDate(year, month) {
this.year = year;
this.month = month;
return this;
}

/*************************************************************************\
CardType setLen(len)
return the CardType object.
\*************************************************************************/
function setLen(len) {
// Create the len array.
if (len.length == 0 || len == null)
len = "13,14,15,16,19";

var tmplen = len;
n = 1;
while (tmplen.indexOf(",") != -1) {
tmplen = tmplen.substring(tmplen.indexOf(",") + 1, tmplen.length);
n++;
}
this.len = new makeArray(n);
n = 0;
while (len.indexOf(",") != -1) {
var tmpstr = len.substring(0, len.indexOf(","));
this.len[n] = tmpstr;
len = len.substring(len.indexOf(",") + 1, len.length);
n++;
}
this.len[n] = len;
return this;
}

/*************************************************************************\
CardType setRules()
return the CardType object.
\*************************************************************************/
function setRules(rules) {
// Create the rules array.
if (rules.length == 0 || rules == null)
rules = "0,1,2,3,4,5,6,7,8,9";
  
var tmprules = rules;
n = 1;
while (tmprules.indexOf(",") != -1) {
tmprules = tmprules.substring(tmprules.indexOf(",") + 1, tmprules.length);
n++;
}
this.rules = new makeArray(n);
n = 0;
while (rules.indexOf(",") != -1) {
var tmpstr = rules.substring(0, rules.indexOf(","));
this.rules[n] = tmpstr;
rules = rules.substring(rules.indexOf(",") + 1, rules.length);
n++;
}
this.rules[n] = rules;
return this;
}

