
var obj = {
	// управляющие элементы
	'sc-mode': {},
	'sc-direct': {},
	'sc-propery-type': {},
	'sc-taking-mortgage': {},

	// поля ввода
	'sc-application': { t: 'int' },
	'sc-appraisal': { t: 'int' },
	'sc-attorney-fees': { t: 'int' },
	'sc-attorney-fees2': { t: 'int' },
	'sc-attorney-fees3': { t: 'int' },
	'sc-bank-attorney': { t: 'int' },
	'sc-bank-attorney2': { t: 'int' },
	'sc-credit-report': { t: 'int' },
	'sc-common-charges': { t: 'int' },
	'sc-downpayment': { t: 'percent' },
	'sc-electronic-tax': { t: 'int' },
	'sc-estate-fees': { t: 'int' },
	'sc-flip-tax': { t: 'int' },
	'sc-gains-tax': { t: 'int' },
	'sc-lead-paint': { t: 'int' },
	'sc-lead-paint2': { t: 'int' },
	//'sc-lien-search': { t: 'int' },
	'sc-lost-stock': { t: 'int' },
	'sc-maintenance-adjustment': { t: 'int' },
	//'sc-managing-agent': { t: 'int' },
	//'sc-managing-agent2': { t: 'int' },
	'sc-managing-agent3': { t: 'int' },
	'sc-mortgage-app': { t: 'int' },
	'sc-movein-deposit': { t: 'int' },
	'sc-movein-deposit2': { t: 'int' },
	'sc-moveout-deposit': { t: 'int' },
	'sc-non-resident-tax': { t: 'int' },
	'sc-origination-rate': { t: 'percent' },
	'sc-other-bank-fees': { t: 'int' },
	'sc-payoff-bank': { t: 'int' },
	'sc-price': { t: 'int' },
	'sc-recognition-agreement': { t: 'int' },
	'sc-recording-expenses': { t: 'int' },
	'sc-satisfaction-fee': { t: 'int' },
	'sc-sellers-attorney': { t: 'int' },
	'sc-stock-transfer': { t: 'int' },
	'sc-survey-fees': { t: 'int' },
	'sc-title-fees': { t: 'int' },
	'sc-ucc1-filing': { t: 'int' },
	//'sc-uss3-filing': { t: 'int' },

	// зависимости показа элементов
	'sc-dep-01a': {d: {'buyer': '+', 'seller': '-'}},
	'sc-dep-01b': {d: {'buyer': '+', 'seller': '-'}},
	'sc-dep-01c': {d: {'buyer': '+', 'seller': '-'}},
	'sc-dep-02': {d: {'buyer': '+', 'seller': '-'}},
	'sc-dep-03': {d: {'buyer': '+', 'seller': '-'}},
	'sc-dep-04': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-05a': {d: {'buyer': '+', 'seller': '-', 'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-05a-s': {d: {'buyer': '+', 'seller': '-', 'condo': '-', 'townhouse': '-', 'coop': '+'}},
	'sc-dep-05b': {d: {'buyer': '+', 'seller': '-'}},
	'sc-dep-05c': {d: {'buyer': '+', 'seller': '-'}},
	'sc-dep-06': {d: {'buyer': '+', 'seller': '-'}},
	'sc-dep-07': {d: {'seller': '-', 'direct': '+', 'no-direct': '-'}},
	'sc-dep-08a': {d: {'seller': '-', 'direct': '+', 'no-direct': '-'}},
	'sc-dep-08b': {d: {'seller': '-', 'direct': '+', 'no-direct': '-'}},
	'sc-dep-09': {d: {'seller': '-', 'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-10a': {d: {'seller': '-', 'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-10b': {d: {'seller': '-', 'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-11': {d: {'seller': '-', 'condo': '+', 'townhouse': '-', 'coop': '-'}},
	'sc-dep-12a': {d: {'seller': '-', 'condo': '+', 'townhouse': '-', 'coop': '-'}},
	'sc-dep-12b': {d: {'seller': '-', 'condo': '+', 'townhouse': '-', 'coop': '-'}},
	'sc-dep-13': {d: {'seller': '-', 'mortgage': '+', 'loan': '-', 'no-mortgage': '-'}},
	'sc-dep-13-s': {d: {'seller': '-', 'mortgage': '-', 'loan': '+', 'no-mortgage': '-'}},
	'sc-dep-14a': {d: {'seller': '-', 'mortgage': '+', 'loan': '+', 'no-mortgage': '-'}},
	'sc-dep-14b': {d: {'seller': '-', 'mortgage': '+', 'loan': '+', 'no-mortgage': '-'}},
	'sc-dep-15': {d: {'seller': '-', 'condo': '-', 'townhouse': '-', 'coop': '+'}},
	'sc-dep-16a': {d: {'seller': '-', 'condo': '-', 'townhouse': '-', 'coop': '+'}},
	'sc-dep-16b': {d: {'seller': '-', 'condo': '-', 'townhouse': '-', 'coop': '+'}},
	'sc-dep-17': {d: {'seller': '-', 'condo': '-', 'townhouse': '-', 'coop': '+'}},
	'sc-dep-18a': {d: {'seller': '-', 'condo': '-', 'townhouse': '-', 'coop': '+'}},
	'sc-dep-18b': {d: {'seller': '-', 'condo': '-', 'townhouse': '-', 'coop': '+'}},
	'sc-dep-19a': {d: {'buyer': '+', 'seller': '-', 'mortgage': '+', 'loan': '+', 'no-mortgage': '-'}},
	'sc-dep-19b': {d: {'buyer': '+', 'seller': '-', 'mortgage': '+', 'loan': '+', 'no-mortgage': '-'}},
	'sc-dep-19c': {d: {'buyer': '+', 'seller': '-', 'mortgage': '+', 'loan': '+', 'no-mortgage': '-'}},
	'sc-dep-20': {d: {'buyer': '+', 'seller': '-', 'mortgage': '+', 'loan': '+', 'no-mortgage': '-'}},
	'sc-dep-21a': {d: {'buyer': '+', 'seller': '-', 'mortgage': '+', 'loan': '+', 'no-mortgage': '-'}},
	//'sc-dep-21b': {d: {'buyer': '+', 'seller': '-', 'mortgage': '+', 'loan': '+', 'no-mortgage': '-'}}, --> sc-mortgage
	'sc-dep-21c': {d: {'buyer': '+', 'seller': '-', 'mortgage': '+', 'loan': '+', 'no-mortgage': '-'}},
	'sc-dep-22': {d: {'buyer': '+', 'seller': '-', 'mortgage': '+', 'loan': '+', 'no-mortgage': '-'}},
	'sc-dep-23': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-24a': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-24b': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-25': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-26a': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-26b': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-27': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-28a': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-28b': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-29': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-30a': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-30b': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-31a': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-31b': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-31c': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-32': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-33a': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-33b': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-33c': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-34': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-35': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-36a': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-36b': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-37': {d: {'seller-condo': '+', 'seller-townhouse': '+', 'seller-coop': '-'}},
	'sc-dep-38': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-39': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-40a': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-40b': {d: {'buyer': '-', 'seller': '+'}},
	'sc-dep-41a': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-41b': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-41c': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-42': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-43a': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-43b': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-43c': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-44': {d: {'seller-condo': '-', 'seller-townhouse': '-', 'seller-coop': '+'}},
	'sc-dep-45a': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-45b': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-45c': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-46': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-47a': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	// 'sc-dep-47b': {d: {'condo': '+', 'coop': '-'}}, --> sc-mortgage-title-insurance
	'sc-dep-47c': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-48': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-49a': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-49b': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-50a': {d: {'condo': '-', 'townhouse': '-', 'coop': '+'}},
	'sc-dep-50b': {d: {'condo': '-', 'townhouse': '-', 'coop': '+'}},
	'sc-dep-51': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-52': {d: {'condo': '-', 'townhouse': '-', 'coop': '+'}},
	'sc-dep-53': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-54': {d: {'condo': '-', 'townhouse': '-', 'coop': '+'}},
	'sc-dep-55a': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	//'sc-dep-55b': {d: {'condo': '+', 'coop': '-'}}, --> sc-mortgage-tax
	'sc-dep-55c': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-56': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-57a': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-57b': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-57c': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-58': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-59a': {d: {'condo': '-', 'townhouse': '-', 'coop': '+'}},
	'sc-dep-59b': {d: {'condo': '-', 'townhouse': '-', 'coop': '+'}},
	'sc-dep-59c': {d: {'condo': '-', 'townhouse': '-', 'coop': '+'}},
	'sc-dep-60': {d: {'condo': '-', 'townhouse': '-', 'coop': '+'}},
	//'sc-dep-61a': {d: {'condo': '-', 'coop': '+'}},
	//'sc-dep-61b': {d: {'condo': '-', 'coop': '+'}},
	//'sc-dep-61c': {d: {'condo': '-', 'coop': '+'}},
	//'sc-dep-62': {d: {'condo': '-', 'coop': '+'}},
	'sc-dep-63': {d: {'buyer': '+', 'seller': '-', 'mortgage': '+', 'loan': '+', 'no-mortgage': '-'}},
	'sc-dep-64': {d: {'buyer': '+', 'seller': '+', 'mortgage': '-', 'loan': '-', 'no-mortgage': '+'}},
	'sc-dep-65': {d: {'buyer': '+', 'seller': '-', 'mortgage': '+', 'loan': '+', 'no-mortgage': '-'}},
	'sc-dep-66': {d: {'buyer': '+', 'seller': '+', 'mortgage': '-', 'loan': '-', 'no-mortgage': '+'}},
	'sc-dep-67a': {d: {'condo': '-', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-67b': {d: {'condo': '-', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-67c': {d: {'condo': '-', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-67d': {d: {'condo': '-', 'townhouse': '+', 'coop': '-'}},
	'sc-dep-68a': {d: {'direct': '+', 'no-direct': '-'}},
	'sc-dep-68b': {d: {'direct': '-', 'no-direct': '+'}},

	// элементы вывода
	'sc-broker-commision': {},
	'sc-closing-costs': {},
	'sc-mansion-text': {},
	'sc-mansion-text2': {},
	'sc-mortgage': {d: {'buyer': '+', 'seller': '-', 'mortgage': '+', 'loan': '+', 'no-mortgage': '-'}},
	'sc-mortgage-tax': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-mortgage-title-insurance': {d: {'condo': '+', 'townhouse': '+', 'coop': '-'}},
	'sc-nyc-transfer-tax': {},
	'sc-nyc-transfer-tax2': {},
	'sc-nys-transfer-tax': {},
	'sc-nys-transfer-tax2': {},
	'sc-origination-amount': {},
	'sc-title-insurance': {}
};

var obj_dependence = {
	'buyer': {},
	'seller': {},
	'direct': {},
	'no-direct': {},
	'condo': {},
	'townhouse': {},
	'coop': {},
	'seller-condo': {},
	'seller-townhouse': {},
	'seller-coop': {},
	'buyer-condo': {},
	'buyer-townhouse': {},
	'buyer-coop': {},
	'mortgage': {},
	'loan': {},
	'no-mortgage': {}
};

var obj_success = true;
var current_mode, direct_mode, property_mode, mortgage_mode;

function cs_Start () {
	var t;

	for (var name in obj) {
		t = document.getElementById(name);
		if (!t) {
			obj_success = false;
			break;
		}

		obj[name].i = t;
		if (obj[name].t) {
			obj[name].i.valuetype = obj[name].t;
			obj[name].i.onkeyup = sc_CheckValue;
		}

		if (obj[name].d) {
			for (var dep in obj[name].d) {
				obj_dependence[dep][name] = obj[name].d[dep];
			}
		}
	}

	if (!obj_success) return;

	obj['sc-mode'].i.onchange = sc_TypeChange;
	obj['sc-direct'].i.onchange = sc_TypeChange;
	obj['sc-propery-type'].i.onchange = sc_TypeChange;
	obj['sc-taking-mortgage'].i.onchange = sc_TypeChange;

	// проверяем настройки калькулятора, пересчитываем все значения
	sc_TypeChange();
}

function sc_Calculate () {
	if (!obj_success) return;

	// общая цена недвижимости
	var price = sc_GetValue('sc-price');

	// процент цены, выплачиваемый сразу
	var downpayment = sc_GetValue('sc-downpayment');

	// сумма кредита
	var mortgage = (price * (100 - downpayment)) / 100;

	// блок 'Buying from Sponsor'
	var nyc_transfer_tax = price * (price > ccVariable['nyc-transfer-tax-divide'] ? ccVariable['nyc-transfer-tax-value-high'] : ccVariable['nyc-transfer-tax-value-low']);
	var nys_transfer_tax = price * ccVariable['nys-transfer-tax'];
	var seller_attorney_fee = sc_GetValue('sc-sellers-attorney');
	var block_sponsor = nyc_transfer_tax + nys_transfer_tax + seller_attorney_fee;

	// блок 'Fees for Condo/Townhouse'
	var attorney_fees = sc_GetValue('sc-attorney-fees');
	var survey_fees = sc_GetValue('sc-survey-fees');
	var recording_expenses = sc_GetValue('sc-recording-expenses');
	var title_insurance = Math.round(price / 100000) * ccVariable['title-insurance'];
	// база Mansion Tax состоит из цены недвижимости и налогов на недвижимость города и штата
	var mansion_tax_base = direct_mode == 'direct' ? price + nyc_transfer_tax + nys_transfer_tax : price;
	var mansion_tax = mansion_tax_base * (mansion_tax_base > ccVariable['mansion-tax-divide'] ? ccVariable['mansion-tax-value-high'] : ccVariable['mansion-tax-value-low']);
	var block_condo_fees = attorney_fees + survey_fees + recording_expenses + title_insurance + mansion_tax;

	// блок 'Fees for Coop'
	var attorney_fees2 = sc_GetValue('sc-attorney-fees2');
	var block_coop_fees = attorney_fees2 + mansion_tax;

	// блок 'Building-related Fees' [condo]
	var application = sc_GetValue('sc-application');
	//var managing_agent = sc_GetValue('sc-managing-agent');
	var movein_deposit = sc_GetValue('sc-movein-deposit');
	var common_charges = sc_GetValue('sc-common-charges');
	var lead_paint = sc_GetValue('sc-lead-paint');
	var block_building_condo = application + movein_deposit + common_charges + lead_paint;

	// блок 'Building-related Fees' [coop]
	//var managing_agent2 = sc_GetValue('sc-managing-agent2');
	var movein_deposit2 = sc_GetValue('sc-movein-deposit2');
	var maintenance_adjustment = sc_GetValue('sc-maintenance-adjustment');
	var lead_paint2 = sc_GetValue('sc-lead-paint2');
	var block_building_coop = movein_deposit2 + maintenance_adjustment + lead_paint2;

	// блок 'Bank and Mortgage-Associated Fees' [condo & coop]
	var appraisal = sc_GetValue('sc-appraisal');
	var credit_report = sc_GetValue('sc-credit-report');
	var mortgage_app = sc_GetValue('sc-mortgage-app');
	var ucc1_filing = sc_GetValue('sc-ucc1-filing');
	var bank_attorney = sc_GetValue(property_mode == 'condo' || property_mode == 'townhouse' ? 'sc-bank-attorney' : 'sc-bank-attorney2');
	var other_bank_fees = sc_GetValue('sc-other-bank-fees');
	var origination_rate = sc_GetValue('sc-origination-rate');
	var origination_amount = (price - mortgage) * origination_rate / 100;
	var mortgage_title_insurance = Math.round(price / 100000) * ccVariable['mortgage-title-insurance'];
	var mortgage_tax = mortgage * (mortgage > ccVariable['mortgage-tax-divide'] ? ccVariable['mortgage-tax-value-high'] : ccVariable['mortgage-tax-value-low']);
	var recognition_agreement = sc_GetValue('sc-recognition-agreement');
	//var lien_search = sc_GetValue('sc-lien-search');
	var block_bank_fees = credit_report + mortgage_app + bank_attorney + origination_amount;
	if (property_mode == 'townhouse') block_bank_fees += ucc1_filing;
	if (property_mode == 'condo' || property_mode == 'townhouse') block_bank_fees += appraisal + mortgage_title_insurance + mortgage_tax + other_bank_fees + recognition_agreement;

	// блок 'Attorney'
	var attorney_fees3 = sc_GetValue('sc-attorney-fees3');
	var block_attorney = attorney_fees3;

	// блок 'Taxes'
	var gains_tax = sc_GetValue('sc-gains-tax');
	var non_resident_tax = sc_GetValue('sc-non-resident-tax');
	var electronic_tax = sc_GetValue('sc-electronic-tax');
	var stock_transfer = sc_GetValue('sc-stock-transfer');
	var lost_stock = sc_GetValue('sc-lost-stock');
	var block_taxes = gains_tax + non_resident_tax + electronic_tax + nyc_transfer_tax + nys_transfer_tax;
	if (current_mode == 'seller' && property_mode == 'coop') {
		block_taxes += lost_stock + lost_stock;
	}

	// блок 'Recording, Filing'
	//var uss3_filing = sc_GetValue('sc-uss3-filing');
	var title_fees = sc_GetValue('sc-title-fees');
	var block_recording = title_fees;

	// блок 'Bank Fees' [seller]
	var payoff_bank = sc_GetValue('sc-payoff-bank');
	var satisfaction_fee = sc_GetValue('sc-satisfaction-fee');
	var block_seller_bank_fees = payoff_bank + satisfaction_fee;

	// блок 'Building-related Fees' [seller]
	var managing_agent3 = sc_GetValue('sc-managing-agent3');
	var moveout_deposit = sc_GetValue('sc-moveout-deposit');
	var flip_tax = sc_GetValue('sc-flip-tax');
	var estate_fees = sc_GetValue('sc-estate-fees');
	var block_building_seller = managing_agent3 + moveout_deposit;
	if (current_mode == 'seller' && property_mode == 'coop') {
		block_building_seller += flip_tax + estate_fees;
	}

	// блок 'Brokers'
	var broker_commision = price * ccVariable['broker-commision'];
	var block_brokers = broker_commision;

	// вычисляем итоговый результат
	var result = 0;

	switch (current_mode) {
		case 'buyer':
			if (direct_mode == 'direct') {
				result += block_sponsor;
			}
			switch (property_mode) {
				case 'condo':
					result += block_condo_fees;
					result += block_building_condo;
					break;
				case 'townhouse':
					result += block_condo_fees;
					break;
				case 'coop':
					result += block_coop_fees;
					result += block_building_coop;
					break;
			}
			if (mortgage_mode == 'mortgage' || mortgage_mode == 'loan') {
				result += block_bank_fees;
			}
			break;

		case 'seller':
			result += block_attorney + block_taxes + block_recording + block_seller_bank_fees + block_building_seller + block_brokers;
			break;
	}

	// расставляем значения
	sc_SetAmount(obj['sc-mortgage'], mortgage);
	sc_SetAmount(obj['sc-mansion-text'], mansion_tax);
	sc_SetAmount(obj['sc-mansion-text2'], mansion_tax);
	sc_SetAmount(obj['sc-title-insurance'], title_insurance);
	sc_SetAmount(obj['sc-nyc-transfer-tax'], nyc_transfer_tax);
	sc_SetAmount(obj['sc-nyc-transfer-tax2'], nyc_transfer_tax);
	sc_SetAmount(obj['sc-nys-transfer-tax'], nys_transfer_tax);
	sc_SetAmount(obj['sc-nys-transfer-tax2'], nys_transfer_tax);
	sc_SetAmount(obj['sc-origination-amount'], origination_amount);
	sc_SetAmount(obj['sc-mortgage-title-insurance'], mortgage_title_insurance);
	sc_SetAmount(obj['sc-mortgage-tax'], mortgage_tax);
	sc_SetAmount(obj['sc-broker-commision'], broker_commision);
	//sc_SetAmount(obj['sc-broker-commision2'], broker_commision_sponsor);

	sc_SetAmount(obj['sc-closing-costs'], result);
}

function sc_TypeChange () {
	if (!obj_success) return;

	current_mode = obj['sc-mode'].i.options[obj['sc-mode'].i.selectedIndex].value;
	property_mode = obj['sc-propery-type'].i.options[obj['sc-propery-type'].i.selectedIndex].value;

	if (current_mode == 'seller') {
		direct_mode = false;
		mortgage_mode = false;
		property_mode = 'seller-' + property_mode;
	}
	else {
		direct_mode = obj['sc-direct'].i.options[obj['sc-direct'].i.selectedIndex].value;
		mortgage_mode = obj['sc-taking-mortgage'].i.options[obj['sc-taking-mortgage'].i.selectedIndex].value;
		if (property_mode == 'coop' && mortgage_mode == 'mortgage') mortgage_mode = 'loan';
	}
	//alert('Direct: ' + direct_mode + ', Mortgage: ' + mortgage_mode + ', Property: ' + property_mode);

	// вносим изменения во внешний вид калькулятора
	sc_ApplyDependence(current_mode);
	sc_ApplyDependence(direct_mode);
	sc_ApplyDependence(property_mode);
	sc_ApplyDependence(mortgage_mode);

	// пересчитываем занчения
	sc_Calculate();
}

function sc_ApplyDependence (name) {

	if (!name || !obj_dependence[name]) return;

	var list = obj_dependence[name];
	for (var name in list) {
		obj[name].i.style.display = list[name] == '+' ? 'block' : 'none';
	}
}

function sc_GetValue (name) {

	if (!obj_success) return NaN;

	var right_value = false;
	var v = obj[name].i.value;

	switch (obj[name].i.valuetype) {
		case 'int':
			right_value = sc_CheckValueInt(v);
			if (right_value) v = parseInt(v);
			break;

		case 'percent':
			right_value = sc_CheckValuePercent(v);
			if (right_value) v = parseFloat(v);
			break;

		default:
			right_value = false;
			break;
	}

	if (right_value && !v) v = 0;

	return right_value ? v : NaN;
}

function sc_CheckValue (inEvent) {
	var ev = inEvent ? inEvent : event;
	var src;

	if (ev && ev.type == 'keyup' && !(ev.ctrlKey || ev.altKey)) {

		if (ev.srcElement) {
			src = ev.srcElement;
		}
		else if (ev.target) {
			src = ev.target;
		}

		if (!src || src.tagName != 'INPUT' || src.type != 'text') return;

		var right_value;

		switch (src.valuetype) {
			case 'int':
				right_value = sc_CheckValueInt(src.value);
				break;

			case 'percent':
				right_value = sc_CheckValuePercent(src.value);
				break;

			default:
				right_value = true;
				break;
		}

		src.className = right_value ? 'block-input' : 'block-input-error';

		sc_Calculate();
	}
}

function sc_CheckValuePercent (value) {
	if (!value) return true;
	return value == parseFloat(value) && 100 >= parseFloat(value) && parseFloat(value) >= 0;
}

function sc_CheckValueInt (value) {
	if (!value) return true;
	return value == parseInt(value) && parseInt(value) >= 0;
}

// вывод числа в указанный элемент
function sc_SetAmount (elem, value) {

	if (!elem || !elem.i) return;

	if (isNaN(value)) {
		elem.i.innerHTML = "?";
		return;
	}

	var setValue, sign = '', aliquot, fraction = '';

	setValue = Math.round(parseFloat(value) * 100) / 100;

	if (setValue < 0) {
		sign = "-";
		setValue = -setValue;
	}

	setValue = setValue.toString();

	if (setValue.indexOf(".") > 0) {
		aliquot = setValue.substr(0, setValue.indexOf("."));
		fraction = setValue.substr(setValue.indexOf(".") + 1);
	} else {
		aliquot = setValue;
	}

	if (aliquot.length > 4) {
		var res = '';
		for (i = Math.ceil(aliquot.length / 3); i > 1; i--) {
			crop = aliquot.length % 3 == 0 ? 3 : aliquot.length % 3;
			res += aliquot.substr(0, crop) + ",";
			aliquot = aliquot.substr(crop);
		}
		aliquot = res + aliquot;
	}

	if (fraction.length) {
		fraction = "." + fraction + (fraction.length == 1 ? "0" : "");
	}

	elem.i.innerHTML = sign + aliquot + fraction;
}
