//Global Variables
var locale;
var fileToCall;
var NumberOfLocations = 5;


//-------------------------------------------------
//   RotatePages
//----------
//  Cycles through the pages, from whatever the start is
// through all the files as determined by the value of the
// global var called 'NumberOfLocations' 
//-------------------------------------------------
function RotatePages(){
	if (Get_Cookie('locale')){
		locale = Number(Get_Cookie('locale')) + 1;
		if (locale > NumberOfLocations){
			locale = 1;}}
	else {locale = (Math.round((Math.random()*4)+1));}	
	Set_Cookie('locale', locale, '', '/', '', '')
	if (Get_Cookie('locale')){
		fileToCall = locale;
		fileToCall = Number(fileToCall);
		do
		{
		GetPageContent(fileToCall + ".htm");
		fileToCall = ++fileToCall;
		if(fileToCall > NumberOfLocations){fileToCall = 1;}
		}
		while(fileToCall != locale);
	}
}

	
//-------------------------------------------------
//   GetPageContent
//----------
// Try the browser, and call the text from the site
//-------------------------------------------------
function GetPageContent(strURL){
	var results = false
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		results = new XMLHttpRequest()
	else 
		if (window.ActiveXObject){ // if IE
		try {results = new ActiveXObject("Msxml2.XMLHTTP")}
			catch (e){
				try{results = new ActiveXObject("Microsoft.XMLHTTP")}
			catch (e){}
		}
	}
	else
		return false
	results.open('GET', strURL, false) //get page synchronously
	results.send(null)
	DisplayPage(results)
}


//-------------------------------------------------
//   DisplayPage
//----------
//   Check the status, dump the response
//-------------------------------------------------
function DisplayPage(strPage){
	if (window.location.href.indexOf("http")==-1 || strPage.status==200)
	document.write(strPage.responseText)
}


//-------------------------------------------------
//   Set_Cookie
//----------
// Cookie hanlder from http://techpatterns.com/downloads/javascript_cookies.php
//-------------------------------------------------
function Set_Cookie(name, value, expires, path, domain, secure){
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime(today.getTime());
	if(expires){
	expires = expires * 1000 * 60 * 60 * 24;}
	var expires_date = new Date(today.getTime() + (expires));
	document.cookie = name + "=" +escape(String(value)) +
	((expires) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	((path )? ";path=" + path : "" ) + 
	((domain) ? ";domain=" + domain : "" ) +
	((secure) ? ";secure" : "" );
}

//-------------------------------------------------
//   Get_Cookie
//----------
// Cookie hanlder from http://techpatterns.com/downloads/javascript_cookies.php
// this function gets the cookie, if it exists
//-------------------------------------------------
function Get_Cookie(name){	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length ))){
		return null;}
	if (start == -1) return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}
