function moveSelected(from,to) {

  newTo = new Array();
  for(i=0; i<to.options.length; i++)
  {
    newTo[newTo.length] = new Option(to.options[i].text,to.options[i].value);
    newTo[newTo.length-1].selected = false;
  }
    
  for(i=0; i<from.options.length; i++) {
    if (from.options[i].selected) {
        //verifico che non sia già contenuto nella tendina destinazione
        found = false;
        for(j=0; j<to.options.length; j++) {
            if(to.options[j].value == from.options[i].value)
                found = true;
        }
        if(!found) {
          newTo[newTo.length] = new Option(from.options[i].text,from.options[i].value);
          from.options[i] = null;
          i--;
        }
    }
  }

  to.options.length = 0;
  for(i=0; i<newTo.length; i++) {
    to.options[to.options.length] = newTo[i];
  }
}

function removeSelected(from) {

  for(i=0; i<from.options.length; i++) {
    if (from.options[i].selected) {
          from.options[i] = null;
          i--;
    }
  }
}


function setImg(valueField,imgField)
{
    
    if (valueField.value.length == 0)
    {
        imgField.src = IcoImg[0].src;
    }
    else
    {
        imgField.src = IcoImg[1].src;
    }
    
}

//accetta . , - _ spazio
function toAlphaModWithChars(checkString)
{
    newString = "";    // REVISED/CORRECTED STRING

    // LOOP THROUGH STRING CHARACTER BY CHARACTER
    for (i = 0; i < checkString.length; i++) {
        ch = checkString.substring(i, i+1);

        // ENSURE CHARACTER IS AN ALPHA CHARACTER
        if ((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z" ) || (ch >= "0" && ch <= "9") || ch == " "
            || ch == "." || ch == "," || ch == "-" || ch == "_"){
            newString += ch;
        }
    }

    return newString;
}  

function toAlphaModWithSpaces(checkString)
{
    newString = "";    // REVISED/CORRECTED STRING

    // LOOP THROUGH STRING CHARACTER BY CHARACTER
    for (i = 0; i < checkString.length; i++) {
        ch = checkString.substring(i, i+1);

        // ENSURE CHARACTER IS AN ALPHA CHARACTER
        if ((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z" ) || (ch >= "0" && ch <= "9") || ch == " ")  {
            newString += ch;
        }
    }

    return newString;
}  

function toAlphaMod(checkString)
{
    newString = "";    // REVISED/CORRECTED STRING

    // LOOP THROUGH STRING CHARACTER BY CHARACTER
    for (i = 0; i < checkString.length; i++) {
        ch = checkString.substring(i, i+1);

        // ENSURE CHARACTER IS AN ALPHA CHARACTER
        if ((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z" ) || (ch >= "0" && ch <= "9"))  {
            newString += ch;
        }
    }

    return newString;
} 

function checkAlphaModWithChars(checkString)
{
    if(toAlphaModWithChars(checkString) != checkString)
    {
        return false;
    }

    return true;
}

function checkAlphaModWithSpaces(checkString)
{
    if(toAlphaModWithSpaces(checkString) != checkString)
    {
        return false;
    }

    return true;
}

function checkAlphaMod(checkString)
{
    if(toAlphaMod(checkString) != checkString)
    {
        return false;
    }

    return true;
}


function validateAlphaNumericString(field) {
    if(field.value == "") {
        return false;
    }
    else {
        if(!checkAlphaMod(field.value)) {
            return false;
        }
    }
    return true;
}

function validateNumericField(field)
{
    if(field.value.search(" ")!= -1) {
        return false;
    }
    if(isNaN(field.value)) {
        return false;
    }
    return true;
}

function validateNumericFieldWithDecimal(field)
{
    if(field.value.search(" ")!= -1) {
        return false;
    }
    if(field.value.indexOf('.')!= -1) {
        return false;
    }
    if(isNaN(field.value)) {
        return false;
    }
    return true;
}


function selectAll(selectField)
{
    for (var i = 0; i < selectField.options.length; i++) {
        selectField.options[i].selected = true;
    }

}


function NewWindow(mypage,myname,w,h,scroll)
{
    var win= null;
    var winl = (screen.width-w)/2;
    var wint = (screen.height-h)/2;
    var settings ='height='+h+',';
    settings +='width='+w+',';
    settings +='top='+wint+',';
    settings +='left='+winl+',';
    settings +='scrollbars='+scroll+',';
    settings +='resizable=yes';
    win=window.open(mypage,myname,settings);
    if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

function disable_form()
{

    for(i=0; i<document.dati.elements.length; i++)
    {        
        if (document.dati.elements[i].name != 'openButton' && document.dati.elements[i].name != 'backStage' && document.dati.elements[i].name != 'btnChangeStatus')
        {
            if (document.dati.elements[i].type == 'button' || document.dati.elements[i].type == 'submit' || document.dati.elements[i].type == 'checkbox' )
            {
                document.dati.elements[i].disabled = true;    
            }
            else
            {
                document.dati.elements[i].readOnly = true;    
            }            
        }            
    }
}
function enable_form()
{

    for(i=0; i<document.dati.elements.length; i++)
    {        
        if (document.dati.elements[i].name != 'openButton' && document.dati.elements[i].name != 'backStage' )
        {
            if (document.dati.elements[i].type == 'button' || document.dati.elements[i].type == 'submit' || document.dati.elements[i].type == 'checkbox' )
            {
                document.dati.elements[i].disabled = false;    
            }
            else
            {
                document.dati.elements[i].readOnly = false;    
            }            
        }            
    }
    changeStatus('STAGE');

}

function delete_public(status)
{
    document.dati.operation.value = "DELETE_PUBLIC";
    changeStatus(status);
}


function disable_public(status)
{
    document.dati.operation.value = "DISABLE_PUBLIC";
    changeStatus(status);
}

function sendChangeStatus()
{
    
    selezionato = "";
    
    for (var i = 0; i < document.dati.selectStatus.options.length; i++)
    {
        if (document.dati.selectStatus.options[ i ].selected)
        {
           selezionato = document.dati.selectStatus.options[ i ].value;
        }
    }
    if (selezionato == 'STAGE_BACK')
    {
        enable_form();
    }
    else
    {
        changeStatus(selezionato);
    }
}


function apriEditor(fieldLabel, path,fieldName)
{   
    document.dati.action = path;    
    document.dati.fieldEditor.value = fieldName;
    document.dati.fieldLabel.value = fieldLabel;
    NewWindow("","editorWindow","850","400","yes");    
    document.dati.target = "editorWindow";
    document.dati.submit();
}


function apriEditorMultipart(fieldLabel, path, fieldName)
{
    document.dati.action = path;    
    document.dati.fieldEditor.value = fieldName;
    document.dati.fieldLabel.value = fieldLabel;
    document.dati.enctype="application/www-urlencoded";
    document.dati.method="post";
    NewWindow("","editorWindow","850","370","yes");    
    document.dati.target = "editorWindow";
    document.dati.submit();
}

function TrimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

//remove all spaces
function trim(checkString){

    newString="";

    for (i =0;  i < checkString.length ; i++) {
        ch = checkString.substring(i, i+1);
        if(ch != " "){
            newString += ch;
        }
    }

    return newString;

}

function control(data_iniziale)
{

	errore = true;
	
	found=data_iniziale.match(/\d{1,2}[\/-]\d{1,2}[\/-]\d\d\d\d/)
	if (found == null)
   	{
		errore = false
	}
	else
	{
		pos_separatore = data_iniziale.search(/[\/-]/);
		giorni = data_iniziale.substring(0,pos_separatore)
		pos_separatore2 = data_iniziale.substring(pos_separatore+1, data_iniziale.length).search(/[\/.-]/) + pos_separatore + 1;
		mesi = data_iniziale.substring(pos_separatore+1,pos_separatore2)
		anni = data_iniziale.substring(pos_separatore2+1,pos_separatore2+5)
				
            c_anni=data_iniziale.substring(pos_separatore2+1,data_iniziale.length)
		if (c_anni.length > 4)
		{
              return false
 		}

		// Primo controllo data

		if (mesi < 1 || mesi > 12)
		{
			errore = false
    		}
	    	else 
	 	{
    			if (giorni < 1 || giorni > 31)
	    		{
	    			errore = false
    			}
 
	   	// controllo mesi con trenta giorni
			if (mesi==4 || mesi==6 || mesi==9 || mesi==11)
			{
				if (giorni == 31)
				{
					errore = false
				}
			}
	
	
		// controllo febbraio
	
			if (mesi == 2)
			{
 				k= (anni % 1000)
				if (k==0)
				{
					bis= (anni % 400)
				}
				else
				{
					k2 = (anni % 100)	
					if (k2 == 0)
					{
					bis = (anni % 400)
					} 
					else
					{
					bis = (anni % 4)
					}
				}
				if (bis == 0)
				{
					if (giorni > 29)
					{
						errore = false
					}
				}
				else if (giorni > 28)
			  	{
				   	errore = false
				}	
			}
		} // }else del controllo mesi
	} // }else del comando che controlla il match 		 			
	return errore;
 }

function StringToDate(data_iniziale)
{
	if (control(data_iniziale)) 
	{
		pos_separatore = data_iniziale.search(/[\/-]/);
		giorni = data_iniziale.substring(0,pos_separatore)
		pos_separatore2 = data_iniziale.substring(pos_separatore+1, data_iniziale.length).search(/[\/.-]/) + pos_separatore + 1;
		mesi = data_iniziale.substring(pos_separatore+1,pos_separatore2)
		anni = data_iniziale.substring(pos_separatore2+1,pos_separatore2+5)
		
			
		if (anni < 1753)
		{
			giornata = "anno errato"
			return giornata
		}

		if (mesi.length == 1) 
			mesi ="0"+mesi;
		if (giorni.length == 1) 
			giorni ="0"+giorni;
		giornata= new Date (anni,mesi-1,giorni)
	}
	else 
	{
		giornata = null // se ritorno un valore nullo, allora c'è un errore
	}
 	return giornata
}

  function isValidDate(gg,mm,aa)
  {

      if(gg.search(" ")!= -1)
      {
	alert("Controllare le cifre digitate per il giorno");
	return false;
      }
      if(mm.search(" ")!=-1)
      {
        alert("Controllare le cifre digitate per il mese");
	return false;
      }
      if(aa.search(" ")!=-1)
      {
	alert("Controllare le cifre digitate per l\' anno");
	return false;
      }

      if(isNaN(gg))
      {
        alert("Il giorno deva essere un valore compreso tra 01 e 31");
        return false;
      }
      else if((gg>31)||(gg.length<2) || (gg<1))
      {
        alert("Il giorno deve essere compreso tra 01 e 31")
        return false;
      }
      else if(isNaN(mm))
      {
        alert("Il mese deva essere un valore compreso tra 01 e 12");
        field.value = temp;
        return false;
      }
      else if((mm>12)||(mm.length<2) || (mm<1))
      {
        alert("Il mese deve essere compreso tra 01 e 12")
        return false;
      }
      else if(isNaN(aa))
      {
        alert("Inserire un valore numerico per l\' anno");
        return false;
      }
      else if ((aa.length<4)||(aa.length>4) )
      {
        alert("L\' anno deve essere nel formato aaaa");
        return false;
      }


    if ((mm=="04")||(mm=="06")||(mm=="09")||(mm=="11"))
    {
      if(gg>30)
      {
        alert("Il mese inserito è di 30 giorni");
        return false;
      }
    }
    else if(mm=="02")
    {
      //processo anno bisestile
      if(gg>29)
      {
        alert("Febbraio ha meno di 30 giorni");
        return false
      }
      else if ((gg=="29")&&(!((aa % 4 == 0) && (!(aa % 100 == 0)) || (aa % 400 == 0))))
      {
        alert("L\'anno inserito non è bisestile");
        return false;
      }
    }//fine controllo correttezza data
    return true;
  }


   
function dateFormat(value)
{
  var test = value;

  if (test.length == 8)
  {
    if (test.substring(5, 6) == "/")
    {
      alert("anno deve essere in formato aaaa!Ricontrollare");
      return false;
    }

    //alert("dateFormat: conversione");
    var gg = test.substring(0,2);
    var mm = test.substring(2,4);
    var aaaa = test.substring(4,8)
    return true;
  }
  else if(test.length < 8)
  {
    alert("Formato data non corretto! Ricontrollare.");
    return false;
  }
  else if(test.length > 8 && test.length <10)
  {
    alert("Formato data non corretto! Ricontrollare.");
    return false
  }
  else if(test.length == 10)
  {
    return true;
  }
  else if(test.length > 10)
  {
    return false;
  }
}



  function isValidDate2(value)
  {
    var daysInMonth = new Array(12);
    daysInMonth[2] = 29;   // si deve testare in programma

    if ((value.charAt(2) == '/') && (value.charAt(5) == '/'))
    {
      gg=value.substring(0,2);
      mm=value.substring(3,5);
      aa=value.substring(6,10);

      if(gg.search(" ")!= -1)
      {
	alert("Controllare le cifre digitate per il giorno");
	return false;
      }
      if(mm.search(" ")!=-1)
      {
        alert("Controllare le cifre digitate per il mese");
	return false;
      }
      if(aa.search(" ")!=-1)
      {
	alert("Controllare le cifre digitate per l\' anno");
	return false;
      }

      if(isNaN(gg))
      {
        alert('Il giorno deva essere un valore compreso tra 01 e 31');
        return false;
      }
      else if((gg>31)||(gg.length<2) || (gg<1))
      {
        alert('Il giorno deve essere compreso tra 01 e 31')
        return false;
      }
      else if(isNaN(mm))
      {
        alert('Il mese deva essere un valore compreso tra 01 e 12');
        return false;
      }
      else if((mm>12)||(mm.length<2) || (mm<1))
      {
        alert('Il mese deve essere compreso tra 01 e 12')
        return false;
      }
      else if(isNaN(aa))
      {
        alert("Inserire un valore numerico per l\' anno");
        return false;
      }
      else if ((aa.length<4)||(aa.length>4) )
      {
        alert("L\' anno deve essere nel formato aaaa");
        return false;
      }
    }
    else
    {
      alert('Inserire la data nel formato gg/mm/aaaa ')
      return false;
    }

    if ((mm=='04')||(mm=='06')||(mm=='09')||(mm=='11'))
    {
      if(gg>30)
      {
        alert('Il mese inserito è di 30 giorni');
        return false;
      }
    }
    else if(mm=='02')
    {
      //processo anno bisestile
      if(gg>29)
      {
        alert('Febbraio ha meno di 30 giorni');
        return false
      }
      else if ((gg=='29')&&(!((aa % 4 == 0) && (!(aa % 100 == 0)) || (aa % 400 == 0))))
      {
        alert("L\'anno inserito non è bisestile");
        return false;
      }
    }
    return true;
  }
  
  function validateEmail(value){
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return filter.test(value);
    
  }
  
  function isNullString(value){
    return TrimString(value).toLowerCase() == "null";
  }
  
  function resetForm(formName){
    if(document.forms[formName]){
        for(var i = 0; i < document.forms[formName].elements.length; i++){
            if(document.forms[formName].elements[i].type){
                if(document.forms[formName].elements[i].type == "text"
                || document.forms[formName].elements[i].type == "select-one"
                || document.forms[formName].elements[i].type == "textarea"
                || document.forms[formName].elements[i].type == "password"){
                    document.forms[formName].elements[i].value = "";
                }
            }
            if(document.forms[formName].elements[i].type == "radio"
                 || document.forms[formName].elements[i].type == "checkbox"){
                    document.forms[formName].elements[i].checked = false;
            }
        }
    }
  }

