
function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}

function selectAll(field, n){
for(i=1; i<=n; i++){
	  fieldTemp = document.getElementById(field+i);
	  fieldTemp.checked=1;
   }
}

function deselectAll(field, n){
for(i=1; i<=n; i++){
	  fieldTemp = document.getElementById(field+i);
	  fieldTemp.checked=0;
   }
}

function invertSelect(field, n){
for(i=1; i<=n; i++)
  {
		fieldTemp = document.getElementById(field+i);
		if(fieldTemp.checked==0)
		{
			fieldTemp.checked=1;
		}
		else
		{
			fieldTemp.checked=0;
		}
  }
}

function one2two() {
    m1len = m1.length ;
    for ( i=0; i<m1len ; i++){
        if (m1.options[i].selected == true ) {
            m2len = m2.length;
            m2.options[m2len]= new Option(m1.options[i].text,m1.options[i].value);
        }
    }

    for ( i = (m1len -1); i>=0; i--){
        if (m1.options[i].selected == true ) {
            m1.options[i] = null;
        }
    }
}

function two2one() {
    m2len = m2.length ;
        for ( i=0; i<m2len ; i++){
            if (m2.options[i].selected == true ) {
                m1len = m1.length;
                m1.options[m1len]= new Option(m2.options[i].text,m2.options[i].value);
            }
        }
        for ( i=(m2len-1); i>=0; i--) {
            if (m2.options[i].selected == true ) {
                m2.options[i] = null;
            }
        }
}

function moveOptionsUp(selectId) {
 var selectList = document.getElementById(selectId);
 var selectOptions = selectList.getElementsByTagName('option');
 for (var i = 1; i < selectOptions.length; i++) {
  var opt = selectOptions[i];
  if (opt.selected) {
   selectList.removeChild(opt);
   selectList.insertBefore(opt, selectOptions[i - 1]);
     }
    }
}

function moveOptionsDown(selectId) {
 var selectList = document.getElementById(selectId);
 var selectOptions = selectList.getElementsByTagName('option');
 for (var i = selectOptions.length - 2; i >= 0; i--) {
  var opt = selectOptions[i];
  if (opt.selected) {
   var nextOpt = selectOptions[i + 1];
   opt = selectList.removeChild(opt);
   nextOpt = selectList.replaceChild(opt, nextOpt);
   selectList.insertBefore(nextOpt, opt);
     }
    }
}








function in_array(the_needle, the_haystack){
	 //alert(the_haystack.length)
	 // alert(the_haystack)
	 matched = false
	 for(c=0; c<=the_haystack.length; c++){
	 	  if(the_needle == the_haystack[c])
	 	  {
	 	  	matched = true
	 	  }
	 }
   return matched;
} 


// ************************************ DOCUMENT READY *******************************************//


  var onloadPopUp = 0;
  
$(document).ready(function() {
  
  if ($('body').attr('rel') == 'welcome')
  {
   $('a[rel*="menu"]').click(function(){
     return false;
   });
  }
  else
  { 
  
	initMenu();
	tooltip();

	
	$('.scrollpane').jScrollPane();	
	
	if ($(".newsScroll").is("div")){$(".newsScroll").scrollable();}
	if ($(".pressScroll").is("div")){$(".pressScroll").scrollable();}
	if ($("a[rel^='popup']").is("a")){$("a[rel^='popup']").prettyPhoto();}
	if ($("a[rel^='footer']").is("a")){$("a[rel^='footer']").prettyPhoto();}
	
	initOnload();
	
	initCatalogue();
	
  }

	//setTimeout('initLiv1Iniziale()',500)
	//initLiv1Iniziale();	
})

// ************************************ DOCUMENT READY *******************************************//


function browserAlert(){
  
  var browserVersion = $.browser.version; 
  
  alert(browserVersion);
  
}

function tooltip(){
  $(".tooltipTrigger").mouseenter(function(){
    id = $(this).attr('id');
    //alert(id);
    //$("div[rel='"+id+"']").show();
    if ( $.browser.msie /*&& $.browser.version == "7.0"*/) {
       $("div[rel='"+id+"']").show();  
      }
      else{
       $("div[rel='"+id+"']").fadeIn("slow"); 
      }      
  })
  .mouseleave(function(){
    if ( $.browser.msie /*&& $.browser.version == "7.0"*/) {
       $("div[rel='"+id+"']").hide(); 
      }
      else{
       $("div[rel='"+id+"']").fadeOut("slow");
      }       
  })
} 

function initOnload()
{
	$('.onload_').remove();
	$('.onload').attr('class','onload_');
	if(onloadPopUp == 0)
	{
	  setTimeout('$(".onload_").click();',1000);
	}
	onloadPopUp++;
}

function initLiv1Iniziale()
{
	$.scrollTo('#'+$('body').attr('rel'), 1500);
}

function initSitoVerticale()
{
	//alert('inizio')
	$('body>div').each(function(){
		
		if($('#'+$(this).attr('id')).html()=='') //prech� nn usare simply "this"
		{
      //getLiv1Content($(this).attr('id'));
      getContent(wwwRoot+'/'+wwwLang+'/'+$(this).attr('id')+'/',scroll)
		}
		else
		{
    $("#"+$(this).attr('id')).children('div').attr('class','contentPage ajax');
		}
	});
	
	//alert('test');
	//alert('fine')
}

function initLinkInterni(liv1)
{
  
 

 
   if(liv1)
  {
    obj = 'div#'+liv1+' a[rel^="menu"]';
  }
  else
  {
    obj = 'a[rel^="menu"]';
  }  
  
  
  
  $(obj).click(function(){
    
    
   	  	if($(this).parent('li').attr('class')=='lang-switch')
   	  	{
          //$(this).parent('li').children('div').css('display','block');
          langSwitch($(this));
          //$(this).parent('li').children('div').fadeIn();              
          //return false
   	  	}
   	  	else if($(this).attr('ajax')=='false')
        {
          window.location.href = $(this).attr('href');
        }
        else
   	  	{
	        urlPage = $(this).attr('href');
	        //alert(urlPage);
	        getContent(urlPage,'true');
	        return false;
   	  	}

  })  
  
  

  
  
//  
//  if(liv1)
//  {
//    obj = 'div#'+liv1+' a';
//  }
//  else
//  {
//    obj = 'a';
//  }
//  
//  $(obj).click(function(){
//      urlPage = $(this).attr('href');
//      //alert(urlPage);
//      if (urlPage != undefined ){ // controllo aggiunto per conflitto con scrollpane
//      getContent(urlPage,'true');
//      return false;
//      }
//  })
  


}


//function getLiv1Content(liv1)
//{
//			$.ajax({
//			   type: "GET",
//			   url: wwwRoot+'/'+wwwLang+'/'+liv1+"/",
//			   data: "mode=ajax",
//			   success: function(msg){
//			   	  $('div#'+liv1).html(msg);	   	
//			   	  $("#"+liv1).children('div').attr('class','contentPage ajax'); 
//			   	  renderMenu(liv1);
//			   }
//			 });
//}

function getContent(urlPage,scroll)
{
      if(scroll==''){scroll='false';}
      var urlPageTmp = '';
	    var infoPath = '';

	    urlPageTmp = urlPage.replace(siteUrl,'');
	    urlPageTmp = urlPageTmp.replace(wwwRoot+'/'+wwwLang+'/','');
	    infoPath = urlPageTmp.split('/');

	    var liv1 = infoPath[0];
	    var liv2 = infoPath[1];
	    var liv3 = infoPath[2];
	    
	    //alert("["+liv1+"]["+liv2+"]");
	    



			$.ajax({
			   type: "GET",
			   url: urlPage,
			   data: "mode=ajax",
			   success: function(msg){

			   	  //$("#"+liv1).children('div').attr('rel','delete'); 
			   	  $("body>div").children('div').attr('rel','delete'); 
			   	  $('div#'+liv1).append(msg);
			   	  $('body>div').css('z-index','1');
			   	  $('div#'+liv1).css('z-index','10');
			   	  
			   	  if($("body>div").children('div[rel="delete"]').length>0)
			   	  {
			   	    $("body>div").children('div[rel="delete"]').attr('class','contentPage ajaxShow'); 
			   	    $("body>div").children(':not(div[rel="delete"])').attr('class','contentPage ajax'); 
			   	  }
			   	  //$('div#'+liv1).fadeIn();
			   	  $("body>div").children(':not(div[rel="delete"])').fadeIn('slow',function(){$("body>div").children('div[rel="delete"]').remove();initOnload()});
			   	  
			   	  renderMenu(liv1);
			      
			      //inizializzazione tooltips
			   	  tooltip();
			   	  
			   	  //inizializzazione scrollpane
			   	  $('.scrollpane').jScrollPane();	
			   	  if ($("a[rel^='popup']").is("a")){$("a[rel^='popup']").prettyPhoto();}
			   	  if ($("a[rel^='footer']").is("a")){$("a[rel^='footer']").prettyPhoto();}
          	if ($(".newsScroll").is("div")){$(".newsScroll").scrollable();}
          	if ($(".pressScroll").is("div")){$(".pressScroll").scrollable();}
			   	  
			   	  //inizializzazione scrollableCatalogue
			   	  setTimeout('initCatalogue()',500);
			   	 
			   	  
			   }
			   
			 });
			 
}



function initMenu()
{
  renderMenu();	
}

function initLinkLiv2(obj)
{
	  //alert('initLinkLiv2')
	  $(obj).click(function(){
      tempUrl = $(obj).attr('href');

      getContent(tempUrl);
      return false;
  });
}

function renderMenu(liv1) {
	 shuffleMenu();
	 

	 if(liv1)
	 {
	 	  menuLiv1 = "#"+liv1+">.contentPage>#top>#menu>ul>li>a";
	 	  menuLiv2 = "#"+liv1+">.contentPage>#top>#menu>ul>li."+liv1+">.menuOpen>ul>li>div>a";
	 }
	 else
	 {
	 	  menuLiv1 = ".contentPage>#top>#menu>ul>li>a";
	 	  menuLiv2 = ".menuOpen>ul>li>div>a";
	 }
	 
	 
	 
	 var liWidth = 0;
	 
   $(menuLiv1).each(function(i) { //i?
    
   	  //alert(liv1+"||"+i+"||"+menuLiv1);  
   	
   	  $(this).click(function(){
   	  	    var item_id     = $(this).parent().attr('class').replace(' selected',''); 
   	  	    //alert("item_id: "+item_id);
				  	//$.scrollTo('#'+item_id, 1000);    
            
				  	return false;   	  	
   	  })
   	
      var item_title  = $(this).text();
      var item_id     = $(this).parent().attr('class').replace(' selected',''); 
      if (item_title != '') {
       	      
          //alert("["+item_title+"]["+item_id+"]");
  	      
          var item_label  = escape($(this).text());
          var item_id     = $(this).parent().attr('class').replace(' selected','');
          var status      = $(this).attr('class');
          var width       = $(this).attr('width');
          
          
          
          if (item_id == "lang-switch")
          {
            $(this).css('background-image','url('+wwwRoot+"/comuni/img/ico/"+wwwLang+".png)");
            $(this).text("");
            
            liLangSwitchWidth = $(this).parent().width();
            
            /* render 2 lev lang menu*/
            liLangSwitchObj = $(this).parent('li');
            $(liLangSwitchObj.children('div').children('ul').children('li')).each(function(){
            
              lang = $(this).attr('id');
              $($(this).children('div').children('a')).css('background-image','url('+wwwRoot+"/comuni/img/ico/"+lang+".png)");
           
            });
            /* render 2 lev lang menu end*/
            
          }
          else if (item_id != 'homepage') 
          {
            liWidth        += width*1;
            $(this).css('background-image','url('+wwwRoot+"/"+wwwLang+"/img/menu/"+item_id+".png)");
            $(this).css('width',width+"px");
            $(this).parent().css('width',width+"px");
            $(this).text("");          
          }
          

      }
   });
   

   //$("#menu ul li[class^='homepage']").css('margin-left',$("#menu ul").width() - liWidth - 10);
   //alert($("#popup").length);
   if ($("#popup").length == 0)
   {
   $("#menu ul li[class^='lang-switch']").css('margin-left',$("#menu ul").width() - liWidth - liLangSwitchWidth);
   }

   //alert(homePageButtonWidth)
   
   $($("#menu ul").width())
   //if ($.browser.msie) { $(".png").ifixpng(); }
   
   
   //render 2 livello
   $(menuLiv2).each(function(i) { 
   	
      var item_title  = $(this).text();
      if (item_title != '') {
       	      
  	      
          var item_label  = escape($(this).text());
          var item_id     = $(this).parents('li').attr('id');
          var status      = $(this).attr('class');
          var width       = $(this).attr('width');
          
      
          $(this).css('background-image','url('+wwwRoot+"/"+wwwLang+"/img/menu/"+item_id+".png)");
          $(this).css('width',width+"px");
          $(this).text(""); 

        	//initLinkLiv2(this);    
      }      
   });   
   
     
 initLinkInterni(liv1);
}

function shuffleMenu() {
  
  $(".contentPage>#top>#menu>ul>li.homepage").each(function(){
     //last_item = $(this).nextAll().last();
     //$(this).insertAfter(last_item);
     $(this).css('display','none');
  });
      
}


function initCatalogue() {
       
  if ($(".productsScroll").is("div")) {
    
        
    //alert("initCatalogue");
    
    //alert($(".productsScroll").is("div"))
    //alert($(".productsScroll").html())
    
    $(".productsScroll").scrollable();
    
    
  
    var api = $(".productsScroll").data("scrollable");
    //alert(api);  
  
    //api.move(2);
    
    api.onBeforeSeek(function(event,index){
      $(".prodPic img").show();
      var items = $(".productsScroll .item");
      var itemsCount = items.length;
      //alert(itemsCount);
      //alert($(items).html());
      var i = this.getIndex();
      //alert("["+itemsCount+"]["+i+"]");
      //i = i + 1;
      if (i > itemsCount) { i = 0; }
      var currentItem = $(items[i]);
      var itemId = currentItem.attr("id");
      $("#"+itemId+" .prodPic img").hide();
    }).onSeek(function(event,index){
      //$(".prodPic img").show();
    });
    
    var items = $(".items").children(".item").length;
    
    if (items == 1){
      $("a.browse").addClass("disabled");
    }
    
    
  
  }

}

function renderTitles(parentObj) {
   $(parentObj+" h1:not(.alt),"+parentObj+" h2:not(.alt),h3:not(.alt)").each(function(i) {
      var item_title  = $(this).text();
      
      if (item_title != '') {
      
   	   var alternativeStyle  = $(this).attr("class");
   	   if(alternativeStyle){
   	     var item_level  = alternativeStyle;
   	   }
   	   else {
   	     var item_level  = this.tagName;
   	   }
   	      
  	      
          var item_label  = escape($(this).text());
   	      var item_width  = $(this).css("width");
   	      if (item_level == 'H1') {
   	         var item_height= '21px';
   	      }
   	      else if (item_level == 'H2') {
   	         var item_height= '21px';
   	      }
   	      else if (item_level == 'H3') {
   	         var item_height= '10px';
   	      }
   	      
   	      //var item_height  = $(this).css("height");
   	      var item_id     = item_level+"_"+i;
   	      //alert(item_width+" || "+item_height);
   	      var img_tag = "<img style=\"/*width:"+item_width+";height:"+item_height+"*/\" src=\""+wwwRoot+"/dynamic/img/titles/"+item_level+"/"+item_width+"/"+item_label+".gif\" alt=\""+item_title+"\" border=\"0\"/>";
   	      
   	      $(this).text("");
   	      $(this).html(img_tag);
      }
   });
   //if ($.browser.msie) { $(".png").ifixpng(); }
}

function langSwitch(obj){

    $(obj).parent('li').children('div').fadeIn();
    
    $(obj).parent('li').mouseleave(function(){
      $(obj).parent('li').children('div').fadeOut();
    })
    
    return false;
    
}
  





function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}


