//<!--

/**
 *	POMOCNE FCE
 */

var __vyvoj_dluhu ;
var __render_type = 1 ;
var __IE = ( document.all ) ? true : false ;

function toggle_printable( reset )
{
	if ( reset == undefined )
	{
		var reset = false ;
	}

	var a = document.getElementById ( "print_link" ) ;
	var c = document.getElementById ( "vyvoj_container" ) ;
	var tb = document.getElementById ( "tcontent" ) ;

	if ( a && c && tb )
	{
		if ( reset || tb.style.height == "auto" )
		{
			/**
			 *	Sbalení tabulky
			 */
			if ( __IE )
			{
				c.style.height = "420px" ;
				c.style.overflowX = "hidden" ;
				c.style.overflowY = "scroll" ;
			} else
			{
				tb.style.height = "420px" ;
				tb.style.overflowX = "hidden" ;
				tb.style.overflowY = "auto" ;
				c.style.height = "auto" ;
			}
			a.innerHTML = __vyvoj_dluhu.popisky[ __PAGE_LANGUAGE_CURRENT ][ 'tabulka_rozbal' ] ;
		} else
		{
			/**
			 *	Rozbalení tabulky
			 */
			c.style.height = "auto" ;
			tb.style.height = "auto" ;

			if ( __IE )
			{
				c.style.overflowX = "hidden" ;
				c.style.overflowY = "visible" ;
			} else
			{
				tb.style.overflowX = "hidden" ;
				tb.style.overflowY = "visible" ;
			}
			a.innerHTML = __vyvoj_dluhu.popisky[ __PAGE_LANGUAGE_CURRENT ][ 'tabulka_sbal' ] ;
		}
	}

	return false ;
}

function render_years()
{
	__render_type = 2 ;

	var ok = init_loan() ;

	if ( ok )
	{
		var r = document.getElementById ( "typ_years" ) ;

		if ( r )
		{
			r.checked = true;
		}
	}
	return false ;
}

function render_months()
{
	__render_type = 1 ;

	var ok = init_loan() ;

	if ( ok )
	{
		var r = document.getElementById ( "typ_months" ) ;

		if ( r )
		{
			r.checked = true;
		}
	}
	return false ;
}

function init_loan()
{
	var t = document.getElementById ( 'vyvojdluhu' ) ;
	var c = document.getElementById ( 'vyvoj_container' ) ;

	if ( typeof ( c ) == 'object' && c != null && typeof ( t ) == 'object' && t != null )
	{
		var x = true ;

		__vyvoj_dluhu.reset() ;

		var vyse = parseInt ( unformat ( document.getElementById ( 'inp_vyse' ).value ) ) ;
		var delka = parseInt ( unformat ( document.getElementById ( 'inp_delka' ).value ) ) ;
		var sazba = parseFloat ( unformat (document.getElementById ( 'inp_sazba' ).value ) ) ;

		document.getElementById ( 'inp_vyse' ).value = isNaN ( vyse ) ? '' : formatNumber ( vyse );
		document.getElementById ( 'inp_delka' ).value = isNaN ( delka ) ? '' : formatNumber ( delka ) ;
		document.getElementById ( 'inp_sazba' ).value = isNaN ( sazba ) ? '' : sazba ;

		x = __vyvoj_dluhu.load_inputs ( vyse, delka, sazba ) ;

		if ( x )
		{
			x = __vyvoj_dluhu.compute() ;
			
			if ( x )
			{
				x = __vyvoj_dluhu.compute_per_year() ;

				if ( x )
				{
					__vyvoj_dluhu.render ( c, __render_type ) ;

					if ( document.all )
					{
						t.style.display = 'block' ;
					} else
					{
						t.style.display = 'table-row-group' ;
					}

					/**
					 * Resetovat tabulkový výpis
					 */
					toggle_printable ( true ) ;

					document.getElementById ( 'inp_splatka' ).value = formatNumber ( parseInt ( __vyvoj_dluhu.splatka ).toString() ) ;

					/**
					 * Odšedit hlavičku výsledků pro samostatnou stránku vývoje dluhu
					 */
					var hlavicka = document.getElementById ( 'vyvojdluhuHead' ) ;

					if ( hlavicka != undefined )
					{
						hlavicka.className = '' ;
					}

					/**
					 * Zobrazit kontakt
					 */
					var kontakt = document.getElementById ( 'divKontakt' ) ;

					if ( kontakt != undefined )
					{
						kontakt.style.display = "block" ;
					}
				} else
				{
					x = false ;
				}
			}
		} else
		{
			x = false ;
		}
	} else
	{
		x = false ;
	}

	if ( x == false )
	{
		__vyvoj_dluhu.delete_table() ;

		/**
		 * Vyšedit hlavičku výsledků pro samostatnou stránku vývoje dluhu
		 */
		var hlavicka = document.getElementById ( 'vyvojdluhuHead' ) ;

		if ( hlavicka != undefined )
		{
			hlavicka.className = 'gray' ;
		}

		if ( c )
		{
			t.style.display = 'none' ;
		}
	}

	return x ;
}

function formatNumber ( number )
{
	var tmp = '' ;
	var out1 = '' ;
	var out2 = '' ;
	var c = '' ;
	var j = 1 ;

	number += '' ;

	for ( var i = number.length-1 ; i >= 0  ; i-- )
	{
		c = number.charAt ( i ) ;
		if ( c != ' ' )
		{
			out1 += c ;

			if ( ( j ) % 3 == 0 )
			{
				out1 += " ";
			}

			j++ ;
		}
	}

	for ( i = out1.length-1 ; i >= 0  ; i-- )
	{
		out2 += out1.charAt ( i ) ;
	}

	return out2 ;
}

function unformat ( num )
{
	var ret = '';

	for ( var i = 0; i < num.length; i++ )
	{
		var c = num.charAt( i ) ;
		if ( c != " " )
		{
			ret += c ;
		}
	}

	return ret ;
}

/**
 *	TODO: RegEx na pořádnou kontrolu
 */
function checkKontakty()
{
	//var tel = document.getElementById ( "kontaktTelefon" ) ;
	var mail = document.getElementById ( "kontaktEmail" ) ;

	if ( mail )		//	tel &&
	{
	
	  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(mail.value)) {		//	tel.value != '' &&
		
			return true ;
		} 
    else	{
			alert ( 'Vyplňte prosím Vaše kontaktní údaje.' ) ;

			/*
			if ( tel.value == '' )
			{
				tel.focus() ;
			} else
			{
			*/
				mail.focus() ;
			//}

			return false ;
		}
	} else
	{
		return false ;
	}
}

function noComma(e)
{
	if ( window.event )
	{
		if( e.keyCode == 44 && e.srcElement.nodeName.toLowerCase() == "input" )
		{
			e.keyCode = 46 ;
			noCommaFF (e.target) ;
			return false ;
		}
	}
	else
	{
		if( e.which == 44 && e.target.nodeName.toLowerCase() == "input" )
		{
			noCommaFF (e.target) ;
			return false ;
		}

	}
	return true;
}


function noCommaFF(obj)
{
	var start = obj.selectionStart;
	var end   = obj.selectionEnd;

	obj.value = obj.value.substr(0, start)
			    + '.'
			    + obj.value.substr(end, obj.value.length);
}


function  validateNumeric( textbox )
{
	/*****************************************************************
	DESCRIPTION: Validates that a string contains only valid numbers.

	PARAMETERS:
	   strValue - String to be tested for validity

	RETURNS:
	   True if valid, otherwise false.
	******************************************************************/
	var objRegExp  =  /(^\d\d*\.\d*$)|(^\d\d*$)|(^\d\d*$)|(^(\d\s*\d*)+\s*\.*\s*(\d\s*\d*)*$)/;
	var strValue = textbox.value + '' ;

	//check for numeric characters
	if ( strValue != '' && !objRegExp.test( strValue ) )
	{
		window.alert ( "Nezadali jste platné číslo." ) ;
		setTimeout( function() { textbox.focus() ; return false; } , 100 );
		if ( 0 )
		{
			window.event.cancelBubble = true;
		}
		return false ;
	} else
	{
		return true ;
	}
}

/**********************
 *	CLASS vyvoj_dluhu
 *
 *********************/

var __vyvoj_dluhu =
{
	delka_splaceni : null,
	vyse_uveru : null,
	sazba : null,
	sazba_mesicni : null,
	splatka : null,
	prubeh : null,
	prubeh_rocni : null,
	computed_base : false,
	computed_years : false,
	table : null,

	popisky : { 1 : { 	'tabulka_rozbal' : 'Rozbalit tabulku pro tisk',
						'tabulka_sbal' : 'Zabalit tabulku',
						'mesic' : 'Měsíc',
						'rok' : 'Rok',
						'zustatek' : 'Zůstatek úvěru',
						'spl_jistiny' : 'Splátka jistiny',
						'spl_uroku' : 'Splátka úroku',
						'dan_uspora' : 'Daňová úspora',
						'uspora_obdobi' : 'Úspora v daném období',
						'uspora_celkem' : 'Úspora celkem' },
				2 : { 	'tabulka_rozbal' : 'Prepare table for printing',
						'tabulka_sbal' : 'Shorten table',
						'mesic' : 'Month',
						'rok' : 'Year',
						'zustatek' : 'Loan balance',
						'spl_jistiny' : 'Repayment of the principle',
						'spl_uroku' : 'Interest payment ',
						'dan_uspora' : 'Savings on tax ',
						'uspora_obdobi' : 'Savings in this period',
						'uspora_celkem' : 'Total savings' } } ,
						
	load_inputs : function ( vyse, delka, sazba )
	{
		this.vyse_uveru = vyse ;
		this.delka_splaceni = delka ;

		if ( !isNaN ( sazba ) && parseFloat ( sazba ) > 0 )
		{
			this.sazba = sazba / 100 ;
			this.sazba_mesicni = sazba / 100 / 12 ;
			this.splatka = this.vyse_uveru / ( ( 1 - ( 1 / ( Math.pow ( 1 + this.sazba_mesicni, this.delka_splaceni * 12 ) ) ) ) / this.sazba_mesicni);
		}

		//this.splatka = splatka ;

		return this.check_inputs() ;
	},

	check_inputs : function ()
	{
		var ok = true ;

		ok &= ( this.vyse_uveru != null && parseInt( this.vyse_uveru  ) > 0 ) ;
		ok &= ( this.delka_splaceni != null && parseInt( this.delka_splaceni  ) > 0 ) ;
		ok &= ( this.sazba != null && parseFloat( this.sazba  ) > 0 ) ;
		ok &= ( this.sazba_mesicni != null && parseFloat( this.sazba_mesicni  ) > 0 ) ;
		ok &= ( this.splatka != null && parseInt( this.splatka  ) > 0 ) ;

		if ( ok )
		{
			ok = true ;
		} else
		{
			this.reset() ;
			ok = false ;
		}

		return ok ;
	},

	reset : function()
	{
		this.vyse_uveru = null;
		this.delka_splaceni = null;
		this.sazba = null;
		this.sazba_mesicni = null;
		this.splatka = null;
		this.prubeh = null ;
		this.prubeh_rocni = null ;

		this.computed_base = false ;
		this.computed_years = false ;
	},

	compute : function()
	{
		var ok = true ;

		if ( this.check_inputs() === true )
		{
			var zustatek = this.vyse_uveru ;

			try
			{
				this.prubeh = new Array() ;

				for ( var i = 1; i < ( this.delka_splaceni * 12 ); i++ )
				{
					var mesic = new Object() ;

					mesic[ 'spl_jistiny' ] = zustatek - ( zustatek * ( 1 + this.sazba_mesicni ) - this.splatka ) ;

					mesic[ 'spl_uroku' ] = this.splatka - mesic[ 'spl_jistiny' ] ;
					mesic[ 'zustatek' ] = zustatek ;
					mesic[ 'uspora' ] = mesic[ 'spl_uroku' ] * 0.15 ;

					this.prubeh[ i ] = mesic ;
					zustatek -= mesic[ 'spl_jistiny' ] ;
				}

				var mesic = new Object() ;

				mesic[ 'spl_uroku' ] = mesic[ 'uspora' ] = 0 ;
				mesic[ 'zustatek' ] = mesic[ 'spl_jistiny' ] = zustatek ;

				this.prubeh[ i ] = mesic ;

			} catch ( ex )
			{
				ok = false ;
				alert ( "Při výpočtu došlo k chybě! \nZkontrolujte prosím zadané údaje!" ) ;
			}
		} else
		{
			ok = false ;
		}

		if ( !ok )
		{
			this.reset ;
		}

		this.computed_base = ok ;
		return ok ;
	} ,

	compute_per_year : function()
	{
		var ok = true ;

		if ( this.computed_base === false )
		{
			ok = this.compute() ;
		}

		if ( ok === true )
		{
			try
			{
				this.prubeh_rocni = new Array() ;
				var m = new Object ;

				var spl_jistiny = 0 ;
				var spl_uroku = 0 ;
				var uspora = 0 ;

				for ( var i = 1; i <= this.delka_splaceni * 12; i++ )
				{
					m = this.prubeh[ i ] ;

					if ( m != null && typeof ( m ) == 'object' )
					{
						if ( i % 12 == 0 )
						{
							var rok = new Object() ;

							rok[ 'zustatek' ] = ( i / 12 != this.delka_splaceni ) ? m[ 'zustatek' ] : 0 ;
							rok[ 'spl_jistiny' ] = spl_jistiny ;
							rok[ 'spl_uroku' ] = spl_uroku ;
							rok[ 'uspora' ] = uspora ;

							this.prubeh_rocni[ i / 12 ] = rok ;

							spl_jistiny = 0 ;
							spl_uroku = 0 ;
							uspora = 0 ;
						} else
						{
							spl_jistiny += m[ 'spl_jistiny' ] ;
							spl_uroku += m[ 'spl_uroku' ] ;
							uspora += m[ 'uspora' ] ;
						}
					}
				}
			} catch ( ex )
			{
				ok = false ;
			}
		}

		this.computed_years = ok ;
		return ok ;
	},

	render : function ( container, type, render_head )
	{
		//var c = document.getElementById ( container_id ) ;
		if ( render_head == undefined )
		{
			render_head = true ;
		}
		c = ( typeof ( container ) == 'object' ) ? container : null ;

		if ( c != null )
		{
			if ( this.table != null )
			{
				this.delete_table() ;
			}

			var obdobi ;
			var interval ;
			var delka ;

			switch ( type )
			{

				case 1 :
					obdobi = this.popisky[ __PAGE_LANGUAGE_CURRENT ][ 'mesic' ] ;
					interval = 12 ;
					delka = this.delka_splaceni * 12 ;
				break ;

				case 2 :
					obdobi = this.popisky[ __PAGE_LANGUAGE_CURRENT ][ 'rok' ] ;
					interval = 0 ;
					delka = this.delka_splaceni ;
				break ;

				default :

				break ;
			}

			/**
			 *	Vytvořit tabulku a její hlavičku
			 */
			var tb = this.create_el ( "TABLE", [ 'id', 'cellSpacing' ], [ 'main_table', '0' ] ) ;

			if ( render_head )
			{
				var tbodyh = this.create_el ( "THEAD", [ "id" ], [ "thead" ] ) ;
				tb.appendChild ( tbodyh ) ;

				var tr = this.create_el ( 'TR', [ "className" ], [ "head" ] ) ;

				var th = this.create_el ( 'TH', [ "innerHTML", "width" ], [ obdobi, "15%" ] ) ;
				tr.appendChild ( th ) ;
				var th = this.create_el ( 'TH', [ "innerHTML" ], [ this.popisky[ __PAGE_LANGUAGE_CURRENT ][ 'zustatek' ] ] ) ;
				tr.appendChild ( th ) ;
				var th = this.create_el ( 'TH', [ "innerHTML" ], [ this.popisky[ __PAGE_LANGUAGE_CURRENT ][ 'spl_jistiny' ] ] ) ;
				tr.appendChild ( th ) ;
				var th = this.create_el ( 'TH', [ "innerHTML" ], [ this.popisky[ __PAGE_LANGUAGE_CURRENT ][ 'spl_uroku'] ] ) ;
				tr.appendChild ( th ) ;
				var th = this.create_el ( 'TH', [ "innerHTML" ], [ this.popisky[ __PAGE_LANGUAGE_CURRENT ][ 'dan_uspora' ] ] ) ;
				tr.appendChild ( th ) ;

				tbodyh.appendChild ( tr ) ;
			}

			var tbody = this.create_el ( "TBODY", [ "id" ], [ "tcontent" ] ) ;
			tb.appendChild ( tbody ) ;

			for ( var i = 1; i <= delka; i++ )
			{
				var obdobi ;

				if ( !interval )
				{
					obdobi = this.prubeh_rocni[ i ] ;
				} else
				{
					obdobi = this.prubeh[ i ] ;
				}

				var tr = this.create_el ( 'TR' ) ;

				var th = this.create_el ( 'TH', [ "innerHTML" ], [ i ] ) ;
				tr.appendChild ( th ) ;
				var td = this.create_el ( 'TD', [ "innerHTML" ], [ formatNumber ( parseInt ( obdobi[ 'zustatek' ] ) ) ] ) ;
				tr.appendChild ( td ) ;
				var td = this.create_el ( 'TD', [ "innerHTML" ], [ formatNumber ( parseInt ( obdobi[ 'spl_jistiny' ] ) ) ] ) ;
				tr.appendChild ( td ) ;
				var td = this.create_el ( 'TD', [ "innerHTML" ], [ formatNumber ( parseInt ( obdobi[ 'spl_uroku' ] ) ) ] ) ;
				tr.appendChild ( td ) ;
				var td = this.create_el ( 'TD', [ "innerHTML" ], [ formatNumber ( parseInt ( obdobi[ 'uspora' ] ) ) ] ) ;
				tr.appendChild ( td ) ;

				tbody.appendChild ( tr )

				if ( interval && i % interval == 0 )
				{
					var rok = this.prubeh_rocni[ i / interval ] ;

					var tr = this.create_el ( 'TR', [ 'className' ], [ 'bck_yellow' ] ) ;

					var th = this.create_el ( 'TH', [ "innerHTML" ], [ ( i / interval ) + ". " + this.popisky[ __PAGE_LANGUAGE_CURRENT ][ 'rok' ].toLowerCase() ] ) ;
					tr.appendChild ( th ) ;
					var th = this.create_el ( 'TH', [ "innerHTML" ], [ formatNumber ( parseInt ( rok[ 'zustatek' ] ) ) ] ) ;
					tr.appendChild ( th ) ;
					var th = this.create_el ( 'TH', [ "innerHTML" ], [ formatNumber ( parseInt ( rok[ 'spl_jistiny' ] ) ) ] ) ;
					tr.appendChild ( th ) ;
					var th = this.create_el ( 'TH', [ "innerHTML" ], [ formatNumber ( parseInt ( rok[ 'spl_uroku' ] ) ) ] ) ;
					tr.appendChild ( th ) ;
					var th = this.create_el ( 'TH', [ "innerHTML" ], [ formatNumber ( parseInt ( rok[ 'uspora' ] ) ) ] ) ;
					tr.appendChild ( th ) ;

					tbody.appendChild ( tr )
				}
			}

			c.appendChild ( tb ) ;
			this.table = tb ;
		}
	},

	create_el : function ( type, attr_names, attr_vals )
	{
		var el = document.createElement ( type ) ;

		if ( el != undefined && attr_names != undefined && attr_vals != undefined && typeof ( attr_names ) == 'object' && typeof ( attr_vals ) == 'object' )
		{
			for ( i in attr_names )
			{
				var atr = attr_names[ i ] ;
				var val = attr_vals[ i ] ;

				if ( atr != undefined && val != undefined && typeof ( atr ) != 'object' && typeof ( val ) != 'object'  )
				{
					el[ atr ] = val ;
				}
			}
		}

		return el ;
	},

	delete_table : function()
	{
		if ( this.table != null )
		{
			var t = document.getElementById ( this.table.id ) ;
			if ( t != undefined )
			{
				t.parentNode.removeChild ( t ) ;
			}

			this.table == null
		}
	}
}


//-->
