/* Version 2 off shop form scripts 
	jquery version

*/
fn_PrettyNames = function(){
	var val = $(this).val();
	
	if(val==undefined){
		val="";
	}
	var inVal = val;
	
	val = val.replace(/^\ +/,""); /* Leerzeichen am anfang entfernen */
	var arVal = val.split(" "); // array anhand von leerzeichen bilden
	val ="";
	for(var i=0;i<arVal.length;i++){
		var first = "";
		var rest = "";
		var word = arVal[i];
		if (word.length>0){
			first = word.substring(0,1).toUpperCase();
			if(word.length>1){
			rest = word.substring(1,word.length).toLowerCase()
			}
			val +=first + rest;
			if(arVal.length > i+1){
				val+= " ";
			}
		}
	}
	if(val!=inVal){
		$(this).val(val);
	}
}

fn_requiredFields = function(){
	var type = $(this).attr("type");
	var name = $(this).attr("name");
	//alert("n: "+ name);
	if(type=="radio"){
		filled = false;
		$("input[name=" + $(this).attr("name") +"]").each(function(){
			if(this.checked){
				filled = true;
			}
		});
		if(!filled){
			$(this).addClass("verify-fail");
			$("label[for="+name+"]").addClass("verify-fail");
		}else{
			$(this).removeClass("verify-fail");
			$("label[for="+name+"]").removeClass("verify-fail");
		}
	}else{
		if($(this).val()==""){
			$(this).addClass("verify-fail");
			$("label[for="+name+"]").addClass("verify-fail");
		}else{
			$(this).removeClass("verify-fail");
			$("label[for="+name+"]").removeClass("verify-fail");
		}
	}
}

fn_email = function(){
	var val = $(this).val();
	if(val==undefined){
		val="";
	}
	var inVal = val;
	
	val = val.replace(/\ +/,""); /* Leerzeichen entfernen */
	
	var name = $(this).attr("name");
	if(!val.match(/^([a-z0-9._-](\+[a-z0-9])*)+@[a-z0-9.-]+\.[a-z]{2,6}$/i)){
		$(this).addClass("verify-fail");
		$("label[for="+name+"]").addClass("verify-fail");
	}else{
		$(this).removeClass("verify-fail");
		$("label[for="+name+"]").removeClass("verify-fail");
	}
	
	if(val!=inVal){
		$(this).val(val);
	}
}

fn_mengefield = function(){
	var menge = $(this).val();
	var name = $(this).attr('name');
	menge = menge .replace(/[^0-9]/, "");
	$(this).val(menge);
	if(menge==''){menge=0}
	var price = $(this).parent().siblings(".prod_single_price").text();
	price = fn_getFloatFromPrice(price);
	price = price * parseInt(menge);
	$(this).parent().siblings(".prod_price").text(price);
	var total = 0;
	
	$(".prod_price").not(".title, .prod_versand, .prod_price_total").each(function(){
		var p = fn_getFloatFromPrice($(this).text());
		total += p;
		
	});
	
	$(this).parent().siblings(".prod_price").html(price +',- € <input type="hidden" value="'+ price +',- €" name="preis_'+name+'" />');
	
	//###value_value_preis_versandkosten### = Versandkosten: ###value_ausland###
  	//###value_value_preis_gesamtsumme### = Rechnungssumme incl. Versandkosten und 19% Mwst.
	if(total>0){
		if($("select[name=Land]").val()=="Deutschland"){
			$(".prod_versand").html('2,- € <input type="hidden" value="2,- €" name="value_preis_versandkosten" />');
			total +=2;
		}else{
			$(".prod_versand").html('5,- € <input type="hidden" value="5,- €" name="value_preis_versandkosten" />');
			total +=5;
		}
	}else{
		$(".prod_versand").text('');
	}
	$(".prod_price_total").html(total +',- €' + '<input type="hidden" value="'+ total +',- €" name="value_preis_gesamtsumme" />');
}

fn_getFloatFromPrice = function(price){
	price = price.replace(/-/,"00");
	price = price.replace(/,/,".");
	price = price.replace(/[^0-9\.]/,"");
	price = parseFloat(price);
	if(isNaN(price) || price==""){
		price = 0.0;
	}
	return price;
}

fn_tooltip_show = function(){
	$("div.tooltipPos").siblings("input,textarea,select").not(this).siblings("div.tooltipPos").children().hide();
	$(this).siblings("div.tooltipPos").children().show();
}

fn_tooltip_hide = function(){
}

fn_submit_validate = function(){
	var msg="";
	
	/* Produkt Überprüfung */
	if(fn_getFloatFromPrice($(".prod_price_total").text())==0){
		msg +="Bitte wenigstens ein Produkt auswählen!\n";
	}
	
	/* Feld eingaben*/
	$(".required").not("input[name=email]").each(fn_requiredFields);
	$("input[name=email]").each(fn_email);
	if($(".verify-fail").length>0){
		msg +="Bitte füllen Sie die rot markierten Felder korrekt aus!\n";
	}
	
	/* AGBs */
	if($("input[name=AGB]:checked").length==0){
		msg +="Bitte akzeptieren Sie die AGBs!\n";
	}
	
	if(msg!=""){
		alert(msg);
		return false;
	}
	/* confirm for DoppelCD zur DVD: Fairplay für Füße (FP0502) */
	
	var returnval = true;
	var fp0502 = parseInt($("#FP0502").val());
	var fp0501 = parseInt($("#FP0501").val());
	//alert("fp0502: "+fp0502 +"\nfp0501: " + fp0501);
	if(fp0502>0 && (isNaN(fp0501)||fp0501==0) ){
		returnval = confirm('Die 2 CDs: Songs zu Fairplay für Füße (FP0502)\nmachen eigentlich nur Sinn zusammen mit der DVD: Fairplay für Füße (FP0501).\nSoll wirklich nur die Musik ohne DVD bestellt werden?\nBestellung ändern über ABBRECHEN, Bestellung so bestätigen mit OKAY');
		if(returnval==false){
			$("#FP0501").focus();
		}
	}
	
	return returnval;
}

/* Binding Functions */

$(document).ready(function(){
	/* Tool Tips */
	$("div.tooltip").wrap('<div class="tooltipPos"><div class="toolbox"></div></div>');
	$("input,textarea,select").hover(fn_tooltip_show,fn_tooltip_hide).focus(fn_tooltip_show);
	
	/* Preis Kalkulation */
	$("form#step1 .prod_menge input[type=text]")
		.each(fn_mengefield)
		.bind("keyup",fn_mengefield);
	$("select[name=Land]").change(function(){
		var tmp = $("#produkte input[type=text]").get(0);
		$(tmp).each(fn_mengefield);
	});
	
	/* input Validation */
	$("form#step1 input.required").not("input[name=email]")
		.blur(fn_requiredFields)
		.change(fn_requiredFields)
		.focus(fn_requiredFields)
		.bind("keyup",fn_requiredFields);
	$("form#step1 input[name=email]")
		.blur(fn_email)
		.change(fn_email)
		.focus(fn_email)
		.bind("keyup",fn_email);
	
	/* Form Validirung */
	$("#step1").submit(fn_submit_validate);
	
	/* Pretty Names */
	$("form#step1 input[name=Vorname],form#step1 input[name=Name],form#step1 input[name=Stadt]").bind("keyup",fn_PrettyNames);
	if($("#ADDITIONAL_MULTIPAGE").length ==0){
		$("#Newsletter").each(function(){ this.checked = true;});
	}
});
