$(document).ready(function() {
	// validate signup form on keyup and submit
	var validator = $("#signupform").validate({

		rules: {

			username: {
				required: true,
				minlength: 6,
				remote: "ajax.pl?uprav=34"
			},
			password: {
				required: true,
				minlength: 6
			},
			password_confirm: {
				required: true,
				minlength: 6,
				equalTo: "#password"
			},
			email: {

				required: true,
				email: true,
				remote: "ajax.pl?uprav=33"

		},
			tipp: "required",
			terms: "required"
		},
		messages: {
		   username: {
				required: "Введите логин",
				minlength: jQuery.format("не менне 6 символов"),
				remote: jQuery.format("введиде другой (англ)")
			},
			password: {
				required: "Введите пароль",
				minlength: jQuery.format("не менне 6 символов")
			},
			password_confirm: {
				required: "Повторите пароль",
				minlength: jQuery.format("не менне 6 символов"),
				equalTo: "Пароли не совпадают"
			},
			email: {
				required: "Введите email",
				remote: jQuery.format("введите другой email")
			},
			tipp: "Ничего не выбрано",
			terms: " "
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {

			if ( element.is(":radio") )
				error.appendTo( element.parent().next().next() );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.next() );
			else
				error.appendTo( element.parent().next() );
		},
		// specifying a submitHandler prevents the default submit, good for the demo
		submitHandler: function() {

		var v = getRadioGroupValue(document.signupform.tipp);


		var url = "ajax.pl?uprav=" + 1
 + "&p[1]=" +  encodeURIComponent(document.getElementById("username").value)
  + "&p[2]=" +  encodeURIComponent(document.getElementById("password").value)
   + "&p[3]=" +  encodeURIComponent(document.getElementById("password_confirm").value)
    + "&p[4]=" +  encodeURIComponent(document.getElementById("email").value)
     + "&p[5]=" +  encodeURIComponent(document.getElementById("p5").value)
      + "&nom=2"
   + "&kl=" + v
 + "&rnd="+Math.random();
  xmlHttp.open("GET", url, true);
  xmlHttp.onreadystatechange = function() {divanketa();};
  xmlHttp.send(null);

		// alert(url);
		//	anketa();



  		disablePopup();

		$("#ppp").animate({opacity: "1"}, 1000)


        $("#bx").animate({opacity: "1"}, 1000)
	    $("#popupContact").animate({opacity: "0"}, 1000)
	    $("#backgroundPopup").animate({opacity: "0"}, 1000)

		//centering with css
		centerPopup();
		//load popup
		loadPopup();

		return false;

		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
	});

	// propose username by combining first- and lastname


});



function getRadioGroupValue(radioGroupObj)
{
  for (var i=0; i < radioGroupObj.length; i++)
    if (radioGroupObj[i].checked) return radioGroupObj[i].value;

  return null;
}


function divanketa()  {
  if(xmlHttp.readyState == 3 ) {}
        else {if(xmlHttp.readyState == 4) {


     //  alert(xmlHttp.responseText.length);

    document.getElementById("spanpopupContact1").innerHTML = xmlHttp.responseText;
	document.getElementById("spanreg").innerHTML = '';

if (xmlHttp.responseText.length <= '200'){
     document.getElementById("spansubmitpopup").innerHTML = '';}else{
	 document.getElementById("spansubmitpopup").innerHTML = '<br><br><center>ОК</center>';  }


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

  }
