/* 	 ********************************************************************************
	 *
	 *	Type:			Javascript
	 *	Author:		Martin Hedvall
	 *	Created:		2008-10-01
	 *	Last change: 	2008-10-30
	 *
	 *	Functions:		viewMoney
	 *
	 *	Description:	Views the money written in the current input field as note images in the
	 *				current column. If column != income column, the amount of money will
	 *				be subtracted from the income column.
	 *
	 **********************************************************************************
*/
function viewMoney(Income, Categories, Category) {
	var IncomeMoney = Income;
	var CategoryMoney = 0;
	if(Category != 0) {
		//Calculate money for income field
		for(var a=1; a<=Categories; a++) {
			var id = "field_" + a;
			if(a!=5) {
				var MoneyInThisCategory = document.getElementById(id);
				//Replace comma with dot in order to make calculations work
				MoneyInThisCategory.value.replace(/,/, ".");
				//Remove possible bad chars
				if(removeBadChars(MoneyInThisCategory.value)!=MoneyInThisCategory.value) {
					document.getElementById(id).value = removeBadChars(MoneyInThisCategory.value);
				}
				
				IncomeMoney -= MoneyInThisCategory.value;
				if(a == Category) {
					CategoryMoney = MoneyInThisCategory.value;
				}
			}
		}
	}
	else {
		//Income field - paint only money in income field
		IncomeMoney = document.getElementById('field_0').value;
		if(removeBadChars(IncomeMoney)!=IncomeMoney) {
			document.getElementById('field_0').value = removeBadChars(IncomeMoney);
		}
		CategoryMoney = 0;
	}
	//Money information collected - paint the notes
	for(var a=1; a<=2; a++) {
		var columnMoney = IncomeMoney;
		var MoneyDivID = "MoneyDiv_0";
		var display_block = "";
		if((a==1) || ((a==2) && (Category != 0))) {
			//Category money for loop number 2. Money will be zero if income field is changed ( = step 1)
			if(a==2) {
				columnMoney = CategoryMoney;
				MoneyDivID = "MoneyDiv_"+Category;
			}
			
			//Error protection - prevent negative amount of notes
			if(columnMoney < 100) {
				columnMoney = 0;
			}
			
			//Calculate number of notes and note positions in the current field
			var leftborderwidth = 24;	//px
			var rightborderwidth = 10; //px
			var linewidth = 5;			//px
			
			//Define constants
			var available_height = 294; //px
			var available_width = 693; //px
			//Note heights
			var note100_height = 25; //px
			var note500_height = 30; //px
			var note1000_height = 33; //px
			var bundle_height = 38; //px
			//Note widths
			var note100_width = 50; //px
			var note500_width = 55; //px
			var note1000_width = 64; //px
			var bundle_width = 67; //px
			//Define spaces
			var max_space = 20; //px, (maximum distance between notes)
			var min_space = 8; //px, (minimum distance between notes)
			var maxNotes = Math.floor((available_height - note1000_height)/min_space);
			var categoryWidth = Math.floor((available_width - leftborderwidth - rightborderwidth - linewidth*Categories)/(Categories+1));
			
			//Define variables
			var display_bundle = false;
			var nr1000 = 0;
			var nr500 = 0;
			var nr100 = 0;
			var totalNotes = 0;
			var spaceBetweenNotes = 30;
			var overflowMoney = 0;
			
			//Calculate the number of notes of each kind
			nr1000 = Math.floor(columnMoney/1000);
			nr500 = Math.floor((columnMoney-1000*nr1000)/500);
			nr100 = Math.floor((columnMoney-1000*nr1000-500*nr500)/100);
			totalNotes = nr1000 + nr500 + nr100;
			
			//Make sure that all notes can fit in the space
			if(totalNotes > maxNotes) {
				//Reduce the number of 1000-notes to be displayed and show bundle
				display_bundle = true;
				nr1000 = nr1000 - (totalNotes-maxNotes) - Math.floor((bundle_height+2)/min_space);
				overflowMoney = columnMoney - nr1000*1000 - nr500*500 - nr100*100;
				if(overflowMoney < 0) {
					overflowMoney = 0;
				}
			}
			
			//No 1000-notes => notes can never exceed available space
			if((note1000_height+totalNotes*max_space) < available_height) {
				spaceBetweenNotes = max_space;
			}
			else if((note1000_height+totalNotes*min_space) < available_height) {
				//Optimal space exist - calculate it!
				spaceBetweenNotes = Math.floor((available_height-note1000_height)/totalNotes);
			}
			else {
				//All notes cannot be displayed -display as many as possible
				spaceBetweenNotes = min_space;
			}
			
			//End of calculations - display everything
			var notemargin = 0;
			
			var noteFixedMargin = Math.floor((categoryWidth-bundle_width)/2);
			if(display_bundle) {
				display_block = display_block + "<img src=\"/images/layout/money_bundle.png\" style=\"padding-left: " + (noteFixedMargin) + "px; margin-top: " + notemargin + "px\"  alt=\"" + overflowMoney + "kr\" />";
				notemargin += bundle_height+2;
			}
			noteFixedMargin = Math.floor((categoryWidth-note1000_width)/2);
			for(var t=1; t <= nr1000; t++) {
				display_block = display_block + "<img src=\"/images/layout/money_1000.jpg\" style=\"padding-left: " + (noteFixedMargin + Math.round(Math.random()*6-3)) + "px; margin-top: " + notemargin + "px\"  alt=\"1000kr\" />";
				notemargin += spaceBetweenNotes;
			}
			
			noteFixedMargin = Math.floor((categoryWidth-note500_width)/2);
			for(var f=1; f<=nr500; f++) {
				display_block = display_block + "<img src=\"/images/layout/money_500.jpg\" style=\"padding-left: " + (noteFixedMargin+Math.round(Math.random()*6-3)) + "px; margin-top: " + notemargin + "px\"  alt=\"500kr\" />";
				notemargin += spaceBetweenNotes;
			}
			noteFixedMargin = Math.floor((categoryWidth-note100_width)/2);
			for(var h=1; h<=nr100; h++) {
				display_block = display_block + "<img src=\"/images/layout/money_100.jpg\" style=\"padding-left: " + (noteFixedMargin+Math.round(Math.random()*6-3)) + "px; margin-top: " + notemargin + "px\"  alt=\"100kr\" />";
				notemargin += spaceBetweenNotes;
			}
			
			//Display the money
			document.getElementById(MoneyDivID).innerHTML = display_block;
			
			//Display the amount of money left of income as text
			document.getElementById('income_left').innerHTML = "Kvar:<br />" + IncomeMoney + "kr";
			if(IncomeMoney >= 0) {
				document.getElementById('income_left').style.color = '#ffffff';
			}
			else {
				document.getElementById('income_left').style.color = '#ff0000';
			}
		}
	}
}

function removeBadChars(sText) {
   var ValidChars = "0123456789.";
   var Char;
   var outPutText = "";
 
   for (i = 0; i < sText.length; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) != -1) {
         //number - store it
			if(Char == ",") {
				Char = ".";
			}
		 outPutText = outPutText+""+Char;
      }
	  //Just one dot is allowed
	  if(Char == ".") {
		//No more dots allowed
		ValidChars = "0123456789";
	  }
   }
   return outPutText;
}
