var xmlHttp = createXmlHttpRequestObject();
//ссылка на XMLHttpRequest
var serverAddress = "testmyform.php";
var showErrors = true;
var cache = new Array();


//создает объект XMLHttpRequest
function createXmlHttpRequestObject(){
	var xmlHttp;//хранит ссылку на XMLHttpRequest
	//для InternetExplorer
	//Для всех кроме IE6 и более старых
	try{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e){
		//для IE6 и более старых
		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<XmlHttpVersions.length && !xmlHttp; i++){
			try{
				xmlHttp = new ActiveXObject(XmlHttpVersions[i])
			}
			catch(e){
			}
		}
	}
	
	if(!xmlHttp)
		alert("Ошибка создания XMLHttpRequest");
	else
		return xmlHttp;
};

function displayErrors($message){
	if(showErrors){
		showErrors = flase;
		alert("Обнаружена ошибка: \n" + $message);
		setTimeout("validate();", 10000);
	}
};
/*function validate(inputValue, inputName){
	if(xmlHttp){
		if(inputName){
			inputValue = encodeURIComponent(inputValue);
			inputName = encodeURIComponent(inputName);
			cache.push("inputValue="+inputValue + "&inputName="+inputName);
		}
		try{
			if((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && cache.length){
				var cacheEntry = cache.shift();
				xmlHttp.open("POST", serverAddress, true);
				xmlHttp.setRequestHeader("Contenet-Type","application/x-www-form-urlencoded");
				xmlHttp.onreadystatechange = handleRequestStateChange;
				xmlHttp.send(cacheEntry);
			}
		}
		catch(e){
			displayError(e.toString());
		}
	}
}
/*function process(){
	//если объект не занят тогда работаем
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		//нолучение значения поля
		name = encodeURIComponent(document.getElementById("name").value);
		//обращение к MyTestForm.php
		xmlHttp.open("GET", "mytestform.php?name=" + name, true);
		//метод который будет обрабатывать ответ сервера
		xmlHttp.onreadystatechange = handleServerResponse;
		//посылает асинхронный запрос
		xmlHttp.send(null);
	}
	else{
		//если соеденение занято то через секунду повторяем
		setTimeout('process()', 1000);
	}
};*/

//вызывается после прибытия ответа сервера
/*function handleServerResponse(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			
			try{
				readResponse();
			}
			catch(e){
				displayError(xmlHttp.statusText);
			}
		}
	}
}

function readResponse(){
	var response = xmlHttp.resopnseText;
	if(response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0)
		trow(response.length == 0 ? "Server error:" : response);
	responseXml = xmlHttp.responseXML;
	xmlDoc = responseXml.documentElement;
	result = xmlDoc.getElementByTagName("result")[0].firstChild.data;
	inputName = xmlDoc.getElementByTagName("fieldid")[0].firstChild.data;
	message = document.getElementById(inputName + "Failed");
	message.className = (result == "0") ? "error" : "hidden";
	setTimeout("validate();", 500);
}

function setFocus(){
	document.getElementById("name").focus();
}
*/
function getdate(){
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		var serverAddr = "daterosigrish.php";
		xmlHttp.open("POST", serverAddr, true);
		xmlHttp.onreadystatechange = dateout;
		xmlHttp.send(null);
	}
	else{
		setTimeout('getdate()', 1000);
	}
};

function dateout(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			var out = xmlHttp.responseText;
			try{
				document.getElementById("date").innerHTML = xmlHttp.responseText;
			}
			catch(e){
				displayError(xmlHttp.statusText);
			}
		}
	}
};

		
/*			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			helloMessage = xmlDocumentElement.firstChild.data;
			document.getElementById("status").innerHTML = '<i>' + helloMessage + '</i>';
			setTimeout('process()', 1000);
		}
		else{
			alert("Error Server"+xmlHttp.statusText);
		}
	}
};

function Answer(){
	
}*/
//function test1(){
	
//};
