/**
 * @author MURKO01
 */

function CodeCollector(){
	this.codeArray = new Array();
	this.seperator;
	this.hiddenFieldId = "hiddenString";
	this.cardNumberFieldId = "cardNumber";
	this.childNodePos = new Array(0,2,4,6);
}

CodeCollector.prototype.doYourWork = function(list_id, hiddenField_Id, seperator){
	var submit = false;
	if (
		typeof(list_id) != 'undefined' &&
		typeof(hiddenField_Id) != 'undefined' &&
		typeof(seperator) != 'undefined'
		
		){
			if (this.validate(list_id, this.cardNumberFieldId)){
				this.seperator = seperator;
				this.collect(list_id);
				this.hiddenFieldId = hiddenField_Id;
				this.fillHiddenField();
				submit = true;
				
			}
			
		}
		return submit;
		 
}

CodeCollector.prototype.collect2 = function(list_id){

    var list = document.getElementById(list_id);
    var listItems = list.getElementsByTagName('li');
	this.codeArray = Array();	
    if (listItems.length > 0){
        for (var i = 0; i < listItems.length; i++){
		   var strippedCodeCell1 = utils.stripWhitespacesLeftAndRight(listItems[i].childNodes[0].value);
		   var strippedCodeCell2 = utils.stripWhitespacesLeftAndRight(listItems[i].childNodes[2].value);
		   var strippedCodeCell3 = utils.stripWhitespacesLeftAndRight(listItems[i].childNodes[4].value);
		   var strippedCodeCell4 = utils.stripWhitespacesLeftAndRight(listItems[i].childNodes[6].value);
		   this.codeArray[i] = new Array();
		   this.codeArray[i][0] = strippedCodeCell1;
		   this.codeArray[i][1] = strippedCodeCell2;
		   this.codeArray[i][2] = strippedCodeCell3;
		   this.codeArray[i][3] = strippedCodeCell4;
		   //alert('holla: ' + this.codeArray[i][0] + '+' + this.codeArray[i][1] + ' + ' + this.codeArray[i][2] + ' + ' +  this.codeArray[i][3]);
		   
        }

    }
    
}

CodeCollector.prototype.collect = function(list_id){

    var list = document.getElementById(list_id);
    var listItems = list.getElementsByTagName('li');
	this.codeArray = Array();	
    if (listItems.length > 0){
        for (var i = 0; i < listItems.length; i++){
		   this.codeArray[i] = new Array();
		   this.codeArray[i][0] = utils.stripWhitespacesLeftAndRight(listItems[i].childNodes[0].value);
		   this.codeArray[i][1] = utils.stripWhitespacesLeftAndRight(listItems[i].childNodes[2].value);
		   this.codeArray[i][2] = utils.stripWhitespacesLeftAndRight(listItems[i].childNodes[4].value);
		   this.codeArray[i][3] = utils.stripWhitespacesLeftAndRight(listItems[i].childNodes[6].value);
		   
		   
        }

    }
    
}
CodeCollector.prototype.addInputfield = function(list_id){
	if (this.validateCodeInput(list_id,"add")){
		this.collect(list_id);
		this.codeArray.push(new Array('','','',''));
		this.writeList(list_id);
	}
	
	
}

CodeCollector.prototype.removeInputfield = function(list_id, elem){
	this.collect(list_id);
	if (elem == '0' && (this.codeArray.length == 1 || this.codeArray.length == 0 )){
	//alert("doIt");
		this.codeArray.splice(elem,1);
		this.instanceList(list_id);
		this.emptyHiddenField();
	} else {
		this.codeArray.splice(elem,1);
		this.writeList(list_id);
		this.emptyHiddenField();
	}
	
	document.getElementById('addError').style.display = "none";
	document.getElementById('submitCodeInputError').style.display = "none";

}

    

CodeCollector.prototype.concatenateCodes = function(){
	 var str = '';
	 if (this.codeArray.length > 0){
	
	 	for (var i = 0; i < this.codeArray.length; i++){
			
			str += this.codeArray[i][0] + '-' +  this.codeArray[i][1] + '-' + this.codeArray[i][2] + '-' + this.codeArray[i][3];
			if(i != this.codeArray.length - 1){
				str +=  this.seperator;
			}
	 	
		}
	 }
	 
	return str;
	 
}



CodeCollector.prototype.fillHiddenField = function(){
	document.getElementById(this.hiddenFieldId).value = this.concatenateCodes();
}

CodeCollector.prototype.emptyHiddenField = function(){
	document.getElementById(this.hiddenFieldId).value = '';
}


CodeCollector.prototype.validateCodeInput = function(list_id, trigger){
	var valid = true;
	
    var list = document.getElementById(list_id);
    var listItems = list.getElementsByTagName('li');
	this.codeArray = Array();	
	
    if (listItems.length > 0) {
		for (var i = 0; i < listItems.length; i++) {
			for (var y = 0; y < this.childNodePos.length; y++) {
				
				if (validator.hasMinLength(listItems[i].childNodes[this.childNodePos[y]].value,4) == false) {
					this.setRedBackground(list_id, i);
					valid = false;
				} 
				
				/*
				if (validator.isNumber(listItems[i].childNodes[this.childNodePos[y]].value)== false){
					this.setRedBackground(list_id, i);
					valid = false;
				}
				*/
				
			}
		}
	}

    if (trigger == "add" && valid == false){
    	document.getElementById('addError').style.display = "block";
    }
	
	 if (trigger == "submit" && valid == false){
    	document.getElementById('submitCodeInputError').style.display = "block";
    }
			
	return valid;	
}

CodeCollector.prototype.validateCardNumberInput = function(field_id, trigger){
	var valid = true;
	var cardNumber = document.getElementById(field_id);
	
	if (validator.isEmpty(cardNumber.value)){
		cardNumber.style.border = "1px solid #FF0100";
		valid = false;
	}
	
	 if (trigger == "submit" && valid == false){
    	document.getElementById('submitCardNumberError').style.display = "block";
    }
			
	return valid;	
}

CodeCollector.prototype.validate = function(list_id, field_id ){
	var valid = true; 
	
	if(this.validateCardNumberInput(field_id,"submit") != true){
		valid = false;
	}
	if(this.validateCodeInput(list_id, "submit") != true){
		valid = false;
	}
			
	return valid;	
}




CodeCollector.prototype.setRedBackground = function(list_id, i){
	
	var list = document.getElementById(list_id);
    var listItems = list.getElementsByTagName('li');
	
	for (var y = 0; y < this.childNodePos.length; y++) {
		listItems[i].childNodes[this.childNodePos[y]].style.border = "1px solid #FF0100";
	}
		
}

CodeCollector.prototype.canI = function(list_id, i){
	val = false;
	if (2==2){
		val = true;
	}
	return val;
}


CodeCollector.prototype.setWhiteBackground = function(list_id, i){
	
	var list = document.getElementById(list_id);
    var listItems = list.getElementsByTagName('li');
	
	for (var y = 0; y < this.childNodePos.length; y++) {
		listItems[i].childNodes[this.childNodePos[y]].style.border = "1px solid #969696"; 
	}
	this.hideCodeInputErrorMessages();
}
CodeCollector.prototype.setWhiteBackgroundCardNumberField = function(){
var list = document.getElementById('cardNumber');
	list.style.border = "1px solid #969696"; 
	document.getElementById('submitCardNumberError').style.display = "none";
}

CodeCollector.prototype.hideCodeInputErrorMessages = function(){
	document.getElementById('addError').style.display = "none";
	document.getElementById('submitCodeInputError').style.display = "none";
}



CodeCollector.prototype.hideCardNumberErrorMessages = function(){
	document.getElementById('submitCardNumberError').style.display = "none";
}



CodeCollector.prototype.instanceList = function(list_id){

	var list = document.getElementById(list_id);
	utils.removeChildNodes(list_id); 
	

		var cellMaxLength = 4;
		var listElem = document.createElement('li');
		listElem.setAttribute('class', 'codes');
		//Erste CodeZelle
		var inputfield1 = document.createElement('input');
		inputfield1.setAttribute('class', 'codecell');
		inputfield1.setAttribute('id', 'code01' );
		inputfield1.setAttribute('tabindex', '01');
		inputfield1.setAttribute('type', 'text');
		inputfield1.setAttribute('maxlength', cellMaxLength);
		inputfield1.setAttribute('value',"");
		inputfield1.setAttribute('onkeypress','javascript:codeCollector.setWhiteBackground(\"'+list_id+'\",\"0\");');
		inputfield1.setAttribute('onkeyup','javascript:utils.jumpIntoNextField(this,this.value)');
		//Zweite CodeZelle
		var inputfield2 = document.createElement('input');
		inputfield2.setAttribute('class', 'codecell');
		inputfield2.setAttribute('id', 'code02');
		inputfield2.setAttribute('tabindex','02');
		inputfield2.setAttribute('type', 'text');
		inputfield2.setAttribute('maxlength', cellMaxLength);
		inputfield2.setAttribute('value',"");
		inputfield2.setAttribute('onkeypress','javascript:codeCollector.setWhiteBackground(\"'+list_id+'\",\"0\");');
		inputfield2.setAttribute('onkeyup','javascript:utils.jumpIntoNextField(this,this.value)');
		//Dritte CodeZelle
		var inputfield3 = document.createElement('input');
		inputfield3.setAttribute('class', 'codecell');
		inputfield3.setAttribute('id', 'code03');
		inputfield3.setAttribute('tabindex','03');
		inputfield3.setAttribute('type', 'text');
		inputfield3.setAttribute('maxlength', cellMaxLength);
		inputfield3.setAttribute('value',"");
		inputfield3.setAttribute('onkeypress','javascript:codeCollector.setWhiteBackground(\"'+list_id+'\",\"0\");');
		inputfield3.setAttribute('onkeyup','javascript:utils.jumpIntoNextField(this,this.value)');
		//Vierte CodeZelle
		var inputfield4 = document.createElement('input');
		inputfield4.setAttribute('class', 'codecell');
		inputfield4.setAttribute('id', 'code04');
		inputfield4.setAttribute('tabindex','04');
		inputfield4.setAttribute('type', 'text');
		inputfield4.setAttribute('maxlength', cellMaxLength);
		inputfield4.setAttribute('value',"");
		inputfield4.setAttribute('onkeypress','javascript:codeCollector.setWhiteBackground(\"'+list_id+'\",\"0\");');
		inputfield4.setAttribute('onkeyup','javascript:utils.jumpIntoNextField(this,this.value)');
		var link = document.createElement("a");
		link.setAttribute('class', 'remove');
        link.setAttribute('title', 'remove');
        link.setAttribute('href', 'javascript:codeCollector.removeInputfield(\"'+list_id+'\",\"0\")');
        link.appendChild(document.createTextNode(" [x]"));
		var newListElem = list.appendChild(listElem);
		newListElem.appendChild(inputfield1);
		newListElem.appendChild(document.createTextNode(' - '));
		newListElem.appendChild(inputfield2);
		newListElem.appendChild(document.createTextNode(' - '));
		newListElem.appendChild(inputfield3);
		newListElem.appendChild(document.createTextNode(' - '));
		newListElem.appendChild(inputfield4);
		newListElem.appendChild(link);
	
	
}

CodeCollector.prototype.writeList = function(list_id){
	var list = document.getElementById(list_id);
	utils.removeChildNodes(list_id); 

    
	for (var i = 0; i < this.codeArray.length; i++) {
		var cellMaxLength = 4;
		var listElem = document.createElement('li');
		listElem.setAttribute('class', 'codes');
		//Erste CodeZelle
		var inputfield1 = document.createElement('input');
		inputfield1.setAttribute('class', 'codecell');
		inputfield1.setAttribute('id', 'code' + i + '1');
		inputfield1.setAttribute('tabindex', i + '1');
		inputfield1.setAttribute('type', 'text');
		inputfield1.setAttribute('maxlength', cellMaxLength);
		inputfield1.setAttribute('value',this.codeArray[i][0]);
		inputfield1.setAttribute('onkeypress','javascript:codeCollector.setWhiteBackground(\"'+list_id+'\",\"'+i+'\");');
		inputfield1.setAttribute('onkeyup','javascript:utils.jumpIntoNextField(this,this.value)');
		//Zweite CodeZelle
		var inputfield2 = document.createElement('input');
		inputfield2.setAttribute('class', 'codecell');
		inputfield2.setAttribute('id', 'code' + i + '2');
		inputfield2.setAttribute('tabindex',i + '2');
		inputfield2.setAttribute('type', 'text');
		inputfield2.setAttribute('maxlength', cellMaxLength);
		inputfield2.setAttribute('value',this.codeArray[i][1]);
		inputfield2.setAttribute('onkeypress','javascript:codeCollector.setWhiteBackground(\"'+list_id+'\",\"'+i+'\");');
		inputfield2.setAttribute('onkeyup','javascript:utils.jumpIntoNextField(this,this.value)');
		//Dritte CodeZelle
		var inputfield3 = document.createElement('input');
		inputfield3.setAttribute('class', 'codecell');
		inputfield3.setAttribute('id', 'code' + i + '3');
		inputfield3.setAttribute('tabindex',i + '3');
		inputfield3.setAttribute('type', 'text');
		inputfield3.setAttribute('maxlength', cellMaxLength);
		inputfield3.setAttribute('value',this.codeArray[i][2]);
		inputfield3.setAttribute('onkeypress','javascript:codeCollector.setWhiteBackground(\"'+list_id+'\",\"'+i+'\");');
		inputfield3.setAttribute('onkeyup','javascript:utils.jumpIntoNextField(this,this.value)');
		//Vierte CodeZelle
		var inputfield4 = document.createElement('input');
		inputfield4.setAttribute('class', 'codecell');
		inputfield4.setAttribute('id', 'code' + i + '4');
		inputfield4.setAttribute('tabindex',i + '4');
		inputfield4.setAttribute('type', 'text');
		inputfield4.setAttribute('maxlength', cellMaxLength);
		inputfield4.setAttribute('value',this.codeArray[i][3]);
		inputfield4.setAttribute('onkeypress','javascript:codeCollector.setWhiteBackground(\"'+list_id+'\",\"'+i+'\");');
		inputfield4.setAttribute('onkeyup','javascript:utils.jumpIntoNextField(this,this.value)');
		var link = document.createElement("a");
		link.setAttribute('class', 'remove');
        link.setAttribute('title', 'remove');
        link.setAttribute('href', 'javascript:codeCollector.removeInputfield(\"'+list_id+'\",\"'+i+'\")');
        link.appendChild(document.createTextNode(" [x]"));
		var newListElem = list.appendChild(listElem);
		newListElem.appendChild(inputfield1);
		newListElem.appendChild(document.createTextNode(' - '));
		newListElem.appendChild(inputfield2);
		newListElem.appendChild(document.createTextNode(' - '));
		newListElem.appendChild(inputfield3);
		newListElem.appendChild(document.createTextNode(' - '));
		newListElem.appendChild(inputfield4);
		newListElem.appendChild(link);
	}
}

CodeCollector.prototype.rewriteSubmittedCodes = function(list_id){
	var list = document.getElementById(list_id);
		utils.removeChildNodes(list_id); 
		var hiddenField = document.getElementById(this.hiddenFieldId);
		
		
		
		
		if (hiddenField.value != " "){
			var str = hiddenField.value;
			var codes = str.split('$');

			for(var i = 0; i < codes.length; i++){
				var listElem = document.createElement('li');
				
	 		
	    		listElem.setAttribute('class', 'codes');
				var codeCells = codes[i].split('-');
				
				for (var y = 0; y < codeCells.length; y++) {
				var inputfield = document.createElement('input');
			inputfield.setAttribute('class', 'codecell');
			inputfield.setAttribute('id', 'code' + i + y);
			inputfield.setAttribute('tabindex', i + y);
			inputfield.setAttribute('type', 'text');
			inputfield.setAttribute('maxlength', 4);
			inputfield.setAttribute('value',codeCells[y]);
			inputfield.setAttribute('onkeypress','javascript:codeCollector.setWhiteBackground(\"'+list_id+'\",\"'+i+'\");');
			inputfield.setAttribute('onkeyup','javascript:utils.jumpIntoNextField(this,this.value)');
			var newListElem = list.appendChild(listElem);
			
			newListElem.appendChild(inputfield);
			if(y < 3){
				newListElem.appendChild(document.createTextNode(' - '));
			}
			
			
				}
				
				var link = document.createElement("a");
			link.setAttribute('class', 'remove');
	 		link.setAttribute('title', 'remove');
	 		link.setAttribute('href', 'javascript:codeCollector.removeInputfield(\"' + list_id + '\",\"' + i + '\")');
	 		link.appendChild(document.createTextNode(" [x]"));
			newListElem.appendChild(link);
					
			}
		}
		
	}

var codeCollector = new CodeCollector();

