function aaXMLHttpRequest(){
	var xhr; 
	try { xhr = new ActiveXObject("Microsoft.XMLHTTP");}
	catch(e) { xhr = new XMLHttpRequest();}
	return xhr;
}



/*ADMIN*/
function update_catextend1( place_id ){
	cats = document.getElementById('extend1').value;
	var xhr = aaXMLHttpRequest(); 
	
	xhr.open("POST", "a_updates.php",  true); 
	
	xhr.onreadystatechange  = function()
	{ 
	 if(xhr.readyState  == 4)
		 if(xhr.status  == 200) {
			 alert('Uaktualniono!');
		 }
	};
	
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send( "place_id=" + place_id + "&cats=" + cats + "&action=update&what=extend1"  );
}



/*MARKERS*/
function show_markers_inbounds( ){
	show_loading_info();
	//document.getElementById('marker_count').innerHTML = "&nbsp;<img src=\"images/ajax_gfx/loading.gif\">";
	
	//create the boundary for the data
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	
	//checking with info window is shown
	var infowin = map.getInfoWindow();
	if( infowin.isHidden() ){activemarkerdata = null;}
	
	//filtr
	if( document.getElementById('showfm').checked )  var showfm = 1;  else var showfm = 0;
	if( document.getElementById('showacc').checked ) var showacc = 1; else var showacc = 0;
	if( document.getElementById('showft').checked )  var showft = 1;  else var showft = 0;

	//clearing
	mgr_clear_markers();
	//requesting new markers
	var getVars = "action=withinbounds&ne="+northEast.toUrlValue()+"&sw="+southWest.toUrlValue()+"&sfm="+showfm+"&sac="+showacc+"&sft="+showft;
	get_markers_big_map( getVars, 'normal' );
}


function show_place_markers(folder){ 
	show_loading_info();
	var getVars = "action=getmarker&ptype=place&folder="+folder;
	get_markers( getVars, 'normal' );
	
	show_loading_info();
	var getVars = "action=getsurround&ptype=place&folder="+folder;
	get_markers( getVars, 'small' );
}


function show_accom_markers(folder){
	show_loading_info();
	var getVars = "action=getmarker&ptype=accom&folder="+folder;
	get_markers( getVars, 'normal' );
	
	show_loading_info();
	var getVars = "action=getsurround&ptype=accom&folder="+folder;
	get_markers( getVars, 'small' );
}


function show_trip_markers(folder){
	show_loading_info();
	var getVars = "action=getsurround&ptype=trip&folder="+folder;
	get_markers( getVars, 'small' );
}


function show_trip_polys(folder){
	show_loading_info();
	var getVars = "action=gettrippline&ptype=trip&folder="+folder;
	get_polys( getVars );
}


function show_marker_info(folder, ptype){
	show_loading_info();
	var getVars = "action=getmarker&ptype="+ptype+"&folder="+folder;
	get_marker_info( getVars );

}

//GENERAL FUNCS
function get_markers( getVars, size ){
	var xhr = aaXMLHttpRequest();

	xhr.open("POST", "_new_search_ajax_getmarkers2.php",  true); 
	
	xhr.onreadystatechange  = function()
	{ 
	 if(xhr.readyState  == 4)
		 if(xhr.status  == 200) {
			 mgr_show_markers( xhr.responseXML, size );//responseText   responseXML
			 hide_loading_info();
		 }
	};
	
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send( getVars  );
}



function get_markers_big_map( getVars, size ){
	var xhr = aaXMLHttpRequest();

	xhr.open("POST", "_new_search_ajax_getmarkers2.php",  true); 
	
	xhr.onreadystatechange  = function()
	{ 
	 if(xhr.readyState  == 4)
		 if(xhr.status  == 200) {
			 xmlResp = xhr.responseXML;
			 mgr_show_markers(xmlResp, size ); //DISPLYING MARKERS //responseText   responseXML
			 if(activemarkerdata!=null) mgr_show_info_on_map(activemarkerdata);  //SHOWING INFO WINDOW
			 //mgr_list_items_big_map(xmlResp); //LISTING INFORMATION ON THE PAGE for the future
			 hide_loading_info();
		 }
	};
	
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send( getVars  );
}


function get_marker_info( getVars ){
	//show_loading_info();
	var xhr = aaXMLHttpRequest();

	xhr.open("POST", "_new_search_ajax_getmarkers2.php",  true); 
	
	xhr.onreadystatechange  = function()
	{ 
	 if(xhr.readyState  == 4)
		 if(xhr.status  == 200) {
			 mgr_prepare_marker_info( xhr.responseXML );//responseText   responseXML
			 hide_loading_info();
		 }
	};
	
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send( getVars  );
}


function get_polys( getVars ){
	var xhr = aaXMLHttpRequest(); 
	
	xhr.open("POST", "_new_search_ajax_getmarkers2.php",  true); 
	
	xhr.onreadystatechange  = function()
	{ 
	 if(xhr.readyState  == 4)
		 if(xhr.status  == 200) {
			 //alert('draw');
			 mgr_drawPolys( xhr.responseXML );
			 hide_loading_info();
		 }
	};
	
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send( getVars );
}



//CONTACT ACCOM


function get_contact_form( ){
	var f = document.getElementById("msg_folder").value;
	var getVars = "action=getform&f="+f;
	contact_form( getVars );
}


function cancel_contact_form( ){
	var f = document.getElementById("msg_f").msg_folder.value;
	var getVars = "action=cancel&f="+f;
	contact_form( getVars );
}


function send_contact_form(){
	var getVars = "action=send";
	var f = document.getElementById("msg_f");
	if(f.msg_folder) getVars = getVars+"&f="+f.msg_folder.value;
	if(f.msg_e) getVars = getVars+"&e="+f.msg_e.value;
	if(f.msg_r) {if(f.msg_r.checked) getVars = getVars+"&r=1"; else getVars = getVars+"&r=0";}
	if(f.msg_b) getVars = getVars+"&b="+ escape (fm_encode(f.msg_b.value));
	if(f.security_code) getVars = getVars+"&sc="+f.security_code.value;

	contact_form( getVars );
}



function feedback_contact_form( t ){
	var div = document.getElementById("contact_form_cont");
	if(div){
		div.innerHTML = t;
		//reload the div
		var scrollDiv = document.getElementById("mycustomscrol200");
		if(scrollDiv.scrollUpdate) scrollDiv.scrollUpdate();
	}
}



function contact_form( getVars ){
	
	feedback_contact_form( "<p style='text-align:center;'>Proszę czekać <img src=\"images/ajax_gfx/loading.gif\"></p>" );
	
	var xhr = aaXMLHttpRequest();

	xhr.open("POST", "_ajax_contact.php",  true); 
	
	xhr.onreadystatechange  = function()
	{ 
	 if(xhr.readyState  == 4)
		 if(xhr.status  == 200) {
			 feedback_contact_form( xhr.responseText );//responseText   responseXML
		 }
	};
	
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send( getVars  );
}





//INFO BOXES

var loading_info_req_nr = 0; //holds info how many time the window was called

function show_loading_info(){
	var div = document.getElementById('map_activity');
	if(div) {
		div.innerHTML = "<p class='pinfo'>Ładujemy, proszę czekać</p>";
		div.className = 'mapactivity';
		loading_info_req_nr++;
	}
}


function hide_loading_info(){
	var div = document.getElementById('map_activity');
	if(div) {
		loading_info_req_nr--;
		if( loading_info_req_nr <= 0 ){
			div.innerHTML = "";
			div.className = 'hidden';
		}
	}
}


function update_loading_info( info ){
	var div = document.getElementById('res');
	if(div) div.innerHTML = info;
}



//COMMENTS/BLOGS/ PIC COMMENTS

function pic_comment_edit( it, id ){
	/*ajax to retrive pic comment by item_type and comment id
	wrap: textarea of id 'edited_text_id', save, cancel into comment_container_id
	*/
	var s = 'it='+it+'&id='+id+'&a=pce';
	comment_pic_init( s, it, id );
}

function pic_comment_save( it, id ){
	var d = escape ( fm_encode( document.getElementById('edited_text_'+id).value ) );
	var s = 'it='+it+'&id='+id+'&a=pcs'+'&d='+d;
	comment_pic_do( s, it, id );
}


function pic_comment_cancel( it, id ){
	var s = 'it='+it+'&id='+id+'&a=pcc';
	comment_pic_do( s, it, id );
}


function comment_pic_print_res( id, txt ){
	var div = document.getElementById( id );
	if(div) div.innerHTML = txt;
}



function comment_pic_init( s, it, id ){
	/* PRINTING COMMENT FORM TO DIV of ID: 'edit_pic_com' */
	var edit_pic_com = document.getElementById( 'edit_pic_com_'+it );
	if(edit_pic_com) {
		document.getElementById( 'edit_pic_com_'+it ).innerHTML = "<div style='position:absolute; left:30px; top:80px;z-index:100;width:450px;height:230px;background-color:white;border:1px solid red; padding:50px 10px 10px 10px;text-align:center;font-size:12px;color:grey;'><img src='/images/ajax_gfx/loader_bar1.gif' border='0'><br>Proszę czekać !</div>";
		
		var xhr = aaXMLHttpRequest(); 
		xhr.open("POST", "a_comments.php",  true);
		xhr.onreadystatechange  = function()
		{ 
		 if(xhr.readyState  == 4)
			 if(xhr.status  == 200) {
				 document.getElementById( 'edit_pic_com_'+it ).innerHTML = fm_decode(xhr.responseText);
			 }else document.getElementById( 'edit_pic_com_'+it ).innerHTML = '<p style="color:red;">Operacja niepowiodla sie</p> <input type="button" value="Zamknij" onclick="">';
		};
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		xhr.send( s );
	}
}


function comment_pic_do( s, it, id ){
	/* PRINTING RESULTS BACK TO COMMENT DIV*/
	var xhr = aaXMLHttpRequest(); 
	xhr.open("POST", "a_comments.php",  true);
	xhr.onreadystatechange  = function()
	{ 
	 if(xhr.readyState  == 4)
		 if(xhr.status  == 200) {
			 comment_pic_print_res( 'comment_container_'+id, fm_decode(xhr.responseText) );
			 /*
			 var t = s +'&a=pcconf&r=1';
			 comment_pic_res_confirm( t );
			 */
		 }/* else {var t = s +'&a=pcconf&r=0';comment_pic_res_confirm( t );}*/
		 
		 document.getElementById( 'edit_pic_com_'+it ).innerHTML = "&nbsp;";
	};
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	xhr.send( s );
}


function comment_pic_res_confirm( s ){
	/* PRINTING CONFORMATION */
	var xhr = aaXMLHttpRequest(); 
	xhr.open("POST", "a_comments.php",  true);
	xhr.onreadystatechange  = function()
	{ 
	 if(xhr.readyState  == 4)
		 if(xhr.status  == 200) {
			 document.getElementById( 'edit_pic_com' ).innerHTML = fm_decode(xhr.responseText);
		 }
	};
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send( s );
}





//general coments
function comment_edit( it, id ){
	/*ajax to retrive comment by item_type and comment id
	wrap: textarea of id 'edited_text_id', save, cancel into comment_container_id
	*/
	var s = 'it='+it+'&id='+id+'&a=e';
	comment_do( s, id );
}


function comment_save( it, id ){
	/* ajax to update comment by item_type and comment id
	remember to switch 'modified' to 1 in the DB
	function returns just comment with top menu - initial state
	
	*/
	var d = escape ( fm_encode( document.getElementById('edited_text_'+id).value ) );
	var s = 'it='+it+'&id='+id+'&a=s'+'&d='+d;
	comment_do( s, id );
}

function comment_cancel( it, id ){
	/* ajax to retrive comment by item_type and comment id
	function returns just comment with top menu - initial state
	
	*/
	
	var s = 'it='+it+'&id='+id+'&a=c';
	comment_do( s, id );
}


function comment_block( it, id ){
	/* ajax to update comment by item_type and comment id, setting comment status=-5
	function returns string 'Komentarz zablokowany' with top menu - initial state, but instead zablokuj teraz jest odblokuj
	*/
	
	var s = 'it='+it+'&id='+id+'&a=b';
	comment_do( s, id );
}

function comment_unblock( it, id ){
	/* ajax to delete comment by item_type and comment id, setting comment status=1
	function returns just comment with top menu - initial state, but odblokuj teraz jest zablokuj
	*/
	var s = 'it='+it+'&id='+id+'&a=ub';
	comment_do( s, id );
}

function comment_delete( it, id ){
	/* ajax to remove komentarz only for comment owners, by item_type and comment id, setting comment status=-2
	function returns string 'Komentarz usuniety' with top menu - initial state, but instead zablokuj teraz jest odblokuj
	*/
	
	var s = 'it='+it+'&id='+id+'&a=d';
	comment_do( s, id );
}

function comment_undelete( it, id ){
	/* ajax to remove komentarz only for comment owners, by item_type and comment id, setting comment status=-2
	function returns string 'Komentarz usuniety' with top menu - initial state, but instead zablokuj teraz jest odblokuj
	*/
	
	var s = 'it='+it+'&id='+id+'&a=ud';
	comment_do( s, id );
}

function comment_print_res( id, txt ){
	var div = document.getElementById( 'comment_container_'+id );
	if(div) div.innerHTML = txt;
}


function comment_do( s, id ){
	var xhr = aaXMLHttpRequest(); 
	xhr.open("POST", "a_comments.php",  true);
	xhr.onreadystatechange  = function()
	{ 
	 if(xhr.readyState  == 4)
		 if(xhr.status  == 200) {
			 comment_print_res( id, fm_decode(xhr.responseText) );
		 }else comment_print_res( id, '<p style="color:red;">Operacja niepowiodla sie</p>' );
	};
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	xhr.send( s );
}



