// ===========================================================================
// funciones.js: Rutinas JS genericas de SGAS
// ===========================================================================
var undefined;

// ===========================================================================
// RUTINAS DE USO INTERNO 
// ===========================================================================
// getArgsQsw: Almacena los argumentos de la QUERY_STRING en 
//            una estructura tipo "hash"
// ===========================================================================
function getArgsQsw(owindow)
{
 var args= new Object();
 var qs=owindow.location.search.substring(1);
 var params= qs.split('&');
 for (var i=0; i < params.length; i++)
 {
   var pos= params[i].indexOf('=');
   if (pos == -1) continue;
   var argname= params[i].substring(0,pos);
   var value= params[i].substring(pos+1);
   args[argname]= unescape(value);
 }
 return args;
} 
// ===========================================================================
// isQs: Chequea si la cadena es una QueryString
// ===========================================================================
function isQs(url)
{
 return (url.indexOf('?') != -1) ? true : false;
}
// ===========================================================================
// isPath: Chequea si la cadena es una Path o una URL entera
//         http://www.ecoiuris/path/fichero.html  o
//         /path/fichero.html
// ===========================================================================
function isPath(url)
{
 return (url.indexOf('http://') != -1) ? false : true;
}
// ===========================================================================
// vAbrir_Ventana: Abre una ventana nueva con las propiedades especificadas
// ===========================================================================
function vAbrir_Ventana(sPagina,sVentana,props)
{ 
 for (i=0;i<=arguments.length;i++) // Todos los argumentos en minusculas
 {	 
   var tmp= new String (arguments[i]);
   arguments[i]=tmp.toLowerCase();
 }
 nuevaventana = window.open(sPagina,sVentana,props);
 return nuevaventana;
}
// ===========================================================================
// fSGAS_getsgid: Retorna el sgid activo
// ===========================================================================
function fSGAS_getsgid(win_sgid)
{
	 if (win_sgid === undefined) win_sgid=top;
	 var args = getArgsQsw(win_sgid);
	 if (args.sgid === undefined) args.sgid="";
	 return args.sgid;
}
// ===========================================================================
// RUTINAS DE USO EXTERNO
// ===========================================================================
// fSGAS_logout: Logout de la sesion SGAS
// ===========================================================================
function fSGAS_logout(urld,win_sgid,win_location)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 if (urld === undefined) urld=vSG_ws;
 var sgid= fSGAS_getsgid(win_sgid);
 if (sgid != "") win_location.location= vSG_ws + "/sgas/SGlogout.cfm?sgid=" + sgid + "&urld=" + escape(urld);
 else win_location.location=urld;
}
// ===========================================================================
// fSGAS_chkses_html: Chequeo de la sesion SGAS en ficheros htmlbde
// ===========================================================================
function fSGAS_chkses_html(urld,win_sgid,win_location,fjs)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 if (fjs === undefined) fjs="&js=s";
 var sgid= fSGAS_getsgid(win_sgid);
 win_location.location=vSG_ws + "/sgas/SGchkses_html.cfm?sgid=" + sgid + fjs + "&urld=" + urld;
}

// ===========================================================================

// fSGAS_chkses_html_rep: Idem a anterior, pero haciendo replace del location.

// ===========================================================================

function fSGAS_chkses_html_rep(urld,win_sgid,win_location,fjs)

{

 if (win_sgid === undefined) win_sgid=top;

 if (win_location === undefined) win_location=self;

 if (fjs === undefined) fjs="&js=s";

 var sgid= fSGAS_getsgid(win_sgid);

 win_location.location.replace(vSG_ws + "/sgas/SGchkses_html.cfm?sgid=" + sgid + fjs + "&urld=" + urld);

}





// ===========================================================================
// fSGAS_chkses_html: Chequeo de la sesion SGAS en ficheros htmlbde de nexus en refresco.
// ===========================================================================
function fSGAS_chkses_html_refresco(urld,win_sgid,win_location,fjs)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 if (fjs === undefined) fjs="&js=s";
 var sgid= fSGAS_getsgid(win_sgid);
 win_location.location.replace( vSG_ws + "/sgas/SGchkses_html.cfm?sgid=" + sgid + fjs + "&urld=" + urld );
}
// ===========================================================================
// fSGAS_chkses_html_ldt: Modificada para archivos tipo "_ldt.html"
// ===========================================================================
function fSGAS_chkses_html_ldt(urld,win_sgid,win_location,fjs)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 if (fjs === undefined) fjs="&js=s";
 var sgid= fSGAS_getsgid(win_sgid);
 win_location.location= vSG_ws + "/sgas/SGchkses_html.cfm?sgid=" + sgid + fjs + "&tf=ldt&urld=" + urld;
}
// ===========================================================================
// fSGAS_chkses_vaps: Chequeo de la sesion SGAS en plantillas VERITY
// ===========================================================================
function fSGAS_chkses_vaps(urld,win_sgid,win_location,wProgreso)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 wProgreso= (wProgreso === undefined) ? "" : "&vp=" + wProgreso;
 var sgid= fSGAS_getsgid(win_sgid);
 win_location.location= vSG_ws + "/sgas/SGchkses_vaps.cfm?sgid=" + sgid + wProgreso + "&urld=" + urld;
}
// ===========================================================================
// fSGAS_creditos_vaps: Controla la gestion de creditos de consumo
// ===========================================================================
function fSGAS_creditos_vaps(urld,win_sgid,win_location)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 var sgid= fSGAS_getsgid(win_sgid);
 win_location.location= vSG_ws + "/sgas/SGcreditos_vaps.cfm?sgid=" + sgid + "&urld=" + escape(urld);
}
// ===========================================================================
// fSGAS_callext: LLamada a una pagina externa. Cierra sesion antes de hacerlo
// ===========================================================================
function fSGAS_callext(urld,win_sgid,win_location)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 var sgid= fSGAS_getsgid(win_sgid);
 if (sgid != "") 
   win_location.location= vSG_ws + "/sgas/SGlogout.cfm?sgid=" + sgid + "&urld=" + escape(urld);
 else
   win_location.location= urld;
}
// ===========================================================================
// fSGAS_sesion: Controla la sesion de SGAS
// ===========================================================================
function fSGAS_sesion(urld,idcomp,win_sgid,win_location)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 if (idcomp === undefined) idcomp= vSG_idcomp;
 var sgid= fSGAS_getsgid(win_sgid);
 win_location.location= vSG_ws + "/sgas/SGsesion.cfm?sgid=" + sgid + "&comp=" + idcomp + vSG_tAUTH + "&urld=" + escape(urld);
}

// ==========================================================================================
// fSGAS_sesion_pers: Controla la sesion de SGAS para clientes con Páginas Personalizadass
// ==========================================================================================
function fSGAS_sesion_pers(urld,idcomp,win_sgid,win_location)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 if (idcomp === undefined) idcomp= vSG_idcomp;
 var sgid= fSGAS_getsgid(win_sgid);
 win_location.location= vSG_ws + "/sgas/SGsesion_pers.cfm?sgid=" + sgid + "&comp=" + idcomp + vSG_tAUTH + "&urld=" + escape(urld);
}

// ===========================================================================
// furl: Hereda la sesion SGAS para paginas ABIERTAS
// ===========================================================================
function furl(urld,win_sgid,win_location)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 var sgid= fSGAS_getsgid(win_sgid);
 if (sgid == "") 
 	win_location.location= isPath(urld) ? vSG_ws + urld : urld;
 else 			 
 {  
    var partes_url = urld.split("#"); // Separacion del anchor 
    var url= partes_url[0]; var ancor= (partes_url[1] === undefined) ? "" : "#" + partes_url[1];
	url= isPath(urld) ? vSG_ws+url : url;
    win_location.location= (isQs(urld)) ? url+"&sgid="+sgid+ancor : url+"?sgid="+sgid+ancor;
 }	
}
// ===========================================================================
// furlPOST: Hereda la sesion SGAS desde un formulario POST
// ===========================================================================
function furlPOST(urld,idForm,win_sgid)
{
 if (win_sgid === undefined) win_sgid=top;
 var sgid= fSGAS_getsgid(win_sgid);
 if (sgid == "")
	document.forms[idForm].action= vSG_ws + urld;
 else	
 {
    var partes_url = urld.split("#"); // Separacion del anchor 
    var url= partes_url[0]; var ancor= (partes_url[1] === undefined) ? "" : "#" + partes_url[1];
  	document.forms[idForm].action= (isQs(urld)) ? vSG_ws+url+"&sgid="+sgid+ancor : vSG_ws+url+"?sgid="+sgid+ancor;
 }
 document.forms[idForm].submit();
}
// ===========================================================================
// fSGAS_retsgid_parent: Devuelve el SGID a la ventana padre
// ===========================================================================
function fSGAS_retid_parent()
{
 var sgid =fSGAS_getsgid(this);
 var str=new String(self.opener.location);
 str=str.toLowerCase();
 if (str.indexOf("sgid") == -1) // La URL NO contiene el parametro SGID ?
 {
    var partes_url = str.split("#"); // Separacion del anchor 
    var url= partes_url[0]; var ancor= (partes_url[1] === undefined) ? "" : "#" + partes_url[1];	
  	self.opener.location= (isQs(url)) ? url+"&sgid="+sgid+ancor : url+"?sgid="+sgid+ancor;
 }
}
// ===========================================================================
// fSGAS_wopen_doc: Controla la sesion de SGAS en una ventana nueva 
// ===========================================================================
function fSGAS_wopen_doc(urld,wname,props,win_sgid)
{
 if (wname === undefined) wname= "ventana";
 if (props === undefined) // Propiedades por defecto: ficheros htmlbde
 {
	var ancho=730;
	var alto=470;
	var posX= (screen.width-ancho)/2;   // Centrado de la ventana
	var posY= (screen.height-(alto+70))/2;
    props= "toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=yes,resizable=1,copyhistory=0,width="+ancho+",height="+alto+",left=" +posX+ ",top=" +posY;
 }	
 if (win_sgid === undefined) win_sgid=top;
 var w= vAbrir_Ventana('',wname,props);
 fSGAS_sesion(urld,vSG_idcomp,win_sgid,w);
}
// ===========================================================================
// furl_wopen_doc: Abre fichero ABIERTO en una ventana nueva 
// ===========================================================================
function furl_wopen_doc(urld,wname,props)
{
 if (wname === undefined) wname= "ventana";
 if (props === undefined) // Propiedades por defecto: ficheros htmlbde
 {
 	var ancho=730;
	var alto=470;
	var posX= (screen.width-ancho)/2;   // Centrado de la ventana
	var posY= (screen.height-(alto+70))/2;
    props= "toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=yes,resizable=1,copyhistory=0,width="+ancho+",height="+alto+",left=" +posX+ ",top=" +posY;
 }	
 var w= vAbrir_Ventana('',wname,props);
 w.location=urld;
}
// ===========================================================================
// fSGAS_delauth: Elimina cookie de autenticacion
// ===========================================================================
function fSGAS_delauth(win_location)
{
 if (win_location === undefined) win_location=self;
 win_location.location= vSG_ws + "/sgas/SGdelCookie.cfm";
}
// ===========================================================================
// fSGAS_chkses_links: Gestion de links en los ficheros htmlbde
// ===========================================================================
function fSGAS_chkses_links(urld,tipolink,win_sgid,win_location)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 var sgid= fSGAS_getsgid(win_sgid);
 //prompt("",urld);
 win_location.location= vSG_ws + "/sgas/SGchkses_links.cfm?sgid=" + sgid + tipolink + "&urld=" + urld;
}
// ===========================================================================
// fSGAS_getimg: Devuelve cadena para visualizar imagen de imgs.px.wke.es
// Uso:  var cadenaIMG= fSGAS_getimg("IM0000089876.jpg");
// ===========================================================================
function fSGAS_getimg(img,win_sgid,iws)
{
 if (win_sgid === undefined) win_sgid=top;
 if (iws === undefined) iws=vSG_ws+vSG_cfmimg;
 var sgid= fSGAS_getsgid(win_sgid);
 var tokkens= img.split('.'); // Quita extension .jpg
 var IM= tokkens[0]; var len= IM.length-4;
 var urlImagen= iws+IM.substr(len,1)+"/"+IM.substr(len+1,1)+"/"+IM.substr(len+2,1)+"/"+IM.substr(len+3,1)+"/"+img+"&sgid="+sgid;
 return urlImagen;
}

// =======================================================================================
// FGS : fSGAS_chkses_vigente: Chequeo de la sesion SGAS en ficheros que enlazan desde el 
//						 archivo _vigente.html
// =======================================================================================
function fSGAS_chkses_vigente(urld,win_sgid,win_location,fjs)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 if (fjs === undefined) fjs="&js=s";
 var sgid= fSGAS_getsgid(win_sgid);
 win_location.location.replace(vSG_ws + "/sgas/SGchkses_html.cfm?sgid=" + sgid + fjs + "&urld=" + urld);
}



// ===========================================================================
// fSGAS_chkses_tdc: Chequeo de la sesion SGAS en ficheros de libro electronico
// ===========================================================================
function fSGAS_chkses_tdc(urld,win_sgid,win_location,fjs)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 if (fjs === undefined) fjs="&js=s";
 var sgid= fSGAS_getsgid(win_sgid);
 urllocation= vSG_ws + "/sgas/SGchkses_html.cfm?sgid=" + sgid + fjs + "&urld=" + urld;
 win_location.location.replace(urllocation);
}

// ===========================================================================
// fSGAS_chkses_diario: Chequeo de la sesion SGAS en ficheros del Diario
// ===========================================================================
function fSGAS_chkses_revista(urld,win_sgid,win_location,fjs,fsrch)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 if (fjs === undefined) fjs="&js=n";
 if (fsrch === undefined) fsrch="";
 var sgid= fSGAS_getsgid(win_sgid);
 win_location.location=vSG_ws + "/sgas/SGchkses_revista.cfm?sgid=" + sgid + fjs + fsrch + "&urld=" + urld;
}
// ===========================================================================
// getfSGAS_chkses_diario: Consigue URL para chequeo de la sesion SGAS en
//                          ficheros del Diario
// ===========================================================================
function getfSGAS_chkses_diario(urld,win_sgid,win_location,fjs)
{
 if (win_sgid === undefined) win_sgid=top;
 if (win_location === undefined) win_location=self;
 if (fjs === undefined) fjs="&js=n";
 var sgid= fSGAS_getsgid(win_sgid);
 var url=vSG_ws + "/sgas/SGchkses_diario.cfm?sgid=" + sgid + fjs + "&urld=" + urld;
 return url;
}

