// JavaScript Document

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareInputsForHints() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		// test to see if the hint span exists first
		if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
			// the span exists!  on focus, show the hint
			inputs[i].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			// when the cursor moves away from the field, hide the hint
			inputs[i].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
	// repeat the same tests as above for selects
	var selects = document.getElementsByTagName("select");
	for (var k=0; k<selects.length; k++){
		if (selects[k].parentNode.getElementsByTagName("span")[0]) {
			selects[k].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			selects[k].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
	// repeat the same tests as above for textbox
	var textareas = document.getElementsByTagName("textarea");
	for (var t=0; t<textareas.length; t++){
		if (textareas[t].parentNode.getElementsByTagName("span")[0]) {
			textareas[t].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			textareas[t].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
	
	
	
}
addLoadEvent(prepareInputsForHints);

//revision de los cmapos obligatorios del formulario

function enviar(){
	//Comprobamos que esten rellenos los campos obligatorios
	check = window.document.forms[1].check.value;
	atencion = window.document.forms[1].atencion.value;
	nombre = window.document.forms[1].nombre.value;
	direccion = window.document.forms[1].direccion.value;
	provincia = window.document.forms[1].provincia.value;
	pais = window.document.forms[1].pais.value;
	telefono = window.document.forms[1].telefono.value;
	email = window.document.forms[1].email.value;
	observaciones = window.document.forms[1].observaciones.value;

	if (nombre == ""){
		msg = "Introduzca su Nombre completo";
		alert(msg);
		window.document.forms[1].nombre.focus();
		window.document.forms[1].nombre.select();
		return false;
	}
	
	if (telefono == ""){
		msg = "El telefono es obligatorio";
		alert(msg);
		window.document.forms[1].telefono.focus();
		window.document.forms[1].telefono.select();
		return false;
	}
	
	if (email == ""){
		msg = "E-mail es un campo obligatorio";
		alert(msg);
		window.document.forms[1].email.focus();
		window.document.forms[1].email.select();
		return false;
	}	
	

	
	window.document.forms[1].submit();
}

function borrar(){
	window.document.forms[1].reset();
}


//FUNCIÓN PARA CONTROLAR QUE UN MAIL SEA VÁLIDO
function mailcorrecto(cadena){
	//Comprobamos que no sea una cadena vacía
	if (cadena == ""){
		return false;
	}

	//Comprobación de caracteres no válidos
	if ((cadena.indexOf("..") != -1) || (cadena.indexOf("@@") != -1)){
		return false;
	}

	//Comprobamos si tiene @
	if (cadena.indexOf("@") == -1){
		return false;
	}

	//Comprobamos el nombre de usuario
	nombreusuario = cadena.substring(0, cadena.indexOf("@"))

	if (nombreusuario.length < 3){
		return false;
	}

	//Comprobamos el dominio
	/*if (nombreusuario.length + 1 <= cadena.length){
		return false;
	}*/
	dominio = cadena.substring(cadena.indexOf("@") + 1, cadena.length)

	//Comprobamos si tiene punto (.)
	if (dominio.indexOf(".") == -1){
		return false
	}

	nombre = dominio.substring(0, dominio.indexOf("."))

	//Comprobamos el nombre
	if (nombre.length  < 3){
		return false;
	}
	extension = dominio.substring(dominio.indexOf(".") + 1, dominio.length)

	//Comprobamos la extensión
	if (extension.length < 2){
		return false;
	}

	return true;
}



//Chequeo del form de mi cuenta

function login(){
	//Comprobamos que esten rellenos los campos obligatorios
	usuario = window.document.forms[1].usuario.value;
	clave = window.document.forms[1].clave.value;

	if (usuario == ""){
		msg = "Introduzca su Nombre de usuario";
		alert(msg);
		window.document.forms[1].usuario.focus();
		window.document.forms[1].usuario.select();
		return false;
	}
	
	if (clave == ""){
		msg = "Introduzca su clave de acceso";
		alert(msg);
		window.document.forms[1].clave.focus();
		window.document.forms[1].clave.select();
		return false;
	}
	

	
	window.document.forms[0].submit();
}

//Chequeo del form de mi cuenta

function recuperar(){
	//Comprobamos que esten rellenos los campos obligatorios
	email = window.document.forms[2].email.value;

	if (email == ""){
		msg = "Introduzca su Email";
		alert(msg);
		window.document.forms[2].email.focus();
		window.document.forms[2].email.select();
		return false;
	}
	
	

	
	window.document.forms[0].submit();
}



function tecnica(){
	//Comprobamos que esten rellenos los campos obligatorios
	nombre = window.document.forms[1].nombre.value;
	cif = window.document.forms[1].cif.value;
	contacto = window.document.forms[1].contacto.value;
	direccion = window.document.forms[1].direccion.value;
	localidad = window.document.forms[1].localidad.value;
	pais = window.document.forms[1].pais.value;
	cpostal = window.document.forms[1].cpostal.value;
	telefono1 = window.document.forms[1].telefono1.value;
	telefono2 = window.document.forms[1].telefono2.value;
	fax = window.document.forms[1].fax.value;
	email = window.document.forms[1].email.value;
	comunicado = window.document.forms[1].comunicado.value;
	observaciones = window.document.forms[1].observaciones.value;

	if (nombre == ""){
		msg = "El campo con la Razon Social es obligatorio";
		alert(msg);
		window.document.forms[1].nombre.focus();
		window.document.forms[1].nombre.select();
		return false;
	}
	
	if (cif == ""){
		msg = "El campo con el CIF es obligatorio";
		alert(msg);
		window.document.forms[1].cif.focus();
		window.document.forms[1].cif.select();
		return false;
	}
	
		
	if (contacto == ""){
		msg = "Ponga el nombre de la persona de contacto";
		alert(msg);
		window.document.forms[1].contacto.focus();
		window.document.forms[1].contacto.select();
		return false;
	}
	
	if (telefono1 == ""){
		msg = "El Telefono es obligatorio";
		alert(msg);
		window.document.forms[1].telefono1.focus();
		window.document.forms[1].telefono1.select();
		return false;
	}
	
	if (email == ""){
		msg = "E-mail es un campo obligatorio";
		alert(msg);
		window.document.forms[1].email.focus();
		window.document.forms[1].email.select();
		return false;
	}
	

	
	window.document.forms[1].submit();
}

<!--
//------------------------------------------/
// Code from http://www.NetEvolution.co.uk
//------------------------------------------/
function checkCheckBoxes() {
	if (document.frmTest.checkbox.checked == false)
		{
		alert ('Debes seleccionar al menos un producto para poder proseguir');
		return false;
		}
	else
		{
		return true;
		}
	}
//-->

<!--
function procesar() {
    var answer = confirm("¿Deseas procesar la oferta? \n\nPinchando aqui los datos se enviarán al departamento comercial para proceder con su solicitud.\n\n Gracias SGS Bombas.")
     if (answer){
     window.location = "finalizar.php";
     }
    
}
//-->





