/**

 * @version 1.0.2
 * @date 2011-12-21
 * @author  Luca Donazzon <luca.donazzon@interlogica.it>

 */
 
// ##########################################################################################
// ##########################################################################################
function myConsoleObj(){
	
	try{
		var fb=(console.log?true:false);	
	}
	catch(e){
		//alert(e)
		var fb=false;
	}
	
	this.log=function($msg){
		if(fb)
			console.log($msg)
	}
}
var myconsole=new myConsoleObj(); 
/* ############################################################################################## */
Element.implement({
  hasEvent: function(eventType,fn) {
    //get the element's events
    var myEvents = this.retrieve('events');
    //can we shoot this down?
    return myEvents && myEvents[eventType] && (fn == undefined || myEvents[eventType].keys.contains(fn));
  }
});
//########################################################################################################################################
function scriviCookie(nomeCookie,valoreCookie,durataCookie){
  var scadenza = new Date();
  var adesso = new Date();
  scadenza.setTime(adesso.getTime() + (parseInt(durataCookie) * 60000));
  document.cookie = nomeCookie + '=' + escape(valoreCookie) + '; expires=' + scadenza.toGMTString() + '; path=/';
}
//########################################################################################################################################
function leggiCookie(nomeCookie){
  if (document.cookie.length > 0)
  {
    var inizio = document.cookie.indexOf(nomeCookie + "=");
    if (inizio != -1)
    {
      inizio = inizio + nomeCookie.length + 1;
      var fine = document.cookie.indexOf(";",inizio);
      if (fine == -1) fine = document.cookie.length;
      return unescape(document.cookie.substring(inizio,fine));
    }else{
       return "";
    }
  }
  return "";
}
//########################################################################################################################################
function translate(t){
	try{
		if(lang) var err=false;
	}catch(e){
		return t;
	}
	
	if ( lang != 'en') return t;
		
	var l=new Array();
	
    l['Manuale'] 			= 'Manual';
    l['Automatico'] 		= 'Automatic';	
    
    l['Diesel'] 			= 'Diesel';
    l['Benzina'] 			= 'Unleaded';
    l['Ibrida'] 			= 'Hybrid';
    
    l['Modello'] 			= 'Model';
    l['Marca'] 				= 'Make';
    l['Tipo'] 				= 'Type';
    l['Cambio'] 			= 'Gear Type';
    l['Alimentazione'] 		= 'Fuel';
    l['Chilometraggio da'] 	= 'Mileage from';
    l['Immatricolazione da']= 'Registration from';
    l['Potenza da'] 		= 'KW from';
    l['Prezzo da']	 		= 'Price from';
    l['Zona']		 		= 'Area';
    l['a'] 	= 'to';
	
	//myconsole.log('lang: '+lang + ' | ' + t + '='+l[t]);
	if( l[t] )
		return l[t];
	else
		return t;
			
}
//########################################################################################################################################
function load_cambio(){

	var newoption = new Option('*', '');
	$('sel_cambio').add(newoption);
	$('sel_cambio').set('disabled','');
		
	var jsonReq = new Request.JSON({ 
		url: '/it/vetrina-carnext.php', 
	  	method: 'post', 
	  	data: {cmd: 'get_cambio', 'idvetrina': idvetrina}, 
	  	onComplete: function(re){ 
	    	if (re.success){
	    		re.dati.each(function(o,i){
	    			var newoption = new Option( translate(o.value), o.value);
	    			if(o.value == sel_cambio){ 
	    				newoption.setAttribute('selected', 'selected');
	    			}
	    			$('sel_cambio').add(newoption);
	    		});
	    		
	    	}else{
	    		myconsole.log(re);
	    		alert('Errore di comunicazione con il server');	
	    	}
	  	} 
	}).send();
}
//########################################################################################################################################
function load_alimentazione(){
	//var newoption = new Option("option html", "option value");	
	var newoption = new Option('*', '');
	$('sel_alimentazione').add(newoption);
	$('sel_alimentazione').set('disabled','');
		
	var jsonReq = new Request.JSON({ 
		url: '/it/vetrina-carnext.php', 
	  	method: 'post', 
	  	data: {cmd: 'get_alimentazione', 'idvetrina': idvetrina}, 
	  	onComplete: function(re){ 
	    	if (re.success){
	    		re.dati.each(function(o,i){
	    			var newoption = new Option( translate(o.value), o.value);
	    			if(o.value == sel_alimentazione){ 
	    				newoption.setAttribute('selected', 'selected');
	    			}
	    			$('sel_alimentazione').add(newoption);
	    		});
	    		
	    	}else{
	    		myconsole.log(re);
	    		alert('Errore di comunicazione con il server');	
	    	}
	  	} 
	}).send();
}
//########################################################################################################################################
function load_marche(){
	//var newoption = new Option("option html", "option value");	
	var newoption = new Option('*', '');
	$('sel_marca').add(newoption);
		
	var jsonReq = new Request.JSON({ 
		url: '/it/vetrina-carnext.php', 
	  	method: 'post', 
	  	data: {cmd: 'get_marche', 'idvetrina': idvetrina}, 
	  	onComplete: function(re){ 
	    	if (re.success){
	    		re.dati.each(function(o,i){
	    			var newoption = new Option(o.value, o.value);
	    			if(o.value == sel_marca){ 
	    				//newoption.setProperty('selected', 'selected');
	    				//newoption.setProperty('selected', true);
	    				newoption.setAttribute('selected', true);
	    				load_modelli(sel_marca);
	    			}
	    			$('sel_marca').add(newoption);
	    		});
	    		if( !$('sel_marca').hasEvent('change') )
					$('sel_marca').addEvent('change', function(){
					    load_modelli(this.getSelected()[0].value);
					});	    		
	    	}else{
	    		myconsole.log(re);
	    		alert('Errore di comunicazione con il server');	
	    	}
	  	} 
	}).send();
}
//########################################################################################################################################
function load_modelli(v_marca){
	$('sel_modello').empty();
	$('sel_tipo').empty();
	$('sel_tipo').set('disabled','disabled');
	var newoption = new Option('*', '');
	$('sel_modello').add(newoption);
	$('sel_modello').set('disabled','');
	
	var jsonReq = new Request.JSON({ 
		url: '/it/vetrina-carnext.php', 
	  	method: 'post', 
	  	data: {cmd: 'get_modelli', marca: v_marca, 'idvetrina': idvetrina}, 
	  	onComplete: function(re){ 
	    	if (re.success){
	    		re.dati.each(function(o,i){
	    			var newoption = new Option(o.value, o.value);
	    			if(o.value == sel_modello){ 
	    				newoption.setAttribute('selected', 'selected');
	    				load_tipo(sel_marca,sel_modello);
	    			}	    			
	    			$('sel_modello').add(newoption);
	    		});
	    		if( !$('sel_modello').hasEvent('change') )
					$('sel_modello').addEvent('change', function(){
						//myconsole.log(this.getSelected()[0].value);
					    load_tipo($('sel_marca').get('value'),this.getSelected()[0].value);
					});
	    	}else{
	    		myconsole.log(re);
	    		alert('Errore di comunicazione con il server');	
	    	}
	  	} 
	}).send();		
}
//########################################################################################################################################
function load_tipo(v_marca,v_modello){
	myconsole.log(sel_tipo );
	$('sel_tipo').empty();
	var newoption = new Option('*', '');
	$('sel_tipo').add(newoption);
	$('sel_tipo').set('disabled','');
	
	var jsonReq = new Request.JSON({ 
		url: '/it/vetrina-carnext.php', 
	  	method: 'post', 
	  	data: {cmd: 'get_tipi', marca: v_marca, modello: v_modello, 'idvetrina': idvetrina}, 
	  	onComplete: function(re){ 
	    	if (re.success){
	    		re.dati.each(function(o,i){
	    			var newoption = new Option(o.value, o.value);
	    			if(o.value == sel_tipo){ 
	    				newoption.setAttribute('selected', 'selected');
	    			}		    			
	    			$('sel_tipo').add(newoption);
	    		});
	    		if( !$('sel_tipo').hasEvent('change') )
					$('sel_tipo').addEvent('change', function(){
						myconsole.log(this.getSelected()[0].value);
					    //load_tipo(v_marca,this.getSelected()[0].value);
					});
	    	}else{
	    		myconsole.log(re);
	    		alert('Errore di comunicazione con il server');	
	    	}
	  	} 
	}).send();	
}

//########################################################################################################################################
function go2det(id){
	$('dett_id').set('value',id);
	$("form-dettaglio").submit();	
}

//########################################################################################################################################
var potenza='';
function load_potenza(){

	$('sel_potenza_min').empty();
	var newoption = new Option('*', '');
	$('sel_potenza_min').add(newoption);
	$('sel_potenza_min').set('disabled','');
	
	var jsonReq = new Request.JSON({ 
		url: '/it/vetrina-carnext.php', 
	  	method: 'post', 
	  	data: {cmd: 'get_potenza', 'idvetrina': idvetrina}, 
	  	onComplete: function(re){ 
	    	if (re.success){
	    		potenza = re.dati; 
	    		re.dati.each(function(o,i){
	    			var newoption = new Option(o.value, o.value);
	    			if(o.value == sel_potenza_min){ 
	    				newoption.setAttribute('selected', 'selected');
	    				load_potenza_max(sel_potenza_min)
	    			}		    			
	    			$('sel_potenza_min').add(newoption);
	    		});
	    		
	    		load_potenza_max();
	    		
	    		if( !$('sel_potenza_min').hasEvent('change') )
					$('sel_potenza_min').addEvent('change', function(){
						//myconsole.log(this.getSelected()[0].value);
					    load_potenza_max(this.getSelected()[0].value);
					});
	    	}else{
	    		myconsole.log(re);
	    		alert('Errore di comunicazione con il server');	
	    	}
	  	} 
	}).send();	
}
//#############################
function load_potenza_max(min){
	$('sel_potenza_max').empty();
	var newoption = new Option('*', '');
	$('sel_potenza_max').add(newoption);
	$('sel_potenza_max').set('disabled','');	
	potenza.each(function(o,i){
		if( min == undefined || parseInt(o.value) >= parseInt(min)){
			var newoption = new Option(o.value, o.value);
			//myconsole.log( '-'+o.value +'=='+ sel_potenza_max + '-' );
			if(o.value == sel_potenza_max){ 
				newoption.setAttribute('selected', 'selected');
			}		    			
			$('sel_potenza_max').add(newoption);			
		}
	});
}
//########################################################################################################################################

//########################################################################################################################################
var immatricolazione='';
function load_immatricolazione(){

	$('sel_immatricolazione_da').empty();
	var newoption = new Option('*', '');
	$('sel_immatricolazione_da').add(newoption);
	$('sel_immatricolazione_da').set('disabled','');
	
	var jsonReq = new Request.JSON({ 
		url: '/it/vetrina-carnext.php', 
	  	method: 'post', 
	  	data: {cmd: 'get_immatricolazione', 'idvetrina': idvetrina}, 
	  	onComplete: function(re){ 
	    	if (re.success){
	    		immatricolazione = re.dati; 
	    		re.dati.each(function(o,i){
	    			var newoption = new Option(o.value, o.value);
	    			if(o.value == sel_immatricolazione_da){ 
	    				newoption.setAttribute('selected', 'selected');
	    				load_immatricolazione_a(sel_immatricolazione_da);
	    			}		    			
	    			$('sel_immatricolazione_da').add(newoption);
	    		});
	    		
	    		load_immatricolazione_a();
	    		
	    		if( !$('sel_immatricolazione_da').hasEvent('change') )
					$('sel_immatricolazione_da').addEvent('change', function(){
						//myconsole.log(this.getSelected()[0].value);
					    load_immatricolazione_a(this.getSelected()[0].value);
					});
	    	}else{
	    		myconsole.log(re);
	    		alert('Errore di comunicazione con il server');	
	    	}
	  	} 
	}).send();	
}
//#############################
function load_immatricolazione_a(da){

	$('sel_immatricolazione_a').empty();
	var newoption = new Option('*', '');
	$('sel_immatricolazione_a').add(newoption);
	$('sel_immatricolazione_a').set('disabled','');	
	immatricolazione.each(function(o,i){
		//myconsole.log( parseInt(o.value) +'>'+ parseInt(min) );
		if( da == undefined || parseInt(o.value) >= parseInt(da)){
			var newoption = new Option(o.value, o.value);
			if(o.value == sel_immatricolazione_a){ 
				newoption.setAttribute('selected', 'selected');
			}		    			
			$('sel_immatricolazione_a').add(newoption);			
		}
	});
}
//########################################################################################################################################
//########################################################################################################################################
var km_max='';
function load_kilometraggio(){
	//myconsole.log('load_kilometraggio');
	$('sel_km_da').empty();
	var newoption = new Option('*', '');
	$('sel_km_da').add(newoption);
	$('sel_km_da').set('disabled','');
	
	var jsonReq = new Request.JSON({ 
		url: '/it/vetrina-carnext.php', 
	  	method: 'post', 
	  	data: {cmd: 'get_kilometraggio', 'idvetrina': idvetrina}, 
	  	onComplete: function(re){ 
	  		//myconsole.log(re);
	    	if (re.success){
	    		km_max = re.max;
	    		//myconsole.log(km_max)
	    		km_max=parseInt(km_max/5000);
	    		var v=0;
	    		for(var i=0; i <= km_max;i++){
	    			var v=i*5000;
	    			var newoption = new Option( v , v);
	    			if(v == parseInt(sel_km_da)){ 
	    				//newoption.setProperty('selected', 'selected');
	    				newoption.setAttribute('selected', 'selected');
	    				load_kilometraggio_a(sel_km_da);
	    			}		    			
	    			$('sel_km_da').add(newoption);	    			
	    		} 
	    		
	    		load_kilometraggio_a();
	    		
	    		if( !$('sel_km_da').hasEvent('change') )
					$('sel_km_da').addEvent('change', function(){
						//myconsole.log(this.getSelected()[0].value);
					    load_kilometraggio_a(this.getSelected()[0].value);
					});
	    	}else{
	    		myconsole.log(re);
	    		alert('Errore di comunicazione con il server');	
	    	}
	  	} 
	}).send();	
}
//#############################
function load_kilometraggio_a(da){

	$('sel_km_a').empty();
	var newoption = new Option('*', '');
	$('sel_km_a').add(newoption);
	$('sel_km_a').set('disabled','');	
	
	var v=0;
	for(var i=1; i <= km_max+1;i++){
		var v=i*5000;
		if(da == undefined || v > parseInt(da)){
			var newoption = new Option( v , v);
			if(v == sel_km_a){ 
				newoption.setAttribute('selected', 'selected');
			}		    			
			$('sel_km_a').add(newoption);				
		}
	}
}
//########################################################################################################################################
var prezzo_max='';
function load_prezzo(){
	$('sel_prezzo_da').empty();
	var newoption = new Option('*', '');
	$('sel_prezzo_da').add(newoption);
	$('sel_prezzo_da').set('disabled','');
	
	//richiesta del 21/12/11 ore 15:07
	var newoption = new Option( 0 , 0);
	if(0 == parseInt(sel_prezzo_da)) newoption.setAttribute('selected', 'selected');
	$('sel_prezzo_da').add(newoption);	
	var newoption = new Option( 2000 , 2000);
	if(2000 == parseInt(sel_prezzo_da)) newoption.setAttribute('selected', 'selected');
	$('sel_prezzo_da').add(newoption);
	//-------------------------------------
	
	var jsonReq = new Request.JSON({ 
		url: '/it/vetrina-carnext.php', 
	  	method: 'post', 
	  	data: {cmd: 'get_prezzo', 'idvetrina': idvetrina}, 
	  	onComplete: function(re){ 
	    	if (re.success){
	    		prezzo_max=parseInt(re.max/5000);
	    		var v=0;
	    		for(var i=1; i <= prezzo_max;i++){
	    			var v=i*5000;
	    			var newoption = new Option( v , v);
	    			if(v == parseInt(sel_prezzo_da)){
	    				//myconsole.log(v+' -> sel_prezzo_da: '+parseInt(sel_prezzo_da)+ '  - ' + typeof sel_prezzo_da); 
	    				newoption.setAttribute('selected', 'selected');
	    				load_prezzo_a(sel_prezzo_da);
	    			}		    			
	    			$('sel_prezzo_da').add(newoption);	    			
	    		}
	    		load_prezzo_a();
	    		
	    		if( !$('sel_prezzo_da').hasEvent('change') )
					$('sel_prezzo_da').addEvent('change', function(){
					    load_prezzo_a(this.getSelected()[0].value);
					});
	    	}else{
	    		myconsole.log(re);
	    		alert('Errore di comunicazione con il server');	
	    	}
	  	} 
	}).send();	
}
//#############################
function load_prezzo_a(da){
	//myconsole.log('da: '+da)
	$('sel_prezzo_a').empty();
	var newoption = new Option('*', '');
	$('sel_prezzo_a').add(newoption);
	$('sel_prezzo_a').set('disabled','');	
	
	var v=0;
	for(var i=1; i <= prezzo_max+1;i++){
		var v=i*5000;

		if(da == undefined || da =='' || v > parseInt(da)){
			//richiesta del 21/12/11 ore 15:07
			if(v==5000 && (da == undefined || da =='' || 2000> parseInt(da)) ){
				var newoption = new Option( 2000 , 2000);
				if(2000 == parseInt(sel_prezzo_a)) newoption.setAttribute('selected', 'selected');
				$('sel_prezzo_a').add(newoption);
			}
			//-------------------------------------
					
			var newoption = new Option( v , v);
			if(v == parseInt(sel_prezzo_a)){
				newoption.setAttribute('selected', 'selected');
			}		    			
			$('sel_prezzo_a').add(newoption);				
		}
	}
	if( !$('sel_prezzo_a').hasEvent('change') )
		$('sel_prezzo_a').addEvent('change', function(){
		    sel_prezzo_a=this.getSelected()[0].value;
		});	
}
//########################################################################################################################################
function load_left_sidebar(){
	$('left_sidebar').empty();
	var h2 = new Element('h2',{html:'Le nostre occasioni'});
	var loading = new Element('img',{
		src:'../siteimg/loading.gif',
		style:'margin:20px 0 0 20px'
	});
	$('left_sidebar').adopt(h2);
	$('left_sidebar').adopt(loading);
	
	//return;	
	var jsonReq = new Request.JSON({ 
		url: '/it/vetrina-carnext.php', 
	  	method: 'post', 
	  	data: {cmd: 'get_occasioni'}, 
	  	onComplete: function(re){ 
	    	if (re.success){
	    		
				$('left_sidebar').empty();
				var h2 = new Element('h2',{html:'Le nostre occasioni'});
	    		$('left_sidebar').adopt(h2);
	    		
	    		var ul = new Element('ul',{'class': 'sub_nav'});
	    		re.dati.each(function(o,i){
	    			//<ul class="sub_nav"></ul>
	    			
	    			var li = new Element('li',{'class': ''});
	    			var a = new Element('a',{
	    				href: '#',
	    				//onclick: "go2det("+o.id+");return false;",
	    				text:o.marca + ' ' + o.modello + ' ' + o.tipo
	    			});
	    			a.addEvent('click',function(e){
	    				e.stop();
	    				myconsole.log(o.id);
	    				go2det(o.id);
	    			});
	    			li.adopt(a);
	    			ul.adopt(li);
	    			//$('left_sidebar').adopt(ul);
	    		});
	    			$('left_sidebar').adopt(ul);
	    		
	    	}else{
	    		myconsole.log(re);
	    		alert('Errore di comunicazione con il server');	
	    	}
	  	} 
	}).send();		
	
}
//########################################################################################################################################
function logout() {
	$("servizio").submit();
}
//########################################################################################################################################
