function readyForPassword() {
  //var addr = document.getElementById('password');
  //addr.value = '';
  if(document.all){
      document.getElementById('text_password').style.setAttribute('cssText','display:none;');
      document.getElementById('crypted_password').style.setAttribute('cssText','color:#333333;border:1px solid #d4d4d4;margin-top:5px;width:180px;');
  }
  else{
      document.getElementById('text_password').setAttribute('style','display:none;');
      document.getElementById('crypted_password').setAttribute('style','color:#333333;border:1px solid #d4d4d4;margin-top:5px;width:180px;');
  }
}

function reset_input(){
	document.getElementById('newmail').value='';
	document.getElementById('newmail').setAttribute('style','color:black;');
}

function cgvread(cgv){
	if (document.cgvform.cgvbox.checked == 1) {
		return true
	}
	else {
		alert('Merci de confirmez que vous avez pris connaissance des conditions générales de ventes');
		return false
	}
	
}

function checkContactForm(email_id) {
  if($('name').value == ''){
    alert('Merci de renseigner votre nom');
    return false;
  }
  if(!checkEmail(email_id)){
    return false;
  }
  if($('subject').value == ''){
    alert('Merci de renseigner le sujet');
    return false;
  }
  if($('message').value == ''){
    alert('Votre E-Mail ne comporte pas de message !');
    return false;
  }
}  

function checkEstimateForm(){
  if (document.getElementById('first_name').value == '') {
    highlight('first_name');
    alert("Merci de saisir votre nom");
    return false;
  }
  if (document.getElementById('last_name').value == '') {
    highlight('last_name');
    alert("Merci de saisir votre nom");
    return false;
  }  
  if (document.getElementById('address').value == '') { 
    highlight('address');
    alert("Merci de préciser votre adresse");
    return false;
  }
  if (document.getElementById('zip').value == '') { 
    highlight('zip');
    alert("Merci de préciser votre code postal");
    return false;
  }
  if (document.getElementById('phone').value == '' && document.getElementById('mobile').value == '') { 
    highlight('phone');
    alert("Merci de préciser au moins un numéro de téléphone");
    return false;
  }
  if (checkEmail('email') == false){
    highlight('email');
    return false;
  }
  if (document.getElementById('subject').value == 0) {
    highlight('subject');
    alert("Merci de choisir le sujet de votre message");
    return false;
  }
  if (document.getElementById('building').value == 0) {
    highlight('building');
    alert("Merci de préciser le type de batiment");
    return false;
  }
  if (document.getElementById('year').value == 0) {
    highlight('year');
    alert("Merci de préciser l'ancienneté du batiment");
    return false;
  }
  if (document.getElementById('actual').value == 0) { 
    highlight('actual');
    alert("Merci de préciser le type d'équipement actuel");
    return false;
  }
}

function checkLogonForm(){
  //Check new customer creation form
  if (document.getElementById('first_name').value == '') {
    highlight('first_name');
    alert("Merci de saisir votre prénom");
    return false;
  }
  if (document.getElementById('last_name').value == '') {
    highlight('last_name');
    alert("Merci de saisir votre nom");
    return false;
  }
  if (document.getElementById('phone').value == ''){
    highlight('phone');
    alert('Merci de renseigner votre numéro de téléphone');
    return false;
  }
  if (checkEmail('login') == false){
    highlight('login');
    return false;
  }
  if (($F('password1') != $F('password2')) || $F('password1') == ''){
    highlight('password1');
    highlight('password2');
    alert('Les mots de passe ne correspondent pas, merci de vérifier')
    return false;
  }
  if (document.getElementById('billing_addr1').value == '') {
    highlight('billing_addr1');
    alert("Merci de saisir votre adresse");
    return false;
  }
  if (document.getElementById('billing_zip').value == '') {
    highlight('billing_zip');
    alert("Merci de saisir votre code postal");
    return false;
  }
  if (document.getElementById('billing_city').value == '') {
    highlight('billing_city');
    alert("Merci de saisir votre ville");
    return false;
  }
  if (document.getElementById('billing_country').value == '') {
    highlight('billing_country');
    alert("Merci de saisir votre pays");
    return false;
  }
}

function highlight(the_element) {
  //highlight selected input
  $(the_element).setStyle({border:'1px dotted red',backgroundColor:'#FFCA6F'}); 
}

function downlight(the_element) {
  //highlight selected input
  $(the_element).setStyle({border:'1px solid #d4d4d4',backgroundColor:'#fff'}); 
}

function sendPassword(){

  if(!$('log_mask')){
    log_mask = Builder.node("div",{id:'log_mask'});
    log_mask.setStyle({display:'none'});
    document.body.appendChild(log_mask);
  }
  $('log_mask').appear();
  
  var sendpassword_modal = new Element('div', {id : 'sendpassword_modal'});
  document.body.appendChild(sendpassword_modal);  
    
  new Ajax.Updater('sendpassword_modal','/sendpassword',{
    method:'get',
    parameters:{token:window._token}
  });  
}

function playMP3(){
  //Automatically add an mp3 player on .mp3 link targets
  var i = 1;
  $$('a').each(function(alink){
    ext = /[^.]+$/.exec(alink);
    if(ext == 'mp3'){
      alink.id = 'song_'+i;
      AudioPlayer.embed('song_'+i, {soundFile: alink.href});
      i++;
    }
  });
}

function readyForInput(id){
  $(id).value='';
  $(id).setStyle({color:'#2f2f2f'});
}

function checkNewComment(){
  if($F('name') == '') {
    alert(I18n['please_enter_your_name']);
    return false;
  }
  if(checkEmailIsValid('email') == '') {
    alert(I18n['please_enter_valid_email']);
    return false;
  }
  if($F('speech') == '') {
    alert(I18n['please_enter_your_comment']);
    return false;
  }
  if($F('captcha') == '') {
    alert(I18n['please_copy_antispam']);
    return false;
  }
  return true;
}

function pay(payment){
 
  if($('cgv').checked == true){
    if(payment == 'card'){
      $('detail_cards').toggle();
      return true;
    }
    if(payment == 'check'){
      $('detail_checks').toggle();
      return true;
    }
  } 
  else {
    alert("Vous devez accepter les conditions générales de ventes avant de valider votre commande");
  }  
}

function loginMember(){
  //Prompt for member login in a modal box
  new Ajax.Updater('logon_container', '/members/login',
  {
    method:'post',
    parameters:"token="+window._token,
    onSuccess: function(transport){
      var response = transport.responseText;
      $('log_mask').appear();
      $('logon_container').show();
    },
    onFailure: function(){ alert("An error occured while updating") }
  });

}

function checkModalLogin(email_id){
  if(!checkEmail(email_id)){
    return false;
  }
  if($('modal_password').value == ''){
    alert('Merci de renseigner votre mot de passe');
    return false;
  }

}

// Google Maps API Call
function load(map_id, map_params, map_locations){
  
  this_locations = map_locations.split('|');
  this_params = map_params.split('|');
  this_locations.pop();
  
  var map_geocoding = new google.maps.Geocoder();  

  // Position Map center
  map_geocoding.geocode({'address': this_params[1]}, function(results, status) {

    var map_center = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
    var optionsCarte = {
      zoom: eval(this_params[0]),
      center: map_center,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    this_map = new google.maps.Map(document.getElementById("map_"+map_id), optionsCarte);
  });    
    
  for(i=0; i<this_locations.length; i++){
    
    this_place = this_locations[i];
    map_geocoding.geocode({'address': this_locations[i]}, function(results, status) {

      var this_marker = new google.maps.Marker({
        map:this_map,
        draggable:true,
        animation: google.maps.Animation.DROP,
        position: new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng())
      });
  
      /*
      alert(results[0].address_components[0]);
      var content_string = '<span>'+this_place+'</span>';
      var infowindow = new google.maps.InfoWindow({
          content: content_string
      });

      google.maps.event.addListener(this_marker, 'click', function() {
        infowindow.open(this_map, this_marker);
      });
      */
    });    
  } 
  
}


