// Fichier verifications.js
// 
// Historique (chronologie inverse)
//
// 11/11/2007	PLJ	Création
//
// Ce fichier fournit des fonctions de vérification de la saisie dans les formulaires
// Chaque type de formulaire est représenté par une classe, 
// la vérification se fait à l'aide de méthodes de ces classes
// 

// Expressions régulières

var expEntier = /^(\-)?([0-9]+)$/g;
var expEntierPos = /^([0-9]+)$/g;
var expCouleur = /^(([0-9A-F]{2}){3,4})$/gi;
var expReel = /^(\-)?(([0-9]+(\.|,)?[0-9]*)|((\.|,)[0-9]+))$/g;


// Vérification du type entier

function FormEntier(nEntier,cEntier)
{
	this.entier = nEntier;
	this.champ = cEntier;
	
	this.incr = incrEntier;
	this.decr = decrEntier;
	this.check = checkEntier;
}

function incrEntier()
{
	this.entier++;
	this.champ.value = this.entier;
}

function decrEntier()
{
	this.entier--;
	this.champ.value = this.entier;
}

function checkEntier()
{
	if(this.champ.value.match(expEntier) != null)
		this.entier = this.champ.value;
	else
		this.champ.value = this.entier;
}


// Vérification du type durée en secondes

function FormDuree(nHeures,nMinutes,nSecondes,cHeures,cMinutes,cSecondes,cDuree)
{
	this.heures = nHeures;
	this.minutes = nMinutes;
	this.secondes = nSecondes;
	this.total = nHeures*3600 + nMinutes*60 + nSecondes;
	this.champHeures = cHeures;
	this.champMinutes = cMinutes;
	this.champSecondes = cSecondes;
	this.champ = cDuree;
	
	this.incr = incrDuree;
	this.decr = decrDuree;
	this.check = checkDuree;
	this.checkIncr = checkIncrDuree;
	this.checkDecr = checkDecrDuree;
	this.setForm = setDuree;
}

function incrDuree(champ)
{
	if(champ == 'h')
		this.heures++;
	else if(champ == 'm')
		this.minutes++;
	else if(champ == 's')
		this.secondes++;
	this.checkIncr();
	this.setForm();
}

function decrDuree(champ)
{
	if(champ == 'h')
		this.heures--;
	else if(champ == 'm')
		this.minutes--;
	else if(champ == 's')
		this.secondes--;
	this.checkDecr();
	this.setForm();
}

function setDuree()
{
	this.champSecondes.value = this.secondes;
	this.champMinutes.value = this.minutes;
	this.champHeures.value = this.heures;
	this.total = this.secondes + this.minutes*60 + this.heures*3600;
	this.champ.value = this.total;
}

function checkIncrDuree()
{
	while(this.secondes >= 60)
	{
		this.secondes -= 60;
		this.minutes++;
	}
	while(this.minutes >= 60)
	{
		this.minutes -= 60;
		this.heures++;
	}
	if(this.heures >= 1000000)
	{
		this.heures = 1000000;
		this.minutes = 0;
		this.secondes = 0;
	}
}

function checkDecrDuree()
{
	if(this.secondes < 0)
		if((this.minutes > 0) || (this.heures > 0))
		{
			this.secondes = 59;
			this.minutes--;
		}
		else
			this.secondes = 0;
	if(this.minutes < 0)
		if(this.heures > 0)
		{
			this.minutes = 59;
			this.heures--;
		}
		else
			this.minutes = 0;
	if(this.heures < 0)
		this.heures = 0;
}

function checkDuree()
{
	if(this.champSecondes.value.match(expEntierPos)!=null)
		this.secondes = this.champSecondes.value;
	if(this.champMinutes.value.match(expEntierPos)!=null)
		this.minutes = this.champMinutes.value;
	if(this.champHeures.value.match(expEntierPos)!=null)
		this.heures = this.champHeures.value;
	this.checkIncr();
	this.setForm();
}


// Vérification du type micro-durée en µs

function FormMicroDuree(nSecondes,nMilli,nMicro,cSecondes,cMilli,cMicro,cDuree)
{
	this.secondes = nSecondes;
	this.milli = nMilli;
	this.micro = nMicro;
	this.total = nSecondes*1000000 + nMilli*1000 + nMicro;
	this.champSecondes = cSecondes;
	this.champMilli = cMilli;
	this.champMicro = cMicro;
	this.champ = cDuree;
	
	this.incr = incrMicroDuree;
	this.decr = decrMicroDuree;
	this.check = checkMicroDuree;
	this.checkIncr = checkIncrMicroDuree;
	this.checkDecr = checkDecrMicroDuree;
	this.setForm = setMicroDuree;
}

function incrMicroDuree(champ)
{
	if(champ == 's')
		this.secondes++;
	else if(champ == 'm')
		this.milli++;
	else if(champ == 'µ')
		this.micro++;
	this.checkIncr();
	this.setForm();
}

function decrMicroDuree(champ)
{
	if(champ == 's')
		this.secondes--;
	else if(champ == 'm')
		this.milli--;
	else if(champ == 'µ')
		this.micro--;
	this.checkDecr();
	this.setForm();
}

function setMicroDuree()
{
	this.champSecondes.value = this.secondes;
	this.champMilli.value = this.milli;
	this.champMicro.value = this.micro;
	this.total = this.micro + this.milli*1000 + this.secondes*1000000;
	this.champ.value = this.total;
}

function checkIncrMicroDuree()
{
	while(this.micro >= 1000)
	{
		this.micro -= 1000;
		this.milli++;
	}
	while(this.milli >= 1000)
	{
		this.milli -= 1000;
		this.secondes++;
	}
	if(this.secondes >= 10000)
	{
		this.secondes = 10000;
		this.milli = 0;
		this.micro = 0;
	}
}

function checkDecrMicroDuree()
{
	if(this.micro < 0)
		if((this.milli > 0) || (this.secondes > 0))
		{
			this.micro = 999;
			this.milli--;
		}
		else
			this.micro = 0;
	if(this.milli < 0)
		if(this.secondes > 0)
		{
			this.milli = 999;
			this.secondes--;
		}
		else
			this.milli = 0;
	if(this.secondes < 0)
		this.secondes = 0;
}

function checkMicroDuree()
{
	if(this.champSecondes.value.match(expEntierPos)!=null)
		this.secondes = this.champSecondes.value;
	if(this.champMilli.value.match(expEntierPos)!=null)
		this.milli = this.champMilli.value;
	if(this.champMicro.value.match(expEntierPos)!=null)
		this.micro = this.champMicro.value;
	this.checkIncr();
	this.setForm();
}


// Vérification du type cardinalité

function FormCardinalite(nCardinalite,cCardinalite,cAffCardinalite)
{
	this.entier = nCardinalite;
	this.champ = cCardinalite;
	this.champAff = cAffCardinalite;
	
	this.incr = incrCardinalite;
	this.decr = decrCardinalite;
	this.check = checkCardinalite;
	this.set = setCardinalite;
}

function incrCardinalite()
{
	this.entier++;
	this.champ.value = this.entier;
	if(this.entier==-1)
		this.champAff.value = 'n';
	else
		this.champAff.value = this.entier;
}

function decrCardinalite()
{
	if(this.entier==-2)
		return;
		
	this.entier--;
	this.champ.value = this.entier;
	if(this.entier==-1)
		this.champAff.value = 'n';
	else if(this.entier==-2)
		this.champAff.value = 'i';
	else
		this.champAff.value = this.entier;

}

function getCardinaliteAff(entier)
{
	if(entier==-1)
		return 'n';
	if(entier<-1)
		return 'i';
	return entier;
}

function checkCardinalite()
{
	if(this.champAff.value.match(expEntierPos) != null)
		this.entier = this.champ.value = this.champAff.value;
	else if(this.champAff.value=='n')
		this.champ.value = this.entier = -1;
	else if(this.champAff.value=='i')
		this.champ.value = this.entier = -2;
	else
		this.champAff.value = getCardinaliteAff(this.entier);
}

function setCardinalite(nEntier)
{
	this.entier = nEntier;
	this.champ.value = nEntier;
	this.champAff.value = getCardinaliteAff(nEntier);
}


// Vérification du type réel

function FormReel(nReel,cReel)
{
	this.reel = nReel;
	this.champ = cReel;
	
	this.incr = incrReel;
	this.decr = decrReel;
	this.check = checkReel;
}

function incrReel()
{
	this.reel++;
	this.champ.value = this.reel;
}

function decrReel()
{
	this.reel--;
	this.champ.value = this.reel;
}

function checkReel()
{
	if(this.champ.value.match(expReel) != null)
	{
		this.champ.value = this.champ.value.replace(',','.');
		this.reel = this.champ.value;
	}
	else
		this.champ.value = this.reel;
}


// Vérification d'une couleur en hexadécimal

function FormCouleur(nCouleur,cCouleur)
{
	this.couleur = nCouleur;
	this.champ = cCouleur;
	
	this.check = checkCouleur;
}

function checkCouleur()
{
	if(this.champ.value.match(expCouleur) != null)
	{
		this.champ.value = this.champ.value.toUpperCase();
		this.couleur = this.champ.value;
	}
	else
		this.champ.value = this.couleur;
}

// Surveillance des données rédactionnelles

function checkRedactionnel(id)
{
	var texte = document.getElementById('texte'+id);
	var check = document.getElementById('check'+id);
	
	if(texte.value=='')
		check.value='';
	else
		check.value='OK';
}

function toLowerCaseRedactionnel(id)
{
	var texte = document.getElementById('texte'+id);
	texte.value=texte.value.toLowerCase();
}

function toUpperCaseRedactionnel(id)
{
	var texte = document.getElementById('texte'+id);
	texte.value=texte.value.toUpperCase();
}

function firstUpperCaseRedactionnel(id)
{
	var texte = document.getElementById('texte'+id);
	
	if(texte.value=='')
		return;
	
	var firstLetter = texte.value.substring(0, 1)
	var reste = texte.value.substring(1, texte.value.length)
	texte.value=firstLetter.toUpperCase() + reste.toLowerCase();
}

// Surveillance deds uploads de fichiers

function checkUpload(id)
{
	var file = document.getElementById('file'+id);
	var exist = document.getElementById('exist'+id);
	var check = document.getElementById('check'+id);
	var keep = document.getElementById('keep'+id);
	
	if(file.value=='' && exist.value==0)
		check.value='';
	else
		check.value='OK';
	
	if(exist.value==1 && file.value!='')
		keep.checked=false;
		
	if(exist.value==1)
		check.value='OK';
}
