/*
	wordCounter JS: Fullsize Image Overlays
	by Andrej Smirnov

	Created only for TCW Transfer-Centrum GmbH & Co. KG (www.tcw.de)

	Functions
	- wordCounterInit()
	- wordCounterText()
	- wordCounter()
	- wordCounterInput()
	- wordCounterTinyMCE()
	- textClear()
	- wordCounterFunc()
*/


/**
 * initialisiert die Startwerten beim Seiteladen.
 * Ersetzt onload-Funktion, wird bei addLoadEvent.js ausgeführt.
 */
function wordCounterInit()
	{
	if (!document.getElementsByName){ return; }

	var anchorTags = new Array("input", "textarea");
	for (var t=0; t<anchorTags.length; t++)
		{
		var anchors = document.getElementsByTagName(anchorTags[t]);

		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++)
			{
			var anchor = anchors[i];
			if (anchor.getAttribute("name") != null && anchor.getAttribute("name") != "")
				{
				nameSplit = anchor.getAttribute("name").split("][");
				if (nameSplit[1] != null && nameSplit[1] != "")
					{
					nameSplit[1] = nameSplit[1].substring(0, nameSplit[1].length-1);
					if (nameSplit[1] != "meta_title" && nameSplit[1] != "meta_description")
						{
						wordCounterText(anchor, "viewdetail", nameSplit[0]+"]["+nameSplit[1]+"]Anzahl");
						}
						else
						{
						charCounter(anchor, "viewdetail", nameSplit[0]+"]["+nameSplit[1]+"]Anzahl");
						}
					}
				}
			}
		}
	}//function wordCounterInit()


/**
 * Wörterzahlung im Textfeld
 */
function wordCounterText(Target, Form, Field)
	{
	if (document[Form] != undefined && document[Form][Field] != undefined)
		{
		text = textClear(Target.value);
		document[Form][Field].value=wordCounterFunc(text);
		}
	}//function wordCounterText()


/**
 * Zeichenzählung im Inputfeld
 */
function charCounter(Target, Form, Field)
	{
		StrLen=Target.value.length;
		while (StrLen>0 && Target.value[0]==" ")
			{
			Target.value=Target.value.substring(1,StrLen); 
			StrLen--;
			}
		document[Form][Field].value=StrLen;
	}//function charCounter()


/**
 * Wörterzahlung im Inputfeld
 */
function wordCounterInput(Target, Form, Field)
{
document[Form][Field].value=wordCounterFunc (Target);
}//wordCounterInput(Target, Form, Field)


/**
 * Wörterzahlung im TinyMCE-Feld
 */
function wordCounterTinyMCE(ID, Form, Field)
	{
	text = document.getElementById(ID+'_ifr').contentWindow.document.getElementsByTagName('body')[0].innerHTML;
	text = textClear(text);
	document[Form][Field].value=wordCounterFunc(text);
	}//wordCounterTinyMCE(ID, Form, Field)



/**
 * Ersetzt in text HTML-Code und -Tags mit " "
 *
 * @param string - HTML-Text
 *
 * @return string - Text
 */
function textClear(text)
	{
	text = text.replace(/<br>/, " ");
	text = text.replace(/(<([^>]+)>)/ig," ");
	text = text.replace(/&nbsp;/g, " ");
	text = text.replace(/\r/, " ");
	return (text);
	}//function textClear()


/**
 * ltrim() und zählt die Wörter, die länger als minWordLength sind.
 *
 * @param Object
 * 
 * @return int - Wörteranzahl
 */
function wordCounterFunc(Target)
{
var minWordLength = "0"; //die minimale Wortlänge
if (Target.value != undefined)
	{
	Text = Target.value;
	isValue = true;
	}
	else
	{
	Text = Target;
	isValue = false;
	}
var wordAnz = 0;
StrLen=Text.length;
// ltrim()
/*
while (StrLen>0 && Text[0]==" ")
{
Text=Text.substring(1,StrLen); 
StrLen--;
}
if (isValue && Text != Target.value)
	{
	Target.value = Text;
	}
else if (!isValue && Text != Target)
	{
	Target = Text;
	}
*/
// ltrim()-end

arr = Text.split(" ");
for (i=0; i<arr.length && arr[i] != "Weiterführende" && arr[i+1] != "Literatur:"; i++)
	{
	if (arr[i]!="" && arr[i].length>=minWordLength)
		{
		wordAnz = wordAnz+1;
		}
	}
return (wordAnz);
}//function wordCounterFunc()

