

/////////////////////////////////////////////////////////////////////////////////
// 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 = parseInt(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.checkDateNoTime = function(o, checkWithNow)
  {
    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 = parseInt(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();
	  var nowDate = now.getFullYear() + '';
	  	  nowDate = nowDate + (now.getMonth()<10 ? "0"+parseInt(now.getMonth()) : now.getMonth()) + ''
	  	  nowDate = nowDate + (now.getDate()<10 ? "0"+parseInt(now.getDate()) : now.getDate());
	  var dDate = year + '' + (month<10 ? "0"+parseInt(month) : month) + '' + (day<10 ? "0"+parseInt(day) : day);

	  if (checkWithNow) {
	      if (dDate < nowDate)
	        return (false);
      }
    }
    else
      return false;
    return true;
  }

  this.checkError = function(o, t)
  {
    var m = false;

    if (t['type'])
    {
      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;
	var err_msg = '';
    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 ((tmp = this.checkError(o, t)))
	          {
	            g = false;
	            nbGFalse++;
	            done[t['id']] = 1;
	            err_msg += tmp + '<br />';
	            if (o.type == 'text' || o.type == 'password' || o.type == 'select' || o.type == 'textarea')
	              o.style.color = chp_on;
	          }
	          else
	            o.style.color = chp;
         }
      }
    }

    return err_msg;
  }


  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;
	  }
  }
  
}

var pv = new paruVendu;
var chp = '#555';
var chp_on ='#e03428';
var chp_brd ='#555555 rgb(221, 221, 221) rgb(221, 221, 221) rgb(85, 85, 85)';
