var flag=0;

function change_checkbox(obj) {
	if (obj.checked)
		flag++;
	else
		flag--;
	with (document.all("but_submit")) {
		if (flag)
			disabled=false;
		else
			disabled=true;
	}
	if (document.all("Update")) {
		with (document.all("Update")) {
			if (flag)
				disabled=false;
			else
				disabled=true;
		}
	}
}

function add_id_tree(type,id) {
	var obj=document.all('id_'+type).value;
	if (obj.trim=='0')
		document.all('id_'+type).value=id;
	else
		document.all('id_'+type).value+=','+id;
}

function del_id_tree(type,id) {
	var obj=new String(document.all('id_'+type).value);
	var splitArr=obj.split(',');
	var z=0;
	var newArr=new Array();
	for (var i=0;i<splitArr.length;i++) {
		if (parseInt(splitArr[i])!=id) {
			newArr[z]=splitArr[i];
			z++;
		}
	}
	document.all('id_'+type).value=newArr.join(',');	
}

function action_tree(id) {
	var splitArr=id.split("_");
	with (document) {
		if (all(id).style.display=="block") {
			all(id).style.display="none";
			all('img_'+id).src="images/plus.gif";
			del_id_tree(splitArr[0],splitArr[1]);
		} else {
			all(id).style.display="block";
			all('img_'+id).src="images/minus.gif";
			add_id_tree(splitArr[0],splitArr[1]);
		}
	}
}

function checkCity(obj) {
	if (obj.value=='zona') {
		alert('Вы должны выбрать город!');
		if (document.all("other_city").style.visibility!="visible")
			obj.value=0;
		else
			obj.value=14;
		return false;
	} else if (obj.value==14) {
		with (document.all("other_city")) {
			style.visibility="visible";
			focus();
		}
	} else {
		with (document.all("other_city")) {
			style.visibility="hidden";
			value="";	
		}
	}
	return true;
}

function actForm(tbl_name,id) {
	with (document) {
		all("table_name").value=tbl_name;
		all("id").value=id;
		fo.submit();
	}
}

var name_cat='';
var id_cat='';

function rename_cat(id) {
	id_cat=id;
		with (document.all(id)) {
			name_cat=innerText;
			innerHTML='<input type="text" name="new_name" onKeyPress="actRename1();" id="new_name" size="28" maxlength="100" onBlur="setTimeout(\'skip_rename1()\',500);" style="height: 17px; font-size: 7pt; color: #191970; background-color: DCDCDC;"> <input type="button" name="act_rename" id="act_rename" value="Ok" onClick="actRename(); return false;" style="height: 17px; width: 20px; font-size: 7pt;">';
		}
		with (document.all("new_name")) {
			focus();
			value=name_cat;
		}
}

function replace_string(find_string, replace_string) {
    var temp_string = this
    var left_string
    var right_string
    var start_location
    var replace_location = temp_string.indexOf(find_string)
    while (replace_location != -1) {
        left_string = temp_string.left(replace_location)
        right_string = temp_string.substring(replace_location + find_string.length)
        temp_string = left_string + replace_string + right_string
        start_location = replace_location + replace_string.length
        replace_location = temp_string.indexOf(find_string, start_location)
    }
    return temp_string   
}

function extract_left(total_chars) {
    return this.substring(0, total_chars)
}

String.prototype.left = extract_left
String.prototype.replace = replace_string


var flag_rename=1;
function check_cat(str) {
	if (str!='') {
		str=str.replace(" ","");
		str=str.replace("(","");
		str=str.replace(")","");
		var charpos = str.search("[^A-Za-z0-9А-Яа-я]");
		if (charpos>=0) {
			alert("В названии категории (подкатегории) допустимы только\nбуквенно-цифровые символы, пробелы и круглые скобки!");
			flag_rename=0;
			return false;
		} else
			return true;
	} else {
		alert("Поле с названием категории (подкатегории) должно быть заполнено!");
		flag_rename=0;
		return false;
	}
}

function actRename1() {
	if (event.keyCode==13)
		actRename();
	else
		return true;
}

function actRename() {
	with (document) {
		if (check_cat(all('new_name').value)) {
			all('new_cat_name').value=all('new_name').value;
			fo.submit();
		}
	}
}

function skip_rename() {
	if (id_cat!='') {
		if (event.keyCode==27) {
			document.all(id_cat).innerHTML=name_cat;
		}
	}
}

function skip_rename1() {
	if (id_cat!=''&&flag_rename) {
		document.all(id_cat).innerHTML=name_cat;
	}
		
}

function delete_cat(cat_type) {
	with (document) {
		if (cat_type=='kat') {
			if (confirm('Вместе с данной категорией будут удалены все подкатегории, а также все данные относящиеся к данной категории и её подкатегориям. Вы уверены, что хотите удалить категорию?')) {
				all("del_cat").value=1;
				fo.submit();
			}
		} else if (cat_type=='podkat') {
			if (confirm('Вместе с данной подкатегорией будут удалены все данные относящиеся к данной подкатегориям. Вы уверены, что хотите удалить подкатегорию?')) {
				all("del_cat").value=1;
				fo.submit();
			}
		}
	}
}

function add_cat(new_cat) {
	if (!new_cat) new_cat=''; 
	new_cat=prompt("Введите название новой категории (подкатегории).",new_cat);
	if (new_cat) {
		if (check_cat(new_cat)) {
			with (document) {
				all("add_cat").value=1;
				all("new_cat_name").value=new_cat;
				fo.submit();
			}
		} else {
			add_cat(new_cat);
		}
	}
}

function reset_checkbox() {
	flag=0;
	document.all("Submit").disabled=true;
}

function select_all(form_name,f_name) {
	document.all("Submit").disabled=false;
	var el=document.forms[form_name].elements[f_name];
	for (i=0;i<el.length;i++) {
		el[i].checked=true;
		flag++;
	}
}

function setMonthYear() {
	document.forms[0].submit();
}

function confirm_del(obj) {
	if (flag>0)
		return confirm("Вы действительно хотите удалить выбранные записи?");
	else
		return false;
}
