///////////////////////
// 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 raiseSubMenu(id){
    if(document.all){
        document.getElementById('submenu_'+id).style.setAttribute('cssText','display:block;list-style-type:none;padding-bottom:0px;');
        var links = document.getElementById('submenu_'+id).childNodes;
        /*for(var i=0;i<links.length;i++) {
            links[i].firstChild.style.setAttribute('cssText','color:#4d4d4d;font-size:15px;margin:0;padding-right:20px;height:100%;display:block;text-align:right;');
            links[i].style.setAttribute('cssText','color:#fff;border:0;margin:0;');
        }*/
    }
    else{
        document.getElementById('submenu_'+id).setAttribute('style','display:block;list-style-type:none;padding-bottom:10px;');
        var links = document.getElementById('submenu_'+id).childNodes;
        /*for(var i=0;i<links.length;i++) {
            links[i].firstChild.setAttribute('style','color:#4d4d4d;font-size:15px;margin:0;padding-right:10px;display:block;height:100%;');
            links[i].setAttribute('style','color:#4d4d4d;border:0;margin:0;');
        }*/
    }
}

function hideSubMenu(id){
    if(document.all){
        document.getElementById('submenu_'+id).style.setAttribute('cssText','display:none;');
    }
    else{
        document.getElementById('submenu_'+id).setAttribute('style','display:none;');
    }
}

function hideSubMenuIE(id){
    if(document.all){
        document.getElementById('submenu_'+id).style.setAttribute('cssText','display:none;');
    }
}

function validatePassword() {
	if (document.getElementById("password1").value == document.getElementById("password2").value) {
		return true;
	} else {
		alert("Les mots de passes ne sont pas identiques")
		return false;
	}
}

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 checkQty(id) {
  if(document.getElementById(id).value == '') {
    alert('Merci de choisir une quantité !');
    return false;
  }
  else {
    return true;
  }
}

function updateCartContent(id) {
  new Ajax.Updater('cart_content','/cart/count',{method:'get',parameters:{authenticity_token: window._token}});
  showPopWin('/cart', 700, 500, hidePopWin); 
  return false;
}

function check_contact_form(){
  if (checkEmail('email') == false){
    return false;
  }
  if (document.getElementById('name').value == '') {
    alert("Merci de saisir votre nom");
    return false;
  }
  
  /*
  if (document.getElementById('password1').value != document.getElementById('password2').value) {
    alert("Vos mots de passe ne correspondent pas, merci de vérifier");
    return false;
  }*/
  
  if (document.getElementById('subject').value == '') {
    alert("Merci de saisir le sujet de votre message");
    return false;
  }
  if (document.getElementById('message').value == '') {
    alert("Vous n'avez pas saisi de message !");
    return false;
  }
  /*
  if (document.getElementById('addr1').value == '') {
    alert("Merci de saisir votre adresse");
    return false;
  }
  if (document.getElementById('zip').value == '') {
    alert("Merci de saisir votre code postal");
    return false;
  }
  if (document.getElementById('city').value == '') {
    alert("Merci de saisir votre ville");
    return false;
  }*/

}

function check_logon_form(){
  //Check new customer creation form
  if (document.getElementById('firstname').value == '') {
    highlight('firstname');
    alert("Merci de saisir votre prénom");
    return false;
  }
  if (document.getElementById('name').value == '') {
    highlight('name');
    alert("Merci de saisir votre nom");
    return false;
  }
  if (document.getElementById('addr1').value == '') {
    highlight('addr1');
    alert("Merci de saisir votre adresse");
    return false;
  }
  if (document.getElementById('zip').value == '') {
    highlight('zip');
    alert("Merci de saisir votre code postal");
    return false;
  }
  deserved_arrs = ['69001','69002','69004','69005','69006','69009'];
  var arr_flag = false;
  for (var i = 0; i < 6; i++) {
    if(document.getElementById('zip').value == deserved_arrs[i]){
      arr_flag = true;
    }
  }
  
  if (arr_flag == false) {
    highlight('zip');
    alert("Désolé, mais nous ne livrons que Lyon centre, dans les arrondissements suivants :\n\n 1er, 2eme, 4eme, 5eme, 6eme et 9eme");
    return false;
  }

  if (document.getElementById('city').value == '') {
    highlight('city');
    alert("Merci de saisir votre ville");
    return false;
  }
  if (document.getElementById('phone').value == ''){
    highlight('phone');
    alert('Merci de renseigner votre numéro de téléphone');
    return false;
  }
  if (checkEmail('email') == false){
    highlight('email');
    return false;
  }
  if (document.getElementById('toship_false').value == 'false' && document.getElementById('toship_true').value == 'false'){
    alert("Préferez vous la livraison à domicile ou bien le retrait sur place ?");
    return false;
  }
  return false;
}

function highlight(the_element) {
  //highlight selected input
  if(document.all){
      document.getElementById(the_element).style.setAttribute('cssText','border:1px solid red');
  }
  else{
      document.getElementById(the_element).setAttribute('style','border:1px solid red');
  }
}

function downlight(the_element) {
  //highlight selected input
  if(document.all){
      document.getElementById(the_element).style.setAttribute('cssText','border:1px solid #d4d4d4');
  }
  else{
      document.getElementById(the_element).setAttribute('style','border:1px solid #d4d4d4');
  }
}

function actionBlock(id,y){  
  if(document.all){
      document.getElementById(id).style.setAttribute('cssText','margin-top:'+y+'px');
  }
  else{
    document.getElementById(id).setAttribute('style','margin-top:'+y+'px');
  }
}


