
function ttipon(obj){
	//tt=$(obj).next('.tooltip');
	$(obj).next('.tooltip').remove();
	
	//$("div.tooltip").stop();
	os=$(obj).offset();
	left=os.left;
	top=os.top;
	w=$(obj).width();
	h=$(obj).height();
	x=left+w+5;
	y=top;
	t=w+' x '+h+' at '+left+' '+top;
	//y = obj.pageY +10; // mouse pos
	
    $(obj).after('<div class="tooltip">'+t+'<div>');
    
	tt=$(obj).next('.tooltip');
    tt.css({"top":y-20, "left":x, 'padding':5, "color":'#be054b', "background-color":'#dfdbd9', 'border':'2px solid #dcd2cc', 'position':'absolute', 'z-index':30});
	tt.animate({opacity:0},0);
	tt.animate({opacity:1, top:y},1000);
};
function ttipoff(obj){
	//$("div.tooltip").stop();
	os=$(obj).offset();
	left=os.left;
	top=os.top;
	w=$(obj).width();
	h=$(obj).height();
	
	tt=$(obj).next('.tooltip');
	console.log(tt);
	//tt.stop();
	//tt.animate({opacity:0, top:y+20}, 500, function(){tt.remove();});
	tt.animate({opacity:0, top:y+20}, 1000, function(){tt.remove();});
};

/* GENERIC FUNCTIONS */
$(document).ready(function(){

	$(".hasttip").hover(function(){ttipon(this);},function(){ttipoff(this);});

	//$(".topnav a").css({"marginTop":10});
	//$(".topnav a img").animate({"Top":"-10"},300);

	$(".topnav .item").hover( 
		function(){$(this).animate({"opacity":.5},0);$(this).animate({"marginTop":3, "opacity":1},100);},
		function(){$(this).animate({"marginTop":0},100);}
	);

	$("#constructiontopmenu").hide(0);
	$("#constructiontop").hover( 
		function(){$("#constructiontopmenu").slideDown(100);},
		function(){$("#constructiontopmenu").slideUp(100);}
	);
	
	
	
	
	
	$(".nfl").click(function(){window.open($(this).attr('title'));});
	
	var sliderollwidth=220;
	var sliderollspeed=320;
	$(".slideroll").css({"overflow":"hidden","position":"relative"});
	$(".slideroll .normal").css({"position":"absolute"});
	$(".slideroll .rollover").css({"position":"absolute", 'width':sliderollwidth});
	$(".slideroll").css({'width':sliderollwidth});
	$(".slideroll").css({'height':80});
	$(".slideroll .rollover").show();
	$(".slideroll .rollover").css({"marginLeft":sliderollwidth});
	$(".slideroll").hover( 
		function(){$(this).children().stop(); $(this).children(".normal").animate({"marginLeft":-sliderollwidth},sliderollspeed); $(this).children(".rollover").animate({"marginLeft":0},sliderollspeed);},
		function(){$(this).children().stop(); $(this).children(".rollover").animate({"marginLeft":sliderollwidth},sliderollspeed); $(this).children(".normal").animate({"marginLeft":0},sliderollspeed);}
	);
	
	
	/*
	var framewidth=$(id+" .ss04mount div.ss04frame").width();
	var frameheight=$(id+" .ss04mount div.ss04frame").height();
	$(id+" .ss04mount").css({'width':framewidth,'height':frameheight});//set mount dimensions	
	*/
	
	
	
	
	$('.sidemenutoggle .menuhead').click(function(){$(this).siblings('li').slideToggle(100);});
	$('.sidemenutoggle .menuhead').siblings('li').slideToggle(100);
	$('.sidemenutoggle .menuhead').hover(function(){
		$(this).css('cursor','pointer');
		},function(){
		$(this).css('cursor','auto');
	});
	
	$('.togglelink').click(function(){$(this).next('.toggletext').slideToggle(100);});
	$("img.roll").hover(function(){this.src=this.src.replace("-norm","-roll");},function(){this.src=this.src.replace("-roll","-norm");});
	$("input.roll").hover(function(){this.src=this.src.replace("-norm","-roll");},function(){this.src=this.src.replace("-roll","-norm");});
	$('#dispmesbut').click(function(){jsf_toggledisplaymes();});

	//function rollon(e){document.body.style.cursor='pointer';}
	//function rolloff(e){document.body.style.cursor='default';}
	
	/*
	$('.sidemenu').animate({opacity:0},0);
	$('.sidemenu').animate({opacity:1},300);
	$('.sidemenu').hide();
	$('.sidemenu').slideDown(300);
	*/
	//$(".toggleboxhead").click(function(){$(this).next().slideToggle(100);});
	//$(".toggleboxfoot").click(function(){$(this).prev().slideToggle(100);});
	//$(".togglebox").hover(function(){$(this).children(".toggleboxbody").slideToggle(100);},function(){$(this).children(".toggleboxbody").slideToggle(100);});
	//$(".togglesmallbox").hover(function(){$(this).children(".togglesmallboxbody").slideToggle(100);},function(){$(this).children(".togglesmallboxbody").slideToggle(100);});

	//$('.togglebox').hover(function(){$(this).children('.toggleboxbody').slideToggle(100);},function(){$(this).children('.toggleboxbody').slideToggle(100);});
	//$('.togglelink').hover(function(){$(this).children('.togglelink').slideToggle(100);},function(){$(this).children('.togglesmallboxbody').slideToggle(100);});

});


function jsf_toggledisplaymes(){$('#dispmesbody').slideToggle();}

function jsf_removetimer(id){
	var obj=document.getElementById(id);
	clearTimeout(obj.timer);
}

function isobject(obj) {
	if (obj == null || obj == undefined) {
		return false;
	}else{
		return true;
	}
}
function jsf_sethref(id,url){
	if (url==''){
		document.getElementById(id).setAttribute('href','javascript:void()');
	}else{
		document.getElementById(id).setAttribute('href',url);
	}
	//to get this working - attach link and remove link
	//galpiclink=document.createElement('a');
	//document.getElementById('galpic').appendChild(galpiclink);
	//document.getElementById('galpiclink').setAttribute('href',link);
	//removeAttribute
}
/* GENERIC VISUAL FUNCTIONS */

function jsf_animate_pos(id,vh,dur,sttime,from,to){
	var obj=document.getElementById(id);
	var now=(new Date()).getTime();
	var animtype="sin";

	var perc=(now-sttime)/dur;
	if (perc>1){perc=1;}

	 if (animtype=="linear"){
		pos=from+(to-from)*perc;
	 }else if (animtype=="sin"){
		ang=90*perc;
		p=Math.sin(ang*Math.PI/180);
		pos=from+(to-from)*p;
	 }

	if (perc==1){
		jsf_setpos(obj,to,vh);
	}else{
		jsf_setpos(obj,pos,vh);
		obj.timer=window.setTimeout("jsf_animate_pos('"+id+"','"+vh+"',"+dur+","+sttime+","+from+","+to+")",1);
	}
}
function jsf_setpos(obj,pos,vh){
	obj.style.position="relative";
	if (vh=='h'){
		obj.style.left=pos+"px";
	}else{
		obj.style.top=pos+"px";
	}
}

function jsf_animate_size(id,vh,dur,sttime,from,to){// todo 1. add sin wave to decelerate items + make so works for all horizontal 
	var obj=document.getElementById(id);
	var now=(new Date()).getTime();
	var animtype="linear";

	var perc=(now-sttime)/dur;
	if (perc>1){perc=1;}

	 if (animtype=="linear"){
		size=from+(to-from)*perc;
	 }else if (animtype=="sin"){
		ang=90*perc;
		p=Math.sin(ang*Math.PI/180);
		size=from+(to-from)*p;
	 }

	if (perc==1){
		jsf_setsize(obj,to,vh);
	}else{
		jsf_setsize(obj,size,vh);
		obj.timer=window.setTimeout("jsf_animate_size('"+id+"','"+vh+"',"+dur+","+sttime+","+from+","+to+")",1);
	}
}
function jsf_setsize(obj,size,vh){
	//obj.style.position="relative";
	if (vh=='h'){
		obj.style.width=size+"px";
	}else{
		obj.style.height=size+"px";
	}
}

function jsf_animate_opacity(id,dur,sttime,from,to){
	var obj=document.getElementById(id);
	var opacity=obj.style.opacity * 100;
	var now=(new Date()).getTime();
	opacity=from+(to-from)*(now-sttime)/dur;
	if (opacity<0)
		jsf_setopacity(obj,0)
	else if (opacity>100)
		jsf_setopacity(obj,100)
	else{
		jsf_setopacity(obj,opacity);
		obj.timer=window.setTimeout("jsf_animate_opacity('"+id+"',"+dur+","+sttime+","+from+","+to+")",1);	}
	
}

function jsf_setopacity(obj,opac){
	obj.style.filter='alpha(opacity='+opac+')'; // old mozilla and firefox
	obj.style.MozOpacity=opac/100; // other browsers
	obj.style.opacity=opac/100;
}

function jsf_img_faderepalce(imgurl,id,linkid,dur,imgholderid,link){
	var foreground=document.getElementById(id);
	if (imgholderid){
		var background=document.getElementById(imgholderid);
		if (background){
			background.style.backgroundImage='url('+foreground.src+')';
			background.style.backgroundRepeat='no-repeat';
			/*background.style.backgroundPosition='top center';*/
		}
	}
	jsf_setopacity(foreground,0);
	foreground.src=imgurl;
	//if (link){
	//	document.getElementById(linkid).setAttribute('href',link);
	//}
	if (foreground.timer){window.clearTimeout(foreground.timer);}
	var now=(new Date()).getTime();
	foreground.timer=window.setTimeout("jsf_animate_opacity('"+id+"','"+dur+"',"+now+",0,50)",10);
}

function jsf_slideon(id,dur,hv,start,end){
	var obj=document.getElementById(id);
	if (obj.timer) window.clearTimeout(obj.timer); 
	var now=(new Date()).getTime();
	obj.timer=window.setTimeout("jsf_animate_pos('"+id+"','"+hv+"',"+dur+",(new Date()).getTime(),"+start+","+end+")",1);
	
}
function jsf_fadeon(id,dur){
	var obj=document.getElementById(id);
	if (obj.timer) window.clearTimeout(obj.timer); 
	var now=(new Date()).getTime();
	obj.timer=window.setTimeout("jsf_animate_opacity('"+id+"','"+dur+"',"+now+",0,100)",1);
}

function jsf_fadeoff(id,dur){
	var obj=document.getElementById(id);
	if (obj.timer) window.clearTimeout(obj.timer); 
	var now=(new Date()).getTime();
	obj.timer=window.setTimeout("jsf_animate_opacity('"+id+"','"+dur+"',"+now+",100,0)",1);
}
/**/
function jsf_himeme(e){
	document.getElementById(e).style.display="none";
}
function jsf_togglevisibility(e) {	
	if (document.getElementById(e).style.display=="none"){
		document.getElementById(e).style.display="block";
		document.getElementById('urlbody').focus();/*purely to remove line from around button, focus must be an input form element to work*/
	}else{
		document.getElementById(e).style.display="none";
		document.getElementById('urlbody').focus();
	}
}
function jsf_togglebar(id) {	
	var but="togb_"+id;
	var con="togc_"+id;
	/*the outline is now removed from the button with css a:focus { outline: none; }
	document.getElementById("togb_"+id).blur();
	document.getElementById("togb_"+id).hideFocus=true;*/
	if (document.getElementById(con).style.display=="none"){
		document.getElementById(but).className="togbutopen";
		//document.getElementById("togc_"+id).style.display="block";
		$("#"+con).slideDown(300);
		/*if ("togc_"+id=="togc_cont"){window.scrollBy(0,100);}
		window.scrollBy(0,100);
		if ("togc_"+id!=="togc_dev"){window.scrollBy(0,120);}*/
	}else{
		document.getElementById(but).className="togbutshut";
		//document.getElementById(togglecontent).style.display="none";
		$("#"+con).slideUp(100);
		/*document.getElementById('pageinput').focus();*/
	}
}
function jsf_togglebar_jumptoandopen(id,anchor){
	document.getElementById("togb_"+id).className="togbutopen";
	$("#togc_"+id).slideDown(300);
	window.location.hash=anchor;
}
function jsf_loadtabs(){
	jsf_clicktab(1);
}
function jsf_clicktab(e) {
	for (i=1; i<10; i++) {
		if (document.getElementById('tab'+i)==null){break;}
		if (e==i){
			document.getElementById('tab'+i).style.display="block";
			document.getElementById('tabbut'+i).className="pagetabon";
		}else{
			document.getElementById('tab'+i).style.display="none";
			document.getElementById('tabbut'+i).className="pagetaboff";
		}
	}
}
function jsf_newwin(url,winprops)  {
	mywin=window.open(url,"win",winprops);//mywin.moveTo(0,0);
	mywin.focus();
}

function jsf_helpwin(url)  {
	mywin=window.open(url,"_helpwin","toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,width=800,height=420");//mywin.moveTo(0,0);
	mywin.focus();
}

function jsf_selectall(id){
    document.getElementById(id).focus();
    document.getElementById(id).select();
}
function jsf_jumptohref(loc){
	window.location.href=loc.value;
}


/* FORM VALIDATION */

function jobsearchbydd_validate(){
	if (document.getElementById('type').value=='' && document.getElementById('sector').value=='' && document.getElementById('location').value==''){
		alert ("Please select at least one option");
	}else{
		return true;	
	}	
	return false;
	/*
	if (document.getElementById('sbs_min').value==''){
		alert ("Please select your minimum salary");
	}else if (document.getElementById('sbs_max').value==''){
		alert ("Please select your maximum salary");
	}else{
		return true;	
	}	
	return false;*/
}
function js_validateform(fm){



	if (fm.formid.value == "processcontact"){
		
		
		if (fm.formname.value == "contact form" || fm.formname.value == "job application" || fm.formname.value == "job referal" || fm.formname.value == "candidate registration" || fm.formname.value == "candidate referal"){
	
			if ((!emailCheck(fm.email.value)) && fm.tel.value==""){
				alert("Please enter a phone number or a valid email address.");
			}else if (fm.chalresponse.value==""){
				alert("Please type the words you see in the pictures.");
			}else{
				fm.submit();
			}

		}else if (fm.formname.value == "xcap reg form"){
			
			if (fm.fname.value==""){
				alert ("Please enter your first name");
			}else if (fm.sname.value==""){
				alert ("Please enter your surname");

			}else if (fm.add1.value==""){
				alert ("Please enter the first line of your address");
			}else if (fm.postcode.value==""){
				alert ("Please enter your postcode");
			}else if (fm.nohome.value=="" && fm.nowork1.value=="" && fm.mobile1.value==""){
				alert ("Please enter at least one telephone number");
			
			}else if (!emailCheck(fm.email.value)){
				alert ("Please enter a valid email address");

			}else if (fm.chalresponse.value==""){
				alert("Please type the words you see in the pictures.");
			}else{
				fm.submit();
			}
			
		}else if (fm.formname.value == "quote form"){
			
			 if (fm.name.value==""){
				alert("Please enter your name.");
			}else if (!emailCheck(fm.email.value)){
				alert("Please enter a valid email address.");
			}else if (fm.tel.value==""){
				alert("Please enter a phone number.");
			}else{
				fm.submit();
			}
			
		}else if (fm.formname.value == "email subscribe form"){
		
			if (!emailCheck(fm.email.value)){
				alert("Please enter a valid email address.");
			}else{
				fm.submit();
			}
	
		}else if (fm.formname.value == "recruitment form"){
	
			if (fm.position.value=="none_selected"){
				alert("Please select a position.");
			}else if (fm.name.value==""){
				alert("Please enter your name.");
			}else if (!emailCheck(fm.email.value)){
				alert("Please enter a valid email address.");
			}else if (fm.tel.value==""){
				alert("Please enter a phone number.");
			}else if (fm.age.value==""){
				alert("Please enter your age.");
			}else if (fm.address.value==""){
				alert("Please enter your address.");
			}else if (fm.experience.value==""){
				alert("Please include your experience.");
			}else{
				fm.submit();
			}
	 

		}else if (fm.formname.value == "question form"){
			
			if (fm.name.value==""){
				alert("Please enter your name.");
			}else if (!emailCheck(fm.email.value)){
				alert("Please enter a valid email address.");
			}else if (fm.question.value==""){
				alert("Please include your question.");
			}else{
				fm.submit();
			}
		
		}else if (fm.formname.value == "custom form"){
			
			if (!emailCheck(fm.email.value)){
				alert("Please enter a valid email address.");
			}else if (fm.comments.value==""){
				alert("Please include your message.");
			}else{
				fm.submit();
			}
		}
	}else{
		return "";
	}
	return "";
}
function checkradio(nm) {
    var c = -1;
    for (var i=nm.length-1; i > -1; i--) {
        if (nm[i].checked) {c = i; i = -1;}
    }
    if (c > -1) return nm[c].value;
    else return null;
}

function jsf_showemaillinkv1(enco1, enco2, email1, email2, email3, email4){
	emailE = email1 + email2 + '<code class=\"link\">@</code>' + email3 + email4;
	document.write('<a href="mailto:' + enco1 + enco2 + '?subject=website contact">' + emailE + '</a>')
}
function emailCheck(e){  
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;     
    /*var emailPattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;*/
    return emailPattern.test(e);  
  }  

function emailCheck_old (emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return false;
		}
	}

	if (user.match(userPat)==null) {
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
			}
		}
		return true;
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return false;
		}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		return false;
	}

	if (len<2) {
		return false;
	}

	return true;
}
/* FORM VALIDATION */

/*unused*/

//function rollon(e){document.body.style.cursor='pointer';}
//function rolloff(e){document.body.style.cursor='default';}
//function setpicroot(u){jsv_picroot=u;}
//function setpicid(n){jsv_swmgal_curpic=n;}
//var jsv_picroot;

function NewWindow(mypage,myname,w,h,scroll,pos){
	if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
	if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
	else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20;}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win=window.open(mypage,myname,settings);
}

