window.onload = function () {
	a = $('requestform').getElementsByTagName("input");
	for ( i = 0; i < a.length; i++ ) {
		if ( a[i].type != "checkbox" && a[i].type != "radio" ) {
			a[i].onblur = function () { this.style.border = '1px solid #aaa'; }
		} else {
			a[i].onblur = function () { this.style.border = ''; }
			a[i].style.border = '';
		}
	}
}

function findPos(obj) {
	var curleft = curtop = 0;

	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function loadSuggestions() {
	if ( $F('veranstaltung') != "" ) {
		$('suggestions').style.left = findPos($('veranstaltung'))[0] - 85 + "px";
		$('suggestions').style.top = (findPos($('veranstaltung'))[1] + 22) + "px";
		new Ajax.Updater("suggestioncontent", "request.list.php", { method: "GET", parameters: "messe="+$F('veranstaltung'), onComplete: function(res) { if ( res.responseText == "no match" ) hideSuggestions(); else $('suggestions').style.display = 'block'; } } );
	} else {
			hideSuggestions();
	}
}

function hideSuggestions() {
	$('suggestions').style.display = 'none';
}

function takeSuggestion(fair, from, to, postal, city) {
	$('veranstaltung').value = fair;
	$('von').value = from;
	$('bis').value = to;
	$('plz').value = postal;
	$('ort').value = city;
	hideSuggestions();
}


function checkStep1() {
	if ( $F('veranstaltung') == '' ) {
		$('veranstaltung').focus();
		alert("Bitte geben Sie den Titel der Veranstaltung an.");
		$('veranstaltung').style.border = "1px solid #900";
		return false;
	}
	
	if ( $F('plz') == '' || isNaN($F('plz')) ) {
		$('plz').focus();
		alert("Bitte geben Sie die Postleitzahl an.");
		$('plz').style.border = "1px solid #900";
		return false;
	}
	
	if ( $F('ort') == '' ) {
		$('ort').focus();
		alert("Bitte geben Sie den Ort der Veranstaltung an.");
		$('ort').style.border = "1px solid #900";
		return false;
	}
	
	if ( $F('von') == '' || $F('von') == "TT.MM.JJJJ" ) {
		$('von').focus();
		alert("Bitte geben Sie den Beginn der Veranstaltung an.");
		$('von').style.border = "1px solid #900";
		return false;
	}
	
	if ( $F('bis') == '' || $F('bis') == "TT.MM.JJJJ" ) {
		$('bis').focus();
		alert("Bitte geben Sie das Ende der Veranstaltung an.");
		$('bis').style.border = "1px solid #900";
		return false;
	}
	
	if ( $F('anzahl') == '' || isNaN($F('anzahl')) ) {
		$('anzahl').focus();
		alert("Bitte geben Sie Ihre gewünschte Anzahl Messehostessen an.");
		$('anzahl').style.border = "1px solid #900";
		return false;
	}
	
	if ( !$('stand').checked && !$('info').checked && !$('cat').checked && !$('promo').checked ) {
		$('stand').focus();
		alert("Bitte geben Sie Ihre gewünschten Personaltypen an.");
		$('anzahl').style.border = "1px solid #900";
		return false;
	}
	
	$('requestform').submit();
}

_state = new Array();
_counter = 0;
_limit = 0;
_choice = new Array();

function setUp(limit) {
	_limit = limit;
}

function switchImage(id, out) {
	g = 0;
	if ( out && _state[id] != true ) g = 1;
	$('img'+id).src = "pimage.php?id="+id+"&w=100&g="+g;
}

function switchImageState(id) {
	if ( _state[id] == true  ) {
		_state[id] = false;
		deleteItem(id);
		$('img'+id).style.backgroundColor = '';
		_counter--;
	} else {
		if ( _limit - _counter > 0 ) {
			_state[id] = true;
			_choice.push(id);
			$('img'+id).style.backgroundColor = '#435eb6';
			_counter++;
		} else {
			alert("Sie haben bereits "+_limit+" Mitarbeiter ausgewählt.\n\nUm Ihre Auswahl zu ändern, können Sie die alte Auswahl einfach per Mausklick wiederrufen.");
		}
	}
	$('counter').innerHTML = _limit - _counter;
}

function deleteItem(id) {
	x = 0;
	for ( i = 0; i < _choice.length; i++ ) {
		if ( _choice[i] == id ) {
			x = i;
		}
	}
	_choice.splice(x, 1);
}

function checkStep2() {
	$('auswahl').value = _choice.join("|");
	$('requestform').submit();
}

function checkStep3() {
	if ( $F('ffirma') == '' ) {
		$('ffirma').focus();
		alert("Bitte geben Sie Ihren Firmennamen an.");
		$('ffirma').style.border = "1px solid #900";
		return false;
	}
	
	if ( $F('fstrasse') == '' ) {
		$('fstrasse').focus();
		alert("Bitte geben Sie die Anschrift Ihrer Firma an.");
		$('fstrasse').style.border = "1px solid #900";
		return false;
	}
	
	if ( $F('fplz') == '' || isNaN($F('fplz')) ) {
		$('fplz').focus();
		alert("Bitte geben Sie die Anschrift Ihrer Firma an.");
		$('fplz').style.border = "1px solid #900";
		return false;
	}
	
	if ( $F('fort') == '' ) {
		$('fort').focus();
		alert("Bitte geben Sie die Anchrift Ihrer Firma an.");
		$('fort').style.border = "1px solid #900";
		return false;
	}
	
	if ( $F('fname') == '' ) {
		$('fname').focus();
		alert("Bitte geben Sie Ihren Namen an.");
		$('fname').style.border = "1px solid #900";
		return false;
	}
	
	if ( $F('ftelefon') == '' ) {
		$('ftelefon').focus();
		alert("Bitte geben Sie Ihre Telefondurchwahl an.");
		$('ftelefon').style.border = "1px solid #900";
		return false;
	}
	
	if ( !checkMail($F('fmail')) ) {
		$('fmail').focus();
		alert("Bitte geben Sie eine gültige E-Mail-Adresse an.");
		$('fmail').style.border = "1px solid #900";
		return false;
	}
	
	if ( $F('kontaktaufnahme') == 0 ) {
		$('kontaktaufnahme').focus();
		alert("Bitte teilen Sie uns mit, wie Sie auf uns gestoßen sind.");
		$('kontaktaufnahme').style.border = "1px solid #900";
		return false;
	}
	
	$('requestform').submit();
}

function checkStep4() {
	$('requestform').submit();
}
