// JavaScript jutil.js

var whitespace = " \t\n\r";

function isset(variable_name) {
    try {
         if (typeof(eval(variable_name)) != 'undefined')
         if (eval(variable_name) != null)
         return true;
     } catch(e) { }
    return false;
   }


function comprobarventana() 
{ 
   if(ventana && !ventana.closed)
   {
       ventana.focus();
	   return false;
   }
   else
       return true;
}

function AbrirVentana(URL,nombre,caracteristicas,width,height) { 

 if(ventana && !ventana.closed)
 {
       ventana.focus();
 }
 else
 {
     ventana = null;
	 var largo = width;
  	 var altura = height;
 	 var izquierda = (screen.width-largo)/2; 
  	 ventana = window.open(''+ URL + '',''+ nombre + '','width=' + largo + ',height=' + altura + ',left=' + izquierda + ',' + caracteristicas);
  } 
}

function isWhitespace (s)
{   var i;
    if (s=="") return true;
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        // si el caracter en que estoy no aparece en whitespace,
        // entonces retornar falso
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}


function isEmail (s)
{
    if (s=="")
    {
	    return false;
	}
    if (isWhitespace(s)) return false;
    var i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function enviarcorreo()
{
	var formuprin = document.getElementById("enviar");
  	var mensaje="";
  	var totOk = true;
	var usuario = formuprin.nombre_usuario.value;
  	usuario = trim(usuario);
  	if(usuario=="" || usuario.length>70)
  	{
       	mensaje += "Debes revisar el campo nombre.\n";
	   	totOk = false;
  	}
	
	var email = formuprin.correo.value;
  	email = trim(email);
  	if(!isEmail(email))
  	{
      	mensaje += "Debes revisar tu correo electronico.\n";
	  	totOk = false;
  	}
	
	var asunto = formuprin.asunto.value;
  	asunto = trim(asunto);
  	if(asunto=="" || asunto.length>70)
  	{
       	mensaje += "Debes revisar el campo asunto.\n";
	   	totOk = false;
  	}
	
	var texto = formuprin.texto_correo.value;
  	texto = trim(texto);
  	if(texto=="")
  	{
       	mensaje += "Debes escribir algún texto en el mensaje.\n";
	   	totOk = false;
  	}
	
	var copia = formuprin.copia.checked;
	
	if(totOk==true)
   {
   		formuprin.submit();
   }
   
   if(totOk==false)
   {
      alert(mensaje);
   }
}

function contactocliente()
{
	
   var formuprin = document.getElementById("enviar");
   var mensaje="";
   var totOk = true;
   
   var vent = comprobarventana();
   
   if(vent==true){
   var asunto = formuprin.asunto.value;
  	asunto = trim(asunto);
  	if(asunto=="" || asunto.length>70)
  	{
       	mensaje += "Debes revisar el campo asunto.\n";
	   	totOk = false;
  	}
	
	var texto = formuprin.texto_correo.value;
  	texto = trim(texto);
  	if(texto=="")
  	{
       	mensaje += "Debes escribir algún texto en el mensaje.\n";
	   	totOk = false;
  	}
	
	var copia = formuprin.copia.checked;
   
   
   if(totOk==true)
   {
   		formuprin.submit();
   }
   
   if(totOk==false)
   {
      alert(mensaje);
   }
   }
}
	
	