var classFix = (document.all)?"className":"class";
// ........................................................
// Скрытые блоки привязанные к Checkboxу
function cmsSetHiddenBlockFor(checkboxName,blockName){
	var oCheckbox,oBlock;
	if((oBlock=document.getElementById(blockName)) && 
		 (oCheckbox=document.getElementById(checkboxName))){
		oCheckbox.blockName = blockName;
	}
}
function cmsShowHiddenBlock(oCheckbox){
	var oBlock;
	if(oCheckbox.blockName &&
		(oBlock=document.getElementById(oCheckbox.blockName))){
		oBlock.style.display = (oCheckbox.checked)?"block":"none";
	}
}
// ........................................................
// Установка привязки битовых Checkboxов к Inputу
function cmsSetBitCheckboxesFor(inputName,checkboxesBitSize){
	var oInput,oBitCheckbox;
	if(oInput=document.getElementById(inputName)){
		oInput.checkboxesBitSize = checkboxesBitSize;
		var value = oInput.value*1;
		for(var i=0; i<checkboxesBitSize;i++){
			if(oBitCheckbox=document.getElementById(inputName+"_"+i)){
				oBitCheckbox.checked = ((value&(1<<i))>0);
				cmsChangeCheckbox(oBitCheckbox);
			}
		}
	}
}
// ........................................................
// Установка значения Selectа
function cmsSetSelectValue(selectName,selectValue){
	var oSelect;
	var value = (selectValue)?selectValue:false;
	if(oSelect=document.getElementById(selectName)){
		if(value){
			for(var i=0; i<oSelect.options.length;i++){
				if(oSelect.options[i].value == value){
					oSelect.selectedIndex = i;
				}
			}
		}
		cmsChangeSelect(oSelect);
	}
}
// ........................................................
// Изменение состояния Checkboxа
function cmsChangeCheckbox(oCheckbox){
	var oInput;
	var names = oCheckbox.id.split("_");
	if(oInput=document.getElementById(names[0])){
		var value = oInput.value*1;
		oInput.value = (oCheckbox.checked)?value|(1<<names[1]):value-(value&(1<<names[1]));
	}
	cmsShowHiddenBlock(oCheckbox);
}
// ........................................................
// Изменение состояния Selectа
function cmsChangeSelect(oSelect){
	var oInput;
	var mask = 0;
	var names = oSelect.id.split("_");
	if((names.length)>1 && (oInput=document.getElementById(names[0]))){
		for(var i=0;i<oInput.checkboxesBitSize;i++) mask += 1 << i;
		oInput.value = (oInput.value & mask) | oSelect.value;
		var value = oSelect.value & mask;
		for(var i=0; i<oInput.checkboxesBitSize;i++){
			if(oBitCheckbox=document.getElementById(names[0]+"_"+i)){
				oBitCheckbox.disabled = ((value&(1<<i))>0);
			}
		}
		cmsSetBitCheckboxesFor(names[0],oInput.checkboxesBitSize);
	}
}
// ........................................................
// Проверка введенных данных
	function cmsCheckInputValue(oInput,type){
		var iclass = oInput.getAttribute(classFix);
		var pos = iclass.indexOf("cms_errorfield");
		if(pos>=0){ 
			if(pos==0) oInput.removeAttribute(classFix);
			else			 oInput.setAttribute(classFix,iclass.substr(0,pos-1));
		}
		switch(type){
			case 0: // положительное целое
				if(oInput.value != oInput.value*1 || !(oInput.value>=0)){
					alert("Infill error: positive integer only\nОшибка заполнения поля: только положительное целое");
					oInput.value = 0;
					oInput.focus();
					if(oInput.getAttribute(classFix).length>0)	oInput.setAttribute(classFix,oInput.getAttribute(classFix)+" cms_errorfield");
					else																				oInput.setAttribute(classFix,"cms_errorfield");
					return false;
				}else{
					oInput.value = Math.abs(Math.round(oInput.value));
					return true;
				}
			break;
			case 1: // email
				var re_mail = /([\w\.\-_]+@[\w\.\-_]+)/;
				if(oInput.value.match(re_mail)!=null){
					return true;
				}else{
					alert("Infill error: email address needed\nОшибка заполнения поля: требуется email адрес");
					oInput.focus();
					if(oInput.getAttribute(classFix).length>0)	{
						oInput.setAttribute(classFix,oInput.getAttribute(classFix)+" cms_errorfield");
					}else{
						oInput.setAttribute(classFix,"cms_errorfield");
					}
					return false;
				}
			break;
			case 2: // checkbox
				var group_id = oInput.id;

				if(oInput.value == 0)
					return true;
				for(i=0; i<oInput.value; i++){
					var chb = document.getElementById(group_id+'_'+i);
					if (chb.checked){
						return true;
					}

				}				
				window.location = "#"+group_id;
				alert("Infill error: not select any option\nОшибка заполнения поля: не выбран ни один вариант");
				return false;
			break;
			case 3: // select
				if(oInput.selectedIndex>0){
					return true;
				}else{
					alert("Infill error: not select any option\nОшибка заполнения поля: не выбран ни один вариант");
					oInput.focus();
					if(oInput.getAttribute(classFix).length>0)	oInput.setAttribute(classFix,oInput.getAttribute(classFix)+" cms_errorfield");
					else																				oInput.setAttribute(classFix,"cms_errorfield");
					return false;
				}
			break;
			case 4: // date
				var group_id = oInput.id;

				for(i=0; i<3; i++){
					var chb = document.getElementById(group_id+'_'+i);
					if (chb.selectedIndex==0){
						window.location = "#"+group_id;
						alert("Infill error: select date\nОшибка заполнения поля: не указана дата");
						return false;
					}
				}	
				
				return true;
				
			break;
			default: // не пустое
				
				if(oInput.value.length>=2){
					return true;
				}else{
					alert("Infill error: text needed\nОшибка заполнения поля: требуется текст");
					oInput.focus();
					if(oInput.getAttribute(classFix).length>0)	oInput.setAttribute(classFix,oInput.getAttribute(classFix)+" cms_errorfield");
					else																				oInput.setAttribute(classFix,"cms_errorfield");
					return false;
				}
			break;
		}
	}
// ........................................................
// Функция поиска и подсветки текста в заданом ноде и его потомхах
function searchInNode(node, word, word_length){
			//alert(111);
	var word_position, skip, spannode, middlebit, endbit, middleclone;
	skip=0;
	word = word.toLowerCase();
	if(node.nodeType==3){
		word_position=node.data.toLowerCase().indexOf(word);
		var pre_word = (word_position>0?node.data.substr(word_position-1,1):"");
		if(word_position>=0 && (word_position==1 || pre_word==" " 
			|| pre_word=="(" || pre_word=="" || pre_word=="'" || pre_word=='"')){
			spannode=document.createElement("span");
			spannode.setAttribute(classFix,"cms_illuminate");
			middlebit=node.splitText(word_position);
			endbit=middlebit.splitText(word_length);
			middleclone=middlebit.cloneNode(true);
			spannode.appendChild(middleclone);
			middlebit.parentNode.replaceChild(spannode,middlebit);
			skip=1;
		}
	}else if(node.nodeType==1&& node.childNodes && node.tagName.toUpperCase()!="SCRIPT" && node.tagName.toUpperCase!="STYLE"){
		for (var child=0; child < node.childNodes.length; ++child){
			child=child+searchInNode(node.childNodes[child], word, word_length);
		}
	}
	return skip;
}

// ........................................................
// Добавление полей в форму обратной связи
var cmsFormAddedFields = new Array;

function cmsFormNewField(num){
	var oFormCanvas	=document.getElementById("cms_formcanvas");
	var oFormField	=document.getElementById("cms_formfield");
	var oNumber = cmsFormAddedFields.length;
	var tempElements;
	if(num>=0) oNumber = num;
	else			 cmsFormAddedFields[oNumber] = new Array(0,"заголовок","");
	var oNewField = oFormField.cloneNode(true);
	oNewField.removeAttribute("id");
	oNewField.style.display = "block";
	oFormCanvas.appendChild(oNewField);
	tempElements = oNewField.getElementsByTagName("select");
	if(tempElements.length>0){
		tempElements[0].setAttribute("name","felements["+oNumber+"][ftype]");
		for(var i=0;i<tempElements[0].options.length;i++){
			if(tempElements[0].options[i].value==cmsFormAddedFields[oNumber][0]) tempElements[0].selectedIndex = i;
		}
	}
	tempElements = oNewField.getElementsByTagName("input");
	if(tempElements.length>0){
		tempElements[0].setAttribute("name","felements["+oNumber+"][fname]");
		tempElements[0].value = cmsFormAddedFields[oNumber][1];
	}
	tempElements = oNewField.getElementsByTagName("textarea");
	if(tempElements.length>0){
		tempElements[0].setAttribute("name","felements["+oNumber+"][fdefault]");
		tempElements[0].value = cmsFormAddedFields[oNumber][2];
	}
}
function cmsFormUpdate(){
	for(var i=0;i<cmsFormAddedFields.length;i++) cmsFormNewField(i);
}
function cmsFormClear(){
	var oFormCanvas	=document.getElementById("cms_formcanvas");
	oFormCanvas.innerHTML = "";
	cmsFormAddedFields = new Array;
}
function cmsFormClearLast(){
	var _cmsFormAddedFields = cmsFormAddedFields;
	cmsFormClear();
	for(var i=0;i<(_cmsFormAddedFields.length-1);i++){
		cmsFormAddedFields[i] = _cmsFormAddedFields[i];
		cmsFormNewField(i);
	}
}
// ........................................................
// Добавление полей в форму голосования
var cmsPollAddedFields = new Array;

function cmsPollNewField(num){
	var oFormCanvas	=document.getElementById("cms_pollcanvas");
	var oFormField	=document.getElementById("cms_pollfield");
	var oNumber = cmsPollAddedFields.length;
	var tempElements;
	if(num>=0) oNumber = num;
	else			 cmsPollAddedFields[oNumber] = new Array("вариант","0");
	var oNewField = oFormField.cloneNode(true);
	oNewField.removeAttribute("id");
	oNewField.style.display = "block";
	oFormCanvas.appendChild(oNewField);
	tempElements = oNewField.getElementsByTagName("input");
		tempElements[0].setAttribute("name","felements["+oNumber+"][fname]");
		tempElements[0].value = cmsPollAddedFields[oNumber][0];
		tempElements[1].setAttribute("name","felements["+oNumber+"][fvalue]");
		tempElements[1].value = cmsPollAddedFields[oNumber][1];
}
function cmsPollUpdate(){
	for(var i=0;i<cmsPollAddedFields.length;i++) cmsPollNewField(i);
}
function cmsPollClear(){
	var oFormCanvas	=document.getElementById("cms_pollcanvas");
	oFormCanvas.innerHTML = "";
	cmsPollAddedFields = new Array;
}
function cmsPollClearLast(){
	var _cmsPollAddedFields = cmsPollAddedFields;
	cmsPollClear();
	for(var i=0;i<(_cmsPollAddedFields.length-1);i++){
		cmsPollAddedFields[i] = _cmsPollAddedFields[i];
		cmsPollNewField(i);
	}
}
// ........................................................
// Добавление картинки голосования
var cmsPollSummary = 0;
function cmsPollResult(num,value){
	var oPollCanvas	=document.getElementById("cms_pollresult_"+num);
	var oPollField	=document.getElementById("cms_pollresult");
	var oNumber = cmsPollAddedFields.length;
	var tempElements;
	if(oPollCanvas && oPollField){
		var oNewField = oPollField.cloneNode(true);
		oNewField.style.display = "inline";
		oNewField.removeAttribute("id");
		value = (cmsPollSummary>0)?value/cmsPollSummary:0.01;
		oNewField.width = Math.ceil(oNewField.width * value);
		oPollCanvas.appendChild(oNewField);
	}
}
// ........................................................
// Версия для печати
var blankwin=null;
function cmsBlankWindow(href) {
    blankwin = window.open(href,"blankwin","height=500,width=615,left=0,top=0,resizable=yes,scrollbars=1,toolbar=0")
	blankwin.focus();
	return false;
}
function cmsPrintVersion(href) {
    cmsBlankWindow(href.indexOf("?")!=-1?href+"&print=1":href+"?print=1");
	return false;
}

// ........................................................
// чудо-фото галерея
	var wrImageWindow;
	function rOpenImageWindow_(src,width,height){ 
		
		wWidth = (width)?width+4:800;
		wHeight = (height)?height+4:600;

		// wWidth = 800;
		// wHeight = 600;
		wrImageWindow=window.open("","blankImageWindow", 'status=no,scrollbars=no,resizable=yes,width='+(wWidth)+',height='+(wHeight)+'');
			wrImageWindow.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><title>Images</title>');
			wrImageWindow.document.write("<base href='"+base+"'>");

			wrImageWindow.document.write("<link type='text/css' rel='stylesheet' href='"+base+"data/styles/slides.css'>");

			wrImageWindow.document.write("</head><body class=popupWnd topmargin=0 marginheight=0 leftmargin=0 marginwidth=0 style='background:#d9effa;' text=black link=black alink=black vlink=black>");
			wrImageWindow.document.write("<script>var classFix = (document.body && document.body.getAttribute('className'))?'className':'class';</script>");
//			wrImageWindow.document.write("<script type='text/javascript' language='javascript' src='"+base+"data/slides.js'></script>");
			wrImageWindow.document.write("<script type='text/javascript' language='javascript'>function fStartShow(){document.getElementById('startShow').value=1;document.getElementById('play').setAttribute(classFix,'active');fShow();return false;}function fStopShow(){document.getElementById('startShow').value=0;document.getElementById('play').setAttribute(classFix,'');return false;}function fShow(){if (document.getElementById('startShow').value==1){forvard();setTimeout('fShow()', 1000*document.getElementById('delay').value);}}function forvard(){var aSrc=document.getElementById('strSrc').value.split(',');oImg = document.getElementById('currimage');currImg=getCurrImg(oImg,aSrc);if (aSrc.length>(currImg+1)){currImg++;}else{currImg=0;}oImg.src=aSrc[currImg]; return false;}function back(){var aSrc=document.getElementById('strSrc').value.split(',');oImg = document.getElementById('currimage');currImg=getCurrImg(oImg,aSrc); if ((currImg)>0){currImg--;}else{currImg=aSrc.length-1;}oImg.src=aSrc[currImg]; return false;}function getCurrImg(oImg,aSrc){for (i=0; i<aSrc.length; i++) {if (aSrc[i]==oImg.src)return i;}alert('error: curent image not found in array');return -1;}</script>");
			wrImageWindow.document.write("<input id='strSrc' type='hidden' value='"+getImages()+"'>");
			wrImageWindow.document.write("<input id='startShow' type='hidden' value='0'>");
			
			wrImageWindow.document.write("<table border=0 cellspacing=1 cellpadding=1 style='width:100%;height:100%'><tr><td align=center style='width:100%;height:100%'>");
			wrImageWindow.document.write("<img onload='window.outerWidth=this.width+20; window.outerHeight=this.height+250' id='currimage' src='"+src+"' border=0>");
			wrImageWindow.document.write("</td></tr><tr><td align=center>");
if (lang == 'eng')
{
				wrImageWindow.document.write("<b>Control <br> playback</b>");
				wrImageWindow.document.write("<div style='width: 124px;height: 18px;'><a href='#' id='back' onclick='return back();' title='backwards'><img src='./img/null.gif' alt=''></a>");
				wrImageWindow.document.write("<a href='#' id='forvard' onclick='return forvard();' title='forward'><img src='./img/null.gif' alt=''></a>");
				wrImageWindow.document.write("<a href='#' id='play' onclick='return fStartShow();' title='play'><img src='./img/null.gif' alt=''></a>");
				wrImageWindow.document.write("<a href='#' id='pause' onclick='return fStopShow();' title='pause'><img src='./img/null.gif' alt=''></a></div>");
			wrImageWindow.document.write("<br clear=all>Speed: <select id='delay'><option value='2' selected='selected' >2</option><option value='3' selected>3</option><option value='4' selected>4</option><option value='5' selected>5</option></select>");
}else{
				wrImageWindow.document.write("<br><b>Управление <br> воспроизведением</b><br>");
				wrImageWindow.document.write("<div style='width: 124px;height: 18px;'><a href='#' id='back' onclick='return back();' title='назад'><img src='./img/null.gif' alt=''></a>");
				wrImageWindow.document.write("<a href='#' id='forvard' onclick='return forvard();' title='вперед'><img src='./img/null.gif' alt=''></a>");
				wrImageWindow.document.write("<a href='#' id='play' onclick='return fStartShow();' title='показать'><img src='./img/null.gif' alt=''></a>");
				wrImageWindow.document.write("<a href='#' id='pause' onclick='return fStopShow();' title='пауза'><img src='./img/null.gif' alt=''></a></div>");
			wrImageWindow.document.write("<br clear=all>Скорость: <select id='delay'><option value='2' selected='selected' >2</option><option value='3' selected>3</option><option value='4' selected>4</option><option value='5' selected>5</option></select>");
}
			wrImageWindow.document.write("<br clear=all><br><br></td></tr></table>");
			wrImageWindow.document.write("</body></html>");
			wrImageWindow.document.close();
		wrImageWindow.resizeTo(wWidth+30,wHeight+40);
		wrImageWindow.focus();
	}
	// получаем массив ссылок на картинки в виде строки
	function getImages(){
		var sSrc="";
		var oContent=document.getElementById("photo_gallery");
		var aDivs=oContent.getElementsByTagName("DIV");
		for (i=0; i<aDivs.length; i++) {
/*
			if (aDivs[i].getAttribute(classFix)=="photo"){
				if (sSrc!="")
					sSrc+=",";
				sSrc+=aDivs[i].getElementsByTagName("IMG")[0].src.replace("preview/","");
			}
			
*/
			
			
			var img = aDivs[i].getElementsByTagName("IMG");
			
			if (img && img.length > 0) {
				
				if(img[0]) {
					if (sSrc!="") sSrc+=",";
					
						sSrc+=img[0].src.replace("preview/",""); 				}
			}
		}
		return sSrc;
	}
	

