var x1=false;
var x2=false;
var x3=false;
//var y=false;
function notAllAlphabets(str)
{
	for(var i=0;i<str.length;i++)
	{
		if((str.charCodeAt(i)<97 || str.charCodeAt(i)>122)
			&&(str.charCodeAt(i)<65 || str.charCodeAt(i)>90))
			return true;
	}
	return false;
}
var count1=0;
var count2=0;
var count3=0;

function checkThis()
{
	if(count1 !=0)
		validate1();
	if(count2!=0) 
		validate2();
	if(count3!=0)
		validate3();
}

function focusFunction1()
{
	checkThis();
	document.getElementById("nameError").innerHTML="";
	count1++;
}
function focusFunction2()
{
	checkThis();
	document.getElementById("emailError").innerHTML="";
	count2++;
}
function focusFunction3()
{
	checkThis();
	document.getElementById("passError").innerHTML="";
	count3++;

}

function validate1()
{
	var name=document.forms["myForm"]["text"].value;
	var nameLength=name.length;


	if(nameLength == 0)
	{
	 	document.getElementById("nameError").innerHTML="You can`t leave this empty";
	 	x1=false;
	 	return false;
	}
	else if(nameLength<6 || nameLength>50)
	{
		if(nameLength<6) 
			document.getElementById("nameError").innerHTML="Please enter at least 6 characters";
		else if(nameLength>50)
			document.getElementById("nameError").innerHTML="Your name exceeds the maximum allowed size";
		x1=false;
		return false;
	}
	else
	{
		x1=true;	
		document.getElementById("nameError").innerHTML="";
	}
}

function validate2()
{
	var email=document.forms["myForm"]["email"].value;
	var emailLength=email.length;
	var posAt=email.indexOf("@");
	var localPart=email.slice(0,posAt);
	var localPartLength=localPart.length;
	var domainPart=email.slice(posAt+1);
	var posDot=domainPart.indexOf(".");
	var s1=domainPart.slice(0,posDot);
	var s2=domainPart.slice(posDot+1);

	if(emailLength == 0)
	{
		document.getElementById("emailError").innerHTML="You can`t leave this empty";
		x2=false;
		return false;
	}
	else if(posAt==-1)
	{
		document.getElementById("emailError").innerHTML="Invalid email address";
		x2=false;
		return false;
	}
	else if(localPartLength<=2)
	{
		document.getElementById("emailError").innerHTML="Local part should be more than 2 characters";
		x2=false;
		return false;
	}
	for(var i=0;i<localPartLength; i++)
	{
		
		if((localPart.charCodeAt(i)<97 || localPart.charCodeAt(i)>122)
			&&(localPart.charCodeAt(i)<65 || localPart.charCodeAt(i)>90)
			&&(localPart.charCodeAt(i)<48 || localPart.charCodeAt(i)>57)
			&&(localPart.charCodeAt(i)!=46 && localPart.charCodeAt(i)!=95))
		{
			document.getElementById("emailError").innerHTML="Use only Alpha-Numeric characters";
			x2=false;
			return false;
		}
	}
	
	if(posDot==-1)
	{
		document.getElementById("emailError").innerHTML="Invalid email address";
		x2=false;
		return false;
	}
	

	if(s1.length<=2)
	{
		document.getElementById("emailError").innerHTML="Invalid email address";
		x2=false;
		return false;
	}
	if(notAllAlphabets(s1))
	{
		document.getElementById("emailError").innerHTML="Domain part should contain only alphabets or periods";
		x2=false;
		return false;
	}
	posDot=s2.indexOf(".")
	while(posDot!=-1)
	{
		s1=s2.slice(0,posDot);
		s2=s2.slice(posDot+1);
		posDot=s2.indexOf(".");
		if(s1.length<=1)
		{
			document.getElementById("emailError").innerHTML="Invalid email address";
			x2=false;
			return false;
		}
		if(notAllAlphabets(s1))
		{
			document.getElementById("emailError").innerHTML="Domain part should contain only alphabets or periods";
			x2=false;
			return false;
		}
	}
	if(s2.length<=1)
	{
		document.getElementById("emailError").innerHTML="Invalid email address";
		x2=false;
		return false;
	}
	if(notAllAlphabets(s2))
	{
		document.getElementById("emailError").innerHTML="Domain part should contain only alphabets or periods";
		x2=false;
		return false;
	}
	x2=true;
	document.getElementById("emailError").innerHTML="";
	
}

function validate3()
{
	var password=document.forms["myForm"]["pass"].value;
	var x=password.length;
	var posSpecialChar=[password.indexOf("$"), password.indexOf("!"), password.indexOf("@"), password.indexOf("_")];
	var b=false;
	for(var i=0;i<4;i++)
	{
		if(posSpecialChar[i]!=-1)
			b=true;
	}

	if(x == 0)
	{
		document.getElementById("passError").innerHTML="You can`t leave this empty";
		x3=false;
		return false;
	}
	else if(x<8 || x>20)
	{
		document.getElementById("passError").innerHTML="Password should be between 8-20 characters.";
		x3=false;
		return false;
	}
	else if(b==false)
	{
		document.getElementById("passError").innerHTML="Password should contain atleast one special character";
		x3=false;
		return false;
	}
	else 
	{
		x3=true;
		document.getElementById("passError").innerHTML="";
	}
}

function validate()
{
	validate1();
	validate2();
	validate3();
	if(x1==true && x2==true && x3==true)
	{
		check();
	}
}

 function check()
 {
 	var namecheck=document.getElementById("nameErr").value;
 	var passwordcheck=document.getElementById("passErr").value;
 	var namenew=namecheck.replace(/</g,"&lt;");
 	var namefinal=namenew.replace(/>/g,"&gt;");
 	var passwordnew=passwordcheck.replace(/</g,"&lt;");
 	var passwordfinal=passwordnew.replace(/>/g,"&gt;");
 	document.getElementById("nameErr").value=namefinal;
 	document.getElementById("passErr").value=passwordfinal;
 	document.getElementById('myForm').submit();
 }