function saveSelection(action,galid,page) {
	var tmp= document.form1.detail.value;
	if ( tmp !="" ) {
		images = tmp.split(',');
	}
	else
		images= new Array();
	var cpt=images.length;

	for (i = 0; i < document.forms.length-1; i++){
		exists=false;
		for ( j=0 ; j < images.length ; j++ ) {
			if ( images[j]==document.forms[i].elements[0].value) {
				exists=true;
				break;
			}
		}
		if ( document.forms[i].elements[0].checked == true && exists==false ) {
			images[j]=document.forms[i].elements[0].value;
			cpt++;
		}
		if ( document.forms[i].elements[0].checked == false && exists==true ) {
			images.splice(j,1);
			j--;
			cpt--;
		}
	}
	tmp=images.join(',');
	document.form1.detail.value = tmp;
	if (action == "continuer"){
		document.form1.action = "./index.php?Gal=" + galid + "&Page=" + page;
	}
	else{
	 if ( cpt < 1 ) {
	   alert ( "Vous n'avez sélectionné aucune photo!");
	   return;
	 }
		document.form1.action = "./index.php?Action=shop";
	}
	document.form1.submit();
}

function calcul() {
	var prix = 0;
	var total=0;
	var nb10x15=0;
	for (i = 0; i < document.formCart.elements.length; i++){
		if ( document.formCart.elements[i].checked == true ) {
			nb = parseInt(document.formCart.elements[i+1].value);
			total+=nb;
			if (document.formCart.elements[i].name=="10x15[]" )
			{
				if (nb10x15==0) {
					prix+=7+(nb-1)*5;
					nb10x15=1;
				}
				else
					prix += nb*5;
			}
			if (document.formCart.elements[i].name=="13x18[]" )
			{
				prix+=nb*10;
			}
			if (document.formCart.elements[i].name=="15x20[]" )
			{
				prix+=nb*12;
			}
		}
	}
	if ( total==1) {
		prix+=1;
	}
	document.formCart.prix.value=prix;
}

function affichage_popup(nom_de_la_page, nom_interne_de_la_fenetre)
{
window.open (nom_de_la_page, nom_interne_de_la_fenetre, config='height=500, width=500, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no')
}

function isblank(s) {
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '	')) return false;
    }
    return true;
}

function emailCheck (emailStr) {
	var myEMailIsValid = true;
	var myAtSymbolAt = emailStr.indexOf('@');
	var myLastDotAt = emailStr.lastIndexOf('.');
	var mySpaceAt = emailStr.indexOf(' ');
	var myLength = emailStr.length;

	// at least one @ must be present and not before position 2
	// @yellow.com : NOT valid
	// x@yellow.com : VALID

	if (myAtSymbolAt < 1 ) {
		myEMailIsValid = false;
	}


	// at least one . (dot) afer the @ is required
	// x@yellow : NOT valid
	// x.y@yellow : NOT valid
	// x@yellow.org : VALID

	if (myLastDotAt < myAtSymbolAt) {
		myEMailIsValid = false;
	}

	// at least two characters [com, uk, fr, ...] must occur after the last . (dot)
	// x.y@yellow. : NOT valid
	// x.y@yellow.a : NOT valid
	// x.y@yellow.ca : VALID

	if (myLength - myLastDotAt <= 2) {
		myEMailIsValid = false;
	}

	// no empty space " " is permitted (one may trim the email)
	// x.y@yell ow.com : NOT valid

	if (mySpaceAt != -1) {
		myEMailIsValid = false
	}

	if (myEMailIsValid == true) {
		return true;
	}
	else {
		return false;
	}

}



function verify(f) {
    var msg;
    var empty_fields = "";
    var errors = "";
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
        if (((e.type == "text") || (e.type == "textarea")) && e.required) {
            // First check if the field is empty
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n          " + e.name;
                continue;
            }

            // Now check for fields that are supposed to be numeric
            if (e.numeric || (e.min != null) || (e.max != null)) {
                var v = parseFloat(e.value);
                if (isNaN(v) ||
                    ((e.min != null) && (v < e.min)) ||
                    ((e.max != null) && (v > e.max))) {
                    errors += "- Le champ " + e.name + " doit être un nombre";
                    if (e.min != null)
                        errors += " supérieur à " + e.min;
                    if (e.max != null && e.min != null)
                        errors += " et inférieur à " + e.max;
                    else if (e.max != null)
                        errors += " inférieur à " + e.max;
                    errors += ".\n";
                }
            }

            // verification adresse email valide
            if ( e.name == "email" ) {
            	if ( emailCheck(e.value) == false ) {
                    errors += "- email non valide";
            	};
            }
        }
    }
    if (!empty_fields && !errors) return true;

    msg  = "_____________________________________________________  _\n\n"
    msg += "Le formulaire ne peut pas etre traité.\n";
    msg += "Corrigez les erreurs suivantes :\n";
    msg += "_____________________________________________________  _\n\n"

    if (empty_fields) {
        msg += "- Le(s) champ(s) suivants sont obligatoires :"
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    return false;
}


function sub() {
	var irc = verify(document.formOrder);
	if ( irc )
		calcul();
	return irc;
}



