lng = {};

$(document).ready(function() {
	checkAdvice();
	
	$("#propertyCheck input").click( function() {
		checkAdvice();
	} );
});

function noResults() {
	$("#advice").empty();
	
	var items = [];
	items.push( '<tr><td colspan="6" style="text-align: center;">'+lng['noResults']+'</td></tr>' );
	
	$(items.join('')).appendTo('#advice');
	
	style_tables();
}

function checkAdvice(){

	var chk = 0;
	var prpIDs = [];
	var maxPoints = 0;
	
	$.each( $("#propertyCheck input"), function(){
		if($(this).attr("checked")){
			chk++;
			prpIDs[prpIDs.length] = $(this).attr( "value" );
		}
	});
	maxPoints = chk * 5;

	if( prpIDs.length > 0 ){
		$.ajax({
			type: "POST",
			url: "?Page=BlankPage.php&JSON=1",
			data: "properties=" + prpIDs,
			dataType: 'json',
			cache: false,
			success: function(data) {
	   			var items = [];
	   			$( "#advice" ).empty();
				
				if( data.length > 0 ) {
					$.each(data, function(key, val) {
						var stars = Math.floor((val['value'] / maxPoints) * 5);
						if( stars == 0 )
							stars = 1;
						
						var starClass = "";
						switch( stars ) {
							case 1:starClass = "1Star";break;
							case 2:starClass = "2Stars";break;
							case 3:starClass = "3Stars";break;
							case 4:starClass = "4Stars";break;
							case 5:starClass = "5Stars";break;
						}
						
						strItem = '';
						strItem += '<tr onclick="window.location.href=\''+val['link']+'\'" style="cursor: pointer">';
						strItem += '<td><a href="'+val['link']+'"><img src="/Static/Images/Default/Icons/info.png" /></a></td>';
						strItem += '<td>'+val['itemCode']+'</td>';
						strItem += '<td>'+val['title']+'</td>';
						strItem += '<td>'+val['unit']+'</td>';
						strItem += '<td>&euro; '+val['price']+'</td>';
						strItem += '<td><img src="/Static/Images/Default/Icons/AdviceGuide'+starClass+'.png" alt="" /></td>';
						strItem += '</tr>';
						
						items.push( strItem );
					});
	   			
					$(items.join('')).appendTo('#advice');
					
					style_tables();
				}
				else {
					noResults();
				}
   			},
 			statusCode: {
    			404: function() {}
			}
		});//end Ajax
	}
	else {
		noResults();
	}
}
