var err = "";
function getVal2(n){return document.getElementById(n).value;}
function getValEl(n){return document.getElementById(n).value;}

function alerterr(s){alert("-----------------------------------------------------------\nВаш запрос не может выполнится из-за ошибки(ок).\nПожалуйста утраните эту ошибку(ки)\n-----------------------------------------------------------\n" + s);}
function ErrorHttpRequest(){alert("-----------------------------------------------------------\nВаш запрос не может выполнится из-за ошибки(ок).\nСервер сообщает:\n-----------------------------------------------------------\n");}

function checkempty(n,t){if (getVal2(n).match(/^[ \s]*$/)){err+=cerr(t+" не заполнено.");}}
function cerr(s){return s+"\n"; }

function getVal(el, first){
	var result = (first)?"?":"&";
	result += el + "=" + getVal2(el);
	return result;
}

//Administrative Zone functions
function WinReload(){
	document.location.reload();	
}

function goto(str){
	document.location = str;	
}

function SendMsg(){
	err = "";
	checkempty("name", "Имя");
	checkempty("m_email", "E-mail");
	checkempty("msg", "Сообщение");
	if (err != ""){
		alerterr(err);
		return;
	}
	document.getElementById('sendederror').style.display = "none";
	document.getElementById('loadingmail').style.display = "";
	var arg = "cmd=SendMsg";
	arg += getVal("tofirm", 0);
	arg += getVal("name", 0);
	arg += getVal("phone", 0);
	arg += getVal("m_email", 0);
	arg += getVal("msg", 0);
	var url = "service.php";
	httpRequest("POST",url,true,handleSndMsg,arg);
}

function handleSndMsg(){
	if (request.readyState == 4) {
		if (request.status == 200) {
			document.getElementById('loadingmail').style.display = "none";
			var resp = request.responseText.split("|");
			alert(resp);
			if (resp[0] == 0){
				document.getElementById('form').innerHTML = '<p align="center">Ваше сообщение отправлено.</p>';
			}else{
				document.getElementById('sendederror').style.display = "";
			}
		} else {alert("A problem occured with communication between the XMLHttpRequest object and the server program.");}				
	}
}

function OpenPhoto(photo, w, h){	
	var NewWin = window.open("img/"+photo, "", "width="+w+", height="+h+",  top=50, left=150, dependent=yes");
}

function dropFile(btn){
 if(document.getElementById) {
 tr = btn;
  while (tr.tagName != 'TR') tr = tr.parentNode;
  tr.parentNode.removeChild(tr);
  checkForLast();
 }
}

function addFile(btn){
 if(document.getElementById)
 {
 tr = btn;
  while (tr.tagName != 'TR') tr = tr.parentNode;
  var idSuffix = Math.round(Math.random()*1000);
  var newTr = tr.parentNode.insertBefore(tr.cloneNode(true),tr.nextSibling);
  thisChilds = newTr.getElementsByTagName('td');
   for (var i = 0; i < thisChilds.length; i++)
   {
    if (thisChilds[i].className == 'header') thisChilds[i].innerHTML = '';
    if (thisChilds[i].className == 'files') thisChilds[i].innerHTML = '<input name="gallery[]" type="file" style="width:280px" >';
   }
  checkForLast();
 }
}

function checkForLast(){
btns = document.getElementsByName('drop');
for (i = 0; i < btns.length; i++){
btns[i].disabled = (btns.length == 1) ? true : false;
}
}

function showDiv(id)
{
	if ($(id).style.display == "") $(id).hide(); else $(id).show();
}

function checkRegister()
{
	err = "";	
	checkempty("reg_email", "E-mail");
	checkempty("company_name", "Название компании");
	checkempty("opis", "Описание");
	checkempty("reg_pass", "Пароль");
	checkempty("re_reg_pass", "Подтверждение пароля");
	checkempty("security", "Код безопасности");
	if (err != ""){
		alerterr(err);
		return;
	}
	document.getElementById('RegisterForm').submit();
}

function checkLogin()
{
	err = "";	
	checkempty("log_email", "E-mail");
	checkempty("log_pass", "Пароль");
	if (err != ""){
		alerterr(err);
		return;
	}
	document.getElementById('LoginForm').submit();
}

function checkEditInfo()
{
	err = "";	
	checkempty("company_name", "Название компании");
	checkempty("opis", "Описание компании");
	checkempty("security", "Код безопасности");
	if (err != ""){
		alerterr(err);
		return;
	}
	document.getElementById('EditInfoForm').submit();
}

function checkAddProgect()
{
	err = "";	
	//SpawPGcore.htmlModeClick(description_obj.getTargetEditor(),description_obj.getToolbarItem('description_mode_strip_1'), '');
	checkempty("title", "Название проекта");
	checkempty("price", "Цена");
	checkempty("description", "Описание");
	if (err != ""){
		alerterr(err);
		//SpawPGcore.designModeClick(description_obj.getTargetEditor(),description_obj.getToolbarItem('description_mode_strip_0'), '');
		return;
	}
	document.getElementById('AddProgectForm').submit();
}

function hitSearchEnterLogin(e) {
 if ((window.event && e.keyCode==13) || (e.which && e.which==13))
   checkLogin();   
 else
   return true;
}
