var Service ={
	Forms : {
		Admin : null
	},
	Galleries : {
		Admin : null	
	},
	XML : {}
};
/*********************** FORM FUNCTIONS ***********************************/
Service.Forms.FormManager = {
	instance :[],
	_create : function(){
		var me = this;
		var formIndex = me.instance.length;
		me.instance[formIndex] = new Service.Forms.Form(formIndex,me);
		me.instance[formIndex]._construct();
		return me.instance[formIndex];
	}
};
Service.Forms.Form = function(indice,parentObject){
	var me = this;
	me.idn = "sform_" + indice;
	me._parent = parentObject;
	me.sections = [];
	me.sections_ac = 0;
	me.components_ac = 0;
	me.xml = null;
	me.name = "";
	me.title = "";
	me.tiempo = null;
	me.description = "";
	me.method = "post";
	me.action = "";
	me._construct = function(){
		me.XML._new();
	};
	me.AddSection = function(){
		var sIndex = me.sections_ac; 
		var arrayIndex = me.sections.length;
		me.sections[arrayIndex] = new Service.Forms.Section(sIndex,me);
		me.sections_ac++;
		return me.sections[arrayIndex];
	};
	me.RemoveSection = function(idn){
		var sIndex = me.getSectionArrayIndex(idn);
		if(sIndex==null)return;
		me.sections.splice(sIndex,1);
	};
	me.getSectionById = function(sid){
		var obj = null;
		for(var i =0;i<me.sections.length;i++){
			var seccion = me.sections[i];
			var idn =  seccion.idn;
			if(sid == idn){
				obj = seccion;	
				break;
			}
		}
		return obj;
	};
	me.getSectionArrayIndex = function(sid){
		var n = null;
		for(var i =0;i<me.sections.length;i++){
			var seccion = me.sections[i];
			var idn =  seccion.idn;
			if(sid == idn){
				n = i;	
				break;
			}
		}
		return n;
	};
	me.XML = {
		_new : function(){
			var xmlString = "<form></form>";
			me.xml = Service.XML._str2xml(xmlString);
		}, 
		_loadXML : function(url,vars,fname){
			var peticion = Service.HTTP._create();
			peticion.url = url;
			peticion.vars = vars;
			peticion.onFinish = function(){
				me.xml = null;
				me.xml = peticion.responseXML;
				if(fname)fname();				
			}
			peticion._get();
		},
		XML2Object : function(){
			var f = me.xml.documentElement;
			var x = Service.XML;
			me.title = x._subnodevalue(f,"title");
			me.description = x._subnodevalue(f,"description");
			me.tiempo = x._subnodevalue(f,"tiempo");
			var sections = f.getElementsByTagName("section");
			for(var i =0;i<sections.length;i++){
				var s = sections[i];	
				var nombre = x._subnodevalue(s,"title");
				var desc = x._subnodevalue(s,"description");
				var oSeccion = me.AddSection();
				oSeccion.title = nombre;
				oSeccion.description = desc;
				var components = s.getElementsByTagName("component");
				for(var j=0;j<components.length;j++){
					var c = components[j];
					var oComponent = oSeccion.AddComponent();
					oComponent.label = x._subnodevalue(c,"label");
					oComponent.helpString = x._subnodevalue(c,"helpstring");
					oComponent.type = c.getAttribute("type");
					
					oComponent.multiLine = (c.getAttribute("multiline")=="1")?true:false;
					oComponent.List.multiple = (c.getAttribute("multiple")=="1")?true:false;
					
					if(c.getElementsByTagName("validation").length > 0){
						var validation = c.getElementsByTagName("validation")[0];
						oComponent.validation.type = validation.getAttribute("type");
						oComponent.validation.num =  validation.getAttribute("num");;
					}
					var opciones = c.getElementsByTagName("option");
					for(var a=0;a<opciones.length;a++){
						var op = oComponent.List.addOption( x._subnodevalue(opciones[a],"name"), x._subnodevalue(opciones[a],"value"));
						op._selected = (opciones[a].getAttribute("selected")=="true")?true:false;										
					}
				}
			}
		},
		toXML : function(){
			this._new();
			var x = Service.XML;
			var doc = me.xml.documentElement;
			var tnode = me.xml.createElement("title");
			x.setText(tnode,me.title);
			doc.appendChild(tnode);
			
			var tdesc = me.xml.createElement("description");
			x.setText(tdesc,me.description);
			doc.appendChild(tdesc);
			
			var tiempoNode = me.xml.createElement("tiempo");
			x.setText(tiempoNode,me.tiempo);
			doc.appendChild(tiempoNode);
			
			var snodes = me.xml.createElement("sections");
			doc.appendChild(snodes);
			for(var i=0; i<me.sections.length;i++){
				var seccion = me.sections[i];
				var sn = me.xml.createElement("section");
				sn.setAttribute("Id",seccion.idn);
				var st = me.xml.createElement("title");
				x.setText(st,seccion.title);
				var sd = me.xml.createElement("description");
				x.setText(sd,seccion.description);
				sn.appendChild(st); /*nodo de titulo de seccion*/
				sn.appendChild(sd); /*nodo de titulo de descripcion*/
				var cns = me.xml.createElement("components");
				for(var j=0;j<seccion.components.length;j++){
					var component = seccion.components[j];
					var cn = this.XmlComponent(component);
					cns.appendChild(cn);
				}
				sn.appendChild(cns);
				snodes.appendChild(sn);
			}
			//prompt("",Service.XML._xml2string(me.xml));
			
		},
		XmlComponent : function(component){
			var x = Service.XML;
			var cn = me.xml.createElement("component");
			cn.setAttribute("type",component.type);
			cn.setAttribute("Id",component.idn);
			var ismultiline = (component.multiLine)?1:0;
			var ismultiple = (component.List.multiple)?1:0
			cn.setAttribute("multiline",ismultiline);
			cn.setAttribute("multiple",ismultiple);
			var clabel = me.xml.createElement("label");
			x.setText(clabel,component.label);
			cn.appendChild(clabel);
			
			var chelp = me.xml.createElement("helpstring");
			x.setText(chelp,component.helpString);
			cn.appendChild(chelp);
			
			var cvalidation = me.xml.createElement("validation");
			cvalidation.setAttribute("type",component.validation.type);
			cvalidation.setAttribute("num",component.validation.num);
			cn.appendChild(cvalidation);
			
			var coptions = this.XmlComponentOptions(component);
			cn.appendChild(coptions);
			
			return cn;
		},
		XmlComponentOptions : function(component){
			var x = Service.XML;
			var coptions = me.xml.createElement("options");
			for(var i=0;i<component.List._options.length;i++){
				var opcion = component.List._options[i];
				var opnode = me.xml.createElement("option");
				opnodeName = me.xml.createElement("name");
				opnodeValue = me.xml.createElement("value");
				opnode.setAttribute("selected",opcion._selected);
				x.setText(opnodeName,opcion.name);
				x.setText(opnodeValue,opcion.value);
				opnode.appendChild(opnodeName);
				opnode.appendChild(opnodeValue);
				coptions.appendChild(opnode);
			}
			return coptions;
		}
	};
};
Service.Forms.Section = function(indice,parentObject){
	var me = this;
	me._parent = parentObject;
	me.idn = me._parent.idn +"_sections_" + indice;
	me.title = "S. " + me.idn;
	me.description = "";
	me._form = me._parent;
	me.components = [];
	me.AddComponent = function(){
		var cIndex = me._form.components_ac;
		var arrayIndex = me.components.length;
		me.components[arrayIndex] = new Service.Forms.Component(cIndex,me);
		me._form.components_ac++;
		return me.components[arrayIndex];
	};
	me.RemoveComponentById = function(idn){
		var cIndex = me.getComponentArrayIndex(idn);
		if(cIndex==null)return;
		me.components.splice(cIndex,1);
	};
	me.getComponentById = function(sid){
		var obj = null;
		for(var i =0;i<me.components.length;i++){
			var component = me.components[i];
			var idn =  component.idn;
			if(sid == idn){
				obj = component;	
				break;
			}
		}
		return obj;
	};
	me.getComponentArrayIndex = function(sid){
		var n = null;
		for(var i =0;i<me.components.length;i++){
			var component = me.components[i];
			var idn =  component.idn;
			if(sid == idn){
				n = i;	
				break;
			}
		}
		return n;
	};
};

Service.Forms.Component = function(indice,parentObject){
	var me = this;
	me._parent = parentObject;
	me.idn = me._parent.idn + "_component_" + indice;
	me._form = parentObject._form;
	me.type = null;
	me.label = "";
	me.helpString = "";
	me.validation = {"type" : null,"num" : null};
	me.multiLine = false;
	me.cbox = false;
	me.List = {
		multiple : false,
		_options : [],
		addOption : function(n,v){
			var fn = this;
			var oIndex = fn._options.length;
			fn._options[oIndex] = new Service.Forms.Options(n,v);
			return fn._options[oIndex];
		},
		removeOption : function(n){
			var fn = this;
			if(fn._options[n]){
				fn._options.splice(n,1);	
			}
		},
		Clear : function(){
			this._options = [];
		}
	}
};
Service.Forms.Options = function(name,value){
	var me = this;
	me.name = name;
	me.value = value;
	me._selected = false;
};
/***************** GALLERY FUNCTIONS **********************/
Service.Galleries.GalleryManager = {
	instance : [],
	_create : function(){
		var me = this;
		var oIndex = me.instance.length;
		me.instance[oIndex] = new Service.Galleries.Gallery(oIndex,me);
		me.instance[oIndex]._construct();
		return me.instance[oIndex];
	}
}
Service.Galleries.Gallery = function(indice,parentObject){
	var me = this;
	me.idn = "sgallery_" + indice;
	me._parent = parentObject;
	me.pictures = [];
	me.pictures_ac = 0;
	me.xml = null;
	me.title = "";
	me.descripcion = "";
	me._construct = function(){
		var xmlString = "<gallery></gallery>";
		me.xml = Service.XML._str2xml(xmlString);
	};
	me.AddPicture = function(path){
		var cIndex = me.pictures_ac;
		var arrayIndex = me.pictures.length;
		me.pictures[arrayIndex] = new Service.Galleries.Picture(cIndex,me);
		me.pictures[arrayIndex].path = path;
		me.pictures_ac++;
		return me.pictures[arrayIndex];
	}
	me.RemovePicture = function(idn){
		var sIndex = me.getPictureArrayIndex(idn);
		if(sIndex==null)return;
		me.pictures.splice(sIndex,1);
	};
	me.getPictureById = function(sid){
		var obj = null;
		for(var i =0;i<me.pictures.length;i++){
			var picture = me.pictures[i];
			var idn =  picture.idn;
			if(sid == idn){
				obj = picture;	
				break;
			}
		}
		return obj;
	};
	me.getPictureArrayIndex = function(sid){
		var n = null;
		for(var i =0;i<me.pictures.length;i++){
			var picture = me.pictures[i];
			var idn =  picture.idn;
			if(sid == idn){
				n = i;	
				break;
			}
		}
		return n;
	};
	me.XML = {
		_new : function(){
			var xmlString = "<gallery></gallery>";
			me.xml = Service.XML._str2xml(xmlString);
		}, 
		_loadXML : function(url,vars,fname){
			var peticion = Service.HTTP._create();
			peticion.url = url;
			peticion.vars = vars;
			peticion.onFinish = function(){
				me.xml = null;
				me.xml = peticion.responseXML;
				if(fname)fname();				
			}
			peticion._get();
		},
		XML2Object : function(){
			var f = me.xml.documentElement;
			var x = Service.XML;
			me.title = x._subnodevalue(f,"title");
			me.description = x._subnodevalue(f,"description")
			var pictures = f.getElementsByTagName("picture");
			for(var i =0;i<pictures.length;i++){
				var s = pictures[i];	
				var nombre = x._subnodevalue(s,"title");
				var desc = x._subnodevalue(s,"description");
				var path = x._subnodevalue(s,"path");
				var oPicture = me.AddPicture();
				oPicture.title = nombre;
				oPicture.description = desc;
				oPicture.path = path;
			}
		},
		toXML : function(){
			this._new();
			var x = Service.XML;
			var doc = me.xml.documentElement;			
			var tnode = me.xml.createElement("title");
			x.setText(tnode,me.title);
			doc.appendChild(tnode);
			
			var tdesc = me.xml.createElement("description");
			x.setText(tdesc,me.description);
			doc.appendChild(tdesc);
			
			var snodes = me.xml.createElement("pictures");
			doc.appendChild(snodes);
			for(var i=0; i<me.pictures.length;i++){
				var picture = me.pictures[i];
				var sn = me.xml.createElement("picture");
				sn.setAttribute("Id",picture.idn);
				
				var st = me.xml.createElement("title");
				x.setText(st,picture.title);
				
				var sd = me.xml.createElement("description");
				x.setText(sd,picture.description);
				
				var sp = me.xml.createElement("path");
				x.setText(sp,picture.path);
				
				sn.appendChild(st); /*nodo de titulo de imagen*/
				sn.appendChild(sd); /*nodo de descripcion de imagen*/
				sn.appendChild(sp);
				snodes.appendChild(sn);
			}
			
		}
	}
	

};
Service.Galleries.Picture = function(oIndex,parentObject){
	var me = this;
	me.path = null;
	me.title = "";
	me.description = "";
	me.idn = "sgallery_" + parentObject.idn + "_" + oIndex;
};

/****************************** AJAX **********************************************/

Service.HTTP = {
	instances : [],
	_create : function(){
		var _this = this;
		var instanceIndex = _this.instances.length;
		_this.instances[instanceIndex] = new _this._r(instanceIndex,_this);
		return _this.instances[instanceIndex];
	},
	_r : function(pindex,parentObject){
		var me = this;
		me.indice = pindex;
		me._parent = parentObject;
		me.http = new Array();
		me.sendAsIso = false;
		me._nocache = true;
		me.status = null;
		me.onFinish = function(){};
		me.onStatus = function(n){};
		me.oVars = [];
		me.response = null;
		me.responseXML = null;
		me.url = "";
		me.vars = "";
		me.createRequestObject = function(){
			try { return new XMLHttpRequest(); } catch(e) {}
			try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) {}
			try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) {}
			return null;	
		}
		me._send = function(_ispost){
			var n = me.http.length;
			me.http[n] = me.createRequestObject();
			if(me.oVars.length > 0)
				me.vars = me.StrVars();
			var strQry = me.url + "?" + me.vars;
			//prompt("",me.vars);
			(_ispost)? me.http[n].open('POST', me.url,true) : me.http[n].open('GET', strQry);
			me.http[n].onreadystatechange = function()
			{
				if(me.http[n].readyState == 4){				
						me.status = me.http[n].status;
						me.onStatus(me.status);
						switch(me.http[n].status){
							case 12029: 
							case 12030: 
							case 12031: 
							case 12152: 
							case 12159:
								document.title = "Retrying request";
								me._send(_ispost);
								return;
							break;	
						}
						if (me.http[n].status == 200 || me.http[n] == 304){
							me.response = me.http[n].responseText;
							me.responseXML = me.http[n].responseXML;
							me.onFinish();
						}
				}
				
			}
			if(_ispost){
				if(me.sendAsIso){
					me.http[n].setRequestHeader("encoding", "ISO-8859-1");
					me.http[n].setRequestHeader("Content-type", "application/x-www-form-urlencoded;Charset=iso-8859-1");
				}else{
					me.http[n].setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
				}
				me.http[n].setRequestHeader("Content-length", me.vars.length);
				me.http[n].send(me.vars);
			}else{
				me.http[n].send(null);	
			}
		};
		me._post = function(){
			me._send(true);
		};
		me._get = function(){
			me._send(false);
		};
		me._add = function(name,value){
			var varsIndex = me.oVars.length;
			me.oVars[varsIndex] = encodeURIComponent(name) + "=" + encodeURIComponent(value);
		};
		me.StrVars = function(){
			if(me._nocache)me._add("ncacherand",Math.random());
			return me.oVars.join("&");
		}
	}
}
/***************** XML FUNCTIONS **************************/ 
Service.XML = {
	_str2xml : function(strXML){
		if (window.ActiveXObject)
		{
			var doc=new ActiveXObject("Microsoft.XMLDOM");
			doc.async="false";
			doc.loadXML(strXML);
		}
		// code for Mozilla, Firefox, Opera, etc.
		else
		{
			var parser=new DOMParser();
			var doc=parser.parseFromString(strXML,"text/xml");
		}// documentElement always represents the root node
		return doc;
	},
	_xml2string : function(xmlDom){
			var strs = null;
			var doc = xmlDom.documentElement;
			if(doc.xml == undefined){
				strs = (new XMLSerializer()).serializeToString(xmlDom);
			}else strs = doc.xml;
			return strs;
	},
	_parent : function(obj,elmStr){
		var pnode = obj.parentNode;
		while(pnode.nodeName.toLowerCase() != elmStr){
			pnode = pnode.parentNode;
		}
		return pnode;
	},
	get_nextsibling : function(n)
	{
		x = n.nextSibling;
		if(!x)return null;
		while (x.nodeType != 1)
		{
			x=x.nextSibling;
		}
		return x;
	},
	get_previoussibling : function(n)
	{
		x = n.previousSibling;
		if(!x)return null;
		while (x.nodeType!=1)
		{
			x=x.previousSibling;
		}
		return x;
	},
	SwapNodes : function(nodoA,arriba,doc){
		if(!doc)doc = document;
	    nodoB = (arriba) ? nodoA.previousSibling : nodoA.nextSibling;
	    while(nodoB && nodoB.nodeType != 1){
		    nodoB = (arriba) ?  nodoB.previousSibling : nodoB.nextSibling;
	    }
	    if(!nodoB || nodoB.nodeType != 1)return;
	    nodoAux = doc.createElement(nodoB.nodeName);
	    nodoA.parentNode.replaceChild(nodoAux,nodoA);
	    nodoB.parentNode.replaceChild(nodoA,nodoB);
	    nodoAux.parentNode.replaceChild(nodoB,nodoAux);
    },
	setText : function(nodo,str){
		var doc = nodo.ownerDocument;
		nodo.appendChild(doc.createTextNode(str));
	},
	_nodevalue : function(nodo){
		valor = (nodo.childNodes.length > 0)?nodo.firstChild.nodeValue:"";
		return valor;
	},
	_subnodevalue : function(nodo,nombre){
		var sbnodolist = nodo.getElementsByTagName(nombre);
		var valor = "";
		if(sbnodolist.length<=0){
			valor ="";
		}
		else{
			var sbnodo = sbnodolist[0];
			valor = this._nodevalue(sbnodo);
		}
		return valor;
		
	}

};