var aReqFields = new Array();
var aBoxes     = new Array();
var delim      = '%';
var sInvalid   = ' (invalid)';

var NS4  = (document.layers) ? 1 : 0;
var IE4  = (document.all) ? 1 : 0;
var ver4 = (NS4 || IE4) ? 1 : 0;

var sLayerElement = '';

if (IE4)
{ sLayerElement = 'all'; }
else if (NS4)
{ sLayerElement = 'layers'; }


// --------------------[ ReqField ]--------------------
// -                                                  -
// -  Gives "required" status to a form field         -
// -                                                  -
// -  > INPUT                                         -
// -  sFieldName  - the name of the field             -
// -  sFieldDesc  - the description of the field      -
// -  nReq        - required ? (0/1)                  -
// -  sExtraCheck - extra check to perform ?          -
// -  leave empty or choose in                        -
// -                                                  -
// -  'ALPHA'       = alphabetic                      -
// -  'NUM'         = numeric                         -
// -  'ALPHANUM'    = alphanumeric                    -
// -  'EMAIL'       = valid email address             -
// -  'ABSURL'      = valid absolute URL              -
// -  'NOT(X)'      = not the single character X      -
// -  'NUM(n)'      = numeric, n digits max           -
// -  'ALPHA(n)'    = alphabetic, n characters max    -
// -  'ALPHANUM(n)' = alphanumeric, n characters max  -
// -  'NUMIN(m,n)'  = numeric, value between m and n  -
// -                                                  -
// -  Latest addition                                 -
// -                                                  -
// -  'DATE_MM/DD/YYYY'                               -
// -                                                  -
// -  sFormName   - the name of the form              -
// -  sLayerName  - the name of the layer (optional)  -
// -                                                  -
// -  > OUTPUT                                        -
// -  none.                                           -
// -                                                  -
// ----------------------------------------------------

function ReqField(sFieldName,sFieldDesc,nReq,sExtraCheck,sFormName,sLayerName)
{
  aReqFields[aReqFields.length] = sFieldName + delim + sFieldDesc + delim + nReq + delim + sExtraCheck + delim + sFormName + delim + sLayerName;
}

function ReqBox(sGroupPrefix,sGroupDesc,nReq,sFormName,sLayerName)
{
  aBoxes[aBoxes.length] = sGroupPrefix + delim + sGroupDesc + delim + nReq + delim +  sFormName + delim + sLayerName;
}


// --------------------[ CheckString ]--------------------
// -                                                     -
// -  Performs a pre-defined RegExp check on a string    -
// -                                                     -
// -  > INPUT                                            -
// -  sStr        - the string to check                  -
// -  sCheck      - the check to perform                 -
// -                                                     -
// -  'ALPHA'       = alphabetic                         -
// -  'NUM'         = numeric                            -
// -  'ALPHANUM'    = alphanumeric                       -
// -  'EMAIL'       = valid email address                -
// -  'ABSURL'      = valid absolute URL                 -
// -  'NOT(X)'      = not the single character X         -
// -  'NUM(n)'      = numeric, n digits max              -
// -  'ALPHA(n)'    = alphabetic, n characters max       -
// -  'ALPHANUM(n)' = alphanumeric, n characters max     -
// -  'NUMIN(m,n)'  = numeric, value between m and n     -
// -                                                     -
// -  Latest addition                                    -
// -                                                     -
// -  'DATE_MM/DD/YYYY'                                  -
// -                                                     -
// -  > OUTPUT                                           -
// -  boolean (check ok ?)                               -
// -                                                     -
// -------------------------------------------------------

function CheckString(sStr,sCheck)
{
  var RE     = null;
  var len    = null;
  var result = true;
  
  // simple tests
  
  switch (sCheck)
  {
    case 'ALPHA' :
    {
      RE = /^[A-Za-z]+$/;
      break;
    }
    case 'NUM' :
    {
      RE = /^\d+$/;
      break;
    }
    case 'ALPHANUM' :
    {
      RE = /^.+$/;
      break;
    }
    case 'EMAIL' :
    {
      RE = /^.+@.+\..+$/;
      break;
    }
    case 'ABSURL' :
    {
      RE = /^http:\/\/.+$/;
      break;
    }
    case 'DATE_DD/MM/YYYY' :
    {
      RE = /^\d{2}\/\d{2}\/\d{4}\/$/;
      break;
    }
  }
  
  // advanced tests
  
  len = sCheck.length;
  
  // NOT(#) = not the character #
  if (sCheck.substr(0,4)=='NOT(')
  {
    sCheck = sCheck.substring(4,len-1);
    RE = new RegExp('^[^'+ sCheck +'].*$');
  }

  if (sCheck.substr(0,6)=='NEVER(')
  {
    sCheck = sCheck.substring(6,len-1);
    RE = new RegExp('^[^'+ sCheck +']*$');
  }

  // NUM(n) = numeric, n chars max
  if (sCheck.substr(0,4)=='NUM(')
  {
    sCheck = sCheck.substring(4,len-1);
    RE = new RegExp('^\\d{0,' + sCheck + '}$');
  }
  
  // ALPHA(n) = alpha, n chars max
  if (sCheck.substr(0,6)=='ALPHA(')
  {
    sCheck = sCheck.substring(6,len-1);
    RE = new RegExp('^[A-Za-z\\w]{0,'+ sCheck +'}$');
  }
  
  // ALPHANUM(n) = alphanumeric, n chars max
  
  if (sCheck.substr(0,9)=='ALPHANUM(')
  {
    sCheck = sCheck.substring(9,len-1);
    RE = new RegExp('^.{0,' + sCheck + '}$');
  }
  
  // NUMIN(x,y) = numeric, between x and y
  if (sCheck.substr(0,6)=='NUMIN(')
  {

    var aRange = new Array();
    var nStr   = null;
    var nMin   = null;
    var nMax   = null;
    
    RE = /^\d+$/;

    sCheck = sCheck.substring(6,len-1);
    aRange = sCheck.split(',');
    nStr = Number(sStr);
    nMin = Number(aRange[0]);
    nMax = Number(aRange[1]);
    
    if ((nStr<nMin)||(nStr>nMax)) result = false;
    
  }

  if (!RE.test(sStr)) result = false;
  return (result);

}


// --------------------[ CheckForm ]--------------------
// -                                                   -
// -  Performs the form validity check                 -
// -                                                   -
// -  > INPUT                                          -
// -  sFormToCheck - the form to check                 -
// -                                                   -
// -  > OUTPUT                                         -
// -  boolean (check ok ? )                            -
// -                                                   -
// -----------------------------------------------------

function CheckForm(sFormToCheck,sTitle)
{
  var len        = aReqFields.length;
  var i          = null;
  var isEmpty    = null;
  var ok         = null;
  var sHeader    = sTitle;
  var sMissing   = '';
  var sInputPath = '';
  var sInputType = '';
  var sValue     = '';
  var aParams    = new Array();
  
  for (i=0;i<len;i++)
  {

    aParams = aReqFields[i].split(delim);
    sFormName  = aParams[4];
    
    if (sFormToCheck==sFormName)
    {
      sFieldName  = aParams[0];
      sFieldDesc  = aParams[1];
      nReq        = aParams[2];
      sExtraCheck = aParams[3];
      sLayerName  = aParams[5];
      
      isEmpty     = false;
      
      if (sLayerName)
      { sInputPath = 'document.' + sLayerElement + '["' + sLayerName + '"].document.' + sFormName + '.' + sFieldName; }
      else
      {	sInputPath = 'document.' + sFormName + '.' + sFieldName; }
      
      eval ('sInputType = ' + sInputPath + '.type;');
      
      if (!sInputType) eval ('sInputType = ' + sInputPath + '[0].type;');
      
      switch (sInputType)
      {
        case 'text':
        case 'password':
        case 'textarea':
        case 'file':
        {
          eval('sValue = ' + sInputPath + '.value;');
          if (!sValue)
          {
            if (nReq==1) sMissing = sMissing + sFieldDesc + '\n';
            isEmpty  = true;
          }
          break;
        }
        case 'select-one':
        case 'select-multiple':
        {
          eval('sValue = ' + sInputPath + '.selectedIndex;');
          if (sValue==-1)
          {
            if (nReq==1) sMissing = sMissing + sFieldDesc + '\n';
            isEmpty  = true;
          }
          else
          {
            eval('sValue = ' + sInputPath + '.options[sValue].value;');
          }
          break;
        }
        case 'radio':
        {
          var j;
          var nRad      = 0;
          var isChecked = false;
          eval('while ('+sInputPath+'[nRad]) nRad++;');
          for (j=0;j<nRad;j++)
          {
            eval('if ('+sInputPath+'[j].checked) isChecked = true;');
          }
          
          if (!isChecked)
          {
            if (nReq==1) sMissing = sMissing + sFieldDesc + '\n';
            isEmpty  = true;
          }

          break;
        }
      }
      
      if ( sExtraCheck && !isEmpty )
      {
      	if (!CheckString(sValue,sExtraCheck)) sMissing = sMissing + sFieldDesc + '\n';
      }
      
    }
    
  }
  
  // now check boxes groups
  
  var aChecked = new Array();
  var aOK      = new Array();
  var len2     = aBoxes.length;
  var flen;
  
  if (len2>0)
  {
    for (i=0;i<len2;i++)
    {
      aChecked[i] = 0;
      aOK[i]      = 0;

      aParams      = aBoxes[i].split(delim);
      sGroupPrefix = aParams[0];
      sGroupDesc   = aParams[1];
      nReq         = aParams[2];
      sFormName    = aParams[3];
      sLayerName   = aParams[4];
      eval('flen = document.'+sFormName+'.elements.length');
      plen = sGroupPrefix.length;
      
      for (j=0;j<flen;j++)
      {
        eval('sField = document.'+sFormName+'.elements['+j+']');
        if (sField.name.substr(0,plen)==sGroupPrefix)
        {
          if (sField.type=="checkbox" && sField.checked)
          {
            aChecked[i]++;
            if (aChecked[i]>=nReq) aOK[i]=1;
          }
        }
      }
      
      sSel = 'selections';
      if (nReq<2) sSel = 'selection';
      if (!aOK[i]) sMissing = sMissing + sGroupDesc + '\n';
    }
  }
  
  ok = (sMissing)?false:true;
  if (!ok) alert(sHeader+sMissing);
  return (ok);

}

function InitReqFields()
{
  aReqFields = new Array();
}

function InitReqBoxes()
{
  aBoxes = new Array();
}


// value-added nifty feature

function SubmitForm(sFormName,sLayer)
{
  if (sLayer)
  { sInputPath = 'document.' + sLayerElement + '["' + sLayer + '"].document.' + sFormName; }
  else
  { sInputPath = 'document.' + sFormName; }
  
  eval (sInputPath + '.submit();');
}

function zoom(burl) 
{
        var pop=window.open('zoom.html?'+burl, 'zoom', 'location=no,toolbar=no,menubar=no, scrollbars=no,resizable=no,width=650,height=570,status=no');
        pop.focus();
}


function zoomperso(burl) 
{
        var pop=window.open('zoomperso.html?'+burl, 'zoomperso', 'location=no,toolbar=no,menubar=no, scrollbars=no,resizable=no,width=650,height=570,status=no');
        pop.focus();
}

function downloader(burl) 
{
        var pop=window.open('download.html?'+burl, 'downloader', 'location=no,toolbar=no,menubar=no, scrollbars=yes,resizable=no,width=650,height=550,status=no');
        pop.focus();
}


function selector(burl) 
{
        var pop=window.open('selector.html?'+burl, 'selector', 'location=no,toolbar=no,menubar=no, scrollbars=no,resizable=no,width=650,height=550,status=yes');
        pop.focus();
}

function contactus(burl) 
{
        var pop=window.open('contactus.html?'+burl, 'contact', 'location=no,toolbar=no,menubar=no, scrollbars=no,resizable=no,width=700,height=435,status=yes');
        pop.focus();
}

function dataprivacy(burl) 
{
        var pop=window.open('dataprivacy.html?'+burl, 'dataprivacy', 'location=no,toolbar=no,menubar=no, scrollbars=no,resizable=no,width=700,height=435,status=yes');
        pop.focus();
}

function pop(burl,bname,bwidth,bheight) 
{
        var pop=window.open('popup.html?page='+burl, bname, 'toolbar=no, resizable=no, location=no, status=no, scrolling=AUTO, directories=no, scrollbars=no,width=' + bwidth + ',height=' + bheight);
        pop.focus();
}

function adminPop(burl,bname,bwidth,bheight) 
{
        var pop=window.open(burl, bname, 'toolbar=no, resizable=yes, location=no, status=yes, scrolling=yes, directories=no, scrollbars=yes,width=' + bwidth + ',height=' + bheight);
        pop.focus();
}

function popup(burl) 
{
        var pop=window.open(burl, 'viewer', 'location=no,toolbar=no,menubar=no, scrollbars=yes,resizable=no,width=800,height=600,status=no');
        pop.focus();
}

function go_alert(url)
{
  if (url!="")
  {
    
			var extpop=window.open(url,'extLink','');
			extpop.focus();
  }
}

function confirmPop(iurl, sTitle)
{
  var strWarning = "You are about to delete " + sTitle + ". Do you wish to proceed ?";
  
  if (confirm(strWarning))
    {
		window.location.href=iurl;
    }
}

var doshow = false;

function showhidediv( nom, doshow, theclass )
{
		/*alert('showhidediv',nom, doshow, theclass);*/
		
		var divID = nom;
		if ( document.getElementById && document.getElementById( divID ) )
		{
			Pdiv = document.getElementById( divID );
			PcH = true;
	 	}
		else if ( document.all && document.all[ divID ] )
		{
			Pdiv = document.all[ divID ];
			PcH = true;
		}
		else if ( document.layers && document.layers[ divID ] )
		{
			Pdiv = document.layers[ divID ];
			PcH = true;
		}
		else
		{
			PcH = false;
		}
		if ( PcH )
		{
			Pdiv.className = doshow ? theclass : 'cachediv';
		}
}


function showhide( nom, doshow )
{
		
		var divID = nom;
		if ( document.getElementById && document.getElementById( divID ) )
		{
			Pdiv = document.getElementById( divID );
			PcH = true;
	 	}
		else if ( document.all && document.all[ divID ] )
		{
			Pdiv = document.all[ divID ];
			PcH = true;
		}
		else if ( document.layers && document.layers[ divID ] )
		{
			Pdiv = document.layers[ divID ];
			PcH = true;
		}
		else
		{
			PcH = false;
		}
		if ( PcH )
		{
			if (Pdiv.className == "cachediv") Pdiv.className = "modify"; 
			else
			if (Pdiv.className == "modify") Pdiv.className = "cachediv";
		}
}
