//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 ="handyvergleich";
var COOKIE_NAME = 'handy_cookie';
var options = { path: '/', expires: 1 };

var batchindex = 0;

function setupMenueHandyVerhalten(){
		$.getJSON(wobase+"getHandyVerhalten"+"?callback=?", jCBHandyVerhalten);  
}
function jCBHandyVerhalten(json){
		for (var i = 0; i < json.length; ++i) {
			 $('#handyTelefonVerhalten').append("<option value='"+json[i]+"'>"+json[i]+"</option>" );
		}
		$('#handyTelefonVerhalten').attr('value', verhalten);
		filterWOEngine();
}

function filterVerhalten(filtervalue){
	handyTelefonVerhalten =filtervalue;
	filterWOEngine();
};

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

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

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

function filterWOEngine(){
	var verhalten = document.handy.handyTelefonVerhalten.value;
	var mitHandy  = document.handy.mitHandy.value;
	var mitFlatrate = document.handy.inclFlatrate.checked;
	
	$.cookie(COOKIE_NAME, verhalten +"|"+ mitHandy +"|"+ mitFlatrate +"|"+batchindex, options);
	$.getJSON(wobase+"getDataHandy"+"?verhalten="+verhalten+"&mitHandy="+mitHandy+"&mitFlatrate="+mitFlatrate+"&allnet=egal&batchindex="+batchindex+"&callback=?", jsonCallback);  
}

function jsonCallback(json){
	if (json.TOTALCOUNT > 0){
		$('#jsonresult').setTemplateURL('fileadmin/templates/handy_vergleich_tmpl.html');
		$('#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 +"handy/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];
	var mitFlatrate = cookie_array[2];
	batchindex = cookie_array[3];
		
	if (mitHandy =='true') { $('#mitHandy').attr('value', 'true');} else {$('#mitHandy').attr('value', 'false');}
	if (mitFlatrate =='true') { $('#inclFlatrate').attr('checked', true);} else {$('#inclFlatrate').attr('checked', false);}
	}
	setupMenueHandyVerhalten();
})



