if (typeof init != 'function')
	document.write('<script src="../js/ajax.js" type="text/javascript"></script>');

/**
 *	updates the number of remaining chars of the textfield
 *	@param	laenge:	actual number of chars in the textfield
 */
function count_chars(element)
{
	if (document.mForm[element].value.length > Max[element])
		document.mForm[element].value = 
			document.mForm[element].value.substring(0, Max[element]);
	document.getElementById('chars').innerHTML = 
		Max[element] - document.mForm[element].value.length;
	if (document.getElementById('chars').innerHTML < 0)
		document.getElementById('chars').innerHTML = 0; // too long message
}


/**
 *	formats the input-elements, when they got the focus
 *	@param	element: the widget which calls the funktion, not used yet
 */
function enter(element)
{
	document.mForm[element].style.color='#000000';
	switch (type)
	{
		case 0: //clear text
		case 1: //clear text and write back if field loose the focus
			if (document.mForm[element].value == Text[element])
				document.mForm[element].value = '';
			break;
		case 2: //select text			
			if (document.mForm[element].value == Text[element])
				document.mForm[element].select();
			break;
		default:
			break;
	}
	if (element == 'opinion')
		count_chars(element);
}


/**
 *	resets the input-elements, when they loose their focus
 *	@param	element:	the widget which calls the funktion, not used yet
 */
function leave(element)
{
	if (type == 1 || type == 2)
	{
		if (document.mForm[element].value == '')
			document.mForm[element].value = Text[element];
		if (document.mForm[element].value == Text[element])
			document.mForm[element].style.color='#acacac';
	}
	if (element == 'opinion')
		count_chars(element);
}


/**
 *	shows the average rating
 */
function rating(value)
{
	value = value.toString().split('.');
	if (value[1] == undefined)
		value[1] = 0;

	var stars_big = "<center><nobr>";

	if (value[0] == 0)
	{
		var rate = "Dieses Produkt wurde noch nicht bewertet!";
		for (i=6; i>=1; i--)
		stars_big += "<img src='../images/layout/stern_gelb_inaktiv.png' alt=''>";
	}
	else
	{
		var rate = "Bewertung dieses Produkts durch unsere Kunden:";
		stars_big += "<img src='../images/layout/stern_gelb_aktiv.png' alt='*'>";

		var i;
		if (value[1])
			i=5;
		else
			i=6;

		for (i; i>=1; i--)
		{		
			if (value[0] < i)
				stars_big += "<img src='../images/layout/stern_gelb_aktiv.png' alt='*'>";
			else
			{
				if (value[1])
				{
					var rotate = 10 - value[1];
					stars_big += "<img src='../images/layout/stern_gelb_" +
						rotate + ".png' alt='*'>";
				}
				break;
			}
		}
		for (i; i>1; i--)
			stars_big += "<img src='../images/layout/stern_gelb_inaktiv.png' alt=''>";
	}

	stars_big += "</nobr></center>";
	return rate + stars_big;
}


/**
 *	shows the rating by stars
 */
function stars(starID)
{
	numStars = starID.substring(4, 5);

	for (var i=6; i>=1; i--)
	{
		if (i >= numStars)
			document.mForm['star'+i].src='../images/layout/stern_gelb_aktiv.png';
		else
			document.mForm['star'+i].src='../images/layout/stern_gelb_inaktiv.png';
	}
}


/**
 *	cut the whitespaces at the beginning and end of the param
 *	@param: string to trim
 */
function cut(text)
{
	return text.replace(/^\s+/g, '').replace(/\s+$/g, '');
}


/**
 *	verifying of user input
 */
function check_opinions(productID, userID, head, body)
{
	if (head == Text['headline'] || head.replace(/ /g, "") == '')
	{
		//alert("Sie müssen Ihrem Beitrag eine Überschrift geben!");
		conf('conf_opn_head');
		document.getElementById('save').disabled = false;
		return false;
	}

	if (body == Text['opinion'] || body.replace(/ /g, "") == '')
	{
		//alert("Schreiben Sie Ihren Beitrag in das Meinungsfeld!");
		conf('conf_opn_text');
		document.getElementById('save').disabled = false;
		return false;
	}

	if (showNickLine) // input field for nickname is visible
	{
		var nick = document.mForm.nickname.value;
		
		if (nick == Text['nickname'] || nick.replace(/ /g, "") == '')
		{
			//alert("Geben Sie einen Benutzernamen an!");
			conf('conf_opn_nick');
			document.getElementById('save').disabled = false;
			return false;
		}

		if (nickRed)
		{
			//alert("Dieser Nickname ist leider schon vergeben.");
			conf('conf_opn_nick1');
			document.getElementById('save').disabled = false;
			return false;
		}
	}
	else
		var nick = "";

	if (numStars == 0)
	{
		//alert("Bitte geben Sie eine Wertung zum Produkt ab,\n" +
		//	"indem Sie auf die Sterne klicken.");
		conf('conf_opn_rate');
		document.getElementById('save').disabled = false;
		return false;
	}

	head = cut(head);
	body = cut(body);
	nick = cut(nick);

	ajax_enter_opinions(productID, userID, head, body, numStars, nick);
	return;
}


/**
 *	sends the params to the database
 */
function ajax_enter_opinions(pid, uid, head, body, stars, nick)
{
	var xmlHttp = init();

	// react at change of the state
	xmlHttp.onreadystatechange = function()
	{
		// show loading.gif if the request is in process
		if (xmlHttp.readyState < 4)
			document.getElementById('opinions_load').style.display = 'block';

		// if the request is complete
		if (xmlHttp.readyState == 4)
		{
			document.getElementById('opinions_load').style.display = 'none';

			// write the response
			if (xmlHttp.responseText.substr(0,5) == " #0# ") // validation error
			{
				document.getElementById('save').disabled = false;
				conf('conf_opn_out');
			}
			else if (xmlHttp.responseText.substr(0,5) == " #1# ")//opinions_stop
			{
				document.getElementById('save').disabled = false;
				conf('conf_opn_stop');
			}
			else if (xmlHttp.responseText.substr(0,5) == " #2# ")//nick assigned
			{
				document.mForm.nickname.style.backgroundColor = "#FF0000";
				nickRed=1;
				document.getElementById('save').disabled = false;
				conf('conf_opn_nick1');
			}
			else if (xmlHttp.responseText.substr(0,5) == " #3# ")//disabled
			{
				document.getElementById('save').disabled = false;
				document.getElementById('save').src =
					"../images/layout/bewertung_senden_g.gif";
				conf('conf_opn_disabled');
			}
			else
			{
				document.getElementById("list").innerHTML =
					xmlHttp.responseText;

				document.mForm.headline.style.color = '#acacac';
				document.mForm.headline.value = Text['headline'];
				document.mForm.opinion.style.color = '#acacac';
				document.mForm.opinion.value = Text['opinion'];
				document.getElementById("nickdiv").style.display = 'none';
				showNickLine = false;

				for (var i=6; i>=1; i--)
					document.mForm['star'+i].src =
						'../images/layout/stern_gelb_inaktiv.png';

				numStars = 0;
				count_chars('opinion');
				document.getElementById('save').disabled = false;
				document.getElementById('save').src =
					"../images/layout/bewertung_senden_g.gif";

				ajax_refresh_average_rating(pid);
			}
		}
	}
	// sending the request to the server-script asynchronly
	var url = "../trade_opinion_entry.php";
	var params = "product=" + pid;
	params += "&head=" + encodeURIComponent(head);
	params += "&body=" + encodeURIComponent(body);
	params += "&stars=" + stars;
	params += "&nick=" + encodeURIComponent(nick);

	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 
	xmlHttp.send(params);
}


/**
 *	checks if the typed nickname is valid
 */
function ajax_check_nickname(nick, component)
{
	var xmlHttp = init();

	// react at change of the state
	xmlHttp.onreadystatechange = function()
	{
		// if the request is complete
		if (xmlHttp.readyState == 4)
		{
			// write the response
			if (xmlHttp.responseText == 0)
			{
				component.style.backgroundColor = "#FF0000";
				nickRed=1;
			}
			else
			{
				component.style.backgroundColor = "#FFFFFF";
				nickRed=0;
			}
		}
	}
	// sending the request to the server-script asynchronly
	var url = "../nickname_valid.php";
	var params = "nick=" + encodeURIComponent(nick);
	params += "&noc=" + Math.random(); // no caching

	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 
	xmlHttp.send(params);
}

/**
 *	updates the average rating for the current product if the opinionbox
 *	is reloaded
 */
function ajax_refresh_average_rating(pid)
{
	var xmlHttp = init();

	// react at change of the state
	xmlHttp.onreadystatechange = function()
	{
		// if the request is complete
		if (xmlHttp.readyState == 4)
		{
			// write the response
			document.getElementById('rate').innerHTML =
				rating(xmlHttp.responseText);
		}
	}
	// sending the request to the server-script asynchronly
	var url = "../refresh_opinions_rating.php";
	var params = "product=" + pid;
	params += "&noc=" + Math.random(); // no caching

	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 
	xmlHttp.send(params);
}

