/* 
 * Copyright © 2007 Touring It Logistic | developer: Mugurel Mirica mmugur81@yahoo.com
 * dependencies: 'form_functions.js'
 */

var AMONTHS = new Array(
	"this index should not be used",
	new Array("Ian", "Feb", "Mar", "Apr", "Mai", "Iun", "Iul", "Aug", "Sep", "Oct", "Noi", "Dec"),	//RO
	new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")	//EN
);


var nObjSelCal = 0;
var aObjSelCal = new Array();
var aObjScName = new Array();

function selectCalendar(obContainer, pName, defMysqlDate, className, bFirstNull, bTextYear, iLang, minYear, maxYear){
/* Parameters:
 * @ obContainer: reference to a div, span , or an object where the controls are to be placed
 * @ name: strin name of the hidden element wich will provide the date in mysql format
 * @ className: string
 * @ bFirstNull: bool if for each select control there is a null value at the begining
 * @ bTextYear: bool if year is a text or a select control
 * @ iLang: int index of ARR_MONTHS
 * @ minYear: int 
 * @ maxYear: int
 */

	//properties
	this.name 		= pName;
	this.container 	= obContainer;
	this.d 			= new Date();	//current date
	//this.ctrlDate	= '';//hidden with final mysql value
	//this.selDay 	= '';
	//this.selMonth = '';
	//this.ctrlYear	= '';
	
	//methods
	//this.refresh	 = sc_refresh;
	this.enable		= sc_enable;

	//EXECUTION CODE ------------------------------------------------------------------------------------------------

	//[-1] defaults
	if (defMysqlDate)
		this.d = dateConvert(defMysqlDate);
	//if (g_bAdmin) dprint('dateConvert('+defMysqlDate+') = '+this.d);
	
	if (!bTextYear && (!minYear || minYear == '0') && (!maxYear || maxYear == '0')){
		minYear = this.d.getFullYear() - 1;
		maxYear = this.d.getFullYear() + 1;
	}
	
	iLang = parseInt(iLang, 10);
	if (!iLang || iLang > 2)
		iLang = 1;
	
	var i = 0;

	//[0] hidden
	this.ctrlDate = document.createElement('input');
	this.ctrlDate.setAttribute("type", "hidden");
	this.ctrlDate.setAttribute("id", this.name);
	this.ctrlDate.setAttribute("name", this.name);


	//[1] day select
	this.selDay = document.createElement('select');
	this.selDay.setAttribute("id", this.name+'|day');
	if (bFirstNull)
		this.selDay.options.add(new Option('', ''), -1);
	for(i=1; i<=31; i++){
		si = (i<10)? '0'+i : i
		this.selDay.options.add(new Option(si, i), -1);
	}
	this.selDay.onchange = sc_refresh;
	this.selDay.className = className;


	//[2] month select
	this.selMonth = document.createElement('select');
	this.selMonth.setAttribute("id", this.name+'|month');
	if (bFirstNull)
		this.selMonth.options.add(new Option('', ''), -1);
	for(i=1; i<=12; i++){
		this.selMonth.options.add(new Option(AMONTHS[iLang][i-1], i), -1);
	}
	this.selMonth.onchange = sc_refresh;
	this.selMonth.className = className;
	
	//[3] year select or text
	if (bTextYear){
		this.ctrlYear = document.createElement('input');
		this.ctrlYear.setAttribute("type", "text");
		this.ctrlYear.setAttribute("id", this.name+'|year');
		this.ctrlYear.setAttribute("size", "4");
	}
	else{
		this.ctrlYear = document.createElement('select');
		this.ctrlYear.setAttribute("id", this.name+'|year');
		if (bFirstNull)
			this.ctrlYear.options.add(new Option('', ''), -1);
		for(i=minYear; i<=maxYear; i++){
			this.ctrlYear.options.add(new Option(i, i), -1);
		}
	}
	this.ctrlYear.onchange = sc_refresh;
	this.ctrlYear.className = className;
	
	//set values1
	if (defMysqlDate){
		//if (g_bAdmin) dprint('ziua = '+this.d.getDate());
		this.selDay.value = this.d.getDate();
		this.selMonth.value = this.d.getMonth() + 1;
		this.ctrlYear.value = this.d.getFullYear();
		this.ctrlDate.value = dateRevert(this.d);
	}
	
	//atach to container
	this.container.appendChild(this.ctrlDate);
	this.container.appendChild(this.selDay);
	this.container.appendChild(this.selMonth);
	this.container.appendChild(this.ctrlYear);
	
	//add to global variable
	nObjSelCal ++;
	aObjSelCal[nObjSelCal-1] = this;
	aObjScName[nObjSelCal-1] = this.name;
}


function sc_refresh(e){
	var obj = (navigator.appName == 'Netscape')? e.target : event.srcElement;
	
	//get object of type selectCalendar
	var aTmp = obj.id.split('|');
	//dprint('triger object: '+obj.id);
	k = array_search(aObjScName, aTmp[0]);
	if (k > -1){
		obSelCal = aObjSelCal[k];
		//dprint('refreshing object with name '+obSelCal.name);
		
		y = str_pad(obSelCal.ctrlYear.value, 4, '0', 'STR_PAD_LEFT');
		m = str_pad(obSelCal.selMonth.value, 2, '0', 'STR_PAD_LEFT');
		d = str_pad(obSelCal.selDay.value, 2, '0', 'STR_PAD_LEFT');
		
		obSelCal.ctrlDate.value = y+'-'+m+'-'+d;
		//if (g_bAdmin) dprint(obSelCal.ctrlDate.value);
		if (obSelCal.ctrlDate.value == '0000-00-00')
			obSelCal.ctrlDate.value = '';
		obSelCal.d = dateConvert(obSelCal.ctrlDate.value);
	}
}

function sc_enable(bool){
	this.ctrlDate.disabled		= !bool
	this.selDay.disabled 		= !bool;
	this.selMonth.disabled 		= !bool;
	this.ctrlYear.disabled 		= !bool;
}
