function alphabeticise(){
	var i=1, end="N";
	while(end=="N"){
		if(document.getElementById(i)){
			//alert("found "+i);
			i++;
		}
		else{
			end="Y";
			var total=i-1;
			//alert("total="+total);
		}
	}
	// set up array of same length as the number of retailers
	var startArr= new Array(total);
	var startArrHref= new Array(total);

	// fill startArr with the details of each retailer (in their original order)
	// also make the current list invisible
	for(i=1;i<total+1;i++){
		startArr[i-1]=document.getElementById(i).innerHTML;
		startArrHref[i-1]=document.getElementById(i).href;
		//document.getElementById(i).style.visibility="hidden";
	}

	startArr.sort();
	startArrHref.sort();
	
	// Now set the list elements on the page to display the new values
	for(i=1;i<total+1;i++){
		document.getElementById(i).href=startArrHref[i-1];
		document.getElementById(i).innerHTML=startArr[i-1];
	}
	// display the new randomised list
	for(i=1;i<total+1;i++){
		document.getElementById(i).style.visibility="visible";
	}
	

	
}



function randomise(){
	var i=1, end="N", n=0, j=0,done="N";
	while(end=="N"){
		if(document.getElementById(i)){
			//alert("found "+i);
			i++;
		}
		else{
			end="Y";
			var total=i-1;
			//alert("total="+total);
		}
	}
	// set up 2 arrays of same length as the number of retailers
	var startArr= new Array(total);
	var displayArr = new Array(total);
	var startArrHref= new Array(total);
	var displayArrHref = new Array(total);

	// fill startArr with the ID numbers of each retailer (in their original order)
	// also make the current list invisible
	for(i=1;i<total+1;i++){
		startArr[i-1]=document.getElementById(i).innerHTML;
		startArrHref[i-1]=document.getElementById(i).href;
		document.getElementById(i).style.visibility="hidden";
	}

	// empty display array to start.
	for(i=0;i<total;i++){
		displayArr[i]="";
		displayArrHref[i]="";
	}

	// start to loop through each element of startArr, generating a random number and slotting the contents of that startArr element into the corresponding random slot in the new displayArr.
	// if that slot is already full, then move along until a spare one is found.  This avoids the situation where you have all except one slot filled, and you're having to repeatedly loop round the
	// random number generator until the right number comes up, which is a slow method.
	for(i=1;i<total+1;i++){
		done="N";
	// get random number within the limits of the #retailers (-1 so it starts at zero)
		var rndNum = Math.round((total-1)*Math.random());

		//alert("rndNum="+rndNum);

		// if there isn't already a value in that element of the display array, put in the current retailer ID
		if(displayArr[rndNum]==""){
			displayArr[rndNum]=startArr[i-1];
			displayArrHref[rndNum]=startArrHref[i-1];
		}
		// otherwise, rather than getting another random number, just move along to the next available 'slot' in the array and put the number there.  This is in an attempt to avoid repeatedly looping round.
		else{
		// set j to start at the random number (could be 0 up to the max #retailers-1)
			j=rndNum;
			while(j<total){
				if(displayArr[j].length==0){
					displayArr[j]=startArr[i-1];
					displayArrHref[j]=startArrHref[i-1];
					done="Y";
					break;
				}
				j++;
			}
			
			// check if we found a spare slot in this iteration, if not then start from the beginning of the array (0) again
			if(done!="Y"){
				j=0;
				while(j<total+1){
					if(displayArr[j].length==0){
						displayArr[j]=startArr[i-1];
						displayArrHref[j]=startArrHref[i-1];
						break;
					}
					j++;
				}
			}
		}
	}
	
	/* check that all slots in the displayArr are filled
	for(i=0;i<total;i++){
		if(displayArr[i].length==0){
			alert("error - position "+i+" in displayArr is unfilled");
		}
	}
	*/

	/* Display array in an alert box 
	var newarr="New Array:";
	for(i=0;i<total;i++){
		newarr=newarr+"\n"+displayArr[i];
		}
	alert(newarr);
	*/
	
	// Now set the list elements on the page to display the new values
	for(i=1;i<total+1;i++){
		var x = document.getElementById(i).id;
		document.getElementById(x).href=displayArrHref[i-1];
		document.getElementById(x).innerHTML=displayArr[i-1];
	}
	// display the new randomised list
	for(i=1;i<total+1;i++){
		document.getElementById(i).style.visibility="visible";
	}

}


//always start randomised
randomise();
