$(document).ready(function() {
	selectedProduct = 0;
	
	if($(document).getUrlParam("p") && $(document).getUrlParam("p") != 99){
		var pid = $(document).getUrlParam("p");
		if(!isNaN(pid) && pid > 0 && pid <= numberOfProducts){
			$("#wvFormSelect").val(pid).attr("selected", true);
			selectedProduct = products[pid];
		}
	}
	else if($(document).getUrlParam("bf")){
		/*$("input[name='wvFormContract']")[1].checked = "checked";*/
		bf = $(document).getUrlParam("bf").replace(",", ".").replace("%2C", ".");
        pm = $(document).getUrlParam("pm");
        pl = $(document).getUrlParam("pl");
		if(isNaN(bf) || bf > 100 || bf < 0){
			$("#formErrorMessage").html(errorText["wrongPriceBF"]);
			$("#formError").show();
			return 0;
		}
		if(isNaN(pm) || pm > 99 || pm < 0){
			$("#formErrorMessage").html(errorText["wrongPricePM"]);
			$("#formError").show();
			return 0;
		}
		if(isNaN(pl) || pl > 99 || pl < 0){
			$("#formErrorMessage").html(errorText["wrongPricePL"]);
			$("#formError").show();
			return 0;
		}
		else{
			var product = {
				provider: "",
				rate: "",
				basicFee: bf,
				priceMobile: pm,
                priceLandline: pl
				/*contract: $(document).getUrlParam("co")*/
			}
			$("#wvFormSelect").val(99).attr("selected", true);
			$("#wvFormBasicfee").val(bf.replace(".", ","));
            $("#wvFormPriceMobile").val($(document).getUrlParam("pm"));
            $("#wvFormPriceLandline").val($(document).getUrlParam("pl"));
			/*$("input[name='wvFormContract']")[$(document).getUrlParam("co")-1].checked = "checked";*/
				
			$("#wvFormAdvContent").show();
			//showWvResult(product);
			selectedProduct = product;
		}
	}
    else{ /* Standard Auswahl */
			var product = {
				provider: "",
				rate: "",
				basicFee: 25.00,
				priceMobile: 29,
                priceLandline: 29
				/*contract: $(document).getUrlParam("co")*/
			}
			$("#wvFormSelect").val(99).attr("selected", true);
			$("#wvFormBasicfee").val("25,00");
            $("#wvFormPriceMobile").val("29");
            $("#wvFormPriceLandline").val("29");
			/*$("input[name='wvFormContract']")[$(document).getUrlParam("co")-1].checked = "checked";*/
				
			$("#wvFormAdvContent").show();
    }
	
	if(selectedProduct != 0){
		tb_show(); //Abdunkeln			
		$('#TB_load').css("left", "40%");
		$('#TB_load').html('<div id="loadingLayer"><div class="left"><img src="/static/img/lay_loading_animation.gif" /></div><div class="left">Ihr Tarifvergleich wird berechnet. <br />Wir bitten um einen Moment Geduld</div></div>');
		setTimeout(function(){
			tb_remove();
			showWvResult(selectedProduct);
		}, 1000);			
	}
	
    //Initialize Wettbewerbsvergleich
	$("#wvFormSelect").change( function() {
        $('#wvFormBasicfee').css('position', 'static');
        $('#wvFormPriceMobile').css('position', 'static');
        $('#wvFormPriceLandline').css('position', 'static');
		if($("#wvFormSelect option:selected").val() == 99)
			$("#wvFormAdvContent").slideDown("slow", function(){
                $('#wvFormBasicfee').css('position', 'relative');
                $('#wvFormPriceMobile').css('position', 'relative');
                $('#wvFormPriceLandline').css('position', 'relative');
            });
		else
			$("#wvFormAdvContent").slideUp("slow", function(){
                $('#wvFormBasicfee').css('position', 'relative');
                $('#wvFormPriceMobile').css('position', 'relative');
                $('#wvFormPriceLandline').css('position', 'relative');
            });
	});
	
});

function wvSendOnPage(){
    $("#formError").hide();
	tb_show(); //Abdunkeln		
	$('#TB_load').css("left", "40%");	
	$('#TB_load').html('<div id="loadingLayer"><div class="left"><img src="/static/img/lay_loading_animation.gif" /></div><div class="left">Ihr Tarifvergleich wird berechnet. <br />Wir bitten um einen Moment Geduld</div></div>');
	setTimeout(function(){
		tb_remove();
		
		if($("#wvFormSelect option:selected").val() == 99){
        
            bf = $("#wvFormBasicfee").val().replace(",", ".");
            pm = $("#wvFormPriceMobile").val();
            pl = $("#wvFormPriceLandline").val();
            if(isNaN(bf) || bf > 100 || bf < 0){
                $("#formErrorMessage").html(errorText["wrongPriceBF"]);
                $("#formError").show();
                return 0;
            }
            else if(isNaN(pm) || pm > 99 || pm < 0){
                $("#formErrorMessage").html(errorText["wrongPricePM"]);
                $("#formError").show();
                return 0;
            }
            else if(isNaN(pl) || pl > 99 || pl < 0){
                $("#formErrorMessage").html(errorText["wrongPricePL"]);
                $("#formError").show();
                return 0;
            }
            else{
                var product = {
                    provider: "",
                    rate: "",
                    basicFee: bf,
                    priceMobile: pm,
                    priceLandline: pl
                    /*contract: $(document).getUrlParam("co")*/
                }
                showWvResult(product);
            }

		}
		else{
            $("#wvFormAdvContent").hide();
			var pid = $("#wvFormSelect option:selected").val();
			if(!isNaN(pid) && pid > 0 && pid <= numberOfProducts)
				showWvResult(products[pid]);
		}
		if($("#wvFormSelect option:selected").val() != 0){
			$("#wvSuggestBox").slideDown();
		}
		else{
			$("#formErrorMessage").html(errorText["noSelect"]);
			$("#formError").show();
		}
	}, 1000);

}


function showWvResult(product){	
	showWvPriceBox(product);
}

function showWvPriceBox(product){
    $("#wvResultFailed").hide();
	$("#wvBasicFeeBox").hide();
	$("#wvPriceMobileBox").hide();
    $("#wvPriceLandlineBox").hide();
    $("#wvResult").hide();
    
    result = 0;
    
    if(product.basicFee > 0){
        $("#wvBasicFeeBox").slideDown('slow');
        $("#wvResult").show();
        basicFee = product.basicFee + "";
        basicFee = basicFee.replace(".", ",");
        $("#wvBasicFeeBox").find(".diff").html(basicFee);
        result = 1;
    }
    if(product.priceMobile > 15){
        $("#wvPriceMobileBox").slideDown('slow');
        $("#wvResult").show();
        $("#wvPriceMobileBox").find(".diff").html(product.priceMobile - 15);
        $("#wvPriceMobileBox").find(".userPrice").html(product.priceMobile);
        result = 1;
    }
    if(product.priceLandline > 15){
        $("#wvPriceLandlineBox").slideDown('slow');
        $("#wvResult").show();
        $("#wvPriceLandlineBox").find(".diff").html(product.priceLandline - 15);
        $("#wvPriceLandlineBox").find(".userPrice").html(product.priceLandline);
        result = 1;
    }
    
    if(result == 0){
        $("#wvResultFailed").slideDown('slow');
    }
	
}


var products = [];
var numberOfProducts = 9;

products[1] = {
	provider: "1&amp;1",
	rate: "Handy-FLAT",
    basicFee: 9.99,
	priceMobile: 19,
    priceLandline: 0

};
products[2] = {
	provider: "Arcor",
	rate: "Mobil",
    basicFee: 5.00,
	priceMobile: 19,
    priceLandline: 0
};
products[3] = {
	provider: "BASE",
	rate: "Prepaid",
    basicFee: 0.00,
	priceMobile: 29,
    priceLandline: 29
};
products[4] = {
	provider: "BASE",
	rate: "Web Edition",
    basicFee: 10.00,
	priceMobile: 29,
    priceLandline: 0
};
products[5] = {
    provider: "T-Mobile",
	rate: "Max Flat S",
    basicFee: 14.95,
	priceMobile: 29,
    priceLandline: 29
};
products[6] = {
	provider: "T-Mobile",
	rate: "Xtra Card",
    basicFee: 0,
	priceMobile: 19,
    priceLandline: 19
};
products[7] = {
	provider: "versatel",
	rate: "Handy-Flat",
    basicFee: 7.50,
	priceMobile: 19,
    priceLandline: 0
};
products[8] = {
	provider: "Vodafone",
	rate: "CallYa 5/15",
    basicFee: 0,
	priceMobile: 15,
    priceLandline: 15
};
products[9] = {
	provider: "Vodafone",
	rate: "SuperFlat Mobile",
    basicFee: 14.95,
	priceMobile: 29,
    priceLandline: 29
};

var errorText = [];
errorText["noSelect"] = "Bitte wählen Sie Ihren Tarif aus!";
errorText["wrongPriceBF"] = "Bitte geben Sie eine gültige Grundgebühr ein (z.B. 9,95 oder 9).";
errorText["wrongPricePL"] = "Bitte geben Sie eine gültige Minutenpreis (Festnetz) ein, bestehend aus maximal zwei Ziffern.";
errorText["wrongPricePM"] = "Bitte geben Sie eine gültige Minutenpreis (Mobile) ein, bestehend aus maximal zwei Ziffern";