SCRIPT ={
	debug: false, 
	sdebug: "",
	COUNTREF: 0,
	EVALERROR: new Array(),
	SCRIPTCALLBACK: undefined,
	SCRIPTNUEVOS: new Array(),
	SCRIPTCURRENT: 0,
	addScript: function (src,id,language){
		var str;
		if(typeof language=="undefined"){
			language="javascript";
		}
		if(typeof id=="undefined"){
			str="<script language='"+language+"' src='"+src+"'></"+"script>";
		}else{
			str="<script language='"+language+"' id='"+id+"' src='"+src+"'></"+"script>";
		}
		this.scriptCheckText(str);	
	},
	scriptCheckText: function (texto,callback){
		this.scriptAlert("scriptCheckText","call",1);	
		var regresar=true;
		if(texto){
			this.scriptAlert("scriptCheckText","Texto: "+texto,2);
			texto=texto.replace(/\n/g,"#@NL@#");
			var oScriptsNuevos=texto.match(/<[ \t]*script[^>]*?>.*?<\/[ \t]*script[ \t]*>/gim);
			this.scriptAlert("scriptCheckText","Nuevos: "+oScriptsNuevos,2);			
			if(oScriptsNuevos&&oScriptsNuevos.length){
				this.SCRIPTCALLBACK=callback;
				this.SCRIPTNUEVOS=oScriptsNuevos;
				this.SCRIPTCURRENT=0;
				regresar=false;								
			}else{				
				this.scriptAlert("scriptCheckText","NoScripts",1);
			}
		}else{
			this.scriptAlert("scriptCheckText","NoTexto",1);
			
		}
		this.scriptAlert("scriptCheckText","End",1);			
		if(regresar){
			callback();
		}else{
			this.scriptAsyncCheckText();
		}
	},
	scriptAsyncCheckText: function(){
		this.scriptAlert("scriptAsyncCheckText","call",1);
		var terminarejecucion=false;
		if(this.SCRIPTNUEVOS){
			this.scriptAlert("scriptAsyncCheckText","ScriptCurrent:"+this.SCRIPTCURRENT,1);
			this.scriptAlert("scriptAsyncCheckText","ScriptNuevos:"+this.SCRIPTNUEVOS.length,1);			
			if(this.SCRIPTCURRENT<this.SCRIPTNUEVOS.length){
				if(this.SCRIPTNUEVOS[this.SCRIPTCURRENT]){
					this.SCRIPTCURRENT++;
					this.scriptAsyncCheckTextProcess(this.SCRIPTNUEVOS[this.SCRIPTCURRENT-1]);						
				}
			}else{				
				terminarejecucion=true;
				
			}
		}else{
			terminarejecucion=true;
		}
		this.scriptAlert("scriptAsyncCheckText","Terminar ejecucion : "+terminarejecucion,1);
		if(terminarejecucion){
			this.scriptAsyncCheckTextEnd();			
		}
	},
	scriptAsyncCheckTextProcess: function(oScriptsNuevos){
		this.scriptAlert("scriptAsyncCheckTextProcess","Call",1);
		var oHead=document.getElementsByTagName("HEAD")[0];
		var oScriptNuevo;
		var bAgregar=true;				
		oScriptsNuevos_info=this.scriptParseTag(oScriptsNuevos);
		if(!oScriptsNuevos_info.defer){
			this.scriptAlert("scriptAsyncCheckTextProcess","Src:"+oScriptsNuevos_info.src,1);
			if(oScriptsNuevos_info.src){ //&&PASO==0
				if(!this.scriptIsLoaded(oScriptsNuevos_info.src,oScriptsNuevos_info.id)){
					try{
						if(oScriptsNuevos_info.id){
							this.scriptAlert("scriptAsyncCheckTextProcess","DO Try Change",1);
							if(document.getElementById(oScriptsNuevos_info.id)){
								this.scriptAlert("scriptAsyncCheckTextProcess","DO  Changed",1);
								document.getElementById(oScriptsNuevos_info.id).src=oScriptsNuevos_info.src;
								bAgregar=false;
							}
						}
						if(bAgregar){
							this.scriptAlert("scriptAsyncCheckTextProcess","DO  Adding",1);
							oScriptNuevo=document.createElement("SCRIPT");
							oScriptNuevo.src=oScriptsNuevos_info.src;
							if(oScriptsNuevos_info.language)
								oScriptNuevo.language=oScriptsNuevos_info.language;
							if(oScriptsNuevos_info.type)
								oScriptNuevo.type=oScriptsNuevos_info.type;
							if(oScriptsNuevos_info.eventfor)
								oScriptNuevo.setAttribute("for",oScriptsNuevos_info.eventfor);
							if(oScriptsNuevos_info.eventevent)
								oScriptNuevo.setAttribute("event",oScriptsNuevos_info.eventevent);
							/*Atachar un manager de count reference*/
							//oScriptNuevo.onreadystatechange=function (){SCRIPT.attachRef(oScriptNuevo);};
							//this.COUNTREF++;
							//oHead.appendChild(oScriptNuevo);
							//this.appendChildAjax(oHead,oScriptNuevo);
							/**/
							var sScriptContenido;
							var oScript=oScriptNuevo;
							if(oScript.language=="vbscript"){
								this.scriptAlert("scriptAsyncCheckTextProcess","DO  Adding in Head",1);
								oHead.appendChild(oScript);
							}else{
								if(oScript.src){				
									this.scriptAlert("scriptAsyncCheckTextProcess","DO  request async of js",1);
									FWK.ahah_open_getcallback(oScript.src,"SCRIPT.scriptAsyncCheckTextProcessEval");
									return;
									/*
									sScriptContenido=FWK.ahah_open_get(oScript.src,null);
									if(sScriptContenido){
										this.scriptAlert("appendChildAjax","DO  Eval",1);
										//prompt("","SCRIPT SRC:"+oScript.src+"\n"+sScriptContenido,"");
										eval(sScriptContenido);
										if(typeof cargarPlayListDefault!="undefined"){
											//alert("FUNCION LOCAL: "+cargarPlayListDefault);
										}
										//alert("FUNCION GLOBAL: "+window.cargarPlayListDefault);
									}
									*/
								}		
							}										
						}
					}catch(e){
						this.scriptAlert("scriptCheck","Exception "+e.name+","+e.message,1);
					}
				}else{
					this.scriptAlert("scriptCheck","DO  Ign Already Loading",1);
				}
			}else if(oScriptsNuevos_info.text&&!oScriptsNuevos_info.src){ //&&PASO==1
				this.scriptAlert("scriptCheck","DO  Interpret",1);
				if(oScriptsNuevos_info.eventevent||oScriptsNuevos_info.eventfor){
					if(oScriptsNuevos_info.eventevent&&oScriptsNuevos_info.eventfor&&oScriptsNuevos_info.text){
						if(document.getElementById(oScriptsNuevos_info.eventfor)){
							this.scriptAlert("scriptCheck","DO  Attach for:"+oScriptsNuevos_info.eventfor+",event:"+oScriptsNuevos_info.eventevent,1);
							document.getElementById(oScriptsNuevos_info.eventfor).attachEvent(oScriptsNuevos_info.eventevent,function () { eval(oScriptsNuevos_info.text);});
						}
					}
				}else{
					try{
						this.scriptAlert("scriptCheck","DO  Eval ing",1);
						self.eval(oScriptsNuevos_info.text);
					}catch(e){
						this.scriptAlert("scriptCheck","DO  Add to error on eval",1);
						this.EVALERROR.push(oScriptsNuevos_info);
					}
				}
			}else{
				this.scriptAlert("scriptCheck ","DO  Nada PASO:"+PASO,1);
			}
		}else{
			this.scriptAlert("scriptCheck","DO  IgnDefer",1);
		}
		this.scriptAsyncCheckText();
	},
	scriptAsyncCheckTextProcessEval: function(req){
		this.scriptAlert("scriptAsyncCheckTextProcessEval","Request async OK eval",1);		
		eval(req.responseText);
		this.scriptAlert("checkPLAYLIST","cargarPlayListContenido:"+window.cargarPlayListContenido);
		this.scriptAlert("checkPLAYLIST","cargarPlayListContenido:"+typeof cargarPlayListContenido);
		setTimeout("SCRIPT.scriptAsyncCheckText();",100);
	},
	scriptAsyncCheckTextEnd: function(){
		this.scriptAlert("scriptAsyncCheckTextEnd","Finalizacion, reingresar al flujo",1);		
		if(this.SCRIPTCALLBACK){
			this.SCRIPTCALLBACK();
		}
	},
	scriptCheckTextOld: function (texto){
		this.scriptAlert("scriptCheckTEXT","call",1);
		var oHead=document.getElementsByTagName("HEAD")[0];
		var oScriptNuevo;
		var bAgregar;
		if(texto){
			this.scriptAlert("scriptCheckTEXT","Texto: "+texto,2);
			texto=texto.replace(/\n/g,"#@NL@#");
			var oScriptsNuevos=texto.match(/<[ \t]*script[^>]*?>.*?<\/[ \t]*script[ \t]*>/gim);
			this.scriptAlert("scriptCheckTEXT","Nuevos: "+oScriptsNuevos,2);
			var oScriptsNuevos_info;
			if(oScriptsNuevos&&oScriptsNuevos.length){
				this.scriptAlert("scriptCheckTEXT","Cantidad n "+oScriptsNuevos.length,1);
				for(var PASO=0;PASO<2;PASO++){
				for(var j=0;j<oScriptsNuevos.length;j++){
					this.scriptAlert("scriptCheckTEXT","Iteration j "+j,1);
					bAgregar=true;
					oScriptsNuevos_info=this.scriptParseTag(oScriptsNuevos[j]);
					if(!oScriptsNuevos_info.defer){
						this.scriptAlert("scriptCheckTEXT","Src:"+oScriptsNuevos_info.src,1);
						if(oScriptsNuevos_info.src&&PASO==0){
							if(!this.scriptIsLoaded(oScriptsNuevos_info.src,oScriptsNuevos_info.id)){
								try{
									if(oScriptsNuevos_info.id){
										this.scriptAlert("scriptCheckTEXT","DO Try Change",1);
										if(document.getElementById(oScriptsNuevos_info.id)){
											this.scriptAlert("scriptCheckTEXT","DO  Changed",1);
											document.getElementById(oScriptsNuevos_info.id).src=oScriptsNuevos_info.src;
											bAgregar=false;
										}
									}
									if(bAgregar){
										this.scriptAlert("scriptCheckTEXT","DO  Adding",1);
										oScriptNuevo=document.createElement("SCRIPT");
										oScriptNuevo.src=oScriptsNuevos_info.src;
										if(oScriptsNuevos_info.language)
											oScriptNuevo.language=oScriptsNuevos_info.language;
										if(oScriptsNuevos_info.type)
											oScriptNuevo.type=oScriptsNuevos_info.type;
										if(oScriptsNuevos_info.eventfor)
											oScriptNuevo.setAttribute("for",oScriptsNuevos_info.eventfor);
										if(oScriptsNuevos_info.eventevent)
											oScriptNuevo.setAttribute("event",oScriptsNuevos_info.eventevent);
										/*Atachar un manager de count reference*/
										oScriptNuevo.onreadystatechange=function (){SCRIPT.attachRef(oScriptNuevo);};
										this.COUNTREF++;
										//oHead.appendChild(oScriptNuevo);
										//this.appendChildAjax(oHead,oScriptNuevo);
										/**/
										var sScriptContenido;
										var oScript=oScriptNuevo;
										if(oScript.language=="vbscript"){
											this.scriptAlert("appendChildAjax","DO  Adding in Head",1);
											oHead.appendChild(oScript);
										}else{
											if(oScript.src){				
												sScriptContenido=FWK.ahah_open_get(oScript.src,null);
												if(sScriptContenido){
													this.scriptAlert("appendChildAjax","DO  Eval",1);
													//prompt("","SCRIPT SRC:"+oScript.src+"\n"+sScriptContenido,"");
													eval(sScriptContenido);
													if(typeof cargarPlayListDefault!="undefined"){
														//alert("FUNCION LOCAL: "+cargarPlayListDefault);
													}
													//alert("FUNCION GLOBAL: "+window.cargarPlayListDefault);
												}
											}		
										}										
										/**/
										/*Esperar hasta 5 segundos que cargue el JS*/
										/*
										var waitTimeStart=new Date().getTime();
										var waitTimeEnd=waitTimeStart;
										while(oScriptNuevo.readyState!="complete"&&oScriptNuevo.readyState!="loaded"&&(waitTimeEnd-waitTimeStart)<5000){
											waitTimeEnd=new Date().getTime();
										}
										this.scriptAlert("scriptCheckTEXT","Salio del Add con readyState:"+oScriptNuevo.readyState,1);
										//window.status=oScriptNuevo.readyState+" "+oScriptNuevo.src;
										/**/
										/**/
										
									}
								}catch(e){
									this.scriptAlert("scriptCheck","Exception "+e.name+","+e.message,1);
								}
							}else{
								this.scriptAlert("scriptCheck","DO  Ign Already Loading",1);
							}
						}else if(oScriptsNuevos_info.text&&!oScriptsNuevos_info.src&&PASO==1){
							this.scriptAlert("scriptCheck","DO  Interpret",1);
							if(oScriptsNuevos_info.eventevent||oScriptsNuevos_info.eventfor){
								if(oScriptsNuevos_info.eventevent&&oScriptsNuevos_info.eventfor&&oScriptsNuevos_info.text){
									if(document.getElementById(oScriptsNuevos_info.eventfor)){
										this.scriptAlert("scriptCheck","DO  Attach for:"+oScriptsNuevos_info.eventfor+",event:"+oScriptsNuevos_info.eventevent,1);
										document.getElementById(oScriptsNuevos_info.eventfor).attachEvent(oScriptsNuevos_info.eventevent,function () { eval(oScriptsNuevos_info.text);});
									}
								}
							}else{
								try{
									this.scriptAlert("scriptCheck","DO  Eval ing",1);
									self.eval(oScriptsNuevos_info.text);
								}catch(e){
									this.scriptAlert("scriptCheck","DO  Add to error on eval",1);
									this.EVALERROR.push(oScriptsNuevos_info);
								}
							}
						}else{
							this.scriptAlert("scriptCheck ","DO  Nada PASO:"+PASO,1);
						}
					}else{
						this.scriptAlert("scriptCheck","DO  IgnDefer",1);
					}
				}
				}
			}else{
				this.scriptAlert("scriptCheck","NoScripts",1);
			}
		}
		this.scriptAlert("scriptCheck","End",1);
	},
	scriptParseTag: function (texto){
		var res=new Array();
		var tmp;
		if(texto){
			tmp=texto.match(/id='([^']*?)'|id="([^"]*?)"/i);
			if(tmp){
				if(tmp[1]){
					res.id=tmp[1];
				}else{
					res.id=tmp[2];
				}
			}
			tmp=texto.match(/src='([^']*?)'|src="([^"]*?)"/i);
			if(tmp){
				if(tmp[1]){
					res.src=tmp[1];
				}else{
					res.src=tmp[2];
				}
			}
			tmp=texto.match(/defer='([^']*?)'|defer="([^"]*?)"/i);
			if(tmp){
				if(tmp[1]){
					res.defer=tmp[1];
				}else{
					res.defer=tmp[2];
				}
			}			
			if(!res.defer){
				tmp=texto.match(/[^a-z]+defer[^a-z]?/i);				
				if(tmp){
					res.defer="yes";
				}
			}
			res.defer=undefined;
			tmp=texto.match(/language='([^']*?)'|language="([^"]*?)"/i);
			if(tmp){
				if(tmp[1]){
					res.language=tmp[1];
				}else{
					res.language=tmp[2];
				}
			}
			tmp=texto.match(/for='([^']*?)'|for="([^"]*?)"/i);
			if(tmp){
				if(tmp[1]){
					res.eventfor=tmp[1];
				}else{
					res.eventfor=tmp[2];
				}
			}			
			tmp=texto.match(/event='([^']*?)'|event="([^"]*?)"/i);
			if(tmp){
				if(tmp[1]){
					res.eventevent=tmp[1];
				}else{
					res.eventevent=tmp[2];
				}
			}	
			tmp=texto.match(/type='([^']*?)'|type="([^"]*?)"/i);
			if(tmp){
				if(tmp[1]){
					res.language=tmp[1];
				}else{
					res.language=tmp[2];
				}
			}			
			tmp=texto.match(/<[ \t]*script[^>]*?>(.*?)<\/[ \t]*script[ \t]*>/i);
			if(tmp){
				res.text=tmp[1];
				res.text=res.text.replace(/#@NL@#/g,"\n");
			}
		}
		this.scriptAlert("scriptParseTag","Results \n"+
			  "id:"+res.id+"\n"+
			  "src:"+res.src+"\n"+
			  "language:"+res.language+"\n"+
			  "text:"+res.text+"\n"+
			  "defer:"+res.defer+"\n"+
			  "type:"+res.type+"\n"+
			  "eventevent:"+res.eventevent+"\n"+
			  "eventfor:"+res.eventfor+"\n"+
			  "WITH:\n\n"+texto+
			  "",1);
		return res;
	},
	scriptIsLoaded: function (src,name){
		var oHead=document; //document.getElementsByTagName("HEAD")[0]; //chekea en toda la pagina
		var oScriptsActuales=oHead.getElementsByTagName("SCRIPT");
		var sinDominio;
		this.scriptAlert("scriptIsLoaded","call",1);
		if(oScriptsActuales){
			this.scriptAlert("scriptIsLoaded","length: "+oScriptsActuales.length,2);
			if(typeof name=="undefined"){
				for(var j=0;j<oScriptsActuales.length;j++){
					this.scriptAlert("scriptIsLoaded","src: "+oScriptsActuales[j].src,2);
					sinDominio=oScriptsActuales[j].src+"";
					sinDominio=sinDominio.replace("http://"+window.location.hostname,"");
					if(oScriptsActuales[j].src==src||src==sinDominio){
						return true;
					}
				}
			}else{
				for(var j=0;j<oScriptsActuales.length;j++){
					this.scriptAlert("scriptIsLoaded","id: "+oScriptsActuales[j].id,2);
					if((oScriptsActuales[j].id==name||oScriptsActuales[j].name==name)&&(oScriptsActuales[j].src==src||!src)){
						return true;
					}
				}
			}
		}
		return false;
	},
	scriptAlert: function (funcion,cadena,nivel){
		this.debug=FWK.debug;
		this.sdebug+=funcion+" :: "+cadena+"\n";
		if(this.debug){						
				if(this.debug=="all"){
					alert(funcion+" :: "+cadena);
				}else{
					if(this.debug==nivel){
						alert(funcion+" :: "+cadena);
					}
				}
		}
	},
	attachRef: function (oScript){	
		//alert(oScript.src+" "+oScript.readyState+" "+SCRIPT.COUNTREF);
		//window.status=oScript.src+" "+oScript.readyState+" "+SCRIPT.COUNTREF;
		if(oScript.readyState=="complete"||oScript.readyState=="loaded"){
			SCRIPT.COUNTREF--;
		}
		if(SCRIPT.COUNTREF==0){
			SCRIPT.processEvalError();
		}
		if(oScript.readyState=="complete"||oScript.readyState=="loaded"){
			oScript.onreadystatechange=null;
		}
	},
	processEvalError: function (){
		this.scriptAlert("processEvalError","Call",1);
		//alert("processEvalError call "+this.EVALERROR.length);
		//window.status="processEvalError call "+this.EVALERROR.length;
		if(this.EVALERROR.length){
			for(var i=0;i<this.EVALERROR.length;i++){
				//try{
					this.scriptAlert("processEvalError","DO  Eval script with Error previously",1);
					//alert("processEvalError DO EVAL "+this.EVALERROR[i].text);
					self.eval(this.EVALERROR[i].text);
				//}catch(e){
				//}
			}
		}
	},
	appendChildAjax: function (oHead,oScript){
		var sScriptContenido;
		if(oScript.language=="vbscript"){
			this.scriptAlert("appendChildAjax","DO  Adding in Head",1);
			oHead.appendChild(oScript);
		}else{
			if(oScript.src){				
				sScriptContenido=FWK.ahah_open_get(oScript.src,null);
				if(sScriptContenido){
					this.scriptAlert("appendChildAjax","DO  Eval",1);
					//prompt("","SCRIPT SRC:"+oScript.src+"\n"+sScriptContenido,"");
					eval(sScriptContenido);
					if(typeof cargarPlayListDefault!="undefined"){
						//alert("FUNCION LOCAL: "+cargarPlayListDefault);
					}
					//alert("FUNCION GLOBAL: "+window.cargarPlayListDefault);
				}
			}		
		}
	}
}