//alert("123");

function createHttpRequest() {
	var httpRequest;
	var browser = navigator.appName;

	if (browser == "Microsoft Internet Explorer") {
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		httpRequest = new XMLHttpRequest();
	}

	return httpRequest;
}

function sendRequest(file, _resultId, getRequestProc) {
	//for(i in httpRequest){
	//}
	resultId = _resultId;
	//document.getElementById(resultId).innerHTML = 'Подождите, идет загрузка...';
	httpRequest.open('get', file, true);
	httpRequest.onreadystatechange = getRequestProc;
	//alert(getRequestProc);
	//httpRequest.overrideMimeType('text/html; charset=windows-1251');
	httpRequest.send(null);
}

function getRequest() {
	if (httpRequest.readyState == 4) {
		//for(m in httpRequest){
		// alert(m);
		//}
		eval(httpRequest.responseText);
	}
}
function getRequest_() {
	if (httpRequest.readyState == 4) {
		document.getElementById(resultId).innerHTML = httpRequest.responseText;
		//document.getElementById(resultId).style.display="block";
		//document.getElementById("rez_div1").style.display="none";
	}
}

function hotelFilter(id,type,desc,page){
	//alert(desc);
	//alert(id+'='+type+'='+desc);
	if(page){
		document.getElementById("town_search1").action="town_"+id+"_"+page+".htm";
		document.getElementById("town_search1").submit();
	}
	else{
	 document.getElementById("town_search").action="town_"+id+".htm";
	 document.getElementById("town_id").value=id;
	 document.getElementById("search_cr").value=type;
	 document.getElementById("search_val").value=desc;
	 document.getElementById("town_search").submit();
	}
	return false;
}
function sendRequestPost(file,procRequest,post_data){
	xmlHttp.open("post",file,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", post.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.onreadystatechange=procRequest;
    xmlHttp.send(post);
}
function check_data(obj){
 try{
	var error="";
	//var email="tkvit@yandex.ru";
	//var re2 = "/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/";
	//alert(re2.test(email));
	var email_ = new RegExp("^([a-zA-Z0-9_\.\-])+\@([a-zA-Z0-9\-])+\.([a-z]{2,4})$","g");
    //var is_valid = email.test("vacancy@hotmail.com");
    //alert(is_valid);
	if(obj.username.value.length<3){error+="Имя пользователя должно быть не короче 3 символов\n";}
	if(obj.new_password.value.length<6){error+="Пароль не может быть короче 6 символов\n";}
	if(obj.new_password.value!==obj.password_confirm.value){error+="Пароль и подтверждение не совпадают\n";}
	if(!email_.test(obj.email.value)){error+="Неверный email\n";}
	if(obj.email.value!==obj.email_confirm.value){error+="Email и подтверждение не совпадают\n";}
	if(error==""){ return true;}else {alert(error);return false;}
 }
 catch (e){
 	alert (e.message);
 	return false;
 }
}
var httpRequest = createHttpRequest();
var resultId = '';
function floatDiv(){
	var link_;
	if(location.pathname=="/auto.html"|location.pathname=="/realty.html"|location.pathname=="/board.html"){
		switch (location.pathname){
			case "/auto.html":{
				link_="/auto-add.html";
				break;
			}
			case "/realty.html":{
				link_="/realty-add.html";
				break;
			}
			case "/board.html":{
				link_="/board-add.html";
				break;
			}
		}
		var div=document.createElement('DIV');
		div.id="float_div";
		div.style.backgroundColor="#48505b"; 
        div.style.fontFamily="Tahoma, Arial"; 
        div.style.fontWeight="normal"; 
        div.style.fontSize="10pt";
        div.style.color="#FFFFFF";
        div.style.verticalAlign="middle";
		div.style.width="160px";
		div.style.position="absolute";
		div.style.zIndex="99";
		div.style.left=screen.width-200;
		div.style.top=document.body.scrollTop+10;
		div.innerHTML="<div align=center style='vertical-align:middle;'><img src='images/close2.jpg' align='right' onclick='document.getElementById(\"float_div\").parentNode.removeChild(document.getElementById(\"float_div\"));'><br/><input type=button value='Добавить объявление' onclick='location.href=\""+link_+"\";'><br/>&nbsp;</div>";
		document.body.appendChild(div);
	}
}
function changePos(){
	try{
	document.getElementById("float_div").style.top=document.body.scrollTop+10;
	}
	catch(e){
		return;
	}
}
window.onload=floatDiv;
window.onscroll=changePos;

function createFolder(userId,form){
	var editDiv=document.getElementById("_edit_block_");
	if(typeof(form)=="object"){
		$(editDiv).load("galery.php?js_action=createFolder&folder_name="+encodeURI(form.folder_name.value)+"&user_id="+userId);
	}
	else{
	  editDiv.innerHTML='<form method="POST" onsubmit="return createFolder(\''+userId+'\',this)"><table align="center">'+
	                  '<tr><td>Название</td>'+
	                  '<td><input type="text" name="folder_name"></td>'+
	                  '<tr><td colspan="2" align="center">'+
	                  '<input type="submit" name="submit_" value="создать">'+
	                  '</td></tr></table></form>';
	}
	return false;                  
}
function editFolder(userId,form){
	var editDiv=document.getElementById("_edit_block_");
	if(typeof(form)=="object"){
		if(form.sel_folder.value!=form.parent_folder.value){
			$(editDiv).load("galery.php?js_action=editFolder&user_id="+userId+"&folder_name="+encodeURI(form.folder_name.value)+"&sel_folder="+form.sel_folder.value+"&parent_folder="+form.parent_folder.value);
		}
	}
	else{
		editDiv.innerHTML='<form method="POST" onsubmit="return editFolder(\''+userId+'\',this)"><table align="center">'+
		              '<tr><td>Выберите папку</td>'+
		              '<td id="sel_td"></td></tr>'+
	                  '<tr><td>Название</td>'+
	                  '<td><input type="text" name="folder_name" id="folder_name"></td></tr>'+
	                  '<tr><td>Выберите родителя</td>'+
		              '<td id="parent_td"></td></tr>'+
	                  '<tr><td colspan="2" align="center">'+
	                  '<input type="submit" name="submit_" value="сохранить">'+
	                  '</td></tr></table></form>';
	    $("#sel_td").load("galery.php?js_action=showSelectFolder&type=select&user_id="+userId);
	    $("#parent_td").load("galery.php?js_action=showSelectFolder&type=parent&user_id="+userId);
	}
	return false;
}
function delFolder(userId,form){
	var delDiv=document.getElementById("_edit_block_");
	if(typeof(form)=="object"){
		if(confirm("Вы уверенны")){
			$(delDiv).load("galery.php?js_action=delFolder&user_id="+userId+"&del_folder="+form.parent_folder.value);
		}
	}
	else{
		delDiv.innerHTML='<form method="POST" onsubmit="return delFolder(\''+userId+'\',this)"><table align="center">'+
		              '<tr><td>Выберите папку</td>'+
		              '<td id="parent_td"></td></tr>'+
	                  '<tr><td colspan="2" align="center">'+
	                  '<input type="submit" name="submit_" value="удалить">'+
	                  '</td></tr></table></form>';
	    $("#parent_td").load("galery.php?js_action=showSelectFolder&type=parent&user_id="+userId);
	}
	return false;
}
function moveFolder(userId,form){
	var moveDiv=document.getElementById("_edit_block_");
	moveDiv.innerHTML='<table align="center">'+
		              '<tr><td>Выберите папку</td>'+
		              '<td id="parent_td"></td></tr>'+
	                  '</table><div id="subfolders"></div>';
    $("#parent_td").load("galery.php?js_action=showSelectFolder&type=parent&move=1&user_id="+userId);
	return false;
}
function selectSubfolders(folder_id){
 if(folder_id!=""){
	$("#subfolders").load("galery.php?js_action=selectSubfolders&folder_id="+folder_id);
 }
}
function folderUp(id,userId,selFolder){
	$.getScript("galery.php?js_action=folderUp&folder_id="+id);
	$("#parent_td").load("galery.php?js_action=showSelectFolder&type=parent&move=1&user_id="+userId+"&sel_folder="+selFolder);
	return false;
}
function folderDown(id,userId,selFolder){
	$.getScript("galery.php?js_action=folderDown&folder_id="+id);
	$("#parent_td").load("galery.php?js_action=showSelectFolder&type=parent&move=1&user_id="+userId+"&sel_folder="+selFolder);
	return false;
}
function fileUpload(userId,complete){
	//alert(userId);
	var uplDiv=document.getElementById("_edit_block_");
	uplDiv.innerHTML='<form method=post enctype=multipart/form-data onsubmit="return sendForm(this,\'galery.php?js_action=fileUpload\',\'callback\')"><table align="center">'+
		              '<tr><td>Выберите папку</td>'+
		              '<td id="parent_td"></td></tr>'+
		              '<tr><td>Выберите файл</td>'+
		              '<td><div style="border-style:solid; border-width:1px;"><p></p>&nbsp;&nbsp;Заголовок <input type=text name=title[]><p></p>&nbsp;&nbsp;Имя&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=file name=foto[]>&nbsp;<input type=button onclick=addField(this) value="+">&nbsp;<input type=button onclick=delField(this) value="-"><p></div></td></tr>'+
		              '<tr><td colspan=2 align=center><input type=submit name="submit_" value="Сохранить"></td>'+
		              '</tr><input type=hidden name=user_id value="'+userId+'">'+
	                  '</table></form><div id="complete">'+((complete==1)?"Файлы загружены":"")+'</div><iframe id="upload_target" onload="" name="upload_target" src="" style="width:0;height:0;border:0px solid #FFFFFF;"></iframe>';
    $("#parent_td").load("galery.php?js_action=showSelectFolder&type=parent&user_id="+userId);
	return false;
}

function addField(obj){
	var inner=obj.parentNode.cloneNode(true);
	$("input:first",inner).attr("value","");
	obj.parentNode.parentNode.appendChild(inner);
}
function delField(obj){
	obj.parentNode.parentNode.removeChild(obj.parentNode);
}

function sendForm(obj,sendPath,callbackPath){
	obj.action=sendPath;
	obj.target="upload_target";
	document.getElementById("upload_target").onload=function () {fileUpload(obj.user_id.value,1);}
	obj.submit();
	return false;
}

function editImage(userId){
	var imageDiv=document.getElementById("_edit_block_");
	imageDiv.innerHTML='<table align="center">'+
		              '<tr><td>Выберите папку</td>'+
		              '<td id="parent_td"></td></tr>'+
	                  '</table><div id="images"></div><p>&nbsp;</p>';
    $("#parent_td").load("galery.php?js_action=showImageFolder&user_id="+userId);
	return false;
}
function reloadImage(folder_id,userId){
	$("#images").load("galery.php?js_action=reloadImage&user_id="+userId+"&folder_id="+folder_id);
}
function editImage_(userId){
	var imageDiv=document.getElementById("_edit_block_");
	imageDiv.innerHTML='<table align="left">'+
		              '<tr><td>Выберите папку</td>'+
		              '<td id="parent_td"></td></tr>'+
	                  '</table><div id="images"></div><p>&nbsp;</p>';
    $("#parent_td").load("galery.php?js_action=showImageFolder_&user_id="+userId);
	return false;
}
function changeTitle(obj,img_id){
	var title=(obj.firstChild.nodeType==3)?obj.innerHTML:obj.firstChild.value;
	obj.innerHTML=(obj.firstChild.nodeType==3)?'<input type="text" value="'+title+'" size=8 onblur="$.getScript(\'galery.php?js_action=changeTitle&title=\'+this.value+\'&img_id='+img_id+'\');this.parentNode.innerHTML=this.value" onkeydown="if(event.keyCode==13){$.getScript(\'galery.php?js_action=changeTitle&title=\'+this.value+\'&img_id='+img_id+'\');this.parentNode.innerHTML=this.value}"></input>':title;
	
}
function deleteImage(obj,img_id){
	$.getScript("galery.php?js_action=deleteImage&img_id="+img_id);
	obj.parentNode.parentNode.removeChild(obj.parentNode);
}
function createNewWind(obj,img_type,coment,fnc_,galery_id,foto_id){
	
	var path_=obj.src;
	if(typeof(img_type)=="string"){
		path_="http://n-novgorod.com/"+obj.src.substring(obj.src.indexOf("=")+1);
		if(obj.src.indexOf("&")!=-1){
			//var length_=(obj.src.indexOf("&")+1)-(obj.src.indexOf("=")+1);
			//alert(length_);
			path_="http://n-novgorod.com/"+obj.src.substring(obj.src.indexOf("=")+1,obj.src.indexOf("&"));
		}
	}
	var wind=document.createElement('DIV');
	wind.style.position='absolute';
	wind.id='floated_div';
	wind.style.padding='0';
	wind.style.top=(document.documentElement.scrollTop+5)+"px";
	wind.style.left=(document.documentElement.scrollLeft+5)+"px";
	wind.style.width='500px';
	wind.style.height='500px';
	//wind.style.paddingTop='20px';
	wind.style.overflow='auto';
	wind.style.backgroundColor='#fff';
	wind.style.borderStyle='outset';
	wind.style.borderWidth='2px';
	wind.innerHTML='<div style="background-color:blue;" align=right>&nbsp;<img onclick="this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);" src=close2.jpg>&nbsp;</div><p>'
	wind.innerHTML+='<div align=center style="border: 1px solid;padding:20;margin:10"><img src="'+path_+'"></div>';
	if(typeof(coment)=="string"){
		wind.innerHTML+='<div style="margin:10;"><span style="cursor:pointer" onclick="$(\'#coment_form\').slideToggle(\'slow\')"><input type=button value="Оставить коментарий"></input></span><p><div id="coment_form" align=center style="border: 1px solid;padding:20;display:none"><form method=post id="_form_" action="" target="" onsubmit=""><table><tr><td>Коментарий</td><td><textarea name=coment cols=35 rows=8></textarea></td></tr><tr><td colspan=2 align=center><input type=hidden name=foto_id value='+foto_id+'><input type=button name="submit_" onclick="saveComent_(\''+galery_id+'\',this.form.parentNode.parentNode.parentNode);"  value="Сохранить"></td></tr></table></form><iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #FFFFFF;"></iframe></div><p><div id="_coment_block_" align=left style="border: 1px solid;padding:20">'+fnc_+'</div></div>';
	}
	//else {wind.innerHTML+='</div>';}
	document.body.appendChild(wind);
	//window.open('','edit_window','height=500,width=500,scrollbars=1');
	//var tmp=wind.document;
	//tmp.write('<html><head><script type="text/javascript" src="test_lib.js"></script><script type="text/javascript" src="js/jquery.js"></script>');
	//tmp.write('<style>.coment_nik{font: 14px Tahoma}.coment_text{font: 12px Tahoma}</style></head><body>');
	//tmp.write('<div align=center style="border: 1px solid;padding:20"><img src="'+path_+'"></div>');
	/*
	if(typeof(coment)=="string"){
		tmp.write('<span style="cursor:pointer" onclick="$(\'#coment_form\').slideToggle(\'slow\')">Оставить коментарий</span><p><div id="coment_form" align=center style="border: 1px solid;padding:20;display:none"><form method=post id="_form_" action="" target="" onsubmit="return false"><table><tr><td>Коментарий</td><td><textarea name=coment cols=35 rows=8></textarea></td></tr><tr><td colspan=2 align=center><input type=button name="submit_" onclick="saveComent_();"  value="Сохранить"></td></tr></table></form><iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #FFFFFF;"></iframe></div><p><div align=left style="border: 1px solid;padding:20">'+fnc_+'</div>');
	}
	tmp.write('</body></html>');
	tmp.close();
	*/
	$("#floated_div").ppdrag();
	$("#coment_form").click(function(){$("#floated_div").ppdrag("destroy");});
	//$("#coment_form").blur(function(){$("#floated_div").ppdrag();});
}
function defineModule(alias,galery_id){
	if(galery_id){
		location.href="http://n-novgorod.com/rubrika25.html?id="+galery_id;
	}
}
function saveComent_(galery_id,closed_div){
	obj=document.getElementById("_form_");
	obj.action='galery.php?js_action=saveComent';
	obj.target="upload_target";
	$("#upload_target").load(function(){
		$("#galery_").load("/galery.php?js_action=loadGalery&id="+galery_id);
		closed_div.parentNode.removeChild(closed_div);
	});
	obj.submit();
	return false;
}
/*
function saveComent_(){
	obj=document.getElementById("_form_");
	obj.action='galery.php?js_action=saveComent';
	obj.target="upload_target";
	$("#upload_target").load(function(){
		$(window.opener.document.getElementById("galery_")).load("/galery.php?js_action=loadGalery&id=39");
	});
	obj.submit();
	return false;
}
function test_func__(){
	alert("123");
}
function test_class(){
	this.test_func__=function(){
		alert("123");
	}
}
*/
//var test_variable=window._test_="aa";
//window.new_wnd=new test_class();
//height=500,width=500,scrollbars=1,statusbar=1 $(\'#coment_form\').slideToggle(\'slow\');