function submAddr(nomForm,declencheur, aStates) {
      var pointeur = document.forms[nomForm];

      if (isEmpty(pointeur.nomfact.value)) return erreurForm(nomForm,"nomfact",true,true,declencheur);
      if (isEmpty(pointeur.prenomfact.value)) return erreurForm(nomForm,"prenomfact",true,true,declencheur);
      if (isEmpty(pointeur.societefact.value)) return erreurForm(nomForm,"societefact",true,true,declencheur);
      if (isEmpty(pointeur.adressefact.value)) return erreurForm(nomForm,"adressefact",true,true,declencheur);
      if (isEmpty(pointeur.villefact.value)) return erreurForm(nomForm,"villefact",true,true,declencheur);
      if (isEmpty(pointeur.zipcodefact.value)) return erreurForm(nomForm,"zipcodefact",true,true,declencheur);
      var ctrySel = pointeur.paysfact;
      var cId = ctrySel.options[ctrySel.selectedIndex].value;
      if (cId=="") return erreurForm(nomForm,"paysfact",true,true,declencheur);
      if (aStates[cId]){
        var state = pointeur.statefact;
        if (state.options[state.selectedIndex].value=="") return erreurForm(nomForm,"statefact",true,true,declencheur);
      }
      if (isEmpty(pointeur.phonefact.value)) return erreurForm(nomForm,"phonefact",true,true,declencheur);
      if (declencheur == "lien") document.forms[nomForm].submit();
}

function initStates(selectBox){
      var i;
      for(i=selectBox.options.length-1;i>=0;i--){
        selectBox.remove(i);
      }
      var opt = document.createElement('option');
      opt.text = '';
      opt.value = '';
      selectBox.add(opt, null);
}

function stateChange(nomForm, aStates){
      var ctrySel = document.forms[nomForm].paysfact;
      var cId = ctrySel.options[ctrySel.selectedIndex].value;
      var stateSel = document.forms[nomForm].statefact;
      var stateid = document.getElementById('statefact');
      if(aStates[cId]){
        stateSel.style.visibility = "visible";
        stateid.style.visibility = "visible";
        initStates(stateSel);
        var n = aStates[cId]['name'].length;
        for (var i=0;i<n;i=i+1){
          var opt = document.createElement('option');
          opt.text = aStates[cId]['name'][i];
          opt.value = aStates[cId]['id'][i];
          stateSel.add(opt, null);
        }
      }
      else{
        stateSel.style.visibility = "hidden";
        stateid.style.visibility = "hidden";
      }
}

function gosubBOTTOM(aForm) {
  /*if(window.event){ //Very important since in IE the back '<' in the result page is detected and it goes one page back of the current page, solution: add 1 to go to the page submitted.
    aForm.pageBOTTOM.value =  parseInt(aForm.pageBOTTOM.value) + 1;
  }*/
  aForm.pageTOP.value = aForm.pageBOTTOM.value;
  aForm.submit();
}

function gosubTOP(aForm) {
  /*if(window.event){
    aForm.pageTOP.value =  parseInt(aForm.pageTOP.value) + 1;
  }*/
  aForm.pageBOTTOM.value = aForm.pageTOP.value;
  aForm.submit();
}

/**
  Called from the search results page when a key is pressed on the input field on the top of the thumbnails where you define to which page to go to
*/
function entsubTOP(aEvent, aForm) {
  //13 is the enter key !
  if(aEvent){
    var keynum = getKeynum(aEvent);
    if(keynum == 13){ //Enter key code
      gosubTOP(aForm);
    }
  }
  else{
    return true;
  }
}

/**
  Same as the other method but for the input buton on the bottom.
*/
function entsubBOTTOM(aEvent, aForm) {
  //13 is the enter key !
  if(aEvent){
    var keynum = getKeynum(aEvent);
    if(keynum == 13){ //Enter key code 
      gosubBOTTOM(aForm);
    }
  }
  else{
    return true;
  }
}

function vignette(pid) {
  document.vignettes.pageTOP.value=2
}

function goCD(nomForm,nomElement) {  // ================================= ACCES CD PAR POP-UP MENU
	var refCD = popUpSel(nomForm,nomElement)
	if (isEmpty(refCD)) return

	// xxx commentaire temporaire : se servir de la var 'refCD' pour construire l'URL
	// window.location.href = page "cd.html" avec refCD
}

<!-- Changes:  Sandeep V. Tamhankar (stamhankar@hotmail.com) -->

/* 1.1.2: Fixed a bug where trailing . in e-mail address was passing
            (the bug is actually in the weak regexp engine of the browser; I
            simplified the regexps to make it work).
   1.1.1: Removed restriction that countries must be preceded by a domain,
            so abc@host.uk is now legal.  However, there's still the 
            restriction that an address must end in a two or three letter
            word.
     1.1: Rewrote most of the function to conform more closely to RFC 822.
     1.0: Original  */

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	//alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    //alert("The username doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        //alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	//alert("The domain name doesn't seem to be valid.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>4) {
   // the address must end in a two letter or three letter word.
   //alert("The address must end in a three-letter domain, or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   //alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}

// ================================= ENVOI FORM LOGIN
function envoiFormIdent(nomForm,declencheur) {
	var pointeur = document.forms[nomForm]

	if (!isEmail(pointeur.login.value)) return erreurForm(nomForm,"login",true,true,declencheur)
	if (isEmpty(pointeur.motdepasse.value)) return erreurForm(nomForm,"motdepasse",true,true,declencheur)
	
	if (declencheur == "lien") document.forms[nomForm].submit()
	// xxx SI NON RECONNU : RELOAD PAGE --> VOIR HTML PAGE D'APPEL (authenticate.html OU home.html) POUR AFFICHAGE MSG ERREUR
	// xxx SI OK : ALLER A LA PAGE REFFERER (PRECEDEMMENT STOCKEE EN LASSO SEULEMENT SI CETTE PAGE FAIT PARTIE DU SITE PA, SINON ALLER A "home.html")
}
// ================================= ENVOI FORM MOTS CLES
function envoiFormRecherche(nomForm,action,declencheur) {
	var pointeur = document.forms[nomForm]
	if (isEmpty(action)){
                var checkWords = true;
                if(pointeur.pano.checked) checkWords = false; 
                if(!pointeur.typephoto.checked && pointeur.typeillus.checked) checkWords = false;
                if(!pointeur.horizontale.checked && !pointeur.verticale.checked && pointeur.carree.checked) checkWords = false; 
                if(!pointeur.couleur.checked && pointeur.nb.checked) checkWords = false;
                if(checkWords){
		  if (isEmpty(pointeur.motsclesok.value)) return erreurForm(nomForm,"motsclesok",true,true,declencheur)
                }
	}


	if ((!pointeur.typephoto.checked) && (!pointeur.typeillus.checked) && (!pointeur.pano.checked)) return erreurForm(nomForm,"typephoto",true,false,declencheur)
	if ((!pointeur.horizontale.checked) && (!pointeur.verticale.checked) && (!pointeur.carree.checked)) return erreurForm(nomForm,"horizontale",true,false,declencheur)
	if ((!pointeur.couleur.checked) && (!pointeur.nb.checked)) return erreurForm(nomForm,"couleur",true,false,declencheur)

	if (declencheur == "lien") pointeur.submit()
	// xxx SI PARAM action = "inside" (ou NON VIDE) : RECHERCHE DANS IMAGES TROUVEES SINON NOUVELLE RECHERCHE
	// xxx GOTO "result.html" (SI TROUVE SINON "search.html")
}

// cumul des champs "contient tous les mots",  "référence de l'image" et "référence du CD"
function dispatchEnvoiFormRecherche(nomForm,action,declencheur) {
	var pointeur = document.forms[nomForm];
	var champ = pointeur.motsclesok.value;
	// réféfence image ?
	var regexp = new RegExp("^[a-z]{2}[^v][0-9]{3,}$","i");
	if(regexp && regexp.test(champ) == true){
		document.getElementById("searchPidField").value = champ;
		return envoiFormPid('searchPid','lien');
	}
	// référence CD ?
	var regexp = new RegExp("^([a-z])[a-z]v?([0-9]{3})$","i");
	if(regexp){
		var identifiant = regexp.exec(champ);
		if(identifiant && identifiant[2]){
			document.getElementById("rechercheCDfield").value = identifiant[2];
			var selectValue = "";
			switch(identifiant[1]){
				case "P": // PhotoAlto
					selectValue = "PAA"; break;
				case "Y": // Zen Shui
					selectValue = "YAA"; break;
				case "W":
					selectValue = "WAA"; break;
				default:
					break;
			}
			if(selectValue != ""){
				document.getElementById("collectionCDSelectbox").value = selectValue;
				return envoiFormNumCD('recherchecd','lien');
			}
		}
	}
	// juste des mots clés
	return envoiFormRecherche(nomForm,action,declencheur);
}


/**
  Returns the key code of a pressed key
*/
function getKeynum(aEvent){
    var keynum;
    if(window.event){      //IE (windoze)
      keynum = aEvent.keyCode;
    }
    else if(aEvent.which){ // Netscape/Firefox/Opera/Safari
      keynum = aEvent.which;
    }
    return keynum;
}

/**
  This method is called when an enter is pressed on the search page, like that we avoid having to click 
  on the search link. Nothing much to say besides that it checks for the code of the key pressed to
  detect the enter key (code 13). Note that the way of detecting this is browser dependent.
  If enter is pressed if sends the search form. 
*/
function entsubMotsCles(aEvent) {
  //13 is the enter key !
  if(aEvent){
    var keynum = getKeynum(aEvent);
    if(keynum == 13){ //Enter key code
      envoiFormRecherche('recherchemot','','lien');
    }
  }
  else{
    return true;
  }
}

  function entsubMotsClesDispatch(aEvent) {
	  //13 is the enter key !
	  if(aEvent){
	    var keynum = getKeynum(aEvent);
	    if(keynum == 13){ //Enter key code
	    	dispatchEnvoiFormRecherche('recherchemot','','lien');
	    }
	  }
	  else{
	    return true;
	  }
	}

/**
  Same as the other method but this one is called from the page display search results and calls another method
*/
function entsubMotsClesBas(aEvent) {
  //13 is the enter key !
  if(aEvent){
    var keynum = getKeynum(aEvent);
    if(keynum == 13){ //Enter key code
      envoiFormRecherche2('recherchemot','','lien');
    }
  }
  else{
    return true;
  }
}


// ++++++++++++++++++++++++++++++++++++++++++++++++
// MODIF GREG
// ================================= ENVOI FORM MOTS CLES ONLY
/*function envoiFormRecherche2(nomForm,action,declencheur) {
	var pointeur = document.forms[nomForm]
	if (isEmpty(action)){
		if (isEmpty(pointeur.motsclesok.value) && isEmpty(pointeur.motsclesnon.value)) return erreurForm(nomForm,"motsclesok",true,true,declencheur)
		if (isEmpty(pointeur.motsclesok.value)) return erreurForm(nomForm,"motsclesok",true,true,declencheur)
	}
	if (declencheur == "lien") pointeur.submit()
}*/

function envoiFormRecherche2(nomForm,action,declencheur) {
	var pointeur = document.forms[nomForm]
	if (isEmpty(action) && pointeur.searchtype[0].checked ){
		if (isEmpty(pointeur.motsclesok.value) ) return erreurForm(nomForm,"motsclesok",true,true,declencheur)
	}
	if (declencheur == "lien") pointeur.submit()
}
// ================================= ENVOI FORM MOTS CLES CRITERES ONLY
function envoiFormRecherche3(nomForm,action,declencheur) {
  var pointeur = document.forms[nomForm]
  if ((!pointeur.typephoto.checked) && (!pointeur.typeillus.checked)) return erreurForm(nomForm,"typephoto",true,false,declencheur)
  if ((!pointeur.horizontale.checked) && (!pointeur.verticale.checked) && (!pointeur.carree.checked) && (!pointeur.pano.checked)) return erreurForm(nomForm,"horizontale",true,false,declencheur)
  if ((!pointeur.couleur.checked) && (!pointeur.nb.checked)) return erreurForm(nomForm,"couleur",true,false,declencheur)
  //if ((!pointeur.oncd.checked) && (!pointeur.notcd.checked)) return erreurForm(nomForm,"oncd",true,false,declencheur)
  if (declencheur == "lien") pointeur.submit()
	// xxx SI PARAM action = "inside" (ou NON VIDE) : RECHERCHE DANS IMAGES TROUVEES SINON NOUVELLE RECHERCHE
	// xxx GOTO "result.html" (SI TROUVE SINON "search.html")
}




// FIN MODIF GREG
// ++++++++++++++++++++++++++++++++++++++++++++++++



// ================================= FORM AJOUT VIGNETTE COCHEE A VISIO
function ajoutCocheVisio(nomForm,nomElement,nomFormImagettes){
	var total = 0
	for (var i = 0 ; i < document.forms[nomFormImagettes].elements.length ; i++) {
		var pointeur = document.forms[nomFormImagettes].elements[i]
		if (pointeur.name.indexOf('coche') > -1) {
			if (pointeur.checked) total++
		}
	}
	if (total < 1) {
		alert("Select images to add to your lightbox.")
		return
	}

	var nomVisio = popUpSel(nomForm,nomElement)
	if (isEmpty(nomVisio)) return
	if (nomVisio == "__________") nomVisio = nomNovoVisio('')

	// xxx OUVRIR OU RAFRAICHIR VISIO AVEC IMAGES COCHEES
	// xxx ET SI LES IMAGES EXISTENT DEJA DANS LA VISIO ???
	if (!isEmpty(nomVisio)) {
		document.forms[nomForm].submit()
		afficheVisio(nomVisio)
	}
}

// ================================= GESTION VISIO
function nomNovoVisio(commentaire) {
	if (commentaire == "") commentaire = "Nme of the new lightbox:"
	var novoNom = prompt(commentaire,"untitled")
	if (isEmpty(novoNom)) return
	
	// xxx ET SI UNE VISIO S'APPELLE DEJA novoNom ???
	// SI OK :
		// xxx CREER VISIO AVEC NOM CONTENU DANS novoNom
		// RELOAD PAGE POUR ACTUALISER LISTE VISIOS
		// NE PAS AFFICHER LA NOUVELLE VISIO ! (SE FAIT DANS PAGE APPELANT CETTE FCN)
	return novoNom
}
// ================================= GESTION DEVIS
function renommerDevis(nomDevis,commentaire) {
	var novoNom = prompt(commentaire,nomDevis)
	if (isEmpty(novoNom)) return

	// xxx ET SI UN DEVIS S'APPELLE DEJA novoNom ???
	// SI OK :
		// xxx RENOMMER DEVIS nomDevis AVEC NOM CONTENU DANS novoNom
		// xxx SI PROCESS OK : RELOAD PAGE
}
function dupliquerDevis(nomDevis,commentaire) {
	var novoNom = prompt(commentaire,nomDevis + " - copy")
	if (isEmpty(novoNom)) return

	// xxx ET SI UN DEVIS S'APPELLE DEJA novoNom ???
	// SI OK :
		// xxx DUPLIQUER LE DEVIS nomDevis AVEC NOM CONTENU DANS novoNom
		// xxx SI PROCESS OK : AFFICHER LE NOUVEAU DEVIS
}
function supprimerDevis(nomDevis,commentaire) {
	if (!confirm(commentaire)) return

	// xxx DESTRUCTION DEVIS nomDevis ET RETOUR A "quotationmgr.html"
}
// ================================= ERREUR FORMS
function erreurForm(nomForm,nomElement,msg,selection,declencheur, message) {
	// tout eteindre
	for (var i = 0 ; i < document.forms[nomForm].elements.length ; i++) {
		colorieZoneForm(document.forms[nomForm].elements[i].name,"#FFFFFF")
	}
	// msg et allumer la bonne
        if(!message) message = '';
	colorieZoneForm(document.forms[nomForm].elements[nomElement].name,"#DDDDDD")
	if (msg) alert("Invalid data! " + message)
	var typeElement = document.forms[nomForm].elements[nomElement].type
	if (selection && (typeElement == "text" || typeElement == "textarea" || typeElement == "password")){
		document.forms[nomForm].elements[nomElement].focus()
		document.forms[nomForm].elements[nomElement].select()
	}
	if (declencheur != "lien") return false
}
function colorieZoneForm(nomDiv,couleur) {
	if (document.getElementById(nomDiv)) document.getElementById(nomDiv).style.backgroundColor = couleur
}

// ================================= DIVERS FORMS
function isEmail(chaine) {
	if ((chaine.indexOf("@") < 0) || (chaine.indexOf(".") < 0)) return false
	if ((chaine.indexOf(" ") >= 0) || (chaine.length < 6)) return false
	return true
}

function popUpSel(nomForm,nomElement) { // renvoit valeur article sel dans popUpMenu
	return document.forms[nomForm].elements[nomElement].options[document.forms[nomForm].elements[nomElement].selectedIndex].value
}

function boucleAllumeCoche(nomForm) { // colorie en gris les DIV des elements 'coche' des FORMS
	for (var i = 0 ; i < document.forms[nomForm].elements.length ; i++) {
		if (document.forms[nomForm].elements[i].name.indexOf('coche') > -1) allumeCoche(document.forms[nomForm].elements[i])
	}
}
function allumeCoche(element) {
	if (document.getElementById(element.name)) document.getElementById(element.name).style.backgroundColor = (element.checked)? "#DDDDDD" : "#FFFFFF"
}
function getNomForm(nomElement) { // renvoit le nom du formulaire a qui appartient l'element passe
	for (var f = 0 ; f < document.forms.length ; f++) {
		for (var e = 0 ; e < document.forms[f].elements.length ; e++) {
			if (document.forms[f].elements[e].name == nomElement) return document.forms[f].name
		}
	}
	return ''
}

