var httpAjax = false; 
var divArea = ""; 
var theUrl = ""; 

    //Öppnar en ny php sida. Använder GET eller POST för att skicka variablar
    function openPage (thediv, url, postparameters) { 
        httpAjax = false; 
        handler = false; 
        divArea = thediv;
        theUrl = url;
        
        openAction = "GET";
        sendAction = null;
        //Om post parametrar skickas med funktionen så kommer open och send Action sättas till POST
        if(postparameters) { openAction = "POST"; sendAction = postparameters; }
        
        
        //Kollar om Ajax kan öppna ett request till servern (XML)
        if (window.XMLHttpRequest) { 
            httpAjax = new XMLHttpRequest(); 
            if (httpAjax.overrideMimeType) { 
                httpAjax.overrideMimeType('text/xml'); 
            } 
        } 
        else if (window.ActiveXObject) { 
            try { 
                httpAjax = new ActiveXObject("Msxml2.XMLHTTP"); 
            }
            catch (e) { 
                try { 
                    httpAjax = new ActiveXObject("Microsoft.XMLHTTP"); 
                } catch (e) { 
                    alert("Couldn´t build an AJAX instance."); 
                    return false; 
                } 
            } 
        } 
        
        
        //Här skickas och hämtas själva responsen ifrån Servern
        try { 
            httpAjax.onreadystatechange = getPage; 
        } 
        catch (e) { 
            alert("onreadystatechange didn´t go well!"); 
            return false; 
        } 

        try { 
        	httpAjax.open(openAction, url, true);        	
        } 
        catch (e) { 
            alert("Couldn´t open url."); 
            return false; 
        } 
        if(openAction == "POST") {
          httpAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          httpAjax.setRequestHeader("Content-length", postparameters.length);
          httpAjax.setRequestHeader("Connection", "close");
        }
        try {
        	httpAjax.send(sendAction); 
        } 
        catch (e) { 
            alert("Couldn´t send request."); 
            return false; 
        } 
        
        return true; 
    } 

    //Får respons ifrån servern. Om servern ger respons så hämtas sidan man vill öppna.
    function getPage () { 
        //0 = Uninitialized - open() has not been called yet.
        //1 = Loading - send() has not been called yet.
        //2 = Loaded - send() has been called, headers and status are available.
        //3 = Interactive - Downloading, responseText holds the partial data.
        //4 = Completed - Finished with all operations.     	
    	  //alert(httpAjax.readyState+" Page: "+divArea+" openAction: "+openAction+" sendAction: "+sendAction);
    	  
        if(httpAjax.readyState == 4){ 
            document.getElementById(divArea).innerHTML = httpAjax.responseText;
            if(theUrl == "t_login.php"){openPage('usermenu','m_leftmenu_user.php'); }
            else if(theUrl == "t_login.php?logout=yes"){openPage('usermenu','m_leftmenu_user.php?logout=yes');}
	    else if(theUrl == "m_leftmenu_user.php?logout=yes"){ openPage('content','p_loginstatus.php?logout=yes'); }
             else if(theUrl == "m_leftmenu_user.php"){ openPage('content','p_loginstatus.php'); }
	    else if(theUrl.substring(0,13) == "p_chatbox.php"){ openPage('chatbox','r_chatbox.php'); }
            return true; 
        } else { 
            document.getElementById(divArea).innerHTML = "Loading..."; 
        } 
    } 
    
    
    
    
    //En POST funktion som hämtar post strängen
    function postPassreq(thediv, url) {

      var poststr = "userreq=" + encodeURIComponent(document.getElementById('userreq').value) + "&posted=" + encodeURIComponent(document.getElementById('posted').value);			
      openPage(thediv,url,poststr);

   }

    //En POST funktion som hämtar post strängen
    function postLogin(thediv, url) {

      var poststr = "username=" + encodeURIComponent(document.getElementById('username').value) + "&password=" + encodeURIComponent(document.getElementById('password').value);			
      openPage(thediv,url,poststr);

   }

    //En POST funktion som hämtar post strängen
    function postJoin(thediv, url) {

      var sexB = "";
      if(document.getElementById('sex1').checked) { sexB = "Male"; }
      else if(document.getElementById('sex2').checked) { sexB = "Female"; }
          	
      var poststr =
      "user=" + encodeURIComponent( document.getElementById("user").value ) +
      "&pass=" + encodeURIComponent( document.getElementById("pass").value ) +
      "&pass2=" + encodeURIComponent( document.getElementById("pass2").value ) +
      "&charname=" + encodeURIComponent( document.getElementById("charname").value ) +
      "&firstname=" + encodeURIComponent( document.getElementById("firstname").value ) +
      "&sex=" + sexB +
      "&country=" + encodeURIComponent( document.getElementById("country").value ) +
      "&birthyear=" + encodeURIComponent( document.getElementById("birthyear").value ) +
      "&level=" + encodeURIComponent( document.getElementById("level").value ) +
      "&class=" + encodeURIComponent( document.getElementById("class").value ) +
      "&hours=" + encodeURIComponent( document.getElementById("hours").value ) +
      "&ventrilo=" + encodeURIComponent( document.getElementById("ventrilo").value ) +
      "&email=" + encodeURIComponent( document.getElementById("email").value ) +
      "&games=" + encodeURIComponent( document.getElementById("games").value ) +
      "&legions=" + encodeURIComponent( document.getElementById("legions").value ) +
      "&presenation=" + encodeURIComponent( document.getElementById("presenation").value ) +
      "&posted=" + encodeURIComponent( document.getElementById("posted").value );

			openPage(thediv,url,poststr);	
			//alert("The div: "+thediv+" url: "+url+" poststr: "+poststr);
   }

    //En POST funktion som hämtar post strängen
    function postUserUpdate(thediv, url) {

      var sexB = "";
      var active = "";
      if(document.getElementById('sex1').checked) { sexB = "Male"; }
      else if(document.getElementById('sex2').checked) { sexB = "Female"; }

      if(document.getElementById('active1').checked) { active = "active"; }
      else if(document.getElementById('active2').checked) { active = "inactive"; }
          	
      var poststr =
      "&charname=" + encodeURIComponent( document.getElementById("charname").value ) +
      "&firstname=" + encodeURIComponent( document.getElementById("firstname").value ) +
      "&lastname=" + encodeURIComponent( document.getElementById("lastname").value ) +
      "&sex=" + sexB +
      "&country=" + encodeURIComponent( document.getElementById("country").value ) +
      "&city=" + encodeURIComponent( document.getElementById("city").value ) +
      "&class=" + encodeURIComponent( document.getElementById("class").value ) +
      "&birthyear=" + encodeURIComponent( document.getElementById("birthyear").value ) +
      "&level=" + encodeURIComponent( document.getElementById("level").value ) +
      "&email=" + encodeURIComponent( document.getElementById("email").value ) +
      "&proff1=" + encodeURIComponent( document.getElementById("proff1").value ) +
      "&proff2=" + encodeURIComponent( document.getElementById("proff2").value ) +
      "&style=" + encodeURIComponent( document.getElementById("style").value ) +
      "&active=" + active +
      "&forumsignature=" + encodeURIComponent( document.getElementById("forumsignature").value ) +
      "&presenation=" + encodeURIComponent( document.getElementById("presenation").value ) +
      "&posted=" + encodeURIComponent( document.getElementById("posted").value );

			openPage(thediv,url,poststr);	
			//alert("The div: "+thediv+" url: "+url+" poststr: "+poststr);
   }

    //En POST funktion som hämtar post strängen
    function postForum(thediv, url) {  
      var stickyB = ""; 
      if(document.getElementById('sticky').checked) { stickyB = "yes"; }
	
      var poststr =
      "topic=" + encodeURIComponent( document.getElementById("topic").value ) +
      "&text=" + encodeURIComponent( document.getElementById("text").value ) +
      "&sticky=" + stickyB +
      "&pubname=" + encodeURIComponent( document.getElementById("pubname").value ) +
      "&posted=" + encodeURIComponent( document.getElementById("posted").value );
      openPage(thediv,url,poststr);
   }


    //En POST funktion som hämtar post strängen
    function postChatBox(thediv, url) {  
	
      var poststr =
      "&chatboxpubname=" + encodeURIComponent( document.getElementById("chatboxpubname").value ) +
      "&chatboxtext=" + encodeURIComponent( document.getElementById("chatboxtext").value ) +
      "&postedchatbox=" + encodeURIComponent( document.getElementById("postedchatbox").value );
      openPage(thediv,url,poststr);	

   }

    //En POST funktion som hämtar post strängen
    function postNewsFocus(thediv, url) {  
	
      var poststr =
      "&charname=" + encodeURIComponent( document.getElementById("charname").value ) +
      "&text=" + encodeURIComponent( document.getElementById("text").value ) +
      "&posted=" + encodeURIComponent( document.getElementById("posted").value );
      openPage(thediv,url,poststr);	

   }

    //En POST funktion som hämtar post strängen
    function postGuestBook(thediv, url) {  
	
      var poststr =
      "&gbpubname=" + encodeURIComponent( document.getElementById("gbpubname").value ) +
      "&gbtext=" + encodeURIComponent( document.getElementById("gbtext").value ) +
      "&postedgb=" + encodeURIComponent( document.getElementById("postedgb").value );
      openPage(thediv,url,poststr);	

   }

    //En POST funktion som hämtar post strängen
    function postMail(thediv, url) {  
	
      var poststr =
      "&topic=" + encodeURIComponent( document.getElementById("topic").value ) +
      "&text=" + encodeURIComponent( document.getElementById("text").value ) +
      "&posted=" + encodeURIComponent( document.getElementById("posted").value );
      openPage(thediv,url,poststr);	

   }

    //En POST funktion som hämtar post strängen
    function postmassMail(thediv, url) {  
	
      var poststr =
      "&topic=" + encodeURIComponent( document.getElementById("topic").value ) +
      "&text=" + encodeURIComponent( document.getElementById("text").value ) +
      "&class=" + encodeURIComponent( document.getElementById("class").value ) +
      "&posted=" + encodeURIComponent( document.getElementById("posted").value );
      openPage(thediv,url,poststr);	

   }

    //En POST funktion som hämtar post strängen
    function postNews(thediv, url) {  
	
      var poststr =
      "&topic=" + encodeURIComponent( document.getElementById("topic").value ) +
      "&type=" + encodeURIComponent( document.getElementById("type").value ) +
      "&text=" + encodeURIComponent( document.getElementById("text").value );
      openPage(thediv,url,poststr);	

   }

    //En POST funktion som hämtar post strängen
    function postRaid(thediv, url) {  
	
      var poststr =
      "text=" + encodeURIComponent( document.getElementById("text").value ) +
      "&topic=" + encodeURIComponent( document.getElementById("topic").value ) +
      "&year=" + encodeURIComponent( document.getElementById("year").value ) +
      "&month=" + encodeURIComponent( document.getElementById("month").value ) +
      "&day=" + encodeURIComponent( document.getElementById("day").value ) +
      "&hour=" + encodeURIComponent( document.getElementById("hour").value ) +
      "&minute=" + encodeURIComponent( document.getElementById("minute").value );
      openPage(thediv,url,poststr);
   }