﻿
function popup(filename, name, width, height) {
  var top = (screen.height - height) / 2;
  var left = (screen.width - width) / 2;
  var param = "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + ",location=no,menubar=no,toolbar=no,scrollbars=no,resizable=no,status=no";
  return window.open(filename, name, param);
}

function trim(string){
  return string.replace(/(^\s*)|(\s*$)/g,'');
}


function xreplace(checkMe,toberep,repwith){
  var temp = checkMe;
  var i = temp.indexOf(toberep);
  while(i > -1){
    temp = temp.replace(toberep, repwith);
    i = temp.indexOf(toberep);
  }
  return temp;
}

function setSelect( elm, val ){
  for(i=0;i<elm.options.length;i++){
    if ( elm.options[i].value == val ){
      elm.selectedIndex=i;
      return true;
    }
  }
  return false;
}

function get_element(id){
	return document.getElementById(id);
}

function getElement(id){
	return document.getElementById(id);
}

function confirm_logout( msg, url ){
  if ( confirm(msg) ){
    window.location=url;
  }
}

function admin_submit( func ){
	document.frmAdmin.func.value=func;
	document.frmAdmin.submit();
}

function admin_button( func ){
	admin_submit( func );
}

function admin_button_checked( func, counter, check_msg, bconfirm, confirm_msg ){	
	if (!counter) counter = '_check_counter';
    var frm = document.frmAdmin;
    cnt = eval( 'frm.' + counter );
    if (cnt) {
      if ( cnt.value == 0 ){
         alert( check_msg );
      }else{
        if ( bconfirm ){
          if ( !confirm(confirm_msg) )
            return;
        }
        admin_button( func );
      }
    }	 
}

function admin_check_process( func, elmt ) {
    var frm = document.frmAdmin;
    cb = eval( 'frm.' + elmt );
    if (cb) {
        for (i = 0; true; i++) {
            cbx = eval('frm.zcb'+i);
            if (!cbx) break;
            cbx.checked = false;
        }
        cb.checked = true;
        admin_button( func );
    }
    return false;
}

function admin_check_all( cnt, control, elmts, counter ) {	
	if (!elmts) elmts = 'zcb';
	if (!control)  control = '_check_control';
	if (!counter) counter = '_check_counter';
	var frm = document.frmAdmin;
	var chkall = false;
	var count = 0;	
	ca = eval( 'frm.' +  control );
	if ( ca ) chkall = ca.checked;	
	for (i=0; i < cnt; i++) {
		cb = eval( 'frm.' + elmts + '' + i );		
		if (cb) {
			cb.checked = chkall;
			count++;
		}
	}	
	cnt = eval( 'frm.' + counter );
	if (cnt){
		if ( chkall )
			cnt.value = count;
		else
			cnt.value = 0;
	}

}

function admin_check_update( chk, counter ) {
	if (!counter) counter = '_check_counter';
	var frm = document.frmAdmin;
	cnt = eval( 'frm.' + counter );
	cb = eval( 'frm.' +  chk );
	if (cnt) {
		if ( cb.checked )
			  cnt.value++;
		else
		  cnt.value--;
	}		
}

function admin_button_process( func, bconfirm, confirm_msg ){			
    var frm = document.frmAdmin;
	if ( bconfirm ){
	  if ( !confirm(confirm_msg) )
		return;
	}
	admin_button( func ); 
}








/*
function sheetTabPriceClick(){
	var tabbuy = get_element( 'sheetTabPriceBuy' );
	var tabrent = get_element( 'sheetTabPriceRent' );
	var divbuy = get_element( 'sheetTabPriceBuyDiv' );
	var divrent = get_element( 'sheetTabPriceRentDiv' );
	if ( tabbuy.className == 'sheettab' ){
		tabbuy.className = 'sheettabdown';
		tabrent.className = 'sheettab';
		divbuy.style.display = 'none';
		divrent.style.display = 'block';
	}else{
		tabbuy.className = 'sheettab';
		tabrent.className = 'sheettabdown';
		divbuy.style.display = 'block';
		divrent.style.display = 'none';
	}
}
*/

function addParamToUrl( url, param, value ){
	var prefix ='';
	var suffix ='';
	var offset = 0;
	var end	   = 0;
	if ( trim(param)!='' ){
		offset = url.indexOf( param + '=' );
		offset--;
		if ( offset<0 ) offset =0;
		if ( offset > 0 ){			
			prefix = url.substr(0, offset );			
			end = url.indexOf( '&', offset+1 );			
			if (end<0)  end=url.length-1;
			if (end<0)  end=0;
			if ( end+1<url.length )
				suffix = url.substr( end, url.length-end);			
			if ( prefix.indexOf('?') )
				url = prefix + '&' + param + '=' + value + suffix;
			else
				url = prefix + '?' + param + '=' + value + suffix;
		}else{
			if ( url.indexOf('?') )
				url += '&' + param + '=' + value;
			else
				url += '?' + param + '=' + value;
		}
	}	
	return url;
}

function clockStart() {
	clockProcess();	
    window.setInterval("clockProcess()", 1000);
    
}

function clockProcess(){
 document.getElementById("mainClock").innerHTML = '<small>' + getCurrentDate() + '<br/>' + getCurrentTime() + '</small>';
}

function getCurrentTime(){
  var hhmmss="", adate, min, sec;
  adate = new Date()
  hhmmss += adate.getHours();
  min = adate.getMinutes();
  if (min < 10) hhmmss += ":0" + min;
  else hhmmss += ":" + min;
  sec = adate.getSeconds();
  if (sec < 10) hhmmss += ":0" + sec;
  else hhmmss += ":" + sec;
  hhmmss = " " + hhmmss;
  return hhmmss;
 }

function getCurrentDate(){
  var adate, date, amois;
  adate = new Date();
  date = adate.getDate();
  amois = adate.getMonth()+1;
  if (amois == 1) date += " janvier";
  else if (amois == 2) date += " février";
  else if (amois == 3) date += " mars";
  else if (amois == 4) date += " avril";
  else if (amois == 5) date += " mai";
  else if (amois == 6) date += " juin";
  else if (amois == 7) date += " juillet";
  else if (amois == 8) date += " août";
  else if (amois == 9) date += " septembre";
  else if (amois == 10) date += " octobre";
  else if (amois == 11) date += " novembre";
  else if (amois == 12) date += " décembre";
  date += " " + adate.getFullYear();
  return date;

}


function openSheetSelFile( url, srvsel, field ){
	var frm = document.frmAdmin;
	var srvobj = eval( 'frm.' + srvsel );
	var name = srvobj.options[srvobj.selectedIndex].value;
	return popup( url + '&field=' + field + '&server=' + name, 'filesel', '400', '400');
}

	
	



//var IMG_PATH 		= '<?php echo $paths->frontTpltImg; ?>';
var IMG_PATH 		= 'datas/templates/images';
var BTN_SIZE 		= 16;
var SCROLL_STEP 	= 5;
var SCROLL_TIMER 	= 30;



function get_offset(element,attr){
  var offset=0;
  while(element){
	offset+=element[attr];
	element=element.offsetParent
  }
  return offset
}

//Positionnement d'un element
function move_element(elmt, x, y){
	if (!elmt) return;
	try{
		elmt.style.left = x + 'px';
		elmt.style.top = y + 'px';
	}catch(err){
	}
}	

function init_scrollbar(name){
	if (!name) name = 'area';
	var area = document.getElementById(name);		
	if (!area) {
		alert('Cannot find area "' + name + '" !\nScrollbar disable !');
		return;
	}		
	var content = document.getElementById(name + '_content');		
	if (!content) {
		alert('Cannot find content "' + name + '_content" !\nScrollbar disable !');
		return;
	}	
	var ax = get_offset(area,'offsetLeft');
	var ay = get_offset(area,'offsetTop');
	var aw = parseInt(area.offsetWidth);
	var ah = parseInt(area.offsetHeight);	
	var ch = parseInt(content.offsetHeight);		
	area.timer = null;
	
	if ( ch<=ah ) return;
	
	var div	= document.createElement("div");	
	div.style.position 	= 'absolute';	
	div.style.width 	= BTN_SIZE + 'px';	
	div.style.zIndex 	= '100';	
	area.appendChild(div);	
	move_element(div, aw-BTN_SIZE, 0); 			
	
	
	//Adding control button
	var upBtn				= document.createElement("img");
	upBtn.id    			= name + '_upbtn';
	upBtn.src 				= IMG_PATH + '/common/up.png';			
	upBtn.style.position 	= 'absolute';
	upBtn.style.cursor 		= 'pointer';					
	upBtn.onmouseover 		= function(){ on_scroll(1, name); };		
	upBtn.onmouseout  		= function(){ on_scroll_stop(name); };		
	div.appendChild(upBtn);		
	//move_element(upBtn, ax+aw-BTN_SIZE, ay+2); 			
	move_element(upBtn, 0, 2); 			
	
	
	var downBtn = document.createElement("img");
	downBtn.id    			= name + '_downbtn';
	downBtn.src 			= IMG_PATH + '/common/down.png';	
	downBtn.style.position 	= 'absolute';	
	downBtn.style.cursor 	= 'pointer';		
	downBtn.onmouseover 	= function(){ on_scroll(-1, name); };		
	downBtn.onmouseout  	= function(){ on_scroll_stop(name); };	
	div.appendChild(downBtn);				
	move_element(downBtn, 0, ah-BTN_SIZE-2); 	

}  

function on_scroll( sens, name ){
	if (!name) name = 'area';
	var area = document.getElementById(name);		
	var content = document.getElementById(name + '_content');			
	var upBtn = document.getElementById(name + '_upbtn');		
	var downBtn = document.getElementById(name + '_downbtn');		
	if (!area) return;
	if (!content) return;	
	if ( !content.style.top ) content.style.top = '0px';
	var ah = parseInt(area.offsetHeight);		
	var cy = parseInt(content.style.top) + (sens*SCROLL_STEP);
	var ch = parseInt(content.offsetHeight);		
	if ( cy > 0 ){  
		cy  = 0;
		upBtn.src = IMG_PATH + '/common/upoff.png';
	}else{
		if ( upBtn.src != IMG_PATH + '/common/up.png' )	upBtn.src = IMG_PATH + '/common/up.png';
	}
	
	if ( cy < -(ch-ah) ){
		cy  = -(ch-ah);				
		downBtn.src = IMG_PATH + '/common/downoff.png';		
	}else{
		if ( downBtn.src != IMG_PATH + '/common/down.png' )	downBtn.src = IMG_PATH + '/common/down.png';
	}	
	content.style.top =  cy + 'px';			
	area.timer = setTimeout('on_scroll(' + sens + ', "' + name + '");', SCROLL_TIMER);
	
}

function on_scroll_stop( name ){
	if (!name) name = 'area';
	var area = document.getElementById(name);		
	clearTimeout(area.timer);
}


var calPopup;
function calShowDate(id){
	var div = document.getElementById('caldiv_' + id)	
	if (!div) return;
	var content = div.innerHTML;
	//alert(content);	
	calPopup = document.createElement("div");
	calPopup.className			= 'boxBack';		
	calPopup.style.zIndex		= 100;
	calPopup.style.visibility  	= 'visible';	
	var box = document.createElement("div");
	box.className			= 'boxBody';					
	box.style.width			= '500px';	
	box.style.height		= '100px';		
	box.style.marginTop		= '320px';		
	calPopup.appendChild(box);			
	var lnk = document.createElement("div");	
	lnk.className			= 'boxClose';
	lnk.innerHTML			= '<a href="javascript:calClose();"><img src="datas/templates/images/common/close.jpg" border="0"></a>';
	box.appendChild(lnk);
	var bloc = document.createElement("div");
	bloc.innerHTML = content;
	bloc.style.border = 'solid 0px red';
	box.appendChild(bloc);
	document.body.appendChild(calPopup);		
}

function calClose(){	
	document.body.removeChild(calPopup);		
}
