//////////////////////////////////////////////////////////////////////////////
//
// straightforward javascript functions
//
//////////////////////////////////////////////////////////////////////////////

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

//////////////////////////////////////////////////////////////////////////////

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

//////////////////////////////////////////////////////////////////////////////

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//////////////////////////////////////////////////////////////////////////////

function openWindow(theURL, theName, width, height) {
        var w = window.open(theURL , ""+theName+"" ,"width=" + width + ",height="+ height + ",toolbar=no,directories=0,menubar=no,status=no,resizable=1,location=0,scrollbars=1,copyhistory=0,alwaysRaised=yes");
		w.focus();
}

//////////////////////////////////////////////////////////////////////////////

// @name      The Fade Anything Technique
// @namespace http://www.axentric.com/aside/fat/
// @version   1.0-RC1
// @author    Adam Michela

var Fat = {
	make_hex : function (r,g,b) 
	{
		r = r.toString(16); if (r.length == 1) r = '0' + r;
		g = g.toString(16); if (g.length == 1) g = '0' + g;
		b = b.toString(16); if (b.length == 1) b = '0' + b;
		return "#" + r + g + b;
	},
	fade_all : function ()
	{
		var a = document.getElementsByTagName("*");
		for (var i = 0; i < a.length; i++) 
		{
			var o = a[i];
			var r = /fade-?(\w{3,6})?/.exec(o.className);
			if (r)
			{
				if (!r[1]) r[1] = "";
				if (o.id) Fat.fade_element(o.id,null,null,"#"+r[1]);
			}
		}
	},
	fade_element : function (id, fps, duration, from, to) 
	{
		if (!fps) fps = 30;
		if (!duration) duration = 3000;
		if (!from || from=="#") from = "#FFFFCC";
		if (!to) to = this.get_bgcolor(id);
		
		var frames = Math.round(fps * (duration / 1000));
		var interval = duration / frames;
		var delay = interval;
		var frame = 0;
		
		if (from.length < 7) from += from.substr(1,3);
		if (to.length < 7) to += to.substr(1,3);
		
		var rf = parseInt(from.substr(1,2),16);
		var gf = parseInt(from.substr(3,2),16);
		var bf = parseInt(from.substr(5,2),16);
		var rt = parseInt(to.substr(1,2),16);
		var gt = parseInt(to.substr(3,2),16);
		var bt = parseInt(to.substr(5,2),16);
		
		var r,g,b,h;
		while (frame < frames)
		{
			r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames));
			g = Math.floor(gf * ((frames-frame)/frames) + gt * (frame/frames));
			b = Math.floor(bf * ((frames-frame)/frames) + bt * (frame/frames));
			h = this.make_hex(r,g,b);
		
			setTimeout("Fat.set_bgcolor('"+id+"','"+h+"')", delay);

			frame++;
			delay = interval * frame; 
		}
		setTimeout("Fat.set_bgcolor('"+id+"','"+to+"')", delay);
	},
	set_bgcolor : function (id, c)
	{
		var o = document.getElementById(id);
		o.style.backgroundColor = c;
	},
	get_bgcolor : function (id)
	{
		var o = document.getElementById(id);
		while(o)
		{
			var c;
			if (window.getComputedStyle) c = window.getComputedStyle(o,null).getPropertyValue("background-color");
			if (o.currentStyle) c = o.currentStyle.backgroundColor;
			if ((c != "" && c != "transparent") || o.tagName == "BODY") { break; }
			o = o.parentNode;
		}
		if (c == undefined || c == "" || c == "transparent") c = "#FFFFFF";
		var rgb = c.match(/rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
		if (rgb) c = this.make_hex(parseInt(rgb[1]),parseInt(rgb[2]),parseInt(rgb[3]));
		return c;
	}
}

//////////////////////////////////////////////////////////////////////////////

function refreshTowns() {
	
	var county = document.forms.registrationForm.county.options[document.forms.registrationForm.county.options.selectedIndex].value;
	var url = 'towns.php?county=' + county;
	loadURL(url, 'towns');
}

//////////////////////////////////////////////////////////////////////////////

function refreshJobSearchTowns() {
	
	var county = document.forms.jobSearchForm.county.options[document.forms.jobSearchForm.county.options.selectedIndex].value;
	var url = 'job_search_towns.php?county=' + county;
	loadURL(url, 'towns');
}

//////////////////////////////////////////////////////////////////////////////

function loadURL(destination, elementID) {
	
	try {
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
		new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e) { /* do nothing */ }
	
	element = elementID;
	xmlhttp.onreadystatechange = triggered;
	xmlhttp.open("GET", destination, true);
	xmlhttp.send(null);
}

//////////////////////////////////////////////////////////////////////////////

function triggered() {
  	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
    
		if( xmlhttp.responseText && (xmlhttp.responseText !='') ) {
			try {	
				document.getElementById(element).innerHTML = xmlhttp.responseText;
			}
			catch(e) {
				// IE fails unless we wrap the string in another element.
				var wrappingDiv = document.createElement('div');
				wrappingDiv.innerHTML = xmlhttp.responseText;
				document.getElementById(element).appendChild(wrappingDiv);
			}
		}
	}
}

//////////////////////////////////////////////////////////////////////////////

function validateClientRegistrationForm() {

	var company = document.forms.registrationForm.company.value;
	var contact = document.forms.registrationForm.contact.value;
	var address1 = document.forms.registrationForm.address1.value;
	var county = document.forms.registrationForm.county.options[document.forms.registrationForm.county.options.selectedIndex].value;
	var town = document.forms.registrationForm.town.options[document.forms.registrationForm.town.options.selectedIndex].value;
	var postcode = document.forms.registrationForm.postcode.value;
	var telephone = document.forms.registrationForm.telephone.value;
	var email = document.forms.registrationForm.email.value;
	var description = document.forms.registrationForm.description.value;
	var admin = document.forms.registrationForm.admin.value;
	
	if( document.forms.registrationForm.terms.checked === false ) {
		alert('Please indicate that you have read the Terms of Use.');
		return false;

	} else if( (company == '') || (contact == '') || (address1 == '') || (county == 0) || (town == 0) || (postcode == '') || (telephone == '') || (email == '') || (description == '')  ) {
		alert('Please fill in all required fields');
		return false;
	
	} else if( provider_admin == '1' ) {
		var password = document.forms.registrationForm.password.value;
		if( password.length > 10 ) alert( 'The password should be no more than 10 characters' );
		return false;

	} else return true;
}

//////////////////////////////////////////////////////////////////////////////

function validateCandidateRegistrationForm() {

	var firstname = document.forms.registrationForm.firstname.value;
	var lastname = document.forms.registrationForm.lastname.value;
	var address1 = document.forms.registrationForm.address1.value;
	var county = document.forms.registrationForm.county.options[document.forms.registrationForm.county.options.selectedIndex].value;
	var town = document.forms.registrationForm.town.options[document.forms.registrationForm.town.options.selectedIndex].value;
	var postcode = document.forms.registrationForm.postcode.value;
	var telephone = document.forms.registrationForm.telephone.value;
	var email = document.forms.registrationForm.email.value;
	var description = document.forms.registrationForm.description.value;
	var admin = document.forms.registrationForm.admin.value;
	var sector_current = document.forms.registrationForm.sector_current.options[document.forms.registrationForm.sector_current.options.selectedIndex].value;
	var num_sectors = document.forms.registrationForm.num_sectors.value;
	var preferred_sector_selected = false;
	for( i=0; i<num_sectors; i++ ) {
		if( document.forms.registrationForm['sector_interested_' + i].checked ) preferred_sector_selected = true;
	}
	var password = document.forms.registrationForm.password.value;
	
	if( !document.forms.registrationForm.terms.checked ) {
		alert('Please indicate that you have read the Terms of Use.');
		return false;

	} else if( (firstname == '') || (lastname == '') || (address1 == '') || (county == 0) || (town == 0) || (postcode == '') || (telephone == '') || (email == '') || (description == '')  || (password == '') || (sector_current == 0) || (preferred_sector_selected == false) ) {
		alert('Please fill in all required fields');
		return false;
	
	} else if( provider_admin == '1' ) {
		var password = document.forms.registrationForm.password.value;
		if( password.length > 10 ) alert( 'The password should be no more than 10 characters' );
		return false;

	} else return true;
}

//////////////////////////////////////////////////////////////////////////////

function SelectAll(formName, checkboxCount, checkboxName, selectAllName) {
	
	var numCheckboxes = document.forms[formName][checkboxCount].value;
	var selectName = ( selectAllName == undefined ) ? 'selectAll' : selectAllName;
	
	var checked = document.forms[formName][selectName].checked;
	for( i=0; i<numCheckboxes; i++ ) {
		document.forms[formName][checkboxName + i].checked = checked;
	}
}

//////////////////////////////////////////////////////////////////////////////

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

//////////////////////////////////////////////////////////////////////////////

function validateSendToFriendForm() {

	var from = document.forms.stfForm.from.value;
	var recipient_name = document.forms.stfForm.recipient_name.value;
	var recipient_email = document.forms.stfForm.recipient_email.value;
	
	if( (from == '') || (recipient_name == '') || (recipient_email == '')  ) {
		alert('Please fill in all fields');
		return false;
	
	} else return true;
}

//////////////////////////////////////////////////////////////////////////////
