var kod;

function setBorder(field, minLength, isValid)
	{
	function makeOptional(arg, defVal)
		{
		return eval(arg + " = typeof " + arg + " == 'undefined' ? " + defVal + " : " + arg + ";");
		}
	makeOptional('minLength', 1);
	makeOptional('isValid', field.value.length >= minLength);
	field.style.border = isValid ? '1px solid gray' : '1px solid #0088cc';
	}

function setSubmitBtn(btnId)
	{
	btn = document.getElementById(btnId);
	var chk = !validateBtn(btn);
	btn.disabled = chk;
	}

function validateBtn(btn)
	{
	//var isValid = true;
	var isValid = false;
	var name = document.getElementById("name").value;
	var email = document.getElementById("email").value;
	var valMail = validateEmail(document.getElementById("email"));
	var code = document.getElementById("security_code").value.length;
	
//	alert('n:' + name + ', e:' + email + ', tel:' + tel + ', code:' + code + ', cb:' + cb + ', cb2:' + cb2 + ', cb3:' + cb3);
  if((name != "" && valMail && code == 5 && kod == 1)){ isValid = true; }
  
  return isValid;
	}
	
function validateBtnC(btn)
	{
	//var isValid = true;
	var isValid = false;
	var name = document.getElementById("name").value;
	var email = document.getElementById("email").value;
	var valMail = validateEmail(document.getElementById("email"));
	var code = document.getElementById("security_code").value.length;
	
//	alert('n:' + name + ', e:' + email + ', tel:' + tel + ', code:' + code + ', cb:' + cb + ', cb2:' + cb2 + ', cb3:' + cb3);
  if((name != "" && valMail && code == 5 && kod == 1)){ isValid = true; }
  
  return isValid;
	}


function validateEmail(field)
	{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
      return reg.test(field.value);
	}

function setSecFieldBorder(req, field)
	{
	if (req.readyState == 4)
		{ // Completed
		setBorder(field, 0, req.status == 200 && req.responseText == '1');
		kod = req.responseText;
		}	
	}

function validateSecCode(field, btnId, baseUrl)
	{
	function makeOptional(arg, defVal)
		{
		return eval(arg + " = typeof " + arg + " == 'undefined' ? '" + defVal + "' : " + arg + ";");
		}
	
	makeOptional('baseUrl', '');
	
	url = baseUrl + 'functions/validateSecCode.php?code=' + field.value;
	
	if (window.XMLHttpRequest)
		{ // != IE
		req = new XMLHttpRequest();
		req.onreadystatechange = function() {
			setSecFieldBorder(req, field);
			setSubmitBtn(btnId);
		}
		
		try { req.open("POST", url, true); } catch (ex) { alert(ex); };
		req.send(null);
		}
	else if (window.ActiveXObject)
		{ // == IE
		req = new ActiveXObject("Microsoft.XMLHTTP");
		req.onreadystatechange = function() {
		//setSecFieldBorder(req, handler, btnId);
		setSecFieldBorder(req, field); setSubmitBtn(btnId);
		}
		
		try { req.open("POST", url, true); } catch (ex) { alert(ex); };
		req.send();
		}
	}