	var oXmlHttp = null;
function checks()
{
var theForm=document.reg;
if(document.reg.name.value=="")	
	{
	alert('Придумайте логин для входа в систему.');
	return;
	}
	else
	{

	var sName = document.reg.name.value;
	if (!oXmlHttp) {
        oXmlHttp = zXmlHttp.createRequest();
    } else if (oXmlHttp.readyState != 0) {
        oXmlHttp.abort();
    }
    oXmlHttp.open("get", "reg_check.php?NAME="+sName, true);
	oXmlHttp.onreadystatechange = function () {
	        if (oXmlHttp.readyState == 4) {
            if (oXmlHttp.status == 200) {
			
	if (oXmlHttp.responseText=="OK\r\n" || oXmlHttp.responseText=="OK\n")
		{
		//document.reg.name.style="background-color: rgb(255, 235, 235); color: red;";
		alert('Такой логин уже сушествует выберите другой.');
		return;
		}			
		}
		}
		}
	

		oXmlHttp.send(null);	
	}
if(document.reg.parol.value==""){alert('Придумайте пароль для входа в систему.');return;}
if(document.reg.parol.value!=document.reg.parol1.value){alert('Пароль не совпадает с подтверждением.');return;}
if(document.reg.country.value==""){alert('Заполните поле "Страна".');return;}
if(document.reg.oblast.value=="-" && document.reg.country.value=="RUS@7"){alert('Заполните поле "Регион".');return;}
if(document.reg.town.value=="")	{alert('Заполните поле "Город".');return;}
if(document.reg.inn.value=="" && document.reg.country.value=="RUS@7")
	{
	alert('Заполните поле "ИНН компании или частного лица".');
	return;
	}
	else
	{
	if(document.reg.country.value=="RUS@7")
		{
		if (!check_inn(document.reg.inn.value))
			{
			return;
			}
		}
if(document.reg.fio.value=="")	{alert('Заполните поле "Контактное лицо (Ф.И.О)".');return;}	
if(document.reg.tel1.value=="")	{alert('Заполните поле "Телефон 1".');return;}		
if(document.reg.email.value=="")
	{
	alert('Заполните поле "E-mail".');	
	return;	
	}
	else
	{
	if (!(document.reg.email.value.indexOf("@") || document.reg.email.value.indexOf(".")))
		{
		alert('Укажите правильный "E-mail".');	
	    return;	
		}
	}
if(document.reg.uradress.value==""){alert('Заполните поле "Юридический адрес".');return;}	
if(document.reg.fakadress.value==""){alert('Заполните поле "Фактический адрес".');return;}	
if (theForm.chlen[0].checked){sChlen="Да"}else{sChlen="Нет"}
var x=window.confirm("Проверьте правильность введенной информации: \n\nИмя для входа:"+theForm.name.value+"\nПароль :"+theForm.parol.value+"\nСтрана:"+theForm.country.options[theForm.country.selectedIndex].text+"\nРегион:"+theForm.oblast.options[theForm.oblast.selectedIndex].text+"\nГород :"+theForm.town.value+"\nИНН :"+theForm.inn.value+"\nНаименование компании :"+theForm.firma.value+"\nЧленство в АСМАП :"+sChlen+"\nЮридический адрес :"+theForm.uradress.value+"\nФактический адрес (почтовый) :"+theForm.fakadress.value+"\nКонтактное лицо (Ф.И.О) :"+theForm.fio.value+"\nТелефон 1 :("+theForm.kod1.value+")"+theForm.tel1.value+"\nТелефон 2 :("+theForm.kod1.value+")"+theForm.tel2.value+"\nТелефон моб. :("+theForm.kod1.value+")"+theForm.tel3.value+"\nE-Mail:"+theForm.email.value+"\n\n\nSkype : "+theForm.skype.value+"\nICQ : "+theForm.icq.value+"\n\nОтправить заявку?")
if (x){document.reg.submit();}else{return;}
}
}

function valid_inn(sInn1)
{
		
		var factor1 = new Array(2,4,10,3,5,9,4,6,8);
		var factor2 = new Array(7,2,4,10,3,5,9,4,6,8);
		var factor3 = new Array(3,7,2,4,10,3,5,9,4,6,8);
		var ResultINN= 0;
		var allSpacesRe = /\s+/g;
		sInn=sInn1.replace(allSpacesRe,"");
        if ((sInn.length == 10) && (sInn != "0000000000"))
			{
			var sum = 0;
			for (i=0; i<=8; i++)
				{
				sum += sInn.substr(i, 1)*factor1[i];
				}
			sum %= 11;
			sum %= 10;
            if (sInn.substr(9, 1) == sum){ResultINN = 1;}
			}
			else
			{
			if ((sInn.length == 12) && (sInn != "000000000000") )
				{
				var sum = 0;
				for (i=0; i<=9; i++)
					{
					sum += sInn.substr(i, 1)*factor2[i];
					}
				sum %= 11;
				sum %= 10;
				var sum2 = 0;
				for (i=0; i<=10; i++)
					{
					sum2 += sInn.substr(i, 1)*factor3[i];
					}
				sum2 %= 11;
				sum2 %= 10;
				if((sInn.substr(10, 1) == sum) && (sInn.substr(11, 1) == sum2)){ResultINN= 1;}
				}
			}
		if(ResultINN == 0)
		{
		alert('ИНН введен неверно.');
		return false;
		}
		else
		{
		return true;
		}
}

function adress()
{
if(document.reg.uradress.value=="")
	{
	alert('Заполните поле "Юридический адрес".');
	document.reg.sovp.checked=false;
	return;
	}
	else
	{
	if(document.reg.sovp.checked)
		{
	    document.reg.fakadress.value=document.reg.uradress.value;
		document.reg.fakadress_h.value=document.reg.uradress.value;
		document.reg.fakadress.disabled=true;
		
		}
		else
		{
		document.reg.fakadress.disabled=false;
		}
	}
}

function country_sel()
{
var rD="@";
var sTel=document.reg.country.value;
var sPos=sTel.indexOf(rD)+1;
var sTel1="+"+sTel.substring(sPos);
	if(document.reg.country.value=="RUS@7")
	{
		document.reg.oblast.disabled=false;
	}
	else
	{
		document.reg.oblast.selectedIndex=0;
		document.reg.oblast.disabled=true;
		document.reg.oblast_h.value=document.reg.oblast.value;
	}
	document.reg.kod_h.value=sTel1;
	document.reg.kod1.value=sTel1;
	document.reg.kod2.value=sTel1;
	document.reg.kod3.value=sTel1;
	
}

function check_name() {
if(document.reg.name.value=="")	
	{
	alert('Придумайте логин для входа в систему.');
	return;
	}
	else
	{
	var sName = document.reg.name.value;
	if (!oXmlHttp) {
        oXmlHttp = zXmlHttp.createRequest();
    } else if (oXmlHttp.readyState != 0) {
        oXmlHttp.abort();
    }
    oXmlHttp.open("get", "reg_check.php?NAME="+sName, true);
	oXmlHttp.onreadystatechange = function () {
	        if (oXmlHttp.readyState == 4) {
            if (oXmlHttp.status == 200) {
			
	if (oXmlHttp.responseText=="OK\r\n" || oXmlHttp.responseText=="OK\n")
		{
		//document.reg.name.style="background-color: rgb(255, 235, 235); color: red;";
		alert('Такой логин уже сушествует выберите другой.');
		return false;
		}
		else
		{
		alert('Данный логин свободен. Можете продолжить регистрацию.');
		return true;
		}
		}
		}
		}
	

		oXmlHttp.send(null);	
	}
}	

function check_inn() {

var oXmlHttp = null;
var iCountry = document.reg.country;
var iOblast = document.reg.oblast;
if (!valid_inn(document.reg.inn.value))
	{return false;}else{
if(document.reg.inn.value=="")	
	{
	alert('Заполните поле "ИНН компании или частного лица.');
	return;
	}
	else
	{
	var sName = document.reg.inn.value;
	if (!oXmlHttp) {
        oXmlHttp = zXmlHttp.createRequest();
    } else if (oXmlHttp.readyState != 0) {
        oXmlHttp.abort();
    }
	
    oXmlHttp.open("get", "inn_check.php?NAME="+sName, true);
	oXmlHttp.onreadystatechange = function () {
	        if (oXmlHttp.readyState == 4) {
            if (oXmlHttp.status == 200) {
		var sInfo = unescape(oXmlHttp.responseText);
			parts=sInfo.split("|*|");
	
	if (parts[0]=="OK")
		{
		alert('На сайте АСМАП с таким ИНН зарегистрирован\n'+parts[7]+'\n'+parts[1]+'\n'+parts[2]+'\n\nМожете продолжить регистрацию');
	    document.reg.firma.value=parts[1];
		document.reg.firma.disabled=true;
		document.reg.firma_h.value=parts[1];
	    document.reg.uradress.value=parts[2];
		document.reg.uradress.disabled=true;
		document.reg.uradress_h.value=parts[2];
	    document.reg.fakadress.value=parts[8];
		document.reg.fakadress.disabled=true;
		document.reg.fakadress_h.value=parts[8];

	    if (parts[3] == "Y") {document.reg.chlen[0].checked=true;}else{document.reg.chlen[1].checked=true;}
		document.reg.chlen.disabled=true;
		document.reg.chlen_h.value=parts[3]; 
		
		var i = 0;
		while (iCountry.options[i].value.substr(0,3) != parts[4])
		{i++;}
		iCountry.selectedIndex=i;
		iCountry.disabled=true;
		document.reg.country_h.value=parts[4]; 
		
	    document.reg.town.value=parts[5];
		document.reg.town.disabled=true;
		document.reg.town_h.value=parts[5]; 
		
		var i = 0;
		while (iOblast.options[i].value != parts[6])
		{i++;}
		iOblast.selectedIndex=i;
		iOblast.disabled=true;
		document.reg.oblast_h.value=parts[6]; 
		document.reg.boss.value=0;
		return true;
		}
		else
		{
		document.reg.boss.value=1;
		alert('Можете продолжить регистрацию.');
		return true;
		}
		}
		}
		}
	

		oXmlHttp.send(null);	
	}
	}
	return true;
}	
function check_secure(){
      var l=document.reg.img_ssylka;
	  var si=document.reg.sid;
      if (l.value.length<6 || l.value.match(/[^0-9]/)){
            che8=0;
            document.getElementById('chf8').innerHTML='<img border="0" src="image/chu.gif" align="absmiddle" width="16" height="16"> <span class="copy">Неправильный код безопасности</span>';
            allowreg();
      }
      else {
            document.getElementById('chf8').innerHTML='<img border="0" src="image/i1.gif" align="absmiddle" width="16" height="16">';
           	  	if (!oXmlHttp) {
        oXmlHttp = zXmlHttp.createRequest();
    } else if (oXmlHttp.readyState != 0) {
        oXmlHttp.abort();
    }
    oXmlHttp.open("get", 'h_check.php?sid='+si.value+'&code='+l.value, true);
	oXmlHttp.onreadystatechange = function () {
	        if (oXmlHttp.readyState == 4) {
            if (oXmlHttp.status == 200) {
			
	if (oXmlHttp.responseText!="OK")
		{
            che8=0;
            document.getElementById('chf8').innerHTML='<img border="0" src="image/chu.gif" align="absmiddle" width="16" height="16"> <span class="red">Неправильно введен код безопасности </span>';
            allowreg();

		}
		else
		{
		    che8=1;
            document.getElementById('chf8').innerHTML='<img border="0" src="image/ch.gif" align="absmiddle" width="16" height="16"> ';
            allowreg();

		}
		}
		}
		}
	

		oXmlHttp.send(null);	
           
		 
      }
}
function allowreg(){
      if (che8==1){
            document.getElementById('SubReg2').disabled=false;
      }
      else {
            document.getElementById('SubReg2').disabled=true;
      }
}
