/*** Init ***/
function onloadpage(pNavBool) {
  if (arguments.callee.done) return;
  arguments.callee.done = true;
  pageProduitName = document.getElementById('popups').innerHTML;
  createPopup();
  document.getElementById('popups').innerHTML = popup;
  document.getElementById('content_popup').innerHTML = popup_form;
  changeClassPopupBlock(true);
  if (pNavBool) document.getElementById('popup_block').style.marginTop = '0px';
  else document.getElementById('popup_block').style.marginTop = ((document.getElementById('popup_block').offsetHeight / 2) * -1) + "px";
  document.getElementById(tabIdInput[5]).addEventListener("paste", pasteIntercept, false);
}

/*** Formulaire ***/
var tabIdInput = new Array("nom1", "prenom1", "prenom2", "mail1", "mail2", "message");
var errorEmpty = "Veuillez remplir tous les champs *";
var errorMail = "Veuillez donner une adresse mail valide";

function checkInfo(pObjForm) {
  var error = false;
  
  // Test champs e-mail
  if (validate_email(tabIdInput[3])) {
    document.getElementById("errorForm").innerHTML = errorMail;
    error = true;
  }
  if (validate_email(tabIdInput[4])) {
    document.getElementById("errorForm").innerHTML = errorMail;
    error = true;
  }
  for (var i = 0; i < tabIdInput.length; i++) {
    if (document.getElementById(tabIdInput[i]).value == "") {
      document.getElementById("errorForm").innerHTML = errorEmpty;
      error = true;
    }
  }
  
  if (error) document.getElementById('errorForm').style.display = "block";
  else sendMail();
}

function validate_email(email) {
  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  var address = document.getElementById(email).value;
  if(reg.test(address) == false) {
    //alert('Adresse E-mail invalide');
    return true;
  }
}

function pasteIntercept(evt) {
  document.getElementById(tabIdInput[5]).value = message_old;
}

function limiteChar(textarea) {
  var n = 0;
  var sub = 1;
  var max = 200;
  
  if (textarea.value.length >= max) textarea.value = textarea.value.substring(0, max);
  var reste = max - textarea.value.length;
  for (var i = 0; i < textarea.value.length; i++) {
    if (textarea.value.substring(i, (i+1)) == "\n") n += 39;
  }
  reste -= n;
  if (reste < 0) {
    if (textarea.value.substring((textarea.value.length-1), textarea.value.length) == "\n") sub = 2;
    textarea.value = textarea.value.substring(0, (textarea.value.length - sub));
    return;
  }
  var affichage_reste =  reste+' caract&egrave;res restants';
  document.getElementById('messageCpt').innerHTML = '('+affichage_reste+')';
  message_old = textarea.value;
}

function raz() {
  hideDiv();
  document.getElementById('content_popup').innerHTML = popup_form;
  eraseInfo();
  changeClassPopupBlock(true);
}

function eraseInfo() {
  for (var i = 0; i < tabIdInput.length; i++) {
    document.getElementById(tabIdInput[i]).value = "";
  }
}

/*** Popup ***/
function createPopup() {
  popup_form += '<div onclick="javascript:hideDiv();" class="cntrl"></div>';
  popup_form += '<h3>Recommander ce produit &agrave; un(e) ami(e)</h3>';
  popup_form += '<div class="photo" style="background-image: url('+_URL_RACINE+''+_URL_IMG+'/'+_NAME_FILE_IMG+''+pageProduitName+'.jpg);"></div>';
  popup_form += '<div id="errorForm" class="error"></div>';
  popup_form += '<div><input type="button" onclick="checkInfo(this.form);" class="btn_send" /></div>';
  popup_form += '<div class="form">';
    popup_form += '<h1>De la part de :</h1>';
    popup_form += '<div class="cellule1">Pr&eacute;nom <span class="champ_oblg">*</span></div>';
    popup_form += '<div class="cellule2"><input type="text" id="'+tabIdInput[1]+'" name="send_firstname" maxlength="25" size="20" /></div>';
    popup_form += '<div class="cellule1">Nom <span class="champ_oblg">*</span></div>';
    popup_form += '<div class="cellule2"><input type="text" id="'+tabIdInput[0]+'" name="send_lastname" maxlength="25" size="20" /></div>';
    popup_form += '<div class="cellule1">E-mail <span class="champ_oblg">*</span></div>';
    popup_form += '<div class="cellule2"><input type="text" id="'+tabIdInput[3]+'" name="send_mail" size="20" /></div>';
    popup_form += '<div style="clear: both;">&nbsp;</div>';
    popup_form += '<h1>Envoyer &agrave; :</h1>';
    popup_form += '<div class="cellule1">Pr&eacute;nom <span class="champ_oblg">*</span></div>';
    popup_form += '<div class="cellule2"><input type="text" id="'+tabIdInput[2]+'" name="rec_firstname" size="20" /></div>';
    popup_form += '<div class="cellule1">E-mail <span class="champ_oblg">*</span></div>';
    popup_form += '<div class="cellule2"><input type="text" id="'+tabIdInput[4]+'" name="rec_mail" size="20" /></div>';
    popup_form += '<div style="clear: both;">&nbsp;</div>';
    popup_form += '<h1>Votre message <span class="champ_oblg">*</span></h1>';
    popup_form += '<textarea name="message" id="'+tabIdInput[5]+'" rows="5"  onkeyup="limiteChar(this);" onkeydown="limiteChar(this);"></textarea>';
    popup_form += '<span id="messageCpt" class="champ_oblg"></span>';
    popup_form += '<div style="clear: both;">&nbsp;<br /><br /><br /><br /></div>';
  popup_form += '</div>';
  
  popup_confirm += '<div onclick="javascript:raz();" class="cntrl"></div>';
  popup_confirm += '<div class=\"center\">';
    popup_confirm += '<h1>Votre message a bien &eacute;t&eacute; envoy&eacute;</h1>';
    popup_confirm += '<div><input type="button" onclick="javascript:raz();" class="btn_confirm" /></div>';
  popup_confirm += '</div>';
}

function hideDiv() {
  if (document.getElementById) { // DOM3 = IE5, NS6 
    document.getElementById('hideshow').style.visibility = 'hidden'; 
  } else { 
    if (document.layers) { // Netscape 4 
      document.hideshow.visibility = 'hidden'; 
    } else { // IE 4 
      document.all.hideshow.style.visibility = 'hidden'; 
    } 
  } 
}

function showDiv() {
  if (document.getElementById) { // DOM3 = IE5, NS6 
    document.getElementById('hideshow').style.visibility = 'visible'; 
  } else { 
    if (document.layers) { // Netscape 4 
      document.hideshow.visibility = 'visible'; 
    } else { // IE 4 
      document.all.hideshow.style.visibility = 'visible'; 
    } 
  }
}

function changeClassPopupBlock(pPopupForm) {
  if (pPopupForm) {
    document.getElementById('popup_block').style.width = '700px';
    document.getElementById('popup_block').style.marginLeft = '-350px';
  } else {
    document.getElementById('popup_block').style.width = '350px';
    document.getElementById('popup_block').style.marginLeft = '-175px';
  }
}

/*** Variables ***/
var _URL_RACINE = "http://www.darjeeling.fr/";
var _URL_IMG = "images/";
var _NAME_FILE_IMG = "popup_";
var _URL_FILE_MAIL = "../../email/";


var pageProduitName = "";
var pageProduitUrl = window.document.location;
var popup_form = "";
var popup_confirm = "";
var message_old = "";

var popup = '<div id="hideshow" style="visibility: hidden;">';
	popup += '<div id="fade"></div>';
	popup += '<div id="popup_block" class="popup_block">';
		popup += '<div id=\"content_popup\" class="popup">';
		popup += '</div>';
	popup += '</div>';
popup += '</div>';

/*** Dev Init ***/
  var navIsIE = false; 
  //CHECK FOR IE6
  function is_ie6(){
    return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined));
  }
  if (is_ie6()) {
    navIsIE = true;
  }
  
  var addLoadEvent = function(func){
    if (window.addEventListener) {
      window.addEventListener('load', function() { onloadpage(navIsIE); }, false);
    } else {
      window.attachEvent('onload', function() { onloadpage(navIsIE); });
    }
  }
  window.onload = function() {
    onloadpage(navIsIE);
  }
/** FIN INIT **/

