﻿// JScript File to hold functions needed for localplus pages

img_uk_rollout = "/media/UK.gif";		// image src for rollout events
area_name_rollout = "/media/AreaName.gif";
	
function IsNumeric(sText)
{
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length; i++) {
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}else{
			IsNumber = true;
			return IsNumber;
		}
	}
	return IsNumber;
}
	
function ValTownPostcode(formName)
{
	// validate the town/postcode entered in txtTownPostcode
	// This supercedes ValPostcode, which is required to support TLCv2.x releases.
	// Once we have fully migrated to TLCv3.x, ValPostcode will be obsolete.
	//e = eval("document.forms."+formName);
	var e;
	e = document.forms[formName];
	var _txtTownPostcode = document.getElementById('LPSearchBar1_txtTownPostcode');
	
	townName = _txtTownPostcode.value;
	//test = trim(test);
	size = townName.length;
 
	if (size == 0) { 
	 	alert("Please enter a Postcode or Town Name"); 
	 	_txtTownPostcode.focus();
	 	return false; 	
 	}
    
	townName = townName.toUpperCase(); //Change to uppercase
	while (townName.slice(0,1) == " ") //Strip leading spaces
	{
		townName = townName.substr(1,size-1);size = townName.length;
	}
 
	while(townName.slice(size-1,size)== " ") //Strip trailing spaces
	{
		townName = townName.substr(0,size-1);size = townName.length;
	}
 
  	document.forms[0].LPSearchBar1_originalLocation.value = _txtTownPostcode.value;
  	
	// if it doesn't contain a number then treat it as town.
	if (IsNumeric(townName)) {
		document.location='LocalPlus.aspx?postcode='+ townName
				+ '&cobrand=' + document.getElementById('cobrand').value
				+ '&searchBar:originalLocation='+ _txtTownPostcode.value
				+ '&topicid='+ document.getElementById('LPSearchBar1_topicIDField').value;
  		return true;		
	}
	else	
	{	
		document.location='LocalPlus.aspx?searchBar:txtTownPostcode=' +	townName
			+ '&cobrand='+ document.getElementById('cobrand').value
			+ '&searchBar:originalLocation=' + _txtTownPostcode.value 
			+ '&NewLocalArea=True&topicid=' + document.getElementById('LPSearchBar1_topicIDField').value;
		return true;
	}		
	
}

function ValCountyTownPostcode(formName) 
{
	// validate the postcode entered in countyTownPostcode.
	// This is populated from the database, then drop-down list selection, so either
	// 1. the user has selected a drop down list item, so the postcode is valid
	// 2. they haven't selected anything yet, so the postcode is blank.
	postcode = document.getElementById('LPSearchBar1_countyTownPostcode').value;
	//test = trim(test);
	size = postcode.length;

	if (postcode == 0) { 
 		alert("Please choose a county then a town."); 
 		return false; 	
 	}
	var townDropDown = document.getElementById('LPSearchBar1_townDropDown');
	originalLocation = townDropDown[townDropDown.selectedIndex].text;
	document.location='LocalPlus.aspx?postcode='+ postcode 
					+ '&cobrand='+ document.getElementById('cobrand').value
					+ '&searchBar:originalLocation=' + originalLocation
					+ '&topicid='+ document.getElementById('LPSearchBar1_topicIDField').value;
	return true;

}

function getTownsForCounty() {
	
	var countyDropDown = document.getElementById('LPSearchBar1_countyDropDown');
	var countyIndex = countyDropDown.selectedIndex;

	var townDD = document.getElementById('LPSearchBar1_townDropDown');
	
	// add each town in _towns[countyIndex]
	townDD.options.length = 0
	
	for (var i=0; i<_towns[countyIndex].length; i++) 
	{
		townDD.options[townDD.length] = new Option(_towns[countyIndex][i],
												   _postcodes[countyIndex][i],
												   false,
												   false);
	}
		
}
	
function postCodeIntoTownvalue()
{
	var countyTownPostcode = document.getElementById('LPSearchBar1_countyTownPostcode');
	var townDropDown = document.getElementById('LPSearchBar1_townDropDown');
	countyTownPostcode.value=townDropDown.value;
}

function showMapImg( inName ) 
{
	document.img_uk.src = "/media/uk_"+inName+".gif";
	document.area_name.src = "/media/area_"+inName+".gif";
}

function resetMapImg() 
{
	document.img_uk.src = img_uk_rollout;
	document.area_name.src = area_name_rollout;
}
	
function clearPostcode()
{
	var countyTownPostcode = document.getElementById('LPSearchBar1_countyTownPostcode');
	countyTownPostcode.value = '';
}

function imgswap(pic,imageName)
{
	
	/*This function takes image names that have "N" for normal or "H" for highlighted as the last
	letter in the image name and also a suffix (".gif" etc) must be present also 
	All images must be in a folder named "media" unless otherwise explicitly stated in all references
	to the image within the HTML page.
	If the image rolled over changed a different image than itself on the same page then add the name of 
	the swapped image as the second parameter (imageName) otherwise leave it blank */
	
	
	
	if (pic.indexOf("media")==-1){
		pic = "media/"+pic;
	}
	
	var suffix = pic.slice(pic.length-4,pic.length);
	
  	var picstartpos = pic.indexOf("media/");
	var picname = pic.substring(picstartpos+6,pic.length-5);
	
	if (typeof(imageName)!="undefined"){
		picname=imageName;}

  	if (document.all){	
		document.all(picname).src=pic;
  	}
  
  	if (document.layers){
  
  	var theString = document.images[picname].src;
  	
  	/*This finds the second to last delimiter to obtain the whole image address for Netscape
  	or just gives it the ../name string which appears to work*/
  	
  	if (pic.charAt(0)==".") {
  	  	document.images[picname].src=pic;
  	} else {
		var lastPos = (theString.slice(0,theString.lastIndexOf("/")-1).lastIndexOf("/")+1);
		document.images[picname].src=theString.slice(0,lastPos)+(pic);
 	}
  }
}
