

function countLines(lines, chars, productId){
		var productId = productId;
		var MAX_LINES = lines;
		var MAX_CHARPERLINE = chars;
		var MAX_CHAR = MAX_LINES*MAX_CHARPERLINE; //bei 13 und 40 = 520
		var ORG_MAX_CHAR = MAX_CHAR;
		//##################
		var detect = navigator.userAgent.toLowerCase();
		var dist = 2;
		if (detect.indexOf('firefox') + 1){// not internet explorer
			dist = 1;
		}
		//##################
		var objTagDiv = null; //div: message of lines
		var objTagForm = document.getElementById('personalMessage_'+productId); //form
		
		//if divObjCounter is not defined - exit
		if (typeof(document.getElementById('divObjCounter_'+productId).innerHTML) == 'undefined') {
			return;
		}
		else {
			objTagDiv = document.getElementById('divObjCounter_'+productId);
		}
		
		var originalText =  objTagForm.value; //unchanged text
		var text=originalText.replace(/\r/, ""); //changed text
		var arrTextSplits = text.split('\n'); //aufgesplitteter Text
		var intTextSplitLength = arrTextSplits.length; //Anzahl an Splits = Anzahl an userbreaks
		var lastLine = arrTextSplits[intTextSplitLength-1];
		var intLastLineLength = lastLine.length;
		MAX_CHAR = chars*(MAX_LINES-(intTextSplitLength-1));
		
		var intLines		= 0; //Anzahl geschriebener Zeilen
		var i				= 0; //runningVar for while
		var intCharLength	= 0;  //number of characters between two \n
		var	index			= 0; //index to get substrings of lines
		var strSubStr		= ''; //substring of lines
		var intLastBlank	= 0; //position of last blankspace in substring
		//Iteration of Splits
		if (intTextSplitLength <= MAX_LINES){
			//Durchlaufen aller Splits
			while (i < intTextSplitLength){
				arrTextSplits[i] = arrTextSplits[i].replace(/\r/,"");
				intCharLength = arrTextSplits[i].length;
				if (intCharLength != 0){
					intLines++;
					if (intCharLength > MAX_CHARPERLINE){
						index = 0;
						//more than MAX_CHARPERLINE characters
						while (intCharLength > MAX_CHARPERLINE){
							strSubStr = arrTextSplits[i].substr(index, MAX_CHARPERLINE); //substring of 40 characters starting with index
							intLastBlank = strSubStr.lastIndexOf(" ")+1; //last index of blank in substring
							if (intLastBlank <= 1){
								//no blank or blank on first position found
								intLastBlank = MAX_CHARPERLINE;
							}
							index += intLastBlank;  //set new index to last index of blank +1
							strSubStr = arrTextSplits[i].substr(index); //get new substring of characters starting from new index to check if still too long
							intCharLength = strSubStr.length;
							intLines++;						
						}
					}
				}else{
					//it was only a \n
					intLines++;
				}
				i++;			
			}
		}
		
		
		if (intLines > MAX_LINES || intTextSplitLength > MAX_LINES || (originalText.length-intTextSplitLength*dist+dist)>ORG_MAX_CHAR){
			var text = '<span id="error" class="textSmall error" style="padding-left:0px;">Dieser Text ist zu Lang.</span>\n';
		    objTagDiv.innerHTML = text;
			return false;
		}
//		_gc_text = objTagForm.elements['personalMessage_'+productId].value;
		var myLength = MAX_CHAR - text.length;
		var myLength = MAX_CHAR - intLastLineLength;
		var strLine = (intLines==MAX_LINES-1)?' Zeile':' Zeilen';
		objTagDiv.innerHTML = 'Es verbleiben '+ String(MAX_LINES-intLines)+ strLine +' und ' + myLength + ' Zeichen.\n';
		return true;
	}
		
	
