$(document).ready(function(){
	var in_pass = document.getElementById('inner_pass');
	if (in_pass!=null) document.getElementById('inner_pass').onkeypress = s_keyPress;
	var in_login = document.getElementById('inner_login');
	if (in_login!=null) document.getElementById('inner_login').onkeypress = s_keyPress;
	var f = document.avtorizform;
	$('#subm').click(function(){
		inner_click();
	});
	$('#but_exit').click(function(){
		$.get("/exit.php",'exit: 1',function(data){
			window.document.location.href ="/";
		});
	});
});

function inner_click(){
	var f = document.avtorizform;
	$.get("/check.php",{l:f.login.value, p:f.pass.value},function(data){
/*	    alert(data);*/
/*	    alert(data[data.length-1]);*/
/*		if (data[data.length-1]=='1'){*/
		if (data=='1'){
/*		    alert(f.action);*/
			f.submit();
		} else {
			alert('Данные неверны');
			f.login.value = '';
			f.pass.value = '';
		}
	});
}

function s_keyPress(evt){
	evt = (evt) ? evt : (window.event) ? window.event : "";
	if(evt.keyCode==13) inner_click();
}

function subsFormCheck(){
	var alertMsg = "Пожалуйста заполните следующие поля:\n";
	var f = document.subscribe_form;
	var l_Msg = alertMsg.length;
	/*if (spaces(f.name.value))
		alertMsg += " - Имя \n";
	if (spaces(f.company.value))
		alertMsg += " - Организация \n";
	if (spaces(f.dolg.value))
		alertMsg += " - Должность \n";*/
	if (spaces(f.phone.value))
		alertMsg += " - Телефон \n";
	if (!f.email.value.match(/^[a-z0-9._%-]+@[a-z0-9._%-]+.[a-z]{2,4}$/))
        alertMsg += " - E-mail \n";
	if (alertMsg.length == l_Msg) {
		$.get("/form_review.php",{isSubscr:f.subscribeform.value,n:f.name.value, c:f.company.value, d:f.dolg.value, t:f.phone.value,  e:f.email.value},function(data){
			if (data.charAt(0)=='1'){
				alert('Ваши данные добавлены');
/*				f.name.value = '';
				f.company.value = '';
				f.dolg.value = '';
				f.phone.value = '';
				f.email.value = '';*/
				f.reset();
			} else {
				//alert('Данные неверны');
				//f.login.value = '';
				//f.pass.value = '';
			}
		});
	} else{
		alert(alertMsg);
		return false;
	}
}

function spaces(str)
{
	var patt = eval("/[\\s]{"+str.length+"}/");
	return !str.search(patt);
}

function subsPresentFormCheck(){
	var alertMsg = "Пожалуйста заполните следующие поля:\n";
	var f = document.present_form;
	var l_Msg = alertMsg.length;

	if (spaces(f.name.value))
		alertMsg += " - Имя \n";
	if (spaces(f.job.value))
		alertMsg += " - Должность \n";
	if (spaces(f.phone.value))
		alertMsg += " - Телефон \n";
	if (spaces(f.company.value))
		alertMsg += " - Организация \n";
	if (spaces(f.city.value))
		alertMsg += " - Город \n";
	if (spaces(f.address.value))
		alertMsg += " - Адрес \n";


	var theme = "";
	if ((f.check1.checked) || (f.check2.checked) || (f.check3.checked) || (f.check4.checked) || (f.check5.checked) || (f.check6.checked)) {
	    var theme = '\n';
	    if (f.check1.checked) {theme += 'Эксперт (РБ)\n'};
	    if (f.check2.checked) {theme += 'Кодекс (РФ)\n'};
	    if (f.check5.checked) {theme += 'Управление персоналом\n'};

	    if (f.check4.checked) {theme += 'Техэксперт (РФ)\n'};
	    if (f.check6.checked) {theme += 'Экономика на ладони\n'};
	    if (f.check3.checked) {theme += 'Нормативные Акты Украины\n'};
	}
	else
		alertMsg += "\nВыберите тему презентации.\n";

    if (alertMsg.length == l_Msg) {
		$.get("/form_review.php",{isPresent:f.presentform.value, n:f.name.value, c:f.company.value, d:f.city.value, t:f.phone.value,  e:f.address.value, j:f.job.value,z:f.note.value,theme: theme},function(data){

			if ((data[data.length-1]=='1') || (data=='1')){
				alert('Форма успешно отправлена. Мы свяжемся с Вами в ближайшее время.');

				f.name.value = '';
				f.job.value = '';
				f.company.value = '';
				f.city.value = '';
				f.phone.value = '';
				f.address.value = '';
				f.note.value = '';
/*				f.email.value = '';*/
				f.submit();



			} else {
				//alert('Данные неверны');
				//f.login.value = '';
				//f.pass.value = '';
			}
		});
	} else{
		alert(alertMsg);



		return false;
	}
}
function subsLetterFormCheck(){
    var alertMsg = "Пожалуйста заполните следующие поля:\n";
	var f = document.letter_form;
	var l_Msg = alertMsg.length;

	if (spaces(f.name.value))
		alertMsg += " - Имя \n";
	if (spaces(f.phone.value))
		alertMsg += " - Телефон \n";
   if (!f.email.value.match(/^[a-z0-9._%-]+@[a-z0-9._%-]+.[a-z]{2,4}$/))
        alertMsg += " - E-mail \n";
	if (spaces(f.message.value))
		alertMsg += " - Сообщение \n";

    if (alertMsg.length == l_Msg) {
		$.get("/subscrletter.php",{n:f.name.value, t:f.phone.value,  e:f.email.value, z:f.message.value}, function(data){
			if (data[data.length-1]=='1'){
				alert('Форма успешно отправлена. Мы свяжемся с Вами в ближайшее время.');
				f.name.value = '';
				f.phone.value = '';
				f.email.value = '';
				f.message.value = '';
/*				f.email.value = '';*/
				//f.submit();
			} else {
				//alert('Данные неверны');
				//f.login.value = '';
				//f.pass.value = '';
			}
		});
	} else{
		alert(alertMsg);
		return false;
	}
}

function setSel(filename){	
	$.post('/addsel.php', {f:filename}, function(data){ });
}

