var xmlHttp = createXmlHttpRequestObject();
var errors_to_submit =6;
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
                                    'MSXML2.XMLHTTP.5.0',
                                    'MSXML2.XMLHTTP.4.0',
                                    'MSXML2.XMLHTTP.3.0',
                                    'MSXML2.XMLHTTP',
                                    'Microsoft.XMLHTTP');
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}



function validform(val) {

  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // initiate reading a file from the server
	var data = new Object();
	data.type = val.id;
	data.value = val.value;

	senddata = JSON.stringify(data);
    xmlHttp.open("GET", "checkform.php?data="+senddata, true);
    xmlHttp.onreadystatechange = handleRequestStateChange;
    xmlHttp.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
      //alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleRequestStateChange() 
{

  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
	 // alert('br')
	response = xmlHttp.responseText;
	
	if(response != '' || response != 'null'){
		var returnData = JSON.parse(response);
		
		if (returnData.error == 'no'){
			var obj_img = document.getElementById(returnData.type+"_img")
			obj_img.innerHTML = "<img src='i/ok.png' style='width:16px;height:16px'>"
			errors_to_submit-=1;
			//alert(window.event.type)
		}
		else {
			var obj_img = document.getElementById(returnData.type+"_img")
			var errortext = document.getElementById('formerrors')
			
			obj_img.innerHTML = "<img src='i/no.png' class='error_image' style='width:16px;height:16px'  onmouseover=\"this.src='i/no2.png';Tip('"+returnData.error_text+"',BORDERSTYLE,'dashed',DELAY,1,ABOVE, true, OFFSETY, 15,  OFFSETX, -10, TITLE, 'Error:', TITLEBGCOLOR, 'red')\" onmouseout=\"this.src='i/no.png';UnTip()\">"	
			
			checkerrortext = document.getElementById("error_"+returnData.type)
			
			if(checkerrortext != null){
				checkerrortext.outerHTML = ''
			}
			
			//errortext.innerHTML += "<span id='error_"+returnData.type+"'style='display:block;color:red;font-weight:bold;font-size:13px'>" +returnData.error_text+"</span><br />"
		}
	}
	
      }
      catch(e)
      {
        // display error message
       // alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" +  xmlHttp.statusText);
    }
  }
}



function valid_pass(n){
var pass1=document.getElementById('pass');

if(n.value==pass1.value) {
div1=document.getElementById("ajax_div_pass");
div2=document.getElementById("ajax_div_repass");

div1.innerHTML="Ok"
div2.innerHTML="Ok"
}

else {

div1=document.getElementById("ajax_div_pass");
div2=document.getElementById("ajax_div_repass");

div1.innerHTML="Пароли не совпадют"
div2.innerHTML="Пароли не совпадют"
}


}

function valid_agree(n){
if(n.checked){

dis=document.getElementById("submit").disabled="none";
}
}