var clearOnFocusArr = new Array()

function isValidEmail(email){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	if (email.search(validRegExp) == -1){
      return false;
    } 
    return true; 
}


function clearOnFocus(id){
	
	if(clearOnFocusArr[id] != 1){
		$(id).value = ''
		clearOnFocusArr[id] = 1
	}
}

function dezRound(num,pos) { if(pos<0) { pos=Math.pow(10,Math.abs(pos)); return Math.round(num/pos)*pos; } else if(pos>0) { pos=Math.pow(10,pos); return Math.round(num*pos)/pos; } else { return Math.round(num); } }


function submitForm(id){
	
	inputText = '';
	
	if($(id + 'InputText') != undefined){
		inputText = $(id + 'InputText').value
	}
	
	//alert($(id + 'Input').value + ', ' + inputText)
	
	if(id == 'formNewsletter'){
		subscribeNewsletter('ajax/newsletter.php', $(id + 'Input').value)
	}

}


function subscribeNewsletter(url, email) {
    if(email!='' && isValidEmail(email)) {
        $('newsletterLoading').show();
        $('formNewsletter').hide();
        $('newsletterError').hide();
        $('newsletterOk').hide();
        url = url + '?email=' + email;
        new Ajax.Request(url, {
            method: 'get',
            onSuccess: function(transport) {
                $('newsletterLoading').hide();
                //$('formNewsletter').show();
                if(transport.responseText == 'ok') {
                    $('newsletterError').hide();
                    $('newsletterOk').show();
                } else {
                    $('newsletterError').show();
                    $('newsletterOk').hide();
                }
            }
        });
    }
}





function formatPrice(price, currency) {
    val = parseInt(price * 100)
    val = val / 100
    val = val.toFixed(2)
	if(currency != undefined){
		val = currency + ' ' + val.replace(/\./,',')
	}
    return val
}






function priceCalculatorSlim(){

	pcVal = $('pcVal').value
	
	if($('minCards')){
	   $('minCards').className = 'anmerkung'
	}
	if(pcVal < parseInt($('minAmount').value) && pcVal!= 0){
		pcVal = $('minAmount').value	
		$('pcVal').value = pcVal
	}
	
	
	
	piecePrice = 0
	piecePriceBr = 0
	blockPricingOld = $('blockPricing' + 1).value
	blockPricingBruttoOld = $('blockPricingBrutto' + 1).value
	blockAmountOld = $('blockAmount' + 1).value
	shippingPriceOld = $('blockShipping' + 1).value
	sonstiges = 0
	sonstigesBrutto = 0
	
	for(i=1; i<50; i++){
		if($('blockAmount' + i) != undefined){
			if(parseInt(pcVal) >= $('blockAmount' + i).value){
				piecePrice = $('blockPricing' + i).value
				piecePriceBr = $('blockPricingBrutto' + i).value
				shippingPrice = $('blockShipping' + i).value
			} else {
				piecePrice = blockPricingOld
				piecePriceBr = blockPricingBruttoOld
				shippingPrice = shippingPriceOld
				break
			}
			blockPricingOld = $('blockPricing' + i).value
			blockPricingBruttoOld = $('blockPricingBrutto' + i).value
			blockAmountOld = $('blockAmount' + i).value
			shippingPriceOld = $('blockShipping' + i).value
		} else {
			break	
		}
	}
	
	if(pcVal == '0'){
		shippingPrice = 0
	}
	
	vatTax = $('vat').value
	gross = pcVal * piecePrice / vatTax
	
	

	





	//alert('pcVal:'+pcVal+', piecePriceBr:'+piecePriceBr)
	$('endpriceBrutto').innerHTML = 'Brutto: ' + formatPrice(((pcVal * piecePriceBr)), '&euro;')
	$('endpriceNetto').innerHTML = 'Netto: ' + formatPrice((pcVal * piecePrice), '&euro;')
	
	
	$('ca').value = pcVal
	
	// Send Price
	url = 'ajax/amountinform.php?pcVal=' + pcVal;
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
            if(transport.responseText == 'ok') {
            } else {
            }
        }
    });
	
	
}






function priceCalculator(firsttime){

	pcVal = $('pcVal').value
	
	if($('minCards')){
	   $('minCards').className = 'anmerkung'
	}
	if(pcVal < parseInt($('minAmount').value) && pcVal!= 0){
		pcVal = $('minAmount').value	
		$('pcVal').value = pcVal
		$('minCards').className = 'anmerkungError'
	}
	
	
	
	piecePrice = 0
	piecePriceBr = 0
	blockPricingOld = $('blockPricing' + 1).value
	blockPricingBruttoOld = $('blockPricingBrutto' + 1).value
	blockAmountOld = $('blockAmount' + 1).value
	shippingPriceOld = $('blockShipping' + 1).value
	sonstiges = 0
	sonstigesBrutto = 0
	
	for(i=1; i<50; i++){
		if($('blockAmount' + i) != undefined){
			if(parseInt(pcVal) >= $('blockAmount' + i).value){
				piecePrice = $('blockPricing' + i).value
				piecePriceBr = $('blockPricingBrutto' + i).value
				shippingPrice = $('blockShipping' + i).value
			} else {
				piecePrice = blockPricingOld
				piecePriceBr = blockPricingBruttoOld
				shippingPrice = shippingPriceOld
				break
			}
			blockPricingOld = $('blockPricing' + i).value
			blockPricingBruttoOld = $('blockPricingBrutto' + i).value
			blockAmountOld = $('blockAmount' + i).value
			shippingPriceOld = $('blockShipping' + i).value
		} else {
			break	
		}
	}
	
	if(pcVal == '0'){
		shippingPrice = 0
	}
	
	vatTax = $('vat').value
	gross = pcVal * piecePrice / vatTax
	
	
	// Briefumschläge aufaddieren
	if(document.form.envelopes[0]){
	for(i=0; i<3; i++){
    	if(document.form.envelopes[i].checked){
			envPrice =  $('envPricing' + i).value;
			envPriceNetto =  $('envPricingNetto' + i).value;
			break;
		}
	}
	} else {
		envPrice = 0
		envPriceNetto = 0
		$('envelopeBox').style.display = 'none'
	}
	
	// Logoeindruck
	if(document.form.logo.checked){
		sonstigesBrutto = sonstigesBrutto + 35.00;
		sonstiges = sonstiges + 29.42;
	} 
	
	// Individualisierung einblenden
	if(parseInt(pcVal)<30){
		$('individualize').style.display = 'none'
		sonstiges = 0;
	} else {
		$('individualize').style.display = 'block'
	}
	
	if($('onStock')){
		if($('onlyOnStock')){
			$('onlyOnStock').className = 'anmerkung'
		}
		if(parseInt(pcVal) > parseInt($('onStock').value) && parseInt(pcVal) < 30){
			$('pcVal').value = 30
			pcVal = 30
			$('onlyOnStock').className = 'anmerkungError'
		}
	}
	
	
	
	// Voucher
	
	// Get Voucher
	if(firsttime != 1 && $('voucher').value != ''){
		url = 'ajax/getVoucher.php?id=' + $('voucher').value;
		new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				if(transport.responseText != 'error') {
					res = transport.responseText.split('|')
					voucherRes = Array()
					for(t=0; t<res.length; t++){
						ress = res[t].split('=')
						voucherRes[t] = ress[1]
					}
					//// EARLY BIRD RABATT: voucherDiscount = dezRound( (parseFloat(voucherRes[2])*-1 + 0.10) ,2)
					if(voucherRes[1]=='prz'){
						voucherDiscount = dezRound( (parseFloat(voucherRes[2])*-1) ,2)
						//alert(voucherRes[2])
						voucherDiscountPercent = voucherDiscount * 100 + '%'
						voucherDiscountTotalBr = (piecePriceBr * voucherDiscount) * pcVal
						voucherDiscountTotal = (piecePrice * voucherDiscount) * pcVal
						$('voucherAmount').innerHTML = formatPrice(voucherDiscountTotalBr, '&euro;');
						$('vAmount').value = voucherDiscount;
						$('vTotalb').value = voucherDiscountTotalBr;
						$('vTotaln').value = voucherDiscountTotal;
						$('voucherMessage').innerHTML = 'Sie erhalten ' + voucherDiscountPercent + ' Rabatt auf den Kartenst&uuml;ckpreis!'
					} else if(voucherRes[1]=='abs' && pcVal>49) { 		// Absolut ab 30 Karten
						voucherDiscount = parseFloat(voucherRes[2] * -1, 2)
						//alert(voucherRes[2])
						voucherDiscountPercent = '&euro; ' + voucherDiscount + ',00'
						voucherDiscountTotalBr = voucherDiscount
						voucherDiscountTotal = voucherDiscount/1.19
						$('voucherAmount').innerHTML = formatPrice(voucherDiscount, '&euro;');
						$('vAmount').value = voucherDiscount;
						$('vTotalb').value = voucherDiscountTotalBr;
						$('vTotaln').value = voucherDiscountTotal;
						$('voucherMessage').innerHTML = 'Sie erhalten ' + voucherDiscountPercent + ' Rabatt auf den Kartenst&uuml;ckpreis!'
					} else { 											// Absolut unter 30 Karten ERROR
						voucherDiscount = 0
						//alert(voucherRes[2])
						voucherDiscountPercent = voucherDiscount + ',00 Euro'
						voucherDiscountTotalBr = voucherDiscount
						voucherDiscountTotal = voucherDiscount/1.19
						$('voucherAmount').innerHTML = formatPrice(voucherDiscount, '&euro;');
						$('vAmount').value = voucherDiscount;
						$('vTotalb').value = voucherDiscountTotalBr;
						$('vTotaln').value = voucherDiscountTotal;
						$('voucherMessage').innerHTML = 'Sie erhalten ' + voucherDiscountPercent + ' Rabatt auf den Kartenst&uuml;ckpreis!<br />Dieser Gutschein gilt erst ab einer Auflagenh&ouml;he von 50 Karten.'
					}
					
					// !!!! 
						$('endpriceBrutto').innerHTML = formatPrice(((pcVal * piecePriceBr) + (envPrice * pcVal) + sonstigesBrutto + voucherDiscountTotalBr), '&euro;')
						$('endpriceNetto').innerHTML = 'Netto: ' + formatPrice((pcVal * piecePrice) + (envPriceNetto * pcVal) + sonstiges + voucherDiscountTotal, '&euro;')
					
					// EARLY BIRD RABATT
					//$('voucherMessage').innerHTML = '-10% Early Bird Rabatt-Aktion auf den Kartenst&uuml;ckpreis<br />Sie erhalten zus&auml;tzliche<br />' + voucherDiscountPercent + ' Rabatt auf den Kartenst&uuml;ckpreis!'
					
					
					
					
					
				} else {
					$('voucherMessage').innerHTML = 'Kein G&uuml;ltiger Gutschein-Code'
					voucherDiscountTotalBr = 0
					voucherDiscountTotal = 0
					$('voucherAmount').innerHTML = formatPrice(voucherDiscountTotalBr, '&euro;');
					$('vAmount').value = 0;
					$('vTotalb').value = 0;
					$('vTotaln').value = 0;	
				}
			}
		});
	}
	
	if($('voucher').value == ''){
		$('voucherMessage').innerHTML = 'Sie haben einen Gutschein bekommen?<br>Dann geben Sie den Code bitte hier ein:'
		voucherDiscountTotalBr = 0
		voucherDiscountTotal = 0
		$('voucherAmount').innerHTML = formatPrice(voucherDiscountTotalBr, '&euro;');
		$('vAmount').value = 0;
		$('vTotalb').value = 0;
		$('vTotaln').value = 0;		
	}
	
	if($('expressZuschlagBrutto')){
		expressZuschlagBr = parseInt($('expressZuschlagBrutto').value)
		expressZuschlag = parseInt($('expressZuschlagNetto').value)
		$('expressZuschlagBr').value = expressZuschlagBr
		$('expressZuschlag').value = expressZuschlag
	} else {
		expressZuschlagBr = 0
		expressZuschlag = 0
	}
	



	//alert('pcVal:'+pcVal+', piecePriceBr:'+piecePriceBr)
	$('endpriceBrutto').innerHTML = formatPrice(((pcVal * piecePriceBr) + (envPrice * pcVal) + sonstigesBrutto + voucherDiscountTotalBr + expressZuschlagBr), '&euro;')
	$('endpriceNetto').innerHTML = 'Netto: ' + formatPrice((pcVal * piecePrice) + (envPriceNetto * pcVal) + sonstiges + voucherDiscountTotal + expressZuschlag, '&euro;')
	$('endpriceShipping').innerHTML = 'Preis zzgl. Versand<br/>(Deutschland ' + formatPrice(shippingPrice, '&euro;') + ')'
	$('piecePriceBrutto').innerHTML = 'St&uuml;ckpreis Brutto: ' + formatPrice(piecePriceBr, '&euro;')
	$('piecePriceNetto').innerHTML = 'St&uuml;ckpreis Netto: ' + formatPrice(piecePrice, '&euro;')
	
	if($('envEndPrice1') && $('envEndPrice2')){
	$('envEndPrice1').innerHTML = '+ ' + formatPrice($('envPricing1').value * pcVal, '&euro;')
	$('envEndPrice2').innerHTML = '+ ' + formatPrice($('envPricing2').value * pcVal, '&euro;')
	}
	
	$('sumn').value = formatPrice(((pcVal * piecePrice) + (envPriceNetto * pcVal) + sonstiges + voucherDiscountTotal + expressZuschlag))
	$('sumb').value = formatPrice(((pcVal * piecePriceBr) + (envPrice * pcVal) + sonstigesBrutto + voucherDiscountTotalBr + expressZuschlagBr))
	$('shipping').value = shippingPrice
	
	/*
	if(parseInt(shippingPrice)>0){
	   	$('pcAmountShipping').innerHTML = '1'
	} else {
		$('pcAmountShipping').innerHTML = '0'
	}
	
	if(pcVal != undefined && pcVal != 0){
		$('calcTable').style.display = 'block'
	}
	*/
	// Send Price
	url = 'ajax/amountinform.php?pcVal=' + pcVal;
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
            if(transport.responseText == 'ok') {
            } else {
            }
        }
    });
	
	
}



function initScroller(){
	srcollOrgPos = $('scroller').offsetTop;	
	
	
	
	version = parseFloat(navigator.appVersion.split("MSIE")[1]);
	if (version < 8){
		 srcollOrgPosLeft = $('scroller').offsetLeft;
	} else {
		srcollOrgPosLeft = $('scroller').offsetLeft + 660;
	}
	
	$('scroller').style.left = srcollOrgPosLeft + 'px'
}

function scrollCheck() {
	
	st = 0
	if (document.documentElement && document.documentElement.scrollTop){
		st = document.documentElement.scrollTop // mozilla
	} else if (document.body && document.body.scrollTop){ 
		st = document.body.scrollTop // ?
	} 
	if(st>srcollOrgPos){
			$('scroller').style.top = 10 + 'px'
		}
		if(st<srcollOrgPos){
			$('scroller').style.top = srcollOrgPos + 'px'
		}
	$('scroller').style.left = srcollOrgPosLeft + 'px'
}

cntVotesjs = 0;
function vote(id, cntVotes){
	cntVotesjs++
	displayCntVotes = cntVotesjs + cntVotes
    $('daumenhoch' + id).hide();
    url = 'ajax/vote.php?grid=' + id;
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
            if(transport.responseText == 'ok') {
				$('daumenhochvoted' + id).show();
				$('cntvotes').innerHTML = '(' + displayCntVotes + ')'
            } else {
                $('daumenhoch' + id).show();
            }
        }
    });
}

function sendFavorites(ids){
	
	idstr = 'idstr=' + ids;

	smail = $('smail').value;
	rmail = $('rmail').value;
	
	$('loading').show();
	$('mailForm').hide();
	

    url = 'ajax/sendFavorites.php?idstr=' + idstr + '&smail=' + smail + '&rmail=' + rmail;
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
            if(transport.responseText == 'ok') {
				$('loading').hide();
				$('success').show();
            } else {
				$('loading').hide();
				$('error').show();
            }
        }
    });
	
}

