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 + "=" + $(el).value;
	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 openpg(pg, m, params){
	$('content').innerHTML = '<div id="loadingpage" class="loading" style="display:none"><img src="images/ajax-loader.gif" alt="Загрузка" border="0"></div>';
	for (var i = 1; i<6; i++) {
		if ($('dm' + i).getAttribute('style'))
        	$('dm' + i).style.cssText = '';    			
	}
	var styles = "background-image: url(images/dm_" + m + ".jpg);"
	if ($('dm' + m).getAttribute('style'))
        $('dm' + m).style.cssText = styles;
	else
		$('dm' + m).setAttribute('style', styles)
	
	$('loadingpage').style.display = "";
	var arg = "cmd=OpenPage&page=" + pg;
	arg += "&" + params;
	var url = "service.php";
	httpRequest("POST",url,true,handleUpdCont,arg);

}

function handleUpdCont(){
	if (request.readyState == 4) {
		if (request.status == 200) {		
			var resp = request.responseText.split("[!]");
			if (resp[1] != ""){	
				var add = "";
				if (resp[0] == "partners") add = '<div class="zag">Партнерам</div>';
				$('content').innerHTML = add + '<div id="loadingpage" class="loading" style="display:none"><img src="images/ajax-loader.gif" alt="Загрузка" border="0"></div>' + resp[1];
				$('loadingpage').style.display = "none";
			}
		} 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 += getVal("name", 0);
	arg += getVal("phone", 0);
	arg += getVal("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("|");
			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;
}
}

