$(document).ready(function(){
	
});

function displayTab(but,iddiv){
	var elts = document.getElementsByName("tab");
	for (var i = 0; i < elts.length; i++){
		elts[i].className="tab";
	}
	var elts = document.getElementsByName("tabs_divs");
	for (var i = 0; i < elts.length; i++){
		elts[i].style.display="none";
	}
	var tab = $(but);
	tab.className="tab_a";
	$(iddiv).style.display='';
}

function MenuShower(act){
	var left = document.getElementById("left");
	var out='';
	for (var i = 0; i < left.childNodes.length; i++){
		var hdr = left.childNodes[i];
		if (hdr.tagName=="H1") {
		  hdr.className='';
		  var inner = left.childNodes[i+1];
			if (inner.tagName!='DIV') inner = left.childNodes[i+2];
		  inner.className='menu_tab';
		  if (hdr.id==act.id){
		    hdr.className='active';
		    inner.className='menu_tab_act';
			}
		}
 }
	return false;
}

function foto_form_create(){
  var el = document.createElement('input');
  el.name='foto[]';
  el.type='file';
  el.enctype='multipart/form-data';
  if (el.addEventListener) el.addEventListener('change', foto_form_create, false);
  else el.attachEvent('onclick', foto_form_create);
  var el2 = document.createElement('input');
  el2.name='filetitle[]';
  el2.type='text';
  el2.style.marginBottom = '10px';
  var el3 = document.createElement('<br>');
  var el4 = document.createElement('<br>');
  var container = document.getElementById("foto_container");
  container.appendChild(el);
  container.appendChild(el2);
  container.appendChild(el3);
  container.appendChild(el4);
}

function foto_form_create_without_title(div,name){
	var el = document.createElement('input');
	el.name=name+"[]";
	el.type='file';
	el.enctype='multipart/form-data';
	el.className='chart_title';
	var el2 = document.createElement('<br>');
	var container = document.getElementById("foto_container_"+div);
	container.appendChild(el2);
	container.appendChild(el);
	container.appendChild(el2);
}

//cmxforms for jquery.validate
// apply inline-box only for mozilla
if( jQuery.browser.mozilla ) {
	// do when DOM is ready
	$( function() {
		// search form, hide it, search labels to modify, filter classes nocmx and error
		$( 'form.cmxform' ).hide().find( 'p>label:not(.nocmx):not(.error)' ).each( function() {
			var $this = $(this);
			var labelContent = $this.html();
			var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
			// create block element with width of label
			var labelSpan = $("<span>")
				.css("display", "block")
				.width(labelWidth)
				.html(labelContent);
			// change display to mozilla specific inline-box
			$this.css("display", "-moz-inline-box")
				// remove children
				.empty()
				// add span element
				.append(labelSpan);
		// show form again
		}).end().show();
	});
};

var question_cnt;
if (!question_cnt) question_cnt=1;

function forms_addQuestionRow(){
	var str = "<tr><td class='title' id='forms_questions_tit_"+question_cnt+"'>Поле №"+question_cnt+"</td>";
	str+="<td class='var' id='forms_questions_"+question_cnt+"'>Тип вопроса<br/>";
	str+="<select name='type[]' id='forms_questions_type_"+question_cnt+"' onchange='return forms_questions_typechange("+question_cnt+",this.value);'><option value=''>-</option><option value='radio'>Выбор одного варианта</option><option value='checkbox'>Выбор нескольких вариантов</option><option value='text'>Простое поле для ввода текста</option><option value='area'>Большое поле для ввода текста</option></select><Br />";
	str+="</td></tr>";
	$("#mainquestions").after(str);
	question_cnt++;
}

function forms_questions_typechange(id,type){
	$("#forms_questions_value_"+id).remove();
	$("#forms_questions_title_"+id).remove();
	$("#forms_questions_type_"+id).after("<div id='forms_questions_title_"+id+"'>Текст вопроса:<br /><input type='text' name=title[] /></div>");
	
	if (type=='radio' || type=='checkbox'){
		$("#forms_questions_type_"+id).after("<div id='forms_questions_value_"+id+"'>Значение:<br /><textarea name=value[] class='area' title='каждый вариант на новой строке'></textarea></div>");
	}
	else if (type=='text' || type=='area'){
		$("#forms_questions_type_"+id).after("<div id='forms_questions_value_"+id+"'>Значение:<br /><input type='text' name=value[] title='подсказка для поля' /></div>");
	}
}
function forms_fillQuestionRow(question_data){
	for (i in question_data) {
		var str = "<tr id='forms_question_tr_"+question_data[i].id+"'><td class='title' id='forms_questions_tit_" + question_data[i].id + "'>Поле №" + question_cnt + "<br><a href='#' onclick='forms_delQuestionRow("+question_data[i].id+");return false;'>удалить</a></td>";
		str+="<input type='hidden' name='exist["+question_data[i].id+"]' value='"+question_data[i].id+"' />";
		str+="<td class='var' id='forms_questions_" + question_data[i].id + "'>Тип вопроса<br/>";
		str+="<select name='type["+question_data[i].id+"]' id='forms_questions_type_" + question_data[i].id + "' onchange='return forms_questions_typechange(" + question_data[i].id + ",this.value);'><option value=''>-</option><option value='radio'"+(question_data[i].type=='radio'?" selected":"")+">Выбор одного варианта</option><option value='checkbox' "+(question_data[i].type=='checkbox'?" selected":"")+">Выбор нескольких вариантов</option><option value='text'"+(question_data[i].type=='text'?" selected":"")+">Простое поле для ввода текста</option><option value='area'"+(question_data[i].type=='area'?" selected":"")+">Большое поле для ввода текста</option></select><Br />";
		str+="<div  id='forms_questions_title_"+question_data[i].id+"'>Текст вопроса:<br /><input type='text' name=title["+question_data[i].id+"] value=\""+question_data[i].title+"\" /></div>";
		str+="<div id='forms_questions_value_"+question_data[i].id+"'>Значение:<br />";
		if (question_data[i].type=='text' || question_data[i].type=='area')
			str+="<input type='text' name=value["+question_data[i].id+"] title='подсказка для поля' value=\""+question_data[i].value.replace("}|{","\n")+"\" />";
		if (question_data[i].type=='radio' || question_data[i].type=='checkbox')
			str+="<textarea name=value["+question_data[i].id+"] class='area' title='каждый вариант на новой строке'>"+question_data[i].value.replace(/~br~/gi,"\n")+"</textarea>";
		str+="</div>";
		str+="</td></tr>";
		$("#mainquestions").after(str);
		question_cnt++;
	}
}

function forms_delQuestionRow(_id){
	$("#forms_question_tr_"+_id).remove();
	$("#ActionForm").append("<input type='hidden' name='fordel[]' value='"+_id+"' />");
	return false;
}

var profile_cnt;
if (!profile_cnt) profile_cnt=1;

function profiles_addRow(status){
	var str = "<tr id='profile_row_"+profile_cnt+"'><td class='title'><input type='text' name='newprofile["+profile_cnt+"][title]' /></td>";
	str+="<td class='var'><select name='newprofile["+profile_cnt+"][type]'><option value='text'>Маленькое поле для ввода текста</option><option value='radio'>Выбор одного</option><option value='checkbox'>Выбор нескольких</option><option value='select'>Выпадающий список</option><option value='area'>Большое поле для ввода текста</option></select><br />";
	str+="<span>Варианты ответов или пояснение к полю</span><br /><textarea class='area' name='newprofile["+profile_cnt+"][value]'></textarea></td>";
	str+="<td class='icon'><a href='"+profile_cnt+"' onclick='return profile_delRow("+profile_cnt+",1)'><img src='/i/icon_no.gif' width='20' height='16' alt='' title='Удалить' /></a></td>";
	str+="<input type='hidden' name='newprofile["+profile_cnt+"][status]' value='"+status+"'/></tr>";
	$("#profiles_"+status).after(str);
	profile_cnt++;
	return false;
}
function profile_delRow(_id,_status){
	$("#profile_row_"+_id).remove();
	if (_status==2){
		$("#ActionForm").append("<input type='hidden' name='fordel[]' value='"+_id+"' />");
	}
	return false;
}
