var host = "http://localhost/dooti/web";
//var host = "http://213.239.199.187";

var public;

function updateByOrt(ort, land) {

	new Ajax.Request(
		"/ajax/getPostleitzahl?ort=" + ort + "&land=" + land,
		{
			method: 'get',
			onSuccess:
				function(transport)
				{
					var response = transport.responseText;
					var contents = response.split("|#|");
					$("plz_td").innerHTML = contents[0];
					if(contents[1])
					{
						for(i = 0; i < $("bundesland").length; i++)
						{
							if($("bundesland")[i].text == contents[1])
							{
								$("bundesland")[i].selected = true;
								break;
							}
						}
					}
				}
		}
	);
}

function updateByPlz(plz, land) {

	new Ajax.Request(
		"/ajax/getOrt?plz=" + plz + "&land=" + land,
		{
			method: 'get',
			onSuccess:
				function(transport)
				{
					var response = transport.responseText;
					var contents = response.split("|#|");
					$("ort_td").innerHTML = contents[0];
					if(contents[1])
					{
						for(i = 0; i < $("bundesland").length; i++)
						{
							if($("bundesland")[i].text == contents[1])
							{
								$("bundesland")[i].selected = true;
								break;
							}
						}
					}
				}
		}
	);
}

function validZipcode()
{
	if (document.getElementById('land').value != "Oesterreich")
	{
		var valid = false;
		var myAjax = new Ajax.Request(
			"/ajax/getValideZipcode?zipcode=" + document.getElementById('plz').value + "&land=" + document.getElementById('land').value,
			{
				method: 'get',
				asynchronous: false,
				onSuccess:
					function(transport)
					{
						var response = transport.responseText;

						if (response == true)
						{
							valid = true;
						}
						else
						{
							valid = false;
						}
					}
			}
		);
		return valid;
	}
	else
	{
		return true;
	}
}

function delJob(jobid)
{
	var answer = confirm("Wollen Sie diesen Job wirklich unwiederruflich entfernen?");
	if(answer) 
	{
		$("tableRow_" + jobid).remove();
		var jobdeleted = false;
		UnTip();
		var myAjax = new Ajax.Request(
		"/ajax/deleteJob?id=" + jobid,
		{
			method: 'get',
			asynchronous: false,
			onSuccess:
				function(transport)
				{
					var response = transport.responseText;

					if (response == "true")
					{
						jobdeleted = true;
					}
					else
					{
						jobdeleted = false;
					}
				}
			}
		);
	}
	return jobdeleted;
}

function activeJob(jobid)
{
	if( $(jobid + "_active").style.display == "none" )
	{
		public = 1;
	}
	else
	{
		public = 0;
	}
	var answer = confirm(public == 0 ? "Wollen Sie diesen Job deaktivieren?" : "Wollen Sie diesen Job wieder aktivieren?");
	if(answer) 
	{
		var jobupdated = false;
		UnTip();
		var myAjax = new Ajax.Request(
		"/ajax/activeJob?id=" + jobid + "&public=" + public,
		{
			method: 'get',
			asynchronous: false,
			onSuccess:
				function(transport)
				{
					var response = transport.responseText;

					if (response == "true")
					{
						jobupdated = true;
					}
					else
					{
						jobupdated = false;
					}
				}
			}
		);
	if(public)
		{
				$(jobid + "_active").style.display = "";
				$(jobid + "_deactive").style.display = "none";
		}
		else
		{
				$(jobid + "_active").style.display = "none";
				$(jobid + "_deactive").style.display = "";
		}
	}
	return jobupdated;
}

function checkJobid(jobid)
{
	var jobfound = false;
	var myAjax = new Ajax.Request(
		"/ajax/getJob?id=" + jobid,
		{
			method: 'get',
			asynchronous: false,
			onSuccess:
				function(transport)
				{
					var response = transport.responseText;

					if (response.indexOf("<li>") > 0)
					{
						var contents = response.split("<li>");
						var count = contents.length - 1;

						if (count > 1)
						{
							jobfound = false;
						}
						else
						{
							jobfound = true;
						}
					}
					else
					{
						jobfound = false;
					}
				}
		}
	);
	return jobfound;
}

function checkJobdesc(jobtitle,fulltime)
{
	jobtitle = httpEncode(jobtitle);
	var jobfound = false;
	var myAjax = new Ajax.Request(
		"/ajax/jobbezeichnungen?bezeichnung=" + jobtitle + "&festanstellung=" + fulltime + "&checkjob=1",
		{
			method: 'get',
			asynchronous: false,
			onSuccess:
				function(transport)
				{
					var response = transport.responseText;

					if (response.indexOf("<li>") > 0)
					{
						var contents = response.split("<li>");
						var count = contents.length - 1;

						if (count > 1)
						{
							jobfound = false;
						}
						else
						{
							jobfound = true;
						}
					}
					else
					{
						jobfound = false;
					}
				}
		}
	);
	return jobfound;
}

function addAbteilungRow(index)
{
	/*
		Change for IE support
	*/
	$('next').value = ++index;
	alert($('next').value);
	//$('count_abteilung').value = $('count_abteilung').value + 1;
	$('load').show();
	new Ajax.Request(
		"/ajax/getNewAbteilung?index=" + $('next').value,
		{
			method: 'get',
			onSuccess:
				function(transport)
				{
					$('load').hide();
					var response = transport.responseText;
					var abteilung = $('abteilung_foo');
					new Insertion.Bottom(abteilung, response);
					displayGarbageImages("garbage_abteilung");
				}
		}
	);
	$("abteilung_counter").value += "," + $('next').value;
}
