// utils.js
// ********************************** PRINT +
// The code by Captain <cerebrum@iname.com>
// Mead & Company, http://www.meadroid.com/wpm/
// fake print() for IE4.x
function printFrame(pframe, onfinish) {
  if ( !pframe ) pframe = window;
//  if ( frame.document.readyState != "complete" &&
//       !confirm("Le document a imprimer n'est pas encore completement telecharge! Continuer l'impression?") )
//  {
//    if ( onfinish ) onfinish();
//    return;
//  }

  isMinIE4 = (document.all)?1:0;
  
  if (isMinIE4)
    Pop2ShowS(pframe.src,"POP",10,10) ;
  else
  {
    loc = pframe.src ;
    re = new RegExp("/NS(/res/.*?)(/res/.*)");
    loc = loc.replace(re,"/NS$2/");
    Pop2ShowS(loc,"POP",10,10) ;
  }
    
  setTimeout("P2S.print();P2S.close()",1000);
  return ;
  
  if (!isMinIE4) {
    pframe.print();
    return;
  }
  if (printIsNativeSupport() ) {
    /* focus handling for this scope is IE5Beta workaround,
       should be gone with IE5 RTM.
    */
    var focused = document.activeElement; 
    pframe.focus();
    pframe.self.print();
    if ( onfinish ) onfinish();
    if ( focused && !focused.disabled ) focused.focus();
    return;
  }
  var eventScope = printGetEventScope(pframe);
  var focused = document.activeElement;
  window.printHelper = function() {
    execScript("on error resume next: printWB.ExecWB 6, 1", "VBScript");
    printFireEvent(pframe, eventScope, "onafterprint");
    printWB.outerHTML = "";
    if ( onfinish ) onfinish();
    if ( focused && !focused.disabled ) focused.focus();
    window.printHelper = null;
  }
  document.body.insertAdjacentHTML("beforeEnd",
    "<object id=\"printWB\" width=0 height=0 \
    classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\"></object>");
  printFireEvent(pframe, eventScope, "onbeforeprint");
  pframe.focus();
  window.printHelper = printHelper;
  setTimeout("window.printHelper()", 0);
}
// helpers
function printIsNativeSupport() {
  var agent = window.navigator.userAgent;
  var i = agent.indexOf("MSIE ")+5;
  return parseInt(agent.substr(i)) >= 5 && agent.indexOf("5.0b1") < 0;
}
function printFireEvent(pframe, obj, name) {
  var handler = obj[name];
  switch ( typeof(handler) ) {
    case "string": pframe.execScript(handler); break;
    case "function": handler();
  }
}
function printGetEventScope(pframe) {
  var pframeset = pframe.document.all.tags("FRAMESET");
  if ( pframeset.length ) return pframeset[0];
  return pframe.document.body;
}
// ********************************** PRINT -
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i < changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i < a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; 
	for(i=0;a&&i < a.length&&(x=a[i])&&x.oSrc;i++) 
		x.src=x.oSrc;
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?")) > 0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i < d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i < d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function tableau() {
  this.length = tableau.arguments.length
  for (var i = 0; i < this.length; i++)
  this[i+1] = tableau.arguments[i]
}
var tablJour = new tableau("Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi");
var tablMois = new tableau("Janvier","F&eacute;vrier","Mars","Avril","Mai","Juin","Juillet","Ao&ucirc;t","Septembre","Octobre","Novembre","D&eacute;cembre");
var DateJour = new Date();

function date() {
  document.write(tablJour[(DateJour.getDay()+1)]," ",DateJour.getDate()," ");
  document.write(tablMois[(DateJour.getMonth()+1)]," ");
  if (!document.all) {
    document.write((DateJour.getFullYear()));
  } else {
    document.write(DateJour.getFullYear());
  }
}
function dateShort() {
  document.write(tablJour[(DateJour.getDay()+1)]," ",DateJour.getDate()," ");
  document.write(tablMois[(DateJour.getMonth()+1)]);
}

function time() {
  document.write(DateJour.getHours(),":",DateJour.getMinutes());
}

function TestCpFrance(strCp, bShort)
{
  var iDpt = 0; 
  var iVil = 0; 
  var iSiz = (bShort) ? 2 : 5;
  if (strCp.length < iSiz) return false;
  iDpt = parseInt(strCp.substr(0,2), 10);
  if (isNaN(iDpt)) return false;
  if (!bShort) {
    iVil = parseInt(strCp.substr(3,3), 10);
    if (isNaN(iVil)) return false;
  }
  if (iDpt < 1 || iDpt > 95) {
    if (iDpt != 97 && iDpt != 98) return false;
  }
  return true;
}

function TestTime(strTime)
{
// format test้ 00:00:00
  re = /^(\d{1,2}):(\d{1,2}):?(\d{1,2})?$/;
  MyArray = re.exec(strTime);
  if (!MyArray) return false;
  iHou = MyArray[1];
  iMin = MyArray[2];
  if (MyArray.length > 3) {
    iSec = MyArray[3];
    if (iSec < 0 && iSec > 59) return false;
  }
  if (iHou < 0 || iHou > 23) return false;
  if (iMin < 0 && iMin > 59) return false;
  return true;
}

function checkDate(iDay, iMonth, iYear) {
  if (iMonth < 1 || iMonth > 12) return false;
  if (iMonth == 2 && iDay > 28) {
    if (iYear%4 != 0) return false;
    if (iDay > 29) return false;
  } else
  if ((iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11) && iDay > 30) {
    return false;
  } else 
  if (iDay > 31) return false;
  return true;
}
function TestDate(strDate)
{
 // format test้ AAAA-MM-JJ HH:MM:SS
  re = /^(\d{4})-(\d{2})-(\d{2})\s?(\d{1,2}:\d{1,2}:\d{1,2})?$/;
  MyArray = re.exec(strDate);
  if (MyArray) {
    iDay = Number(MyArray[3]);
    iMonth = Number(MyArray[2]);
    iYear = Number(MyArray[1]);
    if (!checkDate(iDay, iMonth, iYear)) return false;
    if (MyArray[4]) if (!TestTime(MyArray[4])) return false;
    return true;
  }
// format test้ JJ/MM/AAAA
  re1 = /^(\d{1,2})[- \/](\d{1,2})[- \/](\d{2}|\d{4})$/;
  MyArray = re1.exec(strDate);
  if (MyArray) {
    iDay = Number(MyArray[1]);
    iMonth = Number(MyArray[2]);
    iYear = Number(MyArray[3]);
    if (!checkDate(iDay, iMonth, iYear)) return false;
    return true;
  }
  return false;
}

function SetDateToSql(strDate) {
  if (!TestDate(strDate)) return false;
  sDate = strDate;
  re1 = /^(\d{1,2})[- \/](\d{1,2})[- \/](\d{2}|\d{4})$/;
  MyArray = re1.exec(strDate);
  if (MyArray) {
    iYear = Number(MyArray[3]);
    if (iYear < 100) iYear+=2000;
    iMonth = Number(MyArray[2]);
    sMonth = (iMonth < 10) ? "0"+iMonth : String(iMonth);
    iDay = Number(MyArray[1]);
    sDay = (iDay < 10) ? "0"+iDay : String(iDay);
    sDate = String(iYear)+"-"+sMonth+"-"+sDay;
  }
  return sDate;
}

function isValidMail(Email) {
  var strTmp = Email;
  re1 = /^www\./i;
  MyArray = re1.exec(strTmp);
  if (MyArray) return false;
  re1 = /^[a-zA-Z0-9][-_a-zA-Z0-9.]+@[a-zA-Z0-9][-a-zA-Z0-9]+\./;
  MyArray = re1.exec(strTmp);
  if (!MyArray) return false;
  return true;
}

function isValidTelephone(strTel, bMobile, bFrance) {
  if (bFrance) re1 = (bMobile) ? /^\+?\(?(\+?33)?\)?[-\.\s]*06([-\.\s]*\d\d){4}/ : /^\+?\(?(\+?33)?\)?[-\.\s]*([-\.\s]*\d\d){5}/;
  else re1 = /^[0-9][-\.0-9\s]+/;
  MyArray = re1.exec(strTel);
  if (!MyArray) return false;
  return true;
}

function GetRadioValue( pRadioButton ) {
  var checkedButton = "";
  if (!pRadioButton.length) {
    if (pRadioButton.checked == true) {
      checkedButton = pRadioButton.value;
    }
  } else {
    for (i = 0; i < pRadioButton.length; i++) {
      if (pRadioButton[i].checked == true) {
        checkedButton = pRadioButton[i].value;
      }
    }
  }
  return checkedButton;
}

function Pop2Print(strUrl, strName, isizeX, isizeY) {
  var iPop2Print = null;
  iPop2Print = window.open(strUrl, strName, 'width='+isizeX+',height='+isizeY+',toolbar=no,scrollbars=yes,statusbar=no,menubar=yes,left=15,top=15,resizable=yes,personalbar=no,locationbar=no');
  if (iPop2Print) iPop2Print.focus();
  if (! isMinIE4) iPop2Print.print();
}

function Pop2Show(strUrl, strName, isizeX, isizeY) {
  var iPop2Show = null;
  iPop2Show = window.open(strUrl, strName, 'width='+isizeX+',height='+isizeY+',toolbar=no,scrollbars=no,statusbar=no,menubar=no,left=15,top=15,resizable=yes,personalbar=no,locationbar=no');
  if (iPop2Show) iPop2Show.focus();
}


function PopWindow(strUrl, strName) {
  var iPopWindow = null;
  iPopWindow = window.open(strUrl, strName);
  if (iPopWindow) iPopWindow.focus();
}

var P2S;
function Pop2ShowS(strUrl, strName, isizeX, isizeY) { // avec scroll
  P2S = null;
  P2S = window.open(strUrl, strName, 'width='+isizeX+',height='+isizeY+',toolbar=no,scrollbars=yes,statusbar=no,menubar=no,left=15,top=15,resizable=no,personalbar=no,locationbar=no');
  if (P2S) P2S.focus();
}

function PopImg(fic,x,y) {
var PopImg=null;
PopImg=window.open('','pop','width='+x+',height='+y+',toolbar=no,scrollbars=no,statusbar=no,titlebar=no,menubar=no,left=15,top=15,resizable=no,personalbar=no,locationbar=no');PopImg.document.write('<html><head><title>Absolu-vins</title></head><body onBlur="javascript:self.close();" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0" background="'+fic+'"><table width="100%" height="100%" align="center" valign="middle" border="0" vspace="0" hspace="0"><tr><td valign="top" align="left"><a href="javascript:self.close();"><img src="images/shim.gif" border="0" width="'+x+'" height="'+y+'" alt="Cliquez pour fermer la fen&ecirc;tre" vspace="0" hspace="0"></a></td></tr></table></body></html>');
PopImg.focus();
}

function Pop2ShowC(strUrl, strName, isizeX, isizeY, bScroll) {
  cxx = (screen.width / 2) - (isizeX / 2);
  cyy = (screen.height / 2) - (isizeY / 2);
  var Pop2Show = null;
  if (bScroll)
    Pop2Show = window.open(strUrl, strName, 'width='+isizeX+',height='+isizeY+',toolbar=no,scrollbars=yes,statusbar=no,menubar=yes,left='+cxx+',top='+cyy+',resizable=no,personalbar=no,locationbar=no');
  else 
    Pop2Show = window.open(strUrl, strName, 'width='+isizeX+',height='+isizeY+',toolbar=no,scrollbars=no,statusbar=no,menubar=yes,left='+cxx+',top='+cyy+',resizable=no,personalbar=no,locationbar=no');
  if (Pop2Show) Pop2Show.focus();
}

function Pop2ShowCNM(strUrl, strName, isizeX, isizeY, bScroll) {
  cxx = (screen.width / 2) - (isizeX / 2);
  cyy = (screen.height / 2) - (isizeY / 2);
  var Pop2S = null;
  if (bScroll)
    Pop2S = window.open(strUrl, strName, 'width='+isizeX+',height='+isizeY+',toolbar=no,scrollbars=yes,statusbar=no,menubar=no,left='+cxx+',top='+cyy+',resizable=no,personalbar=no,locationbar=no');
  else 
    Pop2S = window.open(strUrl, strName, 'width='+isizeX+',height='+isizeY+',toolbar=no,scrollbars=no,statusbar=no,menubar=no,left='+cxx+',top='+cyy+',resizable=no,personalbar=no,locationbar=no');
  if (Pop2S) Pop2S.focus();
}

function PopPhot(fic) {
var PopPhot=null;
PopPhot = window.open('','PHOT','width=500,height=500,toolbar=no,scrollbars=no,statusbar=no,titlebar=no,menubar=yes,left=15,top=15,resizable=no,personalbar=no,locationbar=no');PopPhot.document.write('<html><head><title>Annuaire-jeunes</title></head><body onBlur="javascript:self.close();" style="background-image: url('+fic+');background-position: 50% 50%;background-repeat: no-repeat;background-color: black;" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0"><table width="100%" height="100%" align="center" valign="middle" border="0" vspace="0" hspace="0"><tr><td valign="top" align="left"><a href="javascript:self.close();"><img src="/images/im_shim.gif" border="0" width="500" height="500" alt="Cliquez pour fermer la fen&ecirc;tre" vspace="0" hspace="0"></a></td></tr></table></body></html>');
PopPhot.focus();
}

function FullEcran(strUrl) {
  var Fen_imprim = null;
  Fenl=screen.width-50;
  FenL=screen.height-100;
  Fen_imprim = window.open(strUrl,'Pleinecran','width='+Fenl+',height='+FenL+',top=0,left=0,resizable=yes,toolbar=yes,scrollbars=yes,menubar=yes,statusbar=no');
  Fen_imprim.focus();
}

function animages(iCollection,iFreq) {
	var i = 0;
	var max = iCollection.length-1;
	for (i = 0 ; i < max ; i++) {
		if (i == iTemoin) {
			obj = iCollection[i];
			obj.src = "images/" + iCollection[i+1]+".gif";
			i = i + 2;
			if (i < max) continue;
		}
		if (i != iTemoin) {
			obj = iCollection[i];
			obj.src = "images/" + iCollection[i+2]+".gif";
			i = i + 2;
			if (i < max) continue;
		}
	}
	iTemoin = iTemoin + 3;
	if ( iTemoin> (max - 1) ) iTemoin = 0;
	strTimer=iCollection;
	timer=window.setTimeout("animages(strTimer,iFreq)",iFreq);
}

function createArray(n, init) {
	this.size = n;  //This initialization is absolutely necessary
  for (i = 1 ; i <= n ; i++) {
  	this[i] = init;      //Initialize all of the left hand elements
  }
  return this;         //Return the  array object to the caller
}

function replace(str, c1, c2){
	res = "";
	for(i = 0;i < str.length;i++){
	  if (str.charAt(i) == c1)
	    res += c2;
	  else
	    res += str.charAt(i);
	}
	return res;
}
function m_UGetElement(strNam, pForm) {
  var i0;
  for (i0 = 0; i0 < pForm.elements.length; i0++)
    if (pForm.elements[i0].name == strNam) return pForm.elements[i0];
  return false;
}

function AddElement(s_select, s_value, s_text, selected) {
	if (document.all){ // explorer
		newOpt = document.createElement("OPTION") ; 
		newOpt.text = s_text ;
		newOpt.value = s_value ;
		s_select.options.add(newOpt, s_select.length, false, selected) ;
	}
	else { // netscape
		var newOpt = new Option(s_text,s_value, false, selected);
		s_select.options[s_select.length]	= newOpt ;
	}
}

function m_UGetSelect(strObj, pForm) {
  var pSel = m_UGetElement(strObj, pForm);
  if (!pSel) return 0;
  return (pSel.length > 0 && pSel.selectedIndex >= 0) ? parseInt(pSel.options[pSel.selectedIndex].value) : 0;
}

function m_UcleanSelect(strObj, pForm) {
  var pSel = m_UGetElement(strObj, pForm);
  if (!pSel) return ;
  while (pSel.length > 0) pSel.options[0] = null;
}

function m_UAddSelect(strObj, strTxt, strVal, pForm) {
  var pSel = m_UGetElement(strObj, pForm);
  if (!pSel) return ;
  AddElement(pSel, strVal, strTxt, false);
}
function m_USetSelect(strObj, iNum, pForm) {
  var pSel = m_UGetElement(strObj, pForm);
  if (!pSel || pSel.length <= 0) return ;
  pSel.options[iNum].selected = true;
}

function PopImgSnd(nom,ext,x,y,iSon) {
var PopImg=null;
var fic="../images/im_photos/pop/"+nom+"."+ext;
//PopImg=window.open('','pop'+nom,'width='+x+',height='+y+',toolbar=no,scrollbars=no,statusbar=no,titlebar=no,menubar=no,left=15,top=15,resizable=no,personalbar=no,locationbar=no');PopImg.document.write('<html><head><title>Village de Savenni&egrave;res - R&eacute;gion des Vins de Loire</title></head><body background="'+fic+'" onBlur="javascript:self.close();" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0" bgsound="'+iSon+'"><table width="100%" height="100%" align="center" valign="middle"><tr><td><a href="javascript:self.close();"><img src="images/shim.gif" border="0" width="'+a+'" height="'+b+'" alt="Cliquez pour fermer la fen&ecirc;tre"></a></td></tr></table></body></html>');
PopImg=window.open('','pop'+nom,'width='+x+',height='+y+',toolbar=no,scrollbars=no,statusbar=no,titlebar=no,menubar=no,left=15,top=15,resizable=no,personalbar=no,locationbar=no');PopImg.document.write('<html><head><title>Village de Savenni&egrave;res - R&eacute;gion des Vins de Loire</title></head><body onBlur="javascript:self.close();" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0" bgsound="'+iSon+'"><table width="100%" height="100%" align="center" valign="middle" border="0"><tr><td><a href="javascript:self.close();"><img src="'+fic+'" border="0" width="'+x+'" height="'+y+'" alt="Cliquez pour fermer la fen&ecirc;tre"></a></td></tr></table></body></html>');
PopImg.focus();
}

function Pop3() {
var Pop2=null;
Pop2=window.open('pop_inscript.htm','pop','width=300,height=300,toolbar=no,scrollbars=no,statusbar=no,menubar=no,left=15,top=15,resizable=no,personalbar=no,locationbar=no');
Pop2.focus();
}

function Pop4() {
var Pop2=null;
Pop2=window.open('pop_casier1.htm','pop','width=400,height=180,toolbar=no,scrollbars=no,statusbar=no,menubar=no,left=15,top=15,resizable=no,personalbar=no,locationbar=no');
Pop2.focus();
}

function Pop5() {
var Pop2=null;
Pop2=window.open('pop_casier2.htm','pop','width=530,height=280,toolbar=no,scrollbars=no,statusbar=no,menubar=no,left=15,top=15,resizable=no,personalbar=no,locationbar=no');
Pop2.focus();
}

function Pop6() {
var Pop2=null;
Pop2=window.open('pop_client.htm','pop','width=350,height=480,toolbar=no,scrollbars=yes,statusbar=no,menubar=no,left=15,top=15,resizable=no,personalbar=no,locationbar=no');
Pop2.focus();
}

function FDegust(Idc) {
  Pop2Show("ges_degustm.html?IDC="+Idc, "degust", 600, 500);
}
