// JavaScript Document

// declare a global  XMLHTTP Request object

var XmlHttpObj;


// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla

function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)

	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}

	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 

	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}
function RequestRegister()
{
var e= document.getElementById('f_email');

if (e.value == '') { alert ("Please enter your email"); return false;}

if (e.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
{sendRequest(e.value);return true;}
else
{alert ("Invalid email");return false;}


}

function sendRequest(regdate,name,email,mobile)
    {
        
	     	
	var requestUrl;
 
    requestUrl = "RegisterRequest.php?e="+email+"&date="+regdate+"&name="+name+"&mob="+mobile; 


	CreateXmlHttpObj();

	// verify XmlHttpObj variable was successfully initialized

	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		
		XmlHttpObj.onreadystatechange = sendRequestStateChangeHandler;

		// define the iteraction with the server -- true for as asynchronous.

		XmlHttpObj.open("GET", requestUrl,  true);

		// send request to server, null arg  when using "GET"

		XmlHttpObj.send(null);	

		//XmlHttpObj.abort();	

	}
}

// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server

function sendRequestStateChangeHandler()
{
   // state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK		
		if(XmlHttpObj.status == 200)
		{	
			showConfirmation();
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

function showConfirmation(responseNode)
{
	
	var result = 'Din påmelding er registrert';
	document.getElementById("Confirm").innerHTML = result;
	
}

function GetRegisterData()
{
var name= document.getElementById('reg_name').value;
if (name == '') return;
var email= document.getElementById('reg_email').value;
if (email == '') return;
if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)
return;
var regdate= document.getElementById('reg_date').value;
if (regdate == 0) return;
var mobile = document.getElementById('reg_mobile').value;
sendRequest(regdate,name,email,mobile);


}

function LogRequest(ip,total)
{
	var requestUrl;
 
    requestUrl = "LogRequest.php?ip="+ip+"&tot="+total; 


	CreateXmlHttpObj();

	// verify XmlHttpObj variable was successfully initialized

	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		
		XmlHttpObj.onreadystatechange = LogRequestStateChangeHandler;

		// define the iteraction with the server -- true for as asynchronous.

		XmlHttpObj.open("GET", requestUrl,  true);

		// send request to server, null arg  when using "GET"

		XmlHttpObj.send(null);	

		//XmlHttpObj.abort();	

	}
	
}

function LogRequestStateChangeHandler()
{
   // state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK		
		if(XmlHttpObj.status == 200)
		{	
			//alert("Result logged");
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}



