/*JS*/

$(document).ready(function() {	
	
	disableCoAbo();
	if($('#forgotPass').length){ forgotPass(); }
	if($('#introTab').length){ introTabs(); }
	if($('#pushOffers').length){ pushOffers(); }
	//if($('form').length){ inputValue(); }
	if($('#glider').length){ glider(); }
	if($('#howTo').length){ howToModal(); }
	
	if($('#controls').length) {
		$('#controls li a').click( function(){
			var myAnchorLink = $(this).attr('href').split('#')[1];
			$('#glider').removeClass().addClass('bg_'+myAnchorLink);
			$('#'+myAnchorLink+' input:first').attr("checked","checked");
			$('#tabAboChoice').val(myAnchorLink);
		});
	}
	
	if($('#ifcoAbo').length) {
		
		
		$('#ifcoAbo').click( function(){
			
			if ($(this).is(":checked")){
				$('.ifOnlycoAbo').show();
			}else{
				$('.ifOnlycoAbo').hide();
			}
			
			
		});
		
	}
	
	/*functions for IE 6*/
	if ($.browser.msie && $.browser.version <= 6 ) {
		try {
		document.execCommand('BackgroundImageCache', false, true);
		} catch(e) {}		
	}	
	/*end functions for IE 6*/
});

function forgotPass() {
	$('#logBox a.forgotPass').click(function() {	
		$('#forgotPass').slideToggle('normal');
		return false;
	});	
}

function introTabs() {
	$tabLinks = $('#introTab > li > a');
	$tabBlocs = $('#mainContent .tabBloc');
	$tabLinks.removeClass('active').eq(0).addClass('active').addClass('firstTab'); 
	$tabBlocs.eq(0).show();
	
	$tabLinks.click(function() {	
		$tabBlocs.hide();
		$tabLinks.removeClass('active');
		
		var blocToShow = $(this).attr('href').split('#');
		$("#"+blocToShow[1]).show();
		$(this).addClass('active');
	
		return false;
	});	
}

function pushOffers() {
	//$('#pushOffers > ul > li').eq(0).addClass('firstPush');
	
	$('#pushOffers li a').click(function() {	 
		var offerToShow = $(this).attr('href').split('#');		
		$('#introTab , #mainContent .tabBloc , #pushOffers').hide();		
		$("#"+offerToShow[1]).show();	
		return false;	 
	});
	
	$('#offer_easy > a.closeModal , #offer_maxi > a.closeModal , #offer_premium > a.closeModal ').click(function() {
		$(this).parent('div.offerModals').hide();
		$('#introTab , #pushOffers').show();
		introTabs();
		return false;
	});	
}
function howToModal() {
	$('#howTo > ol a').click(function() {
		var howToShow = $(this).attr('href').split('#');
		$('#howTo > ol').hide();
		$("#"+howToShow[1]).show();	
		return false;	
	});

	$('#howTo .howToModals > a.closeHowToModal').click(function() {
		$(this).parent('div.howToModals').hide();
		$('#howTo > ol').show();
		return false;
	});	
	
	$('#howTo .howToModals a.nextHowTo').click(function() {
		$(this).parent('div.howToModals').hide().next('div.howToModals').show();
		return false;
	});
	
	$('#howTo .howToModals a.prevHowTo').click(function() {
		$(this).parent('div.howToModals').hide().prev('div.howToModals').show();
		return false;
	});
	
}

function inputValue() {
	$('input[type=text], input[type=password]').focus(function(){ 
		if($(this).val() == $(this).attr('defaultValue')){ $(this).val(''); }
	});
	$('input[type=text], input[type=password]').blur(function(){
		if($(this).val() == ''){ $(this).val($(this).attr('defaultValue')); }
	});
}

function submitForgotMail() {
	
	var xhr = getHTTPObject(); 	 	
 	if(xhr != null)
	{	
		//document.getElementById("ajaxLoader").style.display = "";
		form = document.getElementById("forgotPass");
		var id = document.getElementById('driverID_forgot_pwd').value;				
		var params = 'id=' + id;
		
	
		xhr.open("post","http://services.europcar.fr/autoliberte/ajax/oubli_mot_de_passe.php",false); 
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send(params);	
		
		var rvalue=xhr.responseText;	
		
		
		if(rvalue==0) 
		{
			$('#forgotPass').slideUp('normal', function() {
				//Function Ajax sendMail(php) et callback/response :
				$('#logBox p.unsend').fadeIn('normal', function(){
					setTimeout(function () { $('#logBox p.unsend').fadeOut('normal') }, 5000);
				});
			});
			return false;
		}
		else
		{

			$('#forgotPass').slideUp('normal', function() {
				//Function Ajax sendMail(php) et callback/response :
				$('#logBox p.send').fadeIn('normal', function(){
					setTimeout(function () { $('#logBox p.send').fadeOut('normal') }, 5000);
				});
			});
			return false;
		}		
	}
	
	return false;	
	
	
	
}

function glider() {
	var myUrl = document.location.toString();
	if (myUrl.match('#')) {
		var myAnchor = myUrl.split('#')[1];
		$('#controls li a[href="#' + myAnchor + '"]').click();
		$('#glider').removeClass().addClass('bg_'+myAnchor);
		$('#tabAboChoice').val(myAnchor);
		
		if (!$('#'+myAnchor+' input:second').is(":checked")){
			
			$('#'+myAnchor+' input:first').attr("checked","checked");
		}
		
		
	} else {
		var myAnchor = "section1";
		$('#controls li a:first').click();
		$('#tabAboChoice').val('section1');
		$('#'+myAnchor+' input:first').attr("checked","checked");
	}
}

function disableCoAbo() {
	if($('.fieldCoAboDisable').length) {
	$('#formLib fieldset.fieldCoAbo input , #formLib fieldset.fieldCoAbo select').attr("disabled","disabled");	
		
	}
}


//* =============================================================================================== */
/*									OBJET AJAX														*/
//* =============================================================================================== */
// retourne un objet xmlHttpRequest.
// m?thode compatible entre tous les navigateurs (IE/Firefox/Opera)
function getHTTPObject(){
  var xhr=null;
  if(window.XMLHttpRequest) // Firefox et autres
  xhr = new XMLHttpRequest();
  else if(window.ActiveXObject){ // Internet Explorer
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e1) {
        xhr = null;
      }
    }
  }
  else { // XMLHttpRequest non support? par le navigateur
    alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
  }
  return xhr;
}


function login_MonEspace()
{
 
 	var xhr = getHTTPObject(); 	 	
 	if(xhr != null)
	{	
		form = document.getElementById("formMonEspace");
		var DriverID = form.driverID.value;	
		var mdp = form.mdp.value;
			
		var params = 'login=' + encodeURIComponent(DriverID) + '&pswd=' + encodeURIComponent(mdp);
				

		xhr.open("post","http://services.europcar.fr/autoliberte/ajax/login.php",false); 
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send(params);	
		
		var rvalue=xhr.responseText;	

		
		if(rvalue==0) 
		{
			document.getElementById("msgRappel").style.display = "block";
			return false;
		}
		else
		{
			document.location.href=rvalue;
			return false;
		}		
	}
	
	return false;	
 }


