// JavaScript Document

var isDHTML  = 0;
var isLayers = 0;
var isAll    = 0;
var isID     = 0;

if (document.getElementById){
  isID    = 1;
  isDHTML = 1;
} else {
  if (document.all){
    isAll   = 1;
    isDHTML = 1;
  } else {
    browserVersion = parseInt(navigator.appVersion);
    if    ((navigator.appName.indexOf('Netscape') != -1)
       && (browserVersion == 4)){
      isLayers = 1;
      isDHTML  = 1;
    }
  }
}

/*
 Captura el ID asociado del objeto deseado y crea el DOM
 correspondiente al navegador que está utilizando el visitante.
 Con esta función también se podrá modificar el estilo del
 objeto (withStyle=1) u otras propiedades asociadas con el
 objeto (withStyle=0).
*/
function findDOM(objectID, withStyle){
  // Devuelve el estilo del objeto.
  if (withStyle == 1){
    if (isID){
      return (document.getElementById(objectID).style);
    } else {
      if (isAll){
        return (document.all[objectID].style);
      } else if (isLayers){
          return (document.layers[objectID]);
      }
    }
  // Devuelve el DOM del objeto.
  } else {
    if (isID){
      return (document.getElementById(objectID));
    } else {
      if (isAll){
        return (document.all[objectID]);
      } else if (isLayers){
          return (document.layers[objectID]);
      }
    }
  }
}

/*-------------------------------------------------*/

/*
 * validarFormulario(formulario) 
 * Comprueba los campos obligatorios estén rellenados correctamente.
 */ 
function validarFormulario(formulario) {
  	var correcto = true;
	
  	correcto = checkBlank(formulario.name);
	
	if (correcto){
		correcto = checkLetras(formulario.name);
	}
	
	if (correcto){
		correcto = checkBlank(formulario.organization);
	}
	
	if (correcto){
		correcto = checkBlank(formulario.paises);
	}
	
	if (correcto){
		correcto = checkBlank(formulario.mail);
	}
	
	if (correcto){
		correcto = isEmailAddress(formulario.mail)
	}
	
	if (correcto){
		correcto = checkBlank(formulario.productApplication);
	}
	
	/*if ((correcto) && (document.all.serialNumber.style.display == 'block')){*/
	if (correcto){
		domStyle = findDOM('serialNumber',1);

		if (domStyle.display == "block"){
			correcto = checkBlank(formulario.serial_number);
			
			if (correcto){
				correcto = checkSerialNumber(formulario.serial_number);
			}
		}
	}
	
	if (correcto){
		correcto = checkBlank(formulario.motivo);
	}
	
  	return correcto;
}

/*
 * checkBlank(form)
 * Comprueba que se haya entrado información en los diferentes campos.
 */
function checkBlank(campo){
	var no_blanco = true;
	var valor;
	
	valor = campo.value;
		
	/* Se quitan los espacios del principio. */
	while('' + valor.charAt(0)==' '){
		valor = valor.substring(1,valor.length);
	}
		
	campo.value = valor;
	
	if(valor==""){
		no_blanco = false;
	}

	/* Se da el mensaje de error si procede. */
	if (!no_blanco){
		alert("Please, fill in compulsory fields (*).");
		campo.focus();
	}
	
	return no_blanco;
}

/*
 * checkLetras(campo)
 * Comprueba que solo se haya entrado texto.
 */
function checkLetras(campo){
	var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ " + "abcdefghijklmnñopqrstuvwxyzáéíóú ";
	var checkStr = campo.value;
	var allValid = true;
	
	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) {
		   allValid = false;
		   break;
    	}
  	}
	
    if (!allValid) {
    	alert("Only alphanumeric values are allow.");
	    campo.focus();
	    return false;
    }
	
	return true;
}

/*
 * emailCheck(emailStr)
 * valida una dirección de correo. 
 */
function isEmailAddress(mail){
	var e = mail.value;
	var filter=/^[A-Za-z][A-Za-z0-9_.-]*@[A-Za-z0-9_-]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	
	if (filter.test(e)){
		return true;
	} else {
		alert("You have entered a wrong e-mail address.\n Please enter a correct e-mail address according to the structure ‘user@domain’.");
		mail.focus();
	}
	
	return false;
}

/*
 * checkSerialNumber()
 * Valida el campo serial number, siendo posible dos formatos distintos 9112/05 -> hardware, 3b573-8ga -> software.
 */
function checkSerialNumber(serialNumber){
	var valor = serialNumber.value;
	var allOk = true;
		
	filterHard = /(^[0-9]{4})((\/|-|\s){1})([0-9]{2}$)/;
	filterSoft = /(^[A-Za-z0-9]{5})(\/|-|\s)([A-Za-z0-9]{3}$)/;
	
	if((!filterHard.test(valor)) && (!filterSoft.test(valor))){
		allOk = false;
		alert("You have entered an invalid Serial Number.\nPlease enter a valid Serial Number according to the structures:\n - Software Products Serial Number example  > 1a23b-3aa.\n - Hardware Products Serial Number example > 1234-56.");
		serialNumber.focus();
	}
	
	return allOk;
}

function changeDisplay(campo){
	if((document.all.serialNumber.style.display != 'block') && (campo == 'Technical Assistance')){
		document.all.serialNumber.style.display = 'block';
	} else {
		document.all.serialNumber.style.display = 'none';		
	}
}

function toggleClamShellMenu(objectID, valorCampo){
	domStyle = findDOM(objectID,1);
	
	if ((domStyle.display != "block") && (valorCampo == 'Technical Assistance')){
		domStyle.display = "block";
	} else {
		domStyle.display = "none";
	}

	return;
}


