var minisitegroup;
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) {
	
	//alert(document.getElementById(pulldownName).text);
//	var pulldown=document.getElementById(pulldownName);
//	var pulldownValue = pulldown.value;
//	var pulldownText=pulldown.options[pulldown.options.selectedIndex].text;
//	alert(pulldownText);
//	//document.getElementById(pulldownName + "list").add(pulldownText);
//	
//	
	// Create an Option object        
	
////	var opt = document.createElement("option");
	// Assign text and value to Option object
//	opt.text = pulldownText;
//        opt.value = pulldownValue;

	
	// Add an Option object to Drop Down/List Box
//	document.getElementById(pulldownName + "list").options.add(opt);
	
	// remove it from the original pull down so it can't be added twice
//	pulldown.options.remove(opt);

	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 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 & 
		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("?");  // 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 logout() {
		 xhReq = createXMLHttpRequest();
		 xhReq.open("POST", "processUserLogout.php", true);
		 xhReq.onreadystatechange = loggedOut;
		//alert("logging out");
		 try {
		   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		   //xhReq.setRequestHeader("Content-Type","text/html")
		 }
		 catch (e) {
			// this only works in internet explorer
		 }
		
		 xhReq.send(null);

}
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');
   
}

