var Courses                  = new Array();

var DocumentDiscounts        = new Array();
var DocumentDiscountFieldIds = new Array();
var DocumentDiscount         = 0;

var TotalPrice1ChbkxFieldIds = new Array();
var TotalPrice2ChbkxFieldIds = new Array();

var Packets                  = new Array();

function trim(string)
{
	return string.replace(/(^\s+)|(\s+$)/g, "");
}

function onWireTransferClick(element)
{
	var WireTransferInputFieldIds = new Array('caption', 'domicile', 'bank', 'rs', 'ks', 'inn', 'kpp', 'bik');

	if (element.checked)
	{
		for (i = 0; i < WireTransferInputFieldIds.length; ++i)
		{
			document.getElementById(WireTransferInputFieldIds[i]).disabled = false;
			document.getElementById(WireTransferInputFieldIds[i]).style.backgroundColor = '#F8FFE1';
		}
		document.getElementById(WireTransferInputFieldIds[0]).focus();
	}
	else
	{
		for (i = 0; i < WireTransferInputFieldIds.length; ++i)
		{
			document.getElementById(WireTransferInputFieldIds[i]).disabled = true;
			document.getElementById(WireTransferInputFieldIds[i]).style.backgroundColor = '#EEEEEE';
		}
	}
}

function round(val, precision)
{
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +    revised by: Onno Marsman
    // *     example 1: round(1241757, -3);
    // *     returns 1: 1242000
    // *     example 2: round(3.6);
    // *     returns 2: 4

    return parseFloat(val).toFixed(precision);
}

function apart(num)
{
	if (3 < num.length)
	{
		num = num.substr(0, num.length-3) + ' ' + num.substr(num.length-3, 3);
	}

	return num;
}

function convertByCourse(price)
{
	var Currency = $F('currency');

	if ('rur' == Currency)
	{
		return price;
	}
	else
	{
		return round(price / Courses['rur_to_'+Currency], 2);
	}
}

function FormatPrice(price)
{
	var Price  = price + '';

	var DotPos = Price.indexOf('.');

	if (-1 != DotPos)
	{
		var BeforeDot = Price.substr(0, DotPos);
		var AfterDot  = Price.substr(DotPos+1);
	}
	else
	{
		var BeforeDot = Price;
		var AfterDot  = '00';
	}

	return apart(BeforeDot) + '.' + AfterDot;
}

function setDocumentDiscount()
{
	DocumentDiscount = 0;

	for (i = 0; i < DocumentDiscountFieldIds.length; ++i)
	{
		var DocumentId         = $F(DocumentDiscountFieldIds[i]);
		var FieldDocumentCode  = trim($F(DocumentDiscountFieldIds[i]+'_code'));
		var FieldDocumentOwner = trim($F(DocumentDiscountFieldIds[i]+'_owner'));

		if ((0 != DocumentId) && (4 < FieldDocumentCode.length) && (4 < FieldDocumentOwner.length))
		{
			DocumentDiscount += DocumentDiscounts[DocumentDiscountFieldIds[i]][DocumentId];
		}
	}

	$('DocumentDiscount').update('<span>' + DocumentDiscount + '</span>');
}

function calcRDiscountByDAmount(classEl)
{
	var Discount    = 0;

	var Amount     = $F(classEl + '_amount');
	var Discount24 = $F(classEl + '_discount_24');
	var Discount5  = $F(classEl + '_discount_5');

	if (2 <= Amount && 5 > Amount)
	{
		Discount = Discount24;
	}
	else if (5 <= Amount)
	{
		Discount = Discount5;
	}

	return Discount;
}

function calcRCost(amount, discount, price)
{
	return price * amount * (100 - discount)/100;
}

function calcPriceWithoutDiscount($price, $discount)
{
	return $price - $price*(100 - $discount)/100;
}

function RecalcRFCost(element, classEl)
{
	if ('undefined' == typeof(classEl))
	{
		return;
	}

	var FieldIsActive = $(classEl + '_is_active');
	var FieldAmount   = $(classEl + '_amount');
	var FieldDiscount = $(classEl + '_discount');
	var FieldCost     = $(classEl + '_cost');

	if ('checkbox' == element.type)
	{
		if (FieldIsActive.checked)
		{
			FieldAmount.disabled = false;
			FieldAmount.style.backgroundColor = '#F8FFE1';
			FieldAmount.value    = 1;
			FieldAmount.focus();
		}
		else
		{
			FieldAmount.value    = 0;
			FieldAmount.style.backgroundColor = '#EEEEEE';
			FieldAmount.disabled = true;
		}
	}
	else if (0 == FieldAmount.value)
	{
		FieldIsActive.click();
	}

	var Discount      = calcRDiscountByDAmount(classEl);
	FieldDiscount.update(Discount);

	var Cost          = calcRCost(FieldAmount.value, Discount, FieldIsActive.value);
	FieldCost.update(FormatPrice(round(Cost, 2)));

	return;
}

function RecalcTotalCost()
{
	var TotalPrice  = 0;
	var TotalPrice1 = 0;
	var TotalPrice2 = 0;

	setDocumentDiscount();

	for (i = 0; i < TotalPrice1ChbkxFieldIds.length; ++i)
	{
		var FieldIsActive = $(TotalPrice1ChbkxFieldIds[i] + '_is_active');
		var FieldAmount   = $(TotalPrice1ChbkxFieldIds[i] + '_amount');

		if (undefined != FieldAmount)
		{
			var Discount  = calcRDiscountByDAmount(TotalPrice1ChbkxFieldIds[i]);
			var Cost      = calcRCost(FieldAmount.value, Discount, FieldIsActive.value);

			TotalPrice1  += (round(Cost, 2) - 0);
		}
	}

	for (i = 0; i < TotalPrice2ChbkxFieldIds.length; ++i)
	{
		var FieldIsActive = $(TotalPrice2ChbkxFieldIds[i] + '_is_active');
		var FieldAmount   = $(TotalPrice2ChbkxFieldIds[i] + '_amount');

		if (undefined != FieldAmount)
		{
			var Discount  = calcRDiscountByDAmount(TotalPrice2ChbkxFieldIds[i]);
			var Cost      = calcRCost(FieldAmount.value, Discount, FieldIsActive.value);

			TotalPrice2  += (round(Cost, 2) - 0);
		}
	}

	TotalPrice1 = round(TotalPrice1, 2);
	TotalPrice2 = round(TotalPrice2, 2);
	TotalPrice  = round((TotalPrice1 - 0) + (TotalPrice2 - 0), 2);

	if (convertByCourse(3000) < TotalPrice)
	{		var DocumentDiscountAmount = calcPriceWithoutDiscount(TotalPrice, DocumentDiscount);
		DocumentDiscountAmount     = round(DocumentDiscountAmount, 2);

		TotalPrice                 = TotalPrice - DocumentDiscountAmount;
		TotalPrice                 = round(TotalPrice, 2);
	}

//	$('TotalPrice1').update(FormatPrice(TotalPrice1));
	$('TotalPrice2').update(FormatPrice(TotalPrice2));
	$('TotalPrice').update(FormatPrice(TotalPrice));
}

function popUp(winURL)
{
	var winURL = window.open(winURL,"popup","width=580,height=500,status=yes,resizable=yes,scrollbars=yes");
}

function initHelpButtons()
{
    if (!document.getElementsByTagName) return false;

	var lnks = document.getElementsByTagName('a');

	for (var i=0; i<lnks.length; i++) {
		if (lnks[i].getAttribute('class') == 'btnHelpW') {
			lnks[i].onclick = function() {
				popUp(this.getAttribute("href"));
				return false;
			}
		}
	}
}

function togglePacketDescription(element)
{
	$$('tr.'+element.id).each(
		function(tr)
		{
			if ('none' == tr.style.display)
			{				tr.style.display = 'table-row';
				$(element.id).innerHTML = 'свернуть';
			}
			else
			{				tr.style.display = 'none';
				$(element.id).innerHTML = 'показать';
			}
		}
	);

	return false;
}

function hidePacketsDesc()
{
	for (i = 0; i < Packets.length; i++)
	{
		togglePacketDescription($(Packets[i]));
	}
}

function onCurrencyChanged()
{
	$('currency_changing').setValue(1);
	$('frmSignUp').submit();
}