/* 
 * Javascript function to make new-window links xhtml compliant
 * http://www.sitepoint.com/article/standards-compliant-world/3
 */

//
String.prototype.trim = function() 
{
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};

//
function externalLinks() {
   if (!document.getElementsByTagName) return;
   var anchors = document.getElementsByTagName("a");
   for (var i=0; i<anchors.length; i++) {
      var anchor = anchors[i];
      if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
         anchor.target = "_blank";
   }
}

//
function goToURL(selectbox)
{
	window.location.href='/admin/pages/edit/?id='+selectbox.options[selectbox.selectedIndex].value;
}

//
function toggle(el)
{
	document.getElementById('about').className = 'hide';
	document.getElementById('contact').className = 'hide';
	
  	var element = document.getElementById(el);
	element.className = (element.className == 'show') ? 'hide' : 'show';
}

//
function popup(page)
{
	window.open("",page,"width=680,height=600,resizable,scrollbars=yes");	
}

//
function switchAll(state) {
	var all = document.getElementsByTagName('input');
	for (var i = 0; i < all.length; i++) { 
		var type = all[i].getAttribute('type'); 
		if (type == 'checkbox') {
			all[i].checked = state;
		} else if ((type == 'hidden') && (all[i].getAttribute('name') == 'rights[all][all]')) {
			all[i].value = state;
		}
	}
}

//
function checkState(element) {
	if (element.checked == false) {
		var all = document.getElementsByTagName('input');
		for (var i = 0; i < all.length; i++) { 
			var type = all[i].getAttribute('type');
			var name = all[i].getAttribute('name');
			 if ((type == 'hidden') && (name == 'rights[all][all]')) {
				all[i].value = 'false';
			}
		}			
	}
}

//
function URLEncode(str)
{
	// 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 < str.length; i++ ) 
	{
		var ch = str.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;
}

//
window.onload = externalLinks;