function dayChange(day, daycount, client) {
	

			var projectdiv = '#project-holder-'+day+daycount;
			
			var hoursdiv = '#'+daycount+'-'+day+'-hour';
			
			var minutesdiv = '#'+daycount+'-'+day+'-minute';
			
			var descdiv = '#'+daycount+'-'+day+'-desc';

			//var finishbutton = '#finish-'+day;
			
			//$(finishbutton).attr('class', 'current-finish-button')	
		
			$.post(	'scripts/projectselection.php',
			
				{client:client, day:day, daycount:daycount},
					function(response){
					
						$(projectdiv).html(response);
						
						$(hoursdiv).val('0');
						
						$(minutesdiv).val('0');
						
						$(descdiv).val('opis');
						
						calculateDay();
						
					},
					
					"html"
			
				);		

	
	}

function addMoreSelection(day, nextval) {
	
				
				nextval = nextval*1 + 1;
				
				//alert(nextval)
	
				var holder = '"#'+day+'-list-holder"';
				
				var maxholder = '"#'+day+'max"';
				
				$.post(	'scripts/addMore.php',
			
				{day:day, next:nextval},
					function(response){
					
						$(holder+" ul").append(response)
						
						$('#'+day+'max').val(nextval)
						
						$("."+day+":input").change(function(){
							

							
							dayChange(day, nextval, $(this).val())
							
							});
						
						$("."+day+"-time:input").change(function(){
							
							calculateDay()
	});
				
					},
					
					"html"
			
				);	
				
				
				
				alert($(maxholder))
}

function loadBirthdays() {
	
				
				
				
				$.post(	'scripts/birthday.php',
					function(response){
					
						$("#birthdayload").html(response)
						

						
					
				
					},
					
					"html"
			
				);	
				
				

}

function saveDay(day, date, user, nextval) {
	
	var clientarr = new Array();
							
							var daydiv = '#'+day;
	
							var canPost = 'true';
							
							var projarr = new Array();
							
							var timearr = new Array();
							
							var descarr = new Array();
							
							var setday = '.'+day+':input';
							
							$(setday).each(function() {
								
									
									
									var atrbs = $(this).attr('id').split('-');
									
									
									
									
									if($(this).val() != 'default'){
										
									
										
									var project = '#'+atrbs[0]+'-'+atrbs[1]+'-project';
									
									
									
									
									var desc = '#'+atrbs[0]+'-'+atrbs[1]+'-desc';
									
									var hours = '#'+atrbs[0]+'-'+atrbs[1]+'-hour';
									
									
									var minutes = '#'+atrbs[0]+'-'+atrbs[1]+'-minute';
									
									var status = '#'+atrbs[0]+'-'+atrbs[1]+'-status';
									hours = $(hours).val()*1;
									
									minutes = $(minutes).val()*1;
									
									if($(project).val() === 'default') {
										canPost = 'false';
										alert('Izaberi projekat');
										return false;}
										
									else {	
									projarr.push($(project).val());
									
									}
									
									descarr.push($(desc).val());
									
									if((hours+minutes) === 0) {
										canPost = 'false';
										alert('trajanje zadatka ne sme biti nula');
										
										return false;
									}
									
									else {
									timearr.push(hours+minutes);
								
									}
								
									clientarr.push($(this).val());
									}
									
							
								
								})
								
							var dayarray = clientarr+'|'+projarr+'|'+descarr+'|'+timearr;
							
							
							if(canPost === 'false') { return false;}
							
							else if(canPost === 'true'){
								
								//$("#header-loading").show("fast");
								

								$.post(	'scripts/updateDay.php',
			
				{client:$(this).val(), user:user, dayarray:dayarray, date:date},
					function(response){
					
							if(response === 'success') { 
							
									//$("#header-loading").hide("fast");
									
										window.location = window.location.href;
										//addMoreSelection(day, nextval)
									
									
									
									
							
							 }
							
							
				
					},
					
					"html"
			
				);	
								
								}
								
							
				

							
							
	
	
	}
	
function savemood(day, mood, user, date) {
	
				

								
								//$("#header-loading").show("fast");
								

								$.post(	'scripts/updateMood.php',
			
				{day:day, user:user, date:date, mood:mood},
					function(response){
					
					divs = response.split('-');
					day1 = '#' + divs[0] + '-' + day;day2 = '#' + divs[1] + '-' + day;day3 = '#' + divs[2] + '-' + day;
					$(day1).attr('class', 'move-day-hidden');$(day2).attr('class', 'move-day-hidden');$(day3).attr('class', 'move-day-hidden');
					alert(response)
							
},
					
					"html"
			
				);	
}

function calculateDay() {
	
	
			var hours = 0;
			
			$('.friday-time:input').each(function(){
				
					if($(this).val() != 'default'){
						
						hours += $(this).val()*1;
					
					}
				
				})
				
				if((hours%1) === 0){
					
					$("#hourfriday").html(hours);
					
					$("#minutefriday").html("00")
					
					}
					
					else{
						
						hours = hours - 0.5;
						
						$("#hourfriday").html(hours);
					
						$("#minutefriday").html("30")
					
						
						
						}	

				var hours = 0;
			
			$('.monday-time:input').each(function(){
				
					if($(this).val() != 'default'){
						
						hours += $(this).val()*1;
					
					}
				
				})
				
				if((hours%1) === 0){
					
					$("#hourmonday").html(hours);
					
					$("#minutemonday").html("00")
					
					}
					
					else{
						
						hours = hours - 0.5;
						
						$("#hourmonday").html(hours);
					
						$("#minutemonday").html("30")
					
						
						
						}	


				var hours = 0;
			
			$('.tuesday-time:input').each(function(){
				
					if($(this).val() != 'default'){
						
						hours += $(this).val()*1;
					
					}
				
				})
				
				if((hours%1) === 0){
					
					$("#hourtuesday").html(hours);
					
					$("#minutetuesday").html("00")
					
					}
					
					else{
						
						hours = hours - 0.5;
						
						$("#hourtuesday").html(hours);
					
						$("#minutetuesday").html("30")
					
						
						
						}	


				var hours = 0;
			
			$('.wednesday-time:input').each(function(){
				
					if($(this).val() != 'default'){
						
						hours += $(this).val()*1;
					
					}
				
				})
				
				if((hours%1) === 0){
					
					$("#hourwednesday").html(hours);
					
					$("#minutewednesday").html("00")
					
					}
					
					else{
						
						hours = hours - 0.5;
						
						$("#hourwednesday").html(hours);
					
						$("#minutewednesday").html("30")
					
						
						
						}	


				var hours = 0;
			
			$('.thursday-time:input').each(function(){
				
					if($(this).val() != 'default'){
						
						hours += $(this).val()*1;
					
					}
				
				})
				
				if((hours%1) === 0){
					
					$("#hourthursday").html(hours);
					
					$("#minutethursday").html("00")
					
					}
					
					else{
						
						hours = hours - 0.5;
						
						$("#hourthursday").html(hours);
					
						$("#minutethursday").html("30")
					
						
						
						}	


				var hours = 0;
			
			$('.saturday-time:input').each(function(){
				
					if($(this).val() != 'default'){
						
						hours += $(this).val()*1;
					
					}
				
				})
				
				if((hours%1) === 0){
					
					$("#hoursaturday").html(hours);
					
					$("#minutesaturday").html("00")
					
					}
					
					else{
						
						hours = hours - 0.5;
						
						$("#hoursaturday").html(hours);
					
						$("#minutesaturday").html("30")
					
						
						
						}	
	
	
	}
	


$(document).ready(function(){
	

	loadBirthdays();
	calculateDay();
	
	
	
		//$(".mood-day").click(function(){
							
							//var day = $(this).attr('id').split('-');
							
							//var mood = day[0];
							
							//day = day[1];
							
							//date = '#'+ day + 'dateval'
							
							//date = $(date).val()
							
							//savemood(day, mood, user, date)

		
		//});
		
		$('#event').submit(function(event){

		event.preventDefault();

		

		var canSubmit = true;

		var output = '';



		     

        if($('#event-text').val().length <= 1){

           alert('Ukucaj događaj')

            canSubmit = false;

        }

        
       

        if(canSubmit === true){

			$.post(

				'scripts/add-event.php',

				{eventval:$('#event-text').val()},

				function(response){

					if(response === "success"){

						$('#event-text').val('');

						alert("Uspešno dodato"); 
						
					}

					else{

						alert(response)

					}

				},

				"html"

			);

        }

		

	});
	
	
		$('#wisdom').submit(function(event){

		event.preventDefault();

		

		var canSubmit = true;

		var output = '';



		     

        if($('#wisdom-text').val().length <= 1){

           alert('Ukucaj mudrost')

            canSubmit = false;

        }

        
       

        if(canSubmit === true){

			$.post(

				'scripts/add-wisdom.php',

				{wisdom:$('#wisdom-text').val()},

				function(response){

					if(response === "success"){

						$('#wisdom-text').val('');

						alert("Uspešno dodato"); 
						
					}

					else{

						alert(response)

					}

				},

				"html"

			);

        }

		

	});
		
		
	
		
		$("#finish-monday").click(function(){
							
							saveDay('monday', $("#mondaydateval").val(), user);

		
		});
		
		$("#finish-tuesday").click(function(){
							
							saveDay('tuesday', $("#tuesdaydateval").val(), user);

		
		});
		
		$("#finish-wednesday").click(function(){
							
							saveDay('wednesday', $("#wednesdaydateval").val(), user);

		
		});
		
		$("#finish-thursday").click(function(){
							
							saveDay('thursday', $("#thursdaydateval").val(), user);

		
		});
		
		$("#finish-friday").click(function(){
							
							saveDay('friday', $("#fridaydateval").val(), user);

		
		});
		
		$("#finish-saterday").click(function(){
							
							saveDay('saturday', $("#saturdaydateval").val(), user);

		
		});
		
		
	
	$('.monday:input').change(function(){
		
			var atrbs = $(this).attr('id').split('-');
			
			
			
			var day = atrbs[1];
			
			var daycount = atrbs[0];
			
			var client = $(this).val();
			
			
			dayChange(day, daycount, client);
				
		});
		
		$('.tuesday:input').change(function(){
		
			var atrbs = $(this).attr('id').split('-');
			
			
			
			var day = atrbs[1];
			
			var daycount = atrbs[0];
			
			var client = $(this).val();
		
			dayChange(day, daycount, client);		
		
		});
		
		$('.wednesday:input').change(function(){
		
			var atrbs = $(this).attr('id').split('-');
			
			
			
			var day = atrbs[1];
			
			var daycount = atrbs[0];
			
			var client = $(this).val();
		
			dayChange(day, daycount, client);
		
		});
		
		$('.thursday:input').change(function(){
		
			var atrbs = $(this).attr('id').split('-');
			
			
			
			var day = atrbs[1];
			
			var daycount = atrbs[0];
			
			var client = $(this).val();
		
			dayChange(day, daycount, client);	
		
		});
		
		$('.friday:input').change(function(){
			
			
		
			var atrbs = $(this).attr('id').split('-');
			
			
			
			var day = atrbs[1];
			
			var daycount = atrbs[0];
			
			var client = $(this).val();
		
			dayChange(day, daycount, client);
		
		});
		
		$('.saturday:input').change(function(){
		
			var atrbs = $(this).attr('id').split('-');
			
			
			
			var day = atrbs[1];
			
			var daycount = atrbs[0];
			
			var client = $(this).val();
		
			dayChange(day, daycount, client);	
		
		});
		
		$('.monday-time:input').change(function(){
			
			
			
			var hours = 0;
			
			$('.monday-time:input').each(function(){
				
					if($(this).val() != 'default'){
						
						hours += $(this).val()*1;
					
					}
				
				})
				
				if((hours%1) === 0){
					
					$("#hourmonday").html(hours);
					
					$("#minutemonday").html("00")
					
					}
					
					else{
						
						hours = hours - 0.5;
						
						$("#hourmonday").html(hours);
					
						$("#minutemonday").html("30")
					
						
						
						}
						
				saveDay('monday', $("#mondaydateval").val(), user, $("#mondaymax").val());

			
		});
		
		$('.tuesday-time:input').change(function(){
			
			
			var hours = 0;
			
			$('.tuesday-time:input').each(function(){
				
					if($(this).val() != 'default'){
						
						hours += $(this).val()*1;
					
					}
				
				})
				
				if((hours%1) === 0){
					
					$("#hourtuesday").html(hours);
					
					$("#minutetuesday").html("00")
					
					}
					
					else{
						
						hours = hours - 0.5;
						
						$("#hourtuesday").html(hours);
					
						$("#minutetuesday").html("30")
					
						
						
						}
						
					saveDay('tuesday', $("#tuesdaydateval").val(), user, $("#tuesdaymax").val());
			
		});
		
		$('.wednesday-time:input').change(function(){
			
			
			var hours = 0;
			
			$('.wednesday-time:input').each(function(){
				
					if($(this).val() != 'default'){
						
						hours += $(this).val()*1;
					
					}
				
				})
				
				if((hours%1) === 0){
					
					$("#hourwednesday").html(hours);
					
					$("#minutewednesday").html("00")
					
					}
					
					else{
						
						hours = hours - 0.5;
						
						$("#hourwednesday").html(hours);
					
						$("#minutewednesday").html("30")
					
						
						
						}
						
						saveDay('wednesday', $("#wednesdaydateval").val(), user, $("#wednesdaymax").val());
			
		});
		
		$('.thursday-time:input').change(function(){
			
			
			var hours = 0;
			
			$('.thursday-time:input').each(function(){
				
					if($(this).val() != 'default'){
						
						hours += $(this).val()*1;
					
					}
				
				})
				
				if((hours%1) === 0){
					
					$("#hourthursday").html(hours);
					
					$("#minutethursday").html("00")
					
					}
					
					else{
						
						hours = hours - 0.5;
						
						$("#hourthursday").html(hours);
					
						$("#minutethursday").html("30")
					
						
						
						}
						
						saveDay('thursday', $("#thursdaydateval").val(), user, $("#thursdaymax").val());
			
		});
		
		$('.friday-time:input').change(function(){
			
			
			var hours = 0;
			
			$('.friday-time:input').each(function(){
				
					if($(this).val() != 'default'){
						
						hours += $(this).val()*1;
					
					}
				
				})
				
				if((hours%1) === 0){
					
					$("#hourfriday").html(hours);
					
					$("#minutefriday").html("00")
					
					}
					
					else{
						
						hours = hours - 0.5;
						
						$("#hourfriday").html(hours);
					
						$("#minutefriday").html("30")
					
						
						
						}
						
						saveDay('friday', $("#fridaydateval").val(), user, $("#fridaymax").val());
			
		});
		
		$('.saturday-time:input').change(function(){
			

			
			
			var hours = 0;
			
			$('.saturday-time:input').each(function(){
				
					if($(this).val() != 'default'){
						
						hours += $(this).val()*1;
					
					}
				
				})
				
				if((hours%1) === 0){
					
					$("#hoursaturday").html(hours);
					
					$("#minutesaturday").html("00")
					
					}
					
					else{
						
						hours = hours - 0.5;
						
						$("#hoursaturday").html(hours);
					
						$("#minutesaturday").html("30")
					
						
						
						}
						
						saveDay('saturday', $("#saturdaydateval").val(), user, $("#saturdaymax").val());
			
		});
		

	$('.description').focus(function(){

		if($(this).val() === 'opis'){

			$(this).val('');


		}

	});	
	
	$('.description').blur(function(){

		if($(this).val() === '' || $(this).val() === ' '){

			$(this).val('opis');
			


		}

	});

	
	
	$('#period-prev').click(function(){
		
		$.post(	'scripts/setweek.php',
			{direction:'down', user:user},
				function(response){
					
					window.location=response;
				
				},
				"html"
		);
	});
	
		$('#period-next').click(function(){
			
	
		$.post(	'scripts/setweek.php',
			{direction:'up', user:user},
				function(response){
					
					window.location=response;
				
				},
				"html"
		);
	});
	
	/* ===================PROJECT AD================== */
	
		/* Delete link */

	$('.delete').click(function(e) {

		e.preventDefault();

		thisHref = $(this).attr('href');

		

		if($(this).next('div.question').length <= 0)

			$(this).after('<div class="question">Sigurni? Biće obrisani svi podaci vezani za element (projekti / zadaci).<br/><span class="yes">Da</span><span class="cancel">Ne</span></div>');

		

		$('.question').animate({opacity: 1}, 300);

		

		$('.yes').live('click', function(){

			window.location = thisHref;

		});

		

		$('.cancel').live('click', function(){

			$(this).parents('div.question').fadeOut(300, function() {

				$(this).remove();

			});

		});

	});
	
	/*******************************************************************************************/
	
	
	/* Edit client */
	
	
$('#edit-client').submit(function(event){

		event.preventDefault();
		
		var canSubmit = true;

		var output = '';

		if($("div.error").length){

            $("div.error").remove();

        }            
        

        if($("div.success").length){

            $("div.success").remove();

        }
		
		if($('#clientname').val().length <= 1 ){

            output += "Upiši ime klijenta.<br/>";

            canSubmit = false;

        }
    
	
        if(canSubmit === true){		
			

			$.post(

				'scripts/edit-client.php',

				{id:$("#id").val(), name:$('#clientname').val()},

				function(response){

					if(response === "success"){

						$("#edit-client").prepend('<div class="success">Izmene uspešno snimljene</div>');


					}

					else{

						$("#edit-client").prepend('<div class="error">' + response + '</div>');

					}

				},

				"html"

			);

        }

		else{

			$("#edit-client").prepend('<div class="error">' + output + '</div>');

		}
		

	});
	
	/*******************************************************************************************/
	
	/* Edit project */
	
	
$('#edit-project').submit(function(event){
	


		event.preventDefault();
		
		var canSubmit = true;

		var output = '';
		
		var offer = '';
		
		offer = $('#offerno').val();

		if($("div.error").length){

            $("div.error").remove();

        }            
        

        if($("div.success").length){

            $("div.success").remove();

        }
		
		if($('#projectname').val().length <= 1 ){

            output += "Upiši ime projekta.<br/>";

            canSubmit = false;

        }
		
		if($('#projectnum').val().length <= 1 ){

            output += "Upiši broj projekta.<br/>";

            canSubmit = false;

        }
		
		if(offer == 'Broj ponude')
		{offer = ''}
    
	
        if(canSubmit === true){		
			

			$.post(

				'scripts/edit-project.php',

				{id:$("#id").val(), name:$('#projectname').val(), client:$("#clientset").val(), projectnum:$("#projectnum").val(), offerno:offer},

				function(response){

					if(response === "success"){

						$("#edit-project").prepend('<div class="success">Izmene uspešno snimljene</div>');


					}

					else{

						$("#edit-project").prepend('<div class="error">' + response + '</div>');

					}

				},

				"html"

			);

        }

		else{

			$("#edit-project").prepend('<div class="error">' + output + '</div>');

		}
		

	});
	
	/*******************************************************************************************/

		
	/* Add client */
	
	
$('#add-client').submit(function(event){

		event.preventDefault();
		
		var canSubmit = true;

		var output = '';

		if($("div.error").length){

            $("div.error").remove();

        }            
        

        if($("div.success").length){

            $("div.success").remove();

        }
		
		if($('#clientname').val().length <= 1 ){

            output += "Upiši ime klijenta.<br/>";

            canSubmit = false;

        }
		
		
    
	
        if(canSubmit === true){		
			

			$.post(

				'scripts/add-client.php',

				{name:$('#clientname').val()},

				function(response){

					if(response === "success"){

						$("#add-client").prepend('<div class="success">Klijent uspešno unesen</div>');
						
						$("#clientname").val('');


					}

					else{

						$("#add-client").prepend('<div class="error">' + response + '</div>');

					}

				},

				"html"

			);

        }

		else{

			$("#add-client").prepend('<div class="error">' + output + '</div>');

		}
		
	});
	
	/*******************************************************************************************/

	/* Add project */


	$('#projectname').focus(function(){

		if($(this).val() === 'Ime projekta'){

			$(this).val('');


		}

	});

	

	$('#projectname').blur(function(){

		if($(this).val() === '' || $(this).val() === ' '){

			$(this).val('Ime projekta');


		}

	});
	
	
	$('#offerno').focus(function(){

		if($(this).val() === 'Broj ponude'){

			$(this).val('');


		}

	});

	

	$('#offerno').blur(function(){

		if($(this).val() === '' || $(this).val() === ' '){

			$(this).val('Broj ponude');


		}

	});
	
$('#add-project').submit(function(event){

		event.preventDefault();
		
		var canSubmit = true;

		var output = '';
		
		var offer = '';
		
		offer = $('#offerno').val();

		if($("div.error").length){

            $("div.error").remove();

        }            
        

        if($("div.success").length){

            $("div.success").remove();

        }
		
		if($('#projectname').val().length <= 1  || $('#projectname').val() === 'Ime projekta' ){

            output += "Upiši ime projekta.<br/>";

            canSubmit = false;

        }
		
		if(offer == 'Broj ponude')
		{offer = ''}
		
		if($('#clientset').val() === 'default' ){

            output += "Izaberi klijenta.<br/>";

            canSubmit = false;

        }
		
		if($("#num-holder").html() == ''){
			
			
			output += "Broj projekta nije učitan, probaj da izabereš klijenta ponovo.<br/>";

            canSubmit = false;
			
			
			}
		

    
	
        if(canSubmit === true){
			
					
			
			var projectnum = parseFloat($("#num-holder").html());
			

			$.post(

				'scripts/add-project.php',

				{name:$('#projectname').val(), client:$('#clientset').val(), projectnum:projectnum, offerno:offer},

				function(response){

					if(response === "success"){

						$("#add-project").prepend('<div class="success">Projekat uspešno unesen</div>');
						
						$("#projectname").val('');
						
						$("#projectnum").val('');
						
						$("#offerno").val('');
						
						$("#clientset").val('default');


					}

					else{

						$("#add-project").prepend('<div class="error">' + response + '</div>');

					}

				},

				"html"

			);

        }

		else{

			$("#add-project").prepend('<div class="error">' + output + '</div>');

		}
		
	});
	
	$("#clientset").change(function(){ 
	
	
	
	$.post(	'scripts/updateProjectNum.php',
			
				{client:$(this).val()},
					function(response){
					
						$("#num-holder").html(response);
						
						var projectnum = parseInt($("#num-holder").html());
			

						

					
					},
					
					"html"
			
				);	
	
	
	 });
	
	/*******************************************************************************************/

});