var tabSliders = {};
var quotationForm = 'quotationForm';

/** STEP 1 **/
isExplorer = function() {

	if( navigator.appName.indexOf("Explorer") > -1 ){
		return true;
	}
	return false;
}

/** STEP 2 **/
changeTotalClaims = function( totalClaims, sectionName, driver ) {
	
	totalClaims = parseInt(totalClaims,10);
	
	if (totalClaims > 4) return totalClaims;
	 
	// Hiding all Claims
	for (var i = 1; i <= 3; i++) {
		$(sectionName + i ).style.display = 'none';
	}
	
	// Show only right number of claims
	for (var j = 1; j <= totalClaims; j++) {
		$(sectionName + j ).style.display = 'block';
	}
	
	if (driver) {
		for (var k = 1; k < 4; k++) {
			if (k > totalClaims) {
				$(driver + '_claimDate' + (k)).value = '';
				$(driver + '_claimNature' + (k)).selectedIndex = 0;
			}
		}
	}
	
	return totalClaims;

}

secondDriverForm = function( show ) {

	if( show )
	{
		toggleQuotationSection('sectionPrincipalInsurance','close');
		toggleQuotationSection('sectionPrincipalClaims','close');
		$('sectionPrincipalInsuranceModifyLink').removeClass("quotation_close");
		toggleQuotationSection('sectionSecondDriver','open');
		$('sectionSecondInsurance').style.display = 'block';
	}
	else
	{
		toggleQuotationSection('sectionSecondDriver','close');
		$('sectionSecondInsurance').style.display = 'none';
		$('sectionSecondClaims').style.display = 'none';
	}
}

buildAccordion = function (show, isContent) {

	var accordionPane; 
	if (!isContent) {
		accordionPane = $('quotation_right_pane');
	} else {
		accordionPane = $('content_right_pane');
	}
	
/*
	var myAccordion = new Accordion(accordionPane, 'div.quotationServiceHead', 'div.quotationServiceBody', {
		alwaysHide: true,
		opacity: false,
		show: show,
		onActive: function(toggler, element){
			toggler.setStyle('color', '#FFFFFF');
			//var img = toggler.getElement('img');
			//img.src = '../img/arrow_service_on.gif';
			toggler.getElement('h4').removeClass('close');
			toggler.getElement('h4').addClass('open');
		},
		onBackground: function(toggler, element){
			toggler.setStyle('color', '#FFFFFF');
			//var img = toggler.getElement('img');
			//img.src = '../img/arrow_service_off.gif';
			toggler.getElement('h4').removeClass('open');
			toggler.getElement('h4').addClass('close');
		}
	});

*/
	if (typeof(show) == "number") {
		_tmp = show;
		show = $A([]);
		show.push(_tmp);
	} else {
		show = $A(show);
	}
	
	if (accordionPane) {
	$ES('div.quotationService' ,  $( accordionPane ) ).each( function( element , index ) {
		
		element.openerCloser = element.getElement('div.quotationServiceHead h4');
		
		element.content = element.getElement('div.quotationServiceBody');
		
		element.content.fx = new Fx.Slide( element.content , { 
			'duration' : 500 , 
			'wait' : true , 
			'onComplete':function(){
				if ( window.ie && !this.element.isOpen)
					this.wrapper.setStyle('height','0');
				else
					this.wrapper.setStyle('height','auto'); 
			}
		});
		
		element.toggleSlide = _toggleSlide;
		element.slideIn =  _slideIn;
		element.slideOut =  _slideOut;
			
		element.openerCloser.getParent().addEvent( 'click' , _toggleSlide.bind( element , [ ] )  );
		
		/* Anomalia 155 */
		if ( !show.contains(index) )
			element.slideIn();
		else
			element.slideOut();
		
	});
	}
}


/* Fat:
 *	open or close the content div
 *	the 'this' is div.summaryContainer
 */
function _toggleSlide(){

	if ( this.content.isOpen )
		this.slideIn();			// close the div
	else
		this.slideOut();				// open the div
		
	
}

/* open the content div */
function _slideOut(){

	this.content.fx.slideIn();

	if ( this.openerCloser.img )
		this.openerCloser.img.src = js_contextPath + '/img/arrow_service_small_on.gif';

	this.content.isOpen = true;
	
	this.openerCloser.removeClass('close').addClass('open');
	
}

/* close the content div */
function _slideIn(){

	this.content.fx.slideOut();

	this.content.isOpen = false;
	
	if ( this.openerCloser.img )
		this.openerCloser.img.src = js_contextPath + '/img/arrow_service_small_off.gif';
	
	this.openerCloser.removeClass('open').addClass('close');
}




changeSection = function( evt ) {
	var slideEffect = true;
	var skipToggle = false;
	// AUNCUNE GESTION SI
	// STEP 1: click sur une checkbox dans le tableau des voitures.
	var event = new Event(evt);
	var target = $(event.target)
	
	// STEP 2: change le nombre de sinistre
	if( target.getParent().id == 'main_claims3Years' || 
		target.getParent().id == 'second_claims3Years' ) 
		return;
	
	// STEP 3: pas d'effet slide
	if( this.id == 'sectionQuotationPrice' || this.id == 'sectionCustomQuotation' || this.id == 'sectionRecommendations' ){
		slideEffect = false;
	}
	
	// STEP 4: pas d'effet slide
	if( this.id == 'sectionInfosCar' ||  this.id == 'sectionInfosPrincipalDriver' || this.id == 'sectionLoginInfos'){
		slideEffect = false;
		skipToggle = true;
		
		// Deactivating all
		$ES('.quotation_section_title', $(quotationForm)).each( function(element) {
			element.addClass('quotation_section_open');
			element.removeClass('quotation_section_active');
		});
		
		// Activating the right one
		$ES('.quotation_section_title', $(this.id)).each( function(element) {
			element.addClass('quotation_section_active');
		});
	}
	
	if ($('sectionSecondDriverTitle')) {
		if (
			$('sectionSecondDriverTitle').hasClass("quotation_section_active") || 
			$('sectionSecondDriverTitle').hasClass("quotation_section_open") && $('sectionSecondDriverContent').hasClass("quotation_close")
		   ) 
			return;
	}
	
	if (!skipToggle) {
		var elements = $$('div.quotation_section, div.quotation_section_middle, div.quotation_section_orange');
		var nbElements = elements.length;
		var inc;
		for( inc = 0; inc < nbElements; inc++ ){
			if( this.id != elements[inc].id ){
				if( elements[inc].getFirst().hasClass('quotation_section_active') ){
					toggleQuotationSection(elements[inc].id,'open',slideEffect);
				}
			}
		}
		
		// set active
		if( this.getFirst().hasClass('quotation_section_open') ){
			toggleQuotationSection(this.id,'active',slideEffect);
		}
	}
}


toggleQuotationSection = function( section_id, status, slideEffect, time , force) {
	throw "toggleQuotationSection";
	time = time || 500;
	var element = $(section_id);
	if( !element ) return;
	if( element.getFirst().hasClass('quotation_section_'+status) && !force ) return;
	
	if( !$defined(slideEffect) ) slideEffect = true;
	/*
	console.info(section_id);
	console.info(status);
	console.info('=============');
	*/
	element.getFirst().removeClass('quotation_section_close');
	element.getFirst().removeClass('quotation_section_active');
	element.getFirst().removeClass('quotation_section_open');
	
	if ( status == 'close' )
		element.getFirst().addClass('quotation_section_'+status);
	else
		element.getFirst().addClass('quotation_section_active').addClass('quotation_section_open');
	
	if( !element.fxSlide )
	{
		element.fxSlide = new Fx.Slide( element.getElement('.quotation_section_content') ,{
			duration: time,
			onComplete: function(content){
				if( element.getElement('.quotation_section_content').hasClass('slideOut') ){
					// Close
					
					element.getElement('.quotation_section_footer').removeClass('quotation_open');
					element.getElement('.quotation_section_footer').setStyle('visibility','hidden'); 	//addClass('quotation_close');
				}else{
					//Open
					
					element.getElement('.quotation_section_footer').removeClass('quotation_close');
					element.getElement('.quotation_section_footer').setStyle('visibility','visible');	//addClass('quotation_open');
					$ES('.help' , element ).setStyle('display','block');
					if ( window.firefox ){
						var tmpEl = element.getElement('.quotation_section_content');
						var diff = tmpEl.getParent().scrollTop - tmpEl.scrollTop;
						if ( diff > 0 ){
							var margin = tmpEl.getStyle('marginTop').toInt();
							tmpEl.setStyle('margin-top', (diff + margin));								
						}
					}
				}
			}
		});
	}
	var height = element.fxSlide.wrapper.style.height;
	if( status == 'open' || status == 'active' ){
		//console.info('Apro il div'+ section_id);
		/**APRO IL DIV*/
		
		element.removeClass('js_closed');
		element.addClass('js_opened');
		
		element.getElement('.quotation_section_content').removeClass('slideOut');
		element.getElement('.quotation_section_content').removeClass('quotation_close');
		
		// Hide modify link
		if ( element.getElement('.quotationModifyLink') )
			element.getElement('.quotationModifyLink').addClass('quotation_close');
		
		if ( !slideEffect ){
			//console.info('no effects');
			element.getElement('.quotation_section_footer').removeClass('quotation_close');
			element.getElement('.quotation_section_footer').addClass('quotation_open');
			element.getElement('.quotation_section_footer').setStyle('visibility','visible');
			
			$ES( '.help' , element ).setStyle('display','block');
		}else{
			//console.info('do effects');
			element.fxSlide.slideIn( null, time );
		}
	}else{
		/**CHIUDO IL DIV*/
		
		element.addClass('js_closed');
		element.removeClass('js_opened');
		
		//console.info('Chiudo il div'+ section_id);
		element.getElement('.quotation_section_content').addClass('slideOut');
		
		// Show modify link
		if (element.getElement('.quotationModifyLink'))
			element.getElement('.quotationModifyLink').removeClass('quotation_close');
		
		$ES( '.help' , element ).setStyle('display','none');
		if (!slideEffect ){
			//console.info('no effects');
			//$$('#'+section_id+' .quotation_section_content')[0].addClass('quotation_close');
			element.getElement('.quotation_section_content').getParent().setStyle('height','0');
			element.getElement('.quotation_section_footer').removeClass('quotation_open');
			element.getElement('.quotation_section_footer').setStyle('visibility','hidden');//addClass('quotation_close');
		}else{
			//console.info('do effects');
			element.fxSlide.slideOut( null , time );
		}
	}
}




/** Le funzioni di gestione Password sono state spostate in common.js */





function favoris() {
if ( navigator.appName != 'Microsoft Internet Explorer' )
{ 
	window.sidebar.addPanel(document.title,document.location,"");
	
}
else {
	window.external.AddFavorite(document.location,document.title);
	
 } 
}



//lancio la ricerca delle localita'
function searchLocality(cmb,errorMessage,prefix,complete){
	this.cmb = cmb;
	this.errorMessage = errorMessage;
	
	if ( this.oldPostCode ){
		if ( this.oldPostCode.toLowerCase() == this.value.toLowerCase() )
			return;
	}
	
	this.cmb.empty();
	
	this.oldPostCode = this.value;
	
	if ( this.value.trim().length==0 ) return;
	
	onCmpl = (function(rsp) {
		if(!rsp){
			manageErrorLoadListLocality(this, true);
			return;
		} 
		manageErrorLoadListLocality(this, false);
		loadListLocality.apply(this, [rsp, this.cmb, this.errorMessage]);
		if (complete && typeof(complete) == 'function') {
			complete.apply(this , arguments);
		}
	}).bind(this); 
		
	
	this.onComplete = onCmpl;

	if (prefix) {
		prefix = '&prefix='+ prefix;
	} else {
		prefix = '';
	} 

	/**
	  * KYOS: nel caso mi trovi in change
	  * chiamo l'azione di ricerca postcode sul change 
	  */
	if(js_frameDialogue == 'change'){
		
		new Json.Remote(js_contextPath+'/do/'+js_frameDialogue+'/searchAjax',{
			method:'post',
			onComplete:this.onComplete.bind(this)
		}).send('method=searchPostCode&postCode='+this.value + prefix);
		
	} else if(js_frameDialogue=='personalData'){
		/**
		  * KYOS: nel caso mi trovi in personalData
		  * chiamo l'azione di ricerca postcode sul personalData 
		  */
		new Json.Remote(js_contextPath+'/do/'+js_frameDialogue+'/searchAjax',{
			method:'post',
			onComplete:this.onComplete.bind(this)
		}).send('method=searchPostCode&postCode='+this.value + prefix);
	
	} else {
		new Json.Remote(js_contextPath + '/do/' + js_staticDialogue + '/searchAjax',{
			method:'post',
			onComplete:this.onComplete.bind(this)
		}).send('method=searchPostCode&postCode=' + this.value + prefix);
	}
	
}

//Riempio il combo delle citta'
function loadListLocality(rsp,cmb,errorMessage){
	cmb = cmb || this.cmb;
	errorMessage = errorMessage || this.errorMessage;
	
	if ( $type(rsp)=='array' ){
	
		rsp.each(function(element){
			if (element.title) { // Skip check on value because of "Sélectioner"	
				var opt = new Element('option',{
					value:element.value,
					title:element.title
				}).setHTML(element.label);
				opt.inject(cmb);
			}
		});
		this.removeClass('error_field');
		cmb.removeProperty( 'disabled' );
	} else {
	
		this.addClass('error_field');
		
		errors_params = {'error_message':errorMessage};
		//showErrorDiv(errors_params);
		
	}
}

function manageErrorLoadListLocality(inputZipCode, displayError){
	var errorLoadListLocality = $('error_loadListLocality_' + inputZipCode.id);
	if(errorLoadListLocality){
		if(displayError){ 
			if(inputZipCode.next){
				inputZipCode.next.disabled = true;
				if(inputZipCode.next._error){
					inputZipCode.next._error.addClass('hidden');
				}
			}
			errorLoadListLocality.removeClass('hidden');
		} else {
			if(inputZipCode.next){
				inputZipCode.next.disabled = false;
				if(inputZipCode.next._error){
					inputZipCode.next._error.removeClass('hidden');
				}
			}
			errorLoadListLocality.addClass('hidden');
		}
		if(inputZipCode.section){ 
			inputZipCode.section.resize();
		} 
	}
}



//used to synchronize checkbox and html:hidden element
function setHidden(srcCheckbox, hiddenElement){
	if (srcCheckbox.checked) {
		hiddenElement.value = 1;
	} else {
		hiddenElement.value = 0;
	}
}

