/**
* Fonctions utilitaires
*/

YAHOO.namespace ("Abo.Utils.Tools"); 


/**
*	Fenêtre de selection de magazines
*/
YAHOO.Abo.Utils.Tools.selectMagazine = function(nbMax, dest, callbackSuccess, callbackFailure){
	this._dest = dest;
	this._nbMax = nbMax;
	this._datatable = null;
	this._callbackSuccess = callbackSuccess;
	this._callbackFailure = callbackFailure;
	
	
	this.getChecks = function(){
		var c = document.getElementById('formSelectMagazineAjax').getElementsByTagName("input");
		var ch = new Array();
		for(var i=0 ; i< c.length ; i++){
			if(c[i].type == "checkbox")
				ch.push(c[i]);
		}
		return ch;
	}
	
	this.countChecks = function(){
		var checks = getChecks();
		var cmpt = 0;
		for(var i=0 ; i< checks.length ; i++){
			if(checks[i].checked)
				cmpt++;
		}
		return cmpt;
	}

	this.initSelectMagazine = function(){
		this.formatCheck = function(elCell, oRecord, oColumn, sData) {
		    var genid = YAHOO.util.Dom.generateId();
		    elCell.innerHTML = "<input id='"+genid+"' type='checkbox' value='"+sData+"' name='selectedMag' />";
		    YAHOO.util.Event.addListener(genid, "click", verifChecks, this._oConfigs.parent,true);
		};
		
		var myColumnDefs = new Array();
		var fields = new Array();
		
		myColumnDefs.push({key:"idMag",label:"Selection",formatter:this.formatCheck,sortable:false});
		fields.push("idMag");
		myColumnDefs.push({key:"titre",label:"Titre",sortable:true});
		fields.push("titre");
			
		this._myDataSource = new YAHOO.util.DataSource(YAHOO.util.Dom.get("selectMagazineAjax")); 
		   this._myDataSource.responseType = YAHOO.util.DataSource.TYPE_HTMLTABLE; 
		   this._myDataSource.responseSchema = { 
		          fields: fields
		      }; 
		
		this._myConfigs = { 
			caption:"Liste des magazines",
			sortedBy:{key:"titre",dir:"asc"},
		       paginated:true, 
		       paginator:{ 
		           containers: null, 
		           currentPage: 1,
		           dropdownOptions: [10,25,50], 
		           pageLinks: 0, 
		           rowsPerPage: 2
		       } ,
		    parent: this
		};
	
	    this._datatable = new YAHOO.Sqli.Widgets.DatatableImpl("markupSelectMag", myColumnDefs, 
	     												this._myDataSource,this._myConfigs);
	
	
		this._datatable.subscribe("columnSortEvent", restoreChecks,this,true); 
		this._datatable.subscribe("paginatorDropdownChangeEvent", restoreChecks,this,true); 
		this._datatable.subscribe("paginatorLinkClickEvent", restoreChecks,this,true); 
	}

	this.restoreChecks = function(){
		if(this._datatable.checks != null){
			var checks = this.getChecks();
		    for(var i=0 ; i<checks.length  ; i++){
			if(this._datatable.checks[checks[i].parentNode.nextSibling.firstChild.data])
				checks[i].checked = this._datatable.checks[checks[i].parentNode.nextSibling.firstChild.data];
			}
		}
	}
	
	this.verifChecks = function(e){
		var check = e.target;
		if(this._countChecks() <= this._nbMax){
			if(this._datatable.checks == null)
				this._datatable.checks = new Array();
			
			this._datatable.checks[check.parentNode.nextSibling.firstChild.data] = check.checked;
			
			return true;	
		}
		check.checked = false;
		YAHOO.Sqli.Utils.Tools.error("Vous ne pouvez cocher que "+ this_nbMax +" magazines maximum !","Selection magazines - Erreur");
		return false;
	}

	this.postSelection = function(){
		var formObject = document.getElementById("formSelectMagazineAjax"); 
		YAHOO.util.Connect.setForm(formObject); 
		YAHOO.util.Connect.asyncRequest('POST', this._dest , 
	 			{success : this._callbackSuccess,
	 			failure : this._callbackFailure});
	}


	this._window = new YAHOO.Sqli.Widgets.Window("magazine.do?method=selectionMagazine",
				"Selection des magazines",
				this.initSelectMagazine,
				this.postSelection);
	this._window.show();
} 
