var xmlHttp;

function GetXmlHttpObject()

{

	var xmlHttp=null;

	try

  	{

		// Firefox, Opera 8.0+, Safari

		xmlHttp=new XMLHttpRequest();

  	}

	catch (e)

  	{

		// Internet Explorer

		try

    	{

    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

    	}

		catch (e)

		{

			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

		}

	}

	return xmlHttp;

}

function stateChanged_db(ctrlname) 

{ 

	

	if(xmlHttp.readyState==4)
	{ 

		//document.getElementById("namediv").innerHTML=xmlHttp.responseText;

		var errortxt=xmlHttp.responseText;

		errtxt=errortxt.replace(/^\s+|\s+$/g, '') //trim

		

		//get error code from response text 

		errcode=errtxt.substr(0,1);

		

		//to extract field name from response text 

		lastdashpos=errtxt.lastIndexOf("-");

		

		errordesc=errtxt.substring(2,lastdashpos);

		

		

		if(errcode==1)

		{

			alert(errordesc);

			

			document.getElementById(ctrlname).focus();

			document.getElementById(ctrlname).style.color='red';

			return false;

		}

		else

		{

			

			document.getElementById(ctrlname).style.color='black';

			return true;

		}

	}

}

function validateDB(tbl,fldname,ctrlname,fldval,phppage)

{ 

	

	if(phppage===undefined)

	{

		phppage="dbchk.php";		

	}

	

	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)

	{

		alert ("Your browser does not support AJAX!");

		return;

	} 

	

	var url=phppage;

	url=url+"?tbl="+tbl+"&fldname="+fldname+"&fldval="+fldval;

	url=url+"&sid="+Math.random();


	xmlHttp.open('GET',url,true);

	

	xmlHttp.onreadystatechange=function(){stateChanged_db(ctrlname);}

	

	xmlHttp.send(null);

}

