 window.onload=function(){
 
 if (document.getElementById('chgimg1')){
    if( document.getElementById('chgimg1').checked==true){change_add(1,'img');} 
  if( document.getElementById('chgvid1').checked==true){change_add(1,'video');}
  if( document.getElementById('chgimg2').checked==true){change_add(2,'img');}
  if( document.getElementById('chgvid2').checked==true){change_add(2,'video');}
}
  if(document.getElementsByTagName)

      function setMaxLength() {
      	var x = document.getElementsByTagName('textarea');
      	var counter = document.createElement('div');
      	counter.className = 'counter';
      	for (var i=0;i<x.length;i++) {
        
        		if (x[i].getAttribute('maxlength')) {
        			var counterClone = counter.cloneNode(true);
        			counterClone.relatedElement = x[i];
        			if(x[i].getAttribute('name')=='rr_review'){
        			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength')+' Caratteri rimanenti';
        			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
        			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];
        			x[i].onkeyup = x[i].onchange = checkMaxLength;
        			x[i].onkeyup();
              
              }else{
              
        			x[i].onkeyup = x[i].onchange = checkMaxLength;
        			x[i].onkeyup();
              
              }
        			
        		}
      		}
      	
      }
    
      function checkMaxLength() {
      
      	var maxLength = this.getAttribute('maxlength');
      	var currentLength = this.value.length;
      	//alert(currentLength + ' / ' + maxLength)
      	var currentvalue= this.value;
      	if (currentLength > maxLength){
      		//this.relatedElement.className = 'toomuch';
      		this.value=currentvalue.substring(0,maxLength);
      	}else{
      	//	this.relatedElement.className = '';
      	}	
      	if(this.getAttribute('name')=='rr_review'){
      	this.relatedElement.firstChild.nodeValue = currentLength;
      	}
      	// not innerHTML
      }
      
      setMaxLength();
      
      
      /* if set rating */
      if (document.getElementById('rr_rating')){
      
         var rate= document.getElementById('rr_rating').value;
          if((rate>0)&&(rate<6)){
    
            voting((rate*30),12);
          }else{
            voting(0,12);
          }
      
          }
      }
    var suggest_pro='';
     
    function search_pros(){
      
      var stringa= document.getElementById('rr_pro_ins').value;
      if(stringa.length>1){
      
        search_pro(stringa);
         
      }else{
      document.getElementById('suggest_pro').style.display = 'none'; 
      }
     
    
    }
    
    function search_cons(){
      
      var stringa= document.getElementById('rr_cons_ins').value;
      if(stringa.length>1){
       
         search_con(stringa);
        
      }else{
      document.getElementById('suggest_cons').style.display = 'none'; 
      }
     
    
    }
    
    function search_bests(){
      
      var stringa= document.getElementById('rr_best_ins').value;
      if(stringa.length>1){
       
         search_best(stringa);
        
      }else{
      document.getElementById('suggest_best').style.display = 'none'; 
      }
     
    
    }
    
   
    
    function search_pro(ricerca){
                      var show_t = "";
                     if (window.XMLHttpRequest) {
                          xmlhttp = new XMLHttpRequest();
                      }
                      // IE
                      else if (window.ActiveXObject) {
                          xmlhttp= new ActiveXObject('Microsoft.XMLHTTP');
                      }
                      
                      xmlhttp.open('POST', 'ratings/form_aj.php', true);
                     // alert('aperto xml');
                      
                      // The callback function
                      xmlhttp.onreadystatechange = function() {
                     //alert('inizializzo');
                         
                          if (xmlhttp.readyState == 4) {
                              if (xmlhttp.status == 200) {
                                //alert('aspetto esponso');
                                
                                max_at=0;
                                var value = xmlhttp.responseXML.getElementsByTagName("xmlresponse")[0];
                                //numero variabili contate
                                count = value.childNodes.length;
                                
                                //alert ("cerco: " + document.getElementById('cliente').value);
                              //alert("totale: "+count);
                                var contatore= count ;
                               
                                if(contatore>1){
                                  for (i=0; i<(contatore); i++){ 
                                      
                                      var id = xmlhttp.responseXML.getElementsByTagName('id')[i].firstChild.data;
                                      var desc = xmlhttp.responseXML.getElementsByTagName('desc')[i].firstChild.data;
                                      
                                      max_at++;
                                     
                                      show_t  = show_t + "<a href=\"javascript:void(0)\" onclick=\"reppro('" + desc + "');carica('pro');\">" + desc +"</a>" + "<br>" ;
                                      document.getElementById('suggest_pro').style.display = 'block';
                                      document.getElementById('suggest_pro').innerHTML = suggest_pro + "" + show_t;
                                   
                                   }
                                
                                  
                                }else{
                                 document.getElementById('suggest_pro').style.display = 'none';
                                  document.getElementById('suggest_pro').innerHTML = suggest_pro; 
                                }
                                                    
                      }
                    }
                  }
            
                  
                xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xmlhttp.send("pros="+ ricerca);
                  
                  //alert(key);
    }
    
       function search_con(ricerca){
                      var show_t = "";
                     if (window.XMLHttpRequest) {
                          xmlhttp = new XMLHttpRequest();
                      }
                      // IE
                      else if (window.ActiveXObject) {
                          xmlhttp= new ActiveXObject('Microsoft.XMLHTTP');
                      }
                      
                      xmlhttp.open('POST', 'ratings/form_aj.php', true);
                     //alert('aperto xml');
                      
                      // The callback function
                      xmlhttp.onreadystatechange = function() {
                     //alert('inizializzo');
                         
                          if (xmlhttp.readyState == 4) {
                              if (xmlhttp.status == 200) {
                                //alert('aspetto esponso');
                                
                                max_at=0;
                                var value = xmlhttp.responseXML.getElementsByTagName("xmlresponse")[0];
                                //numero variabili contate
                                count = value.childNodes.length;
                                
                                //alert ("cerco: " + document.getElementById('cliente').value);
                                //alert("totale: "+count);
                                var contatore= count ;
                               
                                if(contatore>1){
                                  for (i=0; i<(contatore); i++) { 
                                      
                                      var id = xmlhttp.responseXML.getElementsByTagName('id')[i].firstChild.data;
                                      var desc = xmlhttp.responseXML.getElementsByTagName('desc')[i].firstChild.data;
                                      
                                      max_at++;
                                     
                                      show_t  = show_t + "<a href=\"javascript:void(0)\" onclick=\"repcons('" + desc + "');carica('cons');\">" + desc +"</a>" + "<br>" ;
                                    document.getElementById('suggest_cons').style.display = 'block';
                                    document.getElementById('suggest_cons').innerHTML = '' + "" + show_t    ;
                                   }
                                alert(sasso);
                                  
                                }else{
                                  document.getElementById('suggest_cons').innerHTML = ''; 
                                  document.getElementById('suggest_cons').style.display = 'none';
                                }
                                        
                      }
                    }
                  }
                  
                xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xmlhttp.send("cons="+ ricerca);
                  
                  //alert(key);
    }     
    
    function search_best(ricerca){
                      
                      var show_t = "";
                     if (window.XMLHttpRequest) {
                          xmlhttp = new XMLHttpRequest();
                      }
                      // IE
                      else if (window.ActiveXObject) {
                          xmlhttp= new ActiveXObject('Microsoft.XMLHTTP');
                      }
                      
                      xmlhttp.open('POST', 'ratings/form_aj.php', true);
                     //alert('aperto xml');
                      
                      // The callback function
                      xmlhttp.onreadystatechange = function() {
                     //alert('inizializzo');
                         
                          if (xmlhttp.readyState == 4) {
                              if (xmlhttp.status == 200) {
                                //alert('aspetto esponso');
                                
                                max_at=0;
                                var value = xmlhttp.responseXML.getElementsByTagName("xmlresponse")[0];
                                //numero variabili contate
                                count = value.childNodes.length;
                                
                                //alert ("cerco: " + document.getElementById('cliente').value);
                                //alert("totale: "+count);
                                var contatore= count ;
                               
                                if(contatore>1){
                                  for (i=0; i<(contatore); i++) { 
                                      
                                      var id = xmlhttp.responseXML.getElementsByTagName('id')[i].firstChild.data;
                                      var desc = xmlhttp.responseXML.getElementsByTagName('desc')[i].firstChild.data;
                                      
                                      max_at++;
                                     
                                      show_t  = show_t + "<a href=\"javascript:void(0)\" onclick=\"repbest('" + desc + "');carica('best');\">" + desc +"</a>" + "<br>" ;
                                    document.getElementById('suggest_best').style.display = 'block';
                                    
                                    document.getElementById('suggest_best').innerHTML = '' + "" + show_t    ;
                                   }
                                alert(sasso);
                                  
                                }else{
                                  document.getElementById('suggest_best').innerHTML = ''; 
                                  document.getElementById('suggest_best').style.display = 'none';
                                }
                                        
                      }
                    }
                  }
                  
                xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
              
                xmlhttp.send("best="+ ricerca);
                  
                  //alert(key);
    }     
    
   
    
  
  function reppro(sost){
      
     
       document.getElementById('rr_pro_ins').value=sost;
       document.getElementById('rr_pro_ins').focus();
      
    
    }
    
  function repcons(sost){
      
     
      document.getElementById('rr_cons_ins').value=sost;
      document.getElementById('rr_cons_ins').focus();
    
    } 
  
  function repbest(sost){
      
     
      document.getElementById('rr_best_ins').value=sost;
      document.getElementById('rr_best_ins').focus();
    
    }   
  
  function setCaretPosition(elemId, caretPos) {
    var elem = document.getElementById(elemId);

    if(elem != null) {
        if(elem.createTextRange) {
            var range = elem.createTextRange();
            range.move('character', caretPos);
            range.select();
        }
        else {
            if(elem.selectionStart) {
                elem.focus();
                elem.setSelectionRange(caretPos, caretPos);
            }
            else
                elem.focus();
        }
    }
}
    
    function trim(stringa){
        while (stringa.substring(0,1) == ' ')
        {
        stringa = stringa.substring(1, stringa.length);
        }
        while (stringa.substring(stringa.length-1, stringa.length) == ' ')
        {
        stringa = stringa.substring(0,stringa.length-1);
        }
        return stringa; 
   }
   
    var count_pros=1;
    var count_cons=1;
    var count_best=1;
    
    function carica(tipo){
      var valore = document.getElementById("rr_"+ tipo + "_ins").value;
      valore=trim(valore);
      
      var count_max= document.getElementById('count_stop').value;
      
      if(valore!=''){
        if(tipo=='pro'){
          if(count_pros>count_max){
            document.getElementById('error_count_'+tipo).style.display='block';
          }else{
            
            document.getElementById('check_'+ tipo +'_add').innerHTML += '<div class="checkbox"><input name="rr_' + tipo + '[]" value="' + valore + '" type="checkbox" checked="checked"></div><div class="checktext"><span style=\"font-style:italic;\">' + valore + '</div><div class="clear"></div>'; 
            
          }
          document.getElementById('suggest_pro').style.display = 'none';
          
          count_pros++;
        }
        if(tipo=='cons'){
          if(count_cons>count_max){
            document.getElementById('error_count_'+tipo).style.display='block';
          }else{
            document.getElementById('check_'+ tipo +'_add').innerHTML += '<div class="checkbox"><input name="rr_' + tipo + '[]" value="' + valore + '" type="checkbox" checked></div><div class="checktext"><span style=\"font-style:italic;\">' + valore + '</div><div class="clear"></div>'; 
          }
          document.getElementById('suggest_cons').style.display = 'none';
          count_cons++;
        }
        
        if(tipo=='best'){
          if(count_best>count_max){
            document.getElementById('error_count_'+tipo).style.display='block';
          }else{
            document.getElementById('check_'+ tipo +'_add').innerHTML += '<div class="checkbox"><input name="rr_' + tipo + '[]" value="' + valore + '" type="checkbox" checked></div><div class="checktext"><span style=\"font-style:italic;\">' + valore + '</div><div class="clear"></div>'; 
          }
          document.getElementById('suggest_best').style.display = 'none';
          count_best++;
        }
        
         document.getElementById("rr_"+ tipo + "_ins").value=""; 
      }
      
      document.getElementById("rr_"+ tipo + "_ins").focus();
      setCaretPosition("rr_"+ tipo + "_ins",0);
      
    }
 
    function caricaenter(e,valore){

      var key = window.event ? e.keyCode : e.which;
    	var keychar = String.fromCharCode(key);

    	
      if(valore=="pro"){
        var stringa=document.getElementById("rr_pro_ins").value;
        search_pros();
      }
      if(valore=="cons"){
        var stringa=document.getElementById("rr_cons_ins").value;
        search_cons();
      }
      if(valore=="best"){
        var stringa=document.getElementById("rr_best_ins").value;
        search_bests();
      }
    	
    	if(key=='13'){
          carica(valore);     
      }

     }
    
    var fil1='';
    var fil2='';
    
    function change_add(p,tipo){

      if(p==1){
        if(tipo=='img'){
          document.getElementById('add_1_video').style.display='none';
          document.getElementById('add_1_file').style.display='block';
          if(document.getElementById('file1update').value==''){
            document.getElementById('file1update').value=fil1;
          }
        }
        
        if(tipo=='video'){
          document.getElementById('add_1_video').style.display='block';
          document.getElementById('add_1_file').style.display='none';
          
          fil1=document.getElementById('file1update').value;
          document.getElementById('file1update').value='';
        }
        
        document.getElementById('rr_choice1').value=tipo;
        
      }
      
      if(p==2){
        if(tipo=='img'){
          document.getElementById('add_2_video').style.display='none';
          document.getElementById('add_2_file').style.display='block';
          if(document.getElementById('file2update').value==''){
            document.getElementById('file2update').value=fil2;
          }
        }
        
        if(tipo=='video'){
          document.getElementById('add_2_video').style.display='block';
          document.getElementById('add_2_file').style.display='none';
          
          fil2=document.getElementById('file2update').value;
          document.getElementById('file2update').value='';
        }
        
        document.getElementById('rr_choice2').value=tipo;
      }
      
      
    }

