///////////////////////
// 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');
}

//////////////////////////////////////////////////////////////
// correctly handle PNG transparency in Win IE 5.5/6

function correctPNG() {
    for(var i=0; i<document.images.length; i++) {
        var img = document.images[i]
        var imgName = img.src.toUpperCase()
        if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
            img.outerHTML = strNewHTML
            i = i-1
        }
    }
}
if (document.all){
    window.attachEvent("onload", correctPNG);
}

function raiseSubMenu(id){
    if(document.all){
        document.getElementById('submenu_'+id).style.setAttribute('cssText','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.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 readyForSearch() {
    var pattern = document.getElementById('searchInput');
    if(pattern.value == 'Rechercher...'){
        pattern.value='';
        if(document.all){
            document.getElementById('searchInput').style.setAttribute('cssText','height:20px;color:#4d4d4d;font-size:14px;position:absolute;margin:10px auto auto 15px;width:180px;');
        }
        else{
            document.getElementById('searchInput').setAttribute('style','height:15px;color:#4d4d4d;font-size:12px;position:absolute;margin:7px auto auto 15px;width:180px;');
        }
    }
}

function readyForLogin() {
    var addr = document.getElementById('login');
    if(addr.value == 'Votre adresse E-Mail'){
        addr.value = '';
        if(document.all){
            document.getElementById('login').style.setAttribute('cssText','color:#333333 !important;font-size:12px;');
        }
        else{
            document.getElementById('login').setAttribute('style','color:#333333 !important;font-size:12px;');
        }
    }
}

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

function updateCartContent() {

  new Ajax.Updater('cart_content','/cart/count',{method:'get',parameters:{authenticity_token: window._token}});
  if (document.all) {
    document.getElementById('add_cart_anim_block').style.setAttribute('cssText','display:none');
    document.getElementById('add_cart_btn').style.setAttribute('cssText','display:block');
  }
  else {  
    document.getElementById('add_cart_anim_block').setAttribute('style','display:none');
    document.getElementById('add_cart_btn').setAttribute('style','display:block');
  }

}

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 updateShipping() {
    document.getElementById('s_addr1').value = document.getElementById('f_addr1').value;
    document.getElementById('s_addr2').value = document.getElementById('f_addr2').value;
    document.getElementById('s_zip').value = document.getElementById('f_zip').value;
    document.getElementById('s_city').value = document.getElementById('f_city').value;
    document.getElementById('s_phone').value = document.getElementById('f_phone').value;
}

function check_logon_form(){
  //Check new customer creation form
  if (checkEmail('email') == false){
    highlight('email');
    return false;
  }
  if (document.getElementById('password1').value == '') {
    highlight('password1');
    alert("Merci de choisir un mot de passe !");
    return false;
  }
  
  if (document.getElementById('password1').value != document.getElementById('password2').value) {
    highlight('password1');
    highlight('password2');
    alert("Vos mots de passe ne correspondent pas, merci de vérifier");
    return false;
  }
  
  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('f_addr1').value == '') {
    highlight('f_addr1');
    alert("Merci de saisir votre adresse de facturation");
    return false;
  }
  if (document.getElementById('f_zip').value == '') {
    highlight('f_zip');
    alert("Merci de saisir votre code postal de facturation");
    return false;
  }
  if (document.getElementById('f_city').value == '') {
    highlight('f_city');
    alert("Merci de saisir votre ville de facturation");
    return false;
  }
  
  if (document.getElementById('s_addr1').value == '') {
    highlight('s_addr1');
    alert("Merci de saisir votre adresse de livraison");
    return false;
  }
  if (document.getElementById('s_zip').value == '') {
    highlight('s_zip');
    alert("Merci de saisir votre code postal de livraison");
    return false;
  }
  if (document.getElementById('s_city').value == '') {
    highlight('s_city');
    alert("Merci de saisir votre ville de livraison");
    return false;
  }

}

function showCartAnim() {
  //animate cart img
  if (document.all) {
    document.getElementById('add_cart_anim_block').style.setAttribute('cssText','display:block');
    document.getElementById('add_cart_btn').style.setAttribute('cssText','display:none');
  }
  else {
    document.getElementById('add_cart_anim_block').setAttribute('style','display:block');
    document.getElementById('add_cart_btn').setAttribute('style','display:none');
  }  
  
  var letters = document.getElementById('markup').value;
  var numbers = parseInt(document.getElementById('markup').value);
  
  if (letters == "") {
    //is empty
    alert("Vous n'avez pas choisi de marquage !");
    return false;
  }

  if (letters.match(/[a-zA-Z]/) != null || (numbers > 0 && numbers < 32)) {
    //Do something...
  }
  else {
    alert("Merci de choisir une lettre ou un chiffre entre 1 et 31");
    return false;
  }
  document.getElementById('markup').value = "";
  return true;
}

function check_password_form() {
  if (document.getElementById('password1').value == '') {
    highlight('password1');
    alert("Merci de choisir un mot de passe !");
    return false;
  }
  
  if (document.getElementById('password1').value != document.getElementById('password2').value) {
    highlight('password1');
    highlight('password2');
    alert("Vos mots de passe ne correspondent pas, merci de vérifier");
    return false;
  }
}

function check_logon_form_2(){

  //Check new customer creation form
  if (checkEmail('customer_email') == false){
    highlight('customer_email');
    return false;
  }  
  if (document.getElementById('customer_firstname').value == '') {
    highlight('customer_firstname');
    alert("Merci de saisir votre prénom");
    return false;
  }
  if (document.getElementById('customer_name').value == '') {
    highlight('customer_name');
    alert("Merci de saisir votre nom");
    return false;
  }
  if (document.getElementById('customer_f_addr1').value == '') {
    highlight('customer_f_addr1');
    alert("Merci de saisir votre adresse de facturation");
    return false;
  }
  if (document.getElementById('customer_f_zip').value == '') {
    highlight('customer_f_zip');
    alert("Merci de saisir votre code postal de facturation");
    return false;
  }
  if (document.getElementById('customer_f_city').value == '') {
    highlight('customer_f_city');
    alert("Merci de saisir votre ville de facturation");
    return false;
  }
  
  if (document.getElementById('customer_s_addr1').value == '') {
    highlight('customer_s_addr1');
    alert("Merci de saisir votre adresse de livraison");
    return false;
  }
  if (document.getElementById('customer_s_zip').value == '') {
    highlight('customer_s_zip');
    alert("Merci de saisir votre code postal de livraison");
    return false;
  }
  if (document.getElementById('customer_s_city').value == '') {
    highlight('customer_s_city');
    alert("Merci de saisir votre ville de livraison");
    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;
  }*/

}

