﻿//<!--

//Set up the xmlHttp object, depending on the browser, and return it
function setupXmlHttp()
{
	var xmlHttp;
  
  	try
    {    
		// Firefox, Opera 8.0+, Safari    
		xmlHttp=new XMLHttpRequest();    
	}
  	catch (e)
    {   
		// Internet Explorer    
		try
      	{      
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
		}
    	catch (e)
      	{      
			try
        	{        
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
			}
      		catch (e)
        	{        
				alert("Your browser does not support AJAX!");        
				return false;        
			}      
		}    
	}
	
	return xmlHttp;
}

//Set the cookie to store true (menu open) or false (menu shut)
function setCookie(displayType)
{
    var xmlHttp = setupXmlHttp();

//    xmlHttp.onreadystatechange=function()
//	{	
//		if(xmlHttp.readyState==4)
//    	{
//    		alert(xmlHttp.responseText);
//    	}
//	}
	
	xmlHttp.open("GET","/setCookie.aspx?display=" + displayType,true);
	xmlHttp.send(null);
}

// Turn on / off the advanced search options
function toggleSearchOptions(displayType)
{
	document.getElementById("advanced").style.display = displayType;
			    
	if(displayType == 'inline')
	{
	    document.getElementById("advancedTestJS").innerHTML = "<a href=\"#\" onclick=\"toggleSearchOptions('none');\" class=\"minusSymbol\"><span class=\"greenText\">fewer search options</span></a>";
	    //document.getElementById("advanced_switch_more").style.visibility = 'hidden';
	    //document.getElementById("advanced_switch_fewer").style.visibility = 'visible';
	}
	else
	{
		document.getElementById("advancedTestJS").innerHTML = "<a href=\"#\" onclick=\"toggleSearchOptions('inline');\" class=\"plusSymbol\"><span class=\"greenText\">more search options</span></a>";
		//document.getElementById("advanced_switch_more").style.visibility = 'visible';
	    //document.getElementById("advanced_switch_fewer").style.visibility = 'hidden';
    }
    
    //Save the current state of the advanced options in a cookie and set a session variable using AJAX
    
    if(displayType == 'inline')
    {
        setCookie(true);
    }
    else
    {
        setCookie(false);
    }
}

//Open a popup window for the editor
function openPopup(url)
{
	width=520;
	height=500;
	x=parseInt(screen.width/ 2.0) - (width /2.0);
	y=parseInt(screen.height/ 2.0) - (height /2.0);
	
	var win=window.open(url,"editorPopup","top="+y+",left="+x+",scrollbars=no,dialog=yes,minimizable=no,modal=yes,width="+width+",height="+height+",resizable=no");
}

//Popup window for other popups
function openAPopup(url, x, y)
{
	width=x;
	height=y;
	x=parseInt(screen.width/ 2.0) - (width /2.0);
	y=parseInt(screen.height/ 2.0) - (height /2.0);
	
	var win=window.open(url,"popup","top="+y+",left="+x+",scrollbars=no,dialog=yes,minimizable=no,modal=yes,width="+width+",height="+height+",resizable=no");
}

//Show / hide divs on FAQ pages
function toggleDiv(theDiv, totalDivs, spanNo)
{
	
	//Toggle the appropriate div
	if(document.getElementById(theDiv).style.display == 'none')
	{
		//Hide all divs
		for(x=0;x<totalDivs;x++)
		{
		    document.getElementById("q" + x).style.display = 'none';
		    document.getElementById("title" + x).innerHTML = document.getElementById("title" + x).innerHTML.replace(/<strong>/gi, "");
		    document.getElementById("title" + x).innerHTML = document.getElementById("title" + x).innerHTML.replace(/<\/strong>/gi, "");
		    document.getElementById("arrowImg" + x).innerHTML = "<img src='/images/faqArrow.gif' width='16' height='20' />";
		}

		//Show the selected one
		document.getElementById(theDiv).style.display = 'block';
		document.getElementById("title" + spanNo).innerHTML = "<strong>" + document.getElementById("title" + spanNo).innerHTML + "</strong>";
		document.getElementById("arrowImg" + spanNo).innerHTML = "<img src='/images/pixel.gif' width='16' height='20' />";
	}
	else
	{
		document.getElementById(theDiv).style.display = 'none';
		document.getElementById("title" + spanNo).innerHTML = document.getElementById("title" + spanNo).innerHTML.replace(/<strong>/gi, "");
		document.getElementById("title" + spanNo).innerHTML = document.getElementById("title" + spanNo).innerHTML.replace(/<\/strong>/gi, "");
		document.getElementById("arrowImg" + spanNo).innerHTML = "<img src='/images/faqArrow.gif' width='16' height='20' />";
    }
}

function initaliseDivs(totDivs)
{
	//Hide all divs
    for(x=0;x<totDivs;x++)
    {
        document.getElementById("q" + x).style.display = 'none';
        document.getElementById("title" + x).innerHTML = document.getElementById("title" + x).innerHTML.replace(/<strong>/gi, "");
        document.getElementById("title" + x).innerHTML = document.getElementById("title" + x).innerHTML.replace(/<\/strong>/gi, "");
        document.getElementById("arrowImg" + x).innerHTML = "<img src='/images/faqArrow.gif' width='16' height='20' />";
    }
}

//Show a large version of an image once the thumbnail is clicked
function showThumb(img)
{
	document.getElementById("mainImg").innerHTML = '<img src="' + img + '" height="250" title="Image of property" />';
}

//Tabbing functions
origBg = "";

function showTab(tab)
{
	for(x=1;x<=4;x++)
	{
		document.getElementById('tab' + x).style.display = 'inline';
		document.getElementById('tab' + x + 'on').style.display = 'none';
		document.getElementById('tabInfo' + x).style.display = 'none';
	}
	
	document.getElementById('tab' + tab).style.display = 'none';
	document.getElementById('tab' + tab + 'on').style.display = 'inline';
	document.getElementById('tabInfo' + tab).style.display = 'inline';
	
	if(tab == 1)
		document.getElementById('tabButtons').style.background='url(/images/tabInfoTop.gif)';
	else if(tab == 4)
		document.getElementById('tabButtons').style.background='url(/images/tabInfoTop3.gif)';
	else
		document.getElementById('tabButtons').style.background='url(/images/tabInfoTop2.gif)';
	
	document.getElementById('tabButtons').style.backgroundRepeat='no-repeat';
	document.getElementById('tabButtons').style.backgroundPosition='bottom';
}

function doChange(gfx)
{
	origBg = document.getElementById('tabButtons').style.background;
	
	if(origBg.indexOf('tabInfoTop3.gif') > -1)
		document.getElementById('tabButtons').style.background='url(/images/tabInfoTopBoth.gif)';
	else if(origBg.indexOf('tabInfoTop.gif') > -1)
		document.getElementById('tabButtons').style.background='url(/images/tabInfoTopBoth.gif)';
	else
		document.getElementById('tabButtons').style.background='url(/images/' + gfx + ')';
	
	document.getElementById('tabButtons').style.backgroundRepeat='no-repeat';
	document.getElementById('tabButtons').style.backgroundPosition='bottom';
}

function changeBack()
{
	if(origBg != "")
		document.getElementById('tabButtons').style.background=origBg;
	
	//Over-ride the above it tab 1 or 4 are now active	
	if(document.getElementById('tab1on').style.display == "inline")
	{
		document.getElementById('tabButtons').style.background='url(/images/tabInfoTop.gif)';
		document.getElementById('tabButtons').style.backgroundRepeat='no-repeat';
		document.getElementById('tabButtons').style.backgroundPosition='bottom';
	}
	if(document.getElementById('tab4on').style.display == "inline")
	{
		document.getElementById('tabButtons').style.background='url(/images/tabInfoTop3.gif)';
		document.getElementById('tabButtons').style.backgroundRepeat='no-repeat';
		document.getElementById('tabButtons').style.backgroundPosition='bottom';
	}
}

function getDistance(lat1, lon1, lat2, lon2, unit) {
	var radlat1 = Math.PI * lat1/180
	var radlat2 = Math.PI * lat2/180
	var radlon1 = Math.PI * lon1/180
	var radlon2 = Math.PI * lon2/180
	var theta = lon1-lon2
	var radtheta = Math.PI * theta/180
	var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
	dist = Math.acos(dist)
	dist = dist * 180/Math.PI
	dist = dist * 60 * 1.1515
	if (unit=="K") { dist = dist * 1.609344 }
	if (unit=="N") { dist = dist * 0.8684 }
	return dist
} 

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function _usePointFromPostcode(postcode) {
	
    _localSearch.setSearchCompleteCallback(null, 
	    function() {
			
			var theCode;
			
		    if (_localSearch.results[0])
		    {		
			    var resultLat = _localSearch.results[0].lat;
			    var resultLng = _localSearch.results[0].lng;
			    theCode = postcode;
			    var point = new GLatLng(resultLat,resultLng);
                theCode = trim(theCode);
				_lat = point.lat();
				_lng = point.lng();
		    }else{
                theCode = postcode;
                theCode = trim(theCode);
                _lat = 0;
				_lng = 0;
		    }
	    });	
		
    _localSearch.execute(postcode + ", UK");
}

function getLoc()
{
	_usePointFromPostcode(document.getElementById('ctl00_contentPh_userInput').value);
	_lat = -1;
    _lng = -1;
    _whenFinished();
}

function _whenFinished()
{
	if((_lat != -1) && (_lng != -1))
	{
		if((_lat != 0) && (_lng != 0))
		{
		    for(x=0;x<groupid.length;x++)
		    {
		        distance[x] = getDistance(_lat, _lng, latitude[x], longitude[x], "M");
		    }
		    
		    //Sort the distances
		    again = true;
		    
		    while(again == true)
		    {
		        again = false;
		        
		        for(x=0;x<groupid.length-1;x++)
		        {
		            if(eval(distance[x]) > eval(distance[x + 1]))
		            {
		                again = true;
		                
		                t1 = groupid[x];
                        t2 = franchiseName[x];
                        t3 = latitude[x];
                        t4 = longitude[x];
                        t5 = distance[x];
                        t6 = folder[x];
		                
		                groupid[x] = groupid[x+1];
                        franchiseName[x] = franchiseName[x+1];
                        latitude[x] = latitude[x+1];
                        longitude[x] = longitude[x+1];
                        distance[x] = distance[x+1];
                        folder[x] = folder[x+1];
                        
                        groupid[x+1] = t1;
                        franchiseName[x+1] = t2;
                        latitude[x+1] = t3;
                        longitude[x+1] = t4;
                        distance[x+1] = t5;
                        folder[x+1] = t6;
		            }
		        }
		    }
		    
		    document.getElementById("results").innerHTML = "Your closest franchises are:<br /><br />";
		    
		    for(x = 0;x < 3;x++)
                document.getElementById("results").innerHTML += "<a href='" + folder[x].substring(1, folder[x].length) + "'>" + franchiseName[x] + "</a> (" + Math.round(distance[x]) + " miles)<br />";
		}
		else
		    document.getElementById("results").innerHTML = "Sorry, no franchise found for this location. Please try again.";
	}
	else
	{
		window.setTimeout("_whenFinished();",500);
	}
}

function _usePointFromPostcodeSearch(postcode) {
	
    _locSearch.setSearchCompleteCallback(null, 
	    function() {
			
			var theCode;
			
		    if (_locSearch.results[0])
		    {		
			    var resultLat = _locSearch.results[0].lat;
			    var resultLng = _locSearch.results[0].lng;
			    theCode = postcode;
			    var point = new GLatLng(resultLat,resultLng);
                theCode = trim(theCode);
				_lat = point.lat();
				_lng = point.lng();
		    }else{
                theCode = postcode;
                theCode = trim(theCode);
                _lat = 0;
				_lng = 0;
		    }
	    });	
		
    _locSearch.execute(postcode + ", UK");
}

function getSearchLoc(loc, pid)
{
	if(loc.length > 0)
	{
		_usePointFromPostcodeSearch(loc);
		_lat = -1;
    	_lng = -1;
	    _whenSearchFinished(pid);
	}
	else
	{
		window.location.href = "searchresults.aspx?pageId=" + pid + "&page=1&j=ns";
	}
}

function _whenSearchFinished(pid)
{
	if((_lat != -1) && (_lng != -1))
	{
		if((_lat != 0) && (_lng != 0))
		{
			//OK
			window.location.href = "searchresults.aspx?pageId=" + pid + "&page=1&j=ns&lat=" + _lat + "&lng=" + _lng;
		}
		else
		{
			//NOT OK
			window.location.href = "searchresults.aspx?pageId=" + pid + "&page=1&j=ns";
		}
	}
	else
	{
		window.setTimeout("_whenSearchFinished(" + pid + ");",500);
	}
}

//-->