function MM_displayStatusMsg(msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function pop(pagina, popW, popH, scroll) //abre la página que se le pasa con la altura especificada.
{
if (document.all || document.layers || document.getElementById) 
		{
   			w = screen.availWidth;
		    h = screen.availHeight;
		}

		var leftPos = (w-popW)/2, topPos = (h-popH)/2;
		window.open(pagina,"mod","width=" + popW + ",height=" + popH + ",top=" + topPos + ",left=" + leftPos + ",scrollbars=" + scroll);
}

// ----------------------------
//    Validamos el formulario
// ----------------------------
function validacomercial(theForm)
{
  var error = 0

  if (trim(theForm.CONTACTO) == "")             //CONTACTO
  {
  	alert("Debe rellenar el campo:\nPersona de contacto");
    theForm.CONTACTO.value = "";
  	theForm.CONTACTO.focus();
  	error = 1;
    return false;
  }

  if (trim(theForm.TELEFONO) == "")             //TELEFONO
  {
  	alert("Debe rellenar el campo:\nTeléfono")
    theForm.TELEFONO.value = "";
  	theForm.TELEFONO.focus();
  	error = 1;
    return false;
  }else if (!recoger(theForm.TELEFONO.value,1))
 	{
  	  alert ("Escriba únicamente numeros,\n en el Teléfono.");
      theForm.TELEFONO.value = "";
  		theForm.TELEFONO.focus ();
  		error = 1;
      return false;
 	}

  if (theForm.CORREO.value != ""){
    if (!ChequeaEmail(theForm.CORREO,theForm.CORREO.value)){
      theForm.CORREO.value = "";
      theForm.CORREO.focus();
      error = 1;
      return false;
    }
  }
  
    if (error == 0)
	theForm.submit();
    else
	return false;
 
}

// ---------------------------------------------------
//    Funciones que comprueban los caracteres raros
// ---------------------------------------------------

function recoger(campo,tipo_comprobacion)
{
  if (tipo_comprobacion == 1){
      // validamos numeros
         var checkOk = "0123456789";
     }else if( tipo_comprobacion == 2){
	      // validamos minusculas.
	         var checkOk = "abcdefghijklmnopqrstuvwxyz";
       }else if( tipo_comprobacion == 3){
		 // validamos minusculas y numeros.
			   var checkOk = "abcdefghijklmnopqrstuvwxyz0123456789";
		}else if( tipo_comprobacion == 4){
		   // validamos mayusculas.
		 	   var checkOk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";		
	         }else if( tipo_comprobacion == 5){
			  // validamos mayusculas,numeros.
					var checkOk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                 }else if( tipo_comprobacion == 6){
		         // validamos que el campo sea mayusculas,minusculas.
               	      var checkOk="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		          }else if( tipo_comprobacion == 7){
			       // validamos mayusculas,minusculas,numeros.
			       	   var checkOk="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		        	}else if( tipo_comprobacion == 8){
			       // validamos numeros y /.
			       	   var checkOk="0123456789/";
					   }else if( tipo_comprobacion == 9){
					   //validamos cifras(numeros con .)
					   var checkOk="0123456789.";
   					   }else if( tipo_comprobacion == 10){
					   //validamos cifras(numeros con .)
					   var checkOk="0123456789.,";
		        	}
            var checkStr = campo;
		if (checkStr == "")
		{
			return false;
		}
		for ( i = 0; i< checkStr.length; i++) 
		{
			ch=checkStr.charAt(i);
			for ( j= 0; j< checkOk.length; j++)
			{
	        	if (ch == checkOk.charAt(j))
				{
		        break;
		        }
			if(j == checkOk.length-1)
			{
				return false;		
			}
		}
	}
	return true;
}

function muestra(theForm)
{
  if (theForm.PUBLICIDAD.selectedIndex == 5){
    if (document.all){
      document.all('PBL1').innerHTML="Especifique otros medios";
      document.all('PBL2').innerHTML="<input type=TEXT name=PUBLICIDAD2 size=30 maxlength=50>";
    }
  }else{
    if (document.all){
    document.all('PBL1').innerHTML="";
    document.all('PBL2').innerHTML="";
    }
  }
}
function ChequeaEmail(campo,error) {
  var vuelve;
  vuelve = false;  
  if( (campo.value.lastIndexOf( '.') != (campo.value.length - 3)) && (campo.value.lastIndexOf( '.') != (campo.value.length - 4))) {
    alert( "Si desea rellenar el correo electrónico,\nescriba una dirección correcta");
    campo.focus();
  }else{
    if( campo.value.indexOf( '@') == -1) {
      alert( "Si desea rellenar el correo electrónico,\nescriba una dirección correcta");
			campo.focus();
    }else{
        vuelve = true;
    }
  }
   return(vuelve);
}
function desconecta() {
    if (confirm("¿Desea salir del área de clientes?"))
        document.location.href="desconectar.asp";
    else
        return false;
}

function GestionaContacto(theform)
{ 
		document.getElementById('spnProceso').style.display='block';
		document.getElementById('divFondo').style.display='block';
		theform.__EVENTTARGET.value = "btnAlta";
		theform.__EVENTARGUMENT.value = null; 
		theform.submit(); 
}    

function validarDatosContacto(){

        var strCampos="";
        msgAviso="Faltan por especificar los siguientes campos obligatorios: ";
        
        if(document.getElementById("nombre").value=="")
        {
			strCampos += "NOMBRE";
		}

        if(document.getElementById("apellido").value=="")
        {
			strCampos += "APELLIDO";
		}
						
        if(document.getElementById("departamento").value=="")
        {
			strCampos += "DEPARTAMENTO";
		}
						
        if(document.getElementById("email").value=="")
        {
			(strCampos!="") ? strCampos += ", " : strCampos += "";
			strCampos += "E-MAIL";
		}

		if(strCampos!="")
		{
			msgAviso += strCampos;
			alert("Contactos: " + msgAviso);
		}
		else
		{			
			msgAviso="La introducción de los datos es incorrecta debido a que:\n\n";
			var strFallos="";
		
			if(validarEmail(document.getElementById("email").value)==false)
			{
				strFallos += "- El campo EMAIL no es válido.\n";
			}

			if(!validarEntero(document.getElementById("movil").value))
			{
				strFallos += "- El campo MOVIL no es válido.\n";
			}

			if(strFallos!="")
			{
				msgAviso += strFallos + "\n" + "Por favor, revise los datos.";
				alert(msgAviso);				
			}
			else
			{
   			    GestionaContacto(document.frmAltaContacto);
			}
		}
}


function validarDatosPresunto(){

        var strCampos="";
        msgAviso="Faltan por especificar los siguientes campos obligatorios: ";
        
        if(document.getElementById("CIF").value=="")
        {
			strCampos += "CIF";
		}

        if(document.getElementById("razon_social").value=="")
        {
			strCampos += "EMPRESA";
		}
						
      /*  if(document.getElementById("dir_empresa").value=="")
        {
			strCampos += "DIRECCION";
		}

        if(document.getElementById("pob_empresa").value=="")
        {
			strCampos += "LOCALIDAD";
		}

        if(document.getElementById("cp").value=="")
        {
			strCampos += "C.P";
		}*/
						
        if(document.getElementById("telf_empresa").value=="")
        {
			strCampos += "TELEFONO";
		}
		/*				
        if(document.getElementById("fax").value=="")
        {
			strCampos += "FAX";
		}*/

		/*Datos Coordinador*/
        if(document.getElementById("nom_contacto").value=="")
        {
			strCampos += "NOMBRE";
		}
        if(document.getElementById("ape_contacto").value=="")
        {
			strCampos += "APELLIDOS";
		}
        /*if(document.getElementById("telf_contacto").value=="")
        {
			strCampos += "TELEFONO";
		}
        if(document.getElementById("movil").value=="")
        {
			strCampos += "MOVIL";
		}*/
        if(document.getElementById("email_contacto").value=="")
        {
			(strCampos!="") ? strCampos += ", " : strCampos += "";
			strCampos += "E-MAIL";
		}

		if(strCampos!="")
		{
			msgAviso += strCampos;
			alert("Contactos: " + msgAviso);
		}
		else
		{			
			msgAviso="La introducción de los datos es incorrecta debido a que:\n\n";
			var strFallos="";
			
			if(!validarEntero(document.getElementById("telf_empresa").value))
			{
				strFallos += "- El campo TELEFONO no es válido.\n";
			}
	
			if(validarEmail(document.getElementById("email_contacto").value)==false)
			{
				strFallos += "- El campo EMAIL no es válido.\n";
			}

			if(!validarEntero(document.getElementById("movil").value))
			{
				strFallos += "- El campo MOVIL no es válido.\n";
			}

			if(!validarEntero(document.getElementById("telf_contacto").value))
			{
				strFallos += "- El campo TELEFONO no es válido.\n";
			}

			if(!validarEntero(document.getElementById("cp").value))
			{
				strFallos += "- El campo C.P no es válido.\n";
			}

			if(!validarEntero(document.getElementById("fax").value))
			{
				strFallos += "- El campo FAX no es válido.\n";
			}


			if(strFallos!="")
			{
				msgAviso += strFallos + "\n" + "Por favor, revise los datos.";
				alert(msgAviso);				
			}
			else
			{
				GuardarDatos(form1);
			}
		}
}

function validarDatosPrinex(){

        var strCampos="";
        msgAviso="Faltan por especificar los siguientes campos obligatorios: ";
        
        if(document.getElementById("CIF").value=="")
        {
			strCampos += "CIF";
		}

        if(document.getElementById("razon_social").value=="")
        {
			strCampos += "EMPRESA";
		}
						
      /*  if(document.getElementById("dir_empresa").value=="")
        {
			strCampos += "DIRECCION";
		}

        if(document.getElementById("pob_empresa").value=="")
        {
			strCampos += "LOCALIDAD";
		}

        if(document.getElementById("cp").value=="")
        {
			strCampos += "C.P";
		}*/
						
        if(document.getElementById("telf_empresa").value=="")
        {
			strCampos += "TELEFONO";
		}
		/*				
        if(document.getElementById("fax").value=="")
        {
			strCampos += "FAX";
		}*/

		/*Datos Coordinador*/
        if(document.getElementById("nom_contacto").value=="")
        {
			strCampos += "NOMBRE";
		}
        if(document.getElementById("ape_contacto").value=="")
        {
			strCampos += "APELLIDOS";
		}
        /*if(document.getElementById("telf_contacto").value=="")
        {
			strCampos += "TELEFONO";
		}
        if(document.getElementById("movil").value=="")
        {
			strCampos += "MOVIL";
		}*/
        if(document.getElementById("email_contacto").value=="")
        {
			(strCampos!="") ? strCampos += ", " : strCampos += "";
			strCampos += "E-MAIL";
		}

		if(strCampos!="")
		{
			msgAviso += strCampos;
			alert("Contactos: " + msgAviso);
		}
		else
		{			
			msgAviso="La introducción de los datos es incorrecta debido a que:\n\n";
			var strFallos="";
			
			if(!validarEntero(document.getElementById("telf_empresa").value))
			{
				strFallos += "- El campo TELEFONO no es válido.\n";
			}
		
			if(validarEmail(document.getElementById("email_contacto").value)==false)
			{
				strFallos += "- El campo EMAIL no es válido.\n";
			}

			if(!validarEntero(document.getElementById("movil").value))
			{
				strFallos += "- El campo MOVIL no es válido.\n";
			}

			if(!validarEntero(document.getElementById("telf_contacto").value))
			{
				strFallos += "- El campo TELEFONO no es válido.\n";
			}

			if(!validarEntero(document.getElementById("cp").value))
			{
				strFallos += "- El campo C.P no es válido.\n";
			}

			if(!validarEntero(document.getElementById("fax").value))
			{
				strFallos += "- El campo FAX no es válido.\n";
			}


			if(strFallos!="")
			{
				msgAviso += strFallos + "\n" + "Por favor, revise los datos.";
				alert(msgAviso);				
			}
			else
			{
				document.getElementById('divFondo').style.display='block';
				document.getElementById('divEspereTexto').style.display='block';
				document.getElementById('divEspereTexto').innerHTML='<img src=\"imagenes/loading.gif\" width=\"49\" height=\"47\" align=\"absmiddle\"> Actualizando Datos...';
				GuardarDatos(document.form1);
			}
		}
}
				   			    

//Postback
function HacerPostBack(theform,operacion,parametros)
{ 
		theform.__EVENTTARGET.value = "btnPostBack";
		theform.__EVENTARGUMENT.value = operacion + "|" + parametros; 
		theform.submit(); 
}

function EditaContacto(nreg,origen,ficha)
{
	//window.open('prinex_pop_altacontactos.aspx?Registro=' + nreg + '&amp;Origen=' + origen + '&amp;Ficha=' + ficha + '&amp;Modo=1','','width=530, height=180, top=275, left=302');
	window.open('prinex_pop_altacontactos.aspx?Registro=' + nreg + '&Origen=' + origen + '&Ficha=' + ficha + '&Modo=1','','width=530, height=180, top=275, left=302');
}

function EliminaContacto(nreg,origen,ficha)
{
	if (confirm("Desea Eliminar el registro?"))
	{
		window.open('prinex_pop_altacontactos.aspx?Registro=' + nreg + '&Origen=' + origen + '&Ficha=' + ficha + '&Modo=2','','width=530, height=180, top=275, left=302');
	}	
}
function EditaContactoAct(nreg,origen,ficha)
{
	window.open('prinex_pop_altacontactos.aspx?Act=1&Registro=' + nreg + '&Origen=' + origen + '&Ficha=' + ficha + '&Modo=1','','width=530, height=180, top=275, left=302');
}

function EliminaContactoAct(nreg,origen,ficha)
{
	if (confirm("¿Desea Eliminar el registro?"))
	{
		window.open('prinex_pop_altacontactos.aspx?Act=1&Registro=' + nreg + '&amp;Origen=' + origen + '&amp;Ficha=' + ficha + '&amp;Modo=2','','width=530, height=180, top=275, left=302');
	}	
}

function AltaContacto(origen,ficha)
{
	window.open('prinex_pop_altacontactos.aspx?Registro=&Origen=' + origen + '&Ficha=' + ficha + '&Modo=0','','width=530, height=180, top=275, left=302');
}

function AltaContactoAct(origen,ficha)
{
	window.open('prinex_pop_altacontactos.aspx?Act=1&Registro=&Origen=' + origen + '&Ficha=' + ficha + '&Modo=0','','width=530, height=180, top=275, left=302');
}


function GuardarDatos(theform)
{
	theform.__EVENTTARGET.value = "btnGuardar";
	theform.__EVENTARGUMENT.value = null; 
	theform.submit(); 
}

//Validar email
function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function validarEmail(email) {
			
var i			
var blnCorrecto
var longitud
var arremail = new Array(); 
arremail = email.split(",");

for (i=0; i <= arremail.length - 1;i++)
  { 
    if (! isValidEmail(arremail[i])) 
    {
        blnCorrecto = false;
        break;
    }
    blnCorrecto = true;
  }  
  return blnCorrecto;
}

//Validar entero
function validarEntero(cadena)
{
	k="0123456789.- ";
	for (j=0;j<cadena.length;j++)
	{
		if (k.indexOf(cadena.charAt(j)) == -1)
		{
			return false;
		}
	}
	return true;
}

function mostrarEspera(destino)
{
document.getElementById('divFondo').style.display='block';
document.getElementById('divEspereTexto').style.display='block';
document.getElementById('divEspereTexto').innerHTML='<img src=\"imagenes/loading.gif\" width=\"49\" height=\"47\" align=\"absmiddle\"> Recuperando datos...';
document.location.href=destino;
}
<!--precarga de la imagen loading-->

if (document.images)
{
  pic1= new Image(49,47); 
  pic1.src="imagenes/loading.gif"; 
}


function initScrollLayers() {
  var wndo2 = new dw_scrollObj('wn2', 'lyr2');
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
