function fSubmit(form) {
	if (valideForm(form)) {
		document.forms['form2'].submit();
	} else {
		alert('Le formulaire comporte des erreurs. \nVeuillez les corriger et resoumettre le formulaire.');
	}
}

function valideForm(form) {
	// nettoieForm(form);
	var indRetour = true;
	var lstSections = new Array();
	var nbSections = 0;
	var lstNiv1 = getElementsByAttribute(document, 'class', 'NIV1');

	var pattern = new RegExp("(^| )" + 'NOSEL' + "( |$)");
	var doc = '<fieldset><legend><b>D&eacute;tails du projet</b></legend><table border="0" valign="top" width="100%" cellspacing="0" cellpadding="5">';
	for ( var i = 0; i < lstNiv1.length; i++) {
		if (pattern.test(lstNiv1[i].className)) {
			s = parseSectNoSel(lstNiv1[i]);
		} else {
			s = parseSectSel(lstNiv1[i]);
		}
		lstSections[nbSections++] = s;
		var indSectValide = valideSection(s);
		if (!indSectValide) {
			addClass(document.getElementById(s.id), 'errSection');
		} else {
			removeClass(document.getElementById(s.id), 'errSection');
		}
		indRetour = indRetour && indSectValide;
		if (indRetour) {
			doc = doc + s.print() + '\n';
		}
	}
	doc = doc + '</table></fieldset>';
	// indRetour=indRetour&&valideCoord();
	var indCoordValide = valideCoord();
	if (!indCoordValide) {
		addClass(document.getElementById('C01'), 'errSection');
	} else {
		removeClass(document.getElementById('C01'), 'errSection');
		doc = doc + printCoord();
	}
	document.forms['form2'].tables.value = doc;
	return indRetour && indCoordValide;
}

function valideSection(section) {
	switch (section.id) {
	case 'G01':
		return true;
	case 'G02':
		return true;
	case 'G03':
		var indContenu = false;
		for ( var i = 0; i < section.lstBlocs.length; i++) {
			indContenu = indContenu || section.lstBlocs[i].indContenu;
		}
		return indContenu;
	case 'G04':
		// TODO Valider num&eacute;rique
		return (section.lstBlocs[0].lstItems[0].indContenu
				|| section.lstBlocs[0].lstItems[1].indContenu
				|| section.lstBlocs[0].lstItems[2].indContenu || section.lstBlocs[0].lstItems[3].indContenu);
	case 'G05':
		return true;
	case 'G06':
		if ((section.lstBlocs[0].lstItems[1].indContenu && !section.lstBlocs[0].lstItems[2].indContenu)
				|| (!section.lstBlocs[0].lstItems[1].indContenu && section.lstBlocs[0].lstItems[2].indContenu)) {
			return false;
		}
		return true;
	case 'G07':
		/*
		 * if (!section.lstBlocs[0].lstItems[0].indContenu ||
		 * !section.lstBlocs[0].lstItems[1].indContenu) { return false; } if
		 * (!section.lstBlocs[0].lstItems[2].indContenu &&
		 * !section.lstBlocs[0].lstItems[3].indContenu) { return false; }
		 */
		if ((section.lstBlocs[0].lstItems[0].indContenu && !section.lstBlocs[0].lstItems[1].indContenu)
				|| (!section.lstBlocs[0].lstItems[0].indContenu && section.lstBlocs[0].lstItems[1].indContenu)) {
			return false;
		}
		if (section.lstBlocs[0].lstItems[0].indContenu
				&& !section.lstBlocs[0].lstItems[2].indContenu
				&& !section.lstBlocs[0].lstItems[3].indContenu) {
			return false;
		}
		return true;
	case 'G08':
		return true;
	case 'G09':
		// TODO Valider num&eacute;rique
		if (section.lstBlocs[0].lstItems[0].indContenu
				&& !section.lstBlocs[0].lstItems[1].indContenu
				&& !section.lstBlocs[0].lstItems[2].indContenu) {
			return false;
		}
		if (!section.lstBlocs[0].lstItems[0].indContenu
				&& (section.lstBlocs[0].lstItems[1].indContenu || section.lstBlocs[0].lstItems[2].indContenu)) {
			return false;
		}
		return true;
	case 'G10':
		// TODO Valider num&eacute;rique
		return true;
	case 'G11':
		// TODO Valider num&eacute;rique
		return true;
	case 'G12':
		var indValide = true;
		if (section.lstBlocs.length == 1 && !section.lstBlocs[0].indContenu) {
			return false;
		}
		for ( var i = 0; i < section.lstBlocs.length; i++) {
			id = section.lstBlocs[i].id;
			if (id == 'G12O02' || id == 'G12O03' || id == 'G12O04'
					|| id == 'G12O05' || id == 'G12O06' || id == 'G12O07'
					|| id == 'G12O08' || id == 'G12O09' || id == 'G12O10'
					|| id == 'G12O11' || id == 'G12O12' || id == 'G12O13'
					|| id == 'G12O14' || id == 'G12O15' || id == 'G12O17'
					|| id == 'G12O18' || id == 'G12O19' || id == 'G12O20'
					|| id == 'G12O21' || id == 'G12O22' || id == 'G12O23'
					|| id == 'G12O31' || id == 'G12O32' || id == 'G12O33'
					|| id == 'G12O34' || id == 'G12O35') {
				indValide = indValide && section.lstBlocs[i].indContenu;
			}
		}
		return indValide;
	case 'G13':
		var indValide = true;
		if (section.lstBlocs.length == 1 && !section.lstBlocs[0].indContenu) {
			return false;
		}
		for ( var i = 0; i < section.lstBlocs.length; i++) {
			id = section.lstBlocs[i].id;
			if (id == 'G13O02' || id == 'G13O03' || id == 'G13O04' || id == 'G13O05'
					|| id == 'G13O06' || id == 'G13O07' || id == 'G13O08'
					|| id == 'G13O09') {
				indValide = indValide && section.lstBlocs[i].indContenu;
			}
		}
		return indValide;
	case 'G14':

/*		if (section.lstBlocs.length == 1 && !section.lstBlocs[0].indContenu) {
			return false;
		}*/

		return true;
	case 'G15':
/*		if (!section.indContenu) {
			return true;
		}*/
/*		c = section.lstBlocs[0].lstItems[3].indContenu
				|| section.lstBlocs[0].lstItems[4].indContenu;
		p = section.lstBlocs[0].lstItems[5].indContenu
				|| section.lstBlocs[0].lstItems[6].indContenu
				|| section.lstBlocs[0].lstItems[7].indContenu
				|| section.lstBlocs[0].lstItems[8].indContenu;*/
		var c = section.lstBlocs[0].lstItems[3].indContenu
				|| section.lstBlocs[0].lstItems[4].indContenu;
		var p = section.lstBlocs[0].lstItems[6].indContenu
				|| section.lstBlocs[0].lstItems[7].indContenu
				|| section.lstBlocs[0].lstItems[8].indContenu
				|| section.lstBlocs[0].lstItems[9].indContenu;

		if (section.lstBlocs[0].lstItems[0].indContenu
				&& section.lstBlocs[0].lstItems[1].indContenu
				&& section.lstBlocs[0].lstItems[2].indContenu && c && p) {
			return true;
		}
		if (!section.lstBlocs[0].lstItems[0].indContenu
				&& !section.lstBlocs[0].lstItems[1].indContenu
				&& !section.lstBlocs[0].lstItems[2].indContenu && !c && !p) {
			return true;
		}
		return false;
	case 'G16':
		// TODO Valider num&eacute;rique
		return true;
	case 'G17':
		// TODO Valider num&eacute;rique
		var indValide = true;
		if (section.lstBlocs.length == 1 && !section.lstBlocs[0].indContenu) {
			return false;
		}
		for ( var i = 0; i < section.lstBlocs.length; i++) {
			id = section.lstBlocs[i].id;
			if (id == 'G17O02' || id == 'G17O09' || id == 'G17O10'
					|| id == 'G17O12' || id == 'G17O13'
					|| id == 'G17O20' || id == 'G17O23' || id == 'G17O25'
					|| id == 'G17O28' || id == 'G17O38' || id == 'G17O39'
					|| id == 'G17O41' || id == 'G17O46' || id == 'G17O50') {
				indValide = indValide && section.lstBlocs[i].indContenu;
			}
			if (id == 'G17O17') {
				indValide = indValide
						&& section.lstBlocs[i].lstItems[4].indContenu
						&& (section.lstBlocs[i].lstItems[0].indContenu
								|| section.lstBlocs[i].lstItems[1].indContenu
								|| section.lstBlocs[i].lstItems[2].indContenu || section.lstBlocs[i].lstItems[3].indContenu);
			}
			if (id == 'G17O21') {
				indValide = indValide
						&& section.lstBlocs[i].lstItems[0].indContenu
						&& section.lstBlocs[i].lstItems[1].indContenu
						&& (section.lstBlocs[i].lstItems[2].indContenu || section.lstBlocs[i].lstItems[3].indContenu)
						&& section.lstBlocs[i].lstItems[4].indContenu;
			}
			if (id == 'G17O22') {
				indValide = indValide
						&& section.lstBlocs[i].lstItems[0].indContenu
						&& section.lstBlocs[i].lstItems[1].indContenu
						&& (section.lstBlocs[i].lstItems[2].indContenu || section.lstBlocs[i].lstItems[3].indContenu);
			}
			if (id == 'G17O42') {
				indValide = indValide
						&& section.lstBlocs[i].lstItems[0].indContenu
						&& (section.lstBlocs[i].lstItems[1].indContenu
								|| section.lstBlocs[i].lstItems[2].indContenu
								|| section.lstBlocs[i].lstItems[3].indContenu || section.lstBlocs[i].lstItems[4].indContenu);
			}
			if (id == 'G17O43') {
				indValide = indValide
						&& section.lstBlocs[i].lstItems[0].indContenu;
			}
			if (id == 'G17O51') {
				indValide = indValide && section.lstBlocs[i].lstItems[0].indContenu;
			}
			if (id == 'G17O52') {
				indValide = indValide
						&& section.lstBlocs[i].lstItems[0].indContenu
						&& (section.lstBlocs[i].lstItems[1].indContenu
								|| section.lstBlocs[i].lstItems[2].indContenu
								|| section.lstBlocs[i].lstItems[3].indContenu || section.lstBlocs[i].lstItems[4].indContenu);
			}
		}
		return indValide;
	case 'G18':
		var indValide = true;
		if (section.lstBlocs.length == 1 && !section.lstBlocs[0].indContenu) {
			return false;
		}
		for ( var i = 1; i < section.lstBlocs.length; i++) {
			var id = section.lstBlocs[i].id;
			if (id != 'G18O04') {
				indValide = indValide && section.lstBlocs[i].indContenu;
			}
		}
		return indValide;
	case 'G19':
		var indValide = true;
		if (section.lstBlocs.length == 1 && !section.lstBlocs[0].indContenu) {
			return false;
		}
		for ( var i = 0; i < section.lstBlocs.length; i++) {
			var id = section.lstBlocs[i].id;
			if (id == 'G19O03' || id == 'G19O04') {
				indValide = indValide && section.lstBlocs[i].indContenu;
			}
		}
		return indValide;
	case 'G20':
		// TODO Valider date
		return true;
	case 'G21':
		var indValide = true;
		if (section.lstBlocs.length == 1 && !section.lstBlocs[0].indContenu) {
			return false;
		}
		for ( var i = 0; i < section.lstBlocs.length; i++) {
			id = section.lstBlocs[i].id;
			if (id == 'G21O07') {
				indValide = indValide && section.lstBlocs[i].indContenu;
			}
		}
		return indValide;
	case 'G22':
		var indValide = true;
		if (section.lstBlocs.length == 1 && !section.lstBlocs[0].indContenu) {
			return false;
		}
		return true;
	case 'G23':
		return true;
	}
}

function valideCoord() {
	document.getElementById('C01T00').value = trim(document
			.getElementById('C01T00').value);
	document.getElementById('C01T01').value = trim(document
			.getElementById('C01T01').value);
	document.getElementById('C01T02').value = trim(document
			.getElementById('C01T02').value);
	document.getElementById('C01T03').value = trim(document
			.getElementById('C01T03').value);
	document.getElementById('C01T04').value = trim(document
			.getElementById('C01T04').value);
	document.getElementById('C01T05').value = trim(document
			.getElementById('C01T05').value);
	document.getElementById('C01T06').value = trim(document
			.getElementById('C01T06').value);
	document.getElementById('C01T07').value = trim(document
			.getElementById('C01T07').value);
	document.getElementById('C01T08').value = trim(document
			.getElementById('C01T08').value);
	document.getElementById('C01T09').value = trim(
			document.getElementById('C01T09').value).toUpperCase();

	if (document.getElementById('C01T00').value == '') {
		if (document.getElementById('C01T01').value == ''
				|| document.getElementById('C01T03').value == ''
				|| document.getElementById('C01T04').value == ''
				|| document.getElementById('C01T07').value == ''
				|| document.getElementById('C01T08').value == '') {
			return false
		}
	} else {
		if (document.getElementById('C01T00').value != '9999'
				|| document.getElementById('C01T01').value == ''
				|| document.getElementById('C01T03').value == '') {
			return false

		}
	}
/*	if (document.getElementById('C01T09').value != ''
			&& !valCodPost(document.getElementById('C01T09').value)) {
		return false;
	}*/

	if (!valCourriel(document.getElementById('C01T03').value)) {
		return false;
	}

	;
	return true;
}

function printCoord() {
	c = '<fieldset><legend><b>Coordonn&eacute;es</b></legend><table border="0" valign="top" width="100%">';
	if (document.getElementById('C01T00').value != '') {
		c = c + '<tr><td>Code client:</td><td>'
				+ document.getElementById('C01T00').value + '</td></tr>';
	}
	if (document.getElementById('C01T01').value != '') {
		c = c + '<tr><td width="265px">Nom:</td><td>'
				+ document.getElementById('C01T01').value + '</td></tr>';
	}
	if (document.getElementById('C01T02').value != '') {
		c = c + '<tr><td>Entreprise:</td><td>'
				+ document.getElementById('C01T02').value + '</td></tr>';
	}
	if (document.getElementById('C01T03').value != '') {
		c = c + '<tr><td>Courriel:</td><td>'
				+ document.getElementById('C01T03').value + '</td></tr>';
	}
	if (document.getElementById('C01T04').value != '') {
		c = c + '<tr><td>T&eacute;l&eacute;phone:</td><td>'
				+ document.getElementById('C01T04').value + '</td></tr>';
	}
	if (document.getElementById('C01T05').value != '') {
		c = c + '<tr><td>T&eacute;l&eacute;copieur:</td><td>'
				+ document.getElementById('C01T05').value + '</td></tr>';
	}
	if (document.getElementById('C01T06').value != '') {
		c = c + '<tr><td>Adresse:</td><td>'
				+ document.getElementById('C01T06').value + '</td></tr>';
	}
	if (document.getElementById('C01T07').value != '') {
		c = c + '<tr><td>Ville:</td><td>'
				+ document.getElementById('C01T07').value + '</td></tr>';
	}
	if (document.getElementById('C01T08').value != '') {
		c = c + '<tr><td>Province:</td><td>'
				+ document.getElementById('C01T08').value + '</td></tr>';
	}
	if (document.getElementById('C01T09').value != '') {
		c = c + '<tr><td>Code postal:</td><td>'
				+ document.getElementById('C01T09').value + '</td></tr>';
	}
	c = c + '</table></fieldset>'
	return c;
}

function parseSectNoSel(node) {
	var sect = new Section();
	sect.id = node.id;
	sect.nom = node.id;
	b = parseNodeNOSEL(node);
	sect.addBloc(b);
	return sect;
}

function parseNodeNOSEL(node) {
	var lstChildren = node.childNodes;
	var blocLI = new Bloc();
	blocLI.id = node.id;
	var curLabel = '';
	for ( var k = 0; k < lstChildren.length; k++) {
		switch (lstChildren[k].tagName) {
		case 'SPAN':
			blocLI.nom = lstChildren[k].firstChild.data;
			break;
		case 'LABEL':
			curLabel = lstChildren[k].firstChild.data;
			break;
		case 'TEXTAREA':
			blocLI.addItem(new ItemTxt(lstChildren[k].id, curLabel,
					lstChildren[k].value));
			break;
		case 'INPUT': {
			switch (lstChildren[k].type) {
			case 'checkbox':
				blocLI.addItem(new ItemSel(lstChildren[k].id, curLabel,
						lstChildren[k].checked));
				break;
			case 'radio':
				blocLI.addItem(new ItemSel(lstChildren[k].id, curLabel,
						lstChildren[k].checked));
				break;
			case 'text':
				blocLI.addItem(new ItemTxt(lstChildren[k].id, curLabel,
						lstChildren[k].value));
				break;
			}
			curLabel = '';

		}
		}
	}
	return blocLI;
}

function parseSectSel(node) {
	var sect = new Section();
	sect.id = node.id;
	sect.nom = node.id;
	// b = parseNodeNOSEL(node);
	// sect.addBloc(b);
	var lstNiv2 = getElementsByAttribute(node, 'class', 'NIV2');
	var x2 = 'NIV2=';
	for ( var j = 0; j < lstNiv2.length; j++) {
		x2 = x2 + lstNiv2[j].id;
		parseSection1(lstNiv2[j], sect);
	}

	var lstNiv2b = getElementsByAttribute(node, 'class', 'NIV2b');
	var x2b = 'NIV2b=';
	for ( var j = 0; j < lstNiv2b.length; j++) {
		id = lstNiv2b[j].id;
		x2b = x2b + lstNiv2b[j].id;
		parseSection2(lstNiv2b[j], sect);
	}
	return sect;
}

function parseSection1(node, sect) {
	var b = new Bloc();
	b.id = node.id;
	b = parseNodeNOSEL(node);
	b.addItem(new ItemTxt(node.id, '', node.value));
	sect.addBloc(b);
	/*
	 * var lstNiv3 = getElementsByAttribute(node, 'class', 'NIV3'); var x3 =
	 * 'NIV3(' + lstNiv3.id + ')='; for ( var k = 0; k < lstNiv3.length; k++) {
	 * x3 = x3 + '-' + lstNiv3[k].id + '=' + lstNiv3[k].selected + '=' +
	 * lstNiv3[k].checked; }
	 */
}

function parseSection2(node, sect) {
	var lstBlocsLI = node.getElementsByTagName('LI');
	var x3 = 'NIV3b(' + lstBlocsLI.id + '):';
	for ( var k = 0; k < lstBlocsLI.length; k++) {
		// id = lstBlocsLI[k].attributes['rel'].nodeValue;
		// sect.id = id;
		b = parseBlocLI(lstBlocsLI[k]);
		sect.addBloc(b);
	}

}

function parseBlocLI(node) {
	var lstChildren = node.childNodes;
	var blocLI = new Bloc();
	blocLI.id = node.attributes['rel'].nodeValue;
	var curLabel = '';
	for ( var k = 0; k < lstChildren.length; k++) {
		switch (lstChildren[k].tagName) {
		case 'SPAN':
			blocLI.nom = lstChildren[k].firstChild.data;
			break;
		case 'LABEL':
			curLabel = lstChildren[k].firstChild.data;
			break;
		case 'TEXTAREA':
			blocLI.addItem(new ItemTxt(lstChildren[k].id, curLabel,
					lstChildren[k].value));
			break;
		case 'INPUT': {
			switch (lstChildren[k].type) {
			case 'checkbox':
				blocLI.addItem(new ItemSel(lstChildren[k].id, curLabel,
						lstChildren[k].checked));
				break;
			case 'radio':
				blocLI.addItem(new ItemSel(lstChildren[k].id, curLabel,
						lstChildren[k].checked));
				break;
			case 'text':
				blocLI.addItem(new ItemTxt(lstChildren[k].id, curLabel,
						lstChildren[k].value));
				break;
			}
			curLabel = '';

		}
		}
	}
	if (blocLI.nbItems == 0) {
		blocLI.indContenu = true;
	}
	return blocLI;
}

function getElementsByAttribute(node, attribute, attributeValue) {
	var elementArray = new Array();
	var matchedArray = new Array();

	if (node.all) {
		elementArray = node.all;
	} else {
		elementArray = node.getElementsByTagName("*");
	}

	for ( var i = 0; i < elementArray.length; i++) {
		if (attribute == "class") {
			var pattern = new RegExp("(^| )" + attributeValue + "( |$)");

			if (pattern.test(elementArray[i].className)) {
				matchedArray[matchedArray.length] = elementArray[i];
			}
		} else if (attribute == "for") {
			if (elementArray[i].getAttribute("htmlFor")
					|| elementArray[i].getAttribute("for")) {
				if (elementArray[i].htmlFor == attributeValue) {
					matchedArray[matchedArray.length] = elementArray[i];
				}
			}
		} else if (elementArray[i].getAttribute(attribute) == attributeValue) {
			matchedArray[matchedArray.length] = elementArray[i];
		}
	}

	return matchedArray;
}
function addClass(target, classValue) {
	var pattern = new RegExp("(^| )" + classValue + "( |$)");

	if (!pattern.test(target.className)) {
		if (target.className == "") {
			target.className = classValue;
		} else {
			target.className += " " + classValue;
		}
	}

	return true;
}

function removeClass(target, classValue) {
	var removedClass = target.className;
	var pattern = new RegExp("(^| )" + classValue + "( |$)");

	removedClass = removedClass.replace(pattern, "$1");
	removedClass = removedClass.replace(/ $/, "");

	target.className = removedClass;

	return true;
}
