//m3x
//******************************************************************************
function edit_m3x_var(nid){	
	ajax_loadContent('var_edit','admin/node.php?action=edit_m3x_var&nid='+nid);
}//*****************************************************************************

//******************************************************************************
function delete_m3x_var(nid){	
	if (confirm("Изтрии полето ?"))		
		ajax_loadContent('m3x_vars','admin/node.php?action=delete_m3x_var&id='+nid);
}//*****************************************************************************

//******************************************************************************
function load_m3x_vars(parent_id){	
	ajax_loadContent('m3x_vars','admin/node.php?action=load_m3x_vars&parent_id='+parent_id);
}//*****************************************************************************

//******************************************************************************
function new_m3x_var(){	
	ajax_loadContent('var_edit','admin/node.php?action=new_m3x_var');
}//*****************************************************************************

//******************************************************************************
function save_m3x_var(){
if (document.getElementById('var_name').value==''){
	alert('Липсва текст в полето Име');	
	return;
}
collect_send('m3x_vars','response','admin/node.php?action=save_m3x_var');
}//*****************************************************************************


//******************************************************************************
function update_m3x_var(nid){
if (document.getElementById('var_name').value==''){
	alert('Липсва текст в полето Име');	
	return;
}

collect_send('m3x_vars','response','admin/node.php?action=update_m3x_var&nid='+nid);
}//*****************************************************************************



//******************************************************************************
function go2page(page,m3x_name,show_rows,cont_id){	                                        
	if (document.getElementById('show_rows')){
		var ob = document.getElementById('show_rows');
		var show_rows=ob.options[ob.selectedIndex].value;
	}	 	
	ajax_loadContent(cont_id,'tools/infusions/paging/paging.php?action=paging&m3x_name='+m3x_name+'&page='+page+'&show_rows='+show_rows);                                                                  
}//*****************************************************************************


//******************************************************************************
function m3x_table(cont_id,m3x_name,m3x_templ,where,page){	                                        
	var show_rows='';
	if (document.getElementById(m3x_name+'_show_rows')){
		ob = document.getElementById(m3x_name+'_show_rows');
		show_rows=ob.options[ob.selectedIndex].value;
	}

	ajax_loadContent(cont_id,'tools/infusions/paging/paging.php?action=paging&m3x_name='+m3x_name+'&m3x_templ='+m3x_templ+'&where='+where+'&page='+page+'&show_rows='+show_rows);                                                                  
}//*****************************************************************************


//******************************************************************************
function edit_m3x_row(row_id,m3x_name){	
	ajax_loadContent('m3x_vars','admin/node.php?action=load_m3x_vars&row_id='+row_id);
}//*****************************************************************************






//******************************************************************************	
function ajax_loadContent(resp_id,dest){
	var request = (window.XMLHttpRequest) ? new window.XMLHttpRequest() :
	(window.ActiveXObject) ? new window.ActiveXObject("Microsoft.XMLHTTP") :
	null;
	if (!request) { return; }
	request.open("GET",dest, true);
	request.onreadystatechange = function(){
		if (request.readyState == 4){
			var req_resp=request.responseText;
			if (document.getElementById(resp_id))
				document.getElementById(resp_id).innerHTML = req_resp;
			js_exe(req_resp);
		}
	};
	request.send(null);
}



//******************************************************************************	
function dhtmlwin(tile,cont){
	var d = new Date();
	var inlinewin=dhtmlwindow.open(d.getTime()+"mydhtmlwin", "inline","","", "width=400px,height=400px,left=150px,top=10px,resize=1,scrolling=1", "recal")
	inlinewin.load('inline',cont,tile);
	inlinewin.show();
}



//******************************************************************************	
function removeElement(elem_ID){
  var d = document.getElementById(elem_ID).parentNode;
  var olddiv = document.getElementById(elem_ID);
  d.removeChild(olddiv);
}


//******************************************************************************	
function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
//******************************************************************************	


//******************************************************************************	
function js_exe(script){
	
	//script = replace(script,'\n',' ');
	
	var start_string="<script";
	var mid_string=">";
	var end_string="</script>";
	
	var i=0;
	while (script.indexOf(start_string) != -1){
		i=i+1;
		tmp_pos=script.indexOf(start_string)+start_string.length;
		script=script.substring(tmp_pos);
		
		tmp_pos=script.indexOf(mid_string)+mid_string.length;
		script=script.substring(tmp_pos);		
		
		script_elem=script.substring(0,script.indexOf(end_string));
		script_elem=script_elem.replace(/&amp;/, "&")
		eval.call(window, script_elem);
	
				
		tmp_pos=script.indexOf(end_string)+end_string.length;
		script=script.substring(tmp_pos);
	}				
}	

//******************************************************************************
//******************************************************************************
function expandit(curobj, hide) {
	if(document.getElementById(curobj)) {
  		folder=document.getElementById(curobj).style;
	} else {
		if(ns6==1||operaaa==true) {
			folder=curobj.nextSibling.nextSibling.style;
		} else {
			folder=document.all[curobj.sourceIndex+1].style;
		}
   }
	if(folder.display=="none")
	{
		folder.display="";
	} else {
		folder.display="none";
	}
	if(hide) {
		var hide_objects = hide.split(",");
		for(i=0; i<hide_objects.length; i++) {
			hide_objects[i]=hide_objects[i].replace(/^\s*(.*)/, "$1");
			hide_objects[i]=hide_objects[i].replace(/(.*?)\s*$/, "$1");
			if(document.getElementById(hide_objects[i])) {
				hidden=document.getElementById(hide_objects[i]).style;
				if(hidden.display=="") {
					hidden.display="none";
				}
			}
		}
	}
}//*****************************************************************************

//******************************************************************************
function ins_cont(el_id,cont){
	if(document.getElementById(el_id))
		document.getElementById(el_id).innerHTML=cont;		
}//*****************************************************************************

//******************************************************************************
function push_cont(el_id,cont){
	document.getElementById(el_id).innerHTML=document.getElementById(el_id).innerHTML+cont;		
}//*****************************************************************************


//******************************************************************************
function delete_page(subpage_id) {
	if (confirm("Подстраницата ще баде изтрита !!!")){	  
		send_data('','','','POST','response','admin/node.php?action=delete_subpage&subpage_id='+subpage_id);
 		//load_mpages();
	}
}


//******************************************************************************
function load_mpages() {
	var nid = String(document.getElementById('nid').value);

	var a = new Ajax();
	a.set_method('POST');
	a.add_input('action','get_mpages');	
	a.add_input('nid',nid);
	a.set_receive_handler(function() {
			if (a.get_ajax().readyState != 4) { return; }
			clearTimeout(a.get_timeout_obj());
			var o = document.getElementById('mpages');
			o.innerHTML = a.get_ajax().responseText;
	});
	a.send('<?=ADMIN?>node_mpages.php');	
	
}


//******************************************************************************
function startmove() {
	document.onmousemove=ResizeObject;
	document.onmouseup=stopmove;
}

function ResizeObject(evt) {
	document.getElementById("resline").style.width=evt.clientX+'px';
}

function stopmove(evt) {
	document.onmousemove=null;
}
//******************************************************************************

//******************************************************************************
current = 0;
function next(img,sel,path) {
	if (document.getElementById(sel)[current+1]) {
		document.getElementById(img).src = path+document.getElementById(sel)[current+1].value;
		document.getElementById(sel).selectedIndex = ++current;
	}
	else first(img,sel,path);
	}

function previous(img,sel,path) {
	if (current-1 >= 0) {
		document.getElementById(img).src = path+document.getElementById(sel)[current-1].value;
		document.getElementById(sel).selectedIndex = --current;
	}
	else last(img,sel,path);
}

function first(img,sel,path) {
	current = 0;
	document.getElementById(img).src = path+document.getElementById(sel)[0].value;
	document.getElementById(sel).selectedIndex = 0;
}

function last(img,sel,path) {
	current = document.getElementById(sel).length-1;
	document.getElementById(img).src = path+document.getElementById(sel)[current].value;
	document.getElementById(sel).selectedIndex = current;
}

function change(img,sel,path) {
	current = document.getElementById(sel).selectedIndex;
	document.getElementById(img).src = path+document.getElementById(sel)[current].value;
}
//******************************************************************************

//******************************************************************************
function sub_pages_onClick(){
if (document.getElementById("subpages_active").checked)
	tabbar.addTab("mpages","Под страници","",1); 
else 
	tabbar.removeTab("mpages");
}//*****************************************************************************


//******************************************************************************
function page_onClick(){
if (document.getElementById("page_active").checked){
  tabbar.addTab("main_page","Главна страница");
  tabbar.addTab("ext_page","Разширена страница");
	tabbar.setContentHTML("main_page",'<div id="main_page_instructions"><img src="images/edit.gif" /><a onClick="setTextareaToTinyMCE(\'main_page\');">Редактирай</a></div><div name="main_page" id="main_page"></div>');
	tabbar.setContentHTML("ext_page",'<div id="extended_page_instructions"><img src="images/edit.gif" /><a onClick="setTextareaToTinyMCE(\'extended_page\');">Редактирай</a></div><div name="extended_page" id="extended_page"></div>');
}else{ 
  tabbar.removeTab("main_page");
  tabbar.removeTab("ext_page");
}

expandit('page_config');
}//*****************************************************************************

//******************************************************************************
function menu_onClick(){
	expandit('menu_config');
}//*****************************************************************************


//******************************************************************************
function edit_sub_page(subpage_id){
 if(confirm("Ако не сте записали текущата страница, може да загубите променените, които сте правили !!!")){
 
	tinyMCE.execCommand('mceRemoveControl', false, 'extended_page');
	tinyMCE.execCommand('mceRemoveControl', false, 'main_page'); 

  
  send_data('','','','POST','page_config','admin/node.php?action=load_page_config&subpage_id='+subpage_id);

  tabbar.removeTab("main_page");
  tabbar.removeTab("ext_page");
  tabbar.addTab("main_page","Главна страница");
  tabbar.addTab("ext_page","Разширена страница");  
	tabbar.setContentHref('main_page','admin/node.php?action=load_extended_page&subpage_id='+subpage_id,'ajax');
	tabbar.setContentHref('ext_page','admin/node.php?action=load_main_page&subpage_id='+subpage_id,'ajax');
	tabbar.setTabActive("main_page");
 
 	ins_cont('hidden_vars','<input type="hidden" id="subpage_id" name="MapNode" value="'+subpage_id+'"/>');
	ins_cont('response','Подстраница е заредена за редакция');	
 }		
}//*****************************************************************************	

//******************************************************************************
function delete_sub_page(subpage_id){
 if(confirm("Подстраницата ще баде изтрита !!!")){
	removeElement(subpage_id);
	send_data('','','','POST','response','admin/node.php?action=delete_subpage&subpage_id='+subpage_id);
	ins_cont('response','Подстраницата е изтрита');	
 }		
}//*****************************************************************************

//******************************************************************************
function add_sub_page(){
 if(confirm("Ако не сте записали страницата в момента, може да загубите променените !!!")){

	tinyMCE.execCommand('mceRemoveControl', false, 'extended_page');
	tinyMCE.execCommand('mceRemoveControl', false, 'main_page'); 
	
	tabbar.setContentHref('main_page','admin/node.php?action=load_extended_page','ajax');
	tabbar.setContentHref('ext_page','admin/node.php?action=load_main_page','ajax');
	tabbar.setTabActive("config");	
	
	document.getElementById('page_active').checked="true";	
	document.getElementById('page_from').value="";
	document.getElementById('page_to').value="";
	
	tabbar.setTabActive("main_page");
	
	push_cont('hidden_vars','<input type="hidden" id="new_subpage" name="MapNode" value="new_subpage" />');
	ins_cont('response','Новата подстраница е заредена');	
 }		
}//*****************************************************************************



function addscript(newScript) { 
 var jsEl = document.createElement('script'); 
 jsEl.type = 'text/javascript'; 
 jsEl.appendChild(document.createTextNode(newScript)); 
 document.body.appendChild(jsEl); 
}


//******************************************************************************
function ajax2js(url){
var a = new Ajax();
a.set_method("POST");
a.set_receive_handler(function() {
	if (a.get_ajax().readyState != 4) { return; }
	clearTimeout(a.get_timeout_obj());	
	
	js_exe(a.get_ajax().responseText);

});
a.send(url);
}	

//******************************************************************************



