
/* ----------------------------------------------------------- */
// Browser Sniff
/* ----------------------------------------------------------- */

var UserAgent = navigator.userAgent.toLowerCase();
var IsOpera = (UserAgent.indexOf('opera') != -1); // is opera
var IsOpera8 = ((UserAgent.indexOf('opera 8') != -1 || UserAgent.indexOf('opera/8') != -1) ? 1 : 0); // is opera8
var IsNS4 = (document.layers) ? true : false; // is netscape 4
var IsIE4 = (document.all && !document.getElementById) ? true : false; // is IE 4
var IsIE5 = (document.all && document.getElementById) ? true : false; // is IE 5+
var IsNS6 = (!document.all && document.getElementById) ? true : false; // is netscape 6
var IsFireFox = (UserAgent.indexOf("firefox/") != -1); // is firefox
var Transitions = (IsIE5 || IsIE4) ? true : false // do we support transitions

function prepSearchValue() {
	var query = document.getElementById("q");
	var value = query.value.replace(/"/g, "");
	query.value = "\"" + value + "\"";
	document.qs.submit();
}

function checkEnter(e) {
	if(e && e.which) {
		e = e;
		characterCode = e.which;
	} else {
		e = event;
		characterCode = e.keyCode;
	}
	
	if(characterCode == 13) {
		prepSearchValue();
	} else {
		return true;
	} 
}

//Ajax search

function search(value) { 
	if (value == "") {
		$('clearsearchresults').innerHTML = "";
	} else {
		var division = document.getElementById("d").value;
		var pars = 's=' + escape(value) + "&d=" + escape(division);
    	var myAjax = new Ajax.Request('../../ajax_coursesearch.aspx', 
          {method: 'get', parameters: pars, onComplete:doSearchResponse})
	}

}
function doSearchResponse(response) {
    $('clearsearchresults').innerHTML = response.responseText;
}

function updateFLList(value) { 
	if (value == "") {
		initFLList();
	} else {
		var pars = 's=' + escape(value) + "&d=fl";
    	var myAjax = new Ajax.Request('../../ajax_coursesfilterlist.aspx', 
          {method: 'get', parameters: pars, onComplete:doFLSearchResponse})
	}

}
function doFLSearchResponse(response) {
    $('fl_cfl_results').innerHTML = response.responseText;
}

function updateBLList(value) { 
	if (value == "") {
		initBLList();
	} else {
		var pars = 's=' + escape(value) + "&d=bl";
    	var myAjax = new Ajax.Request('../../ajax_coursesfilterlist.aspx', 
          {method: 'get', parameters: pars, onComplete:doBLSearchResponse})
	}

}
function doBLSearchResponse(response) {
    $('bl_cfl_results').innerHTML = response.responseText;
}

function updateLTList(value) { 
	if (value == "") {
		initLTList();
	} else {
		var pars = 's=' + escape(value) + "&d=lt";
    	var myAjax = new Ajax.Request('../../ajax_coursesfilterlist.aspx', 
          {method: 'get', parameters: pars, onComplete:doLTSearchResponse})
	}

}
function doLTSearchResponse(response) {
    $('lt_cfl_results').innerHTML = response.responseText;
}


function initFLList() {
	var pars = "s=&d=fl";
    var myAjax = new Ajax.Request('../../ajax_coursesfilterlist.aspx', 
          {method: 'get', parameters: pars, onComplete:doFLFilterListResponse})
}

function doFLFilterListResponse(response) {
    $('fl_cfl_results').innerHTML = response.responseText;
}

function initBLList() {
	var pars = "s=&d=bl";
	
    var myAjax = new Ajax.Request('../../ajax_coursesfilterlist.aspx', 
          {method: 'get', parameters: pars, onComplete:doBLFilterListResponse})
}

function doBLFilterListResponse(response) {
    $('bl_cfl_results').innerHTML = response.responseText;
}

function initLTList() {
	var pars = "s=&d=lt";
	
    var myAjax = new Ajax.Request('../../ajax_coursesfilterlist.aspx', 
          {method: 'get', parameters: pars, onComplete:doLTFilterListResponse})
}

function doLTFilterListResponse(response) {
    $('lt_cfl_results').innerHTML = response.responseText;
}

function popup(url,name,sizew,sizeh,poppos,auFoyer){
	if(poppos=="center"){
		magauche=(screen.width)?(screen.width-sizew)/2:100;
		monhaut=(screen.height)?(screen.height-sizeh)/2:100;
	}
	else if ((poppos!='center') || poppos==null){
		magauche =+ 0;
		monhaut=+0
	}
	props="width=" + sizew + ",height=" + sizeh + ",top=" + monhaut + ",left=" + magauche + ",scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes,toolbars=no";
	window.open(url,name,props);
}

/* ----------------------------------------------------------- */
// Get a reference to an object on the client-side                   
/* ----------------------------------------------------------- */


function FindControl(strControlName) {

	var objReturn = '';
		if (IsIE5 || IsNS6 || IsOpera || IsOpera8)
		{
			objReturn = document.getElementById(strControlName);
		}
		else if (IsIE4)
		{
			objReturn = document.all[strControlName];
		}
		else if (IsNS4)
		{
			objReturn = document.layers[strControlName];
		}

	return objReturn

}


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";
 }
}


//this is the script for the tabs on the details page
function setTab(e) 
{
    var i;
    
    //get an array of all tabs on the page
    var divs = document.getElementsByTagName('div');
    for( var i = 0; i < divs.length; i++ ) 
    {
      var div = divs[i];
      if(div.id.indexOf('tabdiv_')>-1)
      {
        //get the second part of the string - to get the tab to reset as well
        var s = div.id.substring(7);
        document.getElementById("tabli_" + s).className = '';
        div.style.display = 'none';
      }
    }
   
    var tab = e.id.substring(6); //remove the 'tabli_' part of the string
    document.getElementById("tabdiv_" + tab).style.display = 'block';
    e.className = 'on';
}

function setTabByID(id)
{
	var i;
    
    //get an array of all tabs on the page
    var divs = document.getElementsByTagName('div');
    for( var i = 0; i < divs.length; i++ ) 
    {
      var div = divs[i];
      if(div.id.indexOf('tabdiv_')>-1)
      {
        //get the second part of the string - to get the tab to reset as well
        var s = div.id.substring(7);
        document.getElementById("tabli_" + s).className = '';
        div.style.display = 'none';
      }
    }
	
    document.getElementById("tabdiv_" + id).style.display = 'block';
	document.getElementById("tabli_" + id).className = 'on';
}

function gotoCourse(dd_name)
{
    var ctl = FindControl(dd_name);
    
    //get the selected option
    if(ctl.selectedIndex>0)
    {
        if(ctl.options[ctl.selectedIndex].value!="")
        {	
            window.location.href = ctl.options[ctl.selectedIndex].value;
        }
        else
        {
            alert('You have selected a topic - please select a course within the topic');
        }
    }
    else
    {
        alert('Please select an option');
    }
}

//Set the validation colour
var validationColour = "#E1EAFF";

function validateContactForm()
{

	//check all of the required fields - and turn them a different colour if 
	//they do not have content
	var txtemail = FindControl('txtemail');
	var txtfirstname = FindControl('txtfirstname');
	var txtsurname = FindControl('txtsurname');
	var txtReferer = FindControl('txtReferer');
	var txtRegion = FindControl('txtRegion');
	var txttitle = FindControl('txttitle');
	var txtcompany = FindControl('txtcompany');
	var txtphone = FindControl('txtphone');
	var bValid = true;
	
	//return all backgrounds to white
	txtemail.style.backgroundColor		= "#FFFFFF";
	txtfirstname.style.backgroundColor	= "#FFFFFF";
	txtsurname.style.backgroundColor	= "#FFFFFF";
	txtReferer.style.backgroundColor	= "#FFFFFF";
	txtRegion.style.backgroundColor	= "#FFFFFF";
	txttitle.style.backgroundColor	= "#FFFFFF";
	txtcompany.style.backgroundColor	= "#FFFFFF";
	txtphone.style.backgroundColor	= "#FFFFFF";
	
	if(txtemail.value == "")
	{
		txtemail.style.backgroundColor = validationColour;
		bValid = false;
	}
	else
	{
		//check validity of email format
		if(!validateEmail(txtemail.value))
		{
			txtemail.style.backgroundColor = validationColour;
			txtemail.value = "[Please enter a correctly formatted email address]"
			bValid = false;
		}			
	}

	if(txtfirstname.value == "")
	{
		txtfirstname.style.backgroundColor = validationColour;
		bValid = false;
	}
	if(txtsurname.value == "")
	{
		txtsurname.style.backgroundColor = validationColour;
		bValid = false;
	}		
	
	if(txtReferer.value == "")
	{
		txtReferer.style.backgroundColor = validationColour;
		bValid = false;
	}		
	if(txtRegion.value == "")
	{
		txtRegion.style.backgroundColor = validationColour;
		bValid = false;
	}
	if(txttitle.value == "")
	{
		txttitle.style.backgroundColor = validationColour;
		bValid = false;
	}
	if(txtcompany.value == "")
	{
		txtcompany.style.backgroundColor = validationColour;
		bValid = false;
	}
	if(txtphone.value == "")
	{
		txtphone.style.backgroundColor = validationColour;
		bValid = false;
	}
	if(bValid)
	    document.forms[2].submit();
}

function validateNewsletterSignUp()
{
	var txtemail = FindControl('txtemail');
	var bValid = true;
	
	//return all backgrounds to white
	txtemail.style.backgroundColor = "#FFFFFF";
	
	//check validity of email format
	if(!validateEmail(txtemail.value))
	{
		txtemail.style.backgroundColor = validationColour;
		txtemail.value = "[Please enter a correctly formatted email address]"
		bValid = false;
	}	
	
	if(bValid)
	    document.forms[2].submit();
}

function validateBookingForm()
{ 	
	
	//check all of the required fields - and turn them a different colour if 
	//they do not have content
	var txtemail = FindControl('txtemail');
	var txtfirstname = FindControl('txtfirstname');
	var txtsurname = FindControl('txtsurname');
	var txtReferer = FindControl('txtReferer');
	var txtRegion = FindControl('txtRegion');
	var txttitle = FindControl('txttitle');
	var txtcompany = FindControl('txtcompany');
	var txtphone = FindControl('txtphone');
	var bValid = true;
	
	//return all backgrounds to white
	txtemail.style.backgroundColor		= "#FFFFFF";
	txtfirstname.style.backgroundColor	= "#FFFFFF";
	txtsurname.style.backgroundColor	= "#FFFFFF";
	txtReferer.style.backgroundColor	= "#FFFFFF";
	txtRegion.style.backgroundColor	= "#FFFFFF";
	txttitle.style.backgroundColor	= "#FFFFFF";
	txtcompany.style.backgroundColor	= "#FFFFFF";
	txtphone.style.backgroundColor	= "#FFFFFF";
	
	if(txtemail.value == "")
	{
		txtemail.style.backgroundColor = validationColour;
		bValid = false;
	}
	else
	{
		//check validity of email format
		if(!validateEmail(txtemail.value))
		{
			txtemail.style.backgroundColor = validationColour;
			txtemail.value = "[Please enter a correctly formatted email address]"
			bValid = false;
		}			
	}

	if(txtfirstname.value == "")
	{
		txtfirstname.style.backgroundColor = validationColour;
		bValid = false;
	}

	if(txtsurname.value == "")
	{
		txtsurname.style.backgroundColor = validationColour;
		bValid = false;
	}				
	if(txtReferer.value == "")
	{
		txtReferer.style.backgroundColor = validationColour;
		bValid = false;
	}	
	if(txtRegion.value == "")
	{
		txtRegion.style.backgroundColor = validationColour;
		bValid = false;
	}
	if(txttitle.value == "")
	{
		txttitle.style.backgroundColor = validationColour;
		bValid = false;
	}
	if(txtcompany.value == "")
	{
		txtcompany.style.backgroundColor = validationColour;
		bValid = false;
	}
	if(txtphone.value == "")
	{
		txtphone.style.backgroundColor = validationColour;
		bValid = false;
	}
	// phone entry validation (unused due to lack of suitable regular expression)
	/* else
	{
		//check validity of phone format
		if(!validatePhone(txtphone.value))
		{
			txtphone.style.backgroundColor = validationColour;
			txtphone.value = "[Please enter a correctly formatted phone number]";
			bValid = false;
		}			
	}*/
	if(bValid)
	    document.forms[2].submit();
	
}

function validateNotification()
{ 	
	
	//check all of the required fields - and turn them a different colour if 
	//they do not have content
	var txtemail = FindControl('txtemail');
	var txtfirstname = FindControl('txtfirstname');
	var txtsurname = FindControl('txtsurname');
	var txtRegion = FindControl('txtRegion');
	var txttitle = FindControl('txttitle');
	var txtphone = FindControl('txtphone');
	var bValid = true;
	
	//return all backgrounds to white
	txtemail.style.backgroundColor		= "#FFFFFF";
	txtfirstname.style.backgroundColor	= "#FFFFFF";
	txtsurname.style.backgroundColor	= "#FFFFFF";
	txtRegion.style.backgroundColor	= "#FFFFFF";
	txttitle.style.backgroundColor	= "#FFFFFF";
	txtphone.style.backgroundColor	= "#FFFFFF";
	
	if(txtemail.value == "")
	{
		txtemail.style.backgroundColor = validationColour;
		bValid = false;
	}
	else
	{
		//check validity of email format
		if(!validateEmail(txtemail.value))
		{
			txtemail.style.backgroundColor = validationColour;
			txtemail.value = "[Please enter a correctly formatted email address]"
			bValid = false;
		}			
	}

	if(txtRegion.value == "")
	{
		txtRegion.style.backgroundColor = validationColour;
		bValid = false;
	}
	if(txttitle.value == "")
	{
		txttitle.style.backgroundColor = validationColour;
		bValid = false;
	}
	if(bValid)
	    document.forms[2].submit();
	
}

function validateEmail(email)
{
    email = email.toUpperCase();
	regExp  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;    
    return regExp.test(email);
}
// phone entry validation (unused due to lack of suitable regular expression)
/*
function validatePhone(phone)
{
	regExp  = ;    
    return regExp.test(email);
}
*/

//MAC HACKS
if ((navigator.appVersion.indexOf("Mac") != -1)) 
{
	document.write("<link href='../includes/css/mac.css' rel='stylesheet' type='text/css' />"); 
}

