	
    //Direct user to correct page from left hand nav
    function gotoTravel(country, holiday) {
      window.location = "http://www.radiotimes.com/classified/travel/" + country + "/" + holiday + "/1.jsp";
    }
	
	function gotoPage(category, type) {
	  if (type == 'select1')    // If the submits without selecting a Location from the dropdown
	    {
		alert("Please select a location from the menu");  
		}
      else if (type == 'select2')    // If the submits without selecting a Property Type from the dropdown
        {
        alert("Please select a property type from the menu");
        }
      else if (type == 'select3')    // If the submits without selecting a Property Type from the dropdown
        {
        alert("Please select a travel service from the menu");
        }
      else if (type == 'select4')    // If the submits without selecting a brochure type from the dropdown
        {
        alert("Please select a brochure type from the menu");
        }
      else if ((type == 'insurance')||(type == 'emigration')||(type == 'clothing' && category == 'travel')||(type == 'charities'))
        {
        window.location = "http://www.radiotimes.com/classified/travel/other/" + type + "/1.jsp";			
		}
     else if (type == 'clothing' && category == 'lifestyle') {
 	window.location = "http://www.radiotimes.com/classified/lifestyle/" + type + "/1.jsp";	
}
	  else
	    {
        window.location = "http://www.radiotimes.com/classified/" + category + "/" + type + "/1.jsp";	
        }
	}
	
	// If 'brochureID' is specified in the query string, then show relevant brochure
	function showBrochureFromQueryString() {
	  var query = window.location.search.substring(1);
	  var vars = query.split("&");
	  for (var i=0;i<vars.length;i++) {
	    var pair = vars[i].split("=");
	    if (pair[0] == 'brochureID') {
				showSection(pair[1]);
	    }
	  } 
	}
	
	
    // Show/hide brochures on results page
    function showSection(id) {
      var divs = document.getElementsByTagName("div");
      for (var i=0; i<divs.length; i++ ) {
        if (divs[i].className.indexOf("brochure") == -1) continue;
        if (divs[i].getAttribute("id") != id) {
          divs[i].style.display = "none";
        } else {
          divs[i].style.display = "block";
        }
      }
    }
		
		function addSlashes(inStr) {
			return inStr.replace(/'/,"\\'");	
		}
		
		function stripSlashes(inStr) {
			return inStr.replace(/\\'/,"\'");	
		}

    var maxBrochures = 12;                      //Maximum number of brochures customer can order
    var brochureCookieStr = "";                 //String stored in cookie
    var cookieArray = new Array();              //Array containing info from string
    var cookieName = "RTClassifiedCookie";      //Name of the cookie
    var brochuresLeft = maxBrochures;           //Number of brochures user can still add to basket


    // Get string from cookie and build array.  This function is called on page load.
    function setupData()								
		{	
			if (cookieExists(cookieName)) 							
			{
				brochureCookieStr = getCookie(cookieName);
				if (brochureCookieStr == "") {
					cookieArray = new Array();
					} else {
					cookieArray = brochureCookieStr.split("|");							
					for(i=0; i<cookieArray.length; i++) 
						{
							cookieArray[i] = cookieArray[i].split(",");
                            for(j=0; j<cookieArray[i].length; j++)                            //Replace all ^ with commas (reverse was undertaken when cookie was written)
							  {																  //Added 100206
							  cookieArray[i][j] = cookieArray[i][j].replace(/\^/g, ",");
							  }																  
						}
					}
				brochuresLeft = maxBrochures - cookieArray.length;
			}
		}

	// Given the name of the cookie, returns the value (i.e. the string containing all values)
	function getCookie(searchCookie) 
		{
			var cookieString = unescape(document.cookie);
			var cookieList = cookieString.split("; ");

            var thisCookieStr = -1;

			for(counter = 0; counter < cookieList.length; counter ++)
			{
				currentCookie = cookieList[counter];
				cookieSplitIndex = currentCookie.indexOf("=");
				currentCookieName = currentCookie.substring(0,cookieSplitIndex);
				currentCookieValue = currentCookie.substring(cookieSplitIndex+1);
											
				if(currentCookieName == searchCookie)
				{
					thisCookieStr = currentCookieValue;
				}
			}
				
			return thisCookieStr;	
		}

	
	// Check if the cookie exists or not
	function cookieExists(thisCookie)
		{
			var cookieString = unescape(document.cookie);
			var cookieList = cookieString.split("; ");
		
			var doesExist = false;
			
			for(counter = 0; counter < cookieList.length; counter ++)
			{
				currentCookie = cookieList[counter];
				cookieSplitIndex = currentCookie.indexOf("=");
				currentCookieName = currentCookie.substring(0,cookieSplitIndex);
				currentCookieValue = currentCookie.substring(cookieSplitIndex+1);

				if(currentCookieName == thisCookie)
				{
					doesExist = true;
					break;
				}
			}
					
			return doesExist;		
		}
		
	// Generate the expiry date for use in writeCookie()
	function expiryDate() 
		{
			var expiryDate = new Date();					
			expiryDate.setTime(expiryDate.getTime() + 86400000);
			return expiryDate;	
		}

    // Adds a brochure to the basket
	function addBrochure(brochureId, title, imgPath, webLink, category) 	// A selected brochure is added to the array 
		{  
			if (brochuresLeft == 0)
			{
			  alert("Sorry, but you may only place up to 12 items in your basket.  To add a new brochure you must remove an item from your existing basket.");
              return false;
			}
			else if (inBasket(brochureId))
			{
			  alert("This brochure is already in your basket and cannot be added again.  Please select another.")
			  return false;
			}			
			else 
			{
			  var nextIndex = cookieArray.length;
			  cookieArray[nextIndex] = new Array();
			  cookieArray[nextIndex][0] = brochureId; 
			  cookieArray[nextIndex][1] = title;
			  cookieArray[nextIndex][2] = imgPath;
			  cookieArray[nextIndex][3] = webLink;
			  cookieArray[nextIndex][4] = category;
			  brochuresLeft -= 1;
			  writeCookie();
			}
		}

	// Removes a brochure from the basket	
    function removeBrochure(brochureId) 
		{
			for(q=0; q<cookieArray.length; q++) 
			  {
				if (cookieArray[q][0] == brochureId)
				  {
				    cookieArray.splice(q,1);
				  }
			  }
			brochuresLeft += 1;	
			writeCookie();
		}

    // Writes information stored in array (cookieArray) to cookie (RTClassifiedCookie)
	function writeCookie()									// Writes the information in the array to a cookie
		{
          brochureCookieStr = ""; 						// Clear the current cookie string before starting
          var cookieStr;
		  var temp;
		
          for(k=0; k<cookieArray.length; k++) 
		    {
			temp = cookieArray[k][1];
			temp = temp.replace(/,/g, "^");
			cookieArray[k][1] = temp;
			//cookieArray[k] = cookieArray[k].replace(/,/g, "^");                // Replace commas with ^ before writing cookie; added 10/02/06
            //alert("Cookie Array is: " + cookieArray[k]);
            cookieStr = cookieArray[k].join();
            cookieStr += "|"
            brochureCookieStr += cookieStr;
		    }
			
			brochureCookieStr = brochureCookieStr.substr(0, brochureCookieStr.length-1);
            var expiresPath = expiryDate() + "; path=/";						
            brochureCookieStr = cookieName + "=" + brochureCookieStr + ";" + expiresPath;			
            document.cookie = brochureCookieStr;
		}

	// Prints hidden fields on the order form page
    function formHiddenFields()
		{
			for(i=0; i<cookieArray.length; i++)
				{
				document.write("<input type='hidden' name='brochure" + (i+1) + "' value='" + cookieArray[i][0].substr(0,4) + "' />");
				}		
		}
	
				
    // Calculates the number of brochures in each category for the basket page
	function countBycategory(thisCategory) 
		{
		var categoryCount=0;
		
			for(i=0; i<cookieArray.length; i++)
				{
					if (cookieArray[i][4] == thisCategory)
						{
						categoryCount++;
						}
				}
		return categoryCount;	
		}


		
    // Prints brochure name and web link on the basket Thank You page
	function brochureByCategory(thisCategory)
		{
		for(i=0; i<cookieArray.length; i++)
			{
				if (cookieArray[i][4] == thisCategory)
					{
					document.write(stripSlashes(cookieArray[i][1]) + " " + cookieArray[i][3] );
					}
			}				
		}

	
	
	// Adds information to the shopping basket area
    function basketBrochLeft()
		{
			if (brochuresLeft==0)
			{
			document.write("<p>Your basket is full</p>");
			}
			else
			{
			document.write("<p>You may add another " + brochuresLeft + " brochures to your basket</p>");
			}
		}

		
	function printBrochureTitles() 
		{
			document.write("So far you have added:");
				for(j=0; j<cookieArray.length;j++) 
					{
					document.write(stripSlashes(cookieArray[j][1]));					
					}	
		}	



        // Check the details of the user details form before submission    	
		function checkBrochForm(thisForm)
		{
			var missingField = true;
			
			// check title dropdown
			var titleSelected = false;
			for (i=1;i<thisForm.userTitle.options.length;i++) {
				if (thisForm.userTitle.options[i].selected) {
					var titleSelected = true;
				}			
			}
			if (!titleSelected) {
				missingField = false;
			}

			if(thisForm.firstname.value == '' || thisForm.surname.value == '' || thisForm.address1.value == '' || thisForm.town.value == '' || thisForm.postcode.value == '') {
				missingField = false;
			}
			
			if(!missingField)
			{
				alert("Not all required fields have been completed. Please add the required information before trying again.");
			}

			else
			{
		    	for(i=0; i<thisForm.length; i++)
			    {				
				thisForm[i].value.replace(/,/g, " ");
			    }			
			}
			
			return missingField;
		}

		

        // Check the details of the user details form before submission    	
		function checkOrderByIdForm(thisForm)
		{
			var missingField = true;
			
			// check title dropdown
			var titleSelected = false;
			for (i=1;i<thisForm.userTitle.options.length;i++) {
				if (thisForm.userTitle.options[i].selected) {
					var titleSelected = true;
				}			
			}
			if (!titleSelected) {
				missingField = false;
			}

			if(thisForm.firstname.value == '' || thisForm.surname.value == '' || thisForm.address1.value == '' || thisForm.town.value == '' || thisForm.postcode.value == '') {
				missingField = false;
			}	

			else
			{
		    	for(i=0; i<thisForm.length; i++)
			    {				
				thisForm[i].value.replace(/,/g, " ");
			    }			
			}

			var validIds = true;
			var emptyFields = true;
            var possibleFirstChar = "abcdefghijklmnopqrstuvwxyz";
			var possibleNums = "1234567890";


			for(i=0; i<thisForm.length; i++)
			{
			// If the input is a brochure field and it is not empty
			if ((thisForm[i].name.search(/brochure/) != -1) && (thisForm[i].value != ''))
			  {
			  emptyFields = false;
			  
//			  //If the value is not the correct length
//			  if (thisForm[i].value.length != 4)
//			    {
//				validIds = false;
//				} 			  
//              		  
//			  //If the first character is a lowercase letter
//			  var firstChar = thisForm[i].value.charAt(0).toLowerCase();
//
//              if (possibleFirstChar.indexOf(firstChar) == -1)
//			    {
//				validIds = false;
//				}		  
//			  
//			  //If the last three characters are numbers
//			  var thisChar;
//			  
//			  for(j=1; j<4; j++)
//			    {
//				thisChar = thisForm[i].value.charAt(j);
//				if (possibleNums.indexOf(thisChar) == -1)
//				  {
//				  validIds = false;
//				  }			
//				}
//			  
//			  //Check that the entered codes exist
//			  var codeValid = false;			  
//			  
//			  for(k=0; k<codeArray.length; k++)
//			    {
//				if (thisForm[i].value.toLowerCase() == codeArray[k].toLowerCase())
//				  {
//				  codeValid = true;
//				  }
//				}
//				
//				for(k=0; k<magazineOnlyCodeArray.length; k++)
//			    {
//				if (thisForm[i].value.toLowerCase() == magazineOnlyCodeArray[k].toLowerCase())
//				  {
//				  codeValid = true;
//				  }
//				}
//				
//              
//			  if (codeValid == false)
//			    {
//				validIds = false;
//				}
//			  
			  }	
			}

			
			if(!missingField)
			  {
			  alert("Not all required fields have been completed. Please add the required information before trying again.");
			  }		  
            else if (emptyFields)
			  {
			  alert("Please enter at least one brochure code before submitting");
			  missingField = false;
			  }
            else if (!validIds)
			  {
              
//            alert("A brochure code that you entered is not valid.  Please check before resubmitting.")
//			  missingField = false;
			  }
			
			return missingField;

		}		
		
		
    // Calculate the results per category on basket page		
    function resultsPerCategory(category) 
	  {
      var catCount = 0;
      for(i=0; i<cookieArray.length; i++)
        {
        if (cookieArray[i][4] == category)
          {
          catCount++;
          }
        }
      return catCount;
      }


    // Outputs the brochures by category on the basket page
    function outputBasketBrochures(category) 
      {	
	  var brochCount = 0;
	  for(k=0; k<cookieArray.length; k++)
	    {
		if(category == cookieArray[k][4])
		  {
		  brochCount++;
		  }
		}

      var itemCount = 0;
	  var brochureTitle;
	  
      for(i=0; i<cookieArray.length; i++)
        {
        if(category == cookieArray[i][4])
          {
          itemCount++;
		  
          if((itemCount % 2) == 1)
            {
            document.write("<div class='row'>"); 
            }

          document.write("  <div class='brochureItem'>");
          document.write("    <img src='" + cookieArray[i][2] + "' alt='' class='cover' />");
          document.write("    <p>" + stripSlashes(cookieArray[i][1]) + "</p>");
          document.write("    <span>");

		  if (cookieArray[i][3] != '')
		    {
			brochureTitle = formatTitle(stripSlashes(cookieArray[i][1]));
            document.write("      <a href='" + cookieArray[i][3] + "'onClick=\"_hbSet(\'c1\',\'Classified|" + brochureTitle + "\'); _hbSend();\"  name=\"&lid=Classified|" + brochureTitle + "\" target=\"_blank\"><img src=\"http://www.radiotimes.com/classified/images/viewsite_off_bsktbtn.gif\" alt=\"View Website\" name=\"viewButton"+i+"\" onmouseover=\"viewButton"+i+".src=\'http://www.radiotimes.com/classified/images/viewsite_on_bsktbtn.gif\'\" onmouseout=\"viewButton"+i+".src=\'http://www.radiotimes.com/classified/images/viewsite_off_bsktbtn.gif\'\" /></a>");
/*            document.write("      <a href='" + cookieArray[i][3] + "'><img src='http://www.radiotimes.com/classified/images/viewsite_on_bsktbtn.gif' alt='View Website' /></a>");*/
            }
          document.write("      <a href='http://www.radiotimes.com/classified/basket/basket.jsp' onclick='removeBrochure(\"" + cookieArray[i][0] + "\")'><img src='http://www.radiotimes.com/classified/images/deleteitem_btn.gif' alt='Delete Item' class='deleteBtn' /></a>");
          document.write("    </span>");
          document.write("  </div>");

          if((itemCount % 2) == 0 || (itemCount == brochCount))
            {
            document.write("</div>"); 
            }			  
          }
        }
      }		

    // Output the brochure titles on the Thank You page
    function thankYouResults(category) 
      {  
	  var numResults = resultsPerCategory(category);
      var brochureTitle;
	  
	  if(numResults > 0)
	    {
        document.write("<div class='row'>");

        for(i=0; i<cookieArray.length; i++)
          {
          if (cookieArray[i][4] == category)
            {
	        document.write("<p>" + stripSlashes(cookieArray[i][1]) + "</p>");

	        if(cookieArray[i][3] != '')
	          {
			  brochureTitle = formatTitle(stripSlashes(cookieArray[i][1]));
		      document.write("<a href='" + cookieArray[i][3] + "' onClick=\"_hbSet(\'c1\',\'Classified|" + brochureTitle + "\'); _hbSend();\"  name=\"&lid=Classified|" + brochureTitle + "\" target=\"_blank\"><img src=\"http://www.radiotimes.com/classified/images/viewsite_smlbtn_off.gif\" onmouseover=\"thanksViewSite"+i+".src=\'http://www.radiotimes.com/classified/images/viewsite_smlbtn_on.gif\';\" onmouseout=\"thanksViewSite"+i+".src=\'http://www.radiotimes.com/classified/images/viewsite_smlbtn_off.gif\';\" name=\"thanksViewSite"+i+"\"alt=\"View Website\"></a>");
//document.write("<a href='" + cookieArray[i][3] + "<img src='http://www.radiotimes.com/classified/images/viewsite_off_bsktbtn.gif' alt='View Website'></a>");
		      }
            }
          }
		document.write("</div>");  
        }
      }
	  
    // For external links, formats the brochure title to that required by HBX - replaces spaces with plus signs (+)
    function formatTitle(brochureTitle)
      {
      var newTitle = brochureTitle.replace(/ /g, "+");  // Replace spaces with plus signs (for HBX)
      newTitle = newTitle.replace(/'/g, "_");
      return newTitle;	  
	  }

    // Checks whether the required brochure is already in the basket
    function inBasket(id)
	  {
	  var isInBasket = false;
	  for(i=0; i<cookieArray.length; i++)
	    {
        if(cookieArray[i][0] == id)
		 {
		 isInBasket = true;
		 }
		}
      return isInBasket;  
	  }
	  
    function inBasketImage(brochureId)
	  {
      if (inBasket(brochureId))
	    {
		document.write("<img src='http://www.radiotimes.com/classified/images/addToBasket.gif' alt='Added to Basket' />");
        return false;
		}
      else
	    {
		document.write("<img src='http://www.radiotimes.com/classified/images/addToBasket_off.gif' alt='Add to Basket' name='addToBasket" + brochureId + "' onmouseover=\"addToBasket"+brochureId+".src=\'http://www.radiotimes.com/classified/images/addToBasket_on.gif\';\" onmouseout=\"addToBasket" + brochureId + ".src=\'http://www.radiotimes.com/classified/images/addToBasket_off.gif\';\" />");
		}				
      }
  
      // Outputs the basket info in the sidebar  
    function basketContents()
    {
    document.write("<p>You may add another " + brochuresLeft + " items to your basket</p>");

    var basketCount = maxBrochures - brochuresLeft;
    if (basketCount == 0)
      {
      document.write("<p class='numItems'>Your basket is empty</p>");
      document.write("<div class='added'>");	
      }
    else
      {
      document.write("<p class='numItems'>So far you have added:</p>");
	  document.write("<div class='added'>");
      for(i=0; i<basketCount; i++)
  	    {        
        document.write("<p>" + stripSlashes(cookieArray[i][1]) + "</p>");
	    }
      }
    }	



/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : 
    oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

	
    // Get the id of the first brochure	
	function showFirstBrochure()
	  {
      var brochures = getElementsByClassName(document, "*", "brochure");
      var brochureId = brochures[0].getAttribute("id");
      showSection(brochureId);
	  }	
	
	