// JavaScript Document
/*if (navigator.cookieEnabled == 0) {
  alert("You need to enable cookies for this site to load properly!");
}
else
{
	alert("Cookie Enabled");
}*/

// Checking browser compatabile
function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp = new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}
// State Changed for email
function stateChangedEmail() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		if(xmlHttp.responseText == 0)
		{
			document.getElementById("mess").innerHTML = "<font color='red'><b>Username doesn't exists!</b></font>";
			document.login.email.value = "";
			document.login.email.focus();
		}
		else
		{
			document.getElementById("mess").innerHTML = "";
			return true;
		}

	}
	else{
		document.getElementById("mess").innerHTML = "<img src='http://static.indianexpress.com/frontend/loginv2/images/search_process.gif' />";
	} 

}



function signupValidate()
{
	var memberid = document.login.email.value;
	var memberpass = document.login.password.value;
	
	if( memberid == "" || memberpass == ""){
		document.getElementById("mess").innerHTML = "<font color='red'><b>Username or Password is blank.</b></font>";
		return false;
	}
	else 
	 {
		document.getElementById("mess").innerHTML = "&nbsp;";
		return true;
	 }
}
// to validate user email
function validateEmail(emailId){
	xmlHttp = GetXmlHttpObject();
	
	if(xmlHttp == null) { 
		alert("Browser does not support HTTP Request");
		return;
	}
	
	var url = "validateEmail.php"
	url = url+"?username="+emailId
	xmlHttp.onreadystatechange = stateChangedEmail
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)	
}
// to validate email in signup
function valEmail(emailid)
	{
	//alert(emailid);
	if(emailid=="")
		{
			document.getElementById("txtEmail").innerHTML = "<font color='red'><b>Email cann't be blank!</b></font>";
		}
	else
		{
					xmlHttp = GetXmlHttpObject();
				
				if(xmlHttp == null) { 
					alert("Browser does not support HTTP Request");
					return;
				}
				//alert(emailid);
				var url = "validateSignupEmail.php"
				url = url+"?emailid="+emailid
				xmlHttp.onreadystatechange = stateChangedsignUpEmail
				xmlHttp.open("GET",url,true)
				xmlHttp.send(null)
		}
	}

// to validate username for sign up
function validateUser(userid){
	
	if(userid=="")
	{
		document.getElementById("txtMess").innerHTML = "<font color='red'><b>Username cann't be blank!</b></font>";
	}
	else
	{
		xmlHttp = GetXmlHttpObject();
		
		if(xmlHttp == null) { 
			alert("Browser does not support HTTP Request");
			return;
		}
		
		var url = "validateEmail.php"
		url = url+"?username="+userid
		xmlHttp.onreadystatechange = stateChanged
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
}

// State Changed  username signup
function stateChanged() 
{ 
	
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		//alert(xmlHttp.responseText);
		if(xmlHttp.responseText == 1)
		{
			document.getElementById("txtMess").innerHTML = "<font color='red'><b>Username already exists!</b></font>";
			document.frmReg.username.value = "";
			
		}
		else
		{
			document.getElementById("txtMess").innerHTML = "";
			return true;
		}

	}
	else{
		document.getElementById("txtMess").innerHTML = "<img src='http://static.indianexpress.com/frontend/loginv2/images/search_process.gif' />";
	} 

}

// State Changed  username signup email
function stateChangedsignUpEmail() 
{ 
	
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		//alert(xmlHttp.responseText);
		if(xmlHttp.responseText == 1)
		{
			document.getElementById("txtEmail").innerHTML = "<font color='red'><b>Email already exists!</b></font>";
			document.frmReg.email.value = "";
		}
		else
		{
			document.getElementById("txtEmail").innerHTML = "";
			return true;
		}

	}
	else{
		document.getElementById("txtEmail").innerHTML = "<img src='http://static.indianexpress.com/frontend/loginv2/images/search_process.gif' />";
	} 

}
function forgotValidate()
{
	var username = document.frmForgot.username.value;
	var email = document.frmForgot.email.value;
	if( username == "" && email == ""){
		document.getElementById("mess").innerHTML = "<font color='red'><b>Username or Email is must.</b></font>";
		return false;
	}
	else 
	 {
		document.getElementById("mess").innerHTML = "";
		return true;
	 }
	
}
function formValidate()
{
		
	if( document.frmReg.username.value == ""){
		document.getElementById("txtMess").innerHTML = "<img height='14' width='14' align='absmiddle'  src='http://static.indianexpress.com/frontend/loginv2/images/alert.gif'/>&nbsp;Username cannot be blank.";
		document.frmReg.username.focus();
		return false;
	}
	else
	{
		document.getElementById("txtMess").innerHTML = "";
	}
	if( document.frmReg.password1.value == ""){
		document.getElementById("txtPasswd").innerHTML = "<img height='14' width='14' align='absmiddle'  src='http://static.indianexpress.com/frontend/loginv2/images/alert.gif'/>&nbsp;Password cannot be blank.";
		return false;
	}
	else
	{
		document.getElementById("txtPasswd").innerHTML = "";
	}
	if (document.frmReg.password1.value.length < 6 || document.frmReg.password1.value.length > 14)
	{
		document.getElementById("txtPasswd").innerHTML = "<img height='14' width='14' align='absmiddle'  src='http://static.indianexpress.com/frontend/loginv2/images/alert.gif'/>&nbsp;Your password should be of minimum 6 and maximum 14 characters long.";
		return false;
	}
	else
	{
		document.getElementById("txtPasswd").innerHTML = "";
	}
	if( document.frmReg.password2.value == ""){
		document.getElementById("txtPasswd").innerHTML = "<img height='14' width='14' align='absmiddle'  src='http://static.indianexpress.com/frontend/loginv2/images/alert.gif'/>&nbsp;Re-enter the Password.";
		return false;
	}
	else
	{
		document.getElementById("txtPasswd").innerHTML = "";
	}
	if(document.frmReg.password1.value != document.frmReg.password2.value)
	{
		document.frmReg.password1.value = '';
		document.frmReg.password2.value = '';
		document.getElementById("txtPasswd").innerHTML = "<img height='14' width='14' align='absmiddle'  src='http://static.indianexpress.com/frontend/loginv2/images/alert.gif'/>&nbsp;Both password must be same.";
		return false;
	}
	else
	{
		document.getElementById("txtPasswd").innerHTML = "";
	}

// Email Validation
	var emailID=document.frmReg.email
	
	if ((emailID.value==null)||(emailID.value=="")){
		document.getElementById("txtEmail").innerHTML = "<img height='14' width='14' align='absmiddle'  src='http://static.indianexpress.com/frontend/loginv2/images/alert.gif'/>&nbsp;Email cannot be blank.";
		return false
	}
	else
	{
		if (echeck(emailID.value)==false){
		document.getElementById("txtEmail").innerHTML = "<img height='14' width='14' align='absmiddle'  src='http://static.indianexpress.com/frontend/loginv2/images/alert.gif'/>&nbsp;Email id is invalid.";
		return false
		}else
		{
			document.getElementById("txtEmail").innerHTML = "";
		}
	}
	if( document.frmReg.name.value == ""){
		document.getElementById("txtUser").innerHTML = "<img height='14' width='14' align='absmiddle'  src='http://static.indianexpress.com/frontend/loginv2/images/alert.gif'/>&nbsp;Please enter your Fullname.";
		return false;
	}
	else
	{
		document.getElementById("txtUser").innerHTML = "";
	}
	if( document.frmReg.mobile.value == ""){
		document.getElementById("txtMobile").innerHTML = "<img height='14' width='14' align='absmiddle'  src='http://static.indianexpress.com/frontend/loginv2/images/alert.gif'/>&nbsp;Please enter your Mobile Number.";
		return false;
	}
	else
	{
		document.getElementById("txtMobile").innerHTML = "";
	}
	if( document.frmReg.city.value == ""){
		document.getElementById("txtCity").innerHTML = "<img height='14' width='14' align='absmiddle'  src='http://static.indianexpress.com/frontend/loginv2/images/alert.gif'/>&nbsp;Please enter your City.";
		return false;
	}
	else
	{
		document.getElementById("txtCity").innerHTML = "";
	}
	if( document.frmReg.captcha_user.value == ""){
		document.getElementById("txtCaptcha").innerHTML = "<img height='14' width='14' align='absmiddle'  src='http://static.indianexpress.com/frontend/loginv2/images/alert.gif'/>&nbsp;Please enter the text.";
		return false;
	}
	else
	{
		document.getElementById("txtCaptcha").innerHTML = "";
	}
	if(document.frmReg.captcha_user.value == document.frmReg.captcha.value)
	{
		document.getElementById("txtCaptcha").innerHTML = "";
	}
	else
	{
		document.getElementById("txtCaptcha").innerHTML = "<img height='14' width='14' align='absmiddle'  src='http://static.indianexpress.com/frontend/loginv2/images/alert.gif'/>&nbsp;Please enter the correct vaue.";
		return false;
	}
	
}

function findPositionX(obj)
{
  var curleft = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  }
  else if (obj.x)
    curleft += obj.x;
  return curleft;
}
function findPositionY(obj)
{
  var curtop = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curtop += obj.offsetTop
      obj = obj.offsetParent;
    }
  }
  else if (obj.y)
    curtop += obj.y;
  return curtop;
}
function showToolTip_form(tooltip_id,field_id){
	//alert(tooltip_id);
  var field_object = document.getElementById(tooltip_id);
  var tool_object= document.getElementById(field_id);
  var obj_height = tool_object.offsetHeight;
  var obj_width = tool_object.offsetWidth;
  var leftPos = findPositionX(tool_object) + obj_width;
  var rightPos = findPositionY(tool_object) - (obj_height/2);
  if(leftPos<0) leftPos=0;
  field_object.style.left = leftPos + 'px';
  field_object.style.top = rightPos + 'px';
  field_object.style.display='block';
}
// function to chaeck email id
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		  // alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   // alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		  //  alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   // alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   // alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		   // alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
	
	
	