//
// Javascript utility functions
// Created by RCE & others

/******************************/
function isEmpty (inStr) {
  var s = inStr.replace(/^\s+/,'').replace(/\s+$/,'');
  return ((s.length === 0) || (s === ""));
} /* isEmpty */

/******************************/
function reverseStr (inStr) {
   var outStr="";
   for (i=inStr.length-1;i>-1;i=i-1) {
       outStr += inStr.charAt(i);
   }
   return (outStr);
} /* reverseStr */

/******************************/
function trimStr (inStr) {
	return inStr.replace(/^\s+|\s+$/g, '') ;
}

/******************************/
function is_array(obj){
	if (obj.constructor.toString().indexOf("Array") == -1) {
		return false;
	} else {
		return true;
	}
}

/******************************/
function CheckEmail (emailField,emptyOK) {
  // RCE added new email validation routine from Netscape 10-23-2001
  // Also modified to ensure it contains a valid top-level domain
  // resource: http://data.iana.org/TLD/tlds-alpha-by-domain.txt
  var newstr = "";
  var at = false;
  var dot = false;
  var tld = "";
  var tmp = trimStr(emailField.value.toLowerCase());
  emailField.value = tmp;
  var checkString = tmp;
  var validTLDs =	":AC:AD:AE:AERO:AF:AG:AI:AL:AM:AN:AO:AQ:AR:ARPA:AS:ASIA:AT:AU:AW:AX:AZ:BA:BB:BD:BE:BF:BG" +
				 	":BH:BI:BIZ:BJ:BM:BN:BO:BR:BS:BT:BV:BW:BY:BZ:CA:CAT:CC:CD:CF:CG:CH:CI:CK:CL:CM:CN:CO:COM" +
					":COOP:CR:CU:CV:CX:CY:CZ:DE:DJ:DK:DM:DO:DZ:EC:EDU:EE:EG:ER:ES:ET:EU:FI:FJ:FK:FM:FO:FR:GA" +
					":GB:GD:GE:GF:GG:GH:GI:GL:GM:GN:GOV:GP:GQ:GR:GS:GT:GU:GW:GY:HK:HM:HN:HR:HT:HU:ID:IE:IL:IM:" +
					"IN:INFO:INT:IO:IQ:IR:IS:IT:JE:JM:JO:JOBS:JP:KE:KG:KH:KI:KM:KN:KP:KR:KW:KY:KZ:LA:LB:LC:LI:LK" +
					":LR:LS:LT:LU:LV:LY:MA:MC:MD:ME:MG:MH:MIL:MK:ML:MM:MN:MO:MOBI:MP:MQ:MR:MS:MT:MU:MUSEUM:MV:MW" +
					":MX:MY:MZ:NA:NAME:NC:NE:NET:NF:NG:NI:NL:NO:NP:NR:NU:NZ:OM:ORG:PA:PE:PF:PG:PH:PK:PL:PM:PN:PR" +
					":PRO:PS:PT:PW:PY:QA:RE:RO:RS:RU:RW:SA:SB:SC:SD:SE:SG:SH:SI:SJ:SK:SL:SM:SN:SO:SR:ST:SU:SV:SY" +
					":SZ:TC:TD:TEL:TF:TG:TH:TJ:TK:TL:TM:TN:TO:TP:TR:TRAVEL:TT:TV:TW:TZ:UA:UG:UK:US:UY:UZ:VA:VC:VE" +
					":VG:VI:VN:VU:WF:WS:XN:YE:YT:YU:ZA:ZM:ZW:";

  if (emailField.type == 'hidden') { return (true); }
  
  if (isEmpty(checkString)) {
	  if (emptyOK === true) {
		return (true);
	  }
	  Y_alert("Sorry, but the email field may not be blank.",emailField);
	  return false;
  }

  // Pull the putative TLD and validate it against the acceptable set
  // This involves pulling the tail-end of the field, from the final '.',
  // and comparing it to the list of acceptable TLDs in validTLDs.  Note
  // that the validTLD string has every valid TLD bracketed by colons,
  // so that .co (=":co:") doesn't match .com (=":com:").
  tld = reverseStr(checkString);
  tld = tld.substr(0,tld.search(/\./));			// find the last '.' and copy to it
  tld = new RegExp(":" + reverseStr(tld) + ":","i");			//This becomes the search string
  if (validTLDs.search(tld) == -1) {
    Y_alert("The top-level domain (the part at the end of the address, like '.com' or '.net' or '.me') isn't valid.  Please check the email address.",emailField);   
    return (false);
  }

  for (var i = 0; i < checkString.length; i++) {
    ch = checkString.substring(i, i + 1);
    if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
            || (ch == "@") || (ch == ".") || (ch == "_")
            || (ch == "-") || (ch >= "0" && ch <= "9")) {
      newstr += ch;
      if (ch == "@") {
          if (at === true) { //only one per address!
            at = false;
            break;
          }
          at=true;
      }
    }
  }
  if (!at) {
    Y_alert("Email addresses contain the '@' sign. Please check the email address.",emailField);   
    return (false);
  }
  if (newstr.length != checkString.length) {	// invalid characters were not copied
    Y_alert("The email address you entered contains characters taht are not valid.",emailField);
    return (false);
  }

  return (true);

} /* CheckEmail() */

/******************************/
function CheckZIP (zipField,emptyOK) {

  // Added 2001-12-29 by RCE
  // Checks for valid ZIP code and re-formats to ZIP or ZIP+4
  var checkStr = zipField.value;
  var numStr = "";
  var checkDigit;

  if (zipField.type == 'hidden') { return (true); }

  // strip out non-numeric characters
  for (var i = 0; i < checkStr.length; i++) {
    checkDigit = checkStr.charAt(i);
    if (checkDigit >= "0" && checkDigit <= "9") {
      numStr += checkDigit;
    }
  }
  // make sure it's a valid ZIP code length
  if (((numStr.length != 5) && (numStr.length != 9)) ||
		(numStr.length === 0 && !emptyOK)
	 ) {
    Y_alert ("Please enter a valid ZIP code.",zipField);
    return (false);
  }
  if (numStr.length == 9) {  // ZIP+4 -> reformat
    zipField.value = numStr.substr(0,5) + "-" + numStr.substr(5,4);
  } else {
    zipField.value = numStr;
  }
  return (true);

} /* CheckZIP */

/******************************/
function CheckCreditCard (cardField,emptyOK,maskOK) {

  // Test credit card number for validity
  // Created 2001-04-03 by RCE
  // Modified 2002-08-21 by RCE
  //   added 'emptyOK' to allow user to tab
  //   out of field during data entry
  //
  var checkdigit = 0;
  var checksum = 0;
  var checkStr = cardField.value;
  var digitStr = "";	// for storage of the digits only
  var digitnumber = 0;
  
  if (maskOK === null) {
	  maskOK = false;
  }

  if (cardField.type == 'hidden') { return (true); }

  if (isEmpty(checkStr)) {
    if (!emptyOK) {
      Y_alert("Please enter your credit card number.",cardField);
      return (false);
    } else {
      return (true);
    }
  }
  
  if (maskOK && checkStr.substr(0,8) == "XXXX-XXX") {	// masked? accept it
	  return (true);
  }
  
  if (checkStr == "4222222222222") {	// Authorize.net test card?  accept it (must be formatted like this)
  	return (true);
  }

  // Run through the number backwards
  for (var i = checkStr.length-1; i >= 0; i--) {
    // first strip out all but numbers
    checkdigit = checkStr.charCodeAt(i) - 48;
    if ((checkdigit >= 0) && (checkdigit <= 9)) {
      digitStr += checkStr.charAt(i);	// Save the digit for later
      digitnumber += 1;			// Note: counts 1..n+1, not 0..n
      if (digitnumber % 2 === 0) {
        checkdigit *= 2;		// Even? Double it
	  }
      if (checkdigit > 9) {
        checkdigit = checkdigit-9;	// Too big? Reduce by 9 (same as adding digits)
	  }
      checksum += checkdigit;		// Add to checksum
    }
  }
  if (digitStr == "7200000007004" || digitStr == "5100000000004245" || digitStr == "6011000000000004") {
     // test numbers - fine as is
     return (true);
  }
  if ((digitStr.length == 16 && digitStr.charAt(15) != '4' && digitStr.charAt(15) != '5' && digitStr.charAt(15) != '6') || 
      (digitStr.length == 15 && digitStr.charAt(14) != '3') ||
      (digitStr.length < 15 || digitStr.length > 16)) {
     Y_alert ("Sorry - we only accept Visa, Mastercard, Discover, and American Express. Please try again.",cardField);
     return (false);
  }


  if ((checksum % 10) !== 0) {
    Y_alert("Please re-check your credit card number.",cardField);
    return (false);
  }

  // digitStr now has the card number, backwards.
  // Reverse it.
  checkStr = digitStr;
  digitStr = "";
  var len = checkStr.length;
  for (i = len-1; i>=0; i--) {
    digitStr += checkStr.charAt(i);
  }

  // Now format the string correctly
  // Only current options are Visa and Mastercard,
  // both of which are 16-digit numbers.
  // Format it as XXXX-XXXX-XXXX-XXXX, using digitStr.
  // Append MM-YYYY from orderCCExpMo and orderCCExpYr.
  
  if (digitStr.length == 16) {
     cardField.value = digitStr.substr(0,4) + "-" +
                       digitStr.substr(4,4) + "-" +
                       digitStr.substr(8,4) + "-" +
                       digitStr.substr(12,4);
  } else {
     cardField.value = digitStr.substr(0,4) + "-" +
                       digitStr.substr(4,4) + "-" +
                       digitStr.substr(8,3) + "-" +
                       digitStr.substr(11,4);
  }

  return (true);
} /* CheckCreditCard */

/******************************/
function CheckPhoneNumber (phoneField, emptyOK) {

  var checkOK = "0123456789";
  var checkStr = phoneField.value;
  var newStr = "";
  var digits = 0;
  var allValid = true;

  if (phoneField.type == 'hidden' || (isEmpty(checkStr) && (emptyOK === true))) {
    return (true);
  }
  for (var i = 0;  i < checkStr.length;  i++) {
    var ch = checkStr.charAt(i);
    if (ch >= "0" && ch <= "9") {
      digits++;
      newStr += ch;
    }
  }
  if ((digits!=10)) {
    Y_alert("Please enter a valid phone number.",phoneField);
    return (false);
  }
  phoneField.value = "(" + newStr.substr(0,3) + ") " + newStr.substr(3,3) + "-" +newStr.substr(6,4);
  return (true);
} /* CheckPhoneNumber */

/******************************/

function CheckDate (value) {
	
	var IsoDateRe = new RegExp("^([0-9]{4})-([0-9]{2})-([0-9]{2})$");
	
	var matches = IsoDateRe.exec(value);
	
	if (!matches) { return false; }
	
	var composedDate = new Date(matches[1], (matches[2] - 1), matches[3]);
	
	return ((composedDate.getMonth() == (matches[2] - 1)) &&
			 (composedDate.getDate() == matches[3]) &&
			 (composedDate.getFullYear() == matches[1])
			);
		
}

/******************************/

function integerOnly (field) {
	
	var fldVal = field.value;
	field.value = strvalue(fldVal);
	return (true);
}

/**********************************/

function integerTest (field, negOK, zeroOK, blankOK) {
	var inStr = field.value;
	var outStr = "";
	var negAdded = false;
	var thisChar;
	for (var i=0; i<inStr.length; i++) {
		thisChar = inStr.charAt(i);
		if (thisChar >= '0' && thisChar <= '9') {
			outStr += thisChar;
		} else
		if (thisChar == '-' && negOK && !negAdded) {
			outStr = '-' + outStr;
			negAdded = true;
		}
	}
	field.value = outStr;
	if (isEmpty(outStr) && !blankOK) {
		Y_alert("Sorry - this field cannot be blank",field);
		return false;
	}
	if (!negOK && parseInt(outStr,10) < 0) {
		Y_alert("Sorry - negative numbers are not allowed here",field);
		return false;
	}
	if (!zeroOK && parseInt(outStr,10) === 0) {
		Y_alert ("Sorry - zero values are not allowed here",field);
		return false;
	}
	return true;
}
	

/******************************/
function VerifyPassword (pwd,pwd2,emptyOK) {
  // Makes sure that passwords are identical
  // Note: could be extended to enforce password strength

	var p1 = document.getElementById(pwd);
	var p2 = document.getElementById(pwd2);
	
	if (isEmpty(p1.value) && !emptyOK) {
		Y_alert("Please enter a password.",p1);
		return false;
	}
	if (p1.value != p2.value && !emptyOK) {
		p1.value = "";
		p2.value = "";
		Y_alert("The two passwords must match.  Please try again.",p1);
		return false;
	}
	return true;
} // VerifyPassword

/******************************/
function strvalue (inStr) {
   var outStr = "";
   for (var i=0; i<inStr.length; i++) {
      var thisChar = inStr.charAt(i);
      if (thisChar >= '0' && thisChar <= '9') { outStr += thisChar; }
   }
   return (Math.floor(outStr));

} /* strvalue */

/******************************/
function strdecimal (inStr) {
   // strips everything but the numbers, minus sign, and decimal point
   var outStr = "";
   for (var i=0; i<inStr.length; i++) {
      var thisChar = inStr.charAt(i);
      if ((thisChar >= '0' && thisChar <= '9') || thisChar == '.' || thisChar == '-') { outStr += thisChar; }
   }
   if (outStr === "") { outStr = "0.00"; }
   return (parseFloat(outStr));

} /* strdecimal */

/*************************************/
function charOK (okChars,event) {
   var keycode;
   if (window.event) {
      keycode = window.event.keyCode;
   } else if (event) {
      keycode = event.which;
   } else {
      return true;
   }
   for (var i=0; i<okChars.length; i++) {
      if (okChars.charCodeAt(i) == keycode) {
         return (true);	// character is in approved set
	  }
   }
   return (false); // character not found in approved set

} /* charOK */


/***********************************/
function CommaFormatted(amount,showZero,showDollarSign) {
	amount = strdecimal(amount).toString();	// strip all but numeric-related characters
   var delimiter = ","; // replace comma if desired
   rExp = new RegExp(delimiter,"g");
   amount = amount.replace(rExp,"");	// zap existing delimiters
   rExp = new RegExp("\.","i");
   if (!rExp.test(amount)) {
      amount += '.00';
   }
   var a = amount.split('.',2);
   if (a[0] === '') {
	   a[0] = '0';
   }
   if (a[1] === undefined) {
	   a[1] = '00';
   }
   var d = (a[1] === '' ? '00' : a[1]);
   if (d.length == 1) {
	   d += '0';
   }
   var i = parseInt(a[0],10);
   if(isNaN(i)) {
	   return ('');
   }
   var minus = (i < 0 ? '-' : '');
   i = Math.abs(i);
   var n = new String(i);
   a = [];
   while(n.length > 3) {
      var nn = n.substr(n.length-3);
      a.unshift(nn);
      n = n.substr(0,n.length-3);
   }
   if(n.length > 0) {
	   a.unshift(n);
   }
   n = a.join(delimiter);
   amount = n + '.' + d;
   amount = minus + amount;
   if (showZero === false && parseFloat(amount) === 0) {
	   return ('');
   }
   if (showDollarSign) {
	   amount = '$' + amount;
   }
   return amount;

} /* CommaFormatted */


/***********************************/

function inspect(elm){
   var str = "";
   var j = 1;
   for (var i in elm) {
      str += i + ": " + elm.getAttribute(i) + (j++ % 3 ? "\t" : "\n");
   }
   Y_alert(str);
} /* inspect */

/**********************************/

function getSelectedRadioValue (field) {
	var btns;
	if (field.name) {	// just one
		btns = document.getElementsByName(field.name);
	} else {	// already did this
		btns = field;
	}
	for (var i=0; i<btns.length; i++) {
		if (btns[i].checked) {
			return (btns[i].value);
		}
	}
	return ("");
}

/**********************************/

function setRadioValue (field,value) {
	var btns;
	if (field.name) {	// just one
		btns = document.getElementsByName(field.name);
	} else {	// already did this
		btns = field;
	}
	for (var i=0; i<btns.length; i++) {
		btns[i].checked = (btns[i].value == value);
	}
}

/**********************************/

function getSelectedOptionInfo (fieldID,attrib) {
    // gets the selectedIndex option of type 'attrib'
    var selIndex = document.getElementById(fieldID).selectedIndex;
    if (selIndex == -1) { return ""; }	// no value selected
    return (eval("document.getElementById(fieldID).options[document.getElementById(fieldID).selectedIndex]"+"."+attrib));

} // getSelectedOptionInfo

/**********************************/

function setSelectedOptionByText (selField,txt) {
    for (i=0; i<selField.options.length; i++) {
        if (selField.options[i].text == txt) {
            selField.selectedIndex = i;
            break;
        }
    }
    if (i < selField.options.length) {
        return selField.options[i].value;
    } else {
        return '';
    }
}

/**********************************/

function setSelectedOptionByValue (selField,val) {
    for (i=0; i<selField.options.length; i++) {
        if (selField.options[i].value == val) {
            selField.selectedIndex = i;
            break;
        }
    }
    if (i < selField.options.length) {
        return selField.options[i].value;
    } else {
        return '';
    }
}

/**********************************/

var dirtyBit = 0;

function setDirtyBit() {
	if (document.getElementById('dirtyBit')) {
		document.getElementById('dirtyBit').value = 1;
	} else {
		dirtyBit = 1;
		try {
			setDirtyBitExtraFn();
		}
		catch(err) {
		}
	}
}

/*********************************/
function closeIfNotDirty(close) {
	var docDirtyBit = document.getElementById('dirtyBit');
	if ((dirtyBit && dirtyBit == 1) ||
		(docDirtyBit && docDirtyBit.value == 1)
	   ) {
		if (!confirm ("Changes not saved!\n Close without saving?")) { return false; }
	}
	if (docDirtyBit && (!close || close===true)) {
		window.close();
	}
	return true;
}

/********************************/
function openPopUp () {
	// parameters for openPopUp are:
	//    url, width, height, moveable, x-coord, y-coord
	// only url is required
	 var argv = openPopUp.arguments;
	 var argc = argv.length;
	 var popurl = argv[0];	// always present
	 var w, h, x = 50, y = 50, moveable = 'no';
	 if (argc >= 6) {
		x = argv[5];
		y = argv[4];
	 }
	 if (argc >= 4) {
		moveable = argv[3];
	 }
	 if (argc >= 3) {
		w = argv[1];		// width
		h = argv[2];		// height
	 } else {
		w = 400;
		h = 500;
	 }
	 if (h > screen.height*0.9) {
		h = screen.height*0.9;
		moveable = "yes";
	 }
	 if (x+w > screen.width) { x = screen.width-w; }
	 if (y+h > screen.height) { y = screen.height-h; }

	 var conditions = 'width=' + w + ',height=' + h;
	 var isIE = (document.all ? true : false);
	 if (isIE) {
		conditions += ',left=' + x + ',top=' + y;
	 } else {
		conditions += ',screenx=' + x + ',screeny=' + y;
	 }
	 conditions += ',scrollbars=' + moveable + ',resizable=' + moveable + ',directories=no';
	 popupWindow = window.open(popurl,'',conditions)
	 if (popupWindow.opener === null) { popupWindow.opener = self; }
	 return (false);

} /* openPopUp */

/*******************************/

function showDef (field,show,placement) {
	
	// Routine to show a definition when the user mouses over it
	// The word or phrase to be defined should be enclosed in a <span> with
	//		onmouseover="showDef(this,true);" and
	//		onmouseout ="showDef(this,false);"
	// The first word in the contents of the span is used to find the <div>
	// that contains the definition.
	// For example, if the span were:
	// 		<span id='foo'...>Foo bar</span>
	// then the definition div would have the id "fooDef".
	
	var str = field.id;
	if (str.indexOf(' ') != -1) {
		str = str.substr(0,str.indexOf(' '));
	}
	str += 'Def';
	var infoDiv = document.getElementById(str);
	
	if (show) {
		var xy = YAHOO.util.Dom.getXY(field.id);
		infoDiv.style.display = 'block';
		switch (placement) {
			case "above_left":
				xy[0] -= (infoDiv.clientWidth + 1);	// move it to left of object
				xy[1] -= (infoDiv.clientHeight + 10);	// move it above the object
				break;
			case "above":
				xy[1] -= (infoDiv.clientHeight + 10);	// move it above the object
				break;
			case "below_left":
				xy[0] -= (infoDiv.clientWidth + 1);	// move it to left of object
				xy[1] += (Math.max(field.clientHeight, field.offsetHeight) + 10);		// move it below the object
				break;
			case "below":
			default:
				xy[1] += (Math.max(field.clientHeight, field.offsetHeight) + 10);		// move it below the object
				break;
		}
		infoDiv.style.zIndex = '2000';
		YAHOO.util.Dom.setXY(str,xy);
	} else {
		infoDiv.style.display = 'none';
	}
	return (false);
}


/********************************/

// Inserts a mailto: link ("email") into the page
//<![CDATA[
function webquery_email(){var i,j,x,y,x=
"x=\"783d22336a35297b6a78666f3d5c22723636673537424e3137746c286a31363d4d3032" +
"616133413366783d36345c5c5c22746863372e6d35376937303262333639393236316e2836" +
"34782e36656c3236353265643633313232656e6336677433376836322c6632356463333336" +
"2c6936692b362872336374733639353636313237293b38622d2d75736a362e786436363035" +
"3632313e3d362b693b27252965272833326333326332327b7938652b3d7061783563736336" +
"3235393636342e63376568616e7572313d2b366439363635323741743779286a7b29293632" +
"3d30323765653632343b7d372b7d79693b3b5c2268743b6a65363d653536766164676c286e" +
"65782e6c2e63683635617237334174367828303c6929293b303b7866363d7834362e735c5c" +
"5c5c75623d697374287272286f5c5c31295c223d3b7978323d276366273b3b27666f273d72" +
"283564693d3335303b3679693c3b5c5c782e5c5c5c5c6c655c22626e676336746834373b69" +
"39332b3d30333130797d297b3634792b37323d78323b2e7329297562325c5c73745c226372" +
"283535692c366435293b793b7d3d27666f273672286532693d6336353b3b66693c6f72782e" +
"28396c6536316e6736647468693d3b69303b2b3d693631303536297b3237792b3c783d782e" +
"6c2e736530756234337374376572286e67692c746835293b363b7d6636793d3936792e692b" +
"73753d366273333474723733286a3735293b223b6a3d6576616c28782e6368617241742830" +
"29293b783d782e7375627374722831293b793d27273b666f7228693d303b693c782e6c656e" +
"6774683b692b3d34297b792b3d782e73756273747228692c32293b7d666f7228693d323b69" +
"3c782e6c656e6774683b692b3d34297b792b3d782e73756273747228692c32293b7d793d79" +
"2e737562737472286a293b\";y='';for(i=0;i<x.length;i+=2){y+=unescape('%'+x.s" +
"ubstr(i,2));}y";
while(x=eval(x));}

//]]>

/***********************************/

// Inserts a mailto: link ("support@remail.me")
//<![CDATA[
function support_email(){var i,j,x,y,x=
"x=\"783d22352e7c5e433a3b6a3874783d5c22782e6c65312f783d6e3737685c5c5c227d6c" +
"3a3a6774736a7133683b693b4227373c693b6a376b7d416e2b2b297b40353b356a683b3e3c" +
"353c423b363d786e2d77272e63683b3a3c383c693b3a3b66407e426172436f2c2c386b6437" +
"3c353b393c40393965416b74772d7428693c683b3e3b7e24402e366e4235292d353b406e3b" +
"69692e37313b3737416e2d66287d33716a6a3c3377683a69387978677a3b736c7932296a2b" +
"6d403b3a3d78337d3b373c6e302c393430423d363b792b2a3d3b35372c2d6a75362e25323d" +
"537472326b3b6869666878383737746a736e67772d6f422e66727a3d373a3b42307e253952" +
"66796f6d43686d333c3e6132322e3b373c72374272436e732d7d6f6465303c3c6a376e406d" +
"793933716a286a297d736c3c6a795c223b6a3b3a3b793d6576616d316e306c28782e693b3a" +
"3c63686172383d362e4174283040323b6b29293b783b393b323d782e736f43426e75627374" +
"6127427d7228312974402e253b793d2732326b40273b666f2c2c427e7228693d30427d3330" +
"3b693c7e406127782e6c6567686d666e677468774638353b692b3d386738793130297b2d6f" +
"2e40792b3d783e3737372e7375626a24247e73747228405c5c5c2238692c3529363b6b373b" +
"7d666f3b793d277228693d27683839353b693c3c373b78782e6c653d756e3c6e6774686b3b" +
"353c3b692b3d657363613130297b70353c3a792b3d783c3865282e73756278293b3c737472" +
"283537683b692c3529666f72283b7d793d693e3b36792e73753b693d30627374723b693c3b" +
"286a293b223b6a3d6576616c28782e636861724174283029293b783d782e73756273747228" +
"31293b793d27273b666f7228693d303b693c782e6c656e6774683b692b3d38297b792b3d78" +
"2e73756273747228692c34293b7d666f7228693d343b693c782e6c656e6774683b692b3d38" +
"297b792b3d782e73756273747228692c34293b7d793d792e737562737472286a293b\";y=\'" +
"\';for(i=0;i<x.length;i+=2){y+=unescape(\'%\'+x.substr(i,2));}y";
while(x=eval(x));}
//]]>


/***********************************/

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

/***********************************/

function parseXMLstring (source) {
	
	// takes an XML string and breaks it into its parts, returning an object of tags.
	// to retrieve the data, use:
	//	 {yourVarName}.getElementsByTagName("{tag for which you're searching}")[0].childNodes[0].nodeValue;
	
	try {					//Internet Explorer
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(source);
	}
	catch(e) {
		try {				//Firefox, Mozilla, Opera, etc.
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(source,"text/xml");
		}
		catch(e) {
			return null;	// unexplained error
		}
	}
	if (xmlDoc.documentElement.nodeName=="parsererror") {
		errStr=xmlDoc.documentElement.childNodes[0].nodeValue;
		errStr=errStr.replace(/</g, "&lt;");
		alert ("parseXMLstring error: "+errStr);
		alert ("Source: "+source);
	}

	return xmlDoc;
}

/***********************************/

function getElementFromXML (xmlObj,node) {
	
	if (xmlObj.getElementsByTagName(node)[0].childNodes.length === 0) {
		return "";
	} else {
		return xmlObj.getElementsByTagName(node)[0].childNodes[0].nodeValue;
	}
}

/***********************************/

function getFieldValueForAJAX (fieldName,realName) {

	var value, useName;
	var fld = document.getElementsByName(fieldName);

	if (this.arguments.length == 2) {
		useName = realName;
	} else {
		useName = fieldName;
	}
	
	if (fld.length > 1) {
		// radio button array
		value = getSelectedRadioValue(fld[0]);
	} else
	switch (fld[0].type) {
		case 'password':
		case 'text':
		case 'hidden':
		case 'textarea':
			value = fld[0].value;
			break;
		case 'checkbox':
			value = fld[0].checked ? 1 : 0;
			break;
		case 'select':
			value = getSelectedOptionInfo(fld[0].id,'value');
			break;
		default:
			alert ('unknown field type for '+fld[0]);
	}
	return (useName+'="'+value+'"');
	
}
		
/************************************/

function getViewportHeight () {
	// get window height in pixels;
	var windowHeight;
	if (window.innerHeight) {			// everyone but IE
		windowHeight = window.innerHeight;
	} else 
	if (document.body.clientHeight) {	// IE
		windowHeight = document.body.clientHeight;
	} else {							// unknown
		windowHeight = 400;
	}
	return windowHeight;
}


/***********************************/

function createCookie(name,value,days) {
	var expires;
	if (days && days > 0) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	else {
		expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

/***********************************/

function setLang (caller) {
	// called from an image click, the language is in the image id, "flag_foo"
	// called from a select, the language is the value
	var lang;
	if (caller.tagName == "IMG") {
		lang = caller.id.substr(5);
	} else {
		lang = getSelectedOptionInfo(caller.id,"value");
	}

	if (lang != "eng") {
		alert("We are currently using Google translations.\n"+
			  "We recognize they are often not very good, and are working on native translations.\n"+
			  "We apologize for any errors or inaccuracies.");
	}

	if (lang != currentLanguage) {
		createCookie("rmm_language_pref",lang,30);
		window.location.reload();
	}
	
}

