var TTipos = {
	ajax: 1,
	normal: 2,
	script: 3
};

var TReadyStates = {
	UNINITIALIZED:0,
	LOADING:1,
	LOADED:2,
	INTERACTIVE:3,
	COMPLETED:4
};

var TStatus = {
	CONTINUE:100,
	SWITCHING_PROTOCOLS:101,
	OK:200,
	CREATED:201,
	ACCEPTED:202,
	NON_AUTHORITATIVE_INFORMATION:203,
	NO_CONTENT:204,
	RESET_CONTENT:205,
	PARTIAL_CONTENT:206,
	MULTIPLE_CHOICES:300,
	MOVED_PERMANENTLY:301,
	FOUND:302,
	SEE_OTHER:303,
	NOT_MODIFIED:304,
	USE_PROXY:305,
	TEMPORARY_REDIRECT:307,
	BAD_REQUEST:400,
	UNAUTHORIZED:401,
	PAYMENT_REQUIRED:402,
	FORBIDDEN:403,
	NOT_FOUND:404,
	METHOD_NOT_ALLOWED:405,
	NOT_ACCEPTABLE:406,
	PROXY_AUTHENTICATION_REQUIRED:407,
	REQUEST_TIMEOUT:408,
	CONFLICT:409,
	GONE:410,
	LENGTH_REQUIRED:411,
	PRECONDITION_FAILED:412,
	REQUEST_ENTITY_TOO_LARGE:413,
	REQUEST_URI_TOO_LONG:414,
	UNSUPPORTED_MEDIA_TYPE:415,
	REQUESTED_RANGE_NOT_SUITABLE:416,
	EXPECTATION_FAILED:417,
	INTERNAL_SERVER_ERROR:500,
	NOT_IMPLEMENTED:501,
	BAD_GATEWAY:502,
	SERVICE_UNAVAILABLE:503,
	GATEWAY_TIMEOUT:504,
	HTTP_VERSION_NOT_SUPPORTED:505
};

TTargets = {
	_self:"_self",_blank:"_blank"
}

function TLink(obj){
	this.id 	=	obj.id?		obj.id	:	0;
	this.src	= 	obj.src? 	obj.src : 	"";
	this.params	=	obj.params? obj.params:	"";
	this.method	=	obj.method?	obj.method:	"POST";

	this.history=	obj.history?obj.history:true;

	this.type	=	obj.type?	obj.type:	TTipos.ajax;

	this.func	=	obj.func?	obj.func:	null;
	this.onstart=	obj.onstart?obj.onstart:null;
	this.onend	=	obj.onend?	obj.onend:	null;

	this.extern	=	obj.extern?	obj.extern:	false;

	this.handle	=	obj.handle?	obj.handle:	BUSCAR_POR_ID(this.id);
	this.content=	obj.content?obj.content:false;

	this.target	=	obj.target?	obj.target:	TTargets._self;

	this.title	=	obj.title?	obj.title:	window.location;
	this.form	=	obj.form?	obj.form:	false;
	this.select	=	obj.select?	obj.select:	false;
	this.input	=	obj.input?	obj.input:	false;


	this.status = 0;
	this.result = null;
	this.xml = null;

	this.toStr = function(){
		var str = "id="+this.id+"&";
		str+="title="+this.title+"&";
		str+="src="+this.src+"&";
		str+="type="+this.type+"&";
		str+="params="+this.params+"&";
		str+="method="+this.method+"&";
		str+="history="+this.history+"&";
		str+="func="+this.func+"&";
		str+="onstart="+this.onstart+"&";
		str+="onend="+this.onend+"&";
		str+=this.content?"content="+this.content.id+"&":"";
		str+="target="+this.target;

		return str;
	};

	this.append = function(newObj){
		for(x in newObj) this[x] = newObj[x];
		return this;
	};


	if(this.handle){
		if(this.src == "" && this.handle.href){
			var arrSrc = this.handle.href.split("?");

			this.src= arrSrc[0]?arrSrc[0]:"";
			this.params= arrSrc[1]?arrSrc[1]:"";
		}

		if(this.handle.target && this.handle.target.toLowerCase() == TTargets._blank){
			this.target = TTargets._blank;
		}

//		this.handle.onclick=$$.doEvent;
//alert(this.toStr());
		if (this.form) {
			addOnSubmitEvent(this.id,$$.doEvent);
		} else if (this.input || this.select) {
			addOnChangeEvent(this.id,$$.doEvent);
		} else {
			addOnClickEvent(this.id,$$.doEvent);
		}
	}

	if(this.type == TTipos.ajax &&  this.extern == true && this.src != $$.externa){

		this.params = "url="+this.src+"&"+this.params;
		this.src = $$.externa;
 	}


	if(this.content){
		if(typeof this.content == "string"){
			this.content = BUSCAR_POR_ID(this.content);
		}
	}
	return this;
}

function TLinks(){
	this.extent = 0;
	this.length = 0;
	this.add = TLinks_add;
  	this.validate = TLinks_validate;
	this.doEvent = TLinks_doEvent;
	this.byId = TLinks_byId;
	this.action = function(obj){
		if(obj.content){
 			if(!obj.content.tagName)obj.content =  BUSCAR_POR_ID(obj.content);
		}
		if(obj.history == true)	$$.history.add(obj);

		if(obj.type == TTipos.normal){
			$$.changePage(obj);
		}else if(obj.type == TTipos.ajax || obj.type == TTipos.script){
			obj.result = false;
			obj.xml = false;
			obj.status = 0;

			$$.ajax (obj);
		}
	};
	this.ajax = TLinks_ajax;
	this.changePage = TLinks_changePage;
	this.externa = "linkExterno.php";
	return this;
}

function TLinks_add(obj){
	if(!obj || !obj.id) return false;
	tmp = this.byId(obj.id);

	if(tmp) obj = tmp.append(obj);

	obj = this.validate(obj);

	if(!obj) return false;
	if(!tmp) {
		this[this.length] = obj;
		this.length = this.length+1;
		return this[this.length-1];
	}else{
		return obj;
	}
}

function TLinks_doEvent(ev){
	var ev = ev?ev:window.event;
	var id = EsIE()?ev.srcElement.id:ev.target.id;

	ev.returnValue = false;
	var obj = $$.byId(id);

	if(obj){$$.action(obj);}

	return false;
}

function TLinks_validate(obj){
 	if(!obj) return false;

 	return new TLink(obj);

}
function TLinks_byId(id){
	var obj = false;
	for(i=0;i<this.length;i++){
		if(this[i].id == id){
			obj = this[i];
			break;
		}
	}
	return obj;
}

function TLinks_ajax(obj){
	var xml = false;

	try {
    	xml = new XMLHttpRequest();
	} catch (e) {
    	var a = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0',
	      		'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'MICROSOFT.XMLHTTP.1.0',
    	  		'MICROSOFT.XMLHTTP.1', 'MICROSOFT.XMLHTTP'];
		for (var i = 0; i < a.length; i++) {
			try {
				xml = new ActiveXObject(a[i]);
				break;
			}catch (e) {}
		}
	}

	if (!xml) {alert("Tu navegador no soporta AJAX"); return false;  }
	
 	obj.status = xml.readyState;
	if(obj.onstart){obj.onstart(xml,obj);}

	bComplete = false;
	if (obj.select)
		obj.method = "POST";
	obj.method = obj.method.toUpperCase();
	try{
		xml.abort();
	}catch(ex){alert(ex.description?ex.description:ex);}

	try{
		if (obj.form){
			var frm =BUSCAR_POR_ID(obj.id);
			var numberElements = frm.elements.length;
			for(var i = 0; i < numberElements; i++) {
				if (obj.params)
					obj.params+="&";
				obj.params+=frm.elements[i].name+"="+
				encodeURIComponent(frm.elements[i].value);
			}
		}
		if (obj.select) {
			var sel = BUSCAR_POR_ID(obj.id);
			var frm = getFormFromElement(sel);
			var numberElements = frm.elements.length;
			for (var i = 0; i < numberElements; i++) {
				if (obj.params)
					obj.params+="&";
				obj.params+=frm.elements[i].name+"="
					+encodeURIComponent(frm.elements[i].value);
			}
//			alert(obj.params);
		}
		if (obj.input) {
			var inp = BUSCAR_POR_ID(obj.id);
			var frm = getFormFromElement(inp);
			var numberElements = frm.elements.length;
			for (var i = 0; i < numberElements; i++) {
				if (obj.params)
					obj.params+="&";
				obj.params+=frm.elements[i].name+"="
					+encodeURIComponent(frm.elements[i].value);
			}
		}

		if(obj.method == "GET") {
			try{
				xml.open(obj.method, obj.src+"?"+obj.params, true);
				obj.params="";
			}catch(ex){alert(ex.description?ex.description:ex);}
		}else {
			try{
 				xml.open(obj.method, obj.src, true);
				xml.setRequestHeader("Method", obj.method+" "+obj.src+" HTTP/1.1");
				xml.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			}catch(ex){alert(ex.description?ex.description:ex);}

		}
		obj.status = xml.readyState;

		xml.onreadystatechange = function(){
			if (xml.readyState == TReadyStates.COMPLETED && !bComplete){
 				bComplete = true;

				obj.xml = xml;
				obj.result = obj.xml.responseText;
				obj.status = obj.xml.readyState;

				if(obj.title.length > 0) window.document.title = obj.title;

				if(obj.func) obj.func(xml,obj);

				if(obj.content) {
					if(obj.content.innerHTML)	obj.content.innerHTML =  obj.xml.responseText;
					else if(obj.content.value)	obj.content.value = obj.xml.responseText;
				}


				if(obj.type == TTipos.script){
					try{
						eval(obj.xml.responseText);
					}catch(ex){alert(ex.description?ex.description:ex)}
				}

				if(obj.onend){obj.onend(xml,obj);}
			}
		};

		xml.send(obj.params);

	}catch(ex){  return false; }

	return this;

}
function TLinks_changePage(obj){
	if(obj.onstart){obj.onstart(obj);}
	var src = obj.src + (obj.params.length>0?"?":"")+obj.params;



 	if(obj.target == TTargets._self){
 		if(obj.content.tagName == "IFRAME"){
			obj.content.src = src;
		}else if(obj.content.tagName == ""){

		}else{
			window.location = src;
		}
	}else{
		window.open(src);
	}
}
function getFormFromElement(objSel) {
	objParent = objSel.parentNode;
	while (objParent.tagName.toUpperCase() != "FORM") {
		objParent = objParent.parentNode;
	}
	return objParent;
}

////////////////BOTON BACK

//CREANDO EL OBJETO $$
$$ = new TLinks();


$$.history = {
	page:null,
	obj:null,
	ifrm_accion:null,
	loaded:false,
	load:function(loadPost){
		try{
			$$.history.page = "historial.php";
			var ifrm =  document.getElementById("ifrm_accion");

			if(!ifrm){
				ifrm = document.createElement("IFRAME");
				ifrm.width=800;
				ifrm.height=300;
				ifrm.id="ifrm_accion";
				ifrm.style.display = "none";
				ifrm.src="";

				document.body.appendChild(ifrm);
			}
			$$.history.ifrm_accion = ifrm;

		}catch(e){ alert(ex.description?ex.description:ex);}

		if(loadPost == false)	$$.history.add({id:""});

		$$.history.loaded = true;
	},

	set:function(obj){
		$$.history.obj = obj;
	},

	add:function(obj){

		if(!$$.history.loaded){
 			$$.history.load(true);
		}

		$$.history.set(obj);

		$$.history.navigate();
 	},

	navigate:function(){
		try{
			var strParams="";
			for(x in $$.history.obj){
				strParams+=x+"="+$$.history.obj[x]+"&";
			}

			$$.history.ifrm_accion.src=$$.history.page+"?"+$$.history.obj.toStr();//strParams;
		}catch(ex){alert(ex.description?ex.description:ex);}
	},

	validate:function(obj){
 		var obj_link = $$.byId(obj.id);

		try{
			if(obj_link.id != $$.history.obj.id){
 				$$.history.obj = obj_link;
				$$.history.exec();
			}
		}catch(ex){alert(ex.description?ex.description:ex);}
	},

	exec:function(){
		try{
			if($$.history.obj){
				$$.action($$.history.obj);
			}
		}catch(ex){
			alert(ex.description?ex.description:ex);
		}
	}
} ;

