NS4 = (document.layers) ? 1 : 0;
IE4 = (document.all) ? 1 : 0;

function frm_doAction(frm, strAction){
	frm.hAction.value = strAction;
	frm.submit();
}

function set_cmb(cmb, val){
	//alert(val);
	for(i=0;i<=cmb.length-1;i++){
		if(cmb.options[i].value==val){
			cmb.selectedIndex=i;
			return true;
		}
	}
}

function set_chk(chk, val){
	for(i=0;i<=chk.length-1;i++){
		if(chk[i].value==val){
			chk[i].checked=true;
			return true;
		}
	}
}

function get_radio_val(radbox){

	if(isNaN(radbox.length))
	{
		return radbox.value; 
	}
	for(i=0;i<=radbox.length-1;i++){
		if(radbox[i].checked){
			return(radbox[i].value);
		}
	}	
	return null;
}


//ouvre une url contenu dans la propriéte value d'une combobox cmb
function open_cmb(cmb){
	url=cmb.options[cmb.selectedIndex].value;
	if(url=='#') 	cmb.selectedIndex=0;
	else 		document.location.href=url;
}

//ouvre une url contenu dans la propriéte value d'une combobox cmb dans une frame [GT]

function open_cmb_target(url, frame){
		url=url.options[url.selectedIndex].value;
		if(url=='#') 	url.selectedIndex=0;
		else frame.document.location.href = url;
}





// PIERRE : g deja cree il y a bien longtemps une fonctions du mm nom, et je m'en sert partout, alors trouvez un autre nom !!!
function open_url(url){
	self.document.location.href=url;
}


//------------------------------------------------------
function save_email(){
	var email=document.promotion.email.value;
	
	if(email==''){
		alert("Saisissez une adresse e-mail !");
	}
	else if(!verifMail(email)){
		alert('Votre adresse e-mail est incorrecte.'); 
	}
	else	document.promotion.submit();
}


/*

JavaScript pris sur le site: http://www.scripts-france.net
Ce JavaScript vérifie:
- la présence d'un @
- la présence d'au moins 4 caractères après l'@, dont un point suivi d'au moins 2 caractères
- la présence d'au moins 1 caractère avant l'@ 

*/

function verifMail(a) {
	testm = false ;
 
	for (var j=1 ; j<(a.length) ; j++) {
		if (a.charAt(j)=='@') { 
			if (j<(a.length-4)){ 
				for (var k=j ; k<(a.length-2) ; k++) { 
					if (a.charAt(k)=='.') testm = true; 
				} 
			} 
		} 
	} 
	return testm ; 
}
//------------------------------------------------------
function convert2SQLdate(dte){
	if(dte=='')	return('');
	else 		return(dte.substr(6,4)+"/"+dte.substr(3,2)+"/"+dte.substr(0,2));
}

function convertSQLdate2Date(dte){
	if(dte=='')	return('');
	else 		return(dte.substr(8,2)+"/"+dte.substr(5,2)+"/"+dte.substr(0,4));
}

//------------------------------------------------------
var curdatefieldname;
var mywincalend;

function get_calend(fieldbox){
	if(mywincalend==null||mywincalend.closed)
		 mywincalend=window.open("/library/include/html/commun/calendrier.asp?curdate="+fieldbox.value,"neww","width=220,height=400,scrolling=0");
	curdatefieldname=fieldbox;
	mywincalend.focus();
}

function get_calendrier(fieldbox, defaultvalue){
	var v = fieldbox.value;
	if(v == '')
		v = defaultvalue;
		
	if(mywincalend==null||mywincalend.closed)
		 mywincalend=window.open("/library/include/html/commun/calendrier.asp?curdate="+v,"neww","width=220,height=400,scrolling=0");
	curdatefieldname=fieldbox;
	mywincalend.focus();
}

function set_datefieldvalue(v){
	curdatefieldname.value=v;
	curdatefieldname.focus();
}
var popupsearchdate;

function get_searchdate(rep_societe){
	var typedate_search=1;
	var screentop=(NS4)?self.screenY:self.screenTop;
	var screenleft=(NS4)?self.screenX:self.screenLeft;
	if(popupsearchdate==null||popupsearchdate.closed) popupsearchdate=window.open("/library/include/html/commun/popup_searchdate.asp","toto","width=320,height=260,scrollbars=1");
	popupsearchdate.moveTo(parseInt(screenleft)+100,parseInt(screentop)+200);
	popupsearchdate.focus();
}
//------------------------------------------------------
//dte est une une chaine de caractere au format (jj/mm/aaaa)

//-- DEPRICATED, utiliser : [/library/include/js/lib_date.js]::isValidDate(dte)

function test_validDate(dte){
	
	var strDte = new String(dte);
	
	if(strDte.length>10 || strDte.length<8)	
		return(false);
	var tmpstr= new String(dte);
	
	//jour
	var pos_slash=tmpstr.indexOf('/');
	
	var dte_part=tmpstr.substr(0,pos_slash);

	if(pos_slash==-1 || pos_slash>2 || isNaN(dte_part) || dte_part>31 || dte_part<1)
	{
		return(false);
	}	
	
	//mois
	tmpstr=tmpstr.substr(pos_slash+1);
	pos_slash=tmpstr.indexOf('/');
	dte_part=tmpstr.substr(0,pos_slash);
	
	if(pos_slash==-1 || pos_slash>2 || isNaN(dte_part) || dte_part>12 || dte_part<1)  
	{
		return(false);	
	}	
	//annee
	dte_part=tmpstr.substr(pos_slash+1);
	if(dte_part.length < 2 || dte_part.length > 4){
		return false;
	}
	
	if(!isNaN(dte_part) && parseInt(dte_part)<1000)
	{
		return(false);	
	}
	return(true);
}
//dte est une une chaine de caractere au format (jj/mm/aaaa)

//-- DEPRICATED, utiliser : [/library/include/js/lib_date.js]::strdte_getYear(dte)

function strDate_getDay(dte){
	if(!test_validDate(dte)){
		return(null);
	}
	return(parseInt(dte.substr(0, dte.indexOf('/'))));
}

//dte est une une chaine de caractere au format (jj/mm/aaaa)

//-- DEPRICATED, utiliser : [/library/include/js/lib_date.js]::strdte_getYear(dte)
function strDate_getMonth(dte){
	if(!test_validDate(dte)){
		return(null);
	}
	//jour
	var tmpstr=dte;
	var pos_slash=tmpstr.indexOf('/');
	var dte_part=tmpstr.substr(0,pos_slash);
	
	//mois
	tmpstr=tmpstr.substr(pos_slash+1);
	pos_slash=tmpstr.indexOf('/');
	dte_part=tmpstr.substr(0,pos_slash);
	
	return(parseInt(dte_part));
}

//dte est une une chaine de caractere au format (jj/mm/aaaa)

//-- DEPRICATED, utiliser : [/library/include/js/lib_date.js]::strdte_getYear(dte)
function strDate_getYear(dte){
	if(!test_validDate(dte)){
		return(null);
	}
	//jour
	var tmpstr=dte;
	var pos_slash=tmpstr.indexOf('/');
	var dte_part=tmpstr.substr(0,pos_slash);
	
	//mois
	tmpstr=tmpstr.substr(pos_slash+1);
	pos_slash=tmpstr.indexOf('/');
	dte_part=tmpstr.substr(0,pos_slash);
	
	//annee
	dte_part=tmpstr.substr(pos_slash+1);
	if(dte_part.length == 2){
		return(2000 + parseInt(dte_part));
	}
	else{
		return(parseInt(dte_part));
	}
}

// renvoie 1 si dte1 < dte2
//	   -1 si dte1 > dte2
//	   0 si dte1 = dte2
function compDates(dte1, dte2){
	if(!(test_validDate(dte1) && test_validDate(dte2))){
		return null;
	}
	var tmpDay1 = strDate_getDay(dte1);
	var tmpDay2 = strDate_getDay(dte2);
	var tmpMonth1 = strDate_getMonth(dte1);
	var tmpMonth2 = strDate_getMonth(dte2);
	var tmpYear1 = strDate_getYear(dte1);
	var tmpYear2 = strDate_getYear(dte2);
	
	if(tmpYear1 > tmpYear2){
		return(-1);
	}
	else if(tmpYear1 < tmpYear2){
		return(1);
	}
	else{
		if(tmpMonth1 > tmpMonth2){
			return(-1);
		}
		else if(tmpMonth1 < tmpMonth2){
			return(1);
		}
		else{
			if(tmpDay1 > tmpDay2){
				return(-1);
			}
			else if(tmpDay1 < tmpDay2){
				return(1);
			}
			else{
				return(0);
			}
		}
	}
}
//
//function isNumeric(strg){
//	var fdot = false;
//	if(strg == '' ) return false;
//	for (var j=0 ; j<(strg.length) ; j++) {
//		if(strg.charAt(j) == '.' || strg.charAt(j) == ','){
//			if(!fdot) fdot = true;
//			else	  return(false);
//		}
//		else{
//			if (isNaN(strg.charAt(j))){
//				return(false);
//			}
//		}
//	} 
//	return(true) ; 
//}

function ValIsNumeric(strg){
	var fdot = false;
	if(strg == '' ) return false;
	for (var j=0 ; j<(strg.length) ; j++) {
		if(strg.charAt(j) == '.' || strg.charAt(j) == ','){
			if(!fdot) fdot = true;
			else	  return(false);
		}
		else{
			if(!(strg.charAt(j) == '-' && j == 0)){
				if (isNaN(strg.charAt(j))){
					return(false);
				}
			}
		}
	}
	return(true); 
}
function formatNumber(vt, d){
	var vl='';
	var parseInt_v
	vl+=vt;
	vl=vl.replace(/\s*/g,'');
	if(vl=='')	return('0,'+ addzeros(d));
	var v=''+vl;
	var t=(v.indexOf('.')!=-1)?v.indexOf('.'):v.indexOf(',');
	var decpart=(t!=-1)?v.substr(t+1):'';
	
	if (-1 < v && v < 0)
		parseInt_v = '-' + parseInt(v);
	else
		parseInt_v = parseInt(v);
	if(decpart.length<d)	return(parseInt_v+'.'+decpart+addzeros(d- decpart.length));
	else if(decpart.length == d) 	return(parseInt_v+'.'+decpart);
	else{
		//arrondi
		if(parseInt(decpart.charAt(d)) <= 5){
			var tmpdecpart = decpart.substr(0,d) //+ decpart.charAt(d-1);
		}
		else{
			var tmpdecpart = parseInt(decpart.substr(0,d)) + 1 // + (parseInt(decpart.charAt(d-1)) + 1);
		}
		return(parseInt(v)+'.'+tmpdecpart);
	}
	function addzeros(nb){
		var tmp='';
		for(var i=0;i<=nb-1;i++)tmp+='0';
		return(tmp);
	}
}

//------------------------------
// Verifie que le code postal est un nombre
//------------------------------
function isCPvalid(cp){
//------------------------------
	if(cp == '' ) return false;
	for (var j=0 ; j<(cp.length) ; j++){
		if(isNaN(cp.charAt(j))){
			return(false);
		}
	}
	return(true);
}

function openfenetre(mypage,myname,w,h,scroll,pos){
	if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
	if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
	else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win=window.open(mypage,myname,settings);
	win.focus();
}