// Main configuration


var helper = new Object();

/**
 * Convert Tool-Rev-Part to Trixell-Code
 * @param {String} flav 
 * @param {Number} tool
 * @param {Number} rev
 * @param {Number} part
 * @return {String} TXLCode
 */

helper.trp2TXLCode = function(flav, tool, rev, part) {
		var nTool = pad(tool, 2); 
		var nRev = pad(rev, 2); 
		var nPart = pad(part, 4); 
		var nType = "XXX"; 

		if (flav == "3040") { 
			nType = "GW1" 
		} else if (flav === "3030") { 
			nType = "GX1" 
		} else if (flav === "2121") { 
			nType = "GA1" 
		}	else if (flav === "2121C") { 
			nType = "GC1"
		} 

		return nType + nTool + nRev + nPart; 
}

/** GET NEXT PART NUMBER
	* Gibt den TXLCode für das nächste Bauteil aus
	* erwartet konformen TXLCode
	* gibt einen um 1 inkrementierten TXLCode aus
	*
	**/
helper.getNextPart = function(TXLCode) {
	var typecode = TXLCode.substring(0, 3);
	var toolrev = TXLCode.substring(3, 7);
	var part = TXLCode.substring(7, 11);
	var npart = pad(parseInt(part) + 1, 4); // helper.js->pad()
	return typecode + toolrev + npart;
}


/**
  * returns:
  * 0 = sieht valide aus
  * 1 = nicht 11 Zeichen lang
	* 2 = falscher Produktcode
	* 3 = falsches Zeichen in Tool
	* 4 = falsches Zeichen in Rev
	* 5 = falsches Zeichen in Part
	* 6 = leer / nichts übergeben
	**/
helper.looksLikeTXLCode = function(TXLCode) {
	if (typeof TXLCode != 'undefined'){
		if (TXLCode.length == 11) {
			var typecode = TXLCode.substring(0, 3);
			var tool = TXLCode.substring(3, 5);
			var rev = TXLCode.substring(5, 7);
			var part = TXLCode.substring(7, 11);
			var toolok, revok, partok, typecodeok = false;
			if (typecode == "GW1" || typecode == "GA1" || typecode == "GX1" || typecode == "GC1") { typecodeok = true; } else { typecodeok = false;	}
			if ($.isNumeric(tool)) { toolok = true; } else { toolok = false; }
			if ($.isNumeric(rev)) { revok = true; } else { revok = false; }
			if ($.isNumeric(part)) { partok = true; } else { partok = false; }
			if (typecodeok && toolok && revok && partok) { return 0; }
			else {
				if (!typecodeok) { return 2; }
				if (!toolok) { return 3; }
				if (!revok) { return 4; }
				if (!partok) { return 5;	}
			}
		} else { return 1; }
	} else { return 6; }
}


	/** erstellt einen TXLCode aus Tool-Rev-Part-Notation */
helper.getTXLCode = function(type, tool, rev, part) { 
	var nTool = pad(tool, 2); 
	var nRev = pad(rev, 2); 
	var nPart = pad(part, 4); 
	var nType = "XXX"; 

	if (type == "3040") { 
		nType = "GW1" 
	} else if (type === "3030") { 
		nType = "GX1" 
	} else if (type === "2121") { 
		nType = "GA1" 
	}	else if (type === "2121C") { 
		nType = "GC1"
	} 

	return nType + nTool + nRev + nPart; 
}

helper.getToolRevPart = function(txlcode){
	var tool = txlcode.substring(3, 5);
	var rev = txlcode.substring(5, 7);
	var part = txlcode.substring(7);
	return tool + "-" + rev + "-" + part
}

helper.pad = function(str, max){
	str = str.toString();
	return str.length < max ? pad("0" + str, max) : str;

}

function pad(str, max) {
	str = str.toString();
	return str.length < max ? pad("0" + str, max) : str;
}


/**
 * erwartet referenz auf das formular
 *
 * Wandelt Formulardaten in ein Objekt um damit es im weiteren verlauf
 * in ein JSON-String gewandelt werden kann, was das PHP-Skript dann
 * verarbeitet
 *
 * gibt ein Objekt zurueck
 *
 * jsfiddle.net/sxGtM/3/
 * http://stackoverflow.com/questions/1184624/convert-form-data-to-javascript-object-with-jquery
 **/
$.fn.serializeObject = function(){
	var o = {};
	var a = this.serializeArray();
	$.each(a, function() {
		if (o[this.name] !== undefined) {
			if (!o[this.name].push) {
				o[this.name] = [o[this.name]];
			}
			o[this.name].push(this.value || '');
		} else {
			o[this.name] = this.value || '';
		}
	});
	return o;
}

