/* GENERIC FUNCTIONS */
$(document).ready(function(){
	
	$('#dispmesbut').click(function(){jsf_toggledisplaymes();});
	$('.togglelink').click(function(){$(this).next('.toggletext').slideToggle(100);});
	
	//$(".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);});

	$("img.roll").hover(function(){this.src=this.src.replace("-norm","-roll");},function(){this.src=this.src.replace("-roll","-norm");});
	//$('.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 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(togglebutton,togglecontent) {	
	/*the outline is now removed from the button with css a:focus { outline: none; }
	document.getElementById(togglebutton).blur();
	document.getElementById(togglebutton).hideFocus=true;*/
	if (document.getElementById(togglecontent).style.display=="none"){
		document.getElementById(togglebutton).className="togbutopen";
		//document.getElementById(togglecontent).style.display="block";
		$("#"+togglecontent).slideDown(300);
		/*if (togglecontent=="togc_cont"){window.scrollBy(0,100);}
		window.scrollBy(0,100);
		if (togglecontent!=="togc_dev"){window.scrollBy(0,120);}*/
	}else{
		document.getElementById(togglebutton).className="togbutshut";
		//document.getElementById(togglecontent).style.display="none";
		$("#"+togglecontent).slideUp(100);
		/*document.getElementById('pageinput').focus();*/
	}
}
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{
				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 if (fm.formid.value == "processbasket"){

		if (fm.task.value == "gotocheckout"){
			if (checkradio(fm.delivery)>0){
				return true;
			}else{
				alert ("Please select a delivery method");
				return false;
			}
		}
		
	}else if (fm.formid.value == "processooorder"){

		if (!checkradio(fm.quantityandprice)>0){
			alert("Please select a quantity.");
		}else if (fm.ponumber.value==""){
			alert("Please include a purchase order number.");
		}else{
			fm.submit();
		}
	}
/*
			alert(document.getElementById('delivery'));
			alert(fm.delivery.selectedIndex.value);
			//alert(document.getElementById('delivery').selectedIndex.value);

	if (document.getElementById('sbp_min').value==''){
		alert ("Please select your minimum price");
	}else if (document.getElementById('sbp_max').value==''){
		alert ("Please select your maximum price");
	}else{
		return true;	
	}	
	return false;

FormRef.comments.focus();*/


}
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 (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);
}

