var xmlHttp=createXmlHttpRequestObject();
var serverAddress="login_chk.php";
var showErrors=true;
var cache=new Array();
if (window.ActiveXObject) ie = window[window.XMLHttpRequest ? 'ie7' : 'ie6'] = true;
//XMLHttpRequest
function createXmlHttpRequestObject(){
	var xmlHttp;
	try{
		xmlHttp=new XMLHttpRequest();
	}catch(e){
		var XmlHttpVersions= new Array("MSXML2.XMLHTTP.6.0",
									   "MSXML2.XMLHTTP.5.0",
									   "MSXML2.XMLHTTP.4.0",
									   "MSXML2.XMLHTTP.3.0",
									   "MSXML2.XMLHTTP",
									   "Microsoft.XMLHTTP");
		for (var i=0; i< XmlHttpVesions.length && !xmlHttp; i++){
			try{
				xmlHttp=new ActiveXObject(XmlHttpVersions[i]);
			}catch(e){}
		}
	}
	if(!xmlHttp){
		alert("XMLHttpRequest");	
	}else{
		return xmlHttp;	
	}
}

function displayError($msg){
	if(showErrors){
		showErrors=false;
		alert(":\n" + $msg);
		//setTimeOut("validate();", 10000);
	}
}
function chkEnter(event){
     if((event.keyCode == 0xA)||(event.keyCode == 0xD)){
          checkAll();
	 }
}
function checkAll(){
	var params = new Array();
	var inputs=document.getElementsByTagName('input');
	for( var i = 0; i < inputs.length; i++ ){
		var teg  = inputs.item(i);
		var fieldID = teg.getAttribute('id').toString();
		var fValue = teg.value;//.getAttribute('value').toString();
		var fieldNameVal = encodeURIComponent(fieldID)+'='+
                                      encodeURIComponent(fValue);
		params[i] = fieldNameVal;
	}
	var param = params.join('&');
	//alert(param);
	if(xmlHttp){
		var div=document.getElementById("but");
		div.className='detail_loading';
		try{
			if(xmlHttp.readyState==4 || xmlHttp.readyState==0 ){
				xmlHttp.open("POST", serverAddress, true);
				xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlHttp.onreadystatechange=handleRequestStateChange;
				xmlHttp.send(param +"&validationType=php"+"&ie="+ie);
				}
			}
		catch(e){
			displayError(e.toString());	
			div.className='detail';
			}
		}
		
}

function validate(inputValue, fieldID){
	if(inputValue){
		if(xmlHttp){
			if(fieldID){
				inputValue=encodeURIComponent(inputValue);
				fieldID=encodeURIComponent(fieldID);
				cache.push("inputValue=" + inputValue + "&fieldID=" + fieldID);
			}
			//    
			try{
				if((xmlHttp.readyState==4 || xmlHttp.readyState==0 ) && cache.length > 0){
					var cacheEntry= cache.shift();
					xmlHttp.open("POST", serverAddress, true);
					xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					xmlHttp.onreadystatechange=handleRequestStateChange;
					xmlHttp.send(cacheEntry+"&ie="+ie);
				}
			}
			catch(e){
				displayError(e.toString());	
			}
		}
	}
}

function handleRequestStateChange(){
	if(xmlHttp.readyState==4){
		if(xmlHttp.status==200){
			try{
				readResponse();	
			}catch(e){
				displayError(e.toString());	
			}
			
		}else{
			displayError(xmlHttp.statusText);	
		}
	}
}

function readResponse(){
	var response = xmlHttp.responseText;
	if(response.indexOf("ERRNO") >=0 || response.indexOf("error:") >=0 || response.length==0){
		throw( response.length ==0? "Server error." : response);
	}
	responseXml=xmlHttp.responseXML;
	xmlDoc=responseXml.documentElement;
	if(xmlDoc.getElementsByTagName('redir').length!=0){
		window.location.href=xmlDoc.getElementsByTagName('redir')[0].firstChild.data; 
	}else{
		xmls=xmlDoc.getElementsByTagName('r');
		for( var i = 0; i < xmls.length; i++ ){
			var nli = xmls.item(i);
			var fieldID = nli.getAttribute('fieldID').toString();
			var result = nli.getAttribute('result').toString();
			var msg = nli.getAttribute('msg').toString();
			try{
				if(result=='0'){
					//setFocusById(fieldID);
					showTip(fieldID, msg);
				}else{
					shadowTip(fieldID);
				}					
			}
			catch(e){
				//displayError(e.toString());	
			}
		}
		// validate()
		setTimeout("validate();", 500);
	}
	var div=document.getElementById("but");
	div.className='detail';
}

function setFocusById($id){
	document.getElementById($id).focus();	
}
