function createXMLHttpRequest() {
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   try { return new XMLHttpRequest(); } catch(e) {}
   alert("XMLHttpRequest not supported");
   return null;
 }
 
function stripNonValidXMLCharacters(instring) {
        var out; // Used to hold the output.
        
        // to get the unicode code for a character
        //string.charCodeAt(index)
        // this should also give ascii for first 128 characters
        // so check and print out a list of any ascii beyond the first 128
        var extraChars ="";
        var theResult="";
        
        // from the lower values, keep:  tab - character 9, linefeed, character 10, cr character 13, 
        // then from 32 onwards, till 125 which is the }
        
        
        for (var i=0; i< instring.length;i++) {
        	var theText = instring.substring(i,i+1);
        	var theChar = theText.charCodeAt(0);
        	if ((theChar > 125) || (theChar < 9 ) || (theChar == 39) ||(theChar == 11) || (theChar == 12) || (theChar > 13 && theChar < 32)){
        		extraChars += theText;
        	}
        	else {
        		theResult += theText; // this is valid text
        	}
        }
       
        return theResult;
        
        var current; // Used to reference the current character.

        //if (instring == null || ("".equals(instring))) return ""; // vacancy test.
        /*for (var i = 0; i < instring.length; i++) {
        	
            current = instring.charAt(i); // NOTE: No IndexOutOfBoundsException caught here; it should not happen.
            if ((current == 0x9) ||
                (current == 0xA) ||
                (current == 0xD) ||
                ((current >= 0x20) && (current <= 0xD7FF)) ||
                ((current >= 0xE000) && (current <= 0xFFFD)) ||
                ((current >= 0x10000) && (current <= 0x10FFFF)))
                out += current;
        }
        
        return out;*/
    }    

function addValueToList(pulldownName) {
	var pulldown = document.getElementById(pulldownName);
	var selectedlist = document.getElementById(pulldownName + "list");
	fnMoveItems(pulldownName,pulldownName + "list");
}
function addAllValuesToList(pulldownName) {
	var pulldown = document.getElementById(pulldownName);
	var selectedlist = document.getElementById(pulldownName + "list");
	//fnMoveItems(pulldownName,pulldownName + "list");

	 var varFromBox = document.getElementById(pulldownName);
	 var varToBox = document.getElementById(pulldownName+"list"); 
	 if ((varFromBox != null) && (varToBox != null)) 
	 { 
	  if(varFromBox.length < 1) 
	  {
	   // there are no more options to move
	   return;	
	  }
	  while (varFromBox.length > 0) {
		   var newOption = new Option(); // Create a new instance of ListItem 
		   newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text; 
		   newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value; 
		   varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox
		   varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox 
	  }	
	}
}

function removeAllValuesFromList(pulldownName) {
	var pulldown = document.getElementById(pulldownName);
	var selectedlist = document.getElementById(pulldownName + "list");
//	fnMoveItems(pulldownName,pulldownName + "list");

	 var varFromBox = document.getElementById(pulldownName+"list");
	 var varToBox = document.getElementById(pulldownName); 
	 if ((varFromBox != null) && (varToBox != null)) 
	 { 
	  if(varFromBox.length < 1) 
	  {
	   // there are no more options to move
	   return;	
	  }
	  while (varFromBox.length > 0) {
		   var newOption = new Option(); // Create a new instance of ListItem 
		   newOption.text = varFromBox.options[0].text; 
		   newOption.value = varFromBox.options[0].value; 
		   varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox
		   varFromBox.remove(0); //Remove the item from Source Listbox 
	  }	
	}
}


function removeValueFromList(pulldownName) {
	//alert(pulldownName);
	//document.getElementById(pulldonwName + list).remove;
	var pulldown = document.getElementById(pulldownName);
	var selectedlist = document.getElementById(pulldownName + "list");
	fnMoveItems(pulldownName + "list",pulldownName);
}


// taken from
// www.codeproject.com/KB/aspnet/Listbox_or_combo_box.aspx
function fnMoveItems(lstbxFrom,lstbxTo)
{
 var varFromBox = document.getElementById(lstbxFrom);
 var varToBox = document.getElementById(lstbxTo); 
 if ((varFromBox != null) && (varToBox != null)) 
 { 
  if(varFromBox.length < 1) 
  {
   alert('There are no items in the source ListBox');
   return false;
  }
  if(varFromBox.options.selectedIndex == -1) // when no Item is selected the index will be -1
  {
   alert('Please select an Item to move');
   return false;
  }
  //while ( varFromBox.options.selectedIndex >= 0 ) 
  //{ 
   var newOption = new Option(); // Create a new instance of ListItem 

   newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text; 
   newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value; 
   varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox

   varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox 

  //} 
 }
 return false; 
}

function getURL() {
	// find the URL (including the version) of the current site ...
	var sPath = window.location;
	var paths = String(sPath).split("includes");  // this gets the parameter list	
	var theURL = paths[0];  // note - this will include the final /
	return theURL;

}

function getMinisiteName() {
		//var sPath = window.location;
		//var paths = String(sPath).split("=");
		//minisitename = paths[1];
		//minisitename = minisitename.replace("#","");
		//return minisitename;


		// new code - we have a minisite name and a minisite group now, for british waterways/civa
		var sPath = window.location;
		
		var paths = String(sPath).split("?");  // this gets the parameter list
		
		// now split the parameter list by the & 
		if(paths.length >= 2)
		{
			var parameters = paths[1].split("&");
			var i;
			for (i=0;i<parameters.length;i++) {
				var paramstring = parameters[i];
				if (paramstring.indexOf("minisitename") >-1)  {
					var minisitestring = paramstring.split("=");
					minisitename = minisitestring[1];
					
					minisitename = minisitename.replace("#","");
				}
			}
		}
		
		return minisitename;
}

function getMinisiteGroup() {
		//var sPath = window.location;
		//var paths = String(sPath).split("=");
		//minisitename = paths[1];
		//minisitename = minisitename.replace("#","");
		//return minisitename;


		// new code - we have a minisite name and a minisite group now, for british waterways/civa
		var sPath = window.location;
		
		var paths = String(sPath).split("?");  // this gets the parameter list
		
		// now split the parameter list by the & 
		
		var parameters = paths[1].split("&");
		var i;
		for (i=0;i<parameters.length;i++) {
			var paramstring = parameters[i];
			if (paramstring.indexOf("minisite_group") >-1)  {
				var minisitegroupstring = paramstring.split("=");
				minisitegroup = minisitegroupstring[1];
				
				minisitegroup = minisitegroup.replace("#","");
			}
		}
		
		return minisitegroup;
}

function getMinisiteGroup() {
		var sPath = window.location;
		var paths = String(sPath).split("?");  // this gets the parameter list
		
		// now split the parameter list by the & 
		if (paths[1])  {
			var parameters = paths[1].split("&");
			var i;
			var minisitegroup = "";
			for (i=0;i<parameters.length;i++) {
				var paramstring = parameters[i];
				if (paramstring.indexOf("minisite_group") > -1 )  {
					var minisitestring = paramstring.split("=");
					minisitegroup = minisitestring[1];
					minisitegroup = minisitegroup.replace("#","");
				}
			}
			return minisitegroup;
		}
		else {
			return "";
		}
}



function removeDiv(divName, parentDivName) {

	try  {
		 var d = document.getElementById(parentDivName);
		  var olddiv = document.getElementById(divName);
		  d.removeChild(olddiv);
	}
	catch (e) {

	}
}


function loggedOut() {
  if (xhReq.readyState != 4)  { 
   		return;
   	}
	var serverResponse = xhReq.responseText;
	
	// if the user is logged in then actually switch to the tab!
	//alert(xhReq.responseText);
	
	
	addContentText1 = serverResponse;
	//alert(addContentText1);
	document.getElementById('controlcontent').innerHTML = addContentText1;

}

function doCloseFormWindow() {

 removeDiv('describe','mapwrapper');
 removeDiv('mapoverlay','mapwrapper');
   
}


function checkEnter(event){
	if(event.keyCode==13)
	dosimplesearch();
}
function checkEditEnter(event){
	if(event.keyCode==13)
	doAdminLogin();
}


function checkEnterGoto(event) {
	if(event.keyCode==13)
	processStep3Postcode();
}


function loadHTMLIntoDIV(fileName,divName){
	// get the correct content for the div by loading an HTML file
	 xmlGetDivContent = createXMLHttpRequest();
	 xmlGetDivContent.open("GET", fileName,true);
	 xmlGetDivContent.onreadystatechange = function () {
		if (xmlGetDivContent.readyState == 4) {
	             // Received, OK
			var responseText = xmlGetDivContent.responseText;
			document.getElementById(divName).innerHTML = responseText;
		} else {
		  // Wait...
		}
	 };
	 xmlGetDivContent.send(null);
}



function redirectPage() {
	// take the user back to their minisite or to the community maps overview page
	
	var minisitename = getMinisiteName();
	var minisitegroup = getMinisiteGroup();

	if (minisitename == ""|| minisitename=="communitymaps") {
		// this is the community maps page
		//window.location = theURL+"/CommunityMaps.php";
		window.location = getURL() + "includes/CommunityMaps.php?minisite_group="+minisitegroup;
	}
	else {
		//window.location = theURL+"/MiniSite.php?minisitename="+minisitename;
		window.location = getURL()+"includes/MiniSite.php?minisitename="+minisitename+"&minisite_group="+minisitegroup;
	}
}
