// 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();
	}
}

// called from onChange or onClick event of the continent dropdown list
function ContinentListOnChange() 
{
	var ptype = document.getElementById("ptype");
    
    // get selected continent from dropdown list
    var selectedContinent = ptype.options[ptype.selectedIndex].value;
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    // use the following line if using asp
    requestUrl = "xml_data_provider.php" + "?filter=" + encodeURIComponent(selectedContinent);
    // use the following line if using php
    // requestUrl = "xml_data_provider.php" + "?filter=" + encodeURIComponent(selectedContinent);
    
	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 = StateChangeHandler;
		
		// 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);		
	}
}


// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function StateChangeHandler()
{
	// 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)
		{			
			PopulateCountryList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the country dropdown list
function PopulateCountryList(countryNode)
{
    var m_name = document.getElementById("m_name");
	// clear the country list 
	for (var count = m_name.options.length-1; count >-1; count--)
	{
		m_name.options[count] = null;
	}

	var countryNodes = countryNode.getElementsByTagName('country');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < countryNodes.length; count++)
	{
   		textValue = GetInnerText(countryNodes[count]);
		idValue = countryNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		m_name.options[m_name.length] = optionItem;
	}
}

// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}


function validate()
{

var filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
var email = document.frmreg.email_address;
var email_confirm = document.frmreg.confirm_email_address;
   
if(document.frmreg.firstname.value=="")
	{
		alert('Please enter first name');
		document.frmreg.firstname.focus();
		return false;
	}

	if(document.frmreg.lastname.value=="")
	{
		alert('Please enter last name');
		document.frmreg.lastname.focus();
		return false;
	}

	if ( ( document.frmreg.gender[0].checked == false ) && ( document.frmreg.gender[1].checked == false ) && ( document.frmreg.gender[2].checked == false ) )
	{
	alert('Please select gender');
	return false;
	}

	if(document.frmreg.street_address.value=="")
	{
		alert('Please enter street address');
		document.frmreg.street_address.focus();
		return false;
	}

	if(document.frmreg.city.value=="")
	{
		alert('Please enter city');
		document.frmreg.city.focus();
		return false;
	}

	if(document.frmreg.state.value=="")
	{
		alert('Please enter state');
		document.frmreg.state.focus();
		return false;
	}


	if(document.frmreg.country.value==0)
	{
		alert('Please select country');
		document.frmreg.country.focus();
		return false;
	}

	if(document.frmreg.postcode.value=="")
	{
		alert('Please enter Post/Zip Code');
		document.frmreg.postcode.focus();
		return false;
	}

	if(document.frmreg.dob.value=="")
		{
		alert('Please select dob');
		document.frmreg.dob.focus();
		return false;
	}

	if(document.frmreg.telephone.value=="")
	{
		alert('Please enter telephone');
		document.frmreg.telephone.focus();
		return false;
	}


if( (email.value=="") || (email.value==null))
	{
	alert('Please enter email address');
	email.focus();
	return false;
	}
	
	if (!filter.test(email.value)) {
	alert('Please provide a valid email address');
	email.focus;
	return false;
	}


if( (email_confirm.value=="") || (email_confirm.value==null))
	{
	alert('Please enter confirm email address');
	email_confirm.focus();
	return false;
	}
	
	if (!filter.test(email_confirm.value)) {
	alert('Please provide a valid confirm email address');
	email_confirm.focus;
	return false;
	}

if( (email_confirm.value!=email.value))
	{	
	alert('email address and confirm email address do not matched');
	return false;
	}


	if(document.frmreg.s_number.value=="")
	{
		alert('Please enter serial number');
		document.frmreg.s_number.focus();
		return false;
	}

	return true;
}



