///////////////////////
// Pop-up a new window, given its URL
function popup(url,name){
    window.open(url,name,config='height=500,width=600,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');
}

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 checkEmail(id) {
  var addr = document.getElementById(id).value;

  var place = addr.indexOf("@",1);
  var point = addr.indexOf(".",place+1);
  if ((place > -1)&&(addr.length >2)&&(point > 1)) {
      return(true);
  }
  else  {
      alert('Votre adresse E-Mail est invalide !');
      return(false);
  }
  return false;
}


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 countCartContent(){  
  //alert('hu');
  new Ajax.Updater('cart_content','/cart/count',{method:'get',parameters:{authenticity_token: window._token}});
}

function updateCartContent(id) {
  if(document.getElementById(id).value == '') {
    alert('Merci de choisir une quantité !');
    return false;
  }
  new Ajax.Updater('cart_content','/cart/count',{method:'get',parameters:{authenticity_token: window._token}});
  showPopWin('/cart', 700, 500, hidePopWin); 
  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 start_slide(pics,id,site,size){
  //image 0 is already displayed by default
  var a = 1;
  new PeriodicalExecuter(function(){  
    new Effect.Fade(id, { 
      duration: 1, 
      fps: 50,
      afterFinish: function() {    
        var pic_url =  "/thumbnailer/"+pics[a]+"/"+size;
        //alert(pic_url);
        $(id).src = pic_url;
        (a < pics.length-1) ? a++ : a = 0;    
        new Effect.Appear(id, {
          duration: 1,
          fps: 50,
          queue:'end'
        })   
      }
    })       
  }, 5);
  return true
}

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++;
    }
  });
}

