// detect browser
var bname = navigator.appName;	
var bversion = navigator.appVersion;


function showHelpText(input, text)
{
	if(input.value == '')
		input.value = text;
}

function hideHelpText(input, text)
{
	if(input.value == text)
		input.value = ''; 
}

function supportDOM() {
	return document.getElementById && ( window.attachEvent || window.addEventListener);
}

function ie501win() {
	return document.all && !document.fireEvent && !window.opera;
}

function addEvent(obj,evType,fn) {
  if(obj.addEventListener){
    obj.addEventListener(evType,fn,false);
    return true;
  }else if(obj.attachEvent){
    return obj.attachEvent('on'+evType,fn);
  }else if(document.all&&(!document.mimeType)){
    eval('obj.on'+evType+'=fn');
	return true;
  }else{
	return false;
  }
}

function removeEvent(obj,evType,fn){
  if(obj.addEventListener){
    obj.removeEventListener(evType,fn,false);
    return true;
  }else if(obj.attachEvent){
    return obj.detachEvent('on'+evType,fn);
  }else if(document.all&&(!document.mimeType)){
    eval('obj.on'+evType+'=null');
	return true;
  }else{
	return false;
  }
}

// NOT USED IN NEW DESIGN
// adjust contentcontainer DIVs (as found in DefaultFramework.ascx) so that it fills out vertically nicely for screens larger than 1024*768
function adjustheight() {		
	// make sure getElementById works
	if(supportDOM() && !ie501win() ) {
		if (document.getElementById('contentcontainer') ) { // regular page
			var contentcontainer = document.getElementById('contentcontainer');
			if (bname.search(/microsoft/i) == 0) { // IE
				if( bversion.indexOf('MSIE 7') > -1) { // IE7 can use min-height properly so let's use that instead
					contentcontainer.style.minHeight = screen.height - 250 + "px";
				}
				else { // older IE is crap with min-height, it interprets height as min-height...
					// use the screen height minus a few pixels for toolbars, status bars et c
					contentcontainer.style.height = screen.height - 280 + "px";
				}
			}
			else { // Firefox et c - bigger toolbar so less pixels
				contentcontainer.style.minHeight = screen.height - 300 + "px";				
			}
		}				
	}
}

function createbookmorelink() {
	// get all inputs on the page
	var inputs = document.getElementsByTagName("input");
	var c = '';
	
	// get the booking short formats from the checkboxes
	for(var i=0; i<inputs.length; i++) {		
		// if it's a checkbox and it's checked, the visitor wants to order that one
		if( inputs[i].type == "checkbox" && inputs[i].checked == true ) {			
			var temp = new Array();			
			temp = inputs[i].id.split('|');
			c += temp[temp.length-1] + "__";
		}		
	}
	
	var url = '' // URL back to course booking page
	var qs = ''; // possible addition to query string
	
	if( c.length > 0 ) { // did the visitor actually check any boxes?
		// remove last __, (we couldn't know when it occurred in the for loop since we most probably had more inputs than checkboxes in the inputs array)
		c = c.substring(0,c.length-2);	
		
		// "encrypt it"
		var s_enc = "";
		for(i=0; i<c.length; i++) {
			s_enc += String.fromCharCode(c.charCodeAt(i)^7);
		}
		
		// create the query string addition
		qs = '&c=' + s_enc;
	}

	// create the URL back to the main course list		
	var completeURL = document.URL.split('&');
	url = completeURL[0];	
	
	// redirect, and if any checkboxes were checked, save that content	
	if( qs == '' )
		window.location = url;
	else
		window.location = url+qs;	
}

function URLEncode( sIn ) {
	var SAFECHARS = "0123456789" +
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = sIn;
	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) {
				// Unicode cannot be URL encoded, just put a space there
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	}
	return encoded;	
}

addEvent(window,'load',decryptmails);
// addEvent(window,'load',adjustheight);
