//var wobase= "http://192.168.181.202:44138/cgi-bin/WebObjects/FlatrateBackend.woa/wa/";
var wobase= "http://www.flatrate.de:8082/FlatrateBackend/WebObjects/FlatrateBackend.woa/wa/";
var baseurl ="http://www.flatrate.de/";
var batchindex = 0;
var vergleichcookie ="prepaidvergleich";
var COOKIE_NAME = 'prepaid_cookie';
var options = { path: '/', expires: 1 };
var verhalten= "egal";

function setupMenueVerhalten(){
	$.getJSON(wobase+"getPrepaidVerhalten"+"?callback=?", jCBVerhalten);  
}

function jCBVerhalten(json){
	for (var i = 0; i < json.length; ++i) {
		$('#verhalten').append("<option value='"+json[i]+"'>"+json[i]+"</option>" );
	}
	if (verhalten == "egal"){verhalten = json[0];}
	$('#verhalten').attr('value', verhalten);
	filterWOEngine();
}

function filterVerhalten(filtervalue){
	verhalten =filtervalue;
 	deleteVGLCookie();
	batchindex = 0;
	filterWOEngine();
};

function filterHardware(filtervalue){
 	deleteVGLCookie();
	batchindex = 0;
	filterWOEngine();
};

function filterFlatrate(filtervalue){
 	deleteVGLCookie();
	batchindex = 0;
	filterWOEngine();
};

function filterWOEngine(){
	verhalten = document.prepaid.verhalten.value;
	var mitHandy  = document.prepaid.mitHandy.value;
	var allnet = document.prepaid.incIFlatAllNet.checked;
	var festnetz = document.prepaid.incIFlatFestnetz.checked;
	var festnetzintern = document.prepaid.incIFlatFestnetzInterne.checked;
	var intern = document.prepaid.incIFlatIntern.checked;
	var umts = document.prepaid.inclFlatUMTS.checked;
	
	$.cookie(COOKIE_NAME, verhalten +"|"+ mitHandy +"|"+batchindex, options);
	$.getJSON(wobase+"getDataPrepaid?verhalten="+verhalten+"&mitHandy="+mitHandy+"&allnet="+allnet+"&festnetz="+festnetz+"&festnetzintern="+festnetzintern+"&intern="+intern+"&umts="+umts+"&batchindex="+batchindex+"&callback=?", jCBFlatrateData); 
	/*var hardware  = document.umts.hardware.value;
	var mitFlatrate = document.umts.fr_handy.checked;
	$.cookie(COOKIE_NAME, laufzeit +"|"+ hardware +"|"+mitFlatrate+"|"+batchindex, options);
	$.getJSON(wobase+"getDataUMTS?laufzeit="+laufzeit+"&hardware="+hardware+"&mitFlatrate="+mitFlatrate+"&batchindex="+batchindex+"&callback=?", jCBUMTSData);  
	*/
};

function showbatch(cBatch){
	batchindex = cBatch;
	filterWOEngine();
};



function jCBFlatrateData(json){
	if (json.TOTALCOUNT > 0){
		$('#jsonresult').setTemplateURL('fileadmin/templates/prepaid_vergleich_tmpl.html', null, { filter_data: false });
		$('#jsonresult').processTemplate(json);
		readVGLCookie();
		$('#jsonresult').show("fast");
	}else{
		$('#jsonresult').fadeOut("fast");
		$('#jsonresult').html("Es wurde leider kein Tarif gefunden...");
		$('#jsonresult').fadeIn("normal");
	}
};


function deleteVGLCookie(){
	$('#detailvergleich').html("");
	if(	$.cookie(vergleichcookie)!= null){$.cookie(vergleichcookie, null, options);}	
};

function readVGLCookie(){
	if(	$.cookie(vergleichcookie)!= null){
		var cookie_array= $.cookie(vergleichcookie).split("|");
	    for(var x = 0, y = cookie_array.length; x < y; x++){  $('#vgl_'+cookie_array[x]).attr('checked', true); }
		var numOfTarifs = cookie_array.length -1;
		if (numOfTarifs>0 ){
		   $('#detailvergleich').html (numOfTarifs + " ausgew&auml;hlt.<a href=\"javascript:deleteVglAuswahl();\">Auswahl l&ouml;schen </a>");
		}
		if (numOfTarifs>2 ){
			$('#detailvergleich').html("Sie k&ouml;nnen maximal 3 Tarife vergleichen. <a href=\"javascript:deleteVglAuswahl();\">Auswahl l&ouml;schen </a>");
		}
	}	
};

function setVergleich(cobj){
	$('#detailvergleich').html("");
	
	
	var maxAlert = false;
	if(	$.cookie(vergleichcookie)== null && cobj.checked){
	 	$.cookie(vergleichcookie, cobj.value, options);	
	}
	if(	$.cookie(vergleichcookie) != null){
	   	var cookie_content= $.cookie(vergleichcookie);	
	 	var cookie_array= $.cookie(vergleichcookie).split("|");
		var cookie_new = "";
		if(cookie_array.length > 3 && cobj.checked){
				maxAlert = true;
	            cobj.checked = false;
	        }
	 	if (cobj.checked){cookie_new = cookie_content +"|"+(cobj.value);} 
		if (cobj.checked == false){
			for(var x = 0, y = cookie_array.length; x < y; x++){
                if( cookie_array[x] != cobj.value && cookie_array[x] !=""){cookie_new = cookie_new +"|"+cookie_array[x]; }
            }
		}
		$.cookie(vergleichcookie, cookie_new, options);
	}
	readVGLCookie();
};

function deleteVglAuswahl(){
 	var cookie_array= $.cookie(vergleichcookie).split("|");
	for(var x = 0, y = cookie_array.length; x < y; x++){  $('#vgl_'+cookie_array[x]).attr('checked', false); }
	deleteVGLCookie();
};

function detailvergleich(){
	if(	$.cookie(vergleichcookie)!= null){
		location = baseurl +"prepaid/vergleich/detailvergleich.html";
	}
};

$(document).ready(function () {
if(	$.cookie(COOKIE_NAME)!= null){
	var cookie_array= $.cookie(COOKIE_NAME).split("|");
	verhalten = cookie_array[0];
	var mitHandy = cookie_array[1];
	batchindex = cookie_array[2];
	$('#mitHandy').attr('value', mitHandy);
	
	}
	setupMenueVerhalten();
	
})

