var phl_script;
var phl_msg=new Array();
var phl_drag;
var phl_lock,phl_imgLock,phl_screen;
var phl_fmtDate='dd/mm/yyyy';
var phl_fmtTime='hh:mm:ss';
var phl_cl,phl_blur=false;
var phl_returnValue,phl_tModal=new Array(),phl_numModal=0;
var phl_ie=true;

// upload
function phl_uploadFile(obj){
	phl_openModal('php400_files/php/phl_upload.php','width=450 height=220 top=10 left=10','phl_uploadReturn("'+obj.id+'")');
}

// upload retour
function phl_uploadReturn(idw){
	if(phl_returnValue==null) return;
	var obj=document.getElementById(idw);
	obj.value=phl_returnValue;
}


function phl_openModal(url,wparam,script){
	while(true){
		new phl_modal(url,wparam,script,window);break;
	}
//	phl_blockModal();
}

function phl_modal(url,wparam,script,win){
	var buf='',obj;
	phl_tModal.push(this);
	phl_numModal++;
	this.objDIV='';
	this.img='';
	this.num=phl_numModal;
	this.script=script;
	this.win=win;

	function getNum(name){
		var config=new RegExp(name+"=([^,]+)", "i");
		return (config.test(wparam))? parseInt(RegExp.$1) : 0;
	}

	obj=document.createElement('DIV');
	obj.className='phl_windowBox';
	obj.style.zIndex='9999999';
	this.objDIV=document.body.appendChild(obj);
	this.moveTo(getNum('left'),getNum('top'));

	buf+='<div style="position:absolute;width:100%" class="phl_windowTitlebar" onmousedown="phl_dragWinStart(this.parentNode,event)"><span></span>';
	if(getNum('noclose')!='1') buf+='<img phl_type="winclose" onclick="phl_closeModal()" onmousedown="phl_chgImg(event);event.cancelBubble=true;" onmouseup="phl_chgImg(event)"  onmouseover="phl_chgImg(event)" onmouseout="phl_chgImg(event)" src="/php400/images/window_close.gif" style="position:absolute;cursor:pointer">';
	buf+='</div>';
	buf+='<iframe class="phl_windowScreen" frameborder=0 style="position:relative;width:100%;height:100%;overflow:auto;" src="'+url+'"></iframe>';
	
	buf+='<div style="position:relative;width:100%" class="phl_windowStatus">';
	if(getNum('noresize')!='1') buf+='<img phl_type=winresize ondrag="return false" src="/php400/images/window_resize.gif" style="position:absolute;cursor:SE-resize;overflow:hidden;" onmousedown="phl_resizeWinStart(this.parentNode.parentNode,event)">';
	buf+='</div>';
	this.objDIV.innerHTML=buf;	

	this.resizeTo(getNum('width'),getNum('height'));
}
	phl_modal.prototype.moveTo=function(x,y){
		this.objDIV.style.left=x;
		this.objDIV.style.top=y;
	}

	phl_modal.prototype.resizeTo=function(w,h){
		this.objDIV.style.width=Math.max(w,0);
		this.objDIV.style.height=Math.max(h,0);
		phl_modalResize(this.objDIV);
	}

	phl_modal.prototype.close=function(){
		phl_removeNode(this.objDIV);
	}




function phl_modalResize(win){
	var obj,w,img,h,i;
	obj=win.childNodes;
	w=win.clientWidth;
	obj[2].style.width=w;
	h=win.offsetHeight-obj[0].offsetHeight-obj[2].offsetHeight;
	if(!phl_ie)h-=6;
	obj[1].style.height=h;
	img=obj[0].childNodes;
	for(i=0;i<img.length;i++){
		switch(img[i].getAttribute('phl_type')){
			case 'winclose' :
				img[i].style.left=obj[2].offsetWidth-img[i].offsetWidth-2;
				img[i].style.top=0;
				break;
		}
	}	
	img=obj[2].childNodes;
	for(i=0;i<img.length;i++){
		switch(img[i].getAttribute('phl_type')){
			case 'winresize' :
				img[i].style.left=obj[2].offsetWidth-img[i].offsetWidth-2;
				img[i].style.top=obj[2].offsetHeight-img[i].offsetHeight-2;
				break;
		}
	}
}




// analyse les attributs d'une zone
function phl_anzAttr(fld){
	fld.value=phl_trim(fld.value);
	if(fld.getAttribute('UC')!=null) fld.value=fld.value.toUpperCase();
	if(fld.getAttribute('LC')!=null) fld.value=fld.value.toLowerCase();
	if(fld.getAttribute('UCF')!=null) fld.value=fld.value.charAt(0).toUpperCase()+fld.value.substr(1).toLowerCase();
}

// verif RIB
function phl_checkRIB(bqe,gui,cpt,key){
	var a,tab,tab1,cp;
	if(bqe=='' && gui=='' && cpt=='' && key=='') return true;
	tab="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	tab1="123456789123456789234567890123456789".split("");
	while (cpt.match(/\D/) !=null){
		cpt=cpt.replace(/\D/, tab1[tab.indexOf(cpt.match(/\D/))]);
	}
	cp=parseInt(cpt,10);
	a=bqe%97;
	a=a*100000+parseInt(gui,10);
	a=a%97;
	a=a*Math.pow(10,11)+cp;
	a=a%97;
	a=a*100;
	a=a%97;
	a=97-a;
	return (a==key); 
}

// initialise unz zone date
function phl_dateInit(name){
	var w,h,img,obj=document.forms[0][name];
	phl_setDateScreen(obj);
	obj=obj.nextSibling;
	obj.maxLength=phl_fmtDate.length;
	obj.size=phl_fmtDate.length;
	if(!obj.readOnly){
	 img=obj.nextSibling;
	 img.style.left='-17px';
	 img.style.top='+1px';
	}
}

// zone en erreur
function phl_err(obj,txt){
	// obj.className+=' phl_err';
	// if(!phl_blur){
		obj.className+=' phl_err';
		alert(txt);
		phl_setFocus(obj);
	// }
}


// zone en erreur avec n° de message
function phl_errMsg(obj,num,v1,v2){
	phl_err(obj,phl_rtvMsg(num,v1,v2));
}


// positionne sur la première zone du formulaire
function phl_formFocus(){
	var doc,i,list;
	doc=document.forms[0];
	if(doc==null) return;
	for(i=0;i<doc.length;i++){
		if(doc[i].type!='text' && doc[i].tagName!='TEXTAREA') continue;
		try{doc[i].focus();break;} catch(e){}
	}
	
}


//event focusin
function phl_focusIn(obj){
	if(obj.className.indexOf('phl_err')==-1) obj.className+=' phl_inputFocus';
}

//event focusout
function phl_focusOut(obj){
	phl_anzAttr(obj);
	// phl_blur=true;
	// phl_valid(obj);
	// phl_blur=false;
}


// appuie sur un bouton radio
function phl_radioClick(radio){
	var name=radio.name.substr(5);
	radio.form[name].value=radio.value;
}

// initialise un bouton radio
function phl_radioInit(nameRadio,num){
	if(num==null||num==''||num<1) num=1;
	radioList=document.forms[0][nameRadio];
	if(radioList==null) return;
	if(num>radioList.length) num=1;
	num--;
	radioList[num].checked=true;
}

// focus sur une zone
function phl_setFocus(obj){
	try{obj.focus();} catch(e){}
}
	
// verifie une date de saisie et met à jour la zone date cachée
function phl_setScreenDate(fld){
	var dt,s,d,m,y,dateout;
	if(fld.type=='text') fld=fld.previousSibling;
	fldInput=fld.nextSibling;
	date=phl_trim(fldInput.value);
	if(date!=''){
		var regdate=new RegExp('((0[1-9])|([1-2][0-9])|(3[0-1]))((0[1-9])|(1[0-2]))([0-9][0-9][0-9][0-9])');
		var regdate2=new RegExp('([0-9][0-9][0-9][0-9])((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))');
		dt=date.replace(/\D/g,'');
		if(dt.length<5 || dt.length>8) return false;
		if(dt.length==5 || dt.length==7)dt='0'+dt;
		if(dt.length==6){if(new Number(dt.substr(4,2))<48)s='20'; else s='19';dt=dt.substr(0,4)+s+dt.substr(4,2);}
		if(regdate.test(dt)==false) return false;
		d=dt.substr(0,2);m=dt.substr(2,2);y=dt.substr(4,4);
		if(y=='0000') return false;
		if((m==4||m==6||m==9||m==11)&&d==31) return false;
		if(m==2 && (d>29 || ((y%4)!=0 && d>28)))return false;
	}
	else{
		y='0001';m='01';d='01';
	}
	fldInput.value=phl_cvtDateScreen(y+m+d,'yyyymmdd');
	fld.value=phl_cvtScreenDate(fldInput.value,fld.getAttribute('phl_fmt'));
	if(fldInput.value=='01/01/0001') fldInput.value='';
	return true;
}

// verifie une heure de saisie et met à jour la zone date cachée
function phl_setScreenTime(fld){
	var dt,h,m,s,fldInput,time,t;
	if(fld.type=='text') fld=fld.previousSibling;
	fldInput=fld.nextSibling;
	time=phl_trim(fldInput.value);
	if(time!=''){
		h=0;m=0;s=0;
		t=time.split(':');
		if(t.length>=1) h=new Number(t[0]);
		if(t.length>=2) m=new Number(t[1]);
		if(t.length>=3) s=new Number(t[2]);
		if(h>23 || m>59 || s>59) return false;
	}
	else{
		h=0;m=0;s=0;
	}
	if(h<10) h='0'+h;
	if(m<10) m='0'+m;
	if(s<10) s='0'+s;
	h=new String(h);
	m=new String(m);
	s=new String(s);
	fldInput.value=phl_cvtTimeScreen(h+m+s,'hhmmss');
	fld.value=phl_cvtScreenTime(fldInput.value,fld.getAttribute('phl_fmt'));
	if(fldInput.value=='00:00:00') fldInput.value='';
	return true;
}

// initialise une zone heure
function phl_timeInit(name){
	var w,h,img,obj=document.forms[0][name];
	phl_setTimeScreen(obj);
	obj=obj.nextSibling;
	obj.maxLength=phl_fmtTime.length;
	obj.size=phl_fmtTime.length;
	if(!obj.readOnly){
	 img=obj.nextSibling;
	 img.style.left='-17px';
	 img.style.top='+1px';
	}
}

// enlève les blancs à droite et à gauche
function phl_trim(s){
	return s.replace(/\s*$/,"").replace(/^\s*/,"");
}
 
// enleve les blancs à gauche
function phl_triml(s){
	return s.replace(/^\s*/,"");
}
 
// enleve les blancs à droite 
function phl_trimr(s){
	return s.replace(/\s*$/,"");
}

// valide une zone en fonction de ses attributs
function phl_valid(fld){
	var len,fmt,dec=0,m,re,r,v=fld.value,ret,inputFld;
	var re=new RegExp;
	if(fld.getAttribute('RQ')!=null && v==''){
		phl_errMsg(fld,1);
		return false;
	}
	if(fld.getAttribute('NUM')!=null){
		fld.value=fld.value.replace(',','.');
		fld.value=fld.value.replace('+','');		
		if(fld.value.indexOf('-')!=-1){fld.value='-'+fld.value.replace('-','');v=fld.value;}
		v=fld.value;
		fmt='';
		len=parseInt(fld.getAttribute('LEN'));
		if(fld.getAttribute('DEC')!=null) dec=parseInt(fld.getAttribute('DEC'));
	    neg=(fld.getAttribute('NEG')!=null)?true:false;
		len-=dec;
	    if(dec==0 && !neg) r='(^\\d{0,'+len+'}$)';
	    if(dec==0 && neg) r='(^[-]?\\d{0,'+len+'}$)';
	    if(dec!=0 && !neg) r='(^\\d{0,'+len+'}[.]?\\d{0,'+dec+'}$)';
	    if(dec!=0 && neg) r='(^[-]?\\d{0,'+len+'}[.]?\\d{0,'+dec+'}$)';	
		re.compile(r);
		if(!re.test(v)){
			fmt='';
			if(neg) fmt+='-';
			for(i=0;i<len;i++) fmt+='#';
			if(dec>0) fmt+='.';
			for(i=0;i<dec;i++) fmt+='#';
			phl_errMsg(fld,14,fmt);
			return false;
		}
		return true;
	}
	if(fld.getAttribute('phl_type')=='date'){
		ret=phl_setScreenDate(fld);
		if(fld.type=='hidden') fld=fld.nextSibling;
		if(!ret){
			phl_errMsg(fld,16);
			return false;
		}
		return true;
	}
	if(fld.getAttribute('phl_type')=='time'){
		ret=phl_setScreenTime(fld);
		if(fld.type=='hidden') fld=fld.nextSibling;
		if(!ret){
			phl_errMsg(fld,23);
			return false;
		}
		return true;
	}	
	if(fld.getAttribute('phl_type')=='rib'){
		ret=phl_checkRIB(fld.value,fld.nextSibling.value,fld.nextSibling.nextSibling.value,fld.nextSibling.nextSibling.nextSibling.value);
		if(!ret){
			phl_errMsg(fld,21);
			return false;		
		}
	}
	if(fld.tagName=='TEXTAREA'){
		var maxlength=new Number(fld.getAttribute('maxlength'));
		if(v.length>maxlength){
			phl_errMsg(fld,17,maxlength,v.length);
			return false;
		}
		return true;
	}
	return true;	
}

// test de validation d'un formulaire
function phl_validForm(doc){
	var i;
	if(doc==null) return true;
	for(i=0;i<doc.length;i++){
		if(doc[i].name==null || doc[i].name==''|| doc[i].name.substr(0,5)=='_phl_') continue;
		if(doc[i].tagName=='INPUT' && doc[i].type=='radio' && !doc[i].checked) continue;
		phl_anzAttr(doc[i]);
		if(!phl_valid(doc[i])) return false;
	}
	return true;
}


phl_msg=new Array();
function phl_rtvMsg(num,p1,p2){
	if(p1==null)p1='';
	if(p2==null)p2='';
	try{return phl_msg[num].replace(/[&]1/,p1).replace(/[&]2/,p2);}
	catch(e){return 'Message : '+num+' ????';}
}
phl_msg[0]="OK";
phl_msg[1]="Zone Obligatoire";
phl_msg[2]="Erreur dans la fonction :\n &1";
phl_msg[3]="Confirmez-vous cette suppression ?";
phl_msg[4]="La suppression a bien été effectuée";
phl_msg[5]="Ajout";
phl_msg[6]="Suppression";
phl_msg[7]="Modification";
phl_msg[8]="Actualiser";
phl_msg[9]="Première Page";
phl_msg[10]="Page précédente";
phl_msg[11]="Page suivante";
phl_msg[12]="Dernière page";
phl_msg[13]="Touche non autorisée";
phl_msg[14]="Nombre incorrect (format valide : &1 )";
phl_msg[15]="Format de date : &1 non reconnu";
phl_msg[16]="Date incorrecte";
phl_msg[17]="La zone est limitée à &1 caractères et elle en contient &2 !";
phl_msg[18]="Cette option est déjà en cours d'exécution";
phl_msg[19]="Cette option est déjà en cours d'exécution.\nVoulez vous la relancer ?";
phl_msg[20]="Voulez-vous vraiment vous déconnecter ?";
phl_msg[21]="Le RIB est incorrect";
phl_msg[22]="Format d'heure : &1 non reconnu";
phl_msg[23]="Heure incorrecte";