var baseUrl = document.getElementsByTagName("base")[0].href;

//Функция открытия окна превью

function doPrint() {
	var winWidth=(typeof(window.outerWidth) == "undefined")? window.document.body.clientWidth:window.outerWidth;
	var winHeight=(typeof(window.innerHeight) == "undefined")? window.document.body.clientHeight:window.innerHeight;
	var baseUrl = ((base=document.getElementsByTagName("base"))!=null)? base[0].href :"";
	var w=window.open("/cikrf/print.jsp","_blank","height="+winHeight+",width="+winWidth+",status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,resizable=yes, channelmode=no");
	w.focus();
}

//Подуровень меню

function subLevel(a,_name){
	if(a==null) return;
	var tag=a.parentNode.parentNode;
	if (tag.tagName.toLowerCase() !="td") return;
	var div=document.getElementById(_name);
	if(div==null) return;
	if (div.style.display=="none") {
		tag.style.backgroundColor="white";
		a.style.color="#e1081d";
		div.style.display="";
	}
	else {
		tag.style.backgroundColor="transparent";
		a.style.color="#2C417C";
		div.style.display="none";
	}
}

//Функции открытия новых окон
//---------------------------------
function openWin(name,w,h) {
   Winid=window.open(name, "miscwin","width="+w+", height="+h+", toolbar=no,status=no,scrollbars=yes,resizable=yes, directories=no, menuBar=yes, location=no");
   Winid.focus();
 }

 var myWinOpened=0;
 function openImgWin(name,comm,rootUrl,w,h) {
	if (myWinOpened) { if (!myWin.closed) myWin.close();} 
	if (w==null) w=800;
	if (h==null) h=400;
	if (comm==null) comm="";
	if (rootUrl==null) rootUrl=baseUrl;
	myWin = open("", "imgwin","width="+(w+80)+", height="+(h+100)+", toolbar=no,status=no,scrollbars=yes,resizable=yes, directories=no, location=no");
	myWin.document.open();
	myWin.document.write("<html><head><title>"+comm);
	myWin.document.write("</title></head><body style='overflow:auto' bgcolor=white>");
	myWin.document.write("<center><table><caption style='COLOR: #32498C;'>"+comm+"</caption>");
	myWin.document.write("<tr><th><img src='"+rootUrl+name+"'></th><tr>");
	myWin.document.write("<tr><td align=center><br><a href='javascript:close()' style='color:#1f5f8d;font-size: 12px;'>Закрыть это окно.</a></td></tr>");
	myWin.document.write("</table></center></body></html>");
	myWin.document.close();
	myWinOpened=1;
}
//----------------------------------------

function verify(form,len) {
	var f=form;
	var l= (len==null) ? 1:len;
	var fieldName= "";
	for (var i=0; i < f.elements.length; i++ ) {
		if (f.elements[i].getAttribute("mustBeFilled")==null) { continue;}
		fieldName= (f.elements[i].getAttribute("field")==null) ? f.elements[i].name : '  "'+f.elements[i].getAttribute("field")+'"  ';
		if ( (f.elements[i].type == "text" || f.elements[i].type == "textarea") && f.elements[i].value.length < l)
			{ alert("Проверьте, пожалуйста, правильность ввода поля:  "+fieldName+" ! "); return false;}
		if (f.elements[i].type == "text" && f.elements[i].name.indexOf("mail")!=-1 && !checkmail(f.elements[i].value)) 
			{alert("Проверьте, пожалуйста, правильность ввода поля:  "+fieldName+" ! "); return false;}
		if (f.elements[i].type == "select-one" && f.elements[i].options[f.elements[i].selectedIndex].value == "-1")
			{ alert("Не выбран ни один элемент в выпадающем списке:  "+fieldName+" ! "); return false;}    
	 }
	return true;
}

function checkmail(str) {
	var pattern = /^([a-zA-Z0-9_\.\-])+@(([a-zA-Z0-9\-])+.)+([a-zA-Z0-9]{2,4})+$/;
	var test = str.match(pattern);
	if (!test) return false;
	else return true;
}



function switchPage(lang) {
	if (lang==null || (lang!="rus" && lang!="eng")) return;
	var selfLoc=window.location.href.substring(baseUrl.length,window.location.href.length);
	var ind;
	var newLoc;
//	alert(selfLoc);
	if (lang=="eng") {
	
		ind=elemInArray(pages, selfLoc, 0);
	
		if (ind!=false) {
			newLoc=pages[ind].eng;
		}
		else newLoc=pages[0].eng;
	}
	else {
		ind=elemInArray(pages, selfLoc, 1);
		if (ind!=false) {
			newLoc=pages[ind].rus;
		}
		else newLoc=pages[0].rus;
	}

//	alert(newLoc);
	window.location.href=baseUrl +newLoc;
}

function elemInArray(arr, el, d) {
	if (arr==null) return false;
	if (el==null) return false;
	if (d==null || d=="") d=0;
	var find=false;
	for (var i=0; i<arr.length; i++ ) {
		if (d==0){
			if (arr[i].rus==el) {
				find=i;
				break;
			}
		}
		else {
			if (arr[i].eng==el) {
				find=i;
				break;
			}
		}
	}
	return find;
}


function getFileName(){
	alert('you entered me!!!');
	}