function $(id) {
	return document.getElementById(id);
}

function checkAmountOnChange(obj, amount) {
	// {{{
	if (isNaN(obj.value) || obj.value < 1) {
		obj.value = amount;
	}
	// }}}
}

function checkAmountOnChangeSC(obj, valid) {
	// {{{
	if (obj.value % valid > 0) {
		obj.style.color = '#FFFFFF';
		obj.style.backgroundColor = '#D5351B';
		alert('Bitte geben Sie ein Vielfaches der Inhaltsmenge der VE dieses Produktes ein.');
		return false;
	}
	
	obj.style.color = '#000000';
	obj.style.backgroundColor = '#FFFFFF';
	return true;
	// }}}
}

function checkAmountOnKeyPress(e, obj, valid) {
	// {{{
	var keycode;
	if (window.event)
		keycode = window.event.keyCode;
	else if (e)
		keycode = e.which;
	if (keycode == 13)
	{
		if (obj.value % valid > 0)
		{
			obj.style.color = '#FFFFFF';
			obj.style.backgroundColor = '#D5351B';
			alert('Bitte geben Sie ein Vielfaches der Inhaltsmenge der VE dieses Produktes ein.');
			return false;
		}
	}

	obj.style.color = '#000000';
	obj.style.backgroundColor = '#FFFFFF';
	return true;
	// }}}
}

var amountArr = new Array();
var c = 0;
function remAmount(field, valid) {
	// {{{
	amountArr[c] = new Array();
	amountArr[c]['id'] = field;
	amountArr[c]['valid'] = valid;
	c++;
	// }}}
}

function checkAmountOnSubmit(field, valid) {
	// {{{
	obj = document.getElementById(field);
	if (isNaN(obj.value) || obj.value < 1 || obj.value % valid > 0) {
		obj.style.color = '#FFFFFF';
		obj.style.backgroundColor = '#D5351B';
		alert('Bitte geben Sie ein Vielfaches der Inhaltsmenge der VE dieses Produktes ein.');
		return false;
	}
	
	obj.style.color = '#000000';
	obj.style.backgroundColor = '#FFFFFF';
	return true;
	// }}}
}

function checkAmountOnSubmitSC() {
	// {{{
	var error = false;
	
	for (i = 0; i < amountArr.length; i++) {
		obj = document.getElementById(amountArr[i]['id']);
		if (obj.value % amountArr[i]['valid'] > 0) {
			error = true;
			obj.style.color = '#FFFFFF';
			obj.style.backgroundColor = '#D5351B';
		}
	}
	
	if (error) {
		alert('Bitte geben Sie in den rot makierten Feldern ein Vielfaches der Inhaltsmenge der VE des jeweiligen Produktes ein.');
		return false;
	}
	return true;
	// }}}
}

function openWindow(url, width, height, print) {
	// {{{
	var w = null;
	w = window.open(url, '', 'width=' + width +',height=' + height);
	w.focus();
	
	if(print == true) w.print();
	// }}}
}

function closeWindow(url) {
	// {{{
	if (url) window.opener.location.href = url;
	window.opener.focus();
	window.close();
	// }}}
}

function validateMail(email) {
	// {{{
	if (email.match(/^[a-zA-Z0-9_\.-]{2,}@[a-zA-Z0-9-]{2,}\.([a-zA-Z0-9-]{1,}\.)*[a-zA-Z]{2,}$/)) {
		return true;
	}
	else {
		return false;
	}
	// }}}
}

function closeWindowByKeyDown(keyEvent) {
	// {{{
	if (!keyEvent) {
		keyEvent = window.event;
	}
	if (keyEvent.which) {
    	key = keyEvent.which;
 	}
 	else if (keyEvent.keyCode) {
    	key = keyEvent.keyCode;
    }
    
    if (key == 88) {
    	closeWindow();
    }
	// }}}
}

function confirmCommand(message) {
	// {{{
	return confirm(unescape(message));
	// }}}
}

function sendOrder(disablebuttons, sendstatus) {
	// {{{
	if (sendstatus == false) return false;
	
	if (disablebuttons) {
		for (var i = 0; i < disablebuttons.length; i++) {
			if ($(disablebuttons[i])) {
				$(disablebuttons[i]).style.visibility = 'hidden';
			}
		}	
	}
	
	viewportheight = (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight;
		
	x = Math.round((document.body.clientWidth / 2) -109);
	y = Math.round((viewportheight / 2));

	$('waitbox').style.left = x + 'px';
	$('waitbox').style.top = y + 'px';
	//nn
	$('waitbox').left = x;
	$('waitbox').top = y;
	//op
	if (navigator.userAgent.toLowerCase().indexOf('opera') != -1) {
		$('waitbox').style.pixelLeft = x;
		$('waitbox').style.pixelTop = y - document.body.scrollTop;
	}
	
	// show div
	$('waitbox').style.visibility = 'visible';
	
	return true;
	// }}}
}

function centerLayer() {
	// {{{
	$('waitbox').style.width = screen.availWidth / 4;
	
	x = document.body.clientWidth / 2 - $('waitbox').offsetWidth / 2 + document.body.scrollLeft;
	y = document.body.clientHeight / 2 - $('waitbox').offsetHeight / 2 + document.body.scrollTop;
	
	$('waitbox').style.position = 'absolute';
	$('waitbox').style.left = x ;
	$('waitbox').style.top = y ;
	
	setTimeout('centerLayer()', 100);
	// }}}
}

function toggle(toggleid, openimg, closeimg) {
	// {{{
	if (toggleid[0] && $(toggleid[0])) {
		if ($(toggleid[0]).style.display == 'none') {
			$(toggleid[0]).style.display = 'block';
			if (toggleid[1] && $(toggleid[1]) && closeimg) {
				$(toggleid[1]).src = closeimg;
			}
		}
		else {
			$(toggleid[0]).style.display = 'none';
			if (toggleid[1] && $(toggleid[1]) && openimg) {
				$(toggleid[1]).src = openimg;
			}
		}
	}
	// }}}
}

function checkLogin() {
	// {{{
	var error = 0;
	var msg = 'Es sind folgende Fehler beim Login aufgetreten:\n';
	
	if ($('login_mud_email').value == '') {
		error = 1;
		msg += '- Bitte tragen Sie eine E-Mail-Adresse ein\n';
	}
	
	if ($('login_mud_password').value == '') {
		error = 1;
		msg += '- Bitte tragen Sie ein Passwort ein\n';
	}
	
	if (error != 0) {
		alert(unescape(msg));
		return false;
	}
	return true;
	// }}}
}

function checkRegister(reg_type) {
	// {{{
	var error = 0;
	var msg = "Es sind folgende Fehler bei der Registrierung aufgetreten:\n";
	
	if (reg_type == 'loginname' &&
		$('mud_loginname').value == "") {
		error = 1;
		msg += "- Bitte tragen Sie einen Benutzernamen ein\n";
	}
	
	if ($('mud_email').value == "" ||
		!validateMail($('mud_email').value)) {
		error = 1;
		msg += "- Bitte tragen Sie eine g%FCltige E-Mail-Adresse ein\n";
	}
	
	if ($('mud_password').value == "") {
		error = 1;
		msg += "- Bitte tragen Sie ein Passwort ein\n";
	}	
	if ($('mud_password_repeat').value == "") {
		error = 1;
		msg += "- Bitte trage eine Passwort-Wiederholung ein\n";
	}
	if ($('mud_password_repeat').value != "" && $('mud_password').value != "" &&
		$('mud_password_repeat').value != $('mud_password').value	
	) {
		error = 1;
		msg += "- Passwort und Passwort-Wiederholung stimmen nicht %FCberein\n";
	}	
	if ($('mud_password').value != "" && ($('mud_password').value).length < 5) {
		error = 1;
		msg += "- Ihr Passwort muss min. 5 Buchstaben und Zahlen enthalten\n";
	}
	
	if ($('mup_vorname_r').value == "") {
		error = 1;
		msg += "- Bitte tragen Sie Ihren Vornamen ein\n";
	}
	
	if ($('mup_name_r').value == "") {
		error = 1;
		msg += "- Bitte tragen Sie Ihren Nachnamen ein\n";
	}
	
	if ($('mup_firma_r').value == "") {
		error = 1;
		msg += "- Bitte tragen Sie Ihre Firma ein\n";
	}
	
	if ($('mup_anschrift_r').value == "") {
		error = 1;
		msg += "- Bitte tragen Sie eine Stra%DFe ein\n";
	}
	
	if ($('mup_plz_r').value == "") {
		error = 1;
		msg += "- Bitte tragen Sie eine PLZ ein\n";
	}
	
	if ($('mup_ort_r').value == "") {
		error = 1;
		msg += "- Bitte tragen Sie einen Ort ein\n";
	}
	
	if ($('mup_sco_iso2_r').value == "") {
		error = 1;
		msg += "- Bitte w%E4hlen Sie ein Land aus\n";
	}
	
	if ($('mup_telefon_r').value == "") {
		error = 1;
		msg += "- Bitte tragen Sie eine Telefonnummer ein\n";
	}
	
	if (error != 0) {
		alert(unescape(msg));
		return false;
	}	
	
	return true;
	// }}}
}

function Rating(max_rate, img_src_hot, img_src_cold, ratingTitles) {
	// {{{
	this.max_rate = max_rate;
	this.img_src_hot = '/homepage/media/templatedata/' + img_src_hot;
	this.img_src_cold = '/homepage/media/templatedata/' + img_src_cold;
	this.ratingTitles = ratingTitles;
	this.rating_title = '';
	
	
	this.setRating = function(position) {
		for(var i = 0; i < this.max_rate; i++) {
			if(i + 1 <= position + 1) {
				document.getElementById('rating_' + i).src = this.img_src_hot;
			} else {
				document.getElementById('rating_' + i).src = this.img_src_cold;
			}
		}
		document.getElementById('rating_title').innerHTML = this.ratingTitles[position];
	};
	
	this.restore = function() {
		var value = document.getElementById('spr_points').value;
		for(var i = 0; i < this.max_rate; i++) {
			if(i+1 <= value) {
				document.getElementById('rating_' + i).src = this.img_src_hot;
			} else {
				document.getElementById('rating_' + i).src = this.img_src_cold;
			}
		}
		document.getElementById('rating_title').innerHTML = this.rating_title;
	};
	
	this.saveRating = function(position) {
		document.getElementById('spr_points').value = position + 1;
		this.rating_title = document.getElementById('rating_title').innerHTML;
	}
	
	this.setDefaultRating = function(position) {
		if(position) {
			this.setRating(position-1);
			this.saveRating(position-1);
		}
	}
	// }}}
}

function activateLoading() {
	onclick=ajax_waitboxPosition;
	document.getElementById('ajax_waitbox').style.visibility='visible';

}

function ajax_waitboxPosition (Ereignis) {
  if (document.layer) {
    document.layer[0].left = Ereignis.pageX;
    document.layer[0].top = Ereignis.pageY;
  } else if (document.getElementById) {
    document.getElementById("ajax_waitbox").style.left = Ereignis.pageX + 12 +"px";
    document.getElementById("ajax_waitbox").style.top = Ereignis.pageY - 2 + "px";
  }
}
