// JavaScript Document
var xmlHttp = createXmlHttpRequestObject();
var name,address,cname,emailid,phno,comments;

function createXmlHttpRequestObject()
{
  var xmlHttp;
  if(window.ActiveXObject)
	{
		 try  { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
		 catch (e)	 {  xmlHttp = false; }
	}
	else
	{
		try {	xmlHttp = new XMLHttpRequest();	}
		catch (e)	{	xmlHttp = false;	}
	}
	if (!xmlHttp)	alert("Error creating the XMLHttpRequest object.");
	else	return xmlHttp;
}

function chkqueryentry()
{
	name=document.getElementById('inqname').value;	
	cname=document.getElementById('inqcname').value;
	address=document.getElementById('inqaddress').value
	emailid=document.getElementById('inqemail').value
	phno=document.getElementById('inqphno').value
	comments=document.getElementById('inqcomments').value

	if(name=="")  document.getElementById('errmsg').innerHTML='Please enter the name';
	else if(cname=="")  document.getElementById('errmsg').innerHTML='Please enter the Company/College name';	   
	else if(emailid=="") document.getElementById('errmsg').innerHTML='Please enter the email id';	   
	else if(phno=="")  document.getElementById('errmsg').innerHTML='Please enter the phone number';	   
	else if(comments=="")  document.getElementById('errmsg').innerHTML='Please enter the comments';	   
	else							 
		if(! isValidEmail(emailid))
		  document.getElementById('errmsg').innerHTML='Please check the email address';	   
		else
		if(! isValidPhone(phno))
			 document.getElementById('errmsg').innerHTML='Please check the phone';	   
 		else
		{
 			 document.getElementById('errmsg').innerHTML='';
			 document.inquiryform.submit();
		}


}

function isValidEmail(email)
{
	var pattern = /^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$/;
	return  pattern.test(email);
}
function isValidPhone(Value)
{
	if ( Value.length == 0 )		return true;
	var pattern = /^[0-9]*$/;
	var len=Value.length-1;
	var mValue=Value.substring(1,len)
	if(Value.charAt(0) != '+')
		return pattern.test(Value);
	else
		return pattern.test(mValue);
}
function submitinquiry()
{	
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{	
		var qsubject=document.getElementById('qsubject').value;			
		var info="qsubject="+qsubject+'&name='+name+'&cname='+cname+'&address='+address+'&emailid='+emailid+'&phno='+phno+'&comments='+comments;		

		xmlHttp.open("GET", "../contact/inquiryajax.php?" + info, true);
		xmlHttp.onreadystatechange = chklnameout;
		xmlHttp.send(null);
	}
	else
		setTimeout('chkloginname()',1000);
}

function chklnameout()
{
	if (xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			lnameexist= xmlHttp.responseXML.getElementsByTagName('userext')[0].childNodes[0].nodeValue;
			enqsubmit();
		}
		else
		{
			alert("There was a  problem accessing the server: " + xmlHttp.statusText);
		}
	}
}
