
/**
 * This array is used to remember mark status of rows in browse mode
 */
var marked_row = new Array;


/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
		theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3
//alert ("currentColor:"+currentColor+" - currentColor.toLowerCase():"+currentColor.toLowerCase()+" - theDefaultColor.toLowerCase():"+theDefaultColor.toLowerCase());
    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
		
        if (theAction == 'over' && thePointerColor != '') {
		    newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
		    newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
			    theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function

/*
 * Sets/unsets the pointer and marker in vertical browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 *
 * @author Garvin Hicking <me@supergarv.de> (rewrite of setPointer.)
 */
function setVerticalPointer(theRow, theRowNum, theAction, theDefaultColor1, theDefaultColor2, thePointerColor, theMarkColor) {
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;

    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        domDetect    = false;
    } // end 3

    var c = null;
    // 5.1 ... with DOM compatible browsers except Opera
    for (c = 0; c < rowCellsCnt; c++) {
        if (domDetect) {
            currentColor = theCells[c].getAttribute('bgcolor');
        } else {
            currentColor = theCells[c].style.backgroundColor;
        }

        // 4. Defines the new color
        // 4.1 Current color is the default one
        if (currentColor == ''
            || currentColor.toLowerCase() == theDefaultColor1.toLowerCase()
            || currentColor.toLowerCase() == theDefaultColor2.toLowerCase()) {
            if (theAction == 'over' && thePointerColor != '') {
                newColor              = thePointerColor;
            } else if (theAction == 'click' && theMarkColor != '') {
                newColor              = theMarkColor;
                marked_row[theRowNum] = true;
            }
        }
        // 4.1.2 Current color is the pointer one
        else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
                 && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
            if (theAction == 'out') {
                if (c % 2) {
                    newColor              = theDefaultColor1;
                } else {
                    newColor              = theDefaultColor2;
                }
            }
            else if (theAction == 'click' && theMarkColor != '') {
                newColor              = theMarkColor;
                marked_row[theRowNum] = true;
            }
        }
        // 4.1.3 Current color is the marker one
        else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
            if (theAction == 'click') {
                newColor              = (thePointerColor != '')
                                      ? thePointerColor
                                      : ((c % 2) ? theDefaultColor1 : theDefaultColor2);
                marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                      ? true
                                      : null;
            }
        } // end 4

        // 5. Sets the new color...
        if (newColor) {
            if (domDetect) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            }
            // 5.2 ... with other browsers
            else {
                theCells[c].style.backgroundColor = newColor;
            }
        } // end 5
    } // end for

     return true;
 } // end of the 'setVerticalPointer()' function


/**
 * getElement
 */
function getElement(e,f){
    if(document.layers){
        f=(f)?f:self;
        if(f.document.layers[e]) {
            return f.document.layers[e];
        }
        for(W=0;i<f.document.layers.length;W++) {
            return(getElement(e,fdocument.layers[W]));
        }
    }
    if(document.all) {
        return document.all[e];
    }
    return document.getElementById(e);
}

function cache_affiche_div (cible, hauteur, cache) {
	var div_cible = document.getElementById(cible);
	div_cible.style.visibility=cache;
	div_cible.style.height=hauteur+"px";
}

function charge_formulaire() {
	AjaxDebut('contactAjax.php?div=formulaire&action=formulaire', 'formulaire');
}


function Valide (formulaire) {
		if (formulaire.lettre_cg.checked)
			lettre_cg=1;
		else
			lettre_cg=0;
	
		if (formulaire.lettre_cdt.checked)
			lettre_cdt=1;
		else
			lettre_cdt=0;
			
		lien = "contactAjax.php?action=valide"
			+ "&nom="+formulaire.nom.value
			+ "&prenom="+formulaire.prenom.value
			+ "&email="+formulaire.email.value
			+ "&telephone="+formulaire.telephone.value
			+ "&adresse="+formulaire.adresse.value
			+ "&cp="+formulaire.cp.value
			+ "&ville="+formulaire.ville.value
			+ "&pays="+formulaire.pays.value
			+ "&question="+escape(formulaire.question.value)
			+ "&lettre_cg="+lettre_cg
			+ "&lettre_cdt="+lettre_cdt;
			//alert(lien);
			
		AjaxDebut(lien, 'formulaire');
		
		return false;
	}
	
	
function charge_ecard(cat_id, site, action) {
	AjaxDebut('http://culturea.meuse.fr/web_services_ecard.php?div=goodies&action='+action+"&site="+site+"&cat_id="+cat_id, 'goodies');
}

function charge_carte(cat_id, car_id, action) {
	AjaxDebut("http://culturea.meuse.fr/web_services_ecard.php?div=goodies&action="+action+"&cat_id="+cat_id+"&car_id="+car_id, 'goodies');
}


/*function gestion_carte_ajax(formulaire, action) {
	lien = "http://culturea.meuse.fr/web_services_ecard.php?div=goodies&action="+action
		+"&site="+formulaire.site.value
		+"&cat_id="+formulaire.cat_id.value
		+"&car_id="+formulaire.car_id.value
		+"&envoyeur_nom="+formulaire.envoyeur_nom.value
		+"&envoyeur_email="+formulaire.envoyeur_email.value
		+"&message="+formulaire.message.value
		+"&texte_couleur="+formulaire.texte_couleur.value
		+"&texte_police="+formulaire.texte_police.value
		+"&texte_taille="+formulaire.texte_taille.value
		+"&carte_position="+formulaire.carte_position.value
		+"&copie="+formulaire.copie.value
		+"&envoyeur_abonnement="+formulaire.envoyeur_abonnement.value
		+"&destinataire_nombre="+formulaire.destinataire_nombre.value;
		
		for (var i=0; i<formulaire.carte_position.length;i++) {
			if (formulaire.carte_position[i].checked) {
				lien+="&carte_position="+formulaire.carte_position[i].value;
			}
		}
		//+"&destinataire_nombre="+formulaire.destinataire_nombre.value
	
		for (i=1;i<=formulaire.nbr_dest_actuel.value;i++) {
			lien+="&destinataire_nom_"+i+"="+eval("formulaire.destinataire_nom_"+i+".value");
			lien+="&destinataire_email_"+i+"="+eval("formulaire.destinataire_email_"+i+".value");
		}
	
		//alert(lien);
		AjaxDebut(lien, "goodies");
		return true;
}*/

function carte_validation (formulaire) {
	// Vérification de la saisi des destinataires
	// Test des noms prénoms
	for(i=1;i<=formulaire.destinataire_nombre.options[formulaire.destinataire_nombre.selectedIndex].value;i++) {
		//alert ('toto');
		if (eval("formulaire.destinataire_nom_"+i+".value.length==0")) {
			alert ("Vous n'avez pas renseigné le nom du destinataire n°"+i);
			return false;
		}
	}
	// Test des emails
	for(i=1;i<=formulaire.destinataire_nombre.options[formulaire.destinataire_nombre.selectedIndex].value;i++) {
		if (eval("formulaire.destinataire_email_"+i+".value.length==0")) {
			alert ("Vous n'avez pas renseigné l'email du ddestinataire n°"+i);
			return false;
		}
		if (!is_email(eval("formulaire.destinataire_email_"+i+".value"))) {
			alert ("L'adresse email du destinataire n°"+i+" est incorrecte.");
			return false;
		}
	}
	// Test de l'envoyeur
	if (formulaire.envoyeur_nom.value.length==0) {
		alert ("Vous n'avez pas renseigné votre nom");
		return false;
	}
	if (formulaire.envoyeur_email.value.length==0) {
		alert ("Vous n'avez pas renseigné votre email");
		return false;
	}
	if (!is_email(formulaire.envoyeur_email.value)) {
		alert ("Votre adresse email est incorrecte.");
		return false;
	}
	// Test du message
	if (formulaire.message.value.length==0) {
		alert ("Vous n'avez pas saisi de message !");
		return false;
	}
	
	//return gestion_carte_ajax(formulaire, "previsu");
	//formulaire.action.value="carte_visualisation";
	//formulaire.submit ();
	
	//alert(formulaire.message.value);
	//var chaine=formulaire.message.value;
	//var reg=new RegExp("\r\n", "g");
	//chaine.replace("\n","<br />");

//alert(chaine);

	/*lien = "http://culturea.meuse.fr/web_services_ecard.php?div=goodies&action=previsu"
		+"&site="+formulaire.site.value
		+"&cat_id="+formulaire.cat_id.value
		+"&car_id="+formulaire.car_id.value
		+"&envoyeur_nom="+formulaire.envoyeur_nom.value
		+"&envoyeur_email="+formulaire.envoyeur_email.value
		+"&message="+chaine
		+"&texte_couleur="+formulaire.texte_couleur.value
		+"&texte_police="+formulaire.texte_police.value
		+"&texte_taille="+formulaire.texte_taille.value
		+"&carte_position="+formulaire.carte_position.value
		+"&copie="+formulaire.copie.value
		+"&envoyeur_abonnement="+formulaire.envoyeur_abonnement.value
		+"&destinataire_nombre="+formulaire.destinataire_nombre.value;
		
		for (var i=0; i<formulaire.carte_position.length;i++) {
        	if (formulaire.carte_position[i].checked) {
            lien+="&carte_position="+formulaire.carte_position[i].value;
         }
      }**/
		//+"&destinataire_nombre="+formulaire.destinataire_nombre.value
	
/*	for (i=1;i<=formulaire.nbr_dest_actuel.value;i++) {
		lien+="&destinataire_nom_"+i+"="+eval("formulaire.destinataire_nom_"+i+".value");
		lien+="&destinataire_email_"+i+"="+eval("formulaire.destinataire_email_"+i+".value");
	}*/
	
	//alert(lien);
	//AjaxDebut(lien, "goodies");
	
	return true;

}
function is_email(email) {
	var reg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]{2,}[.][a-zA-Z0-9]{2,4}$/
	var reg2 = /[.@]{2,}/
	return ((reg.exec(email)!=null) && (reg2.exec(email)==null))
}


/*function carte_changement(formulaire) {
		lien = "http://culturea.meuse.fr/web_services_ecard.php?div=goodies&action=formulaire"
		+"&site="+formulaire.site.value
		+"&cat_id="+formulaire.cat_id.value
		+"&car_id="+formulaire.car_id.value
		+"&envoyeur_nom="+formulaire.envoyeur_nom.value
		+"&envoyeur_email="+formulaire.envoyeur_email.value
		+"&message="+formulaire.message.value
		+"&texte_couleur="+formulaire.texte_couleur.value
		+"&texte_police="+formulaire.texte_police.value
		+"&texte_taille="+formulaire.texte_taille.value
		+"&copie="+formulaire.copie.value
		+"&envoyeur_abonnement="+formulaire.envoyeur_abonnement.value
		+"&destinataire_nombre="+formulaire.destinataire_nombre.value;
		
		for (var i=0; i<formulaire.carte_position.length;i++) {
        	if (formulaire.carte_position[i].checked) {
            lien+="&carte_position="+formulaire.carte_position[i].value;
         }
      }
		//+"&destinataire_nombre="+formulaire.destinataire_nombre.value
	
	for (i=1;i<=formulaire.nbr_dest_actuel.value;i++) {
		lien+="&destinataire_nom_"+i+"="+eval("formulaire.destinataire_nom_"+i+".value");
		lien+="&destinataire_email_"+i+"="+eval("formulaire.destinataire_email_"+i+".value");
	}
	
	alert(lien);
	AjaxDebut(lien, "goodies");
}*/


function carte_changement(formulaire) {
	formulaire.action.value="formulaire";
	formulaire.submit();
	return true;
}
function carte_envoi(formulaire) {
	formulaire.action.value="envoi";
	formulaire.submit();
	return false;
}
function carte_destinataire (formulaire) {
	formulaire.action.value="formulaire";
	formulaire.submit ();
	return false;
}
