var err = "";
function getVal(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 ne(n){return getVal(n).match(/^[\S]/)};
function checkempty(n,t){if (getVal(n).match(/^[ \s]*$/)){err+=cerr(t+" не заполнено.");}}
function checkemail(n,t){if (ne(n)&&!getVal(n).match(/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/)){err+=cerr(t+" не верный.");}}
function checkpass (n,t){if (ne(n)&&!getVal(n).match(/^[a-zA-Z0-9]/)){err+=cerr(t+" латинскими символами!!!");}}
function checkmatch(n1,n2,t1,t2){if(ne(n1)&&ne(n2)&&getVal(n1)!=getVal(n2)){err+=cerr(t1+" и "+t2+" не совпадают.");}}
function cerr(s){return s+"\n"; }

function getVal2(el, first){
	var result = (first)?"?":"&";
	result += el + "=" + getVal(el);
	return result;
}

//Administrative Zone functions
function WinReload(){
	document.location.reload();	
}

function goto(str){
	document.location = str;	
}

function handleVoid(){
	if (request.readyState == 4) {
		if (request.status == 200) {					  
			if (request.responseText == "CartSended"){
				document.location = '/cart/sended/';	
			}else{
				//alert (request.responseText);
				WinReload();
			}
		} else {alert("A problem occured with communication between the XMLHttpRequest object and the server program.");}				
	}
}

function SendMsg(){
	err = "";
	checkempty("name", "Имя");
	checkempty("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 += getVal2("name", 0);
	arg += getVal2("phone", 0);
	arg += getVal2("email", 0);
	arg += getVal2("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("|");
			if (resp[0] == 0){
				$('form').innerHTML = '<p align="center">Ваше сообщение отправлено.</p><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 showText(cid)
{
	var NewWin = window.open("show_text.php?cid="+cid, "", "width=400, height=400,  top=50, left=150, scrollbars=yes, dependent=yes");
}

