var cid = null;
var Cals = new Array();

function CalendarChangeTd(id){

	if(cid != null)
		document.getElementById(cid).className='J';
	
	document.getElementById(id).className='_SEL';
	cid = id;

}
function SetValue(id,value){	// DEFAULT FUNCTION FOR onSelect class option
	document.getElementById(id).value = value;
}


function datefr2en( date ){

		day = date.substring(0,2);
		month = date.substring(3,5);
		year = date.substring(6,10);
		/*d = new Date();
		d.setDate(day);
		d.setMonth(month);
		d.setFullYear(year); */
		d = year+'-'+month+'-'+day;
		return d; 

}

function dateen2fr( date ){

		year = date.substring(0,4);
		month = date.substring(5,7);
		day = date.substring(8,10);
		/*d = new Date();
		d.setDate(day);
		d.setMonth(month);
		d.setFullYear(year); */
		d = day+'/'+month+'/'+year;
		return d; 

}


function registerCal(name){

	if(typeof(Cals[ name ]) != 'array')
		Cals[ name ] = {};

	
}

function checkDay( name, day ){

	if(typeof(Cals[ name ]) !== 'object')
		registerCal(name);

	if(typeof(Cals[ name ]['focused']) == 'object')
		Cals[ name ]['focused'].className = 'activeDay day float';
	
	Cals[ name ]['focused'] = day;
	day.className = 'activeDay day float focused';

}

function calClose( id ){

	var obj = document.getElementById( id+'_layer' );
	var calContainer = document.getElementById( id+'_CalContainer' );
	obj.removeChild(calContainer);

}

function changeFormDateVal( id_input, date ){
	
	document.getElementById( id_input ).value = date;
	
}
/*------------ Cal events -----------------*/
var activeWindow = '';
var zIndex = 999;
function getLeft(MyObject)
    {
    if (MyObject.offsetParent)
        return (MyObject.offsetLeft + getLeft(MyObject.offsetParent));
    else
        return (MyObject.offsetLeft);
    }
function getTop(MyObject)
    {
    if (MyObject.offsetParent)
        return (MyObject.offsetTop + getTop(MyObject.offsetParent));
    else
        return (MyObject.offsetTop);
    }
function hideEventWindow(elementId){
	
	if(typeof(activeWindow) === 'object'){
		activeWindow.remove();
		activeWindow = '';
	}

}
function placeEventWindow(elementId){
	
	hideEventWindow();
	var popup = $('popup_'+elementId);
	activeWindow = popup;
	var popupParent = document.getElementById(elementId);
	var left = getLeft(popupParent);
	var top  = getTop(popupParent);
	//popup.style.left = left + 'px';
	if(popup.offsetHeight > top)
		popup.style.top = ( 25 ) + 'px';
	else
		popup.style.top = ( - popup.offsetHeight - 5 ) + 'px';
	
	// Debordements
	if (window.innerHeight) {
		var pHeight = window.innerHeight;
		var pWidth = window.innerWidth;
	} else { // IE
		var pHeight = document.documentElement.clientHeight;
		var pWidth = document.documentElement.clientWidth;
	}
	var cleft = getLeft(popup) + popup.offsetWidth;
	if(cleft > pWidth)
		popup.style.left = (pWidth - cleft - 35) + 'px';
	
	//zIndex++;
	popup.style.zIndex = 1000000;
	popup.absolutize();
	popup.parentNode.parentNode.addChild(popup.parentNode);
}
function callCalMonth(serialized,diff){ xajax_getMonth(serialized,diff,$('hbselector').value) }
