﻿//***************************************************************************************
//Utility function that gets an element based on a string representation of it's Id.
function getElement(ps_Id)
{
	if(document.getElementById)
	{ 
		return document.getElementById(ps_Id);
	}
	return document.all(ps_Id);
}
//***************************************************************************************

//This function is used to show menu child elements.
function showSub(ps_Id)
{    
    // po_Element stores the 'first' nested div section (within the li referenced by ps_id), as this is what needs to be shown.
    var po_Element = getElement(ps_Id).getElementsByTagName('div')[0];
    if (!po_Element) return;
    // pb_HasFurtherElements is used to check if the nested ul has any further elements, if not nothing needs to be shown.
    var pb_HasFurtherElements = po_Element.getElementsByTagName('ul')[0];
    if (pb_HasFurtherElements.innerHTML != '') { po_Element.className = po_Element.className.replace('none','block'); }
}
//***************************************************************************************
//This function is used to hide menu child elements.
function hideSub(ps_Id)
{   
    // po_Element stores the 'first' nested div section (within the li referenced by ps_id), as this is what needs to be hidden.
    var po_Element = getElement(ps_Id).getElementsByTagName('div')[0];    
    if (!po_Element) return;
    // pb_HasFurtherElements is used to check if the nested ul has any further elements, if not nothing needs to be hidden.
    var pb_HasFurtherElements = po_Element.getElementsByTagName('ul')[0];
    if (pb_HasFurtherElements.innerHTML != '') { po_Element.className = po_Element.className.replace('block','none'); }    
}
//***************************************************************************************

//This function toggles the display status of the passed in string id element reference.
function toggleDisplay(po_Element, ps_DisplayType)
{
	//NOTE: First check we have the style attribute to play with.
	if(!po_Element.style.display) return;
	po_Element.style.display = ps_DisplayType;
	return;

}
//***************************************************************************************
//This function looks at the document contained within the IFrame and sets the container frame so we get no scroll bar.
function calcHeight(strIframe)
{
    if(!strIframe) strIframe = 'the_iframe';
    if(!getElement(strIframe)) return;
    var objIFrame = getElement(strIframe);
    //find the height of the internal page
    var the_height = objIFrame.contentWindow.document.body.scrollHeight;

    //change the height of the iframe
    objIFrame.height = the_height;
}
//***************************************************************************************
//These functions set our rollover menu, and timeout status.
var glbTimeOut
function openMenu(ps_Id)
{
    var po_Element = getElement(ps_Id);
	if(!po_Element.style.display) return;
	clearTimeout(glbTimeOut);
	po_Element.style.display = 'block';
	return;
}
function closeMenu(ps_Id)
{
    var po_Element = getElement(ps_Id);
    if(!po_Element.style.display) return;
	glbTimeOut = setTimeout('doClose(\'' + ps_Id + '\')', 500);  
}
function doClose(ps_Id)
{ 
   var po_Element = getElement(ps_Id);    
   po_Element.style.display = 'none'; 
}
//***************************************************************************************


//***************************************************************************************
//These functions set our rollover menu, and timeout status.
var siteMenuTimout;
var siteMenuTimoutFade;

function openChildMenu(ps_Id,pa_ChildMenu,pb_HideSelectElements,pa_SelectIdArray)
{    
    if(pa_ChildMenu)
    {
        //First tidy up any open menus.
        for (var i = 0; i < pa_ChildMenu.length; i++)
        {        
            doChildClose(pa_ChildMenu[i]); 
        }
    }    
    var po_Element = getElement(ps_Id);    
    if(!po_Element.className) return;
    clearTimeout(siteMenuTimout);    
    clearTimeout(siteMenuTimoutFade);    
    po_Element.className = po_Element.className.replace('none','block');                
    //Turn any select elements off.
    if ((pb_HideSelectElements) && (pa_SelectIdArray))
    { 
        toggleSelectElements('hidden', pa_SelectIdArray);
    }
    else if ((pb_HideSelectElements) && (!pa_SelectIdArray))
    { 
        toggleSelectElements('hidden');
    }

	return;
}
function cancelClose()
{
    clearTimeout(siteMenuTimout);
    clearTimeout(siteMenuTimoutFade);
}

function closeChildMenu(ps_Id, pb_Opacity)
{
    var po_Element = getElement(ps_Id);    
    if(!po_Element.className) return;
    /*
    if (!pb_Opacity) 
    {
        siteMenuTimoutFade = setTimeout('doOpacity(\'' + ps_Id + '\',100,0,100)', 300);
    }
    else
    {
        if (pb_Opacity == 'true') { siteMenuTimoutFade = setTimeout('doOpacity(\'' + ps_Id + '\',100,0,100)', 300); };
    } 
    */
    if (pb_Opacity) { siteMenuTimoutFade = setTimeout('doOpacity(\'' + ps_Id + '\',100,0,100)', 300); };  
    siteMenuTimout = setTimeout('doChildClose(\'' + ps_Id + '\')', 400);
    return;
}

function doChildClose(ps_Id)
{
   var po_Element = getElement(ps_Id);   
   if(!po_Element.className) return;
   po_Element.className = po_Element.className.replace('block','none');
    //Turn any select elements back on.
    toggleSelectElements('visible');    
   return;
}
function getElementLeft(Elem) {
		var elem;
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		xPos = elem.offsetLeft;
		tempEl = elem.offsetParent;
  		while (tempEl != null) {
  			xPos += tempEl.offsetLeft;
	  		tempEl = tempEl.offsetParent;
  		}
		return xPos;
}

function reAdjustSubMenuXPos(ps_MT, ps_Id, psSub_Id, ps_AlgnReq_Id)
{    
   var po_MenuTitle = getElement(ps_MT);
   var po_Element = getElement(ps_Id);
   var po_SubElement = getElement(psSub_Id);
   var po_AlignReqd = getElement(ps_AlgnReq_Id);
   if(!po_Element.className) return;   

   if (po_AlignReqd.value != 'N') // Check to see if the sub menu needs re-aligning.
   {
       var int_MenuXPos = getElementLeft(ps_Id);   
       //alert('int_MenuXPos = ' + int_MenuXPos);    
       var int_SubMenuXPos = int_MenuXPos - po_MenuTitle.offsetWidth;
       /*alert('po_MenuTitle.offsetWidth = ' + po_MenuTitle.offsetWidth);    
       alert('int_SubMenuXPos = ' + int_SubMenuXPos);    */
       
       var int_SubMenuLength = 0;
              
       var aElement = document.getElementById(psSub_Id).getElementsByTagName("a");
       for (var i=0; i<aElement.length; i++)   
       { 
            var a_Text = aElement[i].innerHTML;
            var a_Length = a_Text.length;
            int_SubMenuLength += (a_Length * 2) + 17;        
       }
       //alert('int_SubMenuLength = ' + int_SubMenuLength);    
       var int_NewSubMenuXPos = int_SubMenuXPos - int_SubMenuLength;
       //alert('var int_NewSubMenuXPos = int_SubMenuXPos - int_SubMenuLength : ' + int_NewSubMenuXPos);           
                     
       if (int_NewSubMenuXPos <= 0) {         
            int_SubMenuXPos = int_SubMenuXPos;
       }
       else if ((int_NewSubMenuXPos + int_SubMenuLength + int_MenuXPos) > 940) {
            int_SubMenuXPos = int_MenuXPos - int_SubMenuLength - 25;
       }
       else
       {
            int_SubMenuXPos = int_NewSubMenuXPos;            
       }       
       //alert('po_Element.style.left = ' + po_Element.style.left);
       po_Element.style.left = int_SubMenuXPos + 'px';       
       //alert('po_Element.style.left = ' + po_Element.style.left);       
       po_AlignReqd.value = 'N';
   }      
   
}
//***************************************************************************************

//***************************************************************************************
//These functions animate our height for a selected element.
function doHeight(ps_Id, pi_HeightStart, pi_HeightEnd, pi_MilliSeconds)
{
    var li_Speed = Math.round(pi_MilliSeconds / 100); 
    var li_Timer = 1; 
    
    //If our start and end heights are the same just return.
    if(pi_HeightStart == pi_HeightEnd) return;
    
    //Append our Height Style attribute if we don't have one.
    if(!getElement(ps_Id).style.height)
    {
        getElement(ps_Id).style.height == ((pi_HeightStart > pi_HeightEnd)? pi_HeightStart.toString() : pi_HeightEnd.toString()) + "px";
    }
    
    if(pi_HeightStart > pi_HeightEnd)
    {
        for(i = pi_HeightStart; i >= pi_HeightEnd; i--)
        { 
            setTimeout("changeHeight('" + ps_Id + "'," + i + ")",(li_Timer * li_Speed)); 
            li_Timer++; 
        } 
    }
    else if(pi_HeightStart < pi_HeightEnd)
    { 
        for(i = pi_HeightStart; i <= pi_HeightEnd; i++) 
        { 
            setTimeout("changeHeight('" + ps_Id + "'," + i + ")",(li_Timer * li_Speed)); 
            li_Timer++; 
        } 
    } 
}
//Change the height.
function changeHeight(ps_Id, pi_Height)
{ 
    var lo_Style = getElement(ps_Id).style; 
    lo_Style.height = pi_Height + "px"; 
} 
//***************************************************************************************
//These functions set our opacity level for the selected element.
function doOpacity(ps_Id, pi_OpacityStart, pi_OpacityEnd, pi_MilliSeconds)
{ 
    //li_Speed for each frame 
    var li_Speed = Math.round(pi_MilliSeconds / 100); 
    var li_Timer = 0; 
    
    //If our start and end opacity are the same just return.
    if(pi_OpacityStart == pi_OpacityEnd) return;
    
    //Append our Opacity Style attributes if we don't have any.
    if(!getElement(ps_Id).style.opacity)
    {
        getElement(ps_Id).style.opacity == ((pi_OpacityStart > pi_OpacityEnd)? pi_OpacityStart.toString() : pi_OpacityEnd.toString());
    }

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(pi_OpacityStart > pi_OpacityEnd)
    { 
        for(i = pi_OpacityStart; i >= pi_OpacityEnd; i--)
        { 
            setTimeout("changeOpacity('" + ps_Id + "'," + i + ")",(li_Timer * li_Speed)); 
            li_Timer++;
        } 
    }
    else if(pi_OpacityStart < pi_OpacityEnd)
    { 
        for(i = pi_OpacityStart; i <= pi_OpacityEnd; i++) 
        { 
            setTimeout("changeOpacity('" + ps_Id + "'," + i + ")",(li_Timer * li_Speed)); 
            li_Timer++;
        } 
    }    
} 
//Change the opacity for different browsers 
function changeOpacity(ps_Id, pi_Opacity)
{ 
    var lo_Style = getElement(ps_Id).style; 
    lo_Style.opacity = (pi_Opacity / 100); 
    lo_Style.MozOpacity = (pi_Opacity / 100); 
    lo_Style.KhtmlOpacity = (pi_Opacity / 100); 
    lo_Style.filter = "alpha(opacity=" + pi_Opacity + ")"; 
} 
//***************************************************************************************


//***************************************************************************************
// Function used hide all HTML select menus in <= IE6 when CSS menus open behind windowed elements.
function toggleSelectElements(toState, reqSelects)
{   
    if ((navigator.appVersion.indexOf("MSIE")) &&
        (parseFloat(navigator.appVersion.split("MSIE")[1]) < 7))
    {
        if (reqSelects) 
        {
            for (var slct = 0; slct < reqSelects.length; slct++)
            {
                document.getElementById(reqSelects[slct]).style.visibility = toState;
            }
        }
        else
        {
            var colNonFrm = document.getElementsByTagName("select");            
            for (var n = 0; n < colNonFrm.length; n++)
            {
                colNonFrm[n].style.visibility = toState;
            }        
        }        
        /*for (var S = 0; S < document.forms.length; S++)
        {
            for (var R = 0; R < document.forms[S].length; R++)
            {
                if (document.forms[S].elements[R].options) document.forms[S].elements[R].style.visibility = toState;
            }
        }*/        
    }
}
//***************************************************************************************


//***************************************************************************************
// function used to check that a string matches the UK postcode rules
function checkUKPostcode(val)
{
  var re = new RegExp('^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$');
  if (val.match(re))
  {
    return true;
  }
  else
  {
    return false;
  }
}

function checkUKPostcodeFrontPortion(val)
{
  // 13/03/2007 CL
  // simple regex to check for AANA or AANN postcode first portion pattern
  //var re = new RegExp('^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))))$');
  var re = new RegExp('^[A-Za-z]{1,2}[0-9]{1,2}[A-Za-z]{0,1}$'); 
  
  if (val.match(re))
  {
    return true;
  }
  else
  {
    return false;
  }
}

function ValidatePostcodeFrontPortion(val, errorMsg)
{
    if(val == '') {
        alert(errorMsg);
        return false;
    }
    else
    {
        if(!checkUKPostcodeFrontPortion(val))
        {
            alert(errorMsg);
            return false;                            
        }
        else
        {
            return true;
        }
    }
}
//***************************************************************************************


//***************************************************************************************
//function used to store the clickId in the hdn form field found by the id passed in
function QuickSearchClick(hdnId, clickId, dvMainId, manuTx, dvMenu) {
    document.getElementById(hdnId).value = clickId;
    document.getElementById(dvMainId).innerText = manuTx;
    closeChildMenu(dvMenu);
}
//***************************************************************************************

//***************************************************************************************
//function used to handle the rendering of the google maps image from the long ang lat values for each of the location s
function GoogleMapsLoad(lon, lat, zoom) {
    if(document.getElementById("GoogleMap")) {
        if (GBrowserIsCompatible()) {
            //create the map, set the centre, and add the user controls
            var map = new GMap2(document.getElementById("GoogleMap"));
            map.setCenter(new GLatLng(lat, lon), zoom); 
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());

            //now get the bounds for the map in order to add out reference point
            bounds = map.getBounds();
            var southWest = bounds.getSouthWest();
            var northEast = bounds.getNorthEast();
            var lngSpan = northEast.lng() - southWest.lng();
            var latSpan = northEast.lat() - southWest.lat();
            var point = new GLatLng(lat, lon);
            map.addOverlay(new GMarker(point));
        }  
    }    
}
//***************************************************************************************

//***************************************************************************************
function RequiredTextField(id) {
    var e=document.forms['aspnetForm'].getElementsByTagName("input");
        for(var i=0;i<e.length;i++){
            if((e[i].id.indexOf(id) > -1) && (e[i].type) && (e[i].id.indexOf('btn') == -1)) {
                
                if(e[i].name.indexOf('Location') > -1)
                {
                    if( !ValidatePostcodeFrontPortion(e[i].value,'Please enter the first part of your postcode ie IP3'))
                        return false;
                }
                else if(e[i].value == '') {
                    alert('You must enter a ' + id);
                    return false;
                }
                
                return true; 
            }
        }
}
//***************************************************************************************

//***************************************************************************************
function comparer_click(safeUrl,id,comparer) {
    url = safeUrl + "&BookmarkId=" + id + "&AddToComparer=" + comparer;
    window.location.href = url;
}
//***************************************************************************************
function strContains(strToSearch, strToSearchFor)
{
    return strToSearch.indexOf(strToSearchFor) >= 0 ? true : false
}
//***************************************************************************************

//***************************************************************************************
//Utility function that gets Browser Type.
function getBrowser()
{
    var bname = "";
    var bversion = navigator.appVersion.substring(0, navigator.appVersion.indexOf('('));
    bversion = bversion.replace(".", "");
    if (navigator.userAgent.indexOf("MSIE") != -1)
    {
     bname = "IE";
    }
    else if (navigator.userAgent.indexOf("Opera") != -1)
    {
     bname = "OPERA";
    }
    else if ((navigator.appName.indexOf("Netscape") != -1) || parseFloat(navigator.appVersion >= 3.0))
    {
     bname = "NETSCAPE";
    }
    else
    {
     bname = "UNKNOWN_BROWSER";
    }
    return bname + bversion;
}
//***************************************************************************************

//***************************************************************************************
//Function that displays current time.
var timerID = null;
var timerRunning = false;
var timerObject;

function stopclock (){
    if(timerRunning) clearTimeout(timerID);
    timerRunning = false;
}

function startclock (reqObject) {
    if (reqObject)
    {
        timerObject = reqObject;
        stopclock();
        showtime();
    }
}

function showtime () {
    var now = new Date();
    var todaysDate = now.getDate() + "/" + (now.getMonth() + 1) + "/" + now.getFullYear();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds()
    var timeValue = "" + ((hours >12) ? hours -12 :hours)
    timeValue += ((minutes < 10) ? ":0" : ":") + minutes
    timeValue += ((seconds < 10) ? ":0" : ":") + seconds
    timeValue += (hours >= 12) ? " PM" : " AM"
    timerObject.innerHTML = todaysDate + " - " + timeValue;
    timerID = setTimeout("showtime()",1000);
    timerRunning = true;
}
//***************************************************************************************