function str_replace(s,o,n){
	while(s.indexOf(o) > -1){
		s = s.substring(-1,s.indexOf(o)) + n + s.substring(s.indexOf(o)+o.length,s.length);
	}
	return(s);
}

function xml_document(){
	this.path = null;
	this.doc = null;
	success = false; 
	//var MSXML_DOCUMENT_PROGIDS = new Array('MSXML2.FreeThreadedDOMDocument.5.0','MSXML2.FreeThreadedDOMDocument.4.0','MSXML2.FreeThreadedDOMDocument.3.0','MSXML2.FreeThreadedDOMDocument','Microsoft.FreeThreadedDOMDocument');
	var MSXML_DOCUMENT_PROGIDS = new Array('MSXML2.FreeThreadedDOMDocument','Microsoft.FreeThreadedDOMDocument');
	for (var i=0;i < MSXML_DOCUMENT_PROGIDS.length && !success; i++) {
		try{
			this.doc = new ActiveXObject(MSXML_DOCUMENT_PROGIDS[i]);
			success = true;
		}catch (e){}
	}
	if(!this.doc && document.implementation && document.implementation.createDocument){
		this.doc = document.implementation.createDocument("", "document", null);
	}
	this.doc.resolveExternals = false;
	this.doc.validateOnParse = false;
	if(navigator.appName == 'Microsoft Internet Explorer'){
		this.doc.documentElement = this.doc.createElement("document");
	}
	var _DOC = this.doc;
	this.load_xml = load_xml;

	function load_xml(url, eventHandler){
		this.path = url;
		if(navigator.appName == "Microsoft Internet Explorer"){
			_DOC.onreadystatechange = function() {
				if (_DOC.readyState == 4 && eventHandler) {
					eventHandler(_DOC,arguments);
				}
			}
		}else if(eventHandler){
			_DOC.onload = function() { eventHandler(_DOC); }
		}
		this.doc.load(url);
	}
}

function xml_xsl_transform(){
	var dbg_win = null;
	this.result;
	var _result = this.result;
	this.do_transform = do_transform;
	//var scriptFragRegex = RegExp('(?:<script.*?>)((\n|.)*?)(?:<\/script>)','im');
	var scriptFragRegex = /(?:<script.*?>)((\n|.)*?)(?:<\/script>)/im
	var cached_xsl_docs = [];
	if(navigator.appName == "Microsoft Internet Explorer"){
		var xslProc;	
		var xslDoc;
		/* 	xslDoc = new ActiveXObject("Msxml2.XSLTemplate.4.0"); */
		var last_xsl;
		var last_xml;
	}
	
	function eval_scripts(_result){
		scripts = '';
		tString = _result;
		var a = -1;
		var b = -1;

		do {
			a = tString.indexOf('<script');
			tString = tString.substring(a + 1);
			b = tString.indexOf('</script>');
			scripts += '<' + tString.substring(0, b);
		} while(a != -1);
		
		if (scripts.length > 1) {
			scripts = scripts.replace(/<\/?script[^>]*?>/ig, '');
			eval(scripts);
		}
	}
	
	function check_cache(cachePath){
		
		for(var i=0; i < cached_xsl_docs.length; ++i){
			if(cached_xsl_docs[i].cp == cachePath){
				return(cached_xsl_docs[i]);
			}
		}
		return(false);
	}
	
	function do_transform(xml,nxsl,evalScripts,cachePath){
		if(DBUG.get_on()){
			if(dbg_win == null){
			}
		}
		if(navigator.appName == "Microsoft Internet Explorer"){
			//var MSXML_XMLHTTP_PROGIDS = new Array('Msxml2.XSLTemplate.5.0','MSXML2.XSLTemplate.4.0','MSXML2.XSLTemplate.3.0','MSXML2.XSLTemplate','Microsoft.XSLTemplate');
			var MSXML_XMLHTTP_PROGIDS = new Array('MSXML2.XSLTemplate','Microsoft.XSLTemplate');
			var cacheObj = false;
			if(cachePath){ cacheObj = check_cache(cachePath); }
			if(!cacheObj){
				success = false;
				for (var i=0;i < MSXML_XMLHTTP_PROGIDS.length && !success; i++) {
					try{
						xslDoc = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);
						success = true;
					}catch (e){}
				}

				xslDoc.stylesheet = nxsl;
				xslProc = xslDoc.createProcessor();
				if(cachePath){ cached_xsl_docs.push({cp:cachePath,xd:xslDoc,xp:xslProc}); }
				window.status = 'no cache';
			}else{
				xslDoc = cacheObj.xd;
				xslProc = cacheObj.xp;
				window.status = 'used cache';
			}
			
			if(xml != last_xml){
				xslProc.input = xml;
			}
	    	xslProc.transform();
			_result = xslProc.output;
			last_xml = xml;
		}else{
			var processor = new XSLTProcessor();
			processor.importStylesheet(nxsl);
			var newDocument = processor.transformToFragment(xml,document);
			if(newDocument.hasChildNodes()){
				_result = newDocument.firstChild.innerHTML
				_result = str_replace(_result,'&gt;','>');
				_result = str_replace(_result,'&lt;','<');
			}else{
				_result = '';
			}
		}
		//_result = str_replace(_result,'~x!script!x~','script');
		//_result = str_replace(_result,'`',"'");
		if(evalScripts){
			eval_scripts(_result);
		}
		return(_result);
	}
}


function component(document_obj,tag_name){
/*--------------------------------------
<company_website_component>				
	<id>1</id>							
	<parent_id>0</parent_id>			
	<page_number>1001</page_number>		
	<website_id>1</website_id>			
	<component_type>row</component_type>
	<order_number>1</order_number>		
	<size>4</size>						
</company_website_component>			
--------------------------------------*/
	//tag_name
	var obj = this;
	this._xml = (tag_name)?document_obj.createElement(tag_name):document_obj.createElement('company_website_component');
	this.__document = document_obj;
	this.min_size = 1;
	var _document = document_obj;
	_document.documentElement.appendChild(this._xml);

	this.paramaters = new Array();
	this.medias = new Array();
	
	this.id = _document.createElement('id');
	this.parent_id = _document.createElement('parent_id');
	this.page_number = _document.createElement('page_number');
	this.component_type = _document.createElement('component_type');
	this.order_number = _document.createElement('order_num');
	this.size = _document.createElement('size');
	this.key;
	
	this._xml.appendChild(this.id);
	this._xml.appendChild(this.parent_id);
	this._xml.appendChild(this.page_number);
	this._xml.appendChild(this.component_type);
	this._xml.appendChild(this.order_number);
	this._xml.appendChild(this.size);
	
	function remove_all_children(node){
		while(node.hasChildNodes()){
			node.removeChild(node.firstChild);
		}
	}
	// Getters -------------------------------------------------------------- //
	this.get_id = get_id;
	this.get_parent_id = get_parent_id;
	this.get_page_number = get_page_number;
	this.get_component_type = get_component_type;
	this.get_order_number = get_order_number;
	this.get_size = get_size;
	this.get_paramater_value = get_paramater_value;
	this.get_paramater = get_paramater;
	this.get_media = get_media;
	this.get_min_size = get_min_size;
	
	function get_id(){
		return(this.id.firstChild.nodeValue);
	}
	function get_parent_id(){
		return(this.parent_id.firstChild.nodeValue);
	}
	function get_page_number(){
		return(this.page_number.firstChild.nodeValue);
	}
	function get_component_type(){
		return(this.component_type.firstChild.nodeValue);
	}
	function get_order_number(){
		return(Number(this.order_number.firstChild.nodeValue));
	}
	function get_size(){
		return(this.size.firstChild.nodeValue);
	}
	
	function get_paramater_value(key){
		for(var i=0; i < this.paramaters[key].childNodes.length; ++i){
			if(this.paramaters[key].childNodes[i].nodeName == 'value'){
				return(this.paramaters[key].childNodes[i].firstChild.nodeValue);
			}
		}
	}
	function get_paramater(key){
		return(this.paramaters[key]);
	}
	
	function get_media(media_key){
		return(this.medias[media_key]);
	}
	function get_min_size(){
		return(this.min_size);
	}
	
	
	// Setters -------------------------------------------------------------- //
	this.set_id = set_id;
	this.set_parent_id = set_parent_id;
	this.set_page_number = set_page_number;
	this.set_component_type = set_component_type;
	this.set_order_number = set_order_number;
	this.set_size = set_size;
	this.set_paramater = set_paramater;
	this.set_position = set_position;
	this.set_key = set_key;
	this.remove_paramater = remove_paramater;
	this.set_media = set_media;
	this.set_media_width = set_media_width;
	this.set_media_height = set_media_height;
	this.set_media_url = set_media_url;
	this.set_min_size = set_min_size;
	
	function set_id(value){
		remove_all_children(this.id);
		var id_text = _document.createTextNode(String(value));
		this.id.appendChild(id_text);
	}
	
	function set_parent_id(value){
		remove_all_children(this.parent_id);
		var _parent_id_text = _document.createTextNode(String(value));
		this.parent_id.appendChild(_parent_id_text);
	}
	
	function set_page_number(value){
		remove_all_children(this.page_number);
		var _page_number_text = _document.createTextNode(String(value));
		this.page_number.appendChild(_page_number_text);
	}
	
	function set_component_type(value){
		remove_all_children(this.component_type);
		var _component_type_text = _document.createTextNode(String(value));
		this.component_type.appendChild(_component_type_text);
	}
	
	function set_order_number(value){
		remove_all_children(this.order_number);
		var _order_number_text = _document.createTextNode(String(value));
		this.order_number.appendChild(_order_number_text);
	}
	
	function set_size(value){
		remove_all_children(this.size);
		var _size_text_str = (value < Number(this.min_size))? this.min_size:value;
		var _size_text = _document.createTextNode(String(_size_text_str));
		this.size.appendChild(_size_text);
	}
		
	function set_paramater(key,value,tag_name){
		var pram = this.get_paramater(key);
		if(pram){
			remove_all_children(pram);
		}else{
			var pram = (tag_name)?_document.createElement(tag_name):_document.createElement('website_component_parameter');
			this.paramaters.push(pram);
		}
		var cID = _document.createElement('company_website_component_id');
		var cIDA = _document.createElement('company_application_component_id');
		var pKey = _document.createElement('key');
		var pVal = _document.createElement('value');
		var cID_txt = obj.get_id();
		var cID_text = _document.createTextNode(cID_txt);
		var cIDA_text = _document.createTextNode(cID_txt);
		var pKey_text = _document.createTextNode(String(key));
		var pVal_text = _document.createTextNode(String(value));
		cID.appendChild(cID_text);
		cIDA.appendChild(cIDA_text);
		pKey.appendChild(pKey_text);
		pVal.appendChild(pVal_text);
		pram.appendChild(cID);
		pram.appendChild(cIDA);
		pram.appendChild(pKey);
		pram.appendChild(pVal);
		_document.documentElement.appendChild(pram);
		this.paramaters[key] = pram;
	}
	
	function set_min_size(value){
		this.min_size = value;
	}
	
	function remove_paramater(key){
		var pram = this.get_paramater(key);
		
		if(pram && pram.parentNode){
			remove_all_children(pram);
			pram.parentNode.removeChild(pram);
		}
	}
	
	function set_media(media_key,media_value,height_key,height_value,width_key,width_value,url_key,url_value,UID){
		var media = (this.medias[media_key])? this.medias[media_key]: null;
		if(!media){
			var media = this.medias[media_key] = new Array();
		}
		media['keys'] = {m:media_key,h:height_key,w:width_key,u:url_key};
		media['values'] = {m:media_value,h:height_value,w:width_value,u:url_value};
		media['inputs'] = {h:get_input_by_key(height_key,UID),w:get_input_by_key(width_key,UID),u:get_input_by_key(url_key,UID)};
	}
	function set_media_width(media_key,value){
		this.medias[media_key]['values']['w'] = value;
		this.medias[media_key]['inputs']['w'].value = value;
		this.set_paramater(this.medias[media_key]['keys']['w'],value);
	}
	function set_media_height(media_key,value){
		this.medias[media_key]['values']['h'] = value;
		this.medias[media_key]['inputs']['h'].value = value;
		this.set_paramater(this.medias[media_key]['keys']['h'],value);
	}
	function set_media_url(media_key,value){
		this.medias[media_key]['values']['u'] = value;
		this.medias[media_key]['inputs']['u'].value = value;
		this.set_paramater(this.medias[media_key]['keys']['u'],value);
	}
	function remove_media(media_key){
		if(this.medias[media_key] && this.medias[media_key]['inputs']){
			this.set_media_width(media_key,'');
			this.set_media_height(media_key,'');
			this.set_media_url(media_key,'');
			
			this.remove_paramater(this.medias[media_key]['keys']['w']);
			this.remove_paramater(this.medias[media_key]['keys']['h']);
			this.remove_paramater(this.medias[media_key]['keys']['u']);
		}
	}
	function has_media(){
		for(i in this.medias){
			if(this.medias[i]['values']['u'].length > 1){
				return(true);
			}
		}
		return(false);
	}
	function remove_all_medias(){
		for(i in this.medias){
			this.remove_media(i);
		}
	}
	function set_position(is_right){
		if(is_right){
			this.set_order_number(Number(this.get_order_number()) + 1);	
		}else{
			this.set_order_number(Number(this.get_order_number()) - 1);
		}
	}
	
	function set_key(key){
		this.key = key;
	}
	
	function remove(clean){
		if(this._xml){
			this.set_size('0');
			this.set_parent_id('DELETED');
			this.set_id('DELETED');
			this._xml.parentNode.removeChild(this._xml);
			if(clean){
				for(var i=0; i < this.paramaters.length; ++i){
					this.paramaters[i].parentNode.removeChild(this.paramaters[i]);
				}
			}
			delete(this._xml);
		}
	}
	this.remove = remove;
	this.remove_media = remove_media;
	this.remove_all_medias = remove_all_medias;
	this.has_media = has_media;
}