var agt = navigator.userAgent.toLowerCase ();
var is_major = parseInt (navigator.appVersion);
var is_minor = parseFloat (navigator.appVersion);
var is_nav  = ((agt.indexOf ('mozilla') != -1) && (agt.indexOf ('spoofer') == -1) && (agt.indexOf ('compatible') == -1) && (agt.indexOf ('opera') == -1) && (agt.indexOf ('webtv') == -1) && (agt.indexOf ('hotjava') == -1) && (agt.indexOf ('safari') == -1));
var is_nav6 = (is_nav && (is_major == 5) && (agt.indexOf ("netscape") != -1) && (agt.indexOf ("netscape/7") == -1));
var is_nav6up = ((is_nav && (is_major >= 5)) || (is_nav && (agt.indexOf ('netscape/7') != -1)));
var is_ie      = ((agt.indexOf ("msie") != -1) && (agt.indexOf ("opera") == -1) && (agt.indexOf ("safari") == -1));
var is_ie3     = (is_ie && (is_major < 4));
var is_ie4     = (is_ie && (is_major == 4) && (agt.indexOf ("msie 4")!=-1));
var is_ie4up   = (is_ie && (is_major >= 4));
var is_ie5     = (is_ie && (is_major == 4) && (agt.indexOf ("msie 5.0") != -1));
var is_ie5_5   = (is_ie && (is_major == 4) && (agt.indexOf ("msie 5.5") != -1));
var is_ie5up   = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up = (is_ie && !is_ie3 && !is_ie4 && !is_ie5)
var is_firefox = ((agt.indexOf ("firefox") != -1) && (agt.indexOf ("opera") == -1) && (agt.indexOf ("safari") == -1));
var is_opera = (agt.indexOf ("opera") != -1);
var is_nn = ((is_nav6 || is_nav6up) ? true : false);

var pat_text=/^\s*$/;
var pat_email=/^[a-zA-Z][a-zA-Z\_\.\-\d]*@[a-zA-Z\_\.\-\d]+.[a-zA-Z]{2,4}$/;
/*document.write('\
<div style="position: absolute; bottom: 50px; z-index: 100000">\
<a href="" onclick="showAlert(\'тут идет текст Алерта\'); return false">Alert</a><br />\
<a href="" onclick="showConfirm(\'тут идет текст Cofirma\', \'alert(1111)\'); return false">Confirm</a><br />\
<a href="" onclick="showPrompt(\'тут идет текст Prompt\', \'alert(promptText)\'); return false">Prompt</a>\
</div>\
');
*/

document.write('\
<div class="hidden" id="hidden">&nbsp;</div>\
<div class="global_confirm_bg" id="globalConfirmBg">&nbsp;</div>\
<div class="divalert" id="alert">\
	<div class="divalert_text" id="alerttext">&nbsp;</div>\
	<div class="divalert_button" id="alertbutton"><a href="" onclick="hideAlert(); return false"><img src="/webroot/delivery/pic/ba_ok.gif" width="37" height="18" alt="Ok" title="Ok" /></a></div>\
</div>\
<div class="divalert" id="confirm">\
	<div class="divalert_text" id="confirmtext">&nbsp;</div>\
	<div class="divalert_button"><a href="" onclick="doConfirm(); return false"><img src="/webroot/delivery/pic/ba_yes.gif" width="37" height="18" alt="Да" title="Да" /></a>&nbsp;&nbsp;<a href="" onclick="hideConfirm(); return false"><img src="/webroot/delivery/pic/ba_no.gif" width="37" height="18" alt="Нет" title="Нет" /></a></div>\
</div>\
<div class="divalert" id="restore" style="width: 220px">\
	<div class="divalert_text" id="restoretext" style="line-height: 12px">&nbsp;</div>\
	<div class="divalert_input">E-mail&nbsp;<input type="text" style="width: 163px" name="restoreinput" id="restoreinput" value="" /></div>\
	<div class="divalert_button"><a href="" onclick="doRestore(); return false"><img src="/webroot/delivery/pic/b_restore.gif" width="103" height="18" alt="Восстановить" title="Восстановить" /></a>&nbsp;<a href="#" onclick="hideRestore(); return false"><img src="/webroot/delivery/pic/b_close.gif" width="103" height="18" alt="Закрыть" title="Закрыть" /></a></div>\
</div>\
<div class="divalert" id="prompt">\
	<div class="divalert_text" id="prompttext">&nbsp;</div>\
	<div class="divalert_input"><input type="text"  name="promptinput" id="promptinput" value="" /></div>\
	<div class="divalert_button"><a href="" onclick="doPrompt(); return false"><img src="/webroot/delivery/pic/ba_yes.gif" width="37" height="18" alt="Да" title="Да" /></a>&nbsp;&nbsp;<a href="" onclick="hidePrompt(); return false"><img src="/webroot/delivery/pic/ba_no.gif" width="37" height="18" alt="Нет" title="Нет" /></a></div>\
</div>\
<div class="divalarm" id="alarm">\
	<div class="divalarm_text" id="alarmtext">Хана всему, все сломалось!&nbsp;</div>\
</div>\
');

function $(id){
	return document.getElementById(id);
}

function showAlert(text){
	if (is_ie5_5up || is_firefox){
		if ($("alert")) {
			showFullBg();
			if (is_ie) $("alerttext").innerText=text;
				else $("alerttext").innerHTML=text;
			$("alert").style.display="block";
			height2=$("alert").clientHeight/2;
			width2=$("alert").clientWidth/2;
			windowHeight2=($("hidden").offsetTop+10)/2;
			if (window.scrollY) scrollTop=window.scrollY;
				else scrollTop=document.documentElement.scrollTop/2;
			windowHeight2+=scrollTop;
			if (window.innerWidth) windowWidth2=window.innerWidth/2;
				else windowWidth2=window.document.body.offsetWidth/2;
			$("alert").style.top = (document.body.scrollTop+windowHeight2-height2)+"px";
			$("alert").style.left = (document.body.scrollLeft+windowWidth2-width2)+"px";
		}
	} else {
		alert(text);
	}
}

function showBlockAlert(text){
	if (is_ie5_5up || is_firefox){
		if ($("alert")) {
			$("alertbutton").style.display="none";
			showAlert(text);
		}
	} else {
		alert(text)	;
	}
}



function hideAlert(){
	if ($("alert")) {
		if (is_ie) $("alerttext").innerText="";
			else $("alerttext").innerHTML="";
		$("alert").style.display="none";
		$("alertbutton").style.display="block";
	}
	hideFullBg();
}

var confirmAction=false;

function showConfirm(text, action){
	if (is_ie5_5up || is_firefox){
		if ($("confirm")) {
			confirmAction=action;
			showFullBg();
			if (is_ie) $("confirmtext").innerText=text;
				else $("confirmtext").innerHTML=text;
			$("confirm").style.display="block";
			height2=$("confirm").clientHeight/2;
			width2=$("confirm").clientWidth/2;
			windowHeight2=($("hidden").offsetTop+10)/2;
			if (window.scrollY) scrollTop=window.scrollY;
				else scrollTop=document.documentElement.scrollTop/2;
			windowHeight2+=scrollTop;
			if (window.innerWidth) windowWidth2=window.innerWidth/2;
				else windowWidth2=window.document.body.offsetWidth/2;
			$("confirm").style.top = (document.body.scrollTop+windowHeight2-height2)+"px";
			$("confirm").style.left = (document.body.scrollLeft+windowWidth2-width2)+"px";
		}
	} else {
		confirmAction=action;
		if (confirm(text)) doConfirm();
	}
}
function hideConfirm(){
	if ($("confirm")) {
		confirmAction=false;
		if (is_ie) $("confirmtext").innerText=""
			else $("confirmtext").innerHTML="";
		$("confirm").style.display="none";
	}
	hideFullBg();
}


function doConfirm(){
	if (confirmAction) {
		try {
			eval(confirmAction);
		} catch(e) {;}
	}
	hideConfirm();
}

var promptText=false;
var promptAction=false;

function showPrompt(text, action){
	if (is_ie5_5up || is_firefox){
		if ($("prompt")) {
			promptAction=action;
			showFullBg();
			if (is_ie) $("prompttext").innerText=text;
				else $("prompttext").innerHTML=text;
			promptText=false;
			$("prompt").style.display="block";
			$("promptinput").value="";
			height2=$("prompt").clientHeight/2;
			width2=$("prompt").clientWidth/2;
			windowHeight2=($("hidden").offsetTop+10)/2;
			if (window.scrollY) scrollTop=window.scrollY;
				else scrollTop=document.documentElement.scrollTop/2;
			windowHeight2+=scrollTop;
			if (window.innerWidth) windowWidth2=window.innerWidth/2;
				else windowWidth2=window.document.body.offsetWidth/2;
			$("prompt").style.top = (document.body.scrollTop+windowHeight2-height2)+"px";
			$("prompt").style.left = (document.body.scrollLeft+windowWidth2-width2)+"px";
		}
	} else {
		promptAction=action;
		$("promptinput").value=prompt(text);
		if ($("promptinput").value)	doPrompt();
	}
}
function hidePrompt(){
	if ($("prompt")) {
		promptAction=false;
		promptText=false;
		$("promptinput").value="";
		if (is_ie) $("prompttext").innerText="";
			else $("prompttext").innerHTML="";
		$("prompt").style.display="none";
	}
	hideFullBg();
}


function doPrompt(){
	if (promptAction) {
		try {
			promptText=$("promptinput").value;
			eval(promptAction);
		} catch(e) {;}
	}
	hidePrompt();
}


var restoreAction=false;

function showRestore(action){
	text='Востановление пароля.<br />&nbsp;<br />Пароль будет выслан на Ваш адрес<br />электронной почты.';
	if (is_ie5_5up || is_firefox){
		if ($("restore")) {
			restoreAction=action;
			showFullBg();
			$("restoretext").innerHTML=text;
			$("restore").style.display="block";
			$("restoreinput").value="";
			height2=$("restore").clientHeight/2;
			width2=$("restore").clientWidth/2;
			windowHeight2=($("hidden").offsetTop+10)/2;
			if (window.scrollY) scrollTop=window.scrollY;
				else scrollTop=document.documentElement.scrollTop/2;
			windowHeight2+=scrollTop;
			if (window.innerWidth) windowWidth2=window.innerWidth/2;
				else windowWidth2=window.document.body.offsetWidth/2;
			$("restore").style.top = (document.body.scrollTop+windowHeight2-height2)+"px";
			$("restore").style.left = (document.body.scrollLeft+windowWidth2-width2)+"px";
		}
	} else {
		restoreAction=action;
		$("restoreinput").value=prompt(text);
		if ($("restoreinput").value) doRestore();
	}
}
function hideRestore(){
	if ($("restore")) {
		restoreAction=false;
//		promptText=false;
		$("restoreinput").value="";
		$("restoretext").innerHTML="";
		$("restore").style.display="none";
	}
	hideFullBg();
}

function checkMail(obj){
	var pat=/^[a-zA-Z][a-zA-Z\_\.\-\d]*@[a-zA-Z\_\.\-\d]+.[a-zA-Z]{2,4}$/;
	if(pat.test(obj.value)) return true;
		else return false;
}

function checkMail(obj){
	var pat=/^[a-zA-Z][a-zA-Z\_\.\-\d]*@[a-zA-Z\_\.\-\d]+.[a-zA-Z]{2,4}$/;
	if(pat.test(obj.value)) return true;
		else return false;
}

function doRestore(){
	if (restoreAction) {
		try {
			promptText=$("restoreinput").value;
			if (promptText==''){
				hideRestore();
				showAlert('Вы не ввели email для отправки пароля.')	;
				return false;
			} else if (!checkMail($("restoreinput"))) {
				hideRestore();
				showAlert('Вы ввели некорректный email.');
				return false;
			}else {
				eval(restoreAction);
			}
		} catch(e) {;}
	}
	hideRestore();

}

function showFullBg(){
	$("globalConfirmBg").style.width=document.body.clientWidth+"px";
	$("globalConfirmBg").style.height=document.body.clientHeight+"px";
	window.onresize=function(){
		$("globalConfirmBg").style.width=document.body.clientWidth+"px";
		$("globalConfirmBg").style.height=document.body.clientHeight+"px";
	};
	$("globalConfirmBg").style.display="block";
	document.body.className+=" no-select";
}

function hideFullBg(){
	document.body.className=document.body.className.replace(" no-select","");
	document.body.className=document.body.className.replace("no-select","");
	$("globalConfirmBg").style.width="1px";
	$("globalConfirmBg").style.height="1px";
	$("globalConfirmBg").style.display="none";
	window.onresize=null;
}

//Вычисление смещения y координаты объета
function calcTop(x_ele){
//	if (!document.all) return (x_ele.offsetTop);
	var x_ret=0;
	var oParent = x_ele.offsetParent;
	if (oParent == null) return 0
		else x_ret=x_ele.offsetTop + calcTop(oParent);
	return x_ret;
}

//Вычисление смещения x координаты объета
function calcLeft(x_ele){
//	if (!document.all) return (x_ele.offsetLeft);
	var x_ret=0;
	var oParent = x_ele.offsetParent;
	if (oParent == null) return 0
		else x_ret=x_ele.offsetLeft + calcLeft(oParent);
	return x_ret;
}

function showAlarm(el, text){
	if ($("alarm")) {
		$("alarmtext").innerHTML=text;
		$("alarm").style.display="block";
		width2=$("alarm").clientWidth/2;
		widthel2=el.clientWidth/2;
		heightel=el.clientHeight;
		topel = calcTop(el);
		leftel = calcLeft(el);
		$("alarm").style.top = (topel+heightel+10)+"px";
		$("alarm").style.left = (widthel2-width2+leftel)+"px";
	}
}

function hideAlarm(){
	if ($("alarm")) {
		$("alarmtext").innerHTML="";
		$("alarm").style.display="none";
	}
}

function getCheckedValue(radioObj){
	if(!radioObj){ return ""; }
	var radioLength = radioObj.length;
	if(radioLength == undefined){ return (radioObj.checked) ? radioObj.value : ""; }
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked){ return radioObj[i].value; }
	}
	return "";
}

function setCheckedValue(radioObj, newValue){
	if(!radioObj){ return; }
	var radioLength = radioObj.length;
	if(radioLength == undefined){
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++){
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()){ radioObj[i].checked = true; }
	}
}


function checkSearch(){
    var sKeywords = document.getElementById('searchKeywords').value;

    sKeywords = sKeywords.replace(/&\w*;/,    '');
    sKeywords = sKeywords.replace(/[^\d]%/,   '');
    sKeywords = sKeywords.replace(/[\*\?]/,   '');
    sKeywords = sKeywords.replace(/\s.{1,2}\s/, '');
    sKeywords = sKeywords.replace(/^.{1,2}$/, '');
    sKeywords = sKeywords.replace(/^.{1,2}\s/, '');
    sKeywords = sKeywords.replace(/\s.{1,2}$/, '');
    sKeywords = sKeywords.replace(/\s/,       '');

    if (sKeywords.length == 0){ showAlert('Неверный поисковый запрос'); }

    return (sKeywords.length > 0);
}


/**
 *
 * @access public
 * @return void
 **/
function disableElements(id, el){
    if (typeof(id) == 'string' && typeof($('id')) != 'undefined'){ id = $(id); }
    if (!id){ return false; }
    if (typeof(el) == 'undefined'){ el = 'INPUT'; }
    var inputs = id.getElementsByTagName(el);
    for (var i = 0, n = inputs.length; i < n; i ++){
        inputs[i].disabled = true;
    }
}

/**
 *
 * @access public
 * @return void
 **/
function enableElements(id, el){
    if (typeof(id) == 'string' && typeof($('id')) != 'undefined'){ id = $(id); }
    if (!id){ return false; }
    if (typeof(el) == 'undefined'){ el = 'INPUT'; }
    var inputs = id.getElementsByTagName(el);
    for (var i = 0, n = inputs.length; i < n; i ++){
        inputs[i].disabled = false;
    }
}
