var basePriceForLetter = 28.00;
var selectedLetterPrice = false;
var selectedObj = false;
var selectedRadio = false;
var price = 0;
var selectedPrice = 0; 

function sprintf () {
  var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
  while (f) {
    if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
    else if (m = /^\x25{2}/.exec(f)) o.push('%');
    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
      if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
        throw("Expecting number but found " + typeof(a));
      switch (m[7]) {
        case 'b': a = a.toString(2); break;
        case 'c': a = String.fromCharCode(a); break;
        case 'd': a = parseInt(a); break;
        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
        case 'o': a = a.toString(8); break;
        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
        case 'u': a = Math.abs(a); break;
        case 'x': a = a.toString(16); break;
        case 'X': a = a.toString(16).toUpperCase(); break;
      }
      a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
      x = m[5] - String(a).length;
      p = m[5] ? str_repeat(c, x) : '';
      o.push(m[4] ? a + p : p + a);
    }
    else throw ("Huh ?!");
    f = f.substring(m[0].length);
  }
  return o.join('');
}


function addPrice(preis){
	preis = parseFloat(preis);
	if (Object.isNumber(preis)){
		price = price + preis;
		return true;
	}else{
		return false;
	}
}

function substractPrice(preis){
	preis = parseFloat(preis);
	if(Object.isNumber(preis)){
		if(price - preis >= 0){
			price = price - preis;
		}
	return true;
	}else{
		return false;
	}
}

function addLetterPrice(preis,obj){
	if($('versandart_mail').checked==1){
		if(selectedRadio!=obj){
			addPrice(preis);
		}
	}else{
		if(selectedRadio==$('versandart_mail')){
			substractPrice(preis);
		}
	}
	selectedRadio = obj;
	updatePrice();
}

function getValueFromRadioButtons(obj){
    if(obj.length == undefined){
        return obj.value;
    }else{
        for(i=0; i<obj.length; i++){
            if(obj[i].checked){
                return(obj[i].value);
            }
        }
	}
	return false;	
}

function updateEverything(){
	beglaubigt = false;
	if(document.auszug){
		product = getValueFromRadioButtons(document.auszug.produkt);
		if(product!=''){
			product = product.split("||");
			productName = product[0];
			productPrice = parseFloat(product[1]);
			productDescription = product[2];
			productLetterPrice = product[3];
			productId = product[4];
			versandart = getValueFromRadioButtons(document.auszug.versandart);
			if(productLetterPrice==''){
				productLetterPrice = parseFloat(basePriceForLetter);
			}else{
				productLetterPrice = parseFloat(productLetterPrice);
			}
			if(document.auszug.beglaubigt){
				if(document.auszug.beglaubigt.checked){
					beglaubigt = true;
				}else{
					beglaubigt = false;
				}
			}
			if(beglaubigt){
				productPriceComplete = productPrice+productLetterPrice+3;
			}else{
				productPriceComplete = productPrice+(versandart=='brief'?3:0);
			}
			document.auszug.selectedProduct.value = productName;
			document.auszug.selectedProductBeglaubigt.value = beglaubigt?'Ja':'Nein';
			document.auszug.selectedProductPrice.value = productPrice;
			document.auszug.selectedProductLetterPrice.value = beglaubigt?productLetterPrice:'';
			document.auszug.selectedProductId.value = productId;
			document.auszug.selectedProductPriceComplete.value = productPriceComplete;
			document.auszug.selectedProductShippingType.value =  beglaubigt?'Brief':versandart=='brief'?'Brief':'E-Mail';		
			document.auszug.selectedProductShippingPrice.value = beglaubigt?'3':versandart=='brief'?'3':'0';		
			html =  outputHTML('Gewähltes Produkt', productName);
			html += outputHTML('Preis', sprintf("%.2f",  parseFloat(productPrice))+" €");
			if(beglaubigt){
				html += outputHTML('Beglaubigungsvermerk', productLetterPrice+".00 €");
			}
			html += outputHTML('Gewählte Versandart', beglaubigt?'per Brief':versandart=='brief'?'per Brief':'per E-Mail');
			if(versandart=='brief'){
				html += outputHTML('Kosten Versand', "3.00 €");
			}
			html += outputHTML('<strong>Gesamtpreis</strong>', sprintf("%.2f",productPriceComplete)+ " €");
			$('produktWahl').innerHTML = html;
			if($('letterPrice'))
				$('letterPrice').innerHTML = "("+sprintf("%.2f",productLetterPrice)+" €)";
			resetVisuals(productName, beglaubigt, versandart);
		}
	}
}

function outputHTML(label, value){
	return "<dt>"+label+"</dt><dd>"+value+"</dd>";
}

function resetVisuals(productName, beglaubigt){
	product = document.auszug.produkt;
	if(product.length==undefined){
	    $(product.parentNode.parentNode).addClassName('active');
	}
	for(i=0; i<product.length;i++){
		if(product[i].value.indexOf(productName)==0){
			$(product[i].parentNode.parentNode).addClassName('active');
		}else{
			$(product[i].parentNode.parentNode).removeClassName('active');
		}
	}
	if(document.auszug.beglaubigt){
		if(beglaubigt){
			document.auszug.beglaubigt.checked = 1;
			$(document.auszug.beglaubigt.parentNode.parentNode).addClassName('active');
			$('versandart_brief').disabled=true;
			$('versandart_mail').disabled=true;
			$('versandart_mail').checked=true;
		}else{
			document.auszug.beglaubigt.checked = 0;
			$(document.auszug.beglaubigt.parentNode.parentNode).removeClassName('active');
			$('versandart_brief').disabled=false;
			$('versandart_mail').disabled=false;
		}	
	}
}

function selectProduct(){
	updateEverything();
}

function popup(url){
  pdfWindow = window.open(url, "Beispielseite", "width=650,height=650,left=100,top=200");
  pdfWindow.focus();
}

function updatePrice(){
	$('produkt_price').innerHTML = price+ " €";
	$('preis_hidden').value=price;
}

