// JavaScript Document

 $(document).ready(function() {
 
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}
 
	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
 
	$("ul#topnav li .sub").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);
});

$(document).ready(function(){
$('.texthideP').toggle(function()
	{$(this).next("div.texthideDiv").slideDown(200);}, 
function(){
	$(this).next("div.texthideDiv").slideUp(300);
});
});

 
function validateRoute()
		{
			
			eM="You havent:\n"; eF=0; eR=""; // Used to allert users to what they haven't done
			iM="Contain illegal characters:\n"; iF=0; iR=""; // Used to alert users to illegal chars
			dV = "---------------------------------------------------------------------------      \n";
			pR="Oops,\nThere seems to be a few problems with the form you've just filled in.\n"+dV
			p1e=0;
			var safeChars= /^[a-zA-Z]+$/ // Only letters
			var numbers= /^[0-9]+$/ // Only numbers and space
			
			//Validate Postcode
			ePcode = document.getElementById('myform').z1.value.toUpperCase()
			var eMsg1 = ". entered a valid postcode.\n";
			var eMsg2 = ". Your postcode is formatted incorrectly.\n";	
			//if(ePcode.length==6 || ePcode.length==7)
			//{
				if(ePcode.length==6)
				{
					if(!safeChars.test(ePcode.charAt(0))){var p1e=1;}
					if(!safeChars.test(ePcode.charAt(1))){var p1e=1;}
					if(!numbers.test(ePcode.charAt(2))){var p1e=1;}
					if(!numbers.test(ePcode.charAt(3))){var p1e=1;}
					if(!safeChars.test(ePcode.charAt(4))){var p1e=1;}
					if(!safeChars.test(ePcode.charAt(5))){var p1e=1;}
				}
				else
				{
					if(!safeChars.test(ePcode.charAt(0))){var p1e=1;}
					if(!safeChars.test(ePcode.charAt(1))){var p1e=1;}
					if(!numbers.test(ePcode.charAt(2))){var p1e=1;}
					if(!numbers.test(ePcode.charAt(3))){var p1e=1;}
					if(!numbers.test(ePcode.charAt(4))){var p1e=1;}
					if(!safeChars.test(ePcode.charAt(5))){var p1e=1;}
					if(!safeChars.test(ePcode.charAt(6))){var p1e=1;}
				}
			//}
			if (ePcode==""){eF=eF+1;eR=eR+eF+eMsg1;}
			if (p1e==1){iF=iF+1;iR=iR+iF+eMsg2;}

			if(eF==0&iF==0)
			{
				if(ePcode.length==6)
				{
					document.getElementById('myform').z1.value=ePcode.charAt(0)+ePcode.charAt(1)+ePcode.charAt(2)+" "+ePcode.charAt(3)+ePcode.charAt(4)+ePcode.charAt(5)
				}
				else
				{
					document.getElementById('myform').z1.value=ePcode.charAt(0)+ePcode.charAt(1)+ePcode.charAt(2)+ePcode.charAt(3)+" "+ePcode.charAt(4)+ePcode.charAt(5)+ePcode.charAt(6)
				}				
				document.getElementById('myform').setAttribute("action","http://www.mapquest.co.uk/mq/directions/directions.do");
				document.getElementById('myform').submit();
			}
			else
			{
				notDone=""; illegalChars="";
				if(eF!=0) notDone = eM+dV+eR+dV;
				if(iF!=0) illegalChars = iM+dV+iR+dV;
				alert(pR+notDone+illegalChars+"Please correct these problems before continuing.");
				document.getElementById('myform').z1.focus
				return false
			}
			
			
		}

		var aSet=null
		
		function doSearch()
		{
			actionUrl="search.asp?search="&document.getElementById("search").value
			document.getElementById('myForm').setAttribute("action",actionUrl);
			return true
		}
		
		function setOpacity(itemId,value) 
		{
			document.getElementById(itemId).style.opacity = value/10;
			document.getElementById(itemId).style.filter = 'alpha(opacity=' + value*10 + ')';
		}


		function openWin(pageName,imID,dWidth,dHeight,doScroll)
		{
			if(isNaN(dWidth)==true){dWidth=100;}
			if(isNaN(dHeight)==true){dHeight=100;}
			if(doScroll==1){doScroll=",scrollbars=yes";}else{doScroll="";}
			winTop = ((screen.height-dHeight)/2)-13;
			winLeft = ((screen.width-dWidth))-100;
			addages = "top="+(winTop)+",left="+(winLeft)+",status=yes,width="+dWidth+",height="+dHeight+doScroll
			window.open(pageName,imID,addages);
		}
	
	
		function findactive(whichSelect, setWhichOption)
			{
				if (whichSelect=='' || setWhichOption=='')return;
				var a = document.getElementById(whichSelect)
				var numberOfOptions = a.length
				for (i = 1; i < numberOfOptions; i++){
				if (a.options[i].value == setWhichOption) a.options[i].selected = true;}

				if(document.getElementById("myForm")!=null){document.getElementById("myForm").getAttribute(whichSelect).className="hilite"}
			}

	
		function guessTextSize(bestGuess)
			{
				var docHeight=document.getElementById("pageContent").offsetHeight
				var tolerance = 10 //Percentage tolerance each way
				var medium = bestGuess-0
				//var smaller = (medium/100)*75
				var larger = (medium/100)*177.6
				var largest = (medium/100)*294.6
				curDim="medium"
				//if (docHeight>=(smaller-((smaller/100)*tolerance))&&docHeight<=(smaller+((smaller/100)*tolerance))){curDim="smaller"}
				if (docHeight>=(larger-((larger/100)*tolerance))&&docHeight<=(larger+((larger/100)*tolerance))){curDim="larger"}
				if (docHeight>=(largest-((largest/100)*tolerance))&&docHeight<=(largest+((largest/100)*tolerance))){curDim="largest"}
				return curDim
			}

		
		
		
		function switchHeight()
			{
				var curSize=guessTextSize(aGuess)
				if(aSet!=curSize)
				{
					aSet=curSize
					switch(curSize)
					{
					case "smaller": nTop=-2627;pW=360;pH=166;bI="url(images/sn2Smaller.gif)";ted=Math.round(((aGuess+511)/100)*75)
						break;
					  case "larger": nTop=-2627;pW=652;pH=472;bI="url(images/sn2Larger.gif)";ted=Math.round(((aGuess+511)/100)*177.6)
						break;
					  case "largest": nTop=-3470;pW=852;pH=796;bI="url(images/sn2Largest.gif)";ted=Math.round(((aGuess+411)/100)*294.6)
						break;
					  default: nTop=-1883;pW=480;pH=258;bI="url(images/sn2Medium.gif)";ted=Math.round((aGuess+511))
					}
					a=document.getElementById("offices").style
					b=document.getElementById("pic").style
					a.marginTop=nTop
					a.backgroundImage=bI
					b.width=pW
					b.height=pH
				}
				if(aSet==null){aSet=curSize}
				callIt()
			}

		
		function callIt()
			{
				setTimeout('switchHeight()',5);
			}
			
			
		function writeEmail(copy,myStyle)
			{
				copy=copy+"@fra"+"nklins-s"+"ols.co.uk"
				document.write("<a href='mailto:"+copy+"' style='"+myStyle+"'>"+copy+"</a>")
			}
			
						
		function setTransp(obj,opacity)
			{
				a=document.getElementById(obj)
				a.style.filter = "alpha(opacity:"+opacity+")";
				a.style.MozOpacity = opacity/100;
				a.style.KHTMLOpacity = opacity/100;
				a.style.opacity = opacity/100;
			}
			
		window.defaultStatus = "Franklins Solicitors: Make us your first point of call, not your last stop!";
