var NAME = 0
var TYPE = 1;
var REQ = 2;
var MIN = 3;
var MAX = 4;
var CHR = 5;
var INTITULE = 6;

var CHR_ALL = 0;
var CHR_NUM = 1;
var CHR_ALPHA = 2;
var CHR_ALPHANUM = 3;
var CHR_MAIL = 4;
var CHR_IDENTIFIANT = 5;
var CHR_DATE = 6;

var arrayHelp = [];
var arrayErrorMessage = [];

var erreurMessage = '';
var hasSubmitted = false;

function init() {
}

function displayHelp(parId) {
}

function hideHelp(parId) {
}

isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;

function getXMLHttpRequest() {
	var XMLHttp = false;
	try{
		XMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			XMLHttp = false;
		}
	}
	if(!XMLHttp && typeof XMLHttpRequest!='undefined') {
		XMLHttp = new XMLHttpRequest();
	}
	return XMLHttp;
}
 
 

function switchDiv(strDivName,bolVisible){
	if (isNS4) {
		objElement = document.layers[strDivName];
	} else if (isIE4) {
		objElement = document.all[strDivName];
	} else if (isIE5 || isNS6) {
		objElement = document.getElementById(strDivName);
	}
	if(isNS4){
		if(!bolVisible) {
			objElement.visibility ="hidden"
		} else {
			objElement.visibility ="visible"
		}
	}else{
		if (objElement) {
			if(!bolVisible) {
				objElement.style.visibility = "hidden";
			} else {
				objElement.style.visibility = "visible";
			}
		}
	}
}
//retrocompatibilité
function checkValue(tabParam, paramValue) { 
	return isParamValid(tabParam, paramValue);
}

function isParamValid(tabParam, paramValue) {
	errorMsgParam = '';
	res = true;

	if (tabParam[REQ] == 1 && paramValue == '') {
		errorMsgParam = 'Le champ "' + tabParam[INTITULE] + '" doit &ecirc;tre renseign&eacute;';
		res = false;
	}

	if (tabParam[MIN] != '' && paramValue != '' && paramValue.length < tabParam[MIN]) {
		errorMsgParam = 'Le champ "' + tabParam[INTITULE] + '" doit contenir au moins ' + tabParam[MIN] + ' caract&egrave;res';
		res = false;
	}

	if (tabParam[MAX] != '' && paramValue != '' && paramValue.length > tabParam[MAX]) {
		errorMsgParam = 'Le champ "' + tabParam[INTITULE] + '" doit contenir au plus ' + tabParam[MAX] + ' caract&egrave;res';
		res = false;
	}

	if (!res) {
		arrayErrorMessage['par' + tabParam[NAME]] = errorMsgParam;
		return false;
	} 
	
	if (tabParam[CHR] != '') {
		switch (tabParam[CHR]) {
			case '1':
			res = isNum(paramValue);
			break;
			case '2':
			res = isAlpha(paramValue);
			break;
			case '3':
			res = isAlphaNum(paramValue);
			break;
			case '4':
			res = true;			

			if (paramValue != '' && !isValideMail(paramValue)) {
				arrayErrorMessage['par' + tabParam[NAME]] = 'Le champ "' + tabParam[INTITULE] + '" n\'est pas correct';
				return false;
			}
			break;
			case '5':
			res = isIdentifiant(paramValue);
			break;
			case '6':
			res = isDate(paramValue);

			break;
		}
	}
	
	if (tabParam[NAME] == '2001') {
		var m1 = document.getElementById('par2001');
		var mc = document.getElementById('par2001_confirmation');
		if (mc != null && m1 != null && mc.value != '') {
			if (mc.value != m1.value != '') {				
				arrayErrorMessage['par2001'] = 'L\'email et sa confirmation doivent être identiques';
				return false;
			}
		}		
	}
	
	
	if (tabParam[NAME] == 'SECURISATION') {
		var p = document.getElementById('par10001');
		if (p != null && p.value == value) {
			arrayErrorMessage['par' + tabParam[NAME]] = 'Le pseudo et le mot de passe doivent être différents';
			return false;
		}
	}
	
	
	if (tabParam[NAME] == 'SECURISATION_CONFIRM' && document.getElementById('parSECURISATION').value != value) {		
		arrayErrorMessage['par' + tabParam[NAME]] = 'Le mot de passe et le mot de passe de confirmation doivent être identiques';
		return false;
	}


	/*if (!res) {
		arrayErrorMessage['par' + tabParam[NAME]] = 'Le champ "' + tabParam[INTITULE] + '" contient des caractères invalides';
		return false;
	}*/

	return true;
}

function validForm(formParams) { 
	if(document.getElementById('petit_points')) {
		document.getElementById('petit_points').style.display = 'none';

	}
	if(document.getElementById('aide_float')) {
		document.getElementById('aide_float').style.display = 'none';
	}
	//closeAllOpenedPopup();
	return validFormFields(formParams);
}

function validFormFields(formParams) {
	var isValid = null;
	var isOK = true;
	
	tabErrorsMessages = [];
	arrayErrorMessage = [];

	nbErreurs = 0;		
	var p = null;
	
	for(i = 0; i < tabParamsToCheck.length; i++) {	
		value = getValue(formParams, tabParamsToCheck[i][NAME], tabParamsToCheck[i][TYPE]);
		if (tabParamsToCheck[i][NAME] == '10001') {
			p = value;
		}

		isValid = isParamValid(tabParamsToCheck[i], value);
		if (!isValid) {			
			nbErreurs++;
			colorField(tabParamsToCheck[i][NAME], tabParamsToCheck[i][TYPE], true, nbErreurs == 1);
		} else {
			colorField(tabParamsToCheck[i][NAME], tabParamsToCheck[i][TYPE], false, false);
		}
		if (!isValid) {
						isOK = false;
		}
	}	


	if (!isOK || (p != null && !checkPseudo(p))) {
		return false;
	}

	if (typeof validFormSpecific != 'undefined') {
		return validFormSpecific(formParams);
	}	
		
	return true;
}	

var pseudoChecked = {};
function checkPseudo(p) {
	if (typeof(pseudoChecked[p]) == 'undefined') {	
		var xmlhttp = getXMLHttpRequest();
		if (typeof unvId == 'undefined') {
			unvId = 1;
		}
		var url = '/src/checkPseudo.php?p=' + p + '&u=' + unvId;
				
		xmlhttp.open("GET", url, false);
	  	xmlhttp.send(null);
		
	  	
	  	try {			  	
	  		eval('var result = ' + xmlhttp.responseText + ';');  	
	  	} catch(e) {	  		
	  		return true;
	  	}
	  	pseudoChecked[p] = result;
	} else {
		var result = pseudoChecked[p];
	}
  	
	if (!result.success) {	
		
		arrayErrorMessage['par10001'] = result.msg;
		
		focusMe(null, '10001', 1);		
		return false;
	}	
	
	var i = document.createElement('input');
	i.setAttribute('type', 'hidden');
	i.setAttribute('name', 'challenge');
	i.setAttribute('value', result.challenge);
	document.getElementById('myform').appendChild(i);	
	
	return true;
}

function disableSubmit() {
	if (document.getElementById("logo_univers")) {
		document.getElementById("logo_univers").style.filter = "gray(1)";
	}

	if (document.getElementById("validation")) {
		document.getElementById("validation").style.cursor = "wait";
		if (document.getElementById("validation_disable").outerHTML) {
			document.getElementById("validation").innerHTML = document.getElementById("validation_disable").outerHTML;
		} else {
			document.getElementById("validation").innerHTML = 'En cours de chargement';
		}
	}
}


function colorField(paramId, paramType, errorColor, focusMe) {
	paramName = 'par' + paramId;
	colorClass = 'erreurSaisie';
	if (document.getElementById('libelle_' + paramId)) {

		curClassName = document.getElementById('libelle_' + paramId).className;
		document.getElementById('libelle_' + paramId).className = curClassName.replace(' '+colorClass, '');

		if (errorColor) {
			 document.getElementById('libelle_' + paramId).className += ' '+colorClass
		}
	}
	if (document.getElementById('label_' + paramId)) {
		curClassName = document.getElementById('label_' + paramId).className;
		document.getElementById('label_' + paramId).className = curClassName.replace(' '+colorClass, '');

		if (errorColor) {
			 document.getElementById('label_' + paramId).className += ' '+colorClass;
		}
	}

	switch (paramType) {
		case '1':
		case '3':
		case '6':
		case '4':
		case '9':
		if (document.getElementById(paramName)) {
			document.getElementById(paramName).className = document.getElementById(paramName).className.replace(' '+colorClass, '');
			if (errorColor) {
				document.getElementById(paramName).className += ' '+colorClass;
				if (focusMe) {
					document.getElementById(paramName).focus();
				}
			}
		}
		break;
		case '2':
		case '5':
		if (document.getElementById(paramName+'_1')) {
			document.getElementById(paramName+'_1').className = document.getElementById(paramName+'_1').className.replace(' '+colorClass, '');
			if (errorColor) {
				document.getElementById(paramName+'_1').className += ' '+colorClass;
				if (focusMe) {
					document.getElementById(paramName+'_1').focus();
				}
			}
		}
		if (document.getElementById(paramName+'_2')) {
			document.getElementById(paramName+'_2').className = document.getElementById(paramName+'_2').className.replace(' '+colorClass, '');
			if (errorColor) {
				document.getElementById(paramName+'_2').className += ' '+colorClass;
			}
		}
		if (document.getElementById(paramName+'_3')) {
			document.getElementById(paramName+'_3').className = document.getElementById(paramName+'_3').className.replace(' '+colorClass, '');
			if (errorColor) {
				document.getElementById(paramName+'_3').className += ' '+colorClass;
			}
		}

		break;
	}
}

function getElement(paramName) {
	if (document.getElementById(paramName)) {
		return document.getElementById(paramName).value;
	} else {
		return eval('document.forms[0].' + paramName + '.value');
	}
}

function getValue(formParams, paramName, paramType) {
	paramName = 'par' + paramName;
	value = '';

	switch (paramType) {
		case '1':
		case '3':
		case '6':
		case '7':
		case '9':
		if (document.getElementById(paramName)) {
			value = document.getElementById(paramName).value;
		}
		break;
		case '2':
		if (document.getElementById(paramName + '_1') && document.getElementById(paramName + '_2')) {
			value = document.getElementById(paramName + '_1').value + document.getElementById(paramName + '_2').value;
		}
		break;
		case '4':
		if (document.getElementById(paramName)) {
			if (document.getElementById(paramName).checked) {
				value = '1';
			}
		}
		break;
		case '5':
		if (document.getElementById(paramName + '_1') && document.getElementById(paramName + '_2') && document.getElementById(paramName + '_3')) {
			value = document.getElementById(paramName + '_1').value + '/' + document.getElementById(paramName + '_2').value + '/' + document.getElementById(paramName + '_3').value;
			if(value == "//") {
				value = "";
			}
		}
		break;
		case '8':
		if (document.all) {
			tabRadio = eval('document.all.' + paramName);
			for(k = 0; k < tabRadio.length; k++) {
				if (tabRadio[k].checked) {
					value = '1';
				}
			}
		} else {
			value = '1';
		}
		break;
	}

	return value;
}

function isNum(paramValue) {
	if (isNaN(Number(paramValue))) {
		return false;
	} else {
		return true;
	}
}

function isAlpha(paramValue) {
	reAlpha = new RegExp('^[a-zA-Z]*$');
	return reAlpha.test(paramValue);
}

function isAlphaNum(paramValue) {
	reAlphaNum = new RegExp('^[a-zA-Z0-9]*$');
	return reAlphaNum.test(paramValue);
}

function isIdentifiant(paramValue) {
	reIdentifiant = new RegExp('^[a-zA-Z0-9-_\.]*$');
	return reIdentifiant.test(paramValue);
}


function isValideMail(paramValue) {
	

	var reMailForm = new RegExp('^([0-9a-z]+[-._+&])*[0-9a-z]+@([-0-9a-z]+[.])+[a-z]{2,6}$', 'i');
	
	if (!reMailForm.test(paramValue)) {
		return false;
	}
	return true;	
}

function isDate(paramValue) {

	if(paramValue == "") {
		return true;
	}

	tabParamValue = paramValue.split('/');
	if (!isNum(tabParamValue[2]) || !isNum(tabParamValue[1]) || !isNum(tabParamValue[0])) {
		return false;
	}
	dateCourante = new Date(tabParamValue[2], tabParamValue[1] - 1, tabParamValue[0]);
	paramValue = (tabParamValue[0]-0) + '/' + (tabParamValue[1]-0) + '/' + (tabParamValue[2]-0);
	dateCalc = dateCourante.getDate() + '/' + (dateCourante.getMonth() + 1) + '/' + dateCourante.getFullYear();
	if (dateCalc == paramValue) {
		return true;
	} else {
		return false;
	}
}

var tabParamsToCheck = [];

function addParamToCheck(name, type, required, minLength, maxLength, charset, intitule) {
	tabParamsToCheck[tabParamsToCheck.length] = [name, type, required, minLength, maxLength, charset, intitule];
}