/* The following script is owned by media-i 2006 */
/* You may not use this without the permission of media-i in any digital form */


function createRequestObject(){
         var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        return http_request; //return the object

}

/* Function called to get the product categories list */
function getPassword(){
        /* Create the request. The first argument to the open function is the method (POST/GET),
                and the second argument is the url...
                document contains references to all items on the page
                We can reference document.form_category_select.select_category_select and we will
                be referencing the dropdown list. The selectedIndex property will give us the
                index of the selected item.
        */

        http.open('post','login.php');
        /* Define a function to call once a response has been received. This will be our
                handleProductCategories function that we define below. */
        http.onreadystatechange = handlePassword;
        /* Send the data. We use something other than null when we are sending using the POST
                method. */
        /* use this for the post method */
        http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

        // get the data from the login html form changes variables according to the data comming in from the form
        username=document.login.username.value;
        password=document.login.password.value;
        http.send('username='+username+'&password='+ password);

}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handlePassword(){
        /* Make sure that the transaction has finished. The XMLHttpRequest object
                has a property called readyState with several states:
                0: Uninitialized
                1: Loading
                2: Loaded
                3: Interactive
                4: Finished */
				
        if(http.readyState == 4){ //Finished loading the response
                /* using the responseText property of
                        the XMLHttpRequest object. */
                var response = http.responseText;
                /* And now we want to change the  <div> content.
                        we do this using an ability to get/change the content of a page element
                        that we can find: innerHTML. */

                
				/* check to see if it is a databas error */
				if (response == "could not connect") {
				document.getElementById("login").style.display = 'none';
				window.location = "database-error.php";
				return;
				}
				/* Check to see if the response to login was successfull by testing the returning string */
				if (response != "<p align='center' class='error'>You are not authorised</p>") {
				document.getElementById("login").style.display = 'none';
				window.location = "EUROGM/WIND/wind.php";
	
				} else {
				document.getElementById('pass').innerHTML = response;
				/* document.getElementById('loginmessage').innerHTML = "<p class='loginfailed' align='center'>! Your username and password do not match please try again.</p>"; */
				alert("! Your username and password do not match please try again.")
				
				}
				
        }
}

/* The variable http will hold our new XMLHttpRequest object. */
        var http = createRequestObject();

 /* use to supree form sending on the action parameter */

 function validate(){
  return false;
 }
/* clean up pointer variables */
window.onunload = freemem;

function freemem() {
	
	http = null;

}