function cancellaCampo(id){
  
  var value1 = 'Inserisci descrizione';
  var value2 = 'Ulteriori informazioni utili';
  
  if (document.getElementById(id)){
      if (document.getElementById(id).value == value1 || document.getElementById(id).value == value2)
        document.getElementById(id).value = '';
  }
 }

function showAltra(value){

    if (value == "Altra")
      document.getElementById("campo_professione_altra").style.display = "block";
    else  
      document.getElementById("campo_professione_altra").style.display = "none";
}

function showAltro(value){

    if (value == "Altro")
      document.getElementById("campo_applicazione_altro").style.display = "block";
    else  
      document.getElementById("campo_applicazione_altro").style.display = "none";
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function verExt(strFile, extensionsAllowed){
	var strValExt = extensionsAllowed.split(",");
	
	var dd = "";
	for (i=0;i<strValExt.length;i++){
		dd = dd + strValExt[i] + " ";
	}
	var strSlash 	= "\\";	
	//var strFile 	= document.formcontatti.strFile.value;	f
	
	var strNomeFile = strFile.slice(nPos);  		
	//ricaviamo il nome del file	
	var nPos 		= strFile.lastIndexOf(".");	
	var strExt1 	= strNomeFile.slice(nPos);
	var strExt 		= strExt1.toLowerCase();
	//ricaviamo l'estensione del file	
	// Controlliamo che l'estensione sia di tipo valido	
  
	var found = false;
	var strAler = "no";
	for (j=0;j<strValExt.length;j++){
		if (found == false){
			//alert(strExt+strValExt[j]);
      if (strExt === strValExt[j]){
				strAler = "si";
				found = true;
			}
		}
	}

  //alert(strAler);
	if (strAler == "no"){
		var strAlert = "L'operazione non è consentita. Stai tentando l'upload di file di tipo " + strExt + "\n";
		strAlert = strAlert + "E' consentito il solo Upload di file " + dd + "!";
		document.getElementById("my_file_element").value = '';
		return strAlert;
	}
	return true;
}


function checkSubmit(){

  var notValid = '#FF9F9F';
  var valid    = '#B8F5B1';
  var check    = false;

  document.getElementById("nome").style.backgroundColor = valid;
  document.getElementById("sprytextfield1").style.visibility = 'hidden'; 
  if (document.getElementById("nome").value == ""){
      document.getElementById("sprytextfield1").style.visibility = 'visible';
      document.getElementById("nome").style.backgroundColor = notValid;
      document.getElementById("nome").focus();
      check = true;
  } 

  document.getElementById("cognome").style.backgroundColor = valid;
  document.getElementById("sprytextfield2").style.visibility = 'hidden'; 
  if (document.getElementById("cognome").value == ""){
      document.getElementById("sprytextfield2").style.visibility = 'visible';
      document.getElementById("cognome").style.backgroundColor = notValid;
      document.getElementById("cognome").focus();
      check = true;
  } 

  document.getElementById("email").style.backgroundColor = valid;
  document.getElementById("sprytextfield3").style.visibility = 'hidden'; 
  if (document.getElementById("email").value == ""){
      document.getElementById("sprytextfield3").style.visibility = 'visible';
      document.getElementById("email").style.backgroundColor = notValid;
      document.getElementById("email").focus();
      check = true;
  }
  
  document.getElementById("telefono").style.backgroundColor = valid;
  document.getElementById("sprytextfield4").style.visibility = 'hidden'; 
  if (document.getElementById("telefono").value == ""){
      document.getElementById("sprytextfield4").style.visibility = 'visible';
      document.getElementById("telefono").style.backgroundColor = notValid;
      document.getElementById("telefono").focus();
      check = true;
  } 
  
  /*
  document.getElementById("tendinamisure").style.backgroundColor = valid;
  document.getElementById("spryselectA").style.visibility = 'hidden'; 
  if (document.getElementById("tendinamisure").value == ""){
      document.getElementById("spryselectA").style.visibility = 'visible';
      document.getElementById("tendinamisure").style.backgroundColor = notValid;
      document.getElementById("tendinamisure").focus();
  } 
  */

  for (i=0;i<document.forms[0].length;i++){
  
		var name 	= document.forms[0].elements[i].name;
		var value = document.forms[0].elements[i].value;
		
		//alert(name);
		//alert(name.substring(0,4));
		if (name.substring(0,4) == "img_"){
        document.getElementById(name).style.backgroundColor = valid;
        document.getElementById("spryselect"+name.substring(4)).style.visibility = 'hidden'; 
        if (value == ""){
            document.getElementById("spryselect"+name.substring(4)).style.visibility = 'visible';
            document.getElementById(name).style.backgroundColor = notValid;
            document.getElementById(name).focus();
            check = true;
        } 
			
      }
		
	}
	
	document.getElementById("sprycheckbox1").style.visibility = 'hidden';
	if (document.getElementById("accetto").checked == false){
	    document.getElementById("pri").style.backgroundColor = notValid;
      document.getElementById("sprycheckbox1").style.visibility = 'visible';
      check = true;
  }

  if (check == false){
  
    document.getElementById("formcontatti").submit();
  
  }
}


function colorOK(id){
  
  var a = "#FFFFCC";
  document.getElementById(id).style.backgroundColor = a;

}




function MultiSelector( list_target, max ){

	this.list_target = list_target;
	this.count = 0;
	this.id = 0;
	this.img = 0;
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};

	this.addElement = function( element ){

		if( element.tagName == 'INPUT' && element.type == 'file' ){

			element.name = 'file_' + this.id++;
      
			element.multi_selector = this;

			element.onchange = function(){

        var checkExtensions = verExt(element.value, '.jpg,.gif');

        if (checkExtensions == true){
            
    				var new_element = document.createElement( 'input' );
    				new_element.type = 'file';
    
    				this.parentNode.insertBefore( new_element, this );
    
    				this.multi_selector.addElement( new_element );
    				this.multi_selector.addListRow( this );
    
    				this.style.position = 'absolute';
    				this.style.left = '-1000px';
          
        }
        else{
        
          alert(checkExtensions);
        }
			};

			if( this.max != -1 && this.count >= this.max ){
				element.disabled = true;
			};

			this.count++;
			this.current_element = element;
			
		} else {
			alert( 'Error: not a file input element' );
		};

	};

	this.addListRow = function( element ){

		var new_row = document.createElement( 'div' );

    var new_row_b = document.createElement( 'input' );
		new_row_b.type = 'text';
		new_row_b.value = element.value;
    new_row_b.style.width = '300px';

    var separator1 = document.createElement( 'span' );
    separator1.style.visibility = 'visible';
    separator1.style.width = '100px';
    separator1.style.height = '25px';
    separator1.style.border = '#000000 0px solid';
    separator1.text = '&nbsp&nbsp';
    separator1.innerHTML = '&nbsp&nbsp';
    
		var new_row_button = document.createElement( 'input' );
		new_row_button.type = 'button';
		new_row_button.value = 'Cancella';

    var new_select = document.createElement('select');
    new_select.id = "fil_"+this.img;
    new_select.name = "fil_"+this.img;
    
    var new_span = document.createElement('span');
    new_span.id = "spryselect"+this.img;
    new_span.name = "spryselect"+this.img;
    this.img++;

		new_row.element = element;
		new_row_button.onclick= function(){

			this.parentNode.element.parentNode.removeChild( this.parentNode.element );
			this.parentNode.parentNode.removeChild( this.parentNode );

			this.parentNode.element.multi_selector.count--;

			this.parentNode.element.multi_selector.current_element.disabled = false;
			return false;
		};

		//new_row.innerHTML = element.value;

		new_row.appendChild(new_row_b);
		new_row.appendChild(separator1);
    new_row.appendChild(new_row_button);

		this.list_target.appendChild(new_row);
		
	};

};
