
if(document.images) {
  img_contacto = new Image();
  img_portafolio = new Image();
  img_alerta = new Image();
  img_contacto.src = 'contacto_over_morado.gif';
  img_portafolio.src = 'portafolio_over_morado.gif';
  img_alerta.src = 'bg_negro.png';
}

var malInput_color = '#DEB8B9'; // '#CCCCCC';

var topPortafolio = Math.round((screen.availHeight - 585 - 100) / 2);
var leftPortafolio = Math.round((screen.availWidth - 950) / 2);
var leftContacto = leftPortafolio + 100;

function mostrar(n_mostrar, opcion) {
  var itemMostrar = document.getElementById(n_mostrar);
  var icon_c = document.getElementById('link_contacto');
  var icon_p = document.getElementById('link_portafolio');
  
  if(opcion == 0) {
    itemMostrar.style.display = 'none';
    icon_c.style.display = '';
    icon_p.style.display = 'none';
  }
  else {
    borrar_forma();
    
    itemMostrar.style.display = '';
    icon_p.style.display = '';
    icon_c.style.display = 'none';
  }
}

function iconoOver(nombre) {
  if(nombre == 'contacto') {
    document.icono_contacto.src = 'contacto_over_morado.gif';
  }
  else if(nombre == 'portafolio') {
    document.icono_portafolio.src = 'portafolio_over_morado.gif';
  }
}

function iconoOut(nombre) {
  if(nombre == 'contacto') {
    document.icono_contacto.src = 'contacto_out.gif';
  }
  else if(nombre == 'portafolio') {
    document.icono_portafolio.src = 'portafolio_out.gif';
  }
}

function borrar_forma() {
  blanco_forma();
  
  document.forma_contacto.nombre.value = '';  
  document.forma_contacto.apellido.value = '';  
  document.forma_contacto.email.value = '';  
  document.forma_contacto.empresa.value = '';  
  document.forma_contacto.telefono.value = '';  
  document.forma_contacto.comentarios.value = '';  
}

function blanco_forma() {
  document.forma_contacto.nombre.style.backgroundColor = '#FFFFFF';
  document.forma_contacto.apellido.style.backgroundColor = '#FFFFFF';
  document.forma_contacto.email.style.backgroundColor = '#FFFFFF';
  document.forma_contacto.telefono.style.backgroundColor = '#FFFFFF';
  document.forma_contacto.comentarios.style.backgroundColor = '#FFFFFF';
}

function validar_forma() {
  blanco_forma();

  var re_nombres = /^[a-zA-Z\-\sÑñÁáÉéÍíÓóÚúü]+$/; // /^[^\W\d_]+$/;  // /^[^\W\d_]([^\W\d_]|\s)*$/;
  var re_email = /^[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*$/;
  var re_tel = /^[\d\-\(\)\.\s_]+$/;
  
  var incorrectos = 0;
  var mensaje = '';
  var forma = document.forma_contacto;
  
  //with() {
    var elementos = forma.getElementsByTagName('input');
    //var x;
    
    for(var i = 0; i < elementos.length; i++) {
      elementos[i].value = elementos[i].value.replace(/(^\s*)|(\s*$)/g,'');
      //alert(x);
    }
    
    if(forma.nombre.value == '') {
      incorrectos++;
      forma.nombre.style.backgroundColor = malInput_color;
      mensaje += 'Necesitas escribir tu <strong>nombre</strong><br />\n';
    }
    else if(!re_nombres.test(forma.nombre.value)) {
      incorrectos++;
      forma.nombre.style.backgroundColor = malInput_color;
      mensaje += 'El formato de tu <strong>nombre</strong> no es correcto<br />\n';
    }
    
    if(forma.apellido.value == '') {
      incorrectos++;
      forma.apellido.style.backgroundColor = malInput_color;
      mensaje += 'Necesitas escribir tu <strong>apellido</strong><br />\n';
    }
    else if(!re_nombres.test(forma.apellido.value)) {
      incorrectos++;
      forma.apellido.style.backgroundColor = malInput_color;
      mensaje += 'El formato de tu <strong>apellido</strong> no es correcto<br />\n';
    }
    
    if(forma.email.value == '') {
      incorrectos++;
      forma.email.style.backgroundColor = malInput_color;
      mensaje += 'Necesitas proporcionar un <strong>e-mail</strong><br />\n';
    }
    else if(!re_email.test(forma.email.value)) {
      incorrectos++;
      forma.email.style.backgroundColor = malInput_color;
      mensaje += 'El formato del <strong>e-mail</strong> no es correcto<br />\n';
    }
    
    if((forma.telefono.value != '') && (!re_tel.test(forma.telefono.value))) {
      incorrectos++;
      forma.telefono.style.backgroundColor = malInput_color;
      mensaje += 'El formato del <strong>tel&eacute;fono</strong> no es correcto<br />\n';
    }
    
    if(forma.comentarios.value == '') {
      incorrectos++;
      forma.comentarios.style.backgroundColor = malInput_color;
      mensaje += 'Necesitas proporcionar <strong>comentarios</strong><br />\n';
    }
  //}
  
  if(incorrectos > 0) {    
    alertar(1, mensaje, incorrectos);
  }
  else {
    document.forma_contacto.submit();    
    mensaje = "<div style=\"text-align: center;\">Mensaje enviado.</div>\n";    
    borrar_forma();
    alertar(1, mensaje, 1);
  }
}

function alertar(flag, msj, lineas) {
  var alerta = document.getElementById('alerta');
  var alerta_txt = document.getElementById('alerta_txt');
  
  if(flag == 1) {
    var topAlerta = Math.round((screen.availHeight - 200 - 100) / 2);
    var leftAlerta = Math.round((screen.availWidth - 300) / 2);    
    var height_alerta = 92 + (lineas * 18);
    var height_alerta_txt = height_alerta - 42;
    
    alerta.style.height = height_alerta + 'px';
    alerta_txt.style.height = height_alerta_txt + 'px';
    alerta.style.top = topAlerta + 'px';
    alerta.style.left = leftAlerta + 'px';
    alerta_txt.innerHTML = msj + "<p style=\"text-align: center;\"><input type=\"button\" class=\"boton\" onclick=\"alertar(0,'');\" value=\"Aceptar\" /></p>\n";
    
    alerta.style.display = '';    
    habilitar(false);
  }
  else {
    alerta.style.display = 'none';
    alerta_txt.innerHTML = '&nbsp;';
    
    habilitar(true); // Éste podría ir justo después del ELSE para que no se piratee al usar innerHTML en "habilitar(true)"
  }
}

function habilitar(flag) {
  if(flag) {
    document.forma_contacto.btn_borrar.disabled = false;
    document.forma_contacto.btn_enviar.disabled = false;
    
    document.forma_contacto.btn_borrar.style.cursor = 'pointer';
    document.forma_contacto.btn_enviar.style.cursor = 'pointer';
   
    document.getElementById('link_portafolio').onclick = function() { mostrar('info', 0); };  // mostrar_t;
    document.icono_portafolio.onmouseover = function() { iconoOver('portafolio'); };  // iconoOver_t;
    document.icono_portafolio.onmouseout = function() { iconoOut('portafolio'); };  // iconoOut_t;
  }
  else {
    document.forma_contacto.btn_borrar.disabled = true;
    document.forma_contacto.btn_enviar.disabled = true;
    
    document.forma_contacto.btn_borrar.style.cursor = 'default';
    document.forma_contacto.btn_enviar.style.cursor = 'default';
    
    document.getElementById('link_portafolio').onclick = null;
    document.icono_portafolio.onmouseover = null;
    document.icono_portafolio.onmouseout = null;
  }
}

var isSafari = (navigator.userAgent.toLowerCase().indexOf('safari') != -1);

/*function confirmar(flag) {
  alert('BAH');
  if(flag) {
    document.getElementById('enviado').style.display = '';
  }
  else {
    borrar_forma();
    document.getElementById('enviado').style.display = 'none';
    mostrar('info', 0);
  }
}*/