

/////////////////////////////////////////////////////////////////////////////////
// Navigator Class
/////////////////////////////////////////////////////////////////////////////////

function  DetectNavigator()
{
	if (navigator.appName.substring(0, 3) != "Net" && !window.opera)
	{
		this.isIe = 1;
		this.isMoz = 0;
	}
	else
	{
		this.isIe = 0;
		this.isMoz = 1;
	}
}

Nav = new DetectNavigator();

paruVendu = function()
{
  this.nbWeek = 1;
  this.tabParution = new Array();
  this.tabParutionExemp = new Array();
  this.formField = new Array();
  this.oldCheck = 0;
  this.redirError = '';

  this.onOnglet = function(o)
  {
    o.oldC = o.className;
    o.className = o.className == 'so_cur' ? '' : 'so_cur';
  }

  this.outOnglet = function(o)
  {
    o.className = o.oldC;
  }

  this.focusS = function(o, txt)
  {
    if (o.value == txt)
      o.value = '';
  }

  this.leaveS = function(o, txt)
  {
    if (o.value == '')
      o.value = txt;
  }

  this.updateNbTextarea = function(o, chars, lines, max)
  {
    chars = document.getElementById(chars);
    if (lines)
    	lines = document.getElementById(lines);
    if (chars)
    {
      if (o.value.length >= max)
      {
        o.value = o.value.substr(0, max);
        chars.className = 'bad';
        if (lines)
        	lines.className = 'bad';
      }
      else
      {
        chars.className = '';
        if (lines)
        	lines.className = '';
      }
      chars.value = o.value.length ? o.value.length : '';
      if (lines)
      {
      	var nb = o.value.length ? 1 : 0;
      	for (var i = 0; i < o.value.length; ++i)
        	if (o.value.charAt(i) == "\n")
          	++nb;

	     	lines.value = nb ? nb : '';
	    }
    }
  }

  this.addToForm = function(id, label, type, min, idForm)
  {
    var k = new Array();
    k['label'] = label;
    k['type'] = type;
    k['p'] = min;
    k['id'] = id;
    k['idForm'] = idForm;

    this.formField.push(k);
  }
  

  this.checkMail = function(addr)
  {
    if (!addr)
    	return false;

    if (addr.length < 6)
      return true;

    var sep = addr.lastIndexOf('@');
    var ptr = addr.lastIndexOf('.');
    if (sep == -1 || ptr == -1 || ptr < sep || ((sep + 3) >= addr.length))
      return true;

    return false;
  }

  this.checkPrice = function(v)
  {
    if (!v.match(/^[0-9]+$/gi))
    	return (true);
    if (parseInt(v) < 10)
      return (true);
    return (false);
  }

  this.checkDateBirth = function(o)
  {
    if(o.match(/^[0-3][0-9]\/(0|1)[0-9]\/(19|20)[0-9]{2}$/))
    {
      var date_array = o.split('\/');
      var day = date_array[0];

      // Attention! Javascript consider months in the range 0 - 11
      var month = date_array[1] - 1;
      var year = date_array[2];

      // This instruction will create a date object
      var d = new Date(year,month,day);

      if(year != d.getFullYear())
         return false;

      if(month != d.getMonth())
         return false;

      if(day != d.getDate())
         return false;
      var now = new Date();
      if (d > now)
        return (false);
    }
    else
      return false;
    return true;
  }

  this.checkDate = function(o)
  {
    if(o.match(/^[0-3][0-9]\/(0|1)[0-9]\/(19|20)[0-9]{2}$/))
    {
      var date_array = o.split('\/');
      var day = date_array[0];

      // Attention! Javascript consider months in the range 0 - 11
      var month = date_array[1] - 1;
      var year = date_array[2];

      // This instruction will create a date object
      var d = new Date(year,month,day);

      if(year != d.getFullYear())
         return false;

      if(month != d.getMonth())
         return false;

      if(day != d.getDate())
         return false;
      var now = new Date();
      if (d < now)
        return (false);
    }
    else
      return false;
    return true;
  }

  this.checkError = function(o, t)
  {
    var m = false;
	
	
    if (t['type'])
    {
      o.value = o.value.replace(/^\s+/, '').replace(/\s+$/, '');
      if (t['type'] == 'min' && t['p'] && o.value.length < t['p'])
        m = true;
      else if (t['type'] == 'num' && !o.value.match(/^[0-9]+$/gi))
        m = true;
      else if (t['type'] == 'mail' && this.checkMail(o.value))
        m = true;
      else if (t['type'] == 'numprice' && this.checkPrice(o.value))
        m = true;
      else if (t['type'] == 'date' && !this.checkDate(o.value))
        m = true;
      else if (t['type'] == 'dateBirth' && !this.checkDateBirth(o.value))
        m = true;
      else if (t['type'] == 'diff' && document.getElementById(t['p']).value != o.value)
        m = true;
    }
    else
    {
      if (o.length && o.nodeName != 'SELECT')
      {
        var checked = false
        for (var i = 0; i < o.length; ++i)
          if (o[i].checked)
            checked = true;
        if (!checked)
          m = true;
      }
      else if ((o.type == 'checkbox' && !o.checked) || !o.value)
        m = true;
    }

    return (m ? t['label'] : false);
  }

  this.checkForm = function(fo)
  {
    var g = true;
    var nbGFalse = 0;
    document.getElementById('formWrong').style.display = 'none';
    var ul = document.createElement('ul');
    var done = new Array();
    for (var i in this.formField)
    {
      var t = this.formField[i];
      if (!t['idForm'] || t['idForm'] == fo.id)
      {
        var o = eval('(fo.' + t['id'] + ');');
        var tmp;
        if (!done[t['id']] && o)
        {
          	if (this.nomFormulaire = 'inscriptionNonPopup') {
          	if ((document.getElementById('ddlPays').value!='FR' && t['id']=='txtCodePostal'))
          	{
          		continue;
          	}
          	if ((document.getElementById('ddlPays').value=='FR' && t['id']=='txtVille')
          		|| (document.getElementById('ddlPays').value!='FR' && t['id']=='ddlVille'))
         	{
         		continue;
         	}
         	}
          	
          	if ((tmp = this.checkError(o, t)))
	          {
	            g = false;
	            nbGFalse++;
	            done[t['id']] = 1;
	            var li = document.createElement('li');
	            li.innerHTML = tmp;
	            ul.appendChild(li);
	            if (o.type == 'text' || o.type == 'password' || o.type == 'select' || o.type == 'textarea')
	              o.className = 'bad';
	          }
	          else
	            o.className = '';
         }
          
          
      }
    }
    if (!g)
    {
      document.getElementById('formWrong').innerHTML = '';
      document.getElementById('formWrong').appendChild(ul);
      document.getElementById('formWrong').style.display = 'block';
      
      if(this.nomFormulaire == 'inscriptionNonPopup'){
	      nbHauteurARajouter = 700+(nbGFalse*15);
    	  document.getElementById('colonne-droite').style.height = nbHauteurARajouter+'px';
   	  }
      if (document.location.href.search(/#$/) != -1)
        document.location.href = document.location.href;
      else
        document.location.href = document.location.href + '#' + this.redirError;
    }
    return (g);
  }

  this.show = function(id)
  {
    var o = document.getElementById(id);
    if (o)
      o.style.display = 'block';
  }

  this.setSelected = function(o)
  {
    if (o.checked)
    {
      if (this.oldCheck)
        this.oldCheck.innerHTML = '';
      this.oldCheck = document.getElementById(o.id + 'Txt');
      if (this.oldCheck)
        this.oldCheck.innerHTML = 'Cette photo apparaitra dans la liste de résultat';
    }
  }

  this.setNbWeek = function(nb, txt)
  {
    this.nbWeek = nb;
    document.getElementById('nbWeek').innerHTML = txt;
    this.calcPriceParution();
  }

  this.calcPriceParution = function()
  {

    var price = 0;
    for (var i in this.tabParution)
      if (this.tabParution[i] != -1)
        price += this.tabParution[i];
    document.getElementById('totalPrice').innerHTML = (this.nbWeek * price);

    var exemp = 0;
    for (var i in this.tabParutionExemp)
      if (this.tabParutionExemp[i] != -1)
        exemp += this.tabParutionExemp[i];
    document.getElementById('totalExemp').innerHTML = exemp;
  }

  this.addTirage = function(o, price, exemp, id)
  {
    if (o.checked)
    {
      var p = o.parentNode.parentNode.cloneNode(true);

      var i = p.getElementsByTagName('input')[0];
      i.parentNode.removeChild(i);

      p.id = o.parentNode.parentNode.id + 'Recap';

      document.getElementById('recap').appendChild(p);
      document.getElementById('recap').parentNode.innerHTML += ''; // Anti Bug Ie
      this.tabParution[id] = price;
      this.tabParutionExemp[id] = exemp;
    }
    else
    {
      this.tabParutionExemp[id] = -1;
      this.tabParution[id] = -1;
      var p = document.getElementById(o.parentNode.parentNode.id + 'Recap');
      if (p)
        p.parentNode.removeChild(p);
    }
    this.calcPriceParution();
    this.makeOdd(document.getElementById('recap'));
  }

  this.makeOdd = function(tab)
  {
    is = tab.getElementsByTagName('tr');
    var k = 0;
    for (var i = 0; i < is.length; ++i)
	  {
	    var tr = is[i];
	    if (!(k % 2))
	      tr.className = 'odd';
	    else
	      tr.className = '';
	    if (tr.style.display != 'none')
	      ++k;
	  }
  }

  this.changeCountry = function(code)
  {
		/*
        var cp = document.getElementById('codePostalTr');
		var v = document.getElementById('villeTxt');
		var vs = document.getElementById('villeDdl');
		*/
		if (code == 'fr' || code == 'FR')
		{
			/*
            cp.style.display = cp.oldDisplay ? cp.oldDisplay : (Nav.isIe ? 'block' : 'table-row');
			vs.style.display = vs.oldDisplay ? vs.oldDisplay : (Nav.isIe ? 'block' : 'table-row');
			vs.getElementsByTagName('select')[0].style.display = 'block';
			v.oldDisplay = v.style.display;
			v.style.display = 'none';
            */
			if ($('villeDdl')) { $('villeDdl').show(); }
			if ($('codePostalTr')) { $('codePostalTr').show(); }
			if ($('villeTxt')) { $('villeTxt').hide(); }
			if ($('txtCodePostal')) { $('txtCodePostal').disabled = false; }
			if ($('ddlVilleLibelle')) { 
                $('ddlVilleLibelle').value = '';
                $('ddlVilleLibelle').disabled = true; 
            }
		}
		else
		{
			/*
            vs.getElementsByTagName('select')[0].style.display = 'none';
			cp.oldDisplay = cp.style.display;
			cp.style.display = 'none';
			vs.oldDisplay = vs.style.display;
			vs.style.display = 'none';
			v.style.display = Nav.isIe ? 'block' : 'table-row';
            */
			if ($('villeDdl')) $('villeDdl').hide();
			if ($('codePostalTr')) $('codePostalTr').hide();
			if ($('villeTxt')) $('villeTxt').show();
			if ($('txtCodePostal')) { 
                $('txtCodePostal').value = '';
                $('txtCodePostal').disabled = true; 
            }
			if ($('ddlVilleLibelle')) { 
                $('ddlVilleLibelle').disabled = false; 
            }
		}
  }
}

var pv = new paruVendu;

