i18n.load("review"); // do i18n magic

function rv_addUser()
{
	name = document.getElementById("rv_user").value;
	comment = document.getElementById("rv_comment").value;
	//document.getElementById("rv_steps").innerHTML += entry;
	table = document.getElementById('rv_steps');
	var tblBody = table.tBodies[0];
	if (tblBody == null)
	{
		tblBody = document.createElement('tbody');
		document.getElementById('rv_steps').appendChild(tblBody);
	}
	if (table.rows[table.rows.length-1].cells.length == 1)
	{
		table.deleteRow(-1);
	}

	if ((table.rows.length>0) && (table.rows[table.rows.length-1].cells[1].innerHTML == name))
	{
		//alert("Der Benutzer wurde zweimal hintereinander ausgew�hlt.");
		alert(i18n.review.user_twice);
		return;
	}

  	var row = tblBody.insertRow(-1);
  	var cell_status = row.insertCell(0);
  	cell_status.className = 'rv_unknown';
  	var cell_name = row.insertCell(1);
  	cell_name.innerHTML = name;
  	var cell_comment = row.insertCell(2);
  	cell_comment.innerHTML = comment;
	var cell_action = row.insertCell(3);
	cell_action.innerHTML = '<a href="#" onclick="rv_deleteUser(this);">X</a>';
}

function rv_deleteUser(ele)
{
	table = document.getElementById('rv_steps');
	curIndex = ele.parentNode.parentNode.rowIndex;
	if ((curIndex-1>0)
		 &&
		 (curIndex+1<table.rows.length)
		 &&
		 (table.rows[curIndex-1].cells[1].innerHTML == table.rows[curIndex+1].cells[1].innerHTML))
	{
		//alert("Der Benutzer wurde zweimal hintereinander ausgew�hlt.");
		alert(i18n.review.user_twice);
		return;
	}
	table.deleteRow(curIndex);
}

function rv_submit()
{
	//get data from table '.$wgScriptPath.'/hallowelt/review/review_dlg_response.php
	var table = document.getElementById('rv_steps');
	var form = document.forms['rv_dialog'];

	if (!rv_date_check()) return false;

	url = wgScriptPath+'/hallowelt/review/review_dlg_response.php?act=insert';
	url += '&rv_pid='+wgArticleId;
//	url += '&rv_editable='+document.forms['rv_dialog'].rv_editable.value;
	var mode = document.forms['rv_dialog'].rv_mode;
	for (i=0; i<mode.length; i++)
		if (mode[i].checked) url += '&rv_mode='+mode[i].value;
	url += '&rv_startdate='+document.forms['rv_dialog'].rv_startdate.value;
	url += '&rv_enddate='+document.forms['rv_dialog'].rv_enddate.value;
	//check if any user is assigned
	if ((table.rows.length==2) && (table.rows[1].cells.length < 4))
	{
		//alert('Sie haben keinen Benutzer eingetragen.');
		alert(i18n.review.no_user);
		return;
	}
	for (i=1; i<table.rows.length; i++)
	{
		url += '&rv_step_name[]='+table.rows[i].cells[1].innerHTML;
		url += '&rv_step_comment[]='+table.rows[i].cells[2].innerHTML;
		switch (table.rows[i].cells[0].className)
		{
			case 'rv_unknown' :
				url += '&rv_step_status[]=-1'; break;
			case 'rv_no' :
				url += '&rv_step_status[]=0'; break;
			case 'rv_yes' :
				url += '&rv_step_status[]=1'; break;
		}
	}

	hw_requestWithAnswerAndReload(url);
}

function rv_submit_del()
{
	//get data from table '.$wgScriptPath.'/hallowelt/review/review_dlg_response.php
	var table = document.getElementById('rv_steps');
	var form = document.forms['rv_dialog'];

	url = wgScriptPath+'/hallowelt/review/review_dlg_response.php?act=delete';
	url += '&rv_pid='+wgArticleId;

	hw_requestWithAnswerAndReload(url);
}

function handleResponse_submit()
{
    if(hw_ajax.readyState == 4){
        var response = hw_ajax.responseText;
		hw_alert(response);
	}
}

function rv_date_check()
{
	startdate = document.getElementById("rv_startdate").value;
	startdate = startdate.split('.');
	enddate = document.getElementById("rv_enddate").value;
	enddate = enddate.split('.');

	start = new Date(startdate[2], startdate[1], startdate[0]);
	end = new Date(enddate[2], enddate[1], enddate[0]);

	if (end <= start)
	{
		//alert("Das Endedatum muss nach dem Startdatum liegen");
		alert(i18n.review.end_b4_start);
		return false;
	}
	return true;

}
/*
function rv_init_cal()
{
	Calendar.setup(
		{
			inputField	: "rv_startdate",
			ifFormat	: "%d.%m.%Y",
			button		: "rv_start_cal",
			align		: "Tr"
		}
	);
	Calendar.setup(
		{
			inputField	: "rv_enddate",
			ifFormat	: "%d.%m.%Y",
			button		: "rv_end_cal",
			align		: "Tr"
		}
	);
}
*/
function hw_show_review()
{
	toggleMessage(wgScriptPath+'/hallowelt/review/rw_dialog.php?action=getForm&pid='+wgArticleId, 'Workflow', 540, 500);
	//hw_load_js(wgScriptPath+'/hallowelt/lib/jscalendar/calendar.js');
	//hw_load_js(wgScriptPath+'/hallowelt/lib/jscalendar/lang/calendar-de.js');
	//hw_load_js(wgScriptPath+'/hallowelt/lib/jscalendar/calendar-setup.js');
	//hw_load_css(wgScriptPath+'/hallowelt/lib/jscalendar/calendar-win2k-1.css');
	hw_load_css(wgScriptPath+'/hallowelt/review/review.css');
	//rv_init_cal();
}
