var garant = 'EQTC';
var numFieldSexe = new Array();
numFieldSexe[1]='H';
numFieldSexe[2]='';
numFieldSexe[3]='';
numFieldSexe[4]='';
numFieldSexe[5]='';
var ERR;
// execute your scripts when DOM is ready. this is a good habit
$(function() {		
	// initialize scrollable 
	$("div.scrollable").scrollable({
		size: 1,
		items: '#thumbs',  
		keyboard: false,
		hoverClass: 'hover'
	});	
	$("button[rel]").overlay();
	var api = $("#overlay").overlay({api:true}); 
    // define function that opens the overlay 
    window.openOverlay = function() { 
        api.load();
    } 

	var apibtn = $("#overlay").overlay({api:true}); 
    window.openOverlayBtn = function() {
    	CalculDevisLight();
        if(!ERR){
	        apibtn.load();
	        apibtn.onClose(function(){
	        	Ext.get("julie_tarif").hide();
	        	Ext.get("greenbtntarif").show();
			});
		}
		ERR=false;
    } ;

	var apitypes = $("#overlay_types").overlay({api:true}); 
    // define function that opens the overlay 
    window.openOverlayTypes = function() { 
        apitypes.load(); 
    };

	var em = $("#emailsettings").overlay({api:true,expose: '#6096D4'}); 
    // define function that opens the overlay 
    window.openEmail = function() {
    	
        em.load(); 
    } 
	
    // enable tooltip for "download" element. use the "slide" effect 
//    $("#download_now").tooltip();  
    $("#inforel").tooltip();
    $("#infobanc").tooltip();

});

function SetGarantie(val){
	garant = Ext.get(val).getValue();
}

function SetSexe(num,val){
	numFieldSexe[num] = val;
}

function goNext(){
	for(i=0;i<7;i++){
		if(Ext.get('li'+i).hasClass('active')){
			Ext.get('li'+i).removeClass("active");
			newid = i+1;
		}
	}
	Ext.get('li'+newid).addClass("active");	
	$("div.scrollable").scrollable({api: true}).next();
}
function  goTo(id,newid){
	oldid=0;
	for(i=0;i<7;i++)
			if(Ext.get('li'+i).hasClass('active')){
				oldid = i;
				Ext.get('li'+i).removeClass("active");
			}
	sw=0;
	Ext.get(id).addClass("active");
	if(newid < oldid) sw = newid- oldid;
	else sw = newid- oldid;
	$("div.scrollable").scrollable({api: true}).move(sw);
}

function CalculDevisLight(){
	nb_pers=0;
	ERR = false;
	for(i=1;i<=5;i++){
		if(Ext.get("row_"+i).isDisplayed()&&(Ext.get("module_prix_annee_"+i).getValue() != "")){
			nb_pers++;
			if(numFieldSexe[i] == ""){
				ERR=true;	
			}else calculDevis.setBaseParam("sexe_"+i,numFieldSexe[i]);
			calculDevis.setBaseParam("year_"+i,Ext.get("module_prix_annee_"+i).getValue());
			calculDevis.setBaseParam("people_"+i,1);
			dateToday = new Date();
			Year = dateToday.getFullYear();
			if(Ext.get("module_prix_annee_"+i).getValue() == ""){
				ERR=true;	
			}else if((Year - Ext.get("module_prix_annee_"+i).getValue()) > 59) {
				ERR=true;	
			}else if(((Year - Ext.get("module_prix_annee_"+i).getValue()) < 18) && (i == '1')) {
				ERR=true;	
			}		
		}else{
			calculDevis.setBaseParam("people_"+i,0);
			if(i==1) ERR=true;
		}
	}
//	alert("gerror:"+ERR);
	calculDevis.setBaseParam("garantie",garant);
	calculDevis.setBaseParam("formule","EQUITEO");
	calculDevis.setBaseParam("devisligth",1);
	if(!ERR){
		calculDevis.load({
			callback: function(){
				if(calculDevis.getAt(0).data.success){
					flashembed("leflash",
					{
						src:'camember.swf',
						width:700,
						height:520,//634
						align:'top'
					},{
					prix_eqtc:parseFloat(calculDevis.getAt(0).data.prixcomplet),
					garant:parseFloat(calculDevis.getAt(0).data.prix),
					num_pers:nb_pers
					});
				}
			}
		});
	}
}
var currSlide = 0;
var slideI;
var lShow = Array();

function startSlide( )
{
	slideI = setInterval( newSaviezVous, 5000 );
}

function newSaviezVous( )
{
	var dList = document.getElementById( "lesaviezvous" ).getElementsByTagName( "div" );

	if( dList.length < 2 )
	{
		clearInterval( slideI );
		return;
	}

	clearInterval( slideI );

	var i = 0;
	var total = 0;
	var swd = false;

	lShow = Array();

	while( i < dList.length )
	{
		if( dList[i].style.display == "" )
		{
			lShow.push( i );
			var opacityTween = new OpacityTween( dList[i],Tween.regularEaseIn, 100, 0, 1 );
			opacityTween.start();

			if( !swd )
				opacityTween.onMotionFinished = function(){ showSaviezVous(); };

			swd = true;
		}

		i++;
	}
}

function showSaviezVous( )
{
	var dList = document.getElementById( "lesaviezvous" ).getElementsByTagName( "div" );
	var total = dList.length;

	var i = 0;


	while( i < lShow.length )
	{
		dList[lShow[i]].style.display = "none";
		i++;
	}
	i = 0;

	if( total < 7 )
	{
		lShow = Array();
	}



	var tmpSTR = "";

	while( i < 2 )
	{
		var rs = getUniqueSV( lShow, total );
		lShow.push( rs );
		dList[rs].style.display = "";
		
		var opacityTween = new OpacityTween( dList[rs], Tween.regularEaseIn, 0, 100, 1 );
		opacityTween.start();

		i++;
	}
	
	slideI = setInterval( newSaviezVous, 5000 );
}

function getUniqueSV( lShow, total )
{
	var aw = 0;
	var rs = Math.floor( Math.random() * total );

	while( aw < lShow.length )
	{
		if( rs == lShow[aw] )
		{
			rs = getUniqueSV( lShow, total );
		}
		aw++;
	}
	return rs;
}

///////////////////////////////////////////////////////////////////////////
var currcheck;
var cLine = [];
var currcheckInt;

	
	function CheckLogin( ){
		ERR = false;
		try{
			xhr = new ActiveXObject('Msxml2.XMLHTTP');
		}catch (e) {
        	try{
				xhr = new ActiveXObject('Microsoft.XMLHTTP');
			}catch (e2){
				try{
					xhr = new XMLHttpRequest();
				}catch (e3){
					xhr = false;
				}
			}
		}
		xhr.onreadystatechange  = function(){ 
			if(xhr.readyState  == 4){
				if(xhr.status  == 200){
					var xmldoc = xhr.responseXML;
					var rList = xmldoc.getElementsByTagName('row');
					var i = 0;
					var invalid = 0;
					Ext.get("error_old").update('');
					while( i < rList.length ){
						if( rList[i].firstChild.data != 0 ){
							if( i == 0 ){
								if( rList[i].firstChild.data == 2 ){
									Ext.get("error_old").update('<IMG style="margin-right:5px;margin-left:25px;" src="./images/exclamation.png"/><span style="margin:0;color:red;">La 1ère personne doit impérativement <br/>être le souscripteur, majeur</span>');								
								}else{
									Ext.get("error_old").update('<IMG style="margin-right:5px;margin-left:25px;" src="./images/exclamation.png"/><span style="margin:0;color:red;">Vous devez avoir moins de 60 ans <br/>pour souscrire</span>');
								}
							}else{
								Ext.get("error_old").update('<IMG style="margin-right:5px;margin-left:25px;" src="./images/exclamation.png"/><span style="margin:0;color:red;">Vous devez avoir moins de 60 ans <br/>pour souscrire</span>');
							}
							invalid++;
							ERR = true;
						}
						i++;
					}

					var li = i;

					if( ( li < 5 ) && ( invalid == 0 ) ){
						// On affiche une ligne suppl�mentaire
						Ext.get("error_old").update('');

						i = 0;
						while( i < rList.length ){
							document.getElementById( "module_prix_annee_" + ( i + 1 ) ).style.backgroundImage = "url( images/home_input_noglow.png)";
							document.getElementById( "indicateur_" + ( i + 1 ) ).style.visibility = "hidden";
							i++;
						}
						ERR = false;

						document.getElementById( "indicateur_" + ( li + 1 ) ).style.visibility = "";
						document.getElementById( "home_ligne" ).rows[li].style.display ="";
					}
				}
			}
		};

		xhr.open( "POST", "annee_verif.xml.php",  true );
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");      
		
		var i = 1;
		var infos = "mva=1";
		while( i < 6 ){	
			if(!Ext.get("module_prix_annee_" + i).isVisible(true)) document.getElementById( "module_prix_annee_" + i ).value = "";
			infos += "&annee_" + i + "="+ document.getElementById( "module_prix_annee_" + i ).value + "&sexe_"+ i + "="+ document.getElementById( "module_prix_sexe_" + i ).value;
			i++;
		}
		xhr.send( infos );
	}
	
function sCheckLine( el )
{
	//sCheckLine
	currcheck = el;
	el.onblur = function(){ disableCheckLine(); }
	currcheckInt = setInterval( checkLineInterval, 1000 );
}

function disableCheckLine( )
{
	clearInterval( currcheckInt );
	checkLineInterval( );
}

function checkLineInterval( )
{
	var tmpval;
	tmpval = parseInt( currcheck.value );

	if( cLine[ currcheck.id.substr( 18, 19 ) ] )
		if( cLine[ currcheck.id.substr( 18, 19 ) ] == currcheck.value )
			return;

	if( ( ( tmpval + "" ).length == 4 ) && ( isNumeric( currcheck.value ) ) )
	{
		cLine[ currcheck.id.substr( 18, 19 ) ] = currcheck.value;

		if( document.getElementsByName( "module_prix_sexe_" + currcheck.id.substr( 18, 19 ) )[0].checked || document.getElementsByName( "module_prix_sexe_" + currcheck.id.substr( 18, 19 ) )[1].checked )
		{
			CheckLogin( );
		}
	}
	else if( currcheck.value.length == 0 )
	{

	}
	else
	{
	}
}

function checkLine( el )
{
	clearInterval( currcheckInt );

	var elem = document.getElementById( "module_prix_annee_" + el.id.substr( 17, 18 ) );
	var tmpval;

	tmpval = parseInt( elem.value );

/*
	if( cLine[ el.id.substr( 17, 18 ) ] )
		if( cLine[ el.id.substr( 17, 18 ) ] == elem.value )
			return;
*/

	if( ( ( tmpval + "" ).length == 4 ) && ( isNumeric( elem.value ) ) )
	{
		// REQUETE AJAX

		cLine[ el.id.substr( 17, 18 ) ] = elem.value;
		CheckLogin( );
	}
	else if( elem.value.length == 0 )
	{

	}
	else
	{
	}
}
function isNumeric(sText)
{ 
	var ValidChars = "0123456789."; 
	var IsNumber=true; 
	var Char; 
	for (i = 0; i < sText.length && IsNumber == true; i++)
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1)
		{ 
			IsNumber = false; 
		} 
	} 
	return IsNumber;
}

