/**
 * 
 * Saves ANY form input in an object, in order
 * to remember user's input.
 * 
 */

/**
 * Implement "max()"-function to the array prototype
 */
Array.prototype.max = function() {
	var max = this[0];
	var len = this.length;
	for (var i = 1; i < len; i++) 
		if (this[i] > max) 
			max = this[i];
	return max;
}

/* --- */

var $savedFormInput = {'steps': [{'bindings': []}, {'bindings': []}, {'bindings': []}, {'bindings': []}]}
var $jsFormStep = 1;

var _stackWORK = new Array(0,0,0);
var _stackEDU = new Array(0,0,0);

var $historyFormInputCount = 3;

/* --- */

/**
 * Requires Object input for config.
 * @param {Object} configObj input fields which should be saved ({name, type})
 */
function createNewObject( _inObj ){
	if(typeof _inObj != 'object') return;
	var el = _inObj.bindings;
	$savedFormInput.steps[$jsFormStep - 1] = {'bindings': []};
	for(var i=0; i<el.length; i++){
		getRowTargetInput( el[i] );
	}
}

function fillForm(){
	if($savedFormInput.steps[$jsFormStep-1].bindings.length == 0) return;
	_stackWORK = [0,0,0];
	_stackEDU = [0,0,0];
	var el = $savedFormInput.steps[$jsFormStep-1].bindings;
	for(var a=0; a<el.length; a++){
		setRowTargetInput( el[a] );
	}
}

/* --- */

/**
 * @param {Object} _row input format: name, type
 */
function getRowTargetInput( _row ){
	if(_row.type == 'text'){
		validateText(_row.name);
	} else if(_row.type == 'dropdown'){
		validateDropDown(_row.name);
	} else if(_row.type == 'checkbox'){
		validateCheckbox(_row.name);
	} else if(_row.type == 'text_arr'){
		/* do not reuse for any other purpose beside the jobs-history-form! */
		validateTextArr(_row.name);
	}
}

function setRowTargetInput( _row ){
	var el;
	if (_row.type == 'text') {
		el = Ext.DomQuery.select('input[name=' + _row.field + ']');
		if (el.length == 1) 
			el[0].value = unescape(_row.value);
	} else if (_row.type == 'dropdown') {
		el = Ext.get(_row.field);
		if(el) selectDropDownItemByValue(el.id, _row.value);
	} else if(_row.type == 'checkbox') {
		el = Ext.DomQuery.select('input[name^=' + _row.field + ']');
		el = Ext.DomQuery.filter(el, 'input[value=' + _row.value + ']');
		/* optimized for extjs skinned cb component */
		if (el[0]) {
			Ext.get(el[0].id).dom.setAttribute('checked', 'checked');
			Ext.get(el[0].id).parent().parent().addClass('x-form-check-checked');
		}
	} else if(_row.type == 'text_arr'){
		if(_row.field.substring(0,9) == 'date_fromWORK') _stackWORK[0]++;
		if(_row.field.substring(0,7) == 'date_toWORK') _stackWORK[1]++;
		if(_row.field.substring(0,8) == 'activityWORK') _stackWORK[2]++;
		
		if(_row.field.substring(0,8) == 'date_fromEDU') _stackEDU[0]++;
		if(_row.field.substring(0,6) == 'date_toEDU') _stackEDU[1]++;
		if(_row.field.substring(0,7) == 'activityEDU') _stackEDU[2]++;
	}
}

/**
 * not reuseable! just for jobs-history-form!
 */
function fillHistoryForm(){
	var el = $savedFormInput.steps[$jsFormStep-1].bindings;
	for(var a=0; a<el.length; a++){
		if (el[a].type == 'text_arr' && Ext.get(el[a].field) != null) {
			Ext.get(el[a].field).dom.value = unescape(el[a].value);
			Ext.get(el[a].field).removeClass('x-form-empty-field');
		}
	}
}

/* --- */

/**
 * @param {String} input get target text input by name attr
 */
function validateText( input ){
	var el = Ext.DomQuery.select('input[name=' + input + ']');
	var inputValue = escape(el[0].value);
	$savedFormInput.steps[$jsFormStep-1].bindings.push({field: input, value: inputValue, type: 'text'});
}

/**
 * @param {String} input get target dropdown by id
 */
function validateDropDown( input ){
	var inputValue = getSelectedDropDownValue(input);
	$savedFormInput.steps[$jsFormStep-1].bindings.push({field: input, value: inputValue, type: 'dropdown'});
}

/**
 * @param {String} input get target checkbox
 */
function validateCheckbox( input ){
	var el = Ext.DomQuery.select('input[name^="' + input + '"]');
	el = Ext.DomQuery.filter(el, 'input:checked');
	for(var i=0; i<el.length; i++){
		$savedFormInput.steps[$jsFormStep-1].bindings.push({field: input, value: el[i].value, type: 'checkbox'});
	}
}

/**
 * not reuseable! optimized for jobs-history-form!
 * @param {String} input target id
 */
function validateTextArr( input ){
	var el = Ext.DomQuery.select('input[id^=' + input + ']');
	for(var i=0; i<el.length; i++){
		if(el[i].value != 'mm.yyyy' && el[i].value != 'Tätigkeit' && el[i].value != 'Activity')
			$savedFormInput.steps[$jsFormStep-1].bindings.push({field: el[i].id, value: escape(el[i].value), type: 'text_arr'});
	}
}
