/* Fonctions génériques */
function Right(str, n){
  if (n <= 0)
    return "";
  else if (n > String(str).length)
    return str;
  else {
    var iLen = String(str).length;
    return String(str).substring(iLen, iLen - n);
  }
}

function switchBox(box) {
  var form = document.getElementById(box);
  var afficheSrc = form.currentStyle || document.defaultView.getComputedStyle(form, null);

  if (navigator.appName == 'Microsoft Internet Explorer') {
    var afficheIE = afficheSrc.display;
    if(afficheIE == 'none') {
      form.style.display = 'block'
    };
    if(afficheIE == 'block') {
      form.style.display = 'none'
    };
  }
  else {
    var afficheMoz = afficheSrc.getPropertyValue('display');
    if(afficheMoz == 'none') {
      form.style.display = 'block';
    };
    if(afficheMoz == 'block') {
      form.style.display = 'none';
    };
  }
}

/************* TOOLBOX ******************/

function openToolbox() {
  jQuery(document).ready(function($){
    $(".toolbox").animate({
      marginTop: "191px"
    }, "slow");
    $("#clickerOpen").hide();
    $("#clickerClose").show();
  });
}

function generateLink(url,sku) {

  if(sku == 'PULL1') {
    var choixAvantIndex = document.getElementById('choix_Avant').selectedIndex;
    var choixAvant =  document.getElementById('choix_Avant').options[choixAvantIndex].text;
    var choixDosIndex = document.getElementById('choix_Dos').selectedIndex;
    var choixDos =  document.getElementById('choix_Dos').options[choixDosIndex].text;
    var choixManchesIndex = document.getElementById('choix_Manches').selectedIndex;
    var choixManches =  document.getElementById('choix_Manches').options[choixManchesIndex].text;
    var choixFinitionsIndex = document.getElementById('choix_Finitions').selectedIndex;
    var choixFinitions =  document.getElementById('choix_Finitions').options[choixFinitionsIndex].text;
    var choixColIndex = document.getElementById('choix_Col').selectedIndex;
    var choixCol =  document.getElementById('choix_Col').options[choixColIndex].text;

    var link = url+'pull-personnalisable.html'+'?Avant='+choixAvant+'&Dos='+choixDos+'&Manches='+choixManches+'&Finitions='+choixFinitions+'&Col='+choixCol;

    if(document.getElementById('choix_Broderie').value != '' && document.getElementById('choix_Emplacement').selectedIndex != 0 && document.getElementById('choix_Couleur').selectedIndex != 0 && document.getElementById('choix_Police').selectedIndex != 0) {
      var choixBroderie = document.getElementById('choix_Broderie').value;
      var choixEmplacementIndex = document.getElementById('choix_Emplacement').selectedIndex;
      var choixEmplacement =  document.getElementById('choix_Emplacement').options[choixEmplacementIndex].text;
      var choixCouleurIndex = document.getElementById('choix_Couleur').selectedIndex;
      var choixCouleur =  document.getElementById('choix_Couleur').options[choixCouleurIndex].text;
      var choixPoliceIndex = document.getElementById('choix_Police').selectedIndex;
      var choixPolice =  document.getElementById('choix_Police').options[choixPoliceIndex].text;
      link = link + '&Broderie=' + choixBroderie + '&Emplacement=' + choixEmplacement + '&Couleur=' + choixCouleur + '&Police=' + choixPolice;
    }
  }

  if(sku == 'ECHA1') {
    choixAvantIndex = document.getElementById('choix_Avant').selectedIndex;
    choixAvant =  document.getElementById('choix_Avant').options[choixAvantIndex].text;
    choixDosIndex = document.getElementById('choix_Dos').selectedIndex;
    choixDos =  document.getElementById('choix_Dos').options[choixDosIndex].text;
    link = url+'echarpe-double-face.html'+'?Avant='+choixAvant+'&Dos='+choixDos;
  }

  if(sku == 'BONNET1') {
    choixFondIndex = document.getElementById('choix_Fond').selectedIndex;
    choixFond =  document.getElementById('choix_Fond').options[choixFondIndex].text;
    choixMilieuIndex = document.getElementById('choix_Milieu').selectedIndex;
    choixMilieu =  document.getElementById('choix_Milieu').options[choixMilieuIndex].text;
    choixBasIndex = document.getElementById('choix_Bas').selectedIndex;
    choixBas =  document.getElementById('choix_Bas').options[choixBasIndex].text;
    link = url+'bonnet-coton-bio-personnalise.html'+'?Fond='+choixFond+'&Milieu='+choixMilieu+'&Bas='+choixBas;
  }


  return link;
}

function generatePermalien(url, sku) {

  permalien = generateLink(url,sku);

  document.getElementById('webLinkContent').innerHTML = '<input type="text" id="webLinkContentInput" value="'+permalien+'" readonly="readonly"/>';
  document.getElementById('webLinkContentInput').select();
  document.getElementById('webLinkActivate').style.display = 'none';

}

function resetPermalien() {
  document.getElementById('webLinkContent').innerHTML = '';
  document.getElementById('webLinkActivate').style.display = 'block';

}

function postFacebook(url,sku) {
  link = generateLink(url,sku);
  document.getElementById('facebookContent').innerHTML = '<a target="_blank" href="http://www.facebook.com/sharer.php?u='+link+'&t='+'Mon Seyes'+'">Partager cette création</a>';

}
function postTwitter(url,sku) {
  link = generateLink(url,sku);
  ajaxTwitter(link,'twitterContent');

}

function updateToolbox(url,sku) {
  resetPermalien();
  postFacebook(url,sku);
  postTwitter(url,sku);
}

/* Produits complémentaires */
function gotoEcharpeComplementaire(url,sku) {
  if(sku == 'PULL1') {
    var choixAvantIndex = document.getElementById('choix_Avant').selectedIndex;
    var choixAvant =  document.getElementById('choix_Avant').options[choixAvantIndex].text;
    var choixDosIndex = document.getElementById('choix_Dos').selectedIndex;
    var choixDos =  document.getElementById('choix_Dos').options[choixDosIndex].text;
    var choixFinitionsIndex = document.getElementById('choix_Finitions').selectedIndex;
    var choixFinitions =  document.getElementById('choix_Finitions').options[choixFinitionsIndex].text;

    if(choixAvant == choixDos) {
      choixDos = choixFinitions;
    }

    window.location.href = url+'echarpe-double-face.html'+'?Avant='+choixAvant+'&Dos='+choixDos;
  }
  if(sku == 'BONNET1') {
    var choixFondIndex = document.getElementById('choix_Fond').selectedIndex;
    var choixFond =  document.getElementById('choix_Fond').options[choixFondIndex].text;
    var choixMilieuIndex = document.getElementById('choix_Milieu').selectedIndex;
    var choixMilieu =  document.getElementById('choix_Milieu').options[choixMilieuIndex].text;

    window.location.href = url+'echarpe-double-face.html'+'?Avant='+choixFond+'&Dos='+choixMilieu;

  }
}

function gotoPullComplementaire(url) {
  var choixAvantIndex = document.getElementById('choix_Avant').selectedIndex;
  var choixAvant =  document.getElementById('choix_Avant').options[choixAvantIndex].text;
  var choixDosIndex = document.getElementById('choix_Dos').selectedIndex;
  var choixDos =  document.getElementById('choix_Dos').options[choixDosIndex].text;

  window.location.href = url+'pull-personnalisable.html'+'?Avant='+choixAvant+'&Dos='+choixAvant+'&Manches='+choixAvant+'&Finitions='+'Blanc'+'&Col=V';
}

function closeBox(idBlock) {
  document.getElementById(idBlock).style.display = 'none';
}

/**/

function redirectEmptyProduct(currentUrl,finUrl) {
  if(finUrl == "html/" || finUrl == ".html") {
    if(currentUrl.search(/bonnet/i) != -1) {
      window.location = "?Fond=Noir&Milieu=Noir&Bas=Noir&/";
    }
    if(currentUrl.search(/echarpe/i) != -1) {
      window.location = "?Avant=Noir&Dos=Noir&/";
    }
    if(currentUrl.search(/pull/i) != -1) {
      window.location = "?Avant=Noir&Dos=Noir&Manches=Noir&Finitions=Noir&Col=V&/";
    }
    if(currentUrl.search(/ensemble-laine/i) != -1) {
      window.location = "?Echarpe=Longue&Cadeau=Bonnet&Complement=Aucun&Pochette=Sans&Livraison=Standard&/";
    }
  }
}

function activateAddtocart(requiredOptions) {
  var nbreOptions = requiredOptions.length;
  var elementsActifs = 0;
  var active = 0;
  for(i = 0; i < requiredOptions.length; i++) {
    var validation = 'choix_'+ requiredOptions[i];
    var selection = document.getElementById(validation).selectedIndex;
    if(selection == null) {
      active = document.getElementById(validation).value;
    }
    else {
      active = document.getElementById(validation).selectedIndex;
    }
    if(active) {
      elementsActifs = elementsActifs+1;
    }
  }

  /* CONCOURS - validation */
  var email = document.getElementById('choix_email');
  if(email != null) {
    email = document.getElementById('choix_email').value;
    nbreOptions = nbreOptions*1;
    nbreOptions = nbreOptions + 1;
    if(email != 'Votre email...') {
      validation = 1
    } /*cond 1 */
    if(email.indexOf('@') != null) {
      var email_array = email.split('@');
      if(email_array.length == 2) {
        if(email_array[0].length > 0) {
          validation = validation +1
        } /*cond 2 */
        if(email_array[1].indexOf('.')) {
          var domain_array = email_array[1].split('.');
          if(domain_array.length == 2) {
            validation = validation +1
          }
        }
      }
    } /*cond 3 */
    if(validation == '3') {
      elementsActifs = elementsActifs + 1
    }
  }

  /* Fin concours */

  if(elementsActifs == requiredOptions.length){
    document.getElementById('bouton-addtocart').className = 'seyes-addtocart-button';
    document.getElementById('bouton-addtocart-deactivated').className = 'seyes-addtocart-button-off';
  }
  else {
    document.getElementById('bouton-addtocart').className = 'seyes-addtocart-button-off';
    document.getElementById('bouton-addtocart-deactivated').className = 'seyes-addtocart-button-deactivated';
  }
}

function afficheZone(zone) {
  var zoneSelected = 'zone'+zone+'-content';
  var navSelected = 'zone'+zone+'-nav';
  for(i=1; i<6; i++) {
    var zoneEncours = 'zone'+i+'-content';
    var navEncours = 'zone'+i+'-nav';
    document.getElementById(zoneEncours).className = 'zone-off';
    document.getElementById(navEncours).className = 'nav-zone-off';
  }
  document.getElementById(zoneSelected).className = 'zone-on';
  document.getElementById(navSelected).className = 'nav-zone-on';
}

function flashRondFiliere(swf, lien)
{
  document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="200" height="200" border="0">');
  document.write('<param name="movie" value="'+swf+'">');
  document.write('<param name="quality" value="high">');
  document.write('<param name="FlashVars" value="lien='+lien+'">');
  document.write('<embed src="'+swf+'" width="200" height="200" border="0" quality="high" FlashVars="lien='+lien+'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed></object>');
}

/************** MOTEUR ****************/

function changeZone(optionsItem,activeZone) {
  for (i = 0; i < optionsItem.length; i++) {
    var titreToHide = 'titre_' + optionsItem[i];
    var blocToHide = 'bloc_' + optionsItem[i];
    document.getElementById(titreToHide).className = 'titre-off';
    document.getElementById(blocToHide).style.display = 'none';
  }
  var titreToShow = 'titre_' + activeZone;
  var blocToShow = 'bloc_' + activeZone;
  document.getElementById(titreToShow).className = 'titre-on';
  document.getElementById(blocToShow).style.display = 'block';

  /*Affiche les blocs emplacement + couleur + police si broderie sélectionnée*/
  if(blocToShow == 'bloc_Broderie') {
    document.getElementById('bloc_Emplacement').style.display = 'block';
    document.getElementById('bloc_Couleur').style.display = 'block';
    document.getElementById('bloc_Police').style.display = 'block';
  }

  /* Afficher la taille */
  if(document.getElementById('bloc_Taille') != null) {
    document.getElementById('bloc_Taille').style.display = 'block';
  }
  
  /* Cache le titre emplacement + couleur + police */
  document.getElementById('titre_Emplacement').style.display = 'none';
  document.getElementById('titre_Couleur').style.display = 'none';
  document.getElementById('titre_Police').style.display = 'none';
}

function changeSelect(option,index) {
  var selectTochange = 'choix_' +option;
  document.getElementById(selectTochange).selectedIndex = index;
}

function changeSelectedValue(valuesList,activeOption,activeValue) {
  for (i = 0; i < valuesList.length; i++) {
    var valueToHide = 'select_' + activeOption + valuesList[i];
    document.getElementById(valueToHide).className = 'option-off';
  }
  var valueToShow = 'select_' + activeOption + activeValue;
  document.getElementById(valueToShow).className = 'option-on';
}

function updateText(option,text) {
  var textTochange = 'choix_' +option;
  document.getElementById(textTochange).value = text;
}

function showValeur(valeur) {
  valeurSelected = valeur;
  document.getElementById(valeurSelected).className = 'valeur-on';
}

function hideValeur(valeur) {
  valeurSelected = valeur;
  document.getElementById(valeurSelected).className = 'valeur-off';
}

function updateZoom(optionsItem, swf, sku, url) {
  var optionElement = new Array();
  var optionIndex = new Array();
  var optionValue = new Array();
  var flashVar = new Array();

  for (j = 0;j < optionsItem.length; j++) {
    optionElement[j] = 'choix_'+optionsItem[j];
    optionIndex[j] = document.getElementById(optionElement[j]).selectedIndex;
  }

  for (m = 0;m < optionsItem.length;m++) {
    if(optionIndex[m] == null) {
      optionValue[m] = document.getElementById(optionElement[m]).value;
    }
    else {
      optionValue[m] =  document.getElementById(optionElement[m]).options[optionIndex[m]].text;
    }
  }

  /*Affiche le masque de l'emplacement broderie si le texte est vide*/
  if(document.getElementById('choix_Broderie') != null) {
    if(document.getElementById('choix_Broderie').value == '') {
      document.getElementById('masque_Emplacement').style.display = 'block';
    }
    else {
      document.getElementById('masque_Emplacement').style.display = 'none';
    }

    /*Affiche le masque couleur broderie si le texte est vide et l'emplacement non choisi*/
    if(document.getElementById('choix_Broderie').value != '' && document.getElementById('choix_Emplacement').value != '') {
      document.getElementById('masque_Couleur').style.display = 'none';
    }
    else {
      document.getElementById('masque_Couleur').style.display = 'block';
    }

    /*Affiche le masque police broderie si le texte est vide et l'emplacement non choisi et la couleur non choisie*/
    if(document.getElementById('choix_Broderie').value != '' && document.getElementById('choix_Emplacement').value != '' && document.getElementById('choix_Couleur').value != '') {
      document.getElementById('masque_Police').style.display = 'none';
    }
    else {
      document.getElementById('masque_Police').style.display = 'block';
    }

  }

  for (i = 0;i < optionsItem.length;i++)  {
    flashVar[i] = optionElement[i]+'='+optionValue[i];
  /*document.write("<LI>" + flashVar[i] + "\n")*/
  }

  var flashString = 'param name="flashvars" value="';
  for(k = 0;k < flashVar.length; k++) {
    if(k == flashVar.length - 1) {
      flashString = flashString + flashVar[k] + '&sku=' + sku+'"';
    }
    else {
      flashString = flashString + flashVar[k]+'&';
    }
  }

  var flashStringBis = 'FlashVars="';
  for(k = 0;k < flashVar.length; k++) {
    if(k == flashVar.length - 1) {
      flashStringBis = flashStringBis + flashVar[k]+ '&sku=' + sku+'"';
    }
    else {
      flashStringBis = flashStringBis + flashVar[k]+'&';
    }
  }

  var zonezoom = document.getElementById('zone-zoom');
  while (zonezoom.childNodes[0]) {
    zonezoom.removeChild(zonezoom.childNodes[0]);
  }
  document.getElementById('zone-zoom').innerHTML = '<object id="zoom" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="530" height="530" style="z-index:1"> <param name="movie" value="'+swf+'"> <param name="quality" value="high"> <'+flashString+'> <param name="wmode" value="transparent" /><embed src="'+swf+'" width="530" height="530" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent" '+flashStringBis+'></embed></object>';

  updateToolbox(url,sku) // On met à jour la toolbox

}

function zoom(swf) {
  document.write('<object id="zoom" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="530" height="530" style="z-index:1" >');
  document.write('<param name="movie" value="'+swf+'">');
  document.write('<param name="quality" value="high">');
  document.write('<param name="wmode" value="transparent" />');
  document.write('<embed src="'+swf+'" width="530" height="530" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent"></embed></object>');
}

/********** CART *************/
function validateModify(url,id) {
  document.getElementById('validateModify').style.display = 'block';
  document.getElementById('validateModifyLink').innerHTML = '<a href="' + url + 'checkout/cart/modify?id=' + id + '">Oui, je veux modifier ma création</a>';
}