var InstallmentDataContainer = function( id, bank )
{
	this.id		= id;
	this.parent	= null; // no default parent
	this.bank = ( bank ) ? bank: '';
	if ( this.bank ) this.Refresh();
}

InstallmentDataContainer.prototype.IsEmpty = function()
{
	return ( this.bank == '' ) ? true:false;
}

InstallmentDataContainer.prototype.Refresh = function()
{
	var $container = $('#'+this.id);
	
	if ( $container.length <= 0 || !this.parent ) return;
	
	if ( this.bank == '' ) {
		$container.html('');
		return;
	}

	$container.html('<img id=\"imgloading\" src=\"/img/logowaitsmall.gif\">');

	var url = this.bank + '.installment?';
	
	var paramnames = new Array("rn","ck","p", "mp", "sp", "spu", "dav");
	var paramvalues = new Array(this.parent.rate,this.parent.punit,this.parent.price, this.parent.mpr, this.parent.supplierprice, this.parent.supplierpriceunit, this.parent.dav);
	
	for(i=0;i<paramnames.length;i++) {
		url += paramnames[i]+'='+paramvalues[i]+"&";
	}
	
	url += 'fpr=' + GetRandomNumberString();

	this.OnSuccessfulRequest = function( data ) { $container.html( data ); }
	this.OnError = function() { $container.html(GezisitesiResources.StaticText.m003); }

	$.ajax({
		type:'GET',
		url: url,
		dataType: 'html',
		async: true,
		success: this.OnSuccessfulRequest,
		error: this.OnError
	});

}

InstallmentDataContainer.prototype.ClearContent = function()
{
	this.bank = '';
	this.Refresh();
}

InstallmentDataContainer.prototype.SetInstallmentBank = function( bank )
{
	this.bank = bank;
	this.Refresh();
}

/* [END] InstallmentDataContainer */

var InstallmentModule = function( name, punit, price, rate, mp, supplierprice, supplierpriceunit, dav )
{
	this.name = name;
	this.punit = punit;
	this.price = price;
	this.rate = rate;
	this.mpr = mp;
	this.supplierprice = supplierprice;
	this.supplierpriceunit = supplierpriceunit;
	this.dav = dav;
	this.containers = new Array();
}

InstallmentModule.prototype.init = function ()
{
	this.inspanel = new YAHOO.widget.Panel(this.name, { width:"925", fixedcenter: true, constraintoviewport: false, underlay:"none", close:true, visible:false, draggable:true, modal:true, iframe:true, zIndex:10000 } );
	this.inspanel.render();

}

InstallmentModule.prototype.SetDav = function( dav ) { this.dav = dav; }
InstallmentModule.prototype.SetSupplierPrice = function( supplierprice ) { this.supplierprice = supplierprice; }
InstallmentModule.prototype.SetSupplierPriceUnit = function( supplierpriceunit ) { this.supplierpriceunit = supplierpriceunit; }
InstallmentModule.prototype.SetPrice = function ( price ) {this.price = price;}
InstallmentModule.prototype.SetRateKey = function ( rate ) {this.rate = rate;}
InstallmentModule.prototype.SetPriceUnit = function ( punit ) {this.punit = punit;}
InstallmentModule.prototype.SetMP = function ( mp ) {this.mpr = mp;}
InstallmentModule.prototype.Show = function () { this.inspanel.show(); }

InstallmentModule.prototype.AddContainer = function( installmentDataContainer )
{
	installmentDataContainer.parent = this;
	this.containers.push( installmentDataContainer );
	return true;
}

InstallmentModule.prototype.GetAvailableContainer = function()
{
	var containerCount = this.containers.length;
	for( var i = 0; i < containerCount ; i++ )
		if ( this.containers[i].IsEmpty() ) return this.containers[i];
	return null;
}

InstallmentModule.prototype.GetContainerByBank = function( bank )
{
	var containerCount = this.containers.length;
	for( var i = 0; i < containerCount; i++ )
		if ( this.containers[i].bank == bank ) return this.containers[i];
	return null;
}

InstallmentModule.prototype.RefreshContent = function()
{
	var containerCount = this.containers.length;
	for( var i = 0; i < containerCount; i++ )
		this.containers[i].Refresh();
}

InstallmentModule.prototype.LoadInstallment = function( bank, containerId )
{
	var $container = $('#'+containerId);

	if ( $container.length <= 0 ) return;

	if ( bank == '' ) {
		$container.html('');
		return;
	}

	var url = bank + '.installment?';
	
	var paramnames = new Array( "rn", "ck", "p", "mp", "sp", "spu", "dav");
	var paramvalues = new Array( this.rate, this.punit, this.price, this.mpr, this.supplierprice, this.supplierpriceunit, this.dav );
	
	for(i=0;i<paramnames.length;i++)
	{
		url += paramnames[i]+'='+paramvalues[i]+"&";
	}

	url += 'fpr=' + GetRandomNumberString();

	this.OnSuccessfulRequest = function( data ) { $container.html( data ); }
	this.OnError = function() { $container.html(GezisitesiResources.StaticText.m003); }

	$.ajax({
		type:'GET',
		url: url,
		dataType: 'html',
		async: true,
		success: this.OnSuccessfulRequest,
		error: this.OnError
	});

}

InstallmentModule.prototype.onBankSelectionChange = function( bankCheckBox )
{

	if ( !bankCheckBox ) return;
	
	if ( bankCheckBox.checked ) {
		var availableContainer = this.GetAvailableContainer();
		if ( availableContainer ) {
			availableContainer.SetInstallmentBank( bankCheckBox.value );
		} else {
			alert( GezisitesiResources.StaticText.m043 );
			bankCheckBox.checked = false;
		}
	} else {
		var currentContainer = this.GetContainerByBank( bankCheckBox.value );
		if ( currentContainer ) {
			currentContainer.ClearContent();
		}
	}

}

