//==============================================================================
// FRAMEWORK - Javascript 
// Crafted by Goose!2006
//==============================================================================
// javascript:alert(principal.FWK.loadFrameInto('enlacesMod','principal'));

//==============================================================================
// INCLUSIONES 
//==============================================================================

/**
* Incluye archivos js
*/
	function fwk_requires()
	{
		sPath="/scripts/framework/js/";
		aRequires=Array(	"prototype.js",
											"scriptaculous.js"
											);
		for(a=0;a<aRequires.length;a++) document.write('<script type="text/javascript" src="'+sPath+aRequires[a]+'"></script>');
	} 

//==============================================================================
// FRIEND FUNCTIONS
//==============================================================================

//==============================================================================
// OBJETO FWK
//==============================================================================
/*
 *Esto se pone para cuando se usa el IRENDER (carga un frame dentro de otro)
 *las dos llamadas al framework.js no haga que se reinstancie y se limpie 
 *el mismo, haciendo que no ande la carga de los CSS dinamicas, etc.
 */
if(typeof(FWK)=="undefined")
var FWK =	
{
	debug: false, 
	browser : navigator.appName, //find the browser name
	//==============================================================================
	// AHAH 
	//==============================================================================
		//==============================================================================
		// LOAD
		//==============================================================================
			ahah_open_return	:	function (url)
													{
														try
														{
															var req;
															if(this.browser == "Microsoft Internet Explorer")	req = new ActiveXObject("Microsoft.XMLHTTP");
															else req = new XMLHttpRequest();
															if(req)
															{
																req.open("POST",url,false);
																req.send(null)
																if(req.status<400)	return(req.responseText);
																else								return("FWK:Error: "+req.statusText);
															}
														}
														catch(e) { window.status="ahah_open_return	["+e.message+"]"; }
													},
			ahah_open_get	:	function (url)
													{
														try
														{
															var req;
															if(this.browser == "Microsoft Internet Explorer")	req = new ActiveXObject("Microsoft.XMLHTTP");
															else req = new XMLHttpRequest();
															if(req)
															{
																req.open("GET",url,false);
																req.send(null)
																if(req.status<400)	return(req.responseText);
																else								return("FWK:Error: "+req.statusText);
															}
														}
														catch(e) { window.status="ahah_open_return	["+e.message+"]"; }
													},													
			ahah_open_set	:	function(url,target)
											{
												try
												{
													var req;
													var onStateChangeLocal=this.onStateChangeLocal;
													if(this.browser == "Microsoft Internet Explorer") req = new ActiveXObject("Microsoft.XMLHTTP");
													else req = new XMLHttpRequest();
													if(req)
													{
														req.onreadystatechange=function()
															{
																if(req.readyState==4)
																	if(req.status<400)
																	{
																		results=req.responseText;																		
																		// no funcionaba ni en IE ni en otros navegadores
																		// el this.debug da undefiniend en IE y que siempre es un objeto (true) en  otros nav.
																		//if(this.debug) alert(results);																		
																		//onStateChangeDebug=this.debug
																		document.getElementById(target).innerHTML=results;																		
																		
																		
																		if(FWK) if(FWK.onStateChange) FWK.onStateChange(FWK.debug,"end_aha",target); //puesto con FWK por que el this parece que dentro no funciona ya que el alert nunca se ve
																	}
																	else	document.getElementById(target).innerHTML="<font color='red'>FWK:Error: "+req.statusText+"<br/></font>"+document.getElementById(target).innerHTML;
															};
														req.open("POST",url,true);
														req.send(null)
													}
												}
												catch(e) { window.status="ahah_open_set	["+e.message+"]"; }
											},
			ahah_open_getcallback	:	function(url,callback,callwhile)
														{
															try
															{
																var req;
																if(this.browser == "Microsoft Internet Explorer") req = new ActiveXObject("Microsoft.XMLHTTP");
																else req = new XMLHttpRequest();
																if(req)
																{
																	if(!callwhile) callwhile="fwk_doNothing";
																	eval("callbackFunc=function () { if (req.readyState == 4) "+callback+"(req); else "+callwhile+"(req.readyState); };");
																	req.onreadystatechange=callbackFunc;
																	req.open("GET",url,true);
																	req.send(null)
																}
															}
															catch(e) { window.status="ahah_open_callback2 ["+e.message+"]"; }
														},											
			ahah_open_callback	:	function(url,callback,callwhile)
														{
															try
															{
																var req;
																if(this.browser == "Microsoft Internet Explorer") req = new ActiveXObject("Microsoft.XMLHTTP");
																else req = new XMLHttpRequest();
																if(req)
																{
																	if(!callwhile) callwhile="fwk_doNothing";
																	eval("callbackFunc=function () { if (req.readyState == 4) "+callback+"(req); else "+callwhile+"(req.readyState); };");
																	req.onreadystatechange=callbackFunc;
																	req.open("POST",url,true);
																	req.send(null)
																}
															}
															catch(e) { window.status="ahah_open_callback2 ["+e.message+"]"; }
														},
		//==============================================================================
		// POST
		//==============================================================================
			ahah_post_return	:	function (url,form)
													{
														try
														{
															var req;
															if(this.browser == "Microsoft Internet Explorer")	req = new ActiveXObject("Microsoft.XMLHTTP");
															else req = new XMLHttpRequest();
															if(req)
															{
																req.open("POST",url,false);
																req.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
																req.send(form)
																if(req.status<400)	return(req.responseText);
																else								return("FWK:Error: "+req.statusText);
															}
														}
														catch(e) { window.status="ahah_post_return	["+e.message+"]"; }
													},
			ahah_post_set	:	function(url,form,target)
											{
												try
												{
													var req;
													if(this.browser == "Microsoft Internet Explorer") req = new ActiveXObject("Microsoft.XMLHTTP");
													else req = new XMLHttpRequest();
													if(req)
													{
														req.onreadystatechange=function()
															{
																if(req.readyState==4)
																	if(req.status<400)
																	{
																		results=req.responseText;																		
																		// no funcionaba ni en IE ni en otros navegadores
																		// el this.debug da undefiniend en IE y que siempre es un objeto (true) en  otros nav.
																		//if(this.debug) alert(results);
																		document.getElementById(target).innerHTML=results;
																		
																		if(FWK) if(FWK.onStateChange) FWK.onStateChange(FWK.debug,"end_aha",target); //puesto con FWK por que el this parece que dentro no funciona ya que el alert nunca se ve
																	}
																	else	document.getElementById(target).innerHTML="<font color='red'>FWK:Error: "+req.statusText+"<br/></font>"+document.getElementById(target).innerHTML;
															};
														req.open("POST",url,true);
														req.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
														req.send(form)
													}
												}
												catch(e) { window.status="ahah_post_set	["+e.message+"]"; }
											},
			ahah_post_callback	:	function(url,form,callback,callwhile)
														{
															try
															{
																var req;
																if(this.browser == "Microsoft Internet Explorer") req = new ActiveXObject("Microsoft.XMLHTTP");
																else req = new XMLHttpRequest();
																if(req)
																{
																	if(!callwhile) callwhile="fwk_doNothing";
																	eval("callbackFunc=function () { if (req.readyState == 4) "+callback+"(req); else "+callwhile+"(req.readyState); };");
																	req.onreadystatechange=callbackFunc;
																	req.open("POST",url,true);
																	req.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
																	req.send(form)
																}
															}
															catch(e) { window.status="ahah_post_callback	["+e.message+"]"; }
														},
	//==============================================================================
	// LOAD
	//==============================================================================
		//==============================================================================
		// FRAME
		//==============================================================================
		loadFrameInto	: function(destino,frame,parametros,onstateParams)
									{
										url="/scripts/app/framework.php?FWKINLINE=1&FRAME="+frame+parametros;										
										if(this.debug) alert("url:["+url+"]\ndest:["+destino+"]\nparam:["+parametros+"]");
										if(this.onStateChange) this.onStateChange(this.debug,"begin",destino,frame,parametros,onstateParams);
										this.ahah_open_set(url,destino);
										if(this.onStateChange) this.onStateChange(this.debug,"end",destino,frame,parametros,onstateParams);
									},
		loadFrame			: function(frame,parametros)
									{
										url="/scripts/app/framework.php?FWKINLINE=1&FRAME="+frame+parametros;
										if(this.debug) alert("url:["+url+"]\nparam:["+parametros+"]");
										return(this.ahah_open_return(url,null));
									},
		loadFrameCallback	: function(callback,frame,parametros,callwhile)
									{
										url="/scripts/app/framework.php?FWKINLINE=1&FRAME="+frame+parametros;
										if(this.debug) alert("url:["+url+"]\nparam:["+parametros+"]");
										this.ahah_open_callback(url,callback,callwhile);
									},
		//==============================================================================
		// APP 
		//==============================================================================
		loadAppInto	: function(destino,app,parametros,onstateParams)
									{
										url="/scripts/app/framework.php?FWKINLINE=1&APP="+app+parametros;
										if(this.debug) alert("url:["+url+"]\ndest:["+destino+"]\napp:["+app+"]\nparam:["+parametros+"]");
										if(this.onStateChange) this.onStateChange(this.debug,"begin",destino,app,parametros,onstateParams);
										this.ahah_open_set(url,destino);
										if(this.onStateChange) this.onStateChange(this.debug,"end",destino,app,parametros,onstateParams);
									},
		reloadIsNeed	: function (new_sitepage)
									{										
										//Esta funcion se implemento para evitar tener que propagar el Reload de Banners y de conteo de metricas por todas las aplicaciones
										//Esta vinculada con loadAppInto y loadFrameInto que al ser usadas por las aplicaciones de forma transparente realizan el refresco										
										if(FWK.debug) alert(" FWK :: reloadIsNeed Sitepage:"+new_sitepage);
										try{
											top.pageNum=undefined;  //para evitar cache en accipiter se limpia el random
											if(typeof BANNERS!= "undefined"){if(BANNERS.reload){BANNERS.reload();}}
											if(typeof METRICAS!= "undefined"){if(METRICAS.reload){METRICAS.reload();}}	
										}catch(er){
											window.status="Er";
										}
									},
		loadApp			: function(app,parametros)
									{
										url="/scripts/app/framework.php?APP="+app+parametros;
										if(this.debug) alert("url:["+url+"]\napp:["+app+"]\nparam:["+parametros+"]");
										return(this.ahah_open_return(url,null));
									},
		loadAppCallback	: function(callback,app,parametros,callwhile)
									{
										url="/scripts/app/framework.php?APP="+app+parametros;
										if(this.debug) alert("url:["+url+"]\napp:["+app+"]\nparam:["+parametros+"]");
										this.ahah_open_callback(url,callback,callwhile);
									},
		//==============================================================================
		// API
		//==============================================================================
		loadApiInto	: function(destino,app,token,parametros)
									{
										url="/scripts/app/framework.php?APP="+app+"&API="+token+parametros;
										if(this.debug) alert("url:["+url+"]\ndest:["+destino+"]\napp:["+app+"]\nparam:["+parametros+"]");
										this.ahah_open_set(url,destino);
									},
		loadApi			: function(app,token,parametros)
									{
										url="/scripts/app/framework.php?APP="+app+"&API="+token+parametros;
										if(this.debug) alert("url:["+url+"]\napp:["+app+"]\nparam:["+parametros+"]");
										return(this.ahah_open_return(url,null));
									},
		loadApiCallback	: function(callback,app,token,parametros,callwhile)
									{
										url="/scripts/app/framework.php?APP="+app+"&API="+token+parametros;
										if(this.debug) alert("url:["+url+"]\ntoken:["+token+"]\napp:["+app+"]\nparam:["+parametros+"]");
										this.ahah_open_callback(url,callback,callwhile);
									},

	//==============================================================================
	// LOAD
	//==============================================================================
		//==============================================================================
		// FRAME
		//==============================================================================
		postFrameInto	: function(destino,frame,pform,parametros,onstateParams)
									{
										url="/scripts/app/framework.php";
										sform="FWKINLINE=1&FRAME="+frame+parametros+"&"+this.getFormValues(document.getElementById(pform));
										if(this.debug) alert("url:["+url+"]\ndest:["+destino+"]\napp:["+app+"]\nparam:["+parametros+"]\n\nPOST=\n-----------\n"+sform+"\n-----------");
										if(this.onStateChange) this.onStateChange(this.debug,"begin",destino,app,parametros,onstateParams);
										this.ahah_post_set(url,sform,destino);
										if(this.onStateChange) this.onStateChange(this.debug,"end",destino,app,parametros,onstateParams);
										this.reloadIsNeed();
									},
		postFrame			: function(frame,pform,parametros)
									{
										url="/scripts/app/framework.php";
										sform="FWKINLINE=1&FRAME="+frame+parametros+"&"+this.getFormValues(document.getElementById(pform));
										if(this.debug) alert("url:["+url+"]\napp:["+app+"]\nparam:["+parametros+"]\n\nPOST=\n-----------\n"+sform+"\n-----------");
										return(this.ahah_open_return(url,sform));
									},
		postFrameCallback	: function(callback,frame,pform,parametros,callwhile)
									{
										url="/scripts/app/framework.php?";
										sform="FWKINLINE=1&FRAME="+frame+parametros+"&"+this.getFormValues(document.getElementById(pform));
										if(this.debug) alert("url:["+url+"]\napp:["+app+"]\nparam:["+parametros+"]\n\nPOST=\n-----------\n"+sform+"\n-----------");
										this.ahah_post_callback(url,sform,callback,callwhile);
									},
		//==============================================================================
		// APP 
		//==============================================================================
		postAppInto	: function(destino,app,pform,parametros,onstateParams)
									{
										url="/scripts/app/framework.php";
										sform="FWKINLINE=1&APP="+app+parametros+"&"+this.getFormValues(document.getElementById(pform));
										if(this.debug) alert("url:["+url+"]\ndest:["+destino+"]\napp:["+app+"]\nparam:["+parametros+"]\n\nPOST=\n-----------\n"+sform+"\n-----------");
										if(this.onStateChange) this.onStateChange(this.debug,"begin",destino,app,parametros,onstateParams);
										this.ahah_post_set(url,sform,destino);
										if(this.onStateChange) this.onStateChange(this.debug,"end",destino,app,parametros,onstateParams);
									},
		postApp			: function(app,pform,parametros)
									{
										url="/scripts/app/framework.php?";
										sform="APP="+app+parametros+"&"+this.getFormValues(document.getElementById(pform));
										if(this.debug) alert("url:["+url+"]\napp:["+app+"]\nparam:["+parametros+"]\n\nPOST=\n-----------\n"+sform+"\n-----------");
										return(this.ahah_post_return(url,sform));
									},
		postAppCallback	: function(callback,app,pform,parametros,callwhile)
									{
										url="/scripts/app/framework.php?";
										sform="APP="+app+parametros+"&"+this.getFormValues(document.getElementById(pform));
										if(this.debug) alert("url:["+url+"]\napp:["+app+"]\nparam:["+parametros+"]\n\nPOST=\n-----------\n"+sform+"\n-----------");
										this.ahah_post_callback(url,sform,callback,callwhile);
									},
		//==============================================================================
		// API
		//==============================================================================
		postApiInto	: function(destino,app,token,pform,parametros)
									{
										url="/scripts/app/framework.php?APP="+app+"&API="+token+parametros;
										sform="APP="+app+"&API="+token+parametros+"&"+this.getFormValues(document.getElementById(pform));
										if(this.debug) alert("url:["+url+"]\ndest:["+destino+"]\napp:["+app+"]\nparam:["+parametros+"]\n\nPOST=\n-----------\n"+sform+"\n-----------");
										this.ahah_post_set(url,sform,destino);
									},
		postApi			: function(app,token,pform,parametros)
									{
										url="/scripts/app/framework.php";
										sform="APP="+app+"&API="+token+parametros+"&"+this.getFormValues(document.getElementById(pform));
										if(this.debug) alert("url:["+url+"]\napp:["+app+"]\nparam:["+parametros+"]\n\nPOST=\n-----------\n"+sform+"\n-----------");
										return(this.ahah_post_return(url,sform));
									},
		postApiCallback	: function(callback,app,token,pform,parametros,callwhile)
									{
										url="/scripts/app/framework.php?APP="+app+"&API="+token+parametros;
										sform="APP="+app+"&API="+token+parametros+"&"+this.getFormValues(document.getElementById(pform));
										if(this.debug) alert("url:["+url+"]\ntoken:["+token+"]\napp:["+app+"]\nparam:["+parametros+"]\n\nPOST=\n-----------\n"+sform+"\n-----------");
										this.ahah_post_callback(url,sform,callback,callwhile);
									},
									
	//==============================================================================
	// UTILIDADES
	//==============================================================================
		getFormValues	:	function(fobj)
										{
											var str = "";
											for(var i = 0;i < fobj.elements.length;i++)
											{
												switch(fobj.elements[i].type)
												{
													case "password":
													case "textarea":
													case "hidden":
													case "text": str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&"; break;
													case "select-one": str += fobj.elements[i].name + "=" + escape(fobj.elements[i].options[fobj.elements[i].selectedIndex].value) + "&"; break;
													case "checkbox": if(fobj.elements[i].checked) str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&"; break;
													case "radio": if(fobj.elements[i].checked) str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&"; break;
												}
											}
											str = str.substr(0,(str.length - 1));
											if(this.debug) alert(str);
											return str;
										}

};

function	fwk_doNothing(estado) {  } //Esta funcion es el default de los "callwhile"
function	ltrim(s)	//trimeo de enters en respuestas de ajax
{
	i=0; while(s.charAt(i)=='\n'||s.charAt(i)=='\r'||s.charAt(i)==' ') i++;
	return(s.substr(i));
}

//==============================================================================
// OBJETO CSS
//==============================================================================

CSS=
{
	addCss: function(archivo,titulo,alternate)
			{
				if(this.debug) alert("::CSS>> addCss archivo=["+archivo+"] titulo=["+titulo+"]");
				if(!this.existCss(archivo))
				{
					var oHead = document.getElementsByTagName("HEAD")[0];
					var oLink = document.createElement("link");
					oLink.attributes.setNamedItem(this.crearAtributo("href",archivo));
					if(alternate)	oLink.attributes.setNamedItem(this.crearAtributo("rel","alternate stylesheet"));
					else					oLink.attributes.setNamedItem(this.crearAtributo("rel","stylesheet"));
					oLink.attributes.setNamedItem(this.crearAtributo("type","text/css"));
					oLink.attributes.setNamedItem(this.crearAtributo("title",titulo));
					oLink.attributes.setNamedItem(this.crearAtributo("id",titulo));
					if(this.debug) alert(oLink.outerHTML);
					oHead.appendChild(oLink);
					if(this.debug) alert(oHead.innerHTML);
					if(alternate) { this.turnCss(titulo,true); this.turnCss(titulo,false); } //bugfix para que no se prenda
				}
				else if(this.debug) alert("::CSS>> addCss archivo=["+archivo+"] titulo=["+titulo+"] --- YA EXISTIA !!!");
			},
	crearAtributo: function(nombre,valor)
			{
				var nItem = document.createAttribute(nombre);
				nItem.value = valor;
				return(nItem);
			},
	removeCss: function(archivo,titulo)
			{
				if(this.debug) alert("::CSS>> removeCss archivo=["+archivo+"] titulo=["+titulo+"]");
				var aLinks = document.getElementsByTagName("LINK");
				if(titulo==null)	
					for (i=0;i<aLinks.length;i++) 
					{ 
						if(aLinks[i].href==archivo) 
						{ 
								//this.turnCss(aLinks[i].title,false); 
								aLinks[i].removeNode(true); 
						} 
					}
				else							
					for (i=0;i<aLinks.length;i++) 
					{ 
						if(aLinks[i].title==titulo) 
						{ 
							//this.turnCss(aLinks[i].title,false); 
							aLinks[i].removeNode(true); 
						} 
					}
			},
	existCss: function(archivo)
			{
				if(this.debug) alert("::CSS>> existCss archivo=["+archivo+"]");
				for (i=0;i<document.styleSheets.length;i++) if(document.styleSheets(i).href==archivo) return(true);
				return(false);
			},
	turnCss: function(nom,estado)
			{
				if(this.debug) alert("::CSS>> TurnCss nom=["+nom+"] estado=["+estado+"]");
				estado=!estado;
				for (i=0;i<document.styleSheets.length;i++)
				{
					nombre=document.styleSheets(i).title;
					nombre=nombre.substr(0,nom.length);
					if(nom==nombre) { 
						//alert('OOOOO la encontre-----'+nom); 
						document.styleSheets(i).disabled=estado; return; }
				}
			},
	setCss: function(nom)
			{
				for (i=0;i<document.styleSheets.length;i++)
				{
					if(document.styleSheets(i).title==nom)	document.styleSheets(i).disabled=false;
					else																		document.styleSheets(i).disabled=true;
				}
			},
	getCssActivos: function()
			{
				aEstilos=new Array;
				if(this.debug) alert("::CSS>> Total de styles: "+document.styleSheets.length);
				for (i=0;i<document.styleSheets.length;i++) 
				{
					if(!(document.styleSheets(i).disabled))
					{
						if(this.debug) alert("::CSS>> Nro: "+i+" ACTIVO >> "+document.styleSheets(i).title);
						aEstilos.push(document.styleSheets(i).title);
					}
				}
				return(aEstilos);
			}
}

//==============================================================================
// LOAD 
//==============================================================================

//fwk_requires();
//alert("jsFramework\n\nLOAD r6.1.008\nPOST r1.0.000\nCSS r1.0.027");
