var nezzoDonate;
function NezzoDonate(){
	var freeAmountField;
	var freeAmountBackup='';
	var minAmount=0;
	var addressMandatory=false;
	var zipMandatory=false;
	var cityMandatory=false;
	this.checkMethode=function(){
		var inputTag=document.getElementById('tx_nezzodonate_pi1_payment_method').getElementsByTagName('input');
		var value='';
		for(var i=0;i<inputTag.length; i++){
			if(inputTag[i].checked){
				value=inputTag[i].value;
				break;
			}
		}
		var amountElement=getLabel('tx_nezzodonate_pi1_amount').parentNode;
		var amountMandatoryElement=document.getElementById('tx_nezzodonate_pi1_amount-mandatory-icon');
		var tmpElement;
		if(value=='credit_slip' || value=='direct_debit'){
			addClass(amountElement,'tx-nezzodonate-pi1-optional');
			removeClass(amountElement,'tx-nezzodonate-pi1-mandatory');
			
			addClass(amountMandatoryElement,'tx-nezzodonate-pi1-mandatory-nonvisible');
			removeClass(amountMandatoryElement,'tx-nezzodonate-pi1-mandatory-icon');
			if(!addressMandatory && document.getElementById('tx_nezzodonate_pi1_address')){
				tmpElement=getLabel('tx_nezzodonate_pi1_address').parentNode;
				addClass(tmpElement,'tx-nezzodonate-pi1-mandatory');
				removeClass(tmpElement,'tx-nezzodonate-pi1-optional');
				addClass(document.getElementById('tx_nezzodonate_pi1_address-mandatory-icon'),'tx-nezzodonate-pi1-mandatory-icon');
				removeClass(document.getElementById('tx_nezzodonate_pi1_address-mandatory-icon'),'tx-nezzodonate-pi1-mandatory-nonvisible');
			}
			if(!zipMandatory && document.getElementById('tx_nezzodonate_pi1_zip')){
				tmpElement=getLabel('tx_nezzodonate_pi1_zip').parentNode;
				addClass(tmpElement,'tx-nezzodonate-pi1-mandatory');
				removeClass(tmpElement,'tx-nezzodonate-pi1-optional');
				addClass(document.getElementById('tx_nezzodonate_pi1_zip-mandatory-icon'),'tx-nezzodonate-pi1-mandatory-icon');
				removeClass(document.getElementById('tx_nezzodonate_pi1_zip-mandatory-icon'),'tx-nezzodonate-pi1-mandatory-nonvisible');
			}
			if(!cityMandatory && document.getElementById('tx_nezzodonate_pi1_city')){
				tmpElement=getLabel('tx_nezzodonate_pi1_city').parentNode;
				addClass(tmpElement,'tx-nezzodonate-pi1-mandatory');
				removeClass(tmpElement,'tx-nezzodonate-pi1-optional');
				addClass(document.getElementById('tx_nezzodonate_pi1_city-mandatory-icon'),'tx-nezzodonate-pi1-mandatory-icon');
				removeClass(document.getElementById('tx_nezzodonate_pi1_city-mandatory-icon'),'tx-nezzodonate-pi1-mandatory-nonvisible');
			}
		}else{
			addClass(amountElement,'tx-nezzodonate-pi1-mandatory');
			removeClass(amountElement,'tx-nezzodonate-pi1-optional');
			
			addClass(amountMandatoryElement,'tx-nezzodonate-pi1-mandatory-icon');
			removeClass(amountMandatoryElement,'tx-nezzodonate-pi1-mandatory-nonvisible');
			if(!addressMandatory && document.getElementById('tx_nezzodonate_pi1_address')){
				tmpElement=getLabel('tx_nezzodonate_pi1_address').parentNode;
				addClass(tmpElement,'tx-nezzodonate-pi1-optional');
				removeClass(tmpElement,'tx-nezzodonate-pi1-mandatory');
				addClass(document.getElementById('tx_nezzodonate_pi1_address-mandatory-icon'),'tx-nezzodonate-pi1-mandatory-nonvisible');
				removeClass(document.getElementById('tx_nezzodonate_pi1_address-mandatory-icon'),'tx-nezzodonate-pi1-mandatory-icon');
			}
			if(!zipMandatory && document.getElementById('tx_nezzodonate_pi1_zip')){
				tmpElement=getLabel('tx_nezzodonate_pi1_zip').parentNode;
				addClass(tmpElement,'tx-nezzodonate-pi1-optional');
				removeClass(tmpElement,'tx-nezzodonate-pi1-mandatory');
				addClass(document.getElementById('tx_nezzodonate_pi1_zip-mandatory-icon'),'tx-nezzodonate-pi1-mandatory-nonvisible');
				removeClass(document.getElementById('tx_nezzodonate_pi1_zip-mandatory-icon'),'tx-nezzodonate-pi1-mandatory-icon');
			}
			if(!cityMandatory && document.getElementById('tx_nezzodonate_pi1_city')){
				tmpElement=getLabel('tx_nezzodonate_pi1_city').parentNode;
				addClass(tmpElement,'tx-nezzodonate-pi1-optional');
				removeClass(tmpElement,'tx-nezzodonate-pi1-mandatory');
				addClass(document.getElementById('tx_nezzodonate_pi1_city-mandatory-icon'),'tx-nezzodonate-pi1-mandatory-nonvisible');
				removeClass(document.getElementById('tx_nezzodonate_pi1_city-mandatory-icon'),'tx-nezzodonate-pi1-mandatory-icon');
			}
		}
		inputTag=null;
		tmpElement=null;
	}
	this.checkAmount=function(){
		var inputTag=document.getElementById('tx_nezzodonate_pi1_amount').getElementsByTagName('input');
		var value='';
		for(var i=0;i<inputTag.length; i++){
			if(inputTag[i].checked){
				value=inputTag[i].value;
				break;
			}
		}
		var amountElement=getLabel('tx_nezzodonate_pi1_amount').parentNode;
		if(value!='free'){
			if(freeAmountField.value!=''){
				freeAmountBackup=freeAmountField.value;
			}
			freeAmountField.value='';
		}else{
			freeAmountField.value=freeAmountBackup;
		}
		inputTag=null;
	}
	var addClass=function(element,value){
		if(!element.className){
			element.className = value;
		}else{
			if(element.className.indexOf(value) == -1){
				var newClassName = element.className;
				newClassName+= " ";
				newClassName+= value;
				element.className = newClassName;
			}
		}
	}
	var removeClass=function(element,value){
		if(element.className){
			var pattern= new RegExp(value);
			element.className=element.className.replace(pattern,'');
		}
	}
	var getLabel=function(inputID){
		var labels=document.getElementsByTagName('label');
		for(var i=0; i<labels.length; i++) {
			if(labels[i].htmlFor==inputID)
				return labels[i];
		}
		return false;
	}
	this.prepare=function(){
		if (!document.getElementsByTagName) return false;
		if (!document.getElementById) return false;
		if (!document.getElementById('tx_nezzodonate_pi1_payment_method')) return false;
		if (!document.getElementById('tx_nezzodonate_pi1_amount')) return false;
		var inputTag=document.getElementById('tx_nezzodonate_pi1_payment_method').getElementsByTagName('input');
		for(var i=0;i<inputTag.length; i++){
			inputTag[i].onclick=function(){
				nezzoDonate.checkMethode();
			};
		}
		var inputTag=document.getElementById('tx_nezzodonate_pi1_amount').getElementsByTagName('input');
		for(var i=0;i<inputTag.length; i++){
			if(inputTag[i].className.indexOf('freeamount') != -1) {
				freeAmountField=inputTag[i];
				var matchs=freeAmountField.className.match(/min(\d+)/);
				minAmount=parseInt(matchs[1],10);
				matchs=null;
				freeAmountField.onkeyup=function(){
					if(parseInt(this.value,10)>=minAmount){
						document.getElementById('tx_nezzodonate_pi1_amountfree').checked=true;
					}else{ 
						document.getElementById('tx_nezzodonate_pi1_amount').getElementsByTagName('input')[0].checked=true
					}
					
				};
			}else{
				inputTag[i].onclick=function(){
					nezzoDonate.checkAmount();
				};
			}
		}
		if(document.getElementById('tx_nezzodonate_pi1_payment_methoddirect_debit')){
			var aTags=getLabel('tx_nezzodonate_pi1_payment_methoddirect_debit').getElementsByTagName('a');
			if(aTags.length>0){
				aTags[0].onclick=function(){
					window.location=this.href;
				}
				document.getElementById('tx_nezzodonate_pi1_payment_methoddirect_debit').setAttribute('onclick','window.location="'+aTags[0].href+'";');
			}
			aTags=null;
		}
		var spanTag;
		if(document.getElementById('tx_nezzodonate_pi1_address')){
			if(!document.getElementById('tx_nezzodonate_pi1_address-mandatory-icon')){
				spanTag=document.createElement('span');
				spanTag.id='tx_nezzodonate_pi1_address-mandatory-icon';
				addClass(spanTag,'tx-nezzodonate-pi1-mandatory-nonvisible');
				spanTag.appendChild(document.createTextNode(' *'));
				getLabel('tx_nezzodonate_pi1_address').appendChild(spanTag);
			}else{
				addressMandatory=true;
			}
		}
		if(document.getElementById('tx_nezzodonate_pi1_zip')){
			if(!document.getElementById('tx_nezzodonate_pi1_zip-mandatory-icon')){
				spanTag=document.createElement('span');
				spanTag.id='tx_nezzodonate_pi1_zip-mandatory-icon';
				addClass(spanTag,'tx-nezzodonate-pi1-mandatory-nonvisible');
				spanTag.appendChild(document.createTextNode(' *'));
				getLabel('tx_nezzodonate_pi1_zip').appendChild(spanTag);
			}else{
				zipMandatory=true;
			}
		}
		if(document.getElementById('tx_nezzodonate_pi1_city')){
			if(!document.getElementById('tx_nezzodonate_pi1_city-mandatory-icon')){
				spanTag=document.createElement('span');
				spanTag.id='tx_nezzodonate_pi1_city-mandatory-icon';
				addClass(spanTag,'tx-nezzodonate-pi1-mandatory-nonvisible');
				spanTag.appendChild(document.createTextNode(' *'));
				getLabel('tx_nezzodonate_pi1_city').appendChild(spanTag);
			}else{
				cityMandatory=true;
			}
		}
		inputTag=null;
		spanTag=null;
		nezzoDonate.checkMethode();
		return true;
	}
}
function prepareNezzoDonate(){
	nezzoDonate=new NezzoDonate();
	nezzoDonate.prepare();
}
if(typeof window.addLoadEvent !== 'function'){
	function addLoadEvent(func){
		if(window.addEventListener){
			window.addEventListener('load',func,false);
		}else if(window.attachEvent){
			window.attachEvent('onload',func);
		}else{
			var oldonload = window.onload;
			if (typeof window.onload != 'function'){
				window.onload = func;
			}else{
				window.onload = function(){
					oldonload();
					func();
				}
			}
		}
	}
}
addLoadEvent(prepareNezzoDonate);
