function AjaxQuery(url,query, dest){
	$.ajax({
		async:true,
		url: url,
		data: query,
		dataType: "html",
		type:"POST", 
	 	success: function(data){
	 		RunJS(dest, data);
	 	},
	 	beforeSend: function(error){
	 		$('#'+dest).text("Loading data...");
	 	}
	});
}

function LoadAtributeForm(id_sec, id_attr, dest, dest2){
	field2=document.getElementById(dest2);
	if(field2) field2.innerHTML="";
	var act=(id_attr!=null)?'edit':'new';
	AjaxQuery("ajax_sections.php","sel=get_attr_form&act="+act+"&id_sec="+id_sec+"&id_attr="+id_attr, dest);
}

function LoadAttrPropsForm(control_type, id_attr, dest){
	query="ajax_sections.php";
	if(id_attr!=null) query+="&id_attr="+id_attr;
	AjaxQuery(query,"sel=get_attr_props_form&control_type="+control_type, dest);
}

function RunJS(div_name, response){
	var d2 = document.getElementById(div_name);

	//Check user browser
	var agent=(window.navigator.appName=='Netscape')?'Firefox':'IE';

	if(agent=='IE'){
		parseJS(div_name, response, true);
	}
	else{
		//Mozilla
		var d1=d2.parentNode;
		var d_new=document.createElement('ul');
		
		//Set same attributes as original
		var attributes=d2.attributes;
		var len=attributes.length;

		d_new.innerHTML=response;
		for(var i=0; i<len; i++){
			d_new.setAttribute(attributes[i].name, attributes[i].value);
		}
		
		d1.replaceChild(d_new, d2);
	}
}

function parseJS(div_name, html, cut_flag){
   
	if ('undefined' == typeof(cut_flag)) {
		cut_flag = false;
	}
	
	if (cut_flag) {
		var breaker = 0;
		var cut_html= html;
		var pattern = /<script.*src=".*"/g;
		var result  = cut_html.match(pattern);
		
		var indx= 1;
		var pos = 1;
		while (-1 != pos && 11 >= breaker++) {
			pos = cut_html.indexOf('src="');
			if (-1 != pos) {
				x = cut_html.substring(pos+5);
				pos_e = x.indexOf('"');
				cut_html = x.substring(pos_e);
				
				var s = document.createElement('script');
				s.id  = 'dynamic' + (indx);
				s.type= 'text/javascript';
				s.language = 'javascript';
				s.src = x.substring(0, pos_e);
				var b = document.getElementById(div_name);
				b.appendChild(s);
			}
		}
		
		
		cut_html = html;
		pattern = /<script.*\/script>/g;
		result = cut_html.match(pattern);
		
		var js_code = '';
		
		pos = 1;
		breaker = 0;
		
		var _body = document.getElementById(div_name);
		_body.innerHTML=html;
		
		while (-1 != pos && 11 >= breaker++) {
			pos = cut_html.indexOf('<script');
			if (-1 != pos) {
				cut_html = cut_html.substring(pos+5);
				pos_next = cut_html.indexOf('>');
				cut_html = cut_html.substring(pos_next+1);
				pos_end  = cut_html.indexOf('/script>');
				js_code += cut_html.substring(0, pos_end-1);
				cut_html = cut_html.substring(pos_end+8);
			}
		}

		cut_html = null;
		if(js_code!=''){
			var s = document.createElement('script');
			s.id  = 'dynamic' + (indx);
			s.type= 'text/javascript';
			s.language = 'javascript';
			s.text= js_code;
			var b = document.getElementById(div_name);
			b.appendChild(s);			
		}
	}
	
	return;
}