function ShowAndHide(id1,id2){
	if(document.getElementById){
		el1=document.getElementById(id1);
		el2=document.getElementById(id2);
		if(el1.style.display=="none"){
			el1.style.display="block";
			el2.style.display="none";			
		}
		else{
			el1.style.display="none";
			el2.style.display="block";
		}
	}
}

function ShowAndHideMess(id1,id2){
	if(document.getElementById){
		el1=document.getElementById(id1);
		el2=document.getElementById(id2);

			el1.style.display="none";
			el2.style.display="block";
		}
	
}


function ShowAndHideS(id1,id2,id3,id4,id5){
	if(document.getElementById){
		el1=document.getElementById(id2);
		el2=document.getElementById(id1);
		el3=document.getElementById(id3);
		el4=document.getElementById(id4);
		el5=document.getElementById(id5);
		if(el1.style.display=="none"){
			el1.style.display="block";
			el2.style.display="block";
			el4.style.display="block";
			el5.style.display="block";
			el1.bgColor="#ffffa1";
			el2.bgColor="#ffffa1";
			el3.bgColor="#ffffa1";
			el4.bgColor="#ffffa1";
			el5.bgColor="#ffffa1";			
		}
		else{
			el1.style.display="none";
			el2.style.display="none";
			el4.style.display="none";
			el5.style.display="none";
			//el2.style.display="block";
			el3.bgColor="white";
		}
    }
}


function popitup(url) {
	var newwindow=window.open(url,'InserimentoContromisure','height=550,width=600');
	
	if (window.focus){
		newwindow.focus()
	}
	return false;
}


function popitupSugg(url) {
	var newwindow=window.open(url,'VisualizzazioneSuggestion','height=680,width=600');
	
	if (window.focus){
		newwindow.focus()
	}
	return false;
}


function popitupA4(url) {
	var newwindow=window.open(url,'StampaFoglio','height=842,width=595');
	
	if (window.focus){
		newwindow.focus()
	}
	return false;
}


function popitupCorso(url) {
	//var newwindow=window.showModalDialog(url,'','dialogWidth:780px; dialogHeight: 636px; center:yes; status:no;');
	var newwindow=window.open(url,'','height=636px,width=780px');
	
	return false;
}


function popitupLezione(url) {
	var newwindow=window.showModalDialog(url,'','dialogWidth:780px; dialogHeight: 636px; center:yes; resizable:no; status:no;');
	
	return false;
}


function popitupA4Oriz(url) {
	var newwindow=window.open(url,'StampaFoglio','height=595,width=842');
	
	if (window.focus){
		newwindow.focus()
	}
	return false;
}


function popitupA4L(url) {
	var newwindow=window.open(url,'StampaFoglio','height=1050,width=1500');
	
	if (window.focus)
		newwindow.focus()
		
	return false;
}


function popitupA3(url) {
	var newwindow=window.open(url,'StampaFoglio','height=842,width=1190');
	
	if (window.focus)
		newwindow.focus()
		
	return false;
}


function Set_Cookie( name, value, expires, path, domain, secure ){
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}


// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ){
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ ){
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	
	if ( !b_cookie_found ){
		return null;
	}
}


function checkValue(){
	var formVal = document.getElementById('formVal');
	var numEl = formVal.elements.length;	
	var count = 0;
	
	numEl = numEl-1;
	
	while(count<numEl){
		if((formVal.elements[count].value!='')){			
			if(isNaN(formVal.elements[count].value)){
				alert('ATTENZIONE! La valutazione deve essere compresa tra 1 e 5.');
				return false;
			}
			else{
				if(formVal.elements[count].value<1||formVal.elements[count].value>5){
					alert('ATTENZIONE! La valutazione deve essere compresa tra 1 e 5.');
					return false;
				}			
			}
		}
		count = count + 2;
	}
	
	return true;
}


function checkValuePro(){
	var formVal = document.getElementById('formVal');
	var numEl = formVal.elements.length;	
	var count = 0;
	
	numEl = numEl-1;
	
	while(count<numEl){
		if((formVal.elements[count].value!='')){			
			if(isNaN(formVal.elements[count].value)){
				alert('ATTENZIONE! La valutazione deve essere compresa tra 1 e 5.');
				return false;
			}
			else{
				if(formVal.elements[count].value<0||formVal.elements[count].value>5){
					alert('ATTENZIONE! La valutazione deve essere compresa tra 1 e 5.');		
					return false;
				}			
			}
		}
		//alert(formVal.elements[count].value)
		count = count + 4;
	}
	
	return true;
}


function checkValueImp(){
	var formVal = document.getElementById('formVal');
	var numEl = formVal.elements.length;	
	var count = 0;
	
	numEl = numEl-1;
	
	while(count<numEl){
		if((formVal.elements[count].value!='')){			
			if(isNaN(formVal.elements[count].value)){
				alert('ATTENZIONE! La valutazione deve essere compresa tra 1 e 5.');
				return false;
			}
			else{
				if(formVal.elements[count].value<0||formVal.elements[count].value>5){
					alert('ATTENZIONE! La valutazione deve essere compresa tra 1 e 5.');		
					return false;
				}			
			}
		}
		//alert(formVal.elements[count].value);				
		count = count + 4;
	}
	
	return true;
}


function checkSuggestion(){
	var matricola = document.getElementById('ac_matricola');
	var nome = document.getElementById('ac_nome');
	var cognome = document.getElementById('ac_cognome');	
	var qualifica = document.getElementById('ac_qualifica');
	var ente = document.getElementById('select_ente');
	//var unit = document.getElementById('unit');
	var ute = document.getElementById('select_unit');
	var argomento = document.getElementById('argomento');
	var documento = document.getElementById('documento');
	var data_emissione = document.getElementsByName('data_emissione');
	var data_emissione = data_emissione[0];
	var data_chiusura = document.getElementsByName('data_chiusura');
	var data_chiusura = data_chiusura[0];

	/*alert(4)
	if(argomento.value=="")
		alert("cacchio")
		
	alert(matricola.value)
	alert(nome.value)
	alert(cognome.value)*/
	
	if(matricola.value=="")  {
		alert('Matricola mancante.');
		return false;
	}
	
	if(nome.value=="")  {
		alert('Nome mancante.');
		return false;
	}
	
	if(cognome.value=="")  {
		alert('Cognome mancante.');
		return false;
	}
		
	/*if(qualifica.value=="")  {
		alert('Qualifica mancante.');
		return false;
	}*/
	
	if(ute.value==""&&ente.value=="")  {
		alert('UTE o Ente mancante.');
		return false;
	}
	
	if(argomento.value=="")  {
		alert('Argomento mancante.');
		return false;
	}
	
	if(documento.value==""||documento.value=="-")  {
		alert('Documento mancante.');
		return false;
	}
	
	if(data_emissione.value=="")  {
		alert('Data di emissione mancante.');
		return false;
	}
	
	if(isDate(data_emissione.value,"Data Emissione")==false)  {
		return false;
	}
	/*	
	var currentTime = new Date()
	var month = currentTime.getMonth() + 1
	var day = currentTime.getDate()
	var year = currentTime.getFullYear()
	
	var currentData = day + "/" + month + "/" + year;
	
	alert(currentData);
	alert(data_emissione.value);
		
	if(currentData>data_emissione.value){
		alert('Data di emissione non consentita.');
		return false;
	
	}
	
	alert(data_chiusura.value);
		
	if(data_emissione.value>data_chiusura.value){
		alert('Data chiusura non consentita.');
		return false;
	
	} */
	
	/*
	if(data_chiusura.value!=""){
		if(isDate(data_chiusura.value,"Data Chiusura")==false){
			return false;
		}
	}
	*/

	return true;
}


// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;


function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
	// All characters are numbers.
    return true;
}


function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}


function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}


function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}


function isDate(dtStr,type){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	//var strMonth=dtStr.substring(0,pos1)
	//var strDay=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	
	if (pos1==-1 || pos2==-1){
		alert("Il formato di "+type+" deve essere : gg/mm/aaaa")
		return false;
	}
	
	if (strMonth.length<1 || month<1 || month>12){
		alert("Mese non valido in "+type+".")
		return false
	}
	
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		valert("Giorno non valido in.")
		return false
	}
	
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Inserire un anno a 4 cifre tra "+minYear+" e "+maxYear+" in "+type+"")
		return false
	}
	
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert(""+type+" non valida.")
		return false
	}
	return true
}


function confirmDel(){
	if(window.confirm("Procedere con la cancellazione?"))		
		return true;
	else	
		return false;
}


function quickEval(eval){
	for(i=0; i<document.formVal.elements.length; i++){
		if(document.formVal.elements[i].type=="text")
			document.formVal.elements[i].value=eval;
	}
}


function checkPassword(){	
	var PW = document.getElementById('password');
	var CPW = document.getElementById('cPassword');
	
	if(PW.value!=CPW.value||PW.value==""||CPW.value=="")  {
		alert('La password non corrisponde.');
		return false;
	}

	return true;
}

function showPopUp(el) {
	var cvr = document.getElementById("cover")
	var dlg = document.getElementById(el)
	
	cvr.style.display = "block"
	dlg.style.display = "block"
	
	if (document.body.style.overflow = "hidden") {
		cvr.style.width = "100%"
		cvr.style.height = "100%"
	}
}


function closePopUp(el) {
	var cvr = document.getElementById("cover")
	var dlg = document.getElementById(el)
	
	cvr.style.display = "none"
	dlg.style.display = "none"
	document.body.style.overflowY = "scroll"
}


function refreshimage($id){
	document.getElementById($id).src = document.getElementById($id).src + '#';
	//alert('fatto');
}


function reopenCorso(){

	popitupCorso('/php/training/doCorso.php');	
}

/*
// START - Reading SCORM result
var API = null; // SCORM API 

// look up through the frameset hierarchy for the SCORM API 
function findAPI(win)
{
	while ((win.API == null) && (win.parent != null) && (win.parent != win))
	{
		win = win.parent;
	}
	API = win.API;
}

// init the SCORM API 
function initAPI(win)
{
	// look for the SCORM API up in the frameset 
	findAPI(win);
	
	alert('Trovato!')

	// if we still have not found the API, look at the opener and it's frameset 
	if ((API == null) && (win.opener != null))
	{
		findAPI(win.opener);
	}
}
// STOP - Reading SCORM result
*/
