function RemoteRequestObject() {
 	var A = false;
 	try 	{
 		A = new ActiveXObject("Msxml2.XMLHTTP");
 	}
 	catch(e) 	{
 		try 		{
 			A = new ActiveXObject("Microsoft.XMLHTTP");
 		}
 		catch(err) 		{
 			A = false;
 		}
 	}
 	if(!A && typeof(XMLHttpRequest) != 'undefined')
 		A = new XMLHttpRequest();
 		
 	return A;
 }

function fnEmail(txtEmail)
{
	if (txtEmail.value!="")
	{
		var intLen=txtEmail.value.length
		var blnFlag=0
		if (txtEmail.value.charAt(0)=="@" || txtEmail.value.charAt(0)==".")
		{
			alert("Invalid E-Mail Address")
			txtEmail.focus()
			return false
		}
		if (txtEmail.value.charAt(intLen-1)=="@" || txtEmail.value.charAt(intLen-1)==".")
		{
			alert("Invalid E-Mail Address")
			txtEmail.focus()
			return false
		}
		for (var i=0;i<intLen;i++)
		{
			if (txtEmail.value.charAt(i)=="@")
			{
				blnFlag=blnFlag+1
			}
		}
		if (blnFlag>=0 && blnFlag<1 || blnFlag>1)
		{
			alert("Invalid E-Mail Address")
			txtEmail.focus()
			return false
		}
		strSplit=(txtEmail.value).split("@")
		intSptLen=strSplit[1].length
		var intCnt=0
		for(var j=0;j<intSptLen;j++)
		{
			if (strSplit[1].charAt(j)==".")
			{
				intCnt=intCnt+1
			}
		}
		if (intCnt<=0)
		{
			alert("Invalid E-Mail Address")
			txtEmail.focus()
			return false
		}
		return true
	}
}
function fnLogin(){

  if(document.frmLogin.email.value==""){
       alert("Please Enter Email");
	   document.frmLogin.email.focus();
	   return false;
  }
  if(!fnEmail(document.frmLogin.email)){
	return false;
  }
  if(document.frmLogin.psw.value==""){
       alert("Please Enter Password");
	   document.frmLogin.psw.focus();
	   return false;
  }
  email = document.frmLogin.email.value;
  pass  = document.frmLogin.psw.value;
  index  = document.frmLogin.index.value;


  fnValidateLoginAjax(email,pass,index);
 // document.frmLogin.action ="chklogin.php";
  //document.frmLogin.submit();
}
var xmlHttp
function fnValidateLoginAjax(email,pass,index)
{ 
	xmlHttp=RemoteRequestObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 } 
	if(email!=""){
		var url="chklogin.php"
		
		url=url+"?email="+email+"&psw="+pass+"&index="+index+"&randid="+Math.random()
		xmlHttp.onreadystatechange=stateChangedLoin
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
}

function stateChangedLoin() 
{ 
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
			
		document.getElementById("somediv").style.display="";
		document.getElementById("somediv").innerHTML="";
		
		var val1 = xmlHttp.responseText;
		var arr1 = val1.split("|");
				
		if(arr1[0]== 'wrong') {
			
			document.getElementById("login_p").innerHTML="";
			document.getElementById("login_p").innerHTML=arr1[1]
		
		} else if(arr1[0]== 'ok') {
			            	  
			/*jQuery(function() {

				$.post("photogallery/misc.php",{authuser:$('#username').val(),password:$('#psw').val(),action:"login",sessid:Math.random()},function(data) {

					window.location.href = 'mypulse.php';
				});
			});*/
			
			window.location.href = 'profilemanagement.php';
		}
		
	} 
}


