﻿$(document).ready( function() {
	
	checkBoxOnClickEvent();
	var uid;
	
	$(".mySettings").each( function() {
		uid = $(this).attr('uid');
		insertSettingsToInputs(uid);
	});
	
	
	$(".mySettings input[type=text]").change( function() {
		updateUserSetting(this, uid);
		lockReportsCheck();	
	});
	
	$(".mySettings .save").click( function(){
		saveSettings(uid);
		lockReportsCheck();	
	});
	
	$(".mySettings .save").hover( function(){
		$(this).css("cursor","pointer");
	}, function() {
		$(this).css("cursor","auto");
	});
});

function insertSettingsToInputs(uid){
		
	$.post('ajax/getUserSettings.lasso', {
		user_id : uid
	}, function(data) {
		 
		var items = data.split(",");
		var i = 0;
		
		$(".mySettings input[type=text]").each( function() {
			$(this).attr('value',items[i]);
			i++;
		});
		lockReportsCheck();	
	});
}


function saveSettings(uid){
	$(".mySettings input").each( function() {
		updateUserSetting(this, uid);	
	});
}

function validateValue(){
}

function updateUserSetting(that, uid){
	
	var newValue = parseInt($(that).attr('value'));
	var attribute = $(that).attr('name');
	
	
	$.post('ajax/updateUserSettings.lasso', {
		user_id : uid,
		newValue : newValue,
		attribute : attribute
	}, function(data) {
		if(data == 1){
			$(that).parent().parent().find("td.isok").html("<img src='images2/ok.gif' alt='' />");
			$(that).parent().parent().find("input").css('color','#000000');
		}
	});
		
}

function checkBoxOnClickEvent(){
	
	$("input[name=rapport]").click( function(){
		
		var ukat = $(this).attr('value');
		var uid = $(this).attr('uid');
	
		
		$.post('ajax/editReportsForUser.lasso', {
			user_id : uid,
			report_ukat : ukat
		});
		
		if($(".lockThisReport[ukat_id=" + ukat + "]").css("display") == "none")
			$(".lockThisReport[ukat_id=" + ukat + "]").css("display","inline-block");
		else
			$(".lockThisReport[ukat_id=" + ukat + "]").css("display","none");
	});
	
	
	$("input[doSplit]").click( function(){
		
		var uid = $(this).attr('uid');
		if($(this).attr("doSplit") == 1)
			$(this).attr("doSplit","0");
		else
			$(this).attr("doSplit","1");
		
		var doSplit = $(this).attr('doSplit');	
		var attribute = "poster_text5";
		$("td.settingsDoSplit").slideToggle();
		
		$.post('ajax/updateUserSettings.lasso', {
			user_id : uid,
			newValue : doSplit,
			attribute : attribute
		});
	});
	
	$("input[remindeMe]").click( function(){
		
		var uid = $(this).attr('uid');
		if($(this).attr("remindeMe") == 1)
			$(this).attr("remindeMe","0");
		else
			$(this).attr("remindeMe","1");
		
		var remindeMe = $(this).attr('remindeMe');	
		var attribute = "poster_text9";
		
		$.post('ajax/updateUserSettings.lasso', {
			user_id : uid,
			newValue : remindeMe,
			attribute : attribute
		});
		
	});
	
	
	$("input[name=butik/servering]").change( function(){
		
		var uid = $(this).attr('uid');
		var newValue = $(this).attr('value');	
		var attribute = "poster_text6";
		
		if(newValue == parseInt(newValue)){
			
			$.post('ajax/updateUserSettings.lasso', {
				user_id : uid,
				newValue : newValue,
				attribute : attribute
			});
			
			var attribute = "poster_text7";
			var secondValue = 100-newValue;
			
			$("input[name=leverans]").attr("value",secondValue);
			
			$.post('ajax/updateUserSettings.lasso', {
				user_id : uid,
				newValue : secondValue,
				attribute : attribute
			});
		}
		
	});
	
	$("input[name=leverans]").change( function(){
		
		var uid = $(this).attr('uid');
		var newValue = $(this).attr('value');	
		var attribute = "poster_text7";
		
		if(newValue == parseInt(newValue)){
			
			$.post('ajax/updateUserSettings.lasso', {
				user_id : uid,
				newValue : newValue,
				attribute : attribute
			});
			
			var attribute = "poster_text6";
			var secondValue = 100-newValue;
			
			$("input[name=butik/servering]").attr("value",secondValue);
			
			$.post('ajax/updateUserSettings.lasso', {
				user_id : uid,
				newValue : secondValue,
				attribute : attribute
			});
		}
		
	});
	
	
	$("input[name=updateFrekvenze]").change( function(){
		
		var uid = $(this).attr('uid');
		var newValue = $(this).attr('value');	
		var attribute = "poster_text8";
		
		if(newValue == parseInt(newValue)){
			
			newValue == parseInt(newValue);
			if(newValue == 1 || newValue == 0){
			
				$.post('ajax/updateUserSettings.lasso', {
					user_id : uid,
					newValue : newValue,
					attribute : attribute
				}, function(data) {
					$(".reportTbl").each( function(){
						$(this).attr("frekvenze",newValue);
						var currentMonth = $(this).attr('month');
						
						if(currentMonth > 1 && currentMonth <= 3)
							$(this).attr('month',1);
						else if(currentMonth > 4 && currentMonth <= 6)
							$(this).attr('month',2);
						else if(currentMonth > 7 && currentMonth <= 9)
							$(this).attr('month',3);
						else if(currentMonth > 10 && currentMonth <= 12)
							$(this).attr('month',4);
					});
					
					updateReportTbl();
					nextMonthAvalibleCheck();
					prevMonthAvalibleCheck();
				});
				
			}
		}
		
	});
	
	
}


function lockReportsCheck() {
	
	var a = 0;
	var b = 0;
	
	$(".mySettings input").each( function() {
		if($(this).attr("name") == "poster_text1" ){
			a = parseInt($(this).attr("value"));
		}
		
		if($(this).attr("name") == "poster_text4" ){
			b = parseInt($(this).attr("value"));
		}
	});
	
	if(a > 0 && b > 0){
		$(".lockThisReport").each( function() {
			$(this).css("display","none");
		});
	}
	else
	{
		$(".lockThisReport").each( function() {
			$(this).html('<div style="position: relative; font-family: Arial Black; font-size: 13px; text-align: left; margin-left: 10px; width: 98%;"><br /><br />Du har ej matat in antal butiker<br />eller anställda under "inställnignar".<br />Gör detta för att kunna mata in dina värden.<br /></div>');
			$(this).css("display","inline");
		});
	}


}


