function getWindowData(){ 
    var widthViewport,heightViewport,xScroll,yScroll,widthTotal,heightTotal; 
    if (typeof window.innerWidth != 'undefined'){ 
        widthViewport= window.innerWidth-17; 
        heightViewport= window.innerHeight-17; 
    }else if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0){ 
        widthViewport=document.documentElement.clientWidth; 
        heightViewport=document.documentElement.clientHeight; 
    }else{ 
        widthViewport= document.getElementsByTagName('body')[0].clientWidth; 
        heightViewport=document.getElementsByTagName('body')[0].clientHeight; 
    } 
    xScroll=self.pageXOffset || (document.documentElement.scrollLeft+document.body.scrollLeft); 
    yScroll=self.pageYOffset || (document.documentElement.scrollTop+document.body.scrollTop); 
    widthTotal=Math.max(document.documentElement.scrollWidth,document.body.scrollWidth,widthViewport); 
    heightTotal=Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,heightViewport); 
    return [widthViewport,heightViewport,xScroll,yScroll,widthTotal,heightTotal]; 
} 

//*******************************
//Popups con DIVS
//*******************************
//*******************************
//Popups con DIVS
//*******************************
function muestra(ide, ancho, alto)
{
	var dimensiones=getWindowData();
	var TotalHeight=dimensiones[5];
	var YScroll=dimensiones[3];
    var Altura=dimensiones[1];

	//Posiciona
	if (navigator.appName.indexOf("Explorer") != -1)
  	{
		document.getElementById(ide).style.left=(document.body.offsetWidth - ancho)/2;
		document.getElementById(ide).style.top=((Altura  - alto)/2)+YScroll; 
	}
	else
	{
		document.getElementById(ide).style.left= (window.innerWidth - ancho)/2 + "px";
		document.getElementById(ide).style.top= ((Altura  - alto)/2)+YScroll  + "px"; 
	}

	
	//Muestra
	document.getElementById(ide).style.width = ancho + "px";
	document.getElementById(ide).style.height= alto + "px";
	document.getElementById("oculto").style.width=100 + "%";
	document.getElementById("oculto").style.height=TotalHeight + "px";	
	
	document.getElementById(ide).style.visibility="visible";	
	document.getElementById("oculto").style.visibility="visible";
}


function cierra(ide,fondo)
{
document.getElementById(ide).style.visibility="hidden";
document.getElementById(ide).style.width=0 + "px";
document.getElementById(ide).style.height=0 + "px";
document.getElementById(ide).style.left=0 + "px";
document.getElementById(ide).style.top=0 + "px";

    if (fondo==null)
        fondo="S";
        
    if (fondo=="S")
    {
        document.getElementById("oculto").style.visibility="hidden";
        document.getElementById("oculto").style.width=0 + "px";
        document.getElementById("oculto").style.height=0 + "px";
        document.getElementById("oculto").style.left=0 + "px";
        document.getElementById("oculto").style.top=0 + "px";
    }
}



function comprueba_extension(archivo) { 
	extensiones_permitidas = new Array(".jpg"); 
   
   	//recupero la extensión de este nombre de archivo 
   	extension = (archivo.substring(archivo.lastIndexOf("."))).toLowerCase(); 
   
   	//compruebo si la extensión está entre las permitidas 
   	permitida = false; 
   	for (var i = 0; i < extensiones_permitidas.length; i++) { 
   		if (extensiones_permitidas[i] == extension) { 
     		permitida = true; 
     		break; 
    	 } 
 	 } 
	
	return permitida; 
	    
}

function comprueba_extension2(archivo) { 
	extensiones_permitidas = new Array(".mp3",".mp4",".flv"); 
   
   	//recupero la extensión de este nombre de archivo 
   	extension = (archivo.substring(archivo.lastIndexOf("."))).toLowerCase(); 
   
   	//compruebo si la extensión está entre las permitidas 
   	permitida = false; 
   	for (var i = 0; i < extensiones_permitidas.length; i++) { 
   		if (extensiones_permitidas[i] == extension) { 
     		permitida = true; 
     		break; 
    	 } 
 	 } 
	
	return permitida; 
	    
}  

function EstaVacio(campo)
{
		var cadena = campo.value;
		var result = "";
		for (i=0;i<=cadena.length-1;i++) 
		{
			if ((cadena.charAt(i))!=" ")
				{
				  result="ok";
				}
		}
		
		if (result=="")
			return true; //Esta vacio
		else
			return false; //No esta vacio
		
}


function TieneEspacios(campo)
{
		var cadena =campo.value;
		var result = "N";
		for (i=0;i<=cadena.length-1;i++) 
		{
			if ((cadena.charAt(i))==" ")
				{
				  result="S";
				}
		}
		
		if (result=="S")
			return true; //Tiene espacios
		else
			return false; //No tiene Espacios
}


function mail(texto){ 

    var mailres = true;             
    var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-"; 
     
    var arroba = texto.indexOf("@",0); 
    if ((texto.lastIndexOf("@")) != arroba) arroba = -1; 
     
    var punto = texto.lastIndexOf("."); 
                 
     for (var contador = 0 ; contador < texto.length ; contador++){ 
        if (cadena.indexOf(texto.substr(contador, 1),0) == -1){ 
            mailres = false; 
            break; 
     } 
    } 

    if ((arroba > 1) && (arroba + 1 < punto) && (punto + 2 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1)) 
     mailres = true; 
    else 
     mailres = false; 
                 
    return mailres; 
} 

function validaUsuario(formulario)
{
	var mensaje = "";
    if (formulario.cmbtpe.value=="J")
        if (EstaVacio(formulario.txtraz))  
    		mensaje= mensaje + "No ha ingresado la Razon Social del usuario. \n";
    if (formulario.cmbtpe.value=="N")
    {
    	if (EstaVacio(formulario.txtnom))  
    		mensaje= mensaje + "No ha ingresado el nombre del usuario. \n";
        if (EstaVacio(formulario.txtape))  
    		mensaje= mensaje + "No ha ingresado el apellido del usuario. \n";
    }
    if (EstaVacio(formulario.txtdir))  
		mensaje= mensaje + "No ha ingresado la direccion del usuario. \n";
	if (EstaVacio(formulario.txttel))  
		mensaje= mensaje + "No ha ingresado el teléfono del usuario. \n";
    if (EstaVacio(formulario.txtac1))  
		mensaje= mensaje + "No ha ingresado la actividad principal del usuario. \n";
    if (EstaVacio(formulario.txtusr) || TieneEspacios(formulario.txtusr))  
		mensaje= mensaje + "El usuario ingresado no es valido. Es obligatorio y no debe contener espacios en blanco \n";
    if (EstaVacio(formulario.txtrep))  
		mensaje= mensaje + "No ha ingresado el nombre del representante. \n";
    if (EstaVacio(formulario.txtrepcar))  
		mensaje= mensaje + "No ha ingresado el cargo del representante. \n";
    if (EstaVacio(formulario.txtrepmai) || (mail(formulario.txtrepmai.value)==false))  
		mensaje= mensaje + "El email del representante no es válido o no ha sido ingresado. \n";
    
    if (!EstaVacio(formulario.txtejemai) && (mail(formulario.txtrepmai.value)==false))  
		mensaje= mensaje + "El email del ejecutivo no es válido. \n";
        
    if (!EstaVacio(formulario.txtrhhmai) && (mail(formulario.txtrhhmai.value)==false))  
		mensaje= mensaje + "El email de recursos humanos no es válido. \n";
  
    if (!EstaVacio(formulario.txtmarmai) && (mail(formulario.txtmarmai.value)==false))  
		mensaje= mensaje + "El email de MArketing no es válido. \n";
  
    if (!EstaVacio(formulario.txtfinmai) && (mail(formulario.txtfinmai.value)==false))  
		mensaje= mensaje + "El email de finanzas no es válido. \n";
    
    if (!EstaVacio(formulario.txtcommai) && (mail(formulario.txtcommai.value)==false))  
		mensaje= mensaje + "El email de comercio exterior no es válido. \n";
        
    if (!EstaVacio(formulario.txtotrmai) && (mail(formulario.txtotrmai.value)==false))  
		mensaje= mensaje + "El email del contacto adicional no es válido. \n";
        

    if (formulario.cmblog.value=="S")
    {
        if (EstaVacio(formulario.txtlog))  
    		mensaje= mensaje + "No ha ingresado la ruta de la IMAGEN a subir. \n";
    	if (comprueba_extension(formulario.txtlog.value)==false)
    		mensaje= mensaje + "El archivo seleccionado no corresponde a una imagen .jpg\n";
	}
    
    if (formulario.hddtip.value=="M") //Si es modificar
    {
        if (formulario.chkcla.checked==true)
        {
            if (EstaVacio(formulario.txtcla) || TieneEspacios(formulario.txtcla))  
        		mensaje= mensaje + "La contraseña ingresada no es válida. Es obligatoria y no debe contener espacios en blanco. \n";
            if (EstaVacio(formulario.txtcla2))  
        		mensaje= mensaje + "Debe repetir la contraseña. \n";
            if ((formulario.txtcla2.value!=formulario.txtcla.value))
                mensaje= mensaje + "Las contraseñas no coinciden. \n";
        }
    }
    else //Es nuevo
    {
            if (EstaVacio(formulario.txtcla) || TieneEspacios(formulario.txtcla))  
        		mensaje= mensaje + "La contraseña ingresada no es válida. Es obligatoria y no debe contener espacios en blanco. \n";
            if (EstaVacio(formulario.txtcla2))  
        		mensaje= mensaje + "Debe repetir la contraseña. \n";
            if ((formulario.txtcla2.value!=formulario.txtcla.value))
                mensaje= mensaje + "Las contraseñas no coinciden. \n";
    }
    
        	
	if (mensaje=="")
	{
        if (formulario.hddtip.value=="N") //Nuevo
		{
		  formulario.submit();
        }
        if (formulario.hddtip.value=="M") //Modificacion
		{
		  formulario.submit();
        }
	} 
	else
	{
		mensaje = "Ocurrieron los siguientes errores:\n" + mensaje;
		alert(mensaje);
		return false;
	}	
}



function validaAcceso(formulario)
{
	var mensaje = "";
    if (EstaVacio(formulario.txtusr))  
		mensaje= mensaje + "Por favor escriba su nombre de usuario. \n";
	if (EstaVacio(formulario.txtcla))  
		mensaje= mensaje + "Ingrese su contraseña. \n";
    
        	
	if (mensaje=="")
	   formulario.submit();
    else
	{
		mensaje = "Ocurrieron los siguientes errores:\n" + mensaje;
		alert(mensaje);
		return false;
	}	
}


