function ts_calculateDates(divID)
{
	if (ts_checkvars(divID))
	{
		var taskSerieErsteAusfuehrungResult = $('taskSerieErsteAusfuehrungResult_'+divID);
		var taskSerieNaechsteAusfuehrungResult = $('taskSerieNaechsteAusfuehrungResult_'+divID);
		var taskSerieUeberNaechsteAusfuehrungResult = $('taskSerieUeberNaechsteAusfuehrungResult_'+divID);
		if (taskSerieErsteAusfuehrungResult && taskSerieNaechsteAusfuehrungResult && taskSerieUeberNaechsteAusfuehrungResult)
		{
			var t_typ = 0;
			if ($('t_typ_1_'+divID) && $('t_typ_2_'+divID))
			{
				($('t_typ_1_'+divID).checked) ? t_typ = 1 : true;
				($('t_typ_2_'+divID).checked) ? t_typ = 2 : true;
			}
			var w_wochentage = '';
			for (var i = 0; i < 7; i++)
			{
				($('w_wochentage_'+i+'_'+divID).checked) ? w_wochentage += i : true;
			}
			var m_typ = 0;
			if ($('m_typ_1_'+divID) && $('m_typ_2_'+divID))
			{
				($('m_typ_1_'+divID).checked) ? m_typ = 1 : true;
				($('m_typ_2_'+divID).checked) ? m_typ = 2 : true;
			}
			var j_typ = 0;
			if ($('j_typ_1_'+divID) && $('j_typ_2_'+divID))
			{
				($('j_typ_1_'+divID).checked) ? j_typ = 1 : true;
				($('j_typ_2_'+divID).checked) ? j_typ = 2 : true;
			}
			var aTag_sa = 'N';
			if ($('aTag_sa_'+divID))
			{
				($('aTag_sa_'+divID).checked) ? aTag_sa = 'Y' : true;
			}
			var aTag_so = 'N';
			if ($('aTag_so_'+divID))
			{
				($('aTag_so_'+divID).checked) ? aTag_so = 'Y' : true;
			}
			var aTag_ft = 'N';
			if ($('aTag_ft_'+divID))
			{
				($('aTag_ft_'+divID).checked) ? aTag_ft = 'Y' : true;
			}
			var postBodyStr = 'serieTypID='+$('taskSerie_serieTyp_'+divID).value+
												'&t_typ='+t_typ+
												'&t_intervall='+$('t_intervall_'+divID).value+
												'&w_intervall='+$('w_intervall_'+divID).value+
												'&w_wochentage='+w_wochentage+
												'&m_typ='+m_typ+
												'&m_tagTypID='+$('m_tagTypID_'+divID).options[$('m_tagTypID_'+divID).selectedIndex].value+
												'&m_wochentagTypID='+$('m_wochentagTypID_'+divID).options[$('m_wochentagTypID_'+divID).selectedIndex].value+
												'&m_datumTag='+$('m_datumTag_'+divID).value+
												'&m_intervall_1='+$('m_intervall_1_'+divID).value+
												'&m_intervall_2='+$('m_intervall_2_'+divID).value+
												'&j_typ='+j_typ+
												'&j_monatID_1='+$('j_monatID_1_'+divID).options[$('j_monatID_1_'+divID).selectedIndex].value+
												'&j_datumTag='+$('j_datumTag_'+divID).value+
												'&j_tagTypID='+$('j_tagTypID_'+divID).options[$('j_tagTypID_'+divID).selectedIndex].value+
												'&j_wochentagID='+$('j_wochentagID_'+divID).options[$('j_wochentagID_'+divID).selectedIndex].value+
												'&j_monatID_2='+$('j_monatID_2_'+divID).options[$('j_monatID_2_'+divID).selectedIndex].value+
												'&j_intervall_1='+$('j_intervall_1_'+divID).value+
												'&j_intervall_2='+$('j_intervall_2_'+divID).value+
												'&taskID='+$('taskSerie_taskID_'+divID).value+
												'&leistungID='+$('taskSerie_leistungID_'+divID).value+
												'&tsTyp='+$('taskSerie_tsTyp_'+divID).value+
												'&taskSerie_ersteAusfuehrung='+$('taskSerie_ersteAusfuehrung_'+divID).value+
												'&taskSerie_naechsteAusfuehrung='+$('taskSerie_naechsteAusfuehrung_'+divID).value+
												'&aTag_sa='+aTag_sa+
												'&aTag_so='+aTag_so+
												'&aTag_ft='+aTag_ft;
			switch ($('taskSerie_tsTyp_'+divID).value)
			{
				case 'leistung':
					postBodyStr += '&taskDatum='+$('datumFeld').value+
												 '&taskDatumStd='+$('vonStd').value+
												 '&taskDatumMin='+$('vonMin').value+
												 '&ablaufDatum='+$('ablaufDatum_'+divID).value;
					break;
				default:
					postBodyStr += '&taskDatum='+$('taskdatum').value+
												 '&taskDatumStd='+$('taskdatum_stunde').value+
												 '&taskDatumMin='+$('taskdatum_minute').value;
					break;
			}
			$('taskSerieLoadImage_'+divID) ? $('taskSerieLoadImage_'+divID).style.display = 'inline' : true;
			var url = 'ajax/srv/task_serie_ajax.php';
			new Ajax.Request(url,
				{
					method: 'post',
					postBody: postBodyStr,
					onSuccess: function (transport)
					{
						$('taskSerieLoadImage_'+divID) ? $('taskSerieLoadImage_'+divID).style.display = 'none' : true;
						var response = transport.responseXML;
						var rootElement = response.documentElement;
						var nodeList = rootElement.childNodes;
						for (var i = 0; i < nodeList.length; i++)
						{
							if (nodeList.item(i).nodeType == 1)
							{
								switch (nodeList.item(i).nodeName)
								{
									case 'execution_1':
										taskSerieErsteAusfuehrungResult.innerHTML = ts_getNodeText(nodeList.item(i));
										break;
									case 'execution_2':
										taskSerieNaechsteAusfuehrungResult.innerHTML = ts_getNodeText(nodeList.item(i));
										break;
									case 'execution_3':
										taskSerieUeberNaechsteAusfuehrungResult.innerHTML = ts_getNodeText(nodeList.item(i));
										break;
								}
							}
						}
					}
				}
			);
		}		
	}
}

function ts_getNodeText(theNode)
{
	if (typeof theNode.textContent != 'undefined')
	{
		return theNode.textContent;
	}
	else
	{
		return theNode.text;
	}
}

function ts_makeRed(theElement)
{
	theElement.style.borderWidth = '2px';
	theElement.style.borderColor = 'red';	
}

function ts_makeNormal(theElement)
{
	theElement.style.borderWidth = '';
	theElement.style.borderColor = '';	
}

function ts_checkField(theElement)
{
	var ret = true;
	if (theElement)
	{
		var str = trimString(theElement.value);
		if ((str == '') || (str.search(/^[0-9]+/i) == -1) || (parseInt(str, 10) < 1) || (parseInt(str, 10) > 31))
		{
			ts_makeRed(theElement);
			ret = false;
		}
		else
		{
			ts_makeNormal(theElement);
		}
	}
	return ret;
}

function ts_checkvars(divID)
{
	var foundError = false;
	var tserie = $('taskSerie_serieTyp_'+divID);
	if (tserie)
	{
		switch (tserie.value)
		{
			case '0':
				if ($('t_typ_1_'+divID) && ($('t_typ_1_'+divID).checked == true))
				{
					(ts_checkField($('t_intervall_'+divID)) == false) ? foundError = true : true;
				}
				break;
			case '1':
				(ts_checkField($('w_intervall_'+divID)) == false) ? foundError = true : true;
				var hasCheckedFiels = false;
				for (var i = 0; i < 7; i++)
				{
					if ($('w_wochentage_'+i+'_'+divID))
					{
						($('w_wochentage_'+i+'_'+divID).checked == true) ? hasCheckedFiels = true : true;
					}
				}
				if (hasCheckedFiels == false)
				{
					foundError = true;
					if ($('taskSerieWochentageContainer_'+divID))
					{
						$('taskSerieWochentageContainer_'+divID).className = 'taskSerieWochentage_red';
					}
				}
				else
				{
					if ($('taskSerieWochentageContainer_'+divID))
					{
						$('taskSerieWochentageContainer_'+divID).className = 'taskSerieWochentage_normal';
					}					
				}
				break;
			case '2':
				if ($('m_typ_1_'+divID) && ($('m_typ_1_'+divID).checked == true))
				{					
					(ts_checkField($('m_datumTag_'+divID)) == false) ? foundError = true : true;
					(ts_checkField($('m_intervall_1_'+divID)) == false) ? foundError = true : true;
				}
				if ($('m_typ_2_'+divID) && ($('m_typ_2_'+divID).checked == true))
				{
					(ts_checkField($('m_intervall_2_'+divID)) == false) ? foundError = true : true;
				}
				break;
			case '3':
				if ($('j_typ_1_'+divID) && ($('j_typ_1_'+divID).checked == true))
				{					
					(ts_checkField($('j_datumTag_'+divID)) == false) ? foundError = true : true;
					(ts_checkField($('j_intervall_1_'+divID)) == false) ? foundError = true : true;
				}
				if ($('j_typ_2_'+divID) && ($('j_typ_2_'+divID).checked == true))
				{
					(ts_checkField($('j_intervall_2_'+divID)) == false) ? foundError = true : true;
				}
				break;
		}
		if (foundError == true)
		{
			alert(getLang("support", "ts_fieldsNotCorrect"));
			return false;
		}
		else
		{
			if ($('taskSerie_tsTyp_'+divID).value == 'leistung')
			{
				var leistungdatum = le2_lstDatToJsObject();
				var ablaufdatum = ts_ablaufdatumJsObject(divID);
				if (ablaufdatum < leistungdatum)
				{
					alert(getLang("support", "ts_dateErr"));
					return false;
				}
				else
				{
					return true;
				}
			}
			else
			{
				return true;
			}
		}
	}
	else
	{
		return false;
	}
}

function ts_checkSelect(theSource, theTarget, t, setChecked)
{
	if ($(theSource) && $(theTarget))
	{
		switch (t)
		{
			case 'aTag':
				if ($(theSource).options[$(theSource).selectedIndex].value == '4')
				{
					var theOption = new Option('Arbeitstag', 'aTag', false, false);
					$(theTarget).options[$(theTarget).options.length] = theOption;
					if (setChecked == 'true')
					{
						$(theTarget).options[($(theTarget).options.length - 1)].selected = true;
					}
				}
				else
				{
					for (var i = 0; i < $(theTarget).options.length; i++)
					{
						if ($(theTarget).options[i].value == 'aTag')
						{
							$(theTarget).options[i] = null;
						}
					}
				}
				break;
		}
	}
}

function ts_ablaufdatumJsObject(divID)
{
	var ts_tag = parseInt($('ablaufDatum_'+divID).value.toString().substr(0, 2), 10);
	var ts_mon = parseInt($('ablaufDatum_'+divID).value.toString().substr(2, 2), 10) - 1;
	var ts_jahr = parseInt("20" + $('ablaufDatum_'+divID).value.toString().substr(4, 2), 10);
	var ts_datum = new Date(ts_jahr, ts_mon, ts_tag, 23, 59, 0, 0);
	return ts_datum;
}

function ts_showAusfuehrungsContainer(divID)
{
	if ($('taskSerieAusfuehrungsContainer_'+divID))
	{
		$('taskSerieAusfuehrungsContainer_'+divID).show();
	}
}

function ts_closeIntervallInfos()
{
  if ($('taskSerieIntervallInfo')) $('taskSerieIntervallInfo').remove();
}

function ts_showIntervallInfos(taskID, e)
{
  ts_closeIntervallInfos();
  var theClickEle = Event.element(e);
  var theEle = new Element('div', {'id': 'taskSerieIntervallInfo'});
  document.body.appendChild(theEle);
  theEle.style.top = (theClickEle.cumulativeOffset()[1]+18)+'px';
  theEle.style.left = (theClickEle.cumulativeOffset()[0]-150)+'px';
  theEle.innerHTML = show_load(true);
  new Ajax.Request('ajax/srv/tasks.php?t=showIntervallInfos&taskID='+encodeURIComponent(taskID), {
    onComplete: function (t) {
      theEle.innerHTML = t.responseText;
    }
  });
}
