//Valida si la fecha introducida tiene el formato correcto.
function validateDate(oInput){ 
	return new RegExp("^\\d{2}\\/\\d{2}\\/\\d{4}$").test(oInput); 
} 

//comprueba que la fecha sea correcta, bisiestos, etc.
function ValidDate(y, m, d) { // m = 0..11
  with (new Date(y, m, d))
     return ((getFullYear()==y) && (getMonth()==m)) 
}
//comprobacion para los dos formatos con yyyy/mm/dd y yyyy-mm-dd
function validarFecha(fechaIn) { 
  var T // adaptable for other layouts
  var fechaOut = fechaIn.substring(6,10)+"/"+fechaIn.substring(3,5)+"/"+fechaIn.substring(0,2);
  if ((T = /^(\d+)([-\/])(\d\d)(\2)(\d\d)$/.exec(fechaOut)) == null)
    { return false } // bad format
  if (!ValidDate(T[1], T[3]-1, T[5])) { return false } // bad value
  return true 
}

function checkEmail (strng) {
var fallo="";
if (strng == "") {
   fallo = "La direccion de correo electronico es incorrecta.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       fallo = "Por favor, introduce una dirección de correo electrónico valida.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          fallo = "La direccion de correo electronico contiene caracteres no válidos.\n";
       }
    }
return fallo;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var fallo = "";
if (strng == "") {
   fallo = "Nos has introducido un numero de telefono.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       fallo = "El numero de telefono contiene caracteres no válidos.\n";
  
    }
    /*
    No tebnemos es cuenta la longitud del numero de telefono, dado la gran cantidad de casos validos posibles  
    if (!(stripped.length == 10)) {
	fallo = "The phone number is the wrong length. Make sure you included an area code.\n";
    }
    */ 
return fallo;
}


// password - entre un numero "minimo" y otro "maximo", uppercase, lowercase, and numeral

function checkPassword (strng) 
{
minimo =6;
maximo =8;
var fallo = "";
if (strng == "") {
   fallo = "No has introducido ninguna password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < minimo) || (strng.length > maximo)) {
       fallo = "La password tiene menos de "+minimo+" caracteres o más de "+maximo+" caracteres.\n";
    }
    else if (illegalChars.test(strng)) {
      fallo = "La password introducida contiene caracteres no validos.\n";
    }
    /* 
    No obligamos a que la password contenga una mayuscula, una minuscula y un numero
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       fallo = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
    */
return fallo;    
}    


// username - "minimo"-"maximo" chars, uc, lc, and underscore only.

function checkUsername (strng) 
{
minimo=4;
maximo=10;
var fallo = "";
if (strng == "") {
   fallo = "No has introducido nombre de usuario.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < minimo) || (strng.length > maximo)) {
       fallo = "El nombre de usuario introducido es menor de "+minimo+" caracteres o mayor de "+maximo+" caracteres.\n";
    }
    else if (illegalChars.test(strng)) {
    fallo = "El nombre de usuario contiene caracteres no validos.\n";
    } 
return fallo;
}       


// non-empty textbox

function isEmpty(strng, nombrecampo) {
var fallo = "";
  if (strng.length == 0) {
     fallo = "El campo de texto "+nombrecampo+" no ha sido rellenado.\n"
  }
return fallo;	  
}

/*
antigua, no miraba decimales
function isNumber(inputVal) {
      oneDecimal = false
      inputStr = ""+inputVal
      for (var i=0; i < inputStr.length; i++) {
        var oneChar = inputStr.charAt(i)
        if ( i == 0 && oneChar == "-") {
          continue
        }
        if (oneChar == "." && !oneDecimal) {
          oneDecimal = true
          continue
        }
        if (oneChar < "0" || oneChar > "9") {
          return false
        }
      }
      return true
}
*/
function isNumber(inputVal) {
      oneDecimal = false
      inputStr = ""+inputVal
      for (var i=0; i < inputStr.length; i++) {
        var oneChar = inputStr.charAt(i)
        if ( i == 0 && oneChar == "-") {
          continue
        }
        if (oneChar == "." && !oneDecimal) {
          return false;
        }
        if (oneChar < "0" || oneChar > "9") {
          return false
        }
      }
      return true
}



/*****************************************************************************
******************************************************************************
Esto no lo vamos a utilizar
// was textbox altered

function isDifferent(strng) {
var fallo = ""; 
  if (strng != "Can\'t touch this!") {
     fallo = "You altered the inviolate text area.\n";
  }
return fallo;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var fallo = "";
   if (!(checkvalue)) {
       fallo = "Please check a radio button.\n";
    }
return fallo;
}

// valid selector from dropdown list

function checkDropdown(choice) {
var fallo = "";
    if (choice == 0) {
    fallo = "You didn't choose an option from the drop-down list.\n";
    }    
return fallo;
}    
*/
function scapar_noticia(str_entrada){
	re=/'/gi; //'
	str=str_entrada.replace(re,"´");
	re=/"/gi; //"
	str=str.replace(re,"`");
	re=/\n/gi; //
	str=str.replace(re,"");
	re=/\r/gi; //
	str=str.replace(re,"");
	return str;
	
	
}
function scapar(str_entrada){
	re=/'/gi; //'
	str=str_entrada.replace(re,"%2%");
	re=/"/gi; //"
	str=str.replace(re,"%1%");
	return str;
}
function unscapar(str_entrada){
	re=/%1%/gi; //"
	str=str_entrada.replace(re,'"');
	re=/%2%/gi; //'
	str=str.replace(re,"'");
	return str;
}
