
function str_repeat(str, repeat) { 
   var output = ''; 
   for (var i = 0; i < repeat; i++) { 
      output += str; 
   }
   return output; 
}


function print_r(obj, indent, depth) { 
   var ws = '    '; //four whitespaces 
   var output = '';
        
   indent = (!indent) ? 0 : indent; 
   depth  = (!depth)  ? 0 : depth;
       
   if (depth > 10) { 
      return str_repeat(ws, indent) + '*Maximum Depth Reached*\n'; 
   }
        
   if (typeof(obj) == 'object') { 
      output += (indent == 0) ? typeof(obj) + '\n(\n' : ''; 
      indent++; 
      var child = ''; 
      for (var key in obj) { 
         try { 
            child = obj[key]; 
         } catch (e) { 
            child = '*Unable To Evaluate*'; 
         }
              
         output += str_repeat(ws, indent) + '['+key+'] => '; 
         if (typeof(child) == 'object') { 
            indent++; 
            output += typeof(child) + '\n'; 
            output += str_repeat(ws, indent) + '(\n'; 
            output += print_r(child, indent, depth+1); 
            output += str_repeat(ws, indent) + ')\n'; 
            indent--; 
         } else { 
            output += child + '\n'; 
         }
      }

      indent--; 
      output += (indent == 0) ? ')\n' : ''; 

      return output;

   } else { 
      return str_repeat(ws, indent) + obj + '\n'; 
   }
}


function cleanForm(theform) {

   for (i=0; i < theform.elements.length; i++) {
      if (theform.elements[i].tagName == "INPUT") {
         switch (theform.elements[i].type) {
            case "text":
            case "hidden":
               theform.elements[i].value = cleanOutHtml(theform.elements[i].value);
         }
      }

      if (theform.elements[i].tagName == "TEXTAREA") {
         theform.elements[i].value = cleanOutHtml(theform.elements[i].value);
      }
   }
}


function cleanOutHtml(text) {

   var matchTag = /<(?:.|\s)*?>/g;

   return text.replace(matchTag, "");
}

function getInternetExplorerVersion() {
   var rv = -1;
       
   if (navigator.appName == 'Microsoft Internet Explorer') {
      var ua = navigator.userAgent;
      var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");

      if (re.exec(ua) != null) rv = parseFloat( RegExp.$1 );
   }
   return rv;
}

function checkVersion() {
   var ver = getInternetExplorerVersion();

   if ((-1 < ver) && (ver < 7.0)) {
      alert("This Option does not work well with Internet Explorer 6 or less.\n"+
            "Please upgrade to Version 7 or use another browser.");
   }
}


function isArray(testObject) {
   var ver = getInternetExplorerVersion();

   if ((ver > -1) && (ver < 7.0)) {
      return testObject && typeof testObject === 'object' && typeof testObject.length === 'number';
   }

   return (testObject                      &&
	  (typeof testObject === 'object') &&
	  (typeof testObject.length === 'number'));
}


function URLEncode(plaintext) {

	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...

	var SAFECHARS = "0123456789" +			// Numeric
			"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
			"abcdefghijklmnopqrstuvwxyz" +
			"-_.!~*'()";			// RFC2396 Mark characters

	var HEX = "0123456789ABCDEF";

	var encoded = "";

	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";			// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};


function URLDecode(encoded) {
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.

   var HEXCHARS = "0123456789ABCDEFabcdef";

   var plaintext = "";
   var i = 0;

   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2)
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while

    return plaintext;
};

function CheckPasswordStrength(password) {

    strength = 0;

    if (password.value.length < 8) strength = -1;
    if (password.value.length < 7) strength = -2;
    if (password.value.length < 6) strength = -3;
    if (password.value.length < 5) return 0;

    if (password.value.match(/^.*[a-z].*$/))                       strength += 1; // very weak
    if (password.value.match(/^.*[A-Z].*$/))                       strength += 1; // weak
    if (password.value.match(/^.*[0-9].*$/))                       strength += 2; // average
    if (password.value.match(/^.*[#$%&@?!\-_].*$/))                strength += 2; // strong
    if (password.value.match(/^.*[+=.*|:;^`,~(){}\[\]<>\\\/].*$/)) strength += 3; // very strong

    return strength;
}


//
// Fix PNG Images in IE
//

function correctPNG() {
    for (var i = 0; i < document.images.length; i++) {
	var img     = document.images[i];
	var imgName = img.src.toUpperCase();

	if (imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
	    var imgID    = (img.id)        ? "id='"    + img.id        + "' " : "";
	    var imgClass = (img.className) ? "class='" + img.className + "' " : "";
	    var imgTitle = (img.title)     ? "title='" + img.title     + "' " : "title='" + img.alt + "' ";
	    var imgStyle = "display:inline-block;"     + img.style.cssText;

	    if (img.align == "left")    imgStyle = "float:left;"  + imgStyle;
	    if (img.align == "right")   imgStyle = "float:right;" + imgStyle;
	    if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;

	    var strNewHTML = "<span " + imgID + imgClass + imgTitle
		+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
		+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";

	    img.outerHTML = strNewHTML;
	    i = i - 1;
	}
    }
}

//
// Popup Window Functions
//

function dialog(mypage, w, h, scroll, menu) {
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=yes,menubar='+menu;

  if (menu == 'yes') {
    winprops += ',toolbar=yes,locationbar=yes';
  }

  win = window.open(mypage, 'Dialog', winprops);

  if        ((window.navigator.appName.indexOf("Microsoft") ==  0 ) & (window.navigator.appVersion.substring(0,1) == "4")) {
      damnBrowser = 1;
  } else if ((window.navigator.appName.indexOf("Netscape")  ==  0 ) & (window.navigator.appVersion.substring(0,1) == "4")) {
      damnBrowser = 1;
  } else if (parseFloat(navigator.appVersion.substring(0,navigator.appVersion.indexOf(""))) >= 3.0) {

      if (!( (window.navigator.appName.indexOf("Microsoft") == "0") & (window.navigator.userAgent.indexOf("Mac")  >=  0 ) &
             (parseFloat(navigator.appVersion.substring(0,navigator.appVersion.indexOf(""))) < 4.0))) {
         win.focus();
      }
   }
}

//if (window.navigator.appName.indexOf("Microsoft") == 0) { window.attachEvent("onload", correctPNG); }


