var errorStrings = Array();

errorStrings[0] = "You have to select a POSITION from the list"; 
errorStrings[1] = "You have to fill the FIRST NAME field"; 
errorStrings[2] = "You have to fill the LAST NAME field"; 
errorStrings[3] = "You have to select a COUNTRY from the list"; 
errorStrings[4] = "You have to fill the BIRTH DATE field with a valid value (mm/dd/yyyy)"; 
errorStrings[5] = "You have to fill the E-MAIL field with a valid value"; 
errorStrings[6] = "You have to either fill the MESSAGE field or upload at least one DOCUMENT"; 
errorStrings[7] = "You have to read and accept the TERMS AND CONDITIONS"; 
errorStrings[8] = "You have to fill the DESCRIPTION field"; 
errorStrings[9] = "You have to select a non-empty file"; 
errorStrings[10] = "You can only upload a file with one of the allowed extensions."; 
errorStrings[11] = "You have to read and understand the POSITION REQUIREMENTS"; 
errorStrings[12] = "You do not meet the minimum age requirements for this position"; 
errorStrings[13] = "You have to fill the AVAILABILITY DATE field with a valid value (mm/dd/yyyy)"; 
errorStrings[14] = "The file you chose is too big"; 
errorStrings[15] = "Sorry but you don't seem to reach the minimum AGE requirements for this position."; 
errorStrings[16] = "AVAILABILITY DATE should be a value between now and 2 years in the future"; 
errorStrings[17] = "You have to select your GENDER"; 

function showError(id,oggetto){
	if(oggetto != null) oggetto.style.borderColor = "#FF0000";
	return errorStrings[id]+"\n";
}

//controlla se un valore è vuoto
function isEmpty(valore){
	if(valore == "" || valore.length==0) return true;
	return false;
}

//controlla se in un select l'indice selezionato non è lo zero
function isSelected(campo){
	return (campo.selectedIndex > 0);
}

//controlla se un valore può essere una e-mail
function isMail(valore){
	var myregexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	return (myregexp.test(valore) );
}

//controlla se un checkbox è checkato
function isChecked(oggetto){
	return oggetto.checked;
}

//resetta i bordi su errore
function resetBorders(formname,border_el){
	for(var a=0;a<border_el.length;a++){
		eval(formname+"."+border_el[a]).style.borderColor = "#FFFFFF";
	}
}

//controlla e nel caso effettua update
function doUpdate(formname, tipo){
	var obj = document.getElementById(formname);
	if(tipo == 0){
		obj.submit();
		return true;
	}
	var errorstring = "";
	var border_el = Array('apply_position','apply_firstname','apply_lastname','apply_country','apply_birthdate','apply_avadate','apply_email','apply_message','apply_gender');
	resetBorders(formname,border_el);	
	//if(!isSelected(obj.apply_position)) errorstring = errorstring + showError(0,obj.apply_position);
	if(!isSelected(obj.apply_gender)) errorstring = errorstring + showError(17,obj.apply_gender);
	if(isEmpty(obj.apply_firstname.value)) errorstring = errorstring + showError(1,obj.apply_firstname);
	if(isEmpty(obj.apply_lastname.value)) errorstring = errorstring + showError(2,obj.apply_lastname);
	if(!isSelected(obj.apply_country)) errorstring = errorstring + showError(3,obj.apply_country);
	//controllo la data di nascita in base alla posizione selezionata
	var esitoTmp = controllaData(obj.apply_birthdate.value,-100,-minimumPositionAge);
	if(isEmpty(obj.apply_birthdate.value) || !esitoTmp) errorstring = errorstring + showError(4,obj.apply_birthdate);
	else if(esitoTmp==-1) errorstring = errorstring + showError(15,obj.apply_birthdate);
	//controllo la data di disponibilità
	esitoTmp = controllaData(obj.apply_avadate.value,0,2);
	if(isEmpty(obj.apply_avadate.value) || !esitoTmp) errorstring = errorstring + showError(13,obj.apply_avadate);
	else if(esitoTmp==-1) errorstring = errorstring + showError(16,obj.apply_avadate);
	if(isEmpty(obj.apply_email.value) || !isMail(obj.apply_email.value)) errorstring = errorstring + showError(5,obj.apply_email);
	if(isEmpty(obj.apply_message) && okDalPHP == 0) errorstring = errorstring + showError(6,obj.apply_message);
	if(tipo == 1) if(!isChecked(document.getElementById("termsCondsForm").acceptTerms)) errorstring = errorstring + showError(7,null);
	if(errorstring.length >0) alert(errorstring);
	else{
		if(tipo == 1) obj.action.value="confirm";
		obj.submit();
	}
}

//controlla e nel caso imposta la posizione
function doSetPosition(formname){
	var obj = document.getElementById(formname);
	var errorstring = "";
	var border_el = Array('apply_position');
	if(!isSelected(obj.apply_position)) errorstring = errorstring + showError(0,obj.apply_position);
	if(!isChecked(obj.acceptRequirements)) errorstring = errorstring + showError(11,obj.acceptRequirements);
	if(errorstring.length >0) alert(errorstring);else obj.submit();
}

//effettua in automatico il save dei dati
function saveRedirect(tipo){
	var obj = document.getElementById("updateForm");
	obj.tipo_redirect.value = tipo;
	obj.submit();
}

//controlla e nel caso effettua upload
//inutilizzata per cambio tipo di upload
/*
function doUpload(formname){
	var errorstring = "";
	var border_el = Array('upl_description','upl_file');
	resetBorders(formname,border_el);
	var obj = document.getElementById(formname);
	if(isEmpty(obj.upl_description.value)) errorstring = errorstring + showError(8,obj.upl_description);
	if(isEmpty(obj.upl_file.value)) errorstring = errorstring + showError(9,obj.upl_file);
	if(!chkFileExt(obj.upl_file.value)) errorstring = errorstring + showError(10,obj.upl_file);
	if(errorstring.length >0) alert(errorstring);
	else iniziaUpload();
}*/

//prende l'estensione di un file
function chkFileExt(stringa){
	var temp = stringa.split(".");
	var tempext = temp[temp.length-1];
	tempext = tempext.toLowerCase();
	for(a=0;a<allowedExtensions.length;a++) if(allowedExtensions[a] == tempext) return true;
	return false;
}

function getDataByStringa(valore){
	if( !/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.test(valore) ) return 0;
	if(RegExp.$1 > 12) return 0;
	if(RegExp.$2 > 31) return 0;

	var d2 = new Date(RegExp.$3, RegExp.$1-1, RegExp.$2);
	//alert(d2.toString());
	return d2;
}

//controlla se la data è valida
function controllaData(valore,partenza,arrivo){
	var d2 = getDataByStringa(valore);
	if(d2 ==0) return 0;
	
	var d = new Date();
	var diffhours = d2.getDiff(d, "h");
	
	if(Math.abs(diffhours)<24) return 1;
	
	var diff = d2.getDiff(d, "y")
	if( isNaN(diff) ) {
		//alert( "data non valida" );
		return 0;
	}
	//alert(diff);
	if( diff < partenza || diff >arrivo) {
		//alert( "Difference less than 2 years" );
		return -1;
	}	
	
	return 1;
}

/* Extension to Date object by glenngv 
 * (http://www.codingforums.com/member.p...poster&t=62011)
 */
Date.prototype.getDiff = function(date, interval){
  if (typeof date == "string"){
     date = new Date(date);
  }
  if (isNaN(date) || !(date instanceof Date)){
     return NaN; 
  }
  if (typeof interval == "undefined") interval = "ms"; 
  var diff = this - date; 
  switch(interval.toLowerCase()){
    case "s": 
      diff = diff/1000; break;
    case "n": 
      diff = diff/(1000*60); break;
    case "h": 
      diff = diff/(1000*60*60); break;
    case "d": 
      diff = diff/(1000*60*60*24); break;
    case "m": 
      diff = diff/(1000*60*60*24*30); break;
    case "y": 
      diff = diff/(1000*60*60*24*365); break;
    default:
      ; 
  }
  return Math.floor(diff);
}
