var http = false;
if(navigator.appName == "Microsoft Internet Explorer") {
  http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
  http = new XMLHttpRequest();
}
function loadComment()
{
	http.open('GET','/view-comment.asp?id='+Math.random());
	    http.onreadystatechange = function() {
	      if(http.readyState == 4)      
	      document.getElementById('viewcomment').innerHTML = http.responseText;
	};
    http.send(null);
	document.getElementById('comments').style.top = document.getElementById('imgdp').offsetTop -523 + "px";
    document.getElementById('comments').style.left = document.getElementById('imgdp').offsetLeft - 462 + "px";
}
function showComments()
{
	document.getElementById('comments').style.top = document.getElementById('imgdp').offsetTop -523 + "px";
    document.getElementById('comments').style.left = document.getElementById('imgdp').offsetLeft - 462 + "px";
	document.getElementById('comments').style.display="";
}
function closeComments()
{
	document.getElementById('comments').style.display="none";
}
function doValidate()
{
	tbname=document.frm.tbName.value;
	tbEmail=document.frm.tbEmail.value;
	comm=document.frm.tbComment.value;
	if (tbname=="")
	{
	alert("Enter your name")
	document.frm.tbName.focus();   
	return false;
	}  
 
	if(tbEmail== "" )
	{
		alert("Please Enter Your Email");
		document.frm.tbEmail.focus();		
		return false;
	}
	else
	{
		elen= tbEmail.length;
		efirstchar = tbEmail.substring(0, 1);
		elastchar  = tbEmail.substring(elen-1, elen);

		atPos = tbEmail.indexOf("@",1)	
		if (atPos == -1) 
		{
			alert("Enter valid EmailId");
			document.frm.tbEmail.focus();
			document.frm.tbEmail.select();
			return 0;
		}
		if (tbEmail.indexOf("@",atPos+1) != -1) 
		{
			alert("Enter valid EmailId");
			document.frm.tbEmail.focus();
			document.frm.tbEmail.select(); 
			return 0;
		}
		periodPos = tbEmail.indexOf(".",atPos)
		if (periodPos == -1) 
		{	
			alert("Enter valid EmailId");
			document.frm.tbEmail.focus();
			document.frm.tbEmail.select();
			return 0;
		}
		if (periodPos+3 > elen)	
		{
			alert("Enter valid EmailId");
			document.frm.tbEmail.focus();
			document.frm.tbEmail.select();
			return 0;
		}
		if (tbEmail.indexOf("..",1) != -1)
		{
			alert("Enter valid EmailId");
			document.frm.tbEmail.focus();
			document.frm.tbEmail.select();
			return 0;
		}
		if (tbEmail.indexOf("@.",1) != -1) 
		{
			alert("Enter valid EmailId");
			document.frm.tbEmail.focus();
			document.frm.tbEmail.select();
			return 0;
		}
		if (tbEmail.indexOf(".@",1) != -1)
		{
			alert("Enter valid EmailId");
			document.frm.tbEmail.focus();
			document.frm.tbEmail.select();
			return 0;
		}
		if (tbEmail.indexOf("-@",1) != -1)
	 	{
		 	alert("Enter valid EmailId");
			document.frm.tbEmail.focus();
			document.frm.tbEmail.select();
			return 0;
		}
		if (tbEmail.indexOf("@-",1) != -1)
	 	{
		 	alert("Enter valid EmailId");
			document.frm.tbEmail.focus();
			document.frm.tbEmail.select();
			return 0;
		}
		if (tbEmail.indexOf("-.",1) != -1)
	 	{
		 	alert("Enter valid EmailId");
			document.frm.tbEmail.focus();
			document.frm.tbEmail.select();
			return 0;
		}
		if (tbEmail.indexOf(".-",1) != -1)
	 	{
		 	alert("Enter valid EmailId");
			document.frm.tbEmail.focus();
			document.frm.tbEmail.select();
			return 0;
		}
		if (tbEmail.indexOf("--",1) != -1)
		{
		 	alert("Enter valid EmailId");
			document.frm.tbEmail.focus();
			document.frm.tbEmail.select();
			return 0;
		}        
		if((efirstchar<"a" || efirstchar>"z") && (efirstchar<"A" || efirstchar>"Z"))
		{
			alert("Enter valid EmailId");
			document.frm.tbEmail.focus();
			document.frm.tbEmail.select();
			return 0;
		}

		if((elastchar<"a" || elastchar>"z") && (elastchar<"A" || elastchar>"Z"))
		{
			alert("Enter valid EmailId");
			document.frm.tbEmail.focus();
			document.frm.tbEmail.select();	
			return 0;
		}

    	for (var i = 1; i < elen; i++)
		{
			var ch = tbEmail.substring(i, i + 1);
			if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '_') && (ch != "@") && (ch != ".") && (ch !="-"))
			{	
				alert("Enter valid EmailId");
				document.frm.tbEmail.focus();
				document.frm.tbEmail.select();
				return 0;
			}
		}
	}	

	if (comm=="")
	{
	alert("Enter your comment")
	document.frm.tbComment.focus();    
	return false;
	} 
if (comm.length>1000)
	{
	alert("Enter your comment with in 1000 characters")
	document.frm.tbComment.focus();    
	return false;
	} 
	var params="id="+Math.random() + "&tbname="+ escape(encodeURI(tbname)) +"&tbemail=" + escape(encodeURI(tbEmail)) + "&tbcomment=" + escape(encodeURI(comm));
	http.open('POST','/comments-handler.asp',true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	    http.onreadystatechange = function() {
	      if(http.readyState == 4)      
	      alert(http.responseText);
		  document.frm.reset();
		  document.getElementById('viewcomment').style.display='';
		  document.getElementById('postcomment').style.display='none';
	};
    http.send(params);
  
}
window.onload=loadComment;
