
//onerror = function(error,url,line) { alert("Error : " + error + "\nLine : " + line); }


function Login(username, password)
{
	
	username = document.getElementById(username).value;
	password = document.getElementById(password).value;
	
	if(!username)
	{
		alert("Please enter Login User Name");
		return;
	}
	
	if(!password)
	{
		alert("Please enter Login Password");
		return;
	}
	
	jax = createAjax();
	if(!jax)return;
	
	var param = "Action=Login&username=" + username + "&password="+password
	jax.open("POST","Auth.php",true)
	jax.onreadystatechange = function() { loginResponse(jax); }
	jax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
	jax.send(param);	
}

function loginResponse(jax)
{
	if(jax.readyState==4)
	{
		var response = trim(jax.responseText)	

		if( trim(jax.responseText)=="INV")
			alert("Invalid Username or Password");

		else if( trim(jax.responseText)=="VAL")
			window.location = 'userpage.php';

		else
			if(response.length<300)
				alert(response)			
			else
				wideOpen(response)		
	}
}

function Register(username, password, email)
{
	username = document.getElementById(username).value;
	password = document.getElementById(password).value;
	email = document.getElementById(email).value;
		
	if(!username)
	{
		alert("Please enter Registration User Name");
		return;
	}

	if(username.length<6)
	{
		alert("UserName should be more than 5 characters");
		return;
	}
	
	if(!password)
	{
		alert("Please enter Registration Password");
		return;
	}

	if(password.length<6)
	{
		alert("Password should be more than 5 characters");
		return;
	}
	
	if(!email)
	{
		alert("Please enter an Email Identification");
		return;
	}
	
	if(!echeck(email))return;
	
	jax = createAjax();
	if(!jax)return;
	
	var param = "Action=Register&username=" + username + "&password="+password + "&email=" + email
	jax.open("POST","register.php",true)
	jax.onreadystatechange = function() { registerResponse(jax); }
	jax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
	jax.send(param);		
}

function registerResponse(jax)
{
	if(jax.readyState==4)
	{
		if( trim(jax.responseText)=="REG")
		{
			clearRvalues();									
			alert("Registeration Successful.\nAn Activation code is sent to your email.\nPlease confirm by checking your mail.");
		}
		else if( trim(jax.responseText)=="UNA")
		{
			clearRvalues();			
			alert("The UserName or Email provided is already registered")
		}
		else
		{
/*			if(jax.responseText.length<300)
				alert(jax.responseText)			
			else*/
				wideOpen(jax.responseText)
		}
	}
}

function clearRvalues()
{
	document.getElementById('rusername').value=""
	document.getElementById('rpassword').value=""
	document.getElementById('email').value=""
}

function wideOpen(str)
{

	var win = window.open("","sam","height=400,width=500,left=200,top=200");
	win.document.write(str);

}

/*function createAjax()
{
	if(window.XMLHttpRequest)
		return new XMLHttpRequest()
	else if(window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP")
	else 
	{
		alert("Browser dosen't support Ajax");
		return null;
	}
}
*/

function init()
{
	document.getElementById('lusername').focus();
}

function createAjax()
{
	if(window.XMLHttpRequest)
		return new XMLHttpRequest()
	else if(window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP")
	else 
	{
		alert("Browser dosen't support Ajax");
		return null;
	}
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function echeck(myForm) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm)){
return (true)
}
alert("Invalid E-mail Address! Please re-enter.")
return (false)
}