// JavaScript Document

function ajaxFunction()
{
	var ajaxRequest;

	try{
	// Opera 8.0+, Firefox, Safari
	ajaxRequest = new XMLHttpRequest();
	} catch (e){// --- 1
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) { // ---- 2
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				//browsers all not support, rare case
				alert("Your browser broke!");
				return false;
			}
		}
	}
	return ajaxRequest;
}

function headerLoginAjax(url){
	htmlRequest = ajaxFunction();
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
	//var Email=document.getElementById('Email').value;
	//alert('Email');
	if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
		alert ("Browser does not support HTTP Request");
		return;
	} 
	htmlRequest.open('POST', url+'include/header_login.php');
	htmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	if(document.getElementById('Counter').value<=2) {
		if(document.getElementById('Username').value == "" || document.getElementById('Password').value == "" )
		{
			document.getElementById('lgfail_1and2').innerHTML="Username or Password cann't be blank";
		}
		//alert(document.getElementById('Counter').value);
		
		htmlRequest.send('Username='+document.getElementById('Username').value+'&Password='+document.getElementById('Password').value+'&Login='+document.getElementById('Login').value+'&Counter='+document.getElementById('Counter').value);
	}
	//alert(document.getElementById('Counter').value);
	/*if(document.getElementById('Counter').value>2)
	{
		var Email=document.getElementById('Email').value;
		alert(Email);
		if(Email=='') {
			document.getElementById('lgfail_1and2').innerHTML="Email Id cann't be blank ";
		}
		if(Email!='' && re.test(Email)==false) {
			document.getElementById('lgfail_1and2').innerHTML="Invalid Email Id";
		}
	}*/
	/*if(document.getElementById('Username').value == "" || document.getElementById('Password').value == "" )
	{
		document.getElementById('lgfail_1and2').innerHTML="Login failed. Please try again.";
	}*/
	else
	{
		var Email=document.getElementById('Email').value;
		//alert(Email);
		if(Email=='') {
			document.getElementById('lgfail_1and2').innerHTML="Email Id cann't be blank ";
		}
		else if(Email!='' && re.test(Email)==false) {
			document.getElementById('lgfail_1and2').innerHTML="Invalid Email Id";
		}
		else
		{
			//alert(document.getElementById('Counter').value);
			//alert(document.getElementById('Email').value);
			htmlRequest.send('Email='+document.getElementById('Email').value+'&Login='+document.getElementById('Login').value+'&Counter='+document.getElementById('Counter').value);
		}
	}
	htmlRequest.onreadystatechange = function()
	{
		if(htmlRequest.readyState == 4)
		{
			document.getElementById("headerLogin").innerHTML = htmlRequest.responseText;
		}
	}
}

function headerLogoutAjax(url){
	htmlRequest = ajaxFunction();
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
	//var Email=document.getElementById('Email').value;
	//alert('Email');
	if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
		alert ("Browser does not support HTTP Request");
		return;
	} 
	htmlRequest.open('POST', url+'include/header_login.php');
	htmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	htmlRequest.send('Logout=Logout');
	htmlRequest.onreadystatechange = function()
	{
		if(htmlRequest.readyState == 4)
		{
			document.getElementById("headerLogin").innerHTML = htmlRequest.responseText;
		}
	}
}	