/*Prefill textArea with chosen dropdown option*/
function populateTxt(listId, txtAreaId){
	var txtnode ="";
	txtnode = document.getElementById(listId).value;
	if(txtnode != 0 || txtnode != null){
		document.getElementById(txtAreaId).value = txtnode;
	}
}

/* this function checks, that the giftmessage doesn't exceed the settings for it's size 
   This is a special script for house of fraser and checks only the length of a message
*/
function textCounter(theField,maxChars,maxLines,maxPerLine,loopState)
{

	var strTemp = "";
	var strLineCounter = 0;
	var strCharCounter = 0;
	var strTheField = theField.value.replace(/\r/g, "");
	var strLastSpace = 0;
	var strCounterValue = "";
	// id's html structur loopState related
	var wordRestrictionText = "wordRestrictionText" + loopState;
	var wordRestriction = "wordRestriction" + loopState;
	var wordRestrictionCounter = "wordRestrictionCounter" + loopState;
	
	for (var i = 0; i < strTheField.length; i++)
	{
		var strChar = strTheField.substring(i, i + 1);
		if(strChar == " "){
			strLastSpace = i + 1;
		}
			
			strTemp += strChar;
			strCharCounter ++;
			
		
		if (strTemp.length == maxChars)
		{
			theField.value = strTemp;
			break;
		}
		
	}
	strCounterValue = maxChars - strCharCounter + " ";
	if(document.getElementById(wordRestrictionText).firstChild.getAttribute('id') == wordRestriction){
		var newSpan = document.createElement("span")
		var newSpanText = document.createTextNode(strCounterValue);
		newSpan.appendChild(newSpanText);
		document.getElementById(wordRestrictionText).insertBefore(newSpan, document.getElementById(wordRestriction));
		document.getElementById(wordRestrictionText).firstChild.setAttribute("id", wordRestrictionCounter)
	}
	else if(document.getElementById(wordRestrictionText).firstChild.getAttribute('id') == wordRestrictionCounter){
	    document.getElementById(wordRestrictionCounter).firstChild.nodeValue = strCounterValue;
	}
	//theField.value = strTemp;
	
}
