var const_manditory_advice = '<font color="red">Fields marked with an asterisk (*) are mandatory</font>';

function init(sPage) {

	// image preloaders - including these should stop the 
	// flicker on the buttons but will increase the screen
	// load time a fraction
	pic1 = new Image(100,25); 
  	pic1.src= 'interface/icon25_login_over.jpg'; 

	pic2 = new Image(100,25); 
  	pic2.src= 'interface/icon25_login.jpg'; 

	pic3 = new Image(100,25); 
  	pic3.src= 'interface/icon32_go02.jpg'; 
	
	pic4 = new Image(100,25); 
  	pic4.src= 'interface/icon32_go01.jpg'; 


	pic5 = new Image(100,25); 
  	pic5.src= 'interface/btn_submit_over.jpg'; 

	pic6 = new Image(100,25); 
  	pic6.src= 'interface/btn_submit.jpg'; 


	try {
		_obj('but_logon').onmouseover 	= function() {_obj('but_logon').style.backgroundImage = 'url("interface/icon25_login_over.jpg")'; }
		_obj('but_logon').onmouseout	= function() {_obj('but_logon').style.backgroundImage = 'url("interface/icon25_login.jpg")'; }
	} catch(e) {}

	try {
		_obj('next_news').onmouseover 	= function() { _obj('next_news').src = 'interface/icon24_news_arrow_over.jpg' }
		_obj('next_news').onmouseout 	= function() { _obj('next_news').src = 'interface/icon24_news_arrow.jpg' }
	} catch(e) {}

	try {
		_obj('prev_news').onmouseover 	= function() { _obj('prev_news').src = 'interface/icon23_news_arrow_over.jpg' }
		_obj('prev_news').onmouseout 	= function() { _obj('prev_news').src = 'interface/icon23_news_arrow.jpg' }
	} catch(e) {}


	try {
		// _obj('txt_quick_search').onkeyup 	= fnKeyupSearch
		_obj('but_quick_search').onclick 	= fnKeyupSearch
		_obj('txt_quick_search').onkeydown 	= fnKeyDownSearch
		_obj('txt_quick_search').onfocus 	= fnBlankSearchText
		_obj('txt_quick_search').disabled 	= false;
		_obj('but_quick_search').disabled 	= false;
		_obj('but_quick_search').className 	= 'but_go_norm';	
		_obj('but_quick_search').style.backgroundImage = 'url(interface/icon32_go01.jpg)'
	} catch(e) {}

	try {
		_obj('but_quick_search').onmouseover 	= function() { _obj('but_quick_search').style.backgroundImage = 'url(interface/icon32_go02.jpg)' }
		_obj('but_quick_search').onmouseout 	= function() { _obj('but_quick_search').style.backgroundImage = 'url(interface/icon32_go01.jpg)' }
	} catch(e) {}


	try {
		_obj('earl_search_controls_browse_dropdown').disabled = false;
	} catch(e) {}

	try {
		_obj('href_earl_search_controls_view_all').disabled = false;
	} catch(e) {}



	try {
		_obj('but_enquiry_general').onmouseover = function() { _obj('but_enquiry_general').style.backgroundImage = 'url(interface/btn_submit_over.jpg)' }
		_obj('but_enquiry_general').onmouseout 	= function() { _obj('but_enquiry_general').style.backgroundImage = 'url(interface/btn_submit.jpg)' }
	} catch(e) {}
	try {
		_obj('but_enquiry_demo').onmouseover = function() { _obj('but_enquiry_demo').style.backgroundImage = 'url(interface/btn_submit_over.jpg)' }
		_obj('but_enquiry_demo').onmouseout  = function() { _obj('but_enquiry_demo').style.backgroundImage = 'url(interface/btn_submit.jpg)' }
	} catch(e) {}
	try {
		_obj('but_update_profile').onmouseover = function() { _obj('but_update_profile').style.backgroundImage = 'url(interface/btn_submit_over.jpg)' }
		_obj('but_update_profile').onmouseout  = function() { _obj('but_update_profile').style.backgroundImage = 'url(interface/btn_submit.jpg)' }
	} catch(e) {}
	try {
		_obj('but_earl_feedback').onmouseover = function() { _obj('but_earl_feedback').style.backgroundImage = 'url(interface/btn_submit_over.jpg)' }
		_obj('but_earl_feedback').onmouseout  = function() { _obj('but_earl_feedback').style.backgroundImage = 'url(interface/btn_submit.jpg)' }
	} catch(e) {}

	try {
		_obj('but_sub_corporate').onmouseover = function() { _obj('but_sub_corporate').style.backgroundImage = 'url(interface/btn_submit_over.jpg)' }
		_obj('but_sub_corporate').onmouseout  = function() { _obj('but_sub_corporate').style.backgroundImage = 'url(interface/btn_submit.jpg)' }
	} catch(e) {}
	try {
		_obj('but_sub_educational').onmouseover = function() { _obj('but_sub_educational').style.backgroundImage = 'url(interface/btn_submit_over.jpg)' }
		_obj('but_sub_educational').onmouseout  = function() { _obj('but_sub_educational').style.backgroundImage = 'url(interface/btn_submit.jpg)' }
	} catch(e) {}
	try {
		_obj('but_ask_expert').onmouseover = function() { _obj('but_ask_expert').style.backgroundImage = 'url(interface/btn_submit_over.jpg)' }
		_obj('but_ask_expert').onmouseout  = function() { _obj('but_ask_expert').style.backgroundImage = 'url(interface/btn_submit.jpg)' }
	} catch(e) {}
	try {
		_obj('but_earl_cannotfind').onmouseover = function() { _obj('but_earl_cannotfind').style.backgroundImage = 'url(interface/btn_submit_over.jpg)' }
		_obj('but_earl_cannotfind').onmouseout  = function() { _obj('but_earl_cannotfind').style.backgroundImage = 'url(interface/btn_submit.jpg)' }
	} catch(e) {}



	// general enquiry form
	try {
		_obj('but_enquiry_general').disabled = true;
		_obj('txt_enquiry_general_comments').onkeyup 	  = fnTestForm_General_1
		_obj('txt_enquiry_general_phone').onkeyup 	  = fnTestForm_General_2
		_obj('txt_enquiry_general_email_address').onkeyup = fnTestForm_General_3
		_obj('txt_enquiry_general_name').onkeyup 	  = fnTestForm_General_4
		fnTestForm_General_1();
	} catch(e) {}

	// extended demo enquiry form
	try {
		_obj('but_enquiry_demo').disabled = true;
		_obj('txt_enquiry_demo_comments').onkeyup 	= fnTestForm_Extended_1
		_obj('txt_enquiry_demo_phone').onkeyup 	  	= fnTestForm_Extended_2
		_obj('txt_enquiry_demo_email_address').onkeyup 	= fnTestForm_Extended_3
		_obj('txt_enquiry_demo_name').onkeyup 	  	= fnTestForm_Extended_4
		fnTestForm_Extended_1();
	} catch(e) {}


	// profile / register
	try {
		_obj('but_update_profile').disabled = true;
		_obj('txt_profile_name').onkeyup 		= fnTestForm_Profile_1
		_obj('txt_profile_email_address').onkeyup 	= fnTestForm_Profile_2
		_obj('txt_profile_company').onkeyup 		= fnTestForm_Profile_3
		_obj('txt_profile_password').onkeyup 		= fnTestForm_Profile_4
		fnTestForm_Profile_1();
	} catch(e) {}

	// Corporate Pricing
	try {
		_obj('but_sub_corporate').disabled = true;
		_obj('txt_sub_corporate_name').onkeyup 		= fnTestForm_Corporate_Price_1
		_obj('txt_sub_corporate_email_address').onkeyup = fnTestForm_Corporate_Price_2
		_obj('txt_sub_corporate_company').onkeyup 	= fnTestForm_Corporate_Price_3
		_obj('txt_sub_corporate_comments').onkeyup 	= fnTestForm_Corporate_Price_4
		fnTestForm_Corporate_Price_1();
	} catch(e) {}

	// Educational Pricing
	try {
		_obj('but_sub_educational').disabled = true;
		_obj('txt_sub_educational_name').onkeyup 		= fnTestForm_educational_Price_1
		_obj('txt_sub_educational_email_address').onkeyup	= fnTestForm_educational_Price_2
		_obj('txt_sub_educational_company').onkeyup 		= fnTestForm_educational_Price_3
		_obj('txt_sub_educational_comments').onkeyup 		= fnTestForm_educational_Price_4
		fnTestForm_educational_Price_1();
	} catch(e) {}


	// Ask Expert
	try {
		_obj('but_ask_expert').disabled = true;
		_obj('txt_ask_expert_name').onkeyup 		= fnTestForm_ask_expert_1
		_obj('txt_ask_expert_email_address').onkeyup	= fnTestForm_ask_expert_2
		_obj('txt_ask_expert_comments').onkeyup 	= fnTestForm_ask_expert_3
		fnTestForm_ask_expert_1();
	} catch(e) {}

	// Can not find
	try {
		_obj('but_earl_cannotfind').disabled = true;
		_obj('txt_cannotfind_name').onkeyup 		= fnTestForm_cannotfind_1
		_obj('txt_cannotfind_email').onkeyup 		= fnTestForm_cannotfind_2
		_obj('txt_cannotfind_fb1').onkeyup 		= fnTestForm_cannotfind_3
		fnTestForm_cannotfind_1();
	} catch(e) {}

	sWin = window.location.toString().replace('?gclid=CP-L5IyQyowCFQXklAodCQyAJQ','')

	if (sWin.indexOf('view_all=true') > -1) {
		try {
			show_all_nodes_in_tree()
		} catch(e) {}

		if (sWin.indexOf('jumpsco=') > -1) {
			sTempJump = sWin.split('jumpsco=')
			expand_shrink_sco(sTempJump[1])
			try {
				_obj('sco_'+(parseInt(sTempJump[1])+1).toString()).focus()
			} catch(e) {
				_obj('sco_'+(parseInt(sTempJump[1])).toString()).focus()
			}

			if (sTempJump[1] = 1) {
expand_shrink_sco(sTempJump[1])
			try {
				_obj('sco_'+(parseInt(sTempJump[1])+1).toString()).focus()
			} catch(e) {
				_obj('sco_'+(parseInt(sTempJump[1])).toString()).focus()
			}
			}
		}


	}

	if (window.location.toString().indexOf('search=') > -1) {
		try {
			sTemp = sWin.split('search=')
			fnRunSearch(sTemp[1])
			_obj('txt_quick_search').value = sTemp[1];
		} catch(e) {}
	}

	if (window.location.toString().indexOf('browse=') > -1) {
		try {
			sTemp = sWin.split('browse=');
			_obj('earl_search_controls_browse_dropdown').value = sTemp[1];
			fnEARLSearch('earl_search_controls_browse_dropdown')

		} catch(e) {}
	}

}

function fnLog(sParam) {

	sWin = window.location.toString().replace('?gclid=CP-L5IyQyowCFQXklAodCQyAJQ','')

	fnInitReqObj();
	var sURL = sWin.split('earl.aspx');
	sURL = sURL[0] + 'earl_log_support.aspx?' + sParam + '=true&indiv=' + sCurrentUser;

	if (oAJAXRequest != null ) {
		oAJAXRequest.open("GET", sURL, true);
      		oAJAXRequest.send(null);
	}
}

function fnTestForm_cannotfind_1() { fnTestForm_cannotfind('txt_cannotfind_name',100,'name') }
function fnTestForm_cannotfind_2() { fnTestForm_cannotfind('txt_cannotfind_email',100,'email') }
function fnTestForm_cannotfind_3() { fnTestForm_cannotfind('txt_cannotfind_fb1',1000,'free') }
function fnTestForm_cannotfind(sElementId, iCharLimit, sFormat) {

	if (sFormat == 'free') { 
		_obj('spn_' + sElementId).innerHTML = _obj(sElementId).value.length.toString() + ' of ' + iCharLimit.toString() + ' characters.'
	}

	if ((_obj('txt_cannotfind_fb1').value != '') && (_obj('txt_cannotfind_email').value != '') && (_obj('txt_cannotfind_name').value != '')) {
		_obj('but_earl_cannotfind').disabled = false;
		_obj('spn_earl_cannotfind').innerHTML = '';
	} else {
		_obj('but_earl_cannotfind').disabled = true;
		_obj('spn_earl_cannotfind').innerHTML = const_manditory_advice;
	}
}

function fnTestForm_ask_expert_1() { fnTestForm_ask_expert('txt_ask_expert_name',100,'name') }
function fnTestForm_ask_expert_2() { fnTestForm_ask_expert('txt_ask_expert_email_address',100,'email') }
function fnTestForm_ask_expert_3() { fnTestForm_ask_expert('txt_ask_expert_comments',1000,'free') }
function fnTestForm_ask_expert(sElementId, iCharLimit, sFormat) {

	if (sFormat == 'free') { 
		_obj('spn_' + sElementId).innerHTML = _obj(sElementId).value.length.toString() + ' of ' + iCharLimit.toString() + ' characters.'
	}

	if ((_obj('txt_ask_expert_comments').value != '') && (_obj('txt_ask_expert_email_address').value != '') && (_obj('txt_ask_expert_name').value != '')) {
		_obj('but_ask_expert').disabled = false;
		_obj('spn_ask_expert').innerHTML = '';
	} else {
		_obj('but_ask_expert').disabled = true;
		_obj('spn_ask_expert').innerHTML = const_manditory_advice;
	}
}

function fnTestForm_educational_Price_1() { fnTestForm_educational_Price('txt_sub_educational_name',100,'name') }
function fnTestForm_educational_Price_2() { fnTestForm_educational_Price('txt_sub_educational_email_address',100,'email') }
function fnTestForm_educational_Price_3() { fnTestForm_educational_Price('txt_sub_educational_company',100,'name') }
function fnTestForm_educational_Price_4() { fnTestForm_educational_Price('txt_sub_educational_comments',1000,'free') }
function fnTestForm_educational_Price(sElementId, iCharLimit, sFormat) {

	if (sFormat == 'free') { 
		_obj('spn_' + sElementId).innerHTML = _obj(sElementId).value.length.toString() + ' of ' + iCharLimit.toString() + ' characters.'
	}

	if ((_obj('txt_sub_educational_name').value != '') && (_obj('txt_sub_educational_email_address').value != '') && (_obj('txt_sub_educational_company').value != '')) {
		_obj('but_sub_educational').disabled = false;
		_obj('spn_sub_educational').innerHTML = '';
	} else {
		_obj('but_sub_educational').disabled = true;
		_obj('spn_sub_educational').innerHTML = const_manditory_advice;
	}
}


function fnTestForm_Corporate_Price_1() { fnTestForm_Corporate_Price('txt_sub_corporate_name',100,'name') }
function fnTestForm_Corporate_Price_2() { fnTestForm_Corporate_Price('txt_sub_corporate_email_address',100,'email') }
function fnTestForm_Corporate_Price_3() { fnTestForm_Corporate_Price('txt_sub_corporate_company',100,'name') }
function fnTestForm_Corporate_Price_4() { fnTestForm_Corporate_Price('txt_sub_corporate_comments',1000,'free') }
function fnTestForm_Corporate_Price(sElementId, iCharLimit, sFormat) {

	if (sFormat == 'free') { 
		_obj('spn_' + sElementId).innerHTML = _obj(sElementId).value.length.toString() + ' of ' + iCharLimit.toString() + ' characters.'
	}

	if ((_obj('txt_sub_corporate_name').value != '') && (_obj('txt_sub_corporate_email_address').value != '') && (_obj('txt_sub_corporate_company').value != '')) {
		_obj('but_sub_corporate').disabled = false;
		_obj('spn_sub_corporate').innerHTML = '';
	} else {
		_obj('but_sub_corporate').disabled = true;
		_obj('spn_sub_corporate').innerHTML = const_manditory_advice;
	}
}


function fnTestForm_Profile_1() { fnTestForm_Profile('txt_profile_name',100,'name') }
function fnTestForm_Profile_2() { fnTestForm_Profile('txt_profile_company',100,'name') }
function fnTestForm_Profile_3() { fnTestForm_Profile('txt_profile_email_address',100,'email') }
function fnTestForm_Profile_4() { fnTestForm_Update('txt_profile_password',100,'name') }

function fnTestForm_Profile(sElementId, iCharLimit, sFormat) {

	if ((_obj('txt_profile_name').value != '') && (_obj('txt_profile_email_address').value != '') && (_obj('txt_profile_company').value != '') ) {
		_obj('but_update_profile').disabled = false;
		_obj('spn_update_profile').innerHTML = '';
	} else {
		_obj('but_update_profile').disabled = true;
		_obj('spn_update_profile').innerHTML = const_manditory_advice;
	}
}

function fnTestForm_Update(sElementId, iCharLimit, sFormat) {
	
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	
	if (parms[0] = "profile=true") {
			if ((_obj('txt_profile_name').value != '') && (_obj('txt_profile_email_address').value != '') && (_obj('txt_profile_company').value != '') && (_obj('txt_profile_password').value != '') ) {
				_obj('but_update_profile').disabled = false;
				_obj('spn_update_profile').innerHTML = '';
			} 
			else {
				_obj('but_update_profile').disabled = true;
				_obj('spn_update_profile').innerHTML = const_manditory_advice;
			}
	}
}

function fnTestForm_Extended_1() { fnTestForm_Extended('txt_enquiry_demo_comments',1000,'free') }
function fnTestForm_Extended_2() { fnTestForm_Extended('txt_enquiry_demo_phone',100,'phone') }
function fnTestForm_Extended_3() { fnTestForm_Extended('txt_enquiry_demo_email_address',100,'email') }
function fnTestForm_Extended_4() { fnTestForm_Extended('txt_enquiry_demo_name',100,'name') }
function fnTestForm_Extended(sElementId, iCharLimit, sFormat) {

	if (sFormat == 'free') { 
		_obj('spn_' + sElementId).innerHTML = _obj(sElementId).value.length.toString() + ' of ' + iCharLimit.toString() + ' characters.'
	}

	if ((_obj('txt_enquiry_demo_email_address').value != '') && (_obj('txt_enquiry_demo_name').value != '')) {
		_obj('but_enquiry_demo').disabled = false;
		_obj('spn_enquiry_demo').innerHTML = '';
	} else {
		_obj('but_enquiry_demo').disabled = true;
		_obj('spn_enquiry_demo').innerHTML = const_manditory_advice;
	}

}

function fnTestForm_General_1() { fnTestForm('txt_enquiry_general_comments',1000,'free') }
function fnTestForm_General_2() { fnTestForm('txt_enquiry_general_phone',100,'phone') }
function fnTestForm_General_3() { fnTestForm('txt_enquiry_general_email_address',100,'email') }
function fnTestForm_General_4() { fnTestForm('txt_enquiry_general_name',100,'name') }
function fnTestForm(sElementId, iCharLimit, sFormat) {

	if (sFormat == 'free') { 
		_obj('spn_' + sElementId).innerHTML = _obj(sElementId).value.length.toString() + ' of ' + iCharLimit.toString() + ' characters.'
	}

	if ((_obj('txt_enquiry_general_comments').value != '') && (_obj('txt_enquiry_general_email_address').value != '') && (_obj('txt_enquiry_general_name').value != '')) {
		_obj('but_enquiry_general').disabled = false;
		_obj('spn_enquiry_general').innerHTML = '';
	} else {
		_obj('but_enquiry_general').disabled = true;
		_obj('spn_enquiry_general').innerHTML = const_manditory_advice;
	}
}

function BlockAlpha() {
	// function blocks key codes
	var kc = window.event.keyCode;
	var ck = window.event.ctrlKey;
	var ak = window.event.altKey;
	var cancelKey = true;

	// allow numeric keys across top of the screen
	if (kc > 46 && kc < 58) { cancelKey = false; }

	// allow numeric keys on the keypad
	if (kc > 95 && kc < 106) { cancelKey = false; }

	// allow cursor keys
	if (kc > 36 && kc < 41) { cancelKey = false; }

	// backspace, delete and tab
	if (kc == 8 || kc == 46 || kc == 9) { cancelKey = false; }
	
	if (cancelKey == true) {
		try {
			window.event.keyCode = 7;
			event.cancelBubble = true;
			event.returnValue = false;
		} catch(e) {}
	}
 	return !cancelKey;
}

function LimitKeyboard() {

	// function blocks key codes
	var kc = window.event.keyCode;
	var ck = window.event.ctrlKey;
	var ak = window.event.altKey;
	var sk = window.event.shiftKey;
	var cancelKey = true;

	// allow alpha
	if (kc > 64 && kc < 91) { cancelKey = false; }

	// allow numeric keys across top of the screen
	if (kc > 46 && kc < 58) { cancelKey = false; }

	// allow numeric keys on the keypad
	if (kc > 95 && kc < 106) { cancelKey = false; }

	// allow cursor keys
	if (kc > 36 && kc < 41) { cancelKey = false; }

	// allow @
	if (sk && kc == 192) { cancelKey = false; }

	// allow _ and -
	if (kc == 189) { cancelKey = false; }

	// allow : and .
	if (kc == 190) { cancelKey = false; }

	// backspace, delete and tab
	if (kc == 8 || kc == 46 || kc == 9) { cancelKey = false; }
	
	if (cancelKey == true) {
		try {
			window.event.keyCode = 7;
			event.cancelBubble = true;
			event.returnValue = false;
		} catch(e) {}
	}

 	return !cancelKey;
}


document.onkeydown = BlockAltAndFunctionKeys
document.onkeyup = BlockAltAndFunctionKeys
function BlockAltAndFunctionKeys() {
	var ak = window.event.altKey;
	var kc = window.event.keyCode;

	var cancelKey = false;
	if ((kc == 18) || (kc == 113) || (kc == 114) || (kc == 115) || (kc == 116) || (kc == 36) || (kc == 37) || (kc == 39)) { cancelKey = true; }

	if (cancelKey == true) {
		try {
			window.event.keyCode = 7;
			event.cancelBubble = true;
			event.returnValue = false;
		} catch(e) {}
	}

	return !cancelKey;

}


function _obj(sId) { return document.getElementById(sId); }
function _hide(sId) { try {_obj(sId).style.visibility = 'hidden'} catch(e) { } }
function _show(sId) {
	try {
		_obj(sId).style.visibility = 'visible'
	} catch(e) { }
}

function fnEARLSub_Support(sLayerToShow) {

	_hide('earl_content_support_faq');
	_hide('earl_content_support_docs');
	_hide('earl_content_support_expert');
	_hide('divFAQ');
	_hide('divFAQNote');
	_show(sLayerToShow)
}




function fnHideAllDynamicLayers() {

	_hide('earl_content_list');
	_hide('earl_privacy_statement');
	_hide('earl_terms_statement');
	_hide('earl_site_map');
	_hide('earl_search_results');
	_hide('earl_about_us');
	_hide('earl_contact_us');
	_hide('earl_enquiry_form');	// general enquiry
	_hide('earl_enquiry_form_2');	// extended demo
	_hide('earl_support');
	_hide('divFAQ');
	_hide('divFAQNote');

	_hide('earl_content_support_faq');
	_hide('earl_content_support_docs');
	_hide('earl_content_support_expert');
	_hide('divFAQ');
	_hide('divFAQNote');

	// following layers may not exist as they 
	// are dynamically created by the server 
	// side code

	try { _hide('earl_register_updates') } catch(e) {}
	try { _hide('earl_register_updates') } catch(e) {}
	try { _hide('earl_feedback') } catch(e) {}
	try { _hide('earl_feedback_thanks') } catch(e) {}
	try { _hide('earl_features_benefits') } catch(e) {}
	try { _hide('earl_sample_customers') } catch(e) {}
	try { _hide('earl_content_sample') } catch(e) {}
	try { _hide('customerlist') } catch(e) {}
	try { _hide('earl_content_area_presentation') } catch(e) {}
	try { _hide('earl_multimedia') } catch(e) {}
	try { _hide('earl_tour') } catch(e) {}
	try { _hide('earl_feedback_thanks') } catch(e) {}
	try { _hide('earl_cannotfind') } catch(e) {}
	try { _hide('earl_cannotfind_thanks') } catch(e) {}
	try { _hide('earl_enquiry_form_thanks') } catch(e) {}
	try { _hide('earl_enquiry_form_2_thanks') } catch(e) {}
	try { _hide('earl_feedback_thanks') } catch(e) {}
	try { _hide('earl_subscribe_educational_thanks') } catch(e) {}
	try { _hide('earl_subscribe_corporate_thanks') } catch(e) {}
	try { _hide('earl_regsiter_thanks') } catch(e) {}
	try { _hide('earl_profile_update_thanks') } catch(e) {}
	try { _hide('earl_subscribe') } catch(e) {}
	try { _hide('earl_ask_an_expert') } catch(e) {}


	try { _hide('earl_web_links') } catch(e) {}

	try { _hide('earl_report') } catch(e) {}

}


function fnBlankSearchText() {

	// 12 Mar - DOH advised that the text Quick Search should
	// be in the text field, and when the user puts focus on
	// to the field it clears itself

	fnLog('search')

	if (_obj('txt_quick_search').value == 'Quick Search') {
		_obj('txt_quick_search').value = '';
	}

}

function fnKeyupSearch() {

	sWin = window.location.toString().replace('?gclid=CP-L5IyQyowCFQXklAodCQyAJQ','')

	if (sWin.indexOf("content_list") > -1) {
		sSearchTxt = _obj('txt_quick_search').value;
		if (sSearchTxt.length > 1) {
			if ((sSearchTxt.toLowerCase() != 'the') && (sSearchTxt.toLowerCase() != 'or') && (sSearchTxt.toLowerCase() != 'that') && (sSearchTxt.toLowerCase() != 'and') && (sSearchTxt.toLowerCase() != 'them') && (sSearchTxt.toLowerCase() != 'they') && (sSearchTxt.toLowerCase() != 'him') && (sSearchTxt.toLowerCase() != 'her') && (sSearchTxt.toLowerCase() != 'there') && (sSearchTxt.toLowerCase() != 'their') && (sSearchTxt.toLowerCase() != 'in') && (sSearchTxt.toLowerCase() != 'any')) {
				fnRunSearch(sSearchTxt);
			}
		}
	} else {
		sTmp = sWin.split('earl.aspx');
		window.location =  sTmp[0] + 'earl.aspx?content_list=true&search=' + _obj('txt_quick_search').value;
	}
}

function fnKeyDownSearch() {

	// block teh user hitting return to run the search
	if (window.event.keyCode == 13) {
		event.cancelBubble = true;
		event.returnValue = false;
		return false;
	}
}

var oAJAXRequest;

function fnInitReqObj() {

	// creates a new HTTP request object
	try {
		oAJAXRequest = new ActiveXObject("Msxml2.XMLHTTP");
    	} catch(e) {
        	try { oAJAXRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch(oc) { oAJAXRequest = null; }
    	}

	if (!oAJAXRequest&&typeof XMLHttpRequest!="undefined") { oAJAXRequest = new XMLHttpRequest(); }
}


function lstFAQs_change() {

	// init the AJAX request object
	fnInitReqObj();

	_obj('divFAQ').innerHTML = 'Searching...'
	_show('divFAQ');
	_show('divFAQNote');

	sWin = window.location.toString().replace('?gclid=CP-L5IyQyowCFQXklAodCQyAJQ','')

	// define the URL which shall process the command
	var sURL = sWin.replace('earl.aspx','db_get_faq.aspx');
	sURL += '&' + _obj('lstFAQs').value;

	if (oAJAXRequest != null ) {
		oAJAXRequest.onreadystatechange = fnFAQResultsReturned;
		oAJAXRequest.open("GET", sURL, true);
      		oAJAXRequest.send(null);
	} else {
		_obj('divFAQ').innerHTML = ""
	}
}

function lstTechFAQs_change() {

	// init the AJAX request object
	fnInitReqObj();

	_obj('divFAQ').innerHTML = 'Searching...'
	_show('divFAQ');

	sWin = window.location.toString().replace('?gclid=CP-L5IyQyowCFQXklAodCQyAJQ','')

	// define the URL which shall process the command
	var sURL = sWin.toString().replace('earl.aspx','db_get_faq_tech.aspx');
	sURL += '&faq=' + _obj('lstTechFAQs').value;

	if (oAJAXRequest != null ) {
		oAJAXRequest.onreadystatechange = fnFAQResultsReturned;
		oAJAXRequest.open("GET", sURL, true);
      		oAJAXRequest.send(null);
	} else {
		_obj('divFAQ').innerHTML = ""
	}
}


function fnFAQResultsReturned() {

	var const_AJAXReady = 4;
	var const_AJAXStatusGood = 200;

	if (oAJAXRequest.readyState == const_AJAXReady) {
           	if (oAJAXRequest.status == const_AJAXStatusGood) {

			_obj('divFAQ').innerHTML = oAJAXRequest.responseText;
			_obj('divFAQ').style.border = 'solid 1px'

			sHTML = _obj('earl_content_support_expert').innerHTML
			sHTML = sHTML.replace('<BR>',''); sHTML = sHTML.replace('<br>','')
			sHTML = sHTML.replace('<BR>',''); sHTML = sHTML.replace('<br>','')

			_obj('divFAQNote').innerHTML = sHTML 
			_show('divFAQNote')

		} else {
			_obj('divFAQ').innerHTML = ""
			_obj('divFAQ').style.border = 'none'
       		}
		_show('divFAQ');
	}
}


function fnRunSearch(sSearchTxt) { 

	// called on the key up event on the search text field
	// uses AJAX to call the server side search functions passing
	// the text in the search text field as a paramater

	// init the AJAX request object
	fnInitReqObj();

	// init the layer
	fnHideAllDynamicLayers()

	if (_obj('earl_search_results').className == 'earl_content_area_full') {
		try { _hide('earl_interest_capture'); } catch(e) {}
	} else {
		try { _show('earl_interest_capture'); } catch(e) {}
	}

	try {

		sResultsHTML = new String();
		sResultsHTML += '<div id="earl_content_type_search" class="earl_content_area_presentation">Loading...</div>';
		sResultsHTML += '<div id="earl_scrolling_content_notes_2" class="earl_scrolling_content_notes">';
		try {
			sResultsHTML += _obj('earl_scrolling_content_notes').innerHTML;
		} catch(e) {}
		sResultsHTML += '</div>';

		_obj('earl_search_results').innerHTML = sResultsHTML
		_show('earl_search_results');
	} catch(e) {}


	// define the URL which shall process the command
	// var sURL = 'http://www.earl2learn.com/earl/earl_support.aspx';

    var sCur = window.location.toString().split('earl.aspx')
	var sURL = sCur[0] + 'earl_support.aspx'

	sURL += '?earlversion=2&log=' + bIsLoggedOn.toString() + '&clid=' + sCurrentClient.toString() + '&cmd=' + sSearchTxt;

	if (oAJAXRequest != null ) {
		oAJAXRequest.onreadystatechange = fnResultsReturned;
		oAJAXRequest.open("GET", sURL, true);
      		oAJAXRequest.send(null);
	} else {
		_obj('earl_search_results').innerHTML = "ERROR"
	}
}

function fnResultsReturned() {

	var sStatus = new String();
	try { sStatus += oAJAXRequest.readyState.toString(); } catch(e) {}
	try { sStatus += ' - ' + oAJAXRequest.status.toString(); } catch(e) {}
	window.status = sStatus

	if (oAJAXRequest.readyState == 4) {
        	// only if "OK"
           	if (oAJAXRequest.status == 200) {

			sResultsHTML  = new String();
			sResultsHTML  = '<div id="earl_content_type_search" class="earl_content_area_presentation_scroll">'
			try {
				sResultsHTML += oAJAXRequest.responseText;
			} catch(e) {}
			sResultsHTML += '</div>';
			sResultsHTML += '<div id="earl_scrolling_content_notes_2" class="earl_scrolling_content_notes">';
			try {
				sResultsHTML += _obj('earl_scrolling_content_notes').innerHTML;
			} catch(e) {}
			sResultsHTML += '</div>';

			_obj('earl_search_results').innerHTML = sResultsHTML;
		} else {
			_obj('earl_search_results').innerHTML = "ERROR";
       		}
		_show('earl_search_results');
	}
}

function fnLoadSco(sPath,bAccessScreenDirectly,bBlock,iSCOOverRide) {

	// if this is an assessment then 
	var sAssessment = "&ass="
	if (sPath.indexOf('assessment') > -1) {
		sAssessment = "&ass=" + sPath;
		bAccessScreenDirectly = false;
	}

	sThisSCO 	= sPath.split('/');
	sThisSCO	= sThisSCO[0].replace('sco','');

	if (isNaN(iSCOOverRide)) {
		 if (bBlock == true) {
		    alert("To view this content please subscribe to earl.")
	    } else {
	        sCur = window.location.toString().split('earl.aspx')
		    oWin = window.open(sCur[0] + 'earl_demo/earl_loadscreen.aspx?sco=' + sThisSCO + '&earlversion=2' + sAssessment + '&log=' + bIsLoggedOn.toString() + '&indiv=' + sCurrentUser + '&clid=' + sCurrentClient.toString() + '&path=' + sPath + '&bAccessScreenDirectly='+bAccessScreenDirectly.toString(),'oWin','status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=0,height=700px,width=1024px');
		    oWin.focus();
	    }
	} else {
		oWin = window.open(sCur[0] + 'earl_demo/earl_loadscreen.aspx?sco=' + sThisSCO + '&earlversion=2' + sAssessment + '&log=' + bIsLoggedOn.toString() + '&indiv=' + sCurrentUser + '&clid=' + sCurrentClient.toString() + '&path=' + sPath + '&bAccessScreenDirectly='+bAccessScreenDirectly.toString()+'&iOverride='+iSCOOverRide,'oWin','status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=0,height=700px,width=1024px');
	}



}

function fnLIOver() {}


function alternativeScrollIntoView(parentDiv,elementIntoDiv) {

	var principal 	= parentDiv;
	principal.scrollTop = 0;
	var rects 	 = principal.getClientRects()[0];
	var topFinal 	 = rects.top;
	var bottomFinal  = rects.bottom;
	var bottomActual = elementIntoDiv.getClientRects()[0].bottom;
	if (bottomActual == 0) {
		return;
	}
	
	while(bottomActual>bottomFinal||bottomActual<topFinal) {
		var direction="down";
		if(bottomActual<topFinal) direction="up";

		principal.doScroll(direction);
		bottomActual = elementIntoDiv.getClientRects()[0].bottom;
	}
}

function fnEARLSearch(sCallee) {

	fnHideAllDynamicLayers();

	sWin = window.location.toString().replace('?gclid=CP-L5IyQyowCFQXklAodCQyAJQ','')

	if (sWin.toString().indexOf("content_list") > -1) {

		try { _hide('earl_interest_capture'); } catch(e) {}

		_show('earl_content_list');

		if (sCallee == 'earl_search_controls_browse_dropdown') {
			fnLog('navigate')
			sTarget = 'sco_' + _obj('earl_search_controls_browse_dropdown').value;
			expand_shrink_sco(_obj('earl_search_controls_browse_dropdown').value)
			_obj(sTarget).focus();


			if (navigator.userAgent.toString().indexOf('MSIE') > -1) {
				alternativeScrollIntoView(_obj('earl_scrolling_content_list'), _obj(sTarget))
				
			} else {
				_obj(sTarget).scrollIntoView(true);	// true = scroll to the top
			}

			if (sTarget.toString() == "sco_1") {
				_obj('earl_scrolling_content_list').focus();
				_obj('earl_scrolling_content_list').scrollTop = 0;
			}

			// scroll into view can make this slide
			if (_obj('earl_content_list').scrollLeft != 0) {
				_obj('earl_content_list').scrollLeft = 0;
			}

		}

		if (sCallee == 'earl_search_controls_view_all') { show_all_nodes_in_tree() }
	} else {
		sTmp = sWin.toString().split('earl.aspx')
		if (sCallee == 'earl_search_controls_view_all') {
			window.location =  sTmp[0] + 'earl.aspx?content_list=true&view_all=true';
		} else {
			if (sCallee == 'earl_search_controls_browse_dropdown') {
				window.location =  sTmp[0] + 'earl.aspx?content_list=true&browse=' + _obj('earl_search_controls_browse_dropdown').value;
			} else {
				window.location =  sTmp[0] + 'earl.aspx?content_list=true';
			}
		}
	}
}

var sLastExpanded = '1' // set to 1 as the first SCO is expanded by default
var iNbrScos = 180;
function hide_all_nodes_in_tree() {

	for (var x = 1; x<=iNbrScos; x++) {
		try {
			_obj('sco_'+x).className 		= 'sco_name';
			_obj('sco_'+x).height 			= '15px';

			_obj('sco_title_'+x).className 		= 'expand_shrink_sco';

			_obj('_sco_'+x).style.visibility 	= 'hidden'
			_obj('_sco_'+x).style.height 		= 0;
		} catch(e) {

		}
	}
}
function show_all_nodes_in_tree() {

	var bHideAllModel = new Boolean(true);

	if (bHideAllModel) {
		hide_all_nodes_in_tree()
		try {
			_obj('earl_search_controls_browse_dropdown').value = 1;
			_obj('earl_scrolling_content_list').focus();
			_obj('earl_scrolling_content_list').scrollTop = 0;



		} catch(e) {}
	} else {

		for (var x = 1; x<=iNbrScos; x++) {
			try {
				expand_shrink_sco(x,true)
			} catch(e) { }
		}

		try {
			_obj('earl_search_controls_browse_dropdown').value = 1;
			_obj('sco_1').focus();
			_obj('sco_1').scrollIntoView();

			if (_obj('earl_content_list').scrollLeft != 0) {
				_obj('earl_content_list').scrollLeft = 0;
			}
		} catch(e) {}
	}

}

function expand_shrink_sco(sScoId,bignoreHide) {

	if (typeof(bignoreHide) == 'boolean') {
		if (bignoreHide == false) {
			hide_all_nodes_in_tree();
		}
	} else {
		hide_all_nodes_in_tree();
	}

	try {
		aCountLIs = _obj('_sco_'+sScoId).innerHTML.toLowerCase().split('<li>')
		iHeight = (aCountLIs.length * 15)

		// adjust the height of all the SCOs which have names too long
		if (sScoId.toString() == '6') {iHeight += (15*3)}
		if (sScoId.toString() == '33') {iHeight += (15*1)}
		if (sScoId.toString() == '15') {iHeight += (15*1)}
		if (sScoId.toString() == '16') {iHeight += (15*1)}
		if (sScoId.toString() == '19') {iHeight += (15*1)}
		if (sScoId.toString() == '20') {iHeight += (15*34)}
		if (sScoId.toString() == '21') {iHeight += (15*8)}
		if (sScoId.toString() == '22') {iHeight += (15*7)}
		if (sScoId.toString() == '23') {iHeight += (15*15)}
		if (sScoId.toString() == '24') {iHeight += (15*33)}
		if (sScoId.toString() == '25') {iHeight += (15*15)}
		if (sScoId.toString() == '26') {iHeight += (15*14)}
		if (sScoId.toString() == '30') {iHeight += (15*3)}
		if (sScoId.toString() == '31') {iHeight += (15*2)}
		if (sScoId.toString() == '32') {iHeight += (15*1)}
		if (sScoId.toString() == '52') {iHeight += (15*2)}
		if (sScoId.toString() == '54') {iHeight += (15*5)}

		if (sLastExpanded != sScoId.toString()) {
			_obj('sco_'+sScoId).className 			= 'sco_name_expanded';
			_obj('sco_title_'+sScoId).className 		= 'expand_shrink_sco_expanded';
			_obj('_sco_'+sScoId).style.visibility 		= 'visible'
			_obj('_sco_'+sScoId).style.height 		= iHeight.toString() + "px";
			_obj('_sco_'+sScoId).style.overflow 		= "auto";
			sLastExpanded = sScoId;
		} else {
			sLastExpanded = '-1'
		}
	} catch(e) {}

	_obj('earl_search_controls_browse_dropdown').value = sScoId;

}


function fnEARLFooter(sCallee) {

	// called when the user clicks on Privacy Statement link, Terms and Conditions link,
	// or Site Map link.  Purposes is to show the correct layer and hide the other 
	// layers which may display content on the middle.

	fnHideAllDynamicLayers();

	if (_obj(sCallee).className == 'earl_content_area_full') {
		try { _hide('earl_interest_capture'); } catch(e) {}
	} else {
		try { _show('earl_interest_capture'); } catch(e) {
			_obj(sCallee).className = 'earl_content_area_full'
		}
	}
	_show(sCallee);

}

function fnEARLHeader(sCallee) {

	// called when the user clicks on About Us link, Contact Us link, Support link
	// or Useful Documents link.  Purposes is to show the correct layer and hide the other 
	// layers which may display content on the middle.

	fnHideAllDynamicLayers();

	if (_obj(sCallee).className == 'earl_content_area_full') {
		try { _hide('earl_interest_capture'); } catch(e) {}
	} else {
		try { _show('earl_interest_capture'); } catch(e) {}
	}
	_show(sCallee);


}



function send_password() {

	// sends the user their password

	var sErr = ''
	var sEmail = _trim(_obj('txt_username').value);

	if (sEmail == '') { 
		sErr = 'You must enter your email address into the username field'
	}


	if ((sEmail.indexOf('@')==-1) || (sEmail.indexOf('.')==-1)) { 
		sErr = 'You must enter your valid email address into the username field'
	}

	if (sErr == '') {
		fnInitReqObjStatus();
		

		sWin = window.location.toString().replace('?gclid=CP-L5IyQyowCFQXklAodCQyAJQ','')
		var url = sWin.replace('earl.aspx','db_lost.aspx')
		url += "&user=" + sEmail

		if (oReqObjectStatus != null ) {
			oReqObjectStatus.onreadystatechange = recievedUsers;
			oReqObjectStatus.open("GET", url, true);
			oReqObjectStatus.send(null);
    		}
	} else {
		alert(sErr)
	}
}
function recievedUsers() {

	// display the status of the load to the 
	// browser status bar
	var sStatus 	= new String(); try { sStatus += oReqObjectStatus.readyState.toString(); } catch(e) {}; try { sStatus += ' - ' + oReqObjectStatus.status.toString(); } catch(e) {}; window.status = sStatus

	// if the status is ready and returned OK then 
	// display the results to the div layer
	// div_search_results
	if (oReqObjectStatus.readyState == 4) {
		// only if "OK"
		if (oReqObjectStatus.status == 200) {
			if (oReqObjectStatus.responseText=="passed") {
				alert('Your password has been sent to your email address')
			}

			if (oReqObjectStatus.responseText=="failed") {
				alert('Your password could not be found, please check you entered the correct email address')
			}
		} else {
			alert('Your password could not be found, please check you entered the correct email address')
		}
	}
}
function fnInitReqObjStatus() {
	// creates a new HTTP request object
	try { oReqObjectStatus = new ActiveXObject("Msxml2.XMLHTTP");
    	} catch(e) { try { oReqObjectStatus = new ActiveXObject("Microsoft.XMLHTTP"); } catch(oc) { oReqObjectStatus = null; } }
	if (!oReqObjectStatus&&typeof XMLHttpRequest!="undefined") { oReqObjectStatus = new XMLHttpRequest(); }
}

var whitespace = new String(" \t\n\r");
function _trim(sIn) { return _rtrim(_ltrim(sIn)); }

function _ltrim(sIn) {
	
	var s = new String(sIn);
	if (whitespace.indexOf(s.charAt(0)) != -1) {  
	var j=0, i = s.length;
	while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
		j++;
		s = s.substring(j, i);
	}
	return s;
}

function _rtrim(sIn) {

	var s = new String(sIn);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
	var i = s.length - 1;
	while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
		i--;
	    s = s.substring(0, i+1);
	}
	return s;
}

function fnWebLink(sURLKey) {
	// called from the web links
	fnLog('links')
	var sRecordURL = new String('resource.aspx?clid=' + sCurrentClient.toString() + '&linkid='+sURLKey);
	oWin = window.open(sRecordURL);
	oWin.focus();
}
