/**************************************************************************************************************************************************
'***********************												FUNCIONES DE VALIDACIÓN																	 '***********************
'**************************************************************************************************************************************************/

/* ---------------------------- Comprobar Formato Email -------------------------------------------
Admite:	- arroba, a-z, A-Z, 0-9, guión (-), guión bajo (_) y  punto (.)
		- Tiene que haber una sola arroba
		- A la derecha de la arroba, habrá al menos dos grupos de caracteres separados por un punto. 
			El último de ellos tendrá dos o más letras (a-z, A-Z).
		- A la izquierda de la arroba, habrá al menos un carácter.
Recibe el objeto TextFiel y devuelve true si contiene lo antes descrito y falso en cualquier otro caso
*/
function Validar_Email(campo)
{
	if (campo.value != "")
	{
		if (!campo.value.match(/^[\w-_\.]+@([\w-_]+\.)+[A-Za-z]{2,}$/ig)) {  
    		return false;
		}
	}
	return true;
}


/**************************************************************************************************************************************************
'***********************												FUNCIONES DE CALENDARIO																 '***********************
'**************************************************************************************************************************************************/


function EDCal(NombreCampo,Camino, Idioma){

	eval("var valor = document.theform." + NombreCampo + ".value;");
	if ( valor != '' ) {
		if (!(isValidDate( valor, Idioma))){
 	    	eval("document.theform." + NombreCampo + ".select();");
        	eval("document.theform." + NombreCampo + ".focus();");
			return false;
  		}
	}
	//Definir el formato de fecha a utilizar en el calendario.
	selectedLanguage = Idioma.toLowerCase( )
	if (Idioma.toUpperCase( ) == 'EU')
		calDateFormat    = "yyyy/MM/DD";
	else
		calDateFormat    = "DD/MM/yyyy";
	eval("setDateField(document.theform."+ NombreCampo +");");
	var CamiCalen = Camino + "calendario.html";
	top.newWin = window.open(CamiCalen,'cal','width=210,height=230','dependent=yes,screenX=200, screenY=300, titlebar=yes');
     return true;
}
	
function EDCal2(NombreCampoF){
		//setDateField(document.theform.fecha);
		eval("setDateField(document.theform."+ NombreCampoF +");");
	var CamiCalen = Camino + "calendario.html";
        top.newWin = window.open(CamiCalen, 'cal', 'dependent=yes, width=210, height=230, screenX=200, screenY=300, titlebar=yes');
     return true;
    }
	
/*
function EDCal2(){		
		setDateField(document.theform.ffin);
        top.newWin = window.open('calendario.html', 'cal', 'dependent=yes, width=210, height=230, screenX=200, screenY=300, titlebar=yes');
     return true;
    }
*/

function date_to_str_eur (fecha) {

		var Strfecha = ((fecha.getDate()<10) ? "0" : "" ) + fecha.getDate() + "/";
		Strfecha += (((fecha.getMonth() + 1) <10) ? "0" : "") + (fecha.getMonth() + 1) + "/";
		Strfecha += fecha.getFullYear();
		return Strfecha
}

function date_to_str_ekr (fecha) {

		var Strfecha = fecha.getFullYear() + "/";
		Strfecha += (((fecha.getMonth() + 1) <10) ? "0" : "") + (fecha.getMonth() + 1) + "/";
		Strfecha += ((fecha.getDate()<10) ? "0" : "" ) + fecha.getDate();
		return Strfecha
}



//Valida una fecha en formato yyyy-mm-dd o dd/mm/yyyy
function isValidDate(dateStr, idioma)
{
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year
	var datePat_eu = /^(\d{4})(\/|-)(\d{1,2})\2(\d{1,2})$/; // requires 4 digit year

	textmes=new Array(12)
	textmes[1]="Enero"
	textmes[2]="Febrero"
	textmes[3]="Marzo"
	textmes[4]="Abril"
	textmes[5]="Mayo"
	textmes[6]="Junio"
	textmes[7]="Julio"
	textmes[8]="Agosto"
	textmes[9]="Septiembre"
	textmes[10]="Octubre"
	textmes[11]="Noviembre"
	textmes[12]="Diciembre"

	if (idioma.toUpperCase( ) == "EU")
		var matchArray = dateStr.match(datePat_eu); // is the format ok?
	else
		var matchArray = dateStr.match(datePat); // is the format ok?
		
	if (matchArray == null) 
		{
			if (idioma.toUpperCase( ) == "EU")
				alert(dateStr + " Dataren formatua oker dago.")
			else
				alert(dateStr + " Fecha con formato no válido.")
			return false;
		}
	if (idioma.toUpperCase( ) == "EU") {
		year = matchArray[1]; // parse date into variables
		month = matchArray[3];
		day = matchArray[4]; 
	} else {
		day = matchArray[1]; // parse date into variables
		month = matchArray[3];
		year = matchArray[4];
	}
	if (month < 1 || month > 12) 
		{ // check month range
			if (idioma.toUpperCase( ) == "EU")
				alert("Hilabeteak urtarrila eta abenduaren artekoa izan behar du." );
			else	
				alert("Mes debe ser entre Enero y Diciembre" );
			return false;
		}
	if (day < 1 || day > 31) 
		{	if (idioma.toUpperCase( ) == "EU")
				alert("Egunak 1 eta 31aren artekoa izan behar du.");
			else
				alert("Día debe ser entre 1 y 31.");
			return false;
		}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
		{
		var ValMes =eval("textmes["+ month +"];");
		if (idioma.toUpperCase( ) == "EU")
			alert(ValMes+"k ez dauka 31 egun!")
		else
			alert("El mes de  "+ValMes+" no tiene 31 días!")
		return false;
		}
	if (month == 2) 
		{ // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) 
			{if (idioma.toUpperCase( ) == "EU")
				alert(year + "ko otsailak ez dauka " + day + " egun!");

			 else	
				 alert("Febrero del año " + year + " no tiene " + day + " días!");
			return false;
		   }
		}
	if (year <= 1900 ) 
		{	if (idioma.toUpperCase( ) == "EU")
				alert("El año debe ser mayor a 1900.EU");
			else
				alert("El año debe ser mayor a 1900.");
			return false;
		}

return true;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////