// JavaScript Document
/**
 * Author: Didiek Rullianda
 * Created: March, 17th 2008
 * Modified: April, 22th 2008
 * Credits: Ex Diploma, Degree, Postgraduate, and various source codes
 */

/* disable right click*/
var message="\xA9 Copyright by PPIA Victoria 2008";

///////////////////////////////////////////////////////////////////////////////////////////////////////////
function clickIE4()
{
   if (event.button==2)
   {
      alert(message);
      return false;
   }
}

function clickNS4(e)
{
   if (document.layers||document.getElementById&&!document.all)
   {
      if (e.which==2||e.which==3)
      {
         alert(message);
         return false;
      }
   }
}

if (document.layers)
{
   document.captureEvents(Event.MOUSEDOWN);
   document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById)
{
   document.onmousedown=clickIE4;
}

document.oncontextmenu=new Function("alert(message);return false")

///////////////////////////////////////////////////////////////////////////////////////////////////////////

/* xHRObject = XML HTTP Request Object check whether the browser is IE or Firefox */
var xHRObject = false;

if (window.XMLHttpRequest) // for Firefox, Netscape, Opera
{
    xHRObject = new XMLHttpRequest();
}
else if (window.ActiveXObject) // for IE
{
    xHRObject = new ActiveXObject("Microsoft.XMLHTTP");
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////

/* function to validate input */
function valEntry() 
{
	var regform = document.getElementById("regform");
	
	// This statement checks that only upper or lowercase case letters, repeated one or more times 
	var format = /^([A-Za-z]|[-\s])+$/g;
	
	// If First Name is empty 
	if (regform.fname.value == "") {
	   alert("Please enter the First name");
	   regform.fname.focus();
	   return(false);
	}
	
	// If invalid characters in First name 
	if(!regform.fname.value.match(format))
	{ 
		alert("First name is invalid. Please omit the numbers and all special characters.");
		regform.fname.focus();
		regform.fname.select();
	   	return(false);	
	}
	
	// If Last Name is empty 
	if (regform.lname.value == "") {
	   alert("Please enter the Last name");
	   regform.lname.focus();
	   return(false);
	}
	
	// If invalid characters in Last name 
	if(!regform.lname.value.match(format))
	{ 
		alert("Last name is invalid. Please omit the numbers and all special characters.");
		regform.lname.focus();
		regform.lname.select();
	   	return(false);	
	}
	
	// If Institution is empty 
	if (regform.inst.value == "") {
	   alert("Please enter the Institution where you are from");
	   regform.inst.focus();
	   return(false);
	}
	
	// If invalid characters in Institution 
	if(!regform.inst.value.match(format))
	{ 
		alert("Institution is invalid. Please omit the numbers and all special characters.");
		regform.inst.focus();
		regform.inst.select();
	   	return(false);	
	}
	
	// If Email address is empty 
	if (regform.email.value == "") {
	   alert("Please enter the Email Address");
	   regform.email.focus();
	   return(false);
	}
	
	// If invalid characters in email   
	if (regform.email.value.indexOf('@', 0) == -1 || regform.email.value.indexOf('@', 0) == 0 || regform.email.value.indexOf('.', 0) == -1 || 
		regform.email.value.indexOf(',', 0) != -1 || regform.email.value.indexOf(' ', 0) != -1 ) { 
	   	alert("Invalid Email Address") ;
		regform.email.select();
		return (false);
	}
	
	// If Address is empty 
	/*if (regform.address.value == "") {
	   alert("Please enter the Address");
	   regform.address.focus();
	   return(false);
	}
	
	// If Suburb is empty 
	if (regform.suburb.value == "") {
	   alert("Please enter the Suburb");
	   regform.suburb.focus();
	   return(false);
	}
	
	// If State is empty 
	if (regform.state.value == "") {
	   alert("Please enter the State");
	   regform.state.focus();
	   return(false);
	}
	
	// If invalid characters in suburb 
	if(!regform.suburb.value.match(format))
	{ 
		alert("Suburb is invalid. Please omit the numbers and all special characters.");
		regform.suburb.focus();
		regform.suburb.select();
	   	return(false);	
	}
	
	// If invalid characters in state 
	if(!regform.state.value.match(format))
	{ 
		alert("State is invalid. Please omit the numbers and all special characters.");
		regform.state.focus();
		regform.state.select();
	   	return(false);	
	}
	
	// If Postcode is empty 
	if (regform.pcode.value == "") {
	   alert("Please enter the Postcode");
	   regform.pcode.focus();
	   return(false);
	}
	
	// If both contact number are empty 
	if ((regform.phno.value == "") && (regform.mobno.value == "")) {
	   alert("Please enter either phone number or mobile number to be contacted");
	   regform.phno.focus();
	   return(false);
	}*/
	
	// Retrieve data from the html form
	var firstname = document.getElementById("fname").value;
	var lastname = document.getElementById("lname").value;
	var institution = document.getElementById("inst").value;
	var email = document.getElementById("email").value;
	var webpage = document.getElementById("weblog").value;
	var address = document.getElementById("address").value;
	var suburb = document.getElementById("suburb").value;
	var state = document.getElementById("state").value;
	var postcode = document.getElementById("pcode").value;
	var country = document.getElementById("country").value;
	var phone = document.getElementById("phno").value;
	var mobile = document.getElementById("mobno").value;
	
	// Ask user whether they want to submit the form 
	var submitForm = confirm("Are you sure you want to submit this form?");
	
	var spantag = document.getElementById("display_message");
    spantag.innerHTML = "";
	
	if (submitForm == true) // Add data
   	{	
		xHRObject.open("GET", "register.php?id=" + Number(new Date) + "&firstname="+ firstname
						+"&lastname="+ lastname+"&institution="+ institution
						+"&email="+ email+"&webpage="+ webpage+"&address=" 
						+ address+"&suburb="+ suburb+"&state="+ state 
						+"&postcode="+ postcode +"&country="+ country
						+"&phone="+ phone +"&mobile="+ mobile, true);
		xHRObject.send(null);
		alert("Registration successful. \nPlease check your email to retrieve your registration number.");
		spantag.innerHTML = "You have successfully registered to this website.";
		document.getElementById("fname").value = "";
		document.getElementById("lname").value = "";
		document.getElementById("inst").value = "";
		document.getElementById("email").value = "";
		document.getElementById("weblog").value = "";
		document.getElementById("address").value = "";
		document.getElementById("suburb").value = "";
		document.getElementById("state").value = "";
		document.getElementById("pcode").value = "";
		document.getElementById("phno").value = "";
		document.getElementById("mobno").value = "";
		return(false);
   	}
   	else
   	{
		alert("Registration cancelled.");
		return(false);
   	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
/* Ask user if they want to reset the form */
function confirmreset()
{
	var resetForm = confirm("Are you sure you want to reset this form?");
	if (resetForm == true)
	{
	   return(true);
	}
	else
	{
	   return(false);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
/* function to retrieve data from XML and display it to the browser */
function showParticipants() 
{
    xHRObject.open("GET", "data/registration.xml?id=" + Number(new Date), true);
    xHRObject.onreadystatechange = getData;
    xHRObject.send(null); 
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
/* function to get XML element and child nodes */
function getData() 
{
    if ((xHRObject.readyState == 4) &&(xHRObject.status == 200))
    {
        var serverResponse = xHRObject.responseXML;
        var header = serverResponse.getElementsByTagName("participant");
		
        var spantag = document.getElementById("list");
        spantag.innerHTML = "";
		
		if (window.ActiveXObject) // display XML contents for IE browser
        {
			if(parseInt(header.length) < 2)
			{
				spantag.innerHTML += "<br />Number of participants registered: <strong>" + header.length + " </strong>participant.<br /><br />";
			}
			else
			{
				spantag.innerHTML += "<br />Number of participants registered: <strong>" + header.length + " </strong>participants.<br /><br />";
			}
		}
		else if (window.XMLHttpRequest) // display XML for opera, firefox, netscape
		{
			if(parseInt(header.length) < 2)
			{
				spantag.innerHTML += "<br />Number of participants registered: <strong>" + header.length + " </strong>participant.<br /><br />";
			}
			else
			{
				spantag.innerHTML += "<br />Number of participants registered: <strong>" + header.length + " </strong>participants.<br /><br />";
			}
		}
		
		if (header.length==0)
		{
			spantag.innerHTML = "<h2><center>There is no participant</center></h2>";
		}
		
		// Declare variables and create the header, footer, and caption.
  		var oTable = document.createElement("TABLE");
  		var oTHead = document.createElement("THEAD");
  		var oTBody0 = document.createElement("TBODY");
  		var oTBody1 = document.createElement("TBODY");
  		var oTFoot = document.createElement("TFOOT");
  		var oRow, oCell, oCell0, oCell1;
  		var i, j;
        
		// Declare stock data that would normally be read in from a stock Web site.
  		var heading = new Array();

  		heading[0] = "Reg #";
  		heading[1] = "First Name";
  		heading[2] = "Last Name";
		heading[3] = "Institution";
  		heading[4] = "Email Address";
  		heading[5] = "Webpage/Blog";
  		heading[6] = "Address";
		heading[7] = "Suburb";
		heading[8] = "State";
		heading[9] = "Postcode";
		heading[10] = "Country";
		heading[11] = "Telephone No";
  		heading[12] = "Mobile No";
		
		// Insert the created elements into oTable.
  		oTable.appendChild(oTHead);
  		oTable.appendChild(oTBody0);
  		oTable.appendChild(oTBody1);
  		oTable.appendChild(oTFoot);
		
		// Set the table's border width and colors.
  		oTable.border=1;
		
		// Insert a row into the header and set its background color.
  		oRow = document.createElement("TR");
  		oTHead.appendChild(oRow);
		oTHead.setAttribute("bgColor","lightskyblue");
		
		// Create and insert cells into the header row.
  		for (i=0; i<heading.length; i++)
  		{
    		oCell = document.createElement("TH");
    		oCell.innerHTML = heading[i];
    		oRow.appendChild(oCell);
  		}
		
		for (i=0; i<header.length; i++)
        {
			var oBody = (i<2) ? oTBody0 : oTBody1;
    		oRow = document.createElement("TR");
    		oBody.appendChild(oRow);
			
			if (window.ActiveXObject) // display XML contents for IE browser
        	{
				for (j=0; j<heading.length; j++)
    			{
      				oCell = document.createElement("TD");
      				oCell.innerHTML = header[i].childNodes[j].text;
      				oRow.appendChild(oCell);
    			}
			}
			else // display XML contents for Firefox, Opera, Netscape browser
        	{
				for (j=0; j<heading.length; j++)
    			{
      				oCell = document.createElement("TD");
      				oCell.innerHTML = header[i].childNodes[j].textContent;
      				oRow.appendChild(oCell);
    			}
			}
		}
				
		// Insert the table into the document tree
		spantag.appendChild(oTable);
	}
}