/*Situa el foco en el primer elemento del formulario<BODY onLoad="firstFocus()">  */

function firstFocus(strPage1){
 var strPage1;
 if(strPage1.toUpperCase()!="Convocatorias.asp".toUpperCase()){
   if (document.forms.length > 0)
   {
      var TForm = document.forms[0];
      for (i=0;i<TForm.length;i++)
      { 
      	if(document.forms[0].elements[i].disabled==true){
      		 break;
      	}
      	 
         if ((TForm.elements[i].type=="text") || (TForm.elements[i].type=="textarea") || (TForm.elements[i].type.toString().charAt(0)=="s"))
         {
            document.forms[0].elements[i].focus();
            break;
         }
        
      }
      
   }
 }     
}

// Función que comprueba que caracteres estan prohibidos
function checkCaracteresProhibidos(form_object, input_object, object_type, Caracteres, error_message){
error_message=error_message+"\' \" "+Caracteres;
Caracteres="\'\""+Caracteres;

var caracter;
var isplit;
	if (form_object.value.length == 0){
        return true;
	}
	for (var i = 0; i < form_object.value.length; i++){
		caracter=form_object.value.substring(i,i+1);
		isplit = Caracteres.indexOf(caracter);
		if (isplit != -1){
			onError(form_object, input_object, object_type, error_message);
			return false;
		}
	}
	
}

function onError(form_object, input_object, object_type, error_message){
		alert(error_message);
        if (object_type == "RADIO" || object_type == "CHECKBOX") {
        	if (input_object[0]){
				input_object[0].focus();
			}else{
				input_object.focus();
			}
		}else{
			input_object.focus();
		}

		if (object_type == "text" || object_type == "password" || object_type == "textarea"){
			input_object.select();
			input_object.value="";
			input_object.focus();
		}	
       	return false;	
}


function hasValue(obj, obj_type){
    if (obj_type == "text" || obj_type == "PASSWORD" || obj_type == "TEXTAREA"){
			if (obj.value.length == 0){
				return false;
			}else{ 
				return true;
			}
	}else if (obj_type == "SELECT"){
			if (obj.selectedIndex == 0){
				return false;
			}else{
				return true;	
			}
	}else if (obj_type == "RADIO" || obj_type == "CHECKBOX"){
        if (obj[0]){
			for (i=0; i < obj.length; i++){
				if (obj[i].checked){
				return true;
				}
			}
		}else{
			if (obj.checked){
				return true;
			}else{
	       		return false;	
			}
		}
       	return false;	
	}
}

//validar telefono españa
function checktelefono(form_object, input_object, object_type, error_message){ 
    if (form_object.value.length == 0){
        return true;
	}
	if (form_object.value.length != 9){
		onError(form_object, input_object, object_type, error_message);
		return false;
    }
	if (checkinteger(form_object.value)==false){
		onError(form_object, input_object, object_type, error_message);
		return false;
	}
}
	
// Date (mm/dd/yyyy)
function checkusdate(object_value){
    if (object_value.length == 0){
        return true;
	}
   	isplit = object_value.indexOf('/');

	if (isplit == -1 || isplit == object_value.length){
		return false;
	}
    sMonth = object_value.substring(0, isplit);

	if (sMonth.length == 0){
        return false;
	}
	isplit = object_value.indexOf('/', isplit + 1);

	if (isplit == -1 || (isplit + 1 ) == object_value.length){
		return false;
	}
    sDay = object_value.substring((sMonth.length + 1), isplit);

	if (sDay.length == 0){
        return false;
	}
	sYear = object_value.substring(isplit + 1);

	if (!checkinteger(sMonth)){
		return false;
	}else{
		if (!checkrange(sMonth, 1, 12)){ 
			return false;
		}else{
			if (!checkinteger(sYear)){ 
				return false;
			}else{
				if (!checkrange(sYear, 0, 9999)){ 
					return false;
				}else{
					if (!checkinteger(sDay)){
						return false;
					}else{
						if (!checkday(sYear, sMonth, sDay)){
							return false;
						}else{
							return true;
						}
					}
				}
			}
		}
	}
}

	
// Date (yyyy/mm/dd)
function checkdate(object_value){
    if (object_value.length == 0){
        return true;
	}
	isplit = object_value.indexOf('/');

	if (isplit == -1 || isplit == object_value.length){
		return false;
	}
    sYear = object_value.substring(0, isplit);

	isplit = object_value.indexOf('/', isplit + 1);

	if (isplit == -1 || (isplit + 1 ) == object_value.length){
		return false;
	}
    sMonth = object_value.substring((sYear.length + 1), isplit);
	
	if (sMonth.length == 0){
        return false;
	}
	sDay = object_value.substring(isplit + 1);
	
	if (sDay.length == 0){
        return false;
	}
	if (!checkinteger(sMonth)){
		return false;
	}else{
		if (!checkrange(sMonth, 1, 12)){ 
			return false;
		}else{
			if (!checkinteger(sYear)){ 
				return false;
			}else{
				if (!checkrange(sYear, 0, 9999)){ 
					return false;
				}else{
					if (!checkinteger(sDay)){ 
						return false;
					}else{
						if (!checkday(sYear, sMonth, sDay)){
							return false;
						}else{
							return true;
						}
					}
				}
			}
		}
	}
}

//chequeo el rango de fechas
function checkRangedate(form_object, input_object, object_type,form_object1,input_object1, object_type1, error_message){
	isplit = form_object.value.indexOf('/');
	sDay = form_object.value.substring(0, isplit);
	monthSplit = isplit + 1;
	isplit = form_object.value.indexOf('/', monthSplit);
    sMonth = form_object.value.substring((sDay.length + 1), isplit);
	sYear = form_object.value.substring(isplit + 1);
	var Fecha=new Date(sYear,sMonth,sDay);

	isplit1 = form_object1.value.indexOf('/');
	sDay1 = form_object1.value.substring(0, isplit1);
	monthSplit1 = isplit1 + 1;
	isplit1 = form_object1.value.indexOf('/', monthSplit1);
    sMonth1 = form_object1.value.substring((sDay1.length + 1), isplit1);
	sYear1 = form_object1.value.substring(isplit + 1);
	var Fecha1=new Date(sYear1,sMonth1,sDay1);
	
	if(Fecha>Fecha1){
		onError(form_object, input_object, object_type, error_message);
		return false;
	}

}

//chequeo el rango de fechas con formato 'mm/yyyy' y 'yyyy/mm'
function checkRangedateCorto(form_object, input_object, object_type,form_object1,input_object1, object_type1, error_message){
	isplit = form_object.value.indexOf('/');
	sDay = '01';
    sMonth = form_object.value.substring(0, isplit);
	sYear = form_object.value.substring(isplit + 1);	
	if (isplit>=4){
		var Fecha=new Date(sMonth,sYear,sDay);
	}else{
		var Fecha=new Date(sYear,sMonth,sDay);
	}

	isplit1 = form_object1.value.indexOf('/');
	sDay1 = '01'
    sMonth1 = form_object1.value.substring(0, isplit1);
	sYear1 = form_object1.value.substring(isplit + 1);
	if (isplit>=4){
		var Fecha1=new Date(sMonth1,sYear1,sDay1);
	}else{
		var Fecha1=new Date(sYear1,sMonth1,sDay1);
	}
	


	if(Fecha>Fecha1){
		onError(form_object, input_object, object_type, error_message);
		return false;
	}
}

function checkday(checkYear, checkMonth, checkDay){

	maxDay = 31;

	if (checkMonth == 4 || checkMonth == 6 || checkMonth == 9 || checkMonth == 11){
		maxDay = 30;
	}else{
		if (checkMonth == 2){
			if (checkYear % 4 > 0){
				maxDay =28;
			}else{
				if (checkYear % 100 == 0 && checkYear % 400 > 0){
					maxDay = 28;
				}else{
					maxDay = 29;
				}
			}
		}
	}

	return checkrange(checkDay, 1, maxDay); 
}



function checkinteger(object_value){
    if (object_value.length == 0){
        return true;
	}
	var decimal_format = ".";
	var check_char;

	check_char = object_value.indexOf(decimal_format);
    if (check_char < 1){
		return checknumber(object_value);
    }else{
		return false;
	}
}




function checknumber(object_value){
    if (object_value.length == 0)
        return true;

	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

	check_char = start_format.indexOf(object_value.charAt(0))
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
		}
	    else if (trailing_blank)
			return false;
		else
			digits = true;
	}	

    return true
    }



function checkrange(object_value, min_value, max_value)
    {
    if (object_value.length == 0)
        return true;

    if (!checknumber(object_value))
	{
	return false;
	}
    else
	{
	return (numberrange((eval(object_value)), min_value, max_value));
	}
	
    return true;
    }



function checktime(object_value)
    {
    if (object_value.length == 0)
        return true;

	isplit = object_value.indexOf(':');

	if (isplit == -1 || isplit == object_value.length)
		return false;

    sHour = object_value.substring(0, isplit);
	iminute = object_value.indexOf(':', isplit + 1);

	if (iminute == -1 || iminute == object_value.length)
		sMin = object_value.substring((sHour.length + 1));
	else
		sMin = object_value.substring((sHour.length + 1), iminute);

    if (!checkinteger(sHour))
		return false;
    else
    if (!checkrange(sHour, 0, 23)) 
		return false;

	if (!checkinteger(sMin))
		return false;
	else
	if (!checkrange(sMin, 0, 59))
		return false;

    if (iminute != -1)
	{
		sSec = object_value.substring(iminute + 1);

		if (!checkinteger(sSec))
			return false;
		else
		if (!checkrange(sSec, 0, 59))
			return false;	
	}
    
    return true;
    }
    


function checkphone(object_value){
    if (object_value.length == 0){
        return true;
	}
    if (object_value.length != 12){
        return false;
	}
    if (!checknumber(object_value.substring(0,3))){
		return false;
	}else{
		if (!numberrange((eval(object_value.substring(0,3))), 100, 1000)){
			return false;
		}
		if (object_value.charAt(3) != "-" && object_value.charAt(3) != " "){
			return false
		}
		if (!checknumber(object_value.substring(4,7))){
			return false;
		}else{
			if (!numberrange((eval(object_value.substring(4,7))), 100, 1000)){
				return false;
			}
			if (object_value.charAt(7) != "-" && object_value.charAt(7) != " "){
				return false;
			}
			if (object_value.charAt(8) == "-" || object_value.charAt(8) == "+"){
				return false;
			}else{
				return (checkinteger(object_value.substring(8,12)));
			}
		}
	}
}


//validar año
function checkano(form_object, input_object, object_type, error_message){ 
    if (form_object.value.length == 0){
        return true;
	}
	if (form_object.value.length != 4){
		onError(form_object, input_object, object_type, error_message);
		return false;
	}

    if (checkinteger(form_object.value)==false){
		onError(form_object, input_object, object_type, error_message);
		return false;
	}
}

//validar NIF = DNI(entero+letra)
function checkNIF(form_object, input_object, object_type, error_message,error_message_vacio){
    if (form_object.value.length == 0){
        return true;
	}
	if(checkCaracteresProhibidos(form_object, input_object, object_type," ", error_message_vacio)==false){
		return false;
	}
	if (form_object.value.length<7){
		onError(form_object, input_object, object_type, error_message);
		return false;
	}
	var NIE;
	//compruebo si es un NIE en vez de un NIF
	if (form_object.value.substring(0,1)=='x' || form_object.value.substring(0,1)=='X'){
		NIE=true;
	}
	if(NIE==true){

		if (!checkinteger(form_object.value.substring(1,form_object.value.length-1))){
			onError(form_object, input_object, object_type, error_message);
			return false;
		}


		if (!checkcaracter(form_object.value.substring(form_object.value.length-1,form_object.value.length))){
			if (!checkinteger(form_object.value.substring(1,form_object.value.length-1))){
				onError(form_object, input_object, object_type, error_message);
				return false;
			}else{
				onError(form_object, input_object, object_type, error_message);
				return false;
				/*var letras = 'TRWAGMYFPDXBNJZSQVHLCKE';
				var numero = form_object.value%23;
				form_object.value = form_object.value+letras.substring(numero,numero+1);*/
			}
		}else{
			if (!checkinteger(form_object.value.substring(1,form_object.value.length-1))){
				onError(form_object, input_object, object_type, error_message);
				return false;
			}else{
				/*var letras = 'TRWAGMYFPDXBNJZSQVHLCKE';
				var numero = form_object.value.substring(0,form_object.value.length-1)%23;
				form_object.value = form_object.value.substring(0,form_object.value.length-1)+letras.substring(numero,numero+1);*/
				var letras = 'TRWAGMYFPDXBNJZSQVHLCKE';
				var numero = form_object.value.substring(1,form_object.value.length-1)%23;
				var dni_correcto = 'X'+form_object.value.substring(1,form_object.value.length-1)+letras.substring(numero,numero+1);
				var dni_introducido='X'+form_object.value.substring(1,form_object.value.length-1)+form_object.value.substring(form_object.value.length-1,form_object.value.length).toUpperCase();
				if(dni_correcto == dni_introducido){
					form_object.value = dni_introducido;
				}else{
					onError(form_object, input_object, object_type, error_message);
					return false;
				}
			}

		}
	}else{

		if (!checkinteger(form_object.value.substring(0,form_object.value.length-1))){
			onError(form_object, input_object, object_type, error_message);
			return false;
		}


		if (!checkcaracter(form_object.value.substring(form_object.value.length-1,form_object.value.length))){
			if (!checkinteger(form_object.value.substring(0,form_object.value.length-1))){
				onError(form_object, input_object, object_type, error_message);
				return false;
			}else{
				onError(form_object, input_object, object_type, error_message);
				return false;
				/*var letras = 'TRWAGMYFPDXBNJZSQVHLCKE';
				var numero = form_object.value%23;
				form_object.value = form_object.value+letras.substring(numero,numero+1);*/
			}
		}else{
			if (!checkinteger(form_object.value.substring(0,form_object.value.length-1))){
				onError(form_object, input_object, object_type, error_message);
				return false;
			}else{
				/*var letras = 'TRWAGMYFPDXBNJZSQVHLCKE';
				var numero = form_object.value.substring(0,form_object.value.length-1)%23;
				form_object.value = form_object.value.substring(0,form_object.value.length-1)+letras.substring(numero,numero+1);*/
				var letras = 'TRWAGMYFPDXBNJZSQVHLCKE';
				var numero = form_object.value.substring(0,form_object.value.length-1)%23;
				var dni_correcto = form_object.value.substring(0,form_object.value.length-1)+letras.substring(numero,numero+1);
				var dni_introducido=form_object.value.substring(0,form_object.value.length-1)+form_object.value.substring(form_object.value.length-1,form_object.value.length).toUpperCase();
				if(dni_correcto == dni_introducido){
					form_object.value = dni_introducido;
				}else{
					onError(form_object, input_object, object_type, error_message);
					return false;
				}
			}

		}
	}
}

//Comprueba si un caracter es uina letra	
function checkcaracter(caracter){
	var caracter_format = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ";
	var check_caracter;
	check_caracter = caracter_format.indexOf(caracter);
	if (check_caracter < 0){
		return false;
	}else{
		return true;
	}
}

//comprueba siel formato de e-mail es correcto
function checkemail(form_object, input_object, object_type, error_message){
    if (form_object.value.length == 0){
        return true;
	}else{
		if(!(form_object.value.indexOf("@") > -1 && form_object.value.indexOf(".") > -1)){
			 onError(form_object, input_object, object_type, error_message);
			 return false;    
		 }
		 return true;
	}
	return true;
}

function checkzip(object_value)
    {
    if (object_value.length == 0)
        return true;
		
    if (object_value.length != 5 && object_value.length != 10)
        return false;

	if (object_value.charAt(0) == "-" || object_value.charAt(0) == "+")
        return false;

	if (!checkinteger(object_value.substring(0,5)))
		return false;

	if (object_value.length == 5)
		return true;
	
	if (object_value.charAt(5) != "-" && object_value.charAt(5) != " ")
        return false;

	if (object_value.charAt(6) == "-" || object_value.charAt(6) == "+")
        return false;

	return (checkinteger(object_value.substring(6,10)));
    }


function checkcreditcard(object_value)
    {
	var white_space = " -";
	var creditcard_string="";
	var check_char;

    if (object_value.length == 0)
        return true;

	for (var i = 0; i < object_value.length; i++)
	{
		check_char = white_space.indexOf(object_value.charAt(i))
		if (check_char < 0)
			creditcard_string += object_value.substring(i, (i + 1));
	}	

    if (creditcard_string.length == 0)
        return false;	 
	 	
	if (creditcard_string.charAt(0) == "+")
        return false;

	if (!checkinteger(creditcard_string))
		return false;

	var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
	var checkdigit = 0;
	var tempdigit;

	for (var i = 0; i < creditcard_string.length; i++)
	{
		tempdigit = eval(creditcard_string.charAt(i))

		if (doubledigit)
		{
			tempdigit *= 2;
			checkdigit += (tempdigit % 10);

			if ((tempdigit / 10) >= 1.0)
			{
				checkdigit++;
			}

			doubledigit = false;
		}
		else
		{
			checkdigit += tempdigit;
			doubledigit = true;
		}
	}	
	return (checkdigit % 10) == 0 ? true : false;
    }


function checkssc(object_value)
    {
	var white_space = " -+.";
	var ssc_string="";
	var check_char;

    if (object_value.length == 0)
        return true;

    if (object_value.length != 11)
        return false;

	if (object_value.charAt(3) != "-" && object_value.charAt(3) != " ")
        return false;

	if (object_value.charAt(6) != "-" && object_value.charAt(6) != " ")
        return false;
	 
	for (var i = 0; i < object_value.length; i++)
	{
		check_char = white_space.indexOf(object_value.charAt(i))
		if (check_char < 0)
			ssc_string += object_value.substring(i, (i + 1));
	}	

    if (ssc_string.length != 9)
        return false;	 
	 	
	if (!checkinteger(ssc_string))
		return false;

	return true;
    }
	


	

// GUID {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}	
function checkGUID(object_value)
	{
	if (object_value.length == 0)
        return true;
	if (object_value.length != 38)
        return false;
	if (object_value.charAt(0)!="{")
		return false;
	if (object_value.charAt(37)!="}")
		return false;	

	var hex_format = "0123456789abcdefABCDEF";
	var check_char;	

	for (var i = 1; i < 37; i++)
	{		
		if ((i==9)||(i==14)||(i==19)||(i==24)) {
			if (object_value.charAt(i)!="-")
				return false;
		} else {
			check_char = hex_format.indexOf(object_value.charAt(i));
			if (check_char < 0)
				return false;
		}
	}
	return true;
	}


// Funcion de date aplicada
function checkeurodate(form_object, input_object, object_type, error_message,FormatoFecha){
    if (form_object.value.length == 0){
        return true;
	}
	
	isplit = form_object.value.indexOf('/');

	if (isplit == -1){
		isplit = form_object.value.indexOf('.');
	}

	if (isplit == -1 || isplit == form_object.value.length){
		onError(form_object, input_object, object_type, error_message);
		return false;
	}

	if (FormatoFecha=='dd/mm/yyyy')
	{
		sDay = form_object.value.substring(0, isplit);

		monthSplit = isplit + 1;

		isplit = form_object.value.indexOf('/', monthSplit);

		if (isplit == -1){
			isplit = form_object.value.indexOf('.', monthSplit);
		}

		if (isplit == -1 ||  (isplit + 1 )  == form_object.value.length){
			onError(form_object, input_object, object_type, error_message);
			return false;
		}

		sMonth = form_object.value.substring((sDay.length + 1), isplit);

		sYear = form_object.value.substring(isplit + 1);
	}

	if (FormatoFecha=='yyyy/mm/dd')
	{

		sYear = form_object.value.substring(0, isplit);

		monthSplit = isplit + 1;

		isplit = form_object.value.indexOf('/', monthSplit);

		if (isplit == -1){
			isplit = form_object.value.indexOf('.', monthSplit);
		}

		if (isplit == -1 ||  (isplit + 1 )  == form_object.value.length){
			onError(form_object, input_object, object_type, error_message);
			return false;
		}

		sMonth = form_object.value.substring((sYear.length + 1), isplit);

		sDay = form_object.value.substring(isplit + 1);
	}

	if (FormatoFecha=='mm/dd/yyyy')
	{ 	
		sMonth = form_object.value.substring(0, isplit);
		monthSplit = isplit + 1;
		isplit = form_object.value.indexOf('/', monthSplit);

		if (isplit == -1){
			isplit = form_object.value.indexOf('.', monthSplit);
		}

		if (isplit == -1 ||  (isplit + 1 )  == form_object.value.length){
			onError(form_object, input_object, object_type, error_message);
			return false;
		}

		sDay = form_object.value.substring((sMonth.length + 1), isplit);

		sYear = form_object.value.substring(isplit + 1);
	}

	if (FormatoFecha=='mm/yyyy')
	{ 	
		sMonth = form_object.value.substring(0, isplit);
		monthSplit = isplit ;
		isplit = form_object.value.indexOf('/', monthSplit);

		if (isplit == -1){
			isplit = form_object.value.indexOf('.', monthSplit);
		}

		if (isplit == -1 ||  (isplit)  == form_object.value.length){
			onError(form_object, input_object, object_type, error_message);
			return false;
		}

		sDay = "01" //form_object.value.substring((sMonth.length + 1), isplit);
		sYear = form_object.value.substring(isplit + 1);
	}
	
	if (FormatoFecha=='yyyy/mm')
	{
		sYear = form_object.value.substring(0, isplit);
		monthSplit = isplit;
		isplit = form_object.value.indexOf('/', monthSplit);

		if (isplit == -1){
			isplit = form_object.value.indexOf('.', monthSplit);
		}

		if (isplit == -1 ||  (isplit)  == form_object.value.length){
			onError(form_object, input_object, object_type, error_message);
			return false;
		}

		sMonth = form_object.value.substring(isplit + 1);
		sDay = "01";
	}
	
	if (!checkinteger(sMonth)){
		onError(form_object, input_object, object_type, error_message);
		return false;
	}else if(!checkrange(sMonth, 1, 12)){ 
		onError(form_object, input_object, object_type, error_message);
		return false;
	}else if(!checkinteger(sYear)){
		onError(form_object, input_object, object_type, error_message);
		return false;
	}else if(!checkrange(sYear, 0, null)){
		onError(form_object, input_object, object_type, error_message);
		return false;
	}else if(!checkinteger(sDay)){
		onError(form_object, input_object, object_type, error_message);
		return false;
	}else if(!checkday(sYear, sMonth, sDay)){
		onError(form_object, input_object, object_type, error_message);
		return false;
	}else
		return true;
}

function numberrange(object_value, min_value, max_value){
    if (min_value != null){
        if (object_value < min_value)
		return false;
	}

    if (max_value != null){
		if (object_value > max_value)
			return false;
		}
	
    return true;
}