/*
 * This class contains the methods for the adding/removing  panles. 
 * For example additional experiences, education, licensure
 * 
 * Constructor params
 * 
 * @param _maxPanels 		-	Maximum number of panels collection will be having
 * 
 * @param _panelIdPrefix	-	panlelIdPrefix, for example if panle Id is 'edu1' panlelIdPrefix is 'edu'
 * 
 * @param _hiddenIdPrefix	-	hiddenIdPrefix of hidden variable that to indicate that the panle is active or not.
 * 								This is useful in case of NetUI data binding elements 
 * 								for example : 	if the hidden id is '{actionForm.educationEntity1.active}'
 * 												then hiddenIdPrefix is '{actionForm.educationEntity'
 *  
 * @param _hiddenIdSuffix	-	hiddenIdSuffix of hidden variable that to indicate that the panle is active or not.
 * 								This is useful in case of NetUI data binding elements 
 * 								for example : 	if the hidden id is '{actionForm.educationEntity1.active}'
 * 												then hiddenIdSuffix is '.active}'
 * 
 * @param _swapDataCallback	-	Callback function called when panel is removed from the collection data should be moved to the 
 * 								upper level since framework invisibles the last panel. Framework provides the flexibility to the 
 * 								developer to write his own call back method.
 *  
 * @param _clearDataCallback-	Callback method that will be called by the framework to clear the panel data before 
 * 								adding to the collection. This is useful in case of, when panel is added-deleted-added. So as to
 * 								clear the previsous data. Framework provides the flexibility to the developer to write his own call back 
 * 								method.
 * @param _validateCallback-	Callback method that will be called by the framework to validation needs to be done before adding 
 * 								panel to the collection.
 *  
 * 
 * */ 
PanelCollection = function(_maxPanles,_panelIdPrefix,_hiddenIdPrefix,_hiddenIdSuffix,_swapDataCallback,_clearDataCallback,_validateCallback){
	this.panelCounter	= 0;  
	this.panelIdPrefix	= _panelIdPrefix;
	this.maxPanels		= _maxPanles;
	this.hiddenIdPrefix	= _hiddenIdPrefix;
	this.hiddenIdSuffix	= _hiddenIdSuffix;
	this.swapDataCallback  = _swapDataCallback;
	this.clearDataCallback = _clearDataCallback;
	this.validateCallback  = _validateCallback;
		
	/*
	 * This method is helper method for the framework, if validtion has to be done before adding panel to the collection
	 * */
	this.validate = function()
	{
		if(this.validateCallback == 'undefined' || this.validateCallback == null ){
			return true;
		}
		else{
			return this.validateCallback(this);
		}
	}
	/*
	 * Method that adds panels to the collection. Panels are tracked by internal panlelCounter
	 * and total number of panles should be less than maxPanles. It takes care of clearing data 
	 * in the panels since the panels is reusable. It does this by calling callBack clearDataCallback
	 * for the current panel
	 * */
	this.add = function(){
		if(this.validate()){
			if(this.panelCounter < this.maxPanels){
				this.panelCounter++;
				var el = document.getElementById(
													this.panelIdPrefix + (this.panelCounter)
												);			
				var elId= this.hiddenIdPrefix	+	this.panelCounter	+	this.hiddenIdSuffix;				
				document.getElementById(elId).value = true;							
				this.clearDataCallback(this.panelCounter); 				
				el.style.display = "block";
			}
		}
	}
	/*
	 * Method that removes the panel from the collection. It shuffles data to the upper panels 
	 * before it removes the panel since it invisible the lst panel.
	 * */
	this.remove = function(panel){
		var elId= this.hiddenIdPrefix	+	this.panelCounter	+	this.hiddenIdSuffix;
		document.getElementById(elId).value = false;			
		this.shufflePanelsDataBack(panel,this.panelCounter);
		var lastPanelEl = document.getElementById(this.panelIdPrefix + this.panelCounter);
		lastPanelEl.style.display = "none";				
		this.panelCounter--;
	}
	/*
	 * Method that shuffles data 'from' to 'to' panel, up the level
	 * */
	this.shufflePanelsDataBack = function (pI,lI){	
		var i=0;		
		for(i = pI; i < lI ; i++ ){			
			this.swapDataCallback(i+1,i); //Swap data from - to
		}
	}
	
	/*
	 * Method that initializes panels based on the hidden visibility flag. Also initializes the 
	 * panelCounter. Usually called on body load. 
	 * */
	this.initPanels = function (){	
		var i = 0;	
		this.panelCounter = 0;		
		for(i = 1; i <=(this.maxPanels); i++){	
			var elId = this.hiddenIdPrefix	+	i	+	this.hiddenIdSuffix;			
			var visEl = document.getElementById(elId);

			if(visEl != null && visEl.value != null){
				var visible = eval(visEl.value);						
				if(visible){
					this.panelCounter++;				
					var id1= this.panelIdPrefix+i;							
					var el = document.getElementById(id1);
					el.style.display = "block"	;
				}
			}
		}		
	}
	
	this.getString = function(){
		var msg ="";		
		for(i=1;i<6;i++){	
			var elId= this.hiddenIdPrefix	+	i	+	this.hiddenIdSuffix;				
			msg += "active "+i+" : "+document.getElementById(elId).value +"\n";						
		}		
		return msg;		
	}	
}
