function inputTextFocus(item) {
	if(item.value == item.defaultValue)
		item.value = "";
}

function inputTextBlur(item) {
	if(item.value == "")
		item.value = item.defaultValue;
}

function inputPassFocus(item) {
	inputTextFocus(item);
}

function inputPassBlur(item) {
	inputTextBlur(item);
}

function addCamera() {
	addToTable("camsCont","camsSource","Фотоаппарат PENTAX N","camera:")
}
function addLens() {
	addToTable("lensCont","lensSource","Объектив PENTAX N","lens:")
}

function addToTable(tableName, sourceName, textName, inputName) {
	var table = document.getElementById(tableName);
	var num = table.childNodes.length + 1;

	if(/.*Firefox.*/.test(navigator.userAgent)) {
		num -= 2;
	}
	
	var tr = document.createElement("tr");
	var td1 = document.createElement("td");
	td1.setAttribute("class","text-form2");
	td1.className = "text-form2";
	
	
	var text = document.createTextNode(textName +num + ':');
	var textSP = document.createTextNode(" ");

	var td2 = document.createElement("td");
	td2.setAttribute("class","inp2");
	td2.className = "inp2";
	
	var input = document.createElement("select");
	input.setAttribute("name", "data:" + inputName +  num);

	var camSource = document.getElementById(sourceName);
	for(var i=0; i < camSource.childNodes.length; i++) {
		
		/*var input3 = document.createElement("input");
		input3.setAttribute("name", "dataq:" + inputName + num);
		input3.setAttribute("type", "text");*/
		
		if(typeof(camSource.childNodes[i].text) != "undefined") {
			var option = document.createElement("option");
			option.appendChild(document.createTextNode(camSource.childNodes[i].text));
			option.setAttribute("value",camSource.childNodes[i].getAttribute("value"));
			input.appendChild(option);
			
		}
	}

	var input2 = document.createElement("input");
	input2.setAttribute("type", "button");
	input2.setAttribute("value", "Удалить");
	input2.onclick = function() {
		removeFromTable(tr);
	}

	table.appendChild(tr);
	tr.appendChild(td1);
	tr.appendChild(td2);
	td1.appendChild(text);
	td2.appendChild(input);
	td2.appendChild(textSP);	
	td2.appendChild(input2);
	/*td2.appendChild(input3);*/
}

function removeFromTable(tr){
	tr.parentNode.removeChild(tr);
}

function setCookie(cookieName, cookieValue, path, expires, domain, secure) {
	document.cookie =
		escape(cookieName) + '=' + escape(cookieValue)
		+  (expires ? '; expires=' + expires.toGMTString() : '; expires='+new Date(new Date().getDate() + 200000*60 * 60 * 24 * 30 * 3).toGMTString())
		+ (path ? '; path=' + path : '')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
};

function getCookie(cookieName) {
	var cookieValue = '';
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else cookieValue = unescape(document.cookie.substring(posValue));
	}
	return (cookieValue);
};

function userPanelRoll(){
	document.getElementById("userPanel1").style.display = "none";
	document.getElementById("userPanel2").style.display = "";
	setCookie("userPanelState","roll","/");
}

function userPanelUnroll(){
	document.getElementById("userPanel1").style.display = "";
	document.getElementById("userPanel2").style.display = "none";
	setCookie("userPanelState","ulroll","/");
}

function userPanelUpdateState() {
	var state = getCookie("userPanelState");
	if(state == "roll")
		userPanelRoll();
}

function setPhotoAddSection(id) {
	setCookie("photoAddSection",id,"/");
}
function setArticleAddSection(id) {
	setCookie("articleAddSection",id,"/");
}
function proccessDelete() {
	return confirm("Вы действительно хотите удалить?");
}
function proccessDeny() {
	return confirm("Вы действительно хотите запретить?");
}

function toggleLoginRestore() {
	var loginForm = document.getElementById("loginForm");
	var restoreForm = document.getElementById("restoreForm");
	if(loginForm.style.display == "none") {
		restoreForm.style.display = "none";
		loginForm.style.display = "";
	}
	else {
		loginForm.style.display = "none";
		restoreForm.style.display = "";
	}
}

function restoreSubmit(form) {
	var item;
	var flag = false;
	for(var i=0; i < form.elements.length; i++) {
		item = form.elements[i];
		if(item.type == "text")
			if(item.value != item.defaultValue && item.value != "") {
				if(item.name == "data:mail")
					if(!item.value.match(/^[^@]+@[^.]+(\.[^.]+)+$/)) {
						alert("Вы указали не правильный e-mail.");
						return false;
					}
				return true;
			}
	}
	alert("Вы не указали ни логин, ни e-mail.");
	return false;
}

function setBG(node) { 
	document.getElementsByTagName("body")[0].bgColor = node.bgColor;
}

function processQuote() {
	var text = "";
	var author = "";
	if(typeof(document.getSelection) != "undefined") {
		var sel = window.getSelection();
		if(sel != "") {
			text = sel.toString();
			author = "";
			if(sel.focusNode.parentNode)
				author = getAutorName(sel.focusNode.parentNode.id);
		}
	}
	else {
		var sel = document.selection.createRange();
		if(sel.text != null || sel.text != "") {
			text = sel.text;
			if(sel.parentElement())
				author = getAutorName(sel.parentElement().id);
		}
	}
	if(text == "") {
		alert("Вы не выделили текст.\r\nВыделите текст, который вы хотите процитировать.");
		return;
	}
	var textArea = document.getElementById("commentText");
	insertText(textArea,"<quote" + (author == "" ? "" : " author=\"" + author + "\"") + ">" 
		+ text + "</quote>");
	
}
function getAutorName(tid) {
	var pos1 = tid.indexOf("c");
	var pos2 = tid.indexOf("text");
	if(pos1 == 0 && pos2 > 0) {
		var id = tid.substring(1,pos2);
		var authorItem = document.getElementById("c"+id+"autor");
		if(authorItem != null)
			return authorItem.innerHTML;
	}
	return "";
}
function insertText(input, insTexte) {
	var startTag = '';
	var endTag = '';

    if (input.createTextRange) {
		var text;
		input.focus(input.caretPos);
		input.caretPos = document.selection.createRange().duplicate();
		if(input.caretPos.text.length>0) {
			input.caretPos.text = startTag + input.caretPos.text + endTag;
		}
		else {
			input.caretPos.text = startTag + insTexte + endTag;
		}
	}
	else input.value += startTag + insTexte + endTag;
}

function floatPopup(obj) {
	var wind = window.open(obj.href,'bigimg','top=0,left=0,toolbar=0,scrollbars=1,resizable=1,width=720,height=600');
	wind.focus();
	return false;
}
// Переход на следующую старницу
function SwitchPage(direct, last)
{	
	curpage = location.href;
	if(curpage.indexOf('/list') != -1)//Не первая страница нефильтрованного списка
	{
		list_ind = curpage.indexOf('/list') + 5;
		point_ind = curpage.indexOf('.', list_ind);
	}
	else if(curpage.indexOf('list=') != -1)//Не первая страница фильтрованного списка
	{
		list_ind = curpage.indexOf('list=') + 5;
		point_ind = curpage.length;
	}
	else if(curpage.indexOf('filter.htm?') != -1)//Первая страница фильтрованного списка в фотогалерее
	{	
		newpage = curpage + "&list=2";
		//list_ind = point_ind = 0;
		location.href = newpage;
		return;
	}
	else if(curpage.indexOf('all_photos.htm') != -1)//Первая страница в фотках определенного юзера
	{	
		newpage = curpage + "?list=2";
		//list_ind = point_ind = 0;
		location.href = newpage;
		return;
	}
	else //Первая страница не фильтрованного списка
	{
		if(curpage.indexOf('?') != -1) {
			newpage = curpage.substring(0, curpage.indexOf('?')) + "list2.htm" + curpage.substring(curpage.indexOf('?'), curpage.length);
		} else {
			newpage = curpage + "list2.htm";
		}
		location.href = newpage;
		return;
	}
	// Сохранение номера
	list = curpage.substring(list_ind, point_ind);
	numb = list.valueOf();
	if(direct == 1)// На предыдущую
	{
		numb--;
		if(numb < 1)return;
	}
	else if(direct == 2)// На следующую
	{
		numb++;
		if(numb > last)return;
	}
	else return;
	// Создание нового линка
	newpage = curpage.substring(0, list_ind);
	newpage += numb;
	newpage += curpage.substring(point_ind);
	location.href = newpage;
}
	