var abc_server = "http://www.abc.net.au/";
// Specify maximum number of history links to keep, 
//    minimum 1.
// Specify cookie name.


//var CookieName = "ABCRegion";
var CookieName = "ABCGuestID";

var DaysToLive = 0;

var cities_array = new Array();
var all_cities = new Array();
var all_top_cities ;

var index = 0;

var HistoryLink = new Array();
var HistoryTitle = new Array();
var HistoryContent = new String();
DaysToLive = parseInt(DaysToLive);


//functions to extend the Array object
Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
};

  Array.prototype.filter = function(fun)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array();
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
      {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this))
          res.push(val);
      }
    }

    return res;
  };

Array.prototype.in_array = function(p_val) 
{
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
};

String.prototype.capitalize = function(){
    return this.replace(/\S+/g, function(a){
        return a.charAt(0).toUpperCase() + a.slice(1).toLowerCase();
    });
};

//if a cookie for this site exists, read its values
function GetCookie() 
{
var cookiecontent = '';
if(document.cookie.length > 0)  
   {
   //read the bit we don't need.
   //var guestCookie = "ABCGuestID="   ;
   //var cookie_unwanted_start = document.cookie.indexOf(guestCookie);
   //var cookie_unwanted_end = 

   var cookiename = CookieName + '=';
   var cookiebegin = document.cookie.indexOf(cookiename);
   if(cookiebegin > -1) {
      cookiebegin += cookiename.length;
      var cookieend = document.cookie.indexOf(";",cookiebegin);
      if(cookieend < cookiebegin) {  //if there is a cookie exist
		cookieend = document.cookie.length; 
		//alert("document.cookie.length: " + document.cookie.length);
		}
      //cookiecontent = document.cookie.substr(cookiebegin,cookieend);
	  //cookiecontent = document.cookie.substr(cookiebegin,document.cookie.length);	  
	  //alert("cookiecontent: " + cookiecontent);	  
	  //filter the cookie content from long numbers 
	  //cookiecontent = trim(cookiecontent);
	  //cookiecontent = cookiecontent.replace("smtp.abc.net.au.","","i");
	  
	  if (document.cookie.indexOf("smtp.abc.net.au")>-1) //if 
	  	{
	  	cookiecontent = document.cookie.substr(44,document.cookie.length);
		}
	 else {
	  	cookiecontent = document.cookie.substr(cookiebegin,document.cookie.length);	
		}			  
	  //alert("cookiecontent filtered: " + cookiecontent);	 
   	  //alert("cookiecontent filtered from numbers: " + cookiecontent);	 
      }
   }
if( cookiecontent.length < 3 ) { return; } //see if cookie contains any values
cookiecontent = unescape(cookiecontent);
var historyList = cookiecontent.split('&');
for( var i = 0; i < historyList.length; i++ ) 
  {
   var link = historyList[i].split('=',2);
   //alert("link: " + link);
   var temparray = link[0].split('amp;');
   link[0] = temparray.join('&'); 	//get the url link of the page clicked on
   temparray = link[1].split('amp;');
   link[1] = temparray.join('&'); 	//get title of the page visited	
   //alert("temparray: " + temparray); 	//display each city on its own.
   
   //alert("link[0]: " + link[0]);
   //alert ("HistoryLink: " + HistoryLink);
  	
   if (link[0].indexOf("brisbane") && link[0].match("brisbane")	)
	{	
	cities_array.push("brisbane");
	}
   else if (link[0].indexOf("sydney") && link[0].match("sydney"))	
	{	
	cities_array.push("sydney");
	}   	
   else if (link[0].indexOf("melbourne") && link[0].match("melbourne"))	
	{	
	cities_array.push("melbourne");
	}   	
   else if (link[0].indexOf("adelaide") && link[0].match("adelaide"))	
	{
	cities_array.push("adelaide");
	}	
   else if (link[0].indexOf("alicesprings") && link[0].match("alicesprings"))	
	{
	cities_array.push("alicesprings");
	}
   else if (link[0].indexOf("ballarat") && link[0].match("ballarat"))	
	{
	cities_array.push("ballarat");
	}
   else if (link[0].indexOf("brokenhill") && link[0].match("brokenhill"))	
	{
	cities_array.push("brokenhill");
	}
   else if (link[0].indexOf("canberra") && link[0].match("canberra"))	
	{
	cities_array.push("canberra");
	}
   else if (link[0].indexOf("capricornia") && link[0].match("capricornia"))	
	{
	cities_array.push("capricornia");
	}
   else if (link[0].indexOf("centralcoast") && link[0].match("centralcoast"))	
	{
	cities_array.push("centralcoast");
	}
   else if (link[0].indexOf("centralvic") && link[0].match("centralvic"))	
	{
	cities_array.push("centralvic");
	}
   else if (link[0].indexOf("centralwest") && link[0].match("centralwest"))	
	{
	cities_array.push("centralwest");
	}
   else if (link[0].indexOf("darwin") && link[0].match("darwin"))	
	{
	cities_array.push("darwin");
	}
   else if (link[0].indexOf("eyre") && link[0].match("eyre"))	
	{
	cities_array.push("eyre");
	}
   else if (link[0].indexOf("farnorth") && link[0].match("farnorth"))	
	{
	cities_array.push("farnorth");
	}
   else if (link[0].indexOf("gippsland") && link[0].match("gippsland"))	
	{
	cities_array.push("gippsland");
	}
   else if (link[0].indexOf("goldcoast") && link[0].match("goldcoast"))	
	{
	cities_array.push("goldcoast");
	}
   else if (link[0].indexOf("goldfields") && link[0].match("goldfields"))	
	{
	cities_array.push("goldfields");
	}
   else if (link[0].indexOf("goulburnmurray") && link[0].match("goulburnmurray"))	
	{
	cities_array.push("goulburnmurray");
	}
   else if (link[0].indexOf("greatsouthern") && link[0].match("greatsouthern"))	
	{
	cities_array.push("greatsouthern");
	}
   else if (link[0].indexOf("hobart") && link[0].match("hobart"))
	{
	cities_array.push("hobart");
	}
   else if (link[0].indexOf("illawarra") && link[0].match("illawarra"))	
	{
	cities_array.push("illawarra");
	}
   else if (link[0].indexOf("kimberley") && link[0].match("kimberley"))	
	{
	cities_array.push("kimberley");
	}
   else if (link[0].indexOf("midnorthcoast") && link[0].match("midnorthcoast"))	
	{
	cities_array.push("midnorthcoast");
	}
   else if (link[0].indexOf("milduraswanhill") && link[0].match("milduraswanhill"))	
	{
	cities_array.push("milduraswanhill");
	}
   else if (link[0].indexOf("newcastle") && link[0].match("newcastle"))	
	{
	cities_array.push("newcastle");
	}
   else if (link[0].indexOf("newengland") && link[0].match("newengland"))	
	{
	cities_array.push("newengland");
	}
   else if (link[0].indexOf("northandwest") && link[0].match("northandwest"))	
	{
	cities_array.push("northandwest");
	}
   else if (link[0].indexOf("northcoast") && link[0].match("northcoast"))	
	{
	cities_array.push("northcoast");
	}
   else if (link[0].indexOf("northqld") && link[0].match("northqld"))	
	{
	cities_array.push("northqld");
	}
   else if (link[0].indexOf("northtas") && link[0].match("northtas"))	
	{
	cities_array.push("northtas");
	}
   else if (link[0].indexOf("northwestwa") && link[0].match("northwestwa"))	
	{
	cities_array.push("northwestwa");
	//alert("pushing northwestWA into array:");
	}
   else if (link[0].indexOf("northwest") && link[0].match("northwest"))	
	{
	cities_array.push("northwest");
	//alert("pushing northwest into array:");
	}	
   else if (link[0].indexOf("perth") && link[0].match("perth"))	
	{
	cities_array.push("perth");
	}
   else if (link[0].indexOf("riverland") && link[0].match("riverland"))	
	{
	cities_array.push("riverland");
	}
   else if (link[0].indexOf("shepparton") && link[0].match("shepparton"))	
	{
	cities_array.push("shepparton");
	}
   else if (link[0].indexOf("southcoast") && link[0].match("southcoast"))	
	{
	cities_array.push("southcoast");
	}
   else if (link[0].indexOf("southeastnsw") && link[0].match("southeastnsw"))	
	{
	cities_array.push("southeastnsw");
	}
   else if (link[0].indexOf("southeastsa") && link[0].match("southeastsa"))	
	{
	cities_array.push("southeastsa");
	}
   else if (link[0].indexOf("southqld") && link[0].match("southqld"))	
	{
	cities_array.push("southqld");
	}
   else if (link[0].indexOf("southwestvic") && link[0].match("southwestvic"))	
	{
	cities_array.push("southwestvic");
	}
   else if (link[0].indexOf("southwestwa") && link[0].match("southwestwa"))	
	{
	cities_array.push("southwestwa");
	}
   else if (link[0].indexOf("sunshine") && link[0].match("sunshine"))	
	{
	cities_array.push("sunshine");
	}
   else if (link[0].indexOf("upperhunter") && link[0].match("upperhunter"))	
	{
	cities_array.push("upperhunter");
	}
   else if (link[0].indexOf("westernplains") && link[0].match("westernplains"))	
	{
	cities_array.push("westernplains");
	}
   else if (link[0].indexOf("westernvic") && link[0].match("westernvic"))	
	{
	cities_array.push("westernvic");
	}
   else if (link[0].indexOf("westqld") && link[0].match("westqld"))	
	{
	cities_array.push("westqld");
	}
   else if (link[0].indexOf("wheatbelt") && link[0].match("wheatbelt"))	
	{
	cities_array.push("wheatbelt");
	}
   else if (link[0].indexOf("widebay") && link[0].match("widebay"))	
	{
	cities_array.push("widebay");
	}


	//added new ones
   else if (link[0].indexOf("brokenhill") && link[0].match("brokenhill"))	
	{
	cities_array.push("brokenhill");
	}	
   else if (link[0].indexOf("coffscoast") && link[0].match("coffscoast"))	
	{
	cities_array.push("coffscoast");
	}	
   else if (link[0].indexOf("riverina") && link[0].match("riverina"))	
	{
	cities_array.push("riverina");
	}	
   else if (link[0].indexOf("katherine") && link[0].match("katherine"))	
	{
	cities_array.push("katherine");
	}	
   else if (link[0].indexOf("katherine") && link[0].match("katherine"))	
	{
	cities_array.push("katherine");
	}	
   else if (link[0].indexOf("tropic") && link[0].match("tropic"))	
	{
	cities_array.push("tropic");
	}		
   else if (link[0].indexOf("goulburnmurray") && link[0].match("goulburnmurray"))	
	{
	cities_array.push("goulburnmurray");
	}		
   else if (link[0].indexOf("esperance") && link[0].match("esperance"))	
	{
	cities_array.push("esperance");
	}

   //all_cities.sort(); //sort the array according to the number of clicks
   //all_cities.reverse(); //reverse the order of the pheonix so it will apear from highest number of clicks to lowest

   // returns true if x is numeric and false if it is not.
   var isNumeric = function(x) {
   	// returns true if x is numeric and false if it is null or empty(it will be if the city is not clicked..WOOHOO!).
   	var RegExp = /^(-)?(\d*)(\.?)(\d*)$/; 
   	return String(x[0]).match(RegExp);
	}

   //all_top_cities=all_cities.filter(isNumeric); //filter the array from 0/null clicked cities

   }
    
   //alert("cities_array: " + cities_array);
	
   //eliminate duplicate cities from the array   
   cities_array.reverse();
   var i=0;
   var cities_unique_array = new Array();
   for (i=0;i<cities_array.length;i++)
   	{
	if (! cities_unique_array.in_array(cities_array[i]))
		{
		cities_unique_array.push(cities_array[i]);
		}
	} //end for
   //cities_array.in_array()

	//takes the first 4 in the array.
   cities_unique_array = cities_unique_array.slice(0,4);   
   //alert("cities_unique_array: " + cities_unique_array);
	
	if (cities_unique_array.length>0 )	
     {
	 HistoryContent = "Recently visited: ";	
	 }
   var index=0,i=0;
   for (index=0;index<cities_unique_array.length;index++)
	{
	var j=0;
	var city = cities_unique_array[index];
	var city_link = cities_unique_array[index];
	//alert("city: " + city);
	if ((city=="alicesprings") || (city=="northcoast") || (city=="northqld") || (city=="northtas") || (city=="southqld") || (city=="upperhunter") || (city=="coffscoast"))
		{
		city_link = cities_unique_array[index];
		city = add1space(city,5);	
		city = city.capitalize();
		} 
	else if (city=="southcoast")
		{		
		city_link = cities_unique_array[index];
		city = add1space(city,5);	
		city = city.capitalize() + " WA";	
		}
	else if(city=="tropic")	
		{
		city_link = cities_unique_array[index];
		city = "Tropical QLD".capitalize();			
		}
	else if (city=="greatsouthern")
		{
		city_link = cities_unique_array[index];
		city = add1space(city,5);	
		city = city.capitalize() + " WA";			
		}
	else if (city=="brokenhill")
		{
		city_link = cities_unique_array[index];
		city = add1space(city,6);
		city = city.capitalize();	
		}
	else if ((city=="centralcoast") || (city=="centralvic") || (city=="centralwest")||(city=="westernvic") || (city=="westernplains"))
		{
		city_link = cities_unique_array[index];
		city = add1space(city,7);
		city = city.capitalize();	
		}
	else if (city=="milduraswanhill") 
		{
		city_link = cities_unique_array[index];	
		city = add1hyphen(city,7,4);
		
		}
	else if (city=="goulburnmurray")	
		{
		city_link = cities_unique_array[index];
		city = add1space(city,8);	
		city = city.capitalize();
		}				
	else if (city =="northandwest")
		{
		city_link = cities_unique_array[index];
		city = add2spaces(city,5,3);
		city = city.capitalize() + " SA";	
		}
	else if (city =="midnorthcoast")
		{
		city_link = cities_unique_array[index];
		city = add2spaces(city,3,5);	
		city = city.capitalize() + " NSW";	
		}
	else if (city=="northwestwa") 
		{
		city_link = cities_unique_array[index];
		city = add2spaces(city,5,4);			
		//city = city.capitalize() ;								
		}
	else if(city =="northwest")	
		{
		city_link = cities_unique_array[index];
		city = add1space(city,5);			
		city = city.capitalize() + " QLD";			
		}				
	else if ((city=="southeastnsw")||(city=="southeastsa") || (city=="southwestvic") || (city=="southwestwa"))
		{
		city_link = cities_unique_array[index];
		city = add2spaces(city,5,4); //take the location of first space and how many words to take after 'west'		
		//city = city.capitalize();			
		}
	else if (city=="newengland")
		{		
		city_link = cities_unique_array[index];
		city = add1space(city,3);
		city = city.capitalize();	
		}
	else if (city=="farnorth") 
		{
		city_link = cities_unique_array[index];
		city = add1space(city,3);	
		city = city.capitalize() + " QLD";	
		}
	else if ((city=="goldcoast") || (city=="goldfields") || (city=="westqld") || (city=="widebay"))
		{
		city_link = cities_unique_array[index];
		city = 	add1space(city,4);
		city = city.capitalize();	
		}
	else   {
		city_link = cities_unique_array[index];
		city = (cities_unique_array[index]).capitalize();
		}	
	HistoryContent += '<' + 'a href="' + abc_server + city_link.toLowerCase() +'/">' + city +'<'+'/'+'a>';
	if (i<cities_unique_array.length-1)
		{
		i++;
		HistoryContent +=" , ";			
		}   
   	}   
}

function replaceChars(entry) 
{
out = "%20"; // replace this
add = ""; // with this
temp = "" + entry; // temporary holder

while (temp.indexOf(out)>-1) {
	pos= temp.indexOf(out);
	temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length));
	}
}

//http://www.abc.net.au/southeast%20sa/
function filterwhitespace(str) {
str.replace(/\s/g, "");
str.replace("%20","");
str.toLowerCase(); 

//alert("filtered"+str);
return str;
} //end function

//city = add1hyphen(city,7,11);
function add1hyphen(city_str,location1,howmanychars) {
var new_str = "";
var space = " ";
var hyphen = "-";
//city_str = city_str.trim();
var start = city_str.substr(0,location1);
//alert("start: " + start);

var mid   = city_str.substr(location1,howmanychars);
//alert("mid: " + mid);

var end   = city_str.substr(location1+howmanychars,city_str.length);
//alert("end: "+end);
new_string = (start.capitalize() + hyphen + mid.capitalize() + space + end.capitalize()).toString();
//alert(new_string);
return new_string;
}

function add1space(city_str,location)
{
var new_str = "";
var start = city_str.substr(0,location);
var end = city_str.substr(location,city_str.length);
var space = " ";
new_string = start.capitalize() + space + end;
//alert(new_string);
return new_string;
} //end function

function add2spaces(city_str,location1,howmanychars)
{
var new_str = "";
var space = " ";
//city_str = city_str.trim();
var start = city_str.substr(0,location1);
//alert("start: " + start);

var mid   = city_str.substr(location1,howmanychars);
//alert("mid: " + mid);

var end   = city_str.substr(location1+howmanychars,city_str.length);
//alert("end: "+end);
new_string = (start.capitalize() + space + mid.capitalize() + space + end.toUpperCase()).toString();
//alert(new_string);
return new_string;
} //end function



//Create the cookie with the data needed (links and page title)
function PutCookie() 
{
if( HistoryLink.length < 1 ) { return; }
var len = HistoryLink.length;
var pairs = new Array();
for( var i = 0; i < HistoryLink.length; i++ ) 
	{ 
	pairs.push(HistoryLink[i]+'='+HistoryTitle[i]); 
	}
var value = pairs.join('&');
var exp = new String();
if(DaysToLive > 0) {
   var now = new Date();
   now.setTime( now.getTime() + (DaysToLive * 24 * 60 * 60 * 1000) );
   exp = '; expires=' + now.toGMTString();
   }
document.cookie = CookieName + "=" + escape(value) + "; path=/" + exp;
}

//clear memory after displaying the cities from the cookie file
function EmptyArray()
{
cities_array = [];
//all_top_cities = [];
//alert(cities_array + ":" + all_top_cities);
}

GetCookie();
PutCookie();
EmptyArray();

