// creates an XMLHttpRequest instance
function createXmlHttpRequestObject(){
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works

    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
    {
      try
      {
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else
    return xmlHttp;
}

function HideMenu(){
	document.getElementById("menu1").className= "menu";
    document.getElementById("menu2").className= "menu";
    document.getElementById("menu3").className= "menu";
    document.getElementById("menu4").className= "menu";
}

function Logout(){
	window.location = "Logout.php";
}

function LoadEventForm(month,year){
	var xmlhttp = createXmlHttpRequestObject();
    var URL = "Event_Calendar.php?month="+month+"&year="+year;
    xmlhttp.open("GET", URL, true);
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        	document.getElementById("Events").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.send(null);
}

function Login(){
	var xmlhttp = createXmlHttpRequestObject();
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;

    var URL = "loginrequest.php?username="+username+"&password="+password;
    xmlhttp.open("GET", URL, true);

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        	if(xmlhttp.responseText == "Log In"){
            	window.location = "index.php";
            }else{
            	alert(xmlhttp.responseText);
            }
        }
    }
    xmlhttp.send(null);
}

function SelectCalendarType(id,month,year){
	for (var x = 1; x<10; x++){
    	if (document.getElementById("EType"+x)){
        	document.getElementById("EType"+x).style.background = "white";
        }
    }

	var xmlhttp = createXmlHttpRequestObject();
    var URL = "Event_Calendar.php?month="+month+"&year="+year+"&eventtypeid="+id;
    xmlhttp.open("GET", URL, true);

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        	document.getElementById("Events").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.send(null);
}


function LoadPage(page){
	if (page.indexOf("reloadpage") == -1){
	    var xmlhttp = createXmlHttpRequestObject();
	    var URL = page;
	    xmlhttp.open("GET", URL, true);

	    xmlhttp.onreadystatechange = function() {
	        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	            document.getElementById("body").innerHTML = xmlhttp.responseText;
	        }
	    }
	    xmlhttp.send(null);
    }else{
    	window.location.href = page;
    }
}

function ShowTopSubMenu(id){
	document.getElementById("TopSubMenu").innerHTML = "";
	document.getElementById("TopSubMenu").style.visibility = "visible";
    document.getElementById("TopSubMenu").style.left = findPosX(document.getElementById("topmenu"+id))+"px";
	var xmlhttp = createXmlHttpRequestObject();
    var URL = "topsubmenu.php?id="+id;
    xmlhttp.open("GET", URL, true);

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        	document.getElementById("TopSubMenu").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.send(null);
}
function HideTopSubMenu(){
	document.getElementById("TopSubMenu").style.visibility = "hidden";
    document.getElementById("TopSubMenu").innerHTML = "";
}

function SelectCalendarArea(countyid,month,year){
	var xmlhttp = createXmlHttpRequestObject();
    var URL = "Event_Calendar.php?countyid="+countyid+"&month="+month+"&year="+year;
    xmlhttp.open("GET", URL, true);

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        	document.getElementById("Events").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.send(null);
}
function SelectCalendarGrosvenor(month,year){
	var xmlhttp = createXmlHttpRequestObject();
    var URL = "Event_Calendar.php?grosvenor=-1&month="+month+"&year="+year;
    xmlhttp.open("GET", URL, true);

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        	document.getElementById("Events").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.send(null);
}


function ShowEventDetail(id){

	var xmlhttp = createXmlHttpRequestObject();
    var URL = "Event_Detail.php?id="+id+"&newwindow=-1";
    window.open(URL,"","toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=700,height=500");
    //xmlhttp.open("GET", URL, true);

    //xmlhttp.onreadystatechange = function() {
    //    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    //   	document.getElementById("Events").innerHTML = xmlhttp.responseText;
    //   }
    //}
    //xmlhttp.send(null);
}

function ZoomImage(image){
	window.open(image,"","toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=500,height=500");
}
function ShowEventDetailNewWindow(id){
	window.open("Event_Detail.php?id="+id+"&newwindow=-1","","toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=1000,height=600");
}

function NewWindow(site){
	if(site.indexOf("http://") == -1) {
    	site = "http://"+site;
    }
	window.location.href = site;
}

function HideEventPopupInfo(obj){
	document.getElementById("DayInfo").style.visibility = "hidden";
    obj.style.color = "blue";
}

function ShowEventPopupInfo(id,obj,e) {
	document.getElementById("DayInfo").innerHTML = "";
	var xmlhttp = createXmlHttpRequestObject();
    var URL = "Event_PopupInfo.php?id="+id;

    xmlhttp.open("GET", URL, true);

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        	document.getElementById("DayInfo").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.send(null);

	e = (e) ? e : window.event;
	document.getElementById("DayInfo").style.visibility = "visible";
	obj.style.color = "red";
	document.getElementById("DayInfo").style.left = mouseX(e)+15+"px";
	document.getElementById("DayInfo").style.top = mouseY(e)+15+"px";
}

function SocietyLetterSelected(letter){
    if (letter == "-1"){
    	document.getElementById("SocietyLetter").innerHTML = "";
    }else{
	    var xmlhttp = createXmlHttpRequestObject();
	    var URL = "Society_LetterSelected.php?letter="+letter;
	    xmlhttp.open("GET", URL, true);

	    xmlhttp.onreadystatechange = function() {
	        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	            document.getElementById("SocietyLetter").innerHTML = xmlhttp.responseText;
	        }
	    }
	    xmlhttp.send(null);
    }
}
function SpecialSocietyLetterSelected(letter){
    if (letter == "-1"){
    	document.getElementById("SpecialSocietyLetter").innerHTML = "";
    }else{
	    var xmlhttp = createXmlHttpRequestObject();
	    var URL = "Society_SpecialLetterSelected.php?letter="+letter;
	    xmlhttp.open("GET", URL, true);

	    xmlhttp.onreadystatechange = function() {
	        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	            document.getElementById("SpecialSocietyLetter").innerHTML = xmlhttp.responseText;
	        }
	    }
	    xmlhttp.send(null);
    }
}
function ShowSelectedOrganisation(id){
	if (document.getElementById("OrganisationSelected"+id).innerHTML == ""){
	    var xmlhttp = createXmlHttpRequestObject();
	    var URL = "Society_OrganisationSelected.php?id="+id;
	    xmlhttp.open("GET", URL, true);

	    xmlhttp.onreadystatechange = function() {
	        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	            document.getElementById("OrganisationSelected"+id).innerHTML = xmlhttp.responseText;
	        }
	    }
	    xmlhttp.send(null);
    }else{
    	document.getElementById("OrganisationSelected"+id).innerHTML = "";
    }
}

function ShowFederation(id){
	var xmlhttp = createXmlHttpRequestObject();
	var URL = "Society_FederationSelected.php?id="+id;
    xmlhttp.open("GET", URL, true);

	xmlhttp.onreadystatechange = function() {
	    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	        document.getElementById("FederationView").innerHTML = xmlhttp.responseText;
	    }
	}
	xmlhttp.send(null);
}

function ShowSelectedFOrganisation(id){
	if (document.getElementById("FOrganisationSelected"+id).innerHTML == ""){
	    var xmlhttp = createXmlHttpRequestObject();
	    var URL = "Society_OrganisationSelected.php?id="+id;
	    xmlhttp.open("GET", URL, true);

	    xmlhttp.onreadystatechange = function() {
	        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	            document.getElementById("FOrganisationSelected"+id).innerHTML = xmlhttp.responseText;
	        }
	    }
	    xmlhttp.send(null);
    }else{
    	document.getElementById("FOrganisationSelected"+id).innerHTML = "";
    }
}
function ShowSelectedSpecialOrganisation(id){
	if (document.getElementById("SpecialOrganisationSelected"+id).innerHTML == ""){
	    var xmlhttp = createXmlHttpRequestObject();
	    var URL = "Society_OrganisationSelected.php?id="+id;
	    xmlhttp.open("GET", URL, true);

	    xmlhttp.onreadystatechange = function() {
	        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	            document.getElementById("SpecialOrganisationSelected"+id).innerHTML = xmlhttp.responseText;
	        }
	    }
	    xmlhttp.send(null);
    }else{
    	document.getElementById("SpecialOrganisationSelected"+id).innerHTML = "";
    }
}
function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}

function findPosX(obj) {
	var curleft = 0;
	if(obj.offsetParent)
	while(1){
	    curleft += obj.offsetLeft;
	    if(!obj.offsetParent)
	    break;
	    obj = obj.offsetParent;
	}else if(obj.x)
	curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent)
	while(1){
	    curtop += obj.offsetTop;
	    if(!obj.offsetParent)
	    break;
	    obj = obj.offsetParent;
	}else if(obj.y)
	curtop += obj.y;
	return curtop;
}

function Checkpostcode(obj) {
	if (obj.value == "Enter postcode"){
    	obj.value = "";
    }else if (obj.value == ""){
    	obj.value = "Enter postcode";
    }

}
function Searchpostcode() {
	var postcode = document.getElementById("postcode").value;
    if (postcode == "Enter postcode"){
    	exit();
    }
    var xmlhttp = createXmlHttpRequestObject();
    var URL = "postcodefunctions.php?postcode="+postcode;
    xmlhttp.open("GET", URL, true);
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("postcoderesults").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.send(null);
}

function submitForm(formName, navigateValue) {
	if (navigateValue != null && navigateValue != "") {
		document.forms[formName].navigate.value = navigateValue;
	}
    document.forms[formName].submit();
}