
function initLists(formname,moduleid){
	firstlistname='firstlist'+formname;
	secondlistname='secondlist'+formname;
	
   Sortable.create(firstlistname,
     {dropOnEmpty:true,
     containment:[firstlistname],
     constraint:false,
	  onUpdate:function(){
			document.getElementById(formname).value = Sortable.serialize(secondlistname);
			refreshLists(formname,moduleid); },
     ghosting:false});
    
   Sortable.create(secondlistname,
     {
     	dropOnEmpty:true,
     	containment:[firstlistname,secondlistname],
     	constraint:false,
     	ghosting:false,
		onUpdate:function(){
			var noeuds=new Array();
			for(i=0;i<document.getElementById(secondlistname).childNodes.length;i++){
				var del=false;
				for(j=0;j<document.getElementById(secondlistname).childNodes.length;j++){
					if(noeuds[j]==document.getElementById(secondlistname).childNodes[i].id){
						var target = document.getElementById(secondlistname);
						target.removeChild(target.childNodes[i]);
						del = true;
						break;
					}
					
				}
				if(!del){
					noeuds[i]=document.getElementById(secondlistname).childNodes[i].id;
					if(document.getElementById('count_'+(i+1)))
						document.getElementById('count_'+(i+1)).disabled = false;
				}
			}
			
			document.getElementById(formname).value = Sortable.serialize(secondlistname);
		}
	  }
	 );
	 document.getElementById(formname).value = Sortable.serialize(secondlistname);
	 
}

function getNodeId(parent,child){
	for(i=0;i<document.getElementById(parent).childNodes.length;i++){
		if(document.getElementById(parent).childNodes[i].id==child)
			return i;
	}
}

function removeList(id,formname){
	firstlistname='firstlist'+formname;
	secondlistname='secondlist'+formname;
	var target = document.getElementById(secondlistname);
	target.removeChild(target.childNodes[getNodeId(secondlistname,id)]);
	document.getElementById(formname).value = Sortable.serialize(secondlistname);
	
}

function refreshLists(formname,moduleid){
	file='modules/getref.php?curmod='+moduleid+'&mod='+formname;
	if(document.getElementById(formname+'cat'))
		file=file+'&cat='+document.getElementById(formname+'cat').value;
	if(document.getElementById(formname+'search'))
		file=file+'&search='+document.getElementById(formname+'search').value;
	id='firstlist'+formname;
	var contentNode = document.getElementById(id);
	dojo.xhrGet({
	    url: file,
	    handleAs: "text",
	    load: function(data,args){
	    			
					contentNode.innerHTML="";
					
					eval(data);

					for(i=0;i<list.length;i++)
					{
						if(navigator.appVersion.indexOf('MSIE 6') > -1 || navigator.appVersion.indexOf('MSIE 7') > -1 ){
							row=document.createElement("<li id='item_"+idsList[i]+"' class='green'/>");
						}else{
		  					var row = document.createElement('li');
		  					row.setAttribute("id", "item_"+idsList[i])
		  					row.setAttribute("class", "itemList")
		  				}
		    			row.innerHTML = list[i]+"<a href='javascript:removeList(\"item_"+idsList[i]+"\",\""+formname+"\");'><img class='delimg' src='images/icons/delete.png' alt='{DEL}'/></a>";
						contentNode.appendChild(row);
					}
				   initLists(formname,moduleid);

	    },
	    // if any error occurs, it goes here:
	    error: function(error,args){
		console.warn("error!",error);
	    }
	});
} 

function tabs(idF,labelsF,contentsF,hlF){


	this.id = idF;
	this.labels = labelsF;
	this.contents = contentsF;
	this.hl = hlF;


	var contentNode = document.getElementById(this.id);
	nb = contents.length;
	if(navigator.appVersion.indexOf('MSIE 6') > -1 || navigator.appVersion.indexOf('MSIE 7') > -1){
		row=document.createElement("<ul id='"+this.id+'_Tabs'+"' name='"+this.id+'_Tabs'+"' class='tabStyle'/>");
	}else{
  		var row = document.createElement('ul');
  		row.setAttribute("name", this.id+'_Tabs');
  		row.setAttribute("id", this.id+'_Tabs');
  		row.setAttribute("class", "tabStyle");
	}
  	contentNode.appendChild(row);
  	
	for(i=0;i<nb;i++){
		if(navigator.appVersion.indexOf('MSIE 6') > -1 || navigator.appVersion.indexOf('MSIE 7') > -1){
			if(this.hl[i] && i==0)
				row2=document.createElement("<li id='"+this.id+'_Tabs'+i+"' name='"+this.id+'_Tabs'+"' class='tabsStyleSelectedHl'/>");
	  		else if(this.hl[i]){
	  			row2=document.createElement("<li id='"+this.id+'_Tabs'+i+"' name='"+this.id+'_Tabs'+"' class='tabsStyleHl'/>");
	  		}else if(i==0){
	  			row2=document.createElement("<li id='"+this.id+'_Tabs'+i+"' name='"+this.id+'_Tabs'+"' class='tabsStyleSelected'/>");
			}else{
				row2=document.createElement("<li id='"+this.id+'_Tabs'+i+"' name='"+this.id+'_Tabs'+"' class='tabsStyle'/>");
			}
		}else{
	  		var row2 = document.createElement('li');
	  		row2.setAttribute("name", this.id+'_Tabs');
			if(this.hl[i] && i==0)
				row2.setAttribute("class", "tabsStyleSelectedHl");
	  		else if(this.hl[i]){
	  			row2.setAttribute("class", "tabsStyleHl");
	  		}else if(i==0){
	  			row2.setAttribute("class", "tabsStyleSelected");
			}else{
				row2.setAttribute("class", "tabsStyle");
			}
	  		row2.setAttribute("id", this.id+'_Tabs'+i);
	  	}
  		
 		row2.innerHTML = this.labels[i];
 		row.appendChild(row2);
	}

	if(navigator.appVersion.indexOf('MSIE 6') > -1 || navigator.appVersion.indexOf('MSIE 7') > -1){
		row=document.createElement("<div id='"+this.id+'_Content'+"' name='"+this.id+'_Content'+"' class='contentsStyle'/>");
	}else{
  		var row = document.createElement('div');
  		row.setAttribute("name", this.id+'_Content');
  		row.setAttribute("id", this.id+'_Content');
  		row.setAttribute("class", "contentsStyle");
  	}
  	contentNode.appendChild(row);

	for(i=0;i<nb;i++){
	
		if(navigator.appVersion.indexOf('MSIE 6') > -1 || navigator.appVersion.indexOf('MSIE 7') > -1){
			if(this.hl[i] && i==0)
				row2=document.createElement("<div id='"+this.id+'_Content'+i+"' name='"+this.id+'_Content'+"' class='tabsStyleSelectedHl'/>");
			else if(i==0)
				row2=document.createElement("<div id='"+this.id+'_Content'+i+"' name='"+this.id+'_Content'+"' class='contentsStyleSelected'/>");
			else if(this.hl[i])
				row2=document.createElement("<div id='"+this.id+'_Content'+i+"' name='"+this.id+'_Content'+"' class='contentsStyleHl'/>");
			else
				row2=document.createElement("<div id='"+this.id+'_Content'+i+"' name='"+this.id+'_Content'+"' class='contentsStyle'/>");
		}else{
	  		var row2 = document.createElement('div');
	  		row2.setAttribute("name", this.id+'_Content');
	  		if(this.hl[i] && i==0)
	  			row2.setAttribute("class", "contentsStyleSelectedHl");
	  		else if(i==0)
	  			row2.setAttribute("class", "contentsStyleSelected");
	  		else if(this.hl[i])
	  			row2.setAttribute("class", "contentsStyleHl");
	  		else
	  			row2.setAttribute("class", "contentsStyle");
	  		row2.setAttribute("id", this.id+'_Content'+i);
		}
 		row2.innerHTML = contents[i];
 		row.appendChild(row2);
	}


	for(i=0;i<nb;i++){
		eval("document.getElementById('"+this.id+"_Tabs').childNodes["+i+"].onclick=function(){changeTab('"+this.id+"','"+i+"');}");
	}
}

function changeTab(id,icurrent) {
	eval("tab = "+id);
	
		for(i=0;i<document.getElementById(id+'_Tabs').childNodes.length;i++){
			document.getElementById(id+'_Content'+i).className='contentsStyle';
			if(tab.hl[i])
				document.getElementById(id+'_Tabs'+i).className='tabsStyleHl';
			else
				document.getElementById(id+'_Tabs'+i).className='tabsStyle';
		}
		if(tab.hl[icurrent]){
			document.getElementById(id+'_Tabs'+icurrent).className='tabsStyleSelectedHl';
			document.getElementById(id+'_Content'+icurrent).className='contentsStyleSelectedHl';
		}else{
			document.getElementById(id+'_Tabs'+icurrent).className='tabsStyleSelected';
			document.getElementById(id+'_Content'+icurrent).className='contentsStyleSelected';
		}
}

function flipDisplay(id){
	if(document.getElementById(id)){
		if(document.getElementById(id).style.display=='none' || document.getElementById(id).style.display==''){
			document.getElementById(id).style.display='block';
		}else{
			document.getElementById(id).style.display='none';
		}
	}
}

function flipDisplayByName(name){
	e = getElementsByName_iefix('*',name);
	for(i=0;i<e.length;i++){
		if(e[i].style.display=='block' || e[i].style.display==''){
			e[i].style.display='none';
		}else{
			e[i].style.display='block';
		}
	}
}

function noneDisplayByName(name){
	e = getElementsByName_iefix('*',name);
	for(i=0;i<e.length;i++){
		e[i].style.display='none';
	}
}

function flipClass(id){
	if(document.getElementById('bt'+id).className == 'btLiOver'){
		document.getElementById('bt'+id).className = 'btLiOut';
		document.getElementById('text'+id).className = 'btLiOut';
	}else{
		document.getElementById('bt'+id).className = 'btLiOver';
		document.getElementById('text'+id).className = 'btLiOver';
	}
}

function flipTheClass(id,class1,class2){
	if(document.getElementById(id).className == class1){
		document.getElementById(id).className = class2;
	}else{
		document.getElementById(id).className = class1;
	}
}

function getElementsByName_iefix(tag, name) {
     
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}

function changeClassByName(name,class1){
	e = getElementsByName_iefix('*',name);
	for(i=0;i<e.length;i++){
		e[i].className=class1;
	}
}



function menuRollover(id){
	document.getElementById('bt'+id).className = 'btLiOver';
	document.getElementById('text'+id).className = 'btLiOver';
}
function menuRollout(id){
	document.getElementById('bt'+id).className = 'btLiOut';
	document.getElementById('text'+id).className = 'btLiOut';
}


function checkEnableField(depend,val,field){
	for(i=0;i<val.length;i++){
		if(document.getElementById(depend).value==val[i]){
			document.getElementById("table_"+field).style.visibility="visible";
			break;
		}else{
			document.getElementById("table_"+field).style.visibility="hidden";
	
		}
	}
}
function createGalerie(id,idgalerie,slidetime,imagetype) {
	eval("window."+id+"_currentImage = 0");
	dojo.xhrGet({
	 url: 'widmin/modules/galeries/getimages.php?id='+idgalerie+'&imagetype='+imagetype,
    handleAs: 'text',
    load: function(data,args){
    	
      eval("window."+id+"_images = new Array("+data+");");
      document.getElementById(id).src=eval(id+"_images["+id+"_currentImage]");
      eval("window."+id+"_currentImage++");
    }
  });
  setTimeout ("changeImageGalerie('"+id+"','"+slidetime+"')",3000);
}
function changeImageGalerie(id,slidetime) {
	Effect.Fade(id, { duration: 1.0 });
	setTimeout ( "document.getElementById('"+id+"').src="+id+"_images["+id+"_currentImage]", 1000);
	setTimeout (id+"_currentImage++;if("+id+"_currentImage+1>("+id+"_images.length-1)){"+id+"_currentImage=0;}",1000)
	setTimeout ( "Effect.Appear('"+id+"', { duration: 1.0 });", 2000 );
	setTimeout ( "changeImageGalerie('"+id+"','"+slidetime+"');", slidetime );
}
function toMousePosition(id){
  document.getElementById(id).style.left = xMousePos+'px';
  document.getElementById(id).style.top = yMousePos+'px';

}

function initializeMap(id,lat,lon,zoum) {
    var latlng = new google.maps.LatLng(lat,lon);
    geocoder = new google.maps.Geocoder();
    var myOptions = {
      zoom: zoum,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById(id), myOptions);
    return map;
}

function setMarker(id,lat,lon,title,html,icon) {
  var latlng = new google.maps.LatLng(lat,lon);
  if(icon!='')
  	var iconM = new google.maps.MarkerImage(icon);
  var marker = new google.maps.Marker({
      position: latlng, 
      map: id, 
      title:title,
      icon:iconM
  });
  google.maps.event.addListener(marker, 'click', function() {
 	if(typeof( infowindow ) != 'undefined'){   
   	infowindow.close();
   }
  	 infowindow = new google.maps.InfoWindow(
      { content: html,
        size: new google.maps.Size(50,50)
      });
    infowindow.open(id,marker);
  });

}

function setGeoMarker(id,mapp,add,title,html,icon) {
 var address = add;
 if (geocoder) {
   geocoder.geocode( { 'address': address}, function(results, status) {
     if (status == google.maps.GeocoderStatus.OK) {
       mapp.setCenter(results[0].geometry.location);
		  if(icon!='')
		  	var iconM = new google.maps.MarkerImage(icon);
		  var marker = new google.maps.Marker({
		      position: results[0].geometry.location, 
		      map: mapp, 
		      title:title,
		      icon:iconM
		  });
		  google.maps.event.addListener(marker, 'click', function() {
			 	if(typeof( infowindow ) != 'undefined'){   
			   	infowindow.close();
			   }
			  	 infowindow = new google.maps.InfoWindow(
			      { content: html,
			        size: new google.maps.Size(50,50)
			      });
			    infowindow.open(mapp,marker);
		  });
     }
   });
 }
}

function setGeoMarkerOpen(id,mapp,add,html) {
 var address = add;
 if (geocoder) {
   geocoder.geocode( { 'address': address}, function(results, status) {
     if (status == google.maps.GeocoderStatus.OK) {
       mapp.setCenter(results[0].geometry.location);
       var marker = new google.maps.Marker({
           map: mapp, 
           position: results[0].geometry.location
       });
		  var infowindow = new google.maps.InfoWindow(
		      { content: html,
		        size: new google.maps.Size(50,50)
		      });
		  infowindow.open(mapp,marker);
		  google.maps.event.addListener(marker, 'click', function() {
		    infowindow.open(mapp,marker);
		  });
     }
   });
 }
}

function setGeoCenter(mapp,add) {
 var address = add;
 if (geocoder) {
   geocoder.geocode( { 'address': address}, function(results, status) {
     if (status == google.maps.GeocoderStatus.OK) {
       mapp.setCenter(results[0].geometry.location);
     }
   });
 }
}
function setDirectionAndKm(id,kmid,lat1,lon1,lat2,lon2) {
  var latlng1 = new google.maps.LatLng(lat1,lon1);
  var latlng2 = new google.maps.LatLng(lat2,lon2);
  
  var directionsDisplay;
  directionsDisplay = new google.maps.DirectionsRenderer();
  var directionsService = new google.maps.DirectionsService();
  directionsDisplay.setMap(id);

  var request = {
    origin:latlng1, 
    destination:latlng2,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
  };
  directionsService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(result);
		var route = result.routes[0];
      var summaryPanel = document.getElementById(kmid);
      var km="";
      for (var i = 0; i < route.legs.length; i++) {
        var routeSegment = i+1;
        km+=route.legs[i].distance.text;
		}
		 summaryPanel.innerHTML = km;
    }
  });
}

function urlToHtml(url,id){
	var contentNode = document.getElementById(id);	
	dojo.xhrGet({
	    url: url,
	    handleAs: 'text',
	    timeout:20000,
	    load: function(data,args){
	    	if(data==''){
	    		setTimeout('urlToHtml("'+url+'","'+id+'")', 2000);
	    	}else
				contentNode.innerHTML=data;
			
	    },
	    error: function(error,args){
	    	contentNode.innerHTML="";
			console.warn("error!",error);
	    }
	});
}

function urlToAddHtml(url,id){
	var contentNode = document.getElementById(id);	
	dojo.xhrGet({
	    url: url,
	    handleAs: 'text',
	    timeout:20000,
	    load: function(data,args){
	    	if(data==''){
	    		setTimeout('urlToHtml("'+url+'","'+id+'")', 2000);
	    	}else
				contentNode.innerHTML=contentNode.innerHTML+data;
			
	    },
	    error: function(error,args){
	    	contentNode.innerHTML="";
			console.warn("error!",error);
	    }
	});
}
function urlToNothing(url){
	dojo.xhrGet({
	    url: url,
	    handleAs: 'text',
	    timeout:20000
	});
}
function progressBar(current,total){
	pt = Math.round(100/total*current);
	document.getElementById('bar_progress').style.width=pt+'%';
	document.getElementById('bar_text').innerHTML=pt+'%';
}
