
/*******************************************************************************
 * Didžiausio būsto kredito skaičiuoklė / MaxHomeCreditAction ******************
 ******************************************************************************/
function MaxHomeCreditTable() {
	this.validation = new SEBFinValidation();
	this.math = new SEBFinMath();
	this.disableString = "---";

	this.calc = maxHomeCreditResultCalc;	
	this.disable = maxHomeCreditResultDisable;
}

function maxHomeCreditResultCalc() {
	var income = this.validation.getIntValue($("#mhc_table #income"));
	var period = this.validation.getIntValue($("#mhc_table #period"));
	var interest = this.validation.getFloatValue($("#mhc_table #interest"));
	var cValue = this.validation.getFloatValue($("#mhc_c_value"));

	if (income == null || period == null || interest == null) {
		return;
	}

	var incomeCoef = 0;
	if (income <= 900) {
		incomeCoef = 0.2999;
	} else if (income <= 3000) {
		incomeCoef = 0.3999;
	} else if (income <= 7000) {
		incomeCoef = 0.4999;
	} else {
		incomeCoef = 0.5999;
	}
		
	var restIncome = null;
	if (cValue != null && cValue > 0) {
		restIncome = income * incomeCoef - cValue;
	} else {
		restIncome = income * incomeCoef;
	}

	if (income < 600 || (restIncome != null && restIncome < 0)) {
		$("#mhc_income_err").show();
		this.disable();
		return;
	} else {
		$("#mhc_income_err").hide();
	}

	var maxCredit = restIncome * ((1 - 1 / Math.pow((1 + interest / 1200), 12 * period)) / (interest / 1200));
	var amount = Math.round(maxCredit);
	var compen = (period * 12 * restIncome - maxCredit) * 0.33;
		
	restIncome = this.math.roundFloat(restIncome, 2);
	maxCredit = this.math.roundFloat(maxCredit, 2);
	compen = this.math.roundFloat(compen, 2);

	$("#mhc_res_table #return_payment").html(this.validation.floatValueStr(restIncome));
	$("#mhc_res_table #max_credit_sum").html(this.validation.floatValueStr(maxCredit));	
	// $("#mhc_res_table #return_compen").html(this.validation.floatValueStr(compen));

	$("#mhc_request_amount").val(Math.round(amount));
	$("#mhc_request_period").val(period);
}

function maxHomeCreditResultDisable() {
	$("#mhc_res_table #max_credit_sum,#mhc_res_table #return_payment," +
		"#mhc_res_table #return_compen").html(this.disableString);
}

/*******************************************************************************
 * Didžiausio vartojimo kredito skaičiuoklė / MaxUsageCreditAction  ************
 ******************************************************************************/
function MaxUsageCreditTable() {
	this.validation = new SEBFinValidation();
	this.math = new SEBFinMath();
	this.disableString = "---";
	 
	this.calc = maxUsageCreditResultCalc;	
	this.disable = maxUsageCreditResultDisable;
}

function maxUsageCreditResultCalc() {
	var income = this.validation.getIntValue($("#muc_table #income"));
	var period = this.validation.getIntValue($("#muc_table #period"));
	var interest = this.validation.getFloatValue($("#muc_table #interest"));
	var cValue = this.validation.getFloatValue($("#muc_c_value"));
				
	if (income == null || period == null || interest == null) {
		return;
	}
		
	var incomeCoef = 0;
	if (income <= 900) {
		incomeCoef = 0.2999;
	} else if (income <= 3000) {
		incomeCoef = 0.3999;
	} else if (income <= 7000) {
		incomeCoef = 0.4999;
	} else {
		incomeCoef = 0.5999;
	}
		
	var restIncome = null;
	if (cValue != null && cValue > 0) {
		restIncome = income * incomeCoef - cValue;
	} else {
		restIncome = income * incomeCoef;
	}
		
	if (income < 600 || (restIncome != null && restIncome < 0)) {
		$("#muc_income_err").show();
		this.disable();
		return;
	} else {
		$("#muc_income_err").hide();
	}
		
	var maxCredit = restIncome * ((1 - 1 / Math.pow((1 + interest / 1200), 1 * period)) / (interest / 1200));
	var amount = Math.round(maxCredit);
	var compen = (period * 1 * restIncome - maxCredit) * 0.33;
		
	restIncome = this.math.roundFloat(restIncome, 2);
	maxCredit = this.math.roundFloat(maxCredit, 2);
	compen = this.math.roundFloat(compen, 2);

	$("#muc_res_table #return_payment").html(this.validation.floatValueStr(restIncome));
	$("#muc_res_table #max_credit_sum").html(this.validation.floatValueStr(maxCredit));	
	// $("#muc_res_table #return_compen").html(this.validation.floatValueStr(compen));

	$("#muc_request_amount").val(Math.round(amount));
	$("#muc_request_period").val(period);
}

function maxUsageCreditResultDisable() {
	$("#muc_res_table #max_credit_sum,#muc_res_table #return_payment," +
		"#muc_res_table #return_compen").html(this.disableString);
}

/*******************************************************************************
 * Pageidaujamo būsto kredito skaičiuoklė / ReqHomeCreditAction ****************
 ******************************************************************************/
function ReqHomeCreditTable() {
	this.validation = new SEBFinValidation();
	this.math = new SEBFinMath();
	this.disableString = "---";
	 
	this.calc = reqHomeCreditTableCalc;	
	this.disable = reqHomeCreditTableDisable;
}

function reqHomeCreditTableCalc() {
	var credit = this.validation.getIntValue($("#rhc_main_table #credit"));
	var period = this.validation.getIntValue($("#rhc_main_table #period"));
	var interest = this.validation.getFloatValue($("#rhc_main_table #interest"));

	if (credit == null || period == null || interest == null) {
		return;
	}

	var payment = credit / ((1 - 1 / Math.pow((1 + interest / 1200), 12 * period)) / (interest / 1200));
	var compen = (period * 12 * payment - credit) * 0.33;

	payment = this.math.roundFloat(payment, 2);
	compen = this.math.roundFloat(compen, 2);
		 
	$("#rhc_res_table #return_payment").html(this.validation.floatValueStr(payment));	
	// $("#rhc_res_table #return_compen").html(this.validation.floatValueStr(compen));

	$("#req_form #rhc_request_amount").val(credit);
	$("#req_form #rhc_request_period").val(period);
}

function reqHomeCreditTableDisable() {
	$("#rhc_res_table #return_payment,#rhc_res_table #return_compen").html(this.disableString);
}

/*******************************************************************************
 * Pageidaujamo vartojimo kredito skaičiuoklė / UsageCreditAction **************
 ******************************************************************************/
function ReqUsageCreditTable() {
	this.validation = new SEBFinValidation();
	this.math = new SEBFinMath();
	this.disableString = "---";
	 
	this.calc = reqUsageCreditTableCalc;	
	this.disable = reqUsageCreditTableDisable;
}

function reqUsageCreditTableCalc() {
	var credit = this.validation.getIntValue($("#ruc_main_table #credit"));
	var period = this.validation.getIntValue($("#ruc_main_table #period"));
	var interest = this.validation.getFloatValue($("#ruc_main_table #interest"));

	if (credit == null || period == null || interest == null) {
		return;
	}

	var payment = credit / ((1 - 1 / Math.pow((1 + interest / 1200), period)) / (interest / 1200));
	var compen = (period * payment - credit) * 0.33;

	payment = this.math.roundFloat(payment, 2);
	compen = this.math.roundFloat(compen, 2);
		 
	$("#ruc_res_table #return_payment").html(this.validation.floatValueStr(payment));	
	// $("#ruc_res_table #return_compen").html(this.validation.floatValueStr(compen));

	$("#req_form #ruc_request_amount").val(credit);
	$("#req_form #ruc_request_period").val(period);
}

function reqUsageCreditTableDisable() {
	$("#ruc_res_table #return_payment,#ruc_res_table #return_compen").html(this.disableString);
}




/*******************************************************************************
 * Gyvybės draudimo skaičiuoklė / LifeInsuranceCalculatorAction ****************
 ******************************************************************************/

function LifeInsuranceCalculator() {
	this.validation = new SEBFinValidation();
	this.math = new SEBFinMath();
	this.disableString = "---";
	
	this.processField = lifeInsCalcFild;
	this.validField = validFieldFun;

	this.calc = lifeInsCalcCalc;
	this.disable = lifeInsCalcDisable;
}

function validFieldFun(mainField, errField, emptyErrField, isValid) {
	if (!isValid) {
		if (this.validation.isEmpty(mainField)) {
			errField.hide();
			emptyErrField.show();
		} else {
			emptyErrField.hide();
			errField.show();
		}
		//mainField.css("border-color", "red");
		this.disable();
		return;
	}

	errField.hide();
	emptyErrField.hide();
	//mainField.css("border-color", "black");
	this.calc();
}

function lifeInsCalcFild(fieldId, validFieldRequired, required) {
	var checkBoxFlag = false;
	if (fieldId == "#lic_main_table td.field #ins_exist") {
		checkBoxFlag = true;
		fieldId = "#lic_main_table td.field #ins_exist_sum";
		if (!$("#lic_main_table td.field #ins_exist")[0].checked) {
			this.calc();
			return;
		}
	}
	var mainField = $(fieldId);
	var isValid = false;
	
	//if (fieldId == "#lic_main_table td.field #need_per") {
//		isValid = this.validation.isInt(mainField) && this.validation.isIntInRange(mainField, 0, Math.pow(2, 63) - 1);
	//} else {
		isValid = this.validation.isInt(mainField) && this.validation.isIntInRange(mainField, 0, Math.pow(2, 63) - 1);	
	//}
	
	if (checkBoxFlag) {
		isValid = isValid || this.validation.isEmpty(mainField);
		this.disable();
	}

	if (validFieldRequired) {
		var errField = $(fieldId + "_err");
		var emptyErrField =	$(fieldId + "_err_empty");
		
		this.validField(mainField, errField, emptyErrField, isValid);
	}
	return isValid;
}

function lifeInsCalcCalc() {
	var validForm = true;
	var thisObj = this;
	$("#lic_main_table td.field input").each(function() {
		var id = $(this)[0].id;
		
		if (id == "ins_exist_sum" && $("#ins_exist")[0].checked) {
			validForm = validForm && thisObj.processField("#lic_main_table td.field #" + id, false, true);
		} else if (id != "ins_exist" && id != "ins_exist_sum") {
			validForm = validForm && thisObj.processField("#lic_main_table td.field #" + id, false, true);
		}  
	});
	
	if (!validForm) {
		this.disable();
		return;
	}

	var salary = this.validation.getIntValue($("#lic_main_table #salary"));					// A
	var savings = this.validation.getIntValue($("#lic_main_table #savings"));				// B
	var bank_loan = this.validation.getIntValue($("#lic_main_table #bank_loan"));			// C
	var other_loan = this.validation.getIntValue($("#lic_main_table #other_loan"));			// D
	var need_per = this.validation.getIntValue($("#lic_main_table #need_per"));				// E
	var need_year = this.validation.getIntValue($("#lic_main_table #need_year"));			// F
	var need_college = this.validation.getIntValue($("#lic_main_table #need_college"));		// G
	var need_funeral = this.validation.getIntValue($("#lic_main_table #need_funeral"));		// H
	var need_other = this.validation.getIntValue($("#lic_main_table #need_other"));			// I
	var ins_exist_sum = this.validation.getIntValue($("#lic_main_table #ins_exist_sum")); 	// J	

    var result = need_per * need_year - ins_exist_sum - savings
    	+ bank_loan + other_loan + need_college + need_funeral + need_other;
	
	if (result < 0) {
		result = 0;
	}
	
	if (result < 0 || isNaN(result)) {
		this.disable();
		$("#lic_table_err").show();
	} else {
		result = this.math.roundFloat(result, 2);
   		$("#offer_ins").html(this.validation.floatValueStr(result));
   		$("#lic_table_err").hide();
	}	
}

function lifeInsCalcDisable() {
	$("#lic_res_table #offer_ins").html(this.disableString);
}


/*******************************************************************************
 * Paraiška kreditui gauti / RequestCreditAction *******************************
 ******************************************************************************/

function RequestCredit() {
	this.validation = new SEBFinValidation();
	this.validateField = validateFieldRCFFun;
	this.validField = validFieldRCFFun;
	this.formValidation = new SEBFinFormValidation("_err", "_err_empty");

	this.onlyAlphabet = onlyAlphabetFun;
	this.personCodeField =personCodeFieldFun;
	this.phoneField = phoneFieldFun; 
	this.emailField = emailFieldFun;
	this.noEmptyField = noEmptyFieldFun;
	this.intRangeField = intRangeFieldFun;
	this.doubleField = doubleFieldFun;
	
	this.spouseFieldVisibility = spouseFieldVisibilityFun;
	this.spouseWorkField = spouseWorkFieldFun;
	this.answerTypeField = answerTypeFieldFun;
	this.creditCity = creditCityFun;
	this.creditPurpose = creditPurposeFun; 
	this.submit = submitRCFFun;
}

function validateFieldRCFFun(fieldId, validFieldRequired, required, isValid) {
	var mainField = $(fieldId);

	if (!required) {
		isValid = isValid || this.validation.isEmpty(mainField);
	}

	if (validFieldRequired) {
		var errField = $(fieldId + "_err");
		var emptyErrField =	$(fieldId + "_err_empty");

		this.validField(mainField, errField, emptyErrField, isValid);
	}

	return isValid;
}

function validFieldRCFFun(mainField, errField, emptyErrField, isValid) {
	if (!isValid) {
		if (this.validation.isEmpty(mainField)) {
			errField.hide();
			emptyErrField.show();
		} else {
			emptyErrField.hide();
			errField.show();
		}
		return;
	}
	errField.hide();
	emptyErrField.hide();
}

function onlyAlphabetFun(fieldId, validFieldRequired, required) {
	var isValid = this.validation.isAlphabet($(fieldId));
	return this.validateField(fieldId, validFieldRequired, required, isValid);
}

function personCodeFieldFun(fieldId, validFieldRequired, required) {
	var isValid = this.validation.isValidPersonCode($(fieldId));	
	return this.validateField(fieldId, validFieldRequired, required, isValid);
}

function phoneFieldFun(fieldId, validFieldRequired, required) {
	var isValid = this.validation.isValidPhone($(fieldId));	
	return this.validateField(fieldId, validFieldRequired, required, isValid);
}

function emailFieldFun(fieldId, validFieldRequired, required) {
	var isValid = this.validation.isValidEmail($(fieldId));	
	return this.validateField(fieldId, validFieldRequired, required, isValid);
}

function noEmptyFieldFun(fieldId, validFieldRequired, required) {	
	var isValid = !this.validation.isEmpty($(fieldId));
	this.validateField(fieldId, validFieldRequired, required, false);
	return isValid;
}

function intRangeFieldFun(fieldId, validFieldRequired, required, from, till) {
	var isValid = this.validation.isIntInRange($(fieldId), from, till);	
	return this.validateField(fieldId, validFieldRequired, required, isValid);
}

function doubleFieldFun(fieldId, validFieldRequired, required) {
	var isValid = this.validation.isFloat($(fieldId));	
	return this.validateField(fieldId, validFieldRequired, required, isValid);
}

function spouseFieldVisibilityFun(field) {
	var spouseHeader = $("#rcf_main_table #spouse_start_tr");
	var spouseFields = spouseHeader.nextAll().slice(0, 9);
	var visible = false;

	if (field.val() == "2" || field.val() == "3") {
		visible = true;
	}

	if (visible) {
		spouseHeader.show();
		spouseFields.show();
	} else {
		spouseHeader.hide();
		spouseFields.hide();	
	}
}

function spouseWorkFieldFun(field) {
	if (field.val() == 1) {
		$("#rcf_main_table #s_position,#rcf_main_table #s_work_place").parent().parent().show();
	} else {
		$("#rcf_main_table #s_position,#rcf_main_table #s_work_place").parent().parent().hide();
	}
}

function answerTypeFieldFun(field) {
	if (field.val() == "2") {
		$("#email_label").css("font-weight", "bold");
	} else {
		$("#email_label").css("font-weight", "normal");
	}
}

function creditCityFun(field)  {
	$("#credit_branch").removeOption(/./);
	$("#credit_branch").addOption(cities[field[0].value], false);
}

function creditPurposeFun(field) {
	if (field[0].selectedIndex == 3) {
		$("#credit_amount_curr").attr("disabled", true);
		$("input[name=return_type]").attr("disabled", true);
		$("#aside_term").val("1");
		$("#aside_term").parent().parent().hide();
		$("#hostage").attr("disabled", true).val("");
		$("#hostage_info").attr("disabled", true).val("");
		
	} else {
		$("#credit_amount_curr").attr("disabled", false);
		$("input[name=return_type]").attr("disabled", false);
		if (!submitedForm) {
			$("#aside_term").val("");
		}
		$("#aside_term").parent().parent().show();
		$("#hostage").attr("disabled", false);
		$("#hostage_info").attr("disabled", false);
	}

	if (field[0].selectedIndex == 2) {
		$("#hostage").attr("disabled", true).val("");
		$("#hostage_info").attr("disabled", true).val("");
		$("input[name=return_type][value=1]").hide();
		$("input[name=return_type][value=2]").attr("checked","checked");
		$("#return_type1").hide();
		$("#credit_amount_curr").removeOption(/./);
		$("#credit_amount_curr").addOption(currency1, false);
		$("#aside_term").val("1");
		$("#aside_term").parent().parent().hide();
		this.formValidation.hideErrors($("#aside_term"));		
    } else if (field[0].selectedIndex != 3) {
		$("#hostage").attr("disabled", false);
		$("#hostage_info").attr("disabled", false);
		$("input[name=return_type][value=1]").show();
		$("#return_type1").show();		
		$("#credit_amount_curr").addOption(currency2, false);
		$("#aside_term").parent().parent().show();
    }

	$("#credit_purpose_type").removeOption(/./);
	$("#credit_purpose_type").addOption(purposes[field[0].value]);
	$("#credit_purpose_type")[0].selectedIndex = 0;
	$("#credit_purpose_type").selectOptions(savedCreditPurposeTypeOpt);
}

function submitRCFFun() {
	var validForm = true;
	
	validForm = this.onlyAlphabet("#first_name", true, true) && validForm;
	validForm = this.onlyAlphabet("#last_name", true, true) && validForm;
	validForm = this.personCodeField("#person_code", true, true) && validForm;
	validForm = this.phoneField("#phone", true, true) && validForm;
	validForm = this.noEmptyField("#position", true, true) && validForm;
	validForm = this.noEmptyField("#work_place", true, true) && validForm;	
	if ($("input[name=answer_type]")[1].checked) {
		validForm = this.emailField("#email", true, true) && validForm;
	} else {
		validForm = this.emailField("#email", true, false) && validForm;
	}
	
	if ($("#rcf_main_table #material_status").val() == "2" || $("#rcf_main_table #material_status").val() == "3") {
		validForm = this.onlyAlphabet("#s_first_name", true, true) && validForm;
		validForm = this.onlyAlphabet("#s_last_name", true, true) && validForm;
		validForm = this.personCodeField("#s_person_code", true, true) && validForm;
		validForm = this.phoneField("#s_phone", true, true) && validForm;
		validForm = this.emailField("#s_email", true, false) && validForm;

		if($("input[name=s_works]")[0].checked) {
			validForm = this.noEmptyField("#s_position", true, true) && validForm;
			validForm = this.noEmptyField("#s_work_place", true, true) && validForm;
		}
	}
	
	validForm = this.intRangeField("#member_count", true, true, 1, 99) && validForm;
	validForm = this.doubleField("#family_income", true, true) && validForm;
	validForm = this.doubleField("#fin_obligation", true, true) && validForm;	
	validForm = this.doubleField("#credit_amount", true, true) && validForm;
	validForm = this.intRangeField("#credit_period", true, true, 1, 40) && validForm;
	validForm = this.intRangeField("#credit_period_month", true, true, 0, 12) && validForm;

	var hostageValidationRequired = true;
	if ($("#rcf_main_table #credit_purpose")[0].selectedIndex == 3) {
		validForm = this.intRangeField("#aside_term", true, false, 1, 36) && validForm;
		hostageValidationRequired = false;
	} else {
		validForm = this.intRangeField("#aside_term", true, true, 1, 36) && validForm;
		hostageValidationRequired = true;
	}

	if ($("#rcf_main_table #credit_purpose")[0].selectedIndex == 2) {
		hostageValidationRequired = false;
    } else if ($("#rcf_main_table #credit_purpose")[0].selectedIndex != 3) {
    	hostageValidationRequired = true;
    }

	validForm = this.doubleField("#hostage", true, hostageValidationRequired) && validForm;
	return validForm;
}


/*******************************************************************************
 * Finansinio lizingo skaičiuoklė / LeasingFinCalculatorAction ****************
 ******************************************************************************/
function LeasingFinCalculator() {
	this.validation = new SEBFinValidation();
	this.formValidation = new SEBFinFormValidation("_err", "_err_empty");
	this.math = new SEBFinMath();	

	this.calcPayment = calcPaymentFun;
	this.checkIncludeMonthly = checkIncludeMonthlyFun;
	this.checkType = checkTypeFun;
	this.submit = submitFlcFormFun;
}

function calcPaymentFun(usingPerc) {
	if (this.validation.isEmpty($("#price"))) {
		return;	
	}

	var price = this.validation.getFloatValue($("#price"));
	var paymentLt = this.validation.getFloatValue($("#paymentLt"));
	var paymentPerc = this.validation.getFloatValue($("#paymentPerc"));

	if (price == null || (usingPerc && paymentPerc == null) || (!usingPerc && paymentLt == null)) {
		return;	
	}

	if (usingPerc) {
		var lt = this.math.roundFloat(price * paymentPerc / 100, 2);
		this.validation.setFloatValue($("#paymentLt"), lt);
		this.formValidation.hideErrors($("#paymentLt"));
	} else {
		var perc = this.math.roundFloat(100 * paymentLt / price, 2);
		this.validation.setFloatValue($("#paymentPerc"), perc);
		this.formValidation.hideErrors($("#paymentPerc"));
	}
}

function checkIncludeMonthlyFun() {
	if ($("#include_monthly")[0].checked) {
		$("#extra_fee,#car_reg_date,#car_reg").parent().parent().show();
	} else {
		$("#extra_fee,#car_reg_date,#car_reg").parent().parent().hide();
	}
}

function checkTypeFun() {
	if ($("#type")[0].selectedIndex == 0) {
		$("#flc_main_table #include_monthly").parent().parent().show();
		if ($("#flc_main_table #include_monthly").attr("checked")) {
			$("#extra_fee,#car_reg_date,#car_reg").parent().parent().show();			
		}
	} else {
		$("#extra_fee").attr("selectedIndex", 0).parent().parent().hide();
		$("#car_reg_date,#car_reg").parent().parent().hide();
		$("#flc_main_table #include_monthly").attr("checked", false).parent().parent().hide();
	}
}

function submitFlcFormFun() {
	var isValid = true;

	isValid = this.formValidation.isFloatField($("#price"), true) && isValid; 
	isValid = this.formValidation.isFloatField($("#paymentLt"), true) && isValid;
	isValid = this.formValidation.isFloatInRangeField($("#paymentPerc"), 0, 99, false) && isValid;
	isValid = this.formValidation.isIntInRangeField($("#period"), 1, 240, true) && isValid;
	isValid = this.formValidation.isFloatInRangeField($("#year_interest"), 1, 20, true) && isValid;
	if ($("#include_monthly")[0].checked) {
		isValid = this.formValidation.isFloatInRangeField($("#extra_fee"), 0, 100, true) && isValid;
	}

	return isValid;
}

/*******************************************************************************
 * Main area *******************************************************************
 ******************************************************************************/

$(function() {
	var validation = new SEBFinFormValidation("_err", "_err_empty");

	////////////////////////////////////////////////////////////////////////////
	// Didžiausio būsto kredito skaičiuoklė / MaxHomeCreditAction     //////////
	////////////////////////////////////////////////////////////////////////////
	$("#mhc_table input[type=text]").change(function() {
		var thisCalc = new MaxHomeCreditTable();
		var fieldId = $(this)[0].id;
		
		if (fieldId == "income") {
			validation.isIntInRangeField($(this), 0, Number.MAX_VALUE, true);
		} else if (fieldId == "period") {
			validation.isIntInRangeField($(this), 1, 40, true);
		} else if (fieldId == "interest") {
			validation.isFloatInRangeField($(this), 0, Number.MAX_VALUE, true);
		}
		
		var isValid = $("#mhc_table .errorDiv:visible").length > 0 ? false : true;
		if (isValid) {
			thisCalc.calc();	
		} else {
			thisCalc.disable();
		}
	});
    $("#mhc_credit_send").click(function() {
		$("#req_form").submit();
		return false;
    });
    
	////////////////////////////////////////////////////////////////////////////
	// Didžiausio vartojimo kredito skaičiuoklė / MaxUsageCreditAction     /////
	////////////////////////////////////////////////////////////////////////////
	$("#muc_table input[type=text]").change(function() {
		var thisCalc = new MaxUsageCreditTable();
		var fieldId = $(this)[0].id;
		
		if (fieldId == "income") {
			validation.isIntInRangeField($(this), 0, Number.MAX_VALUE, true);
		} else if (fieldId == "period") {
			validation.isIntInRangeField($(this), 4, 60, true);
		} else if (fieldId == "interest") {
			validation.isFloatInRangeField($(this), 0, Number.MAX_VALUE, true);
		}
		
		var isValid = $("#muc_table .errorDiv:visible").length > 0 ? false : true;
		if (isValid) {
			thisCalc.calc();	
		} else {
			thisCalc.disable();
		}
	});
    $("#muc_credit_send").click(function() {
		$("#req_form").submit();
		return false;
    });	
	
	////////////////////////////////////////////////////////////////////////////
	// Pageidaujamo būsto kredito skaičiuoklė / ReqHomeCreditAction     ////////
	////////////////////////////////////////////////////////////////////////////
	$("#rhc_main_table input[type=text]").change(function() {
		var thisCalc = new ReqHomeCreditTable();
		var fieldId = $(this)[0].id;
		
		if (fieldId == "credit") {
			validation.isIntInRangeField($(this), 0, Number.MAX_VALUE, true);
		} else if (fieldId == "period") {
			validation.isIntInRangeField($(this), 1, 40, true);
		} else if (fieldId == "interest") {
			validation.isFloatInRangeField($(this), 0, Number.MAX_VALUE, true);
		}
		
		var isValid = $("#rhc_main_table .errorDiv:visible").length > 0 ? false : true;
		if (isValid) {
			thisCalc.calc();	
		} else {
			thisCalc.disable();
		}
	});
    $("#rhc_credit_send").click(function() {
		$("#req_form").submit();
		return false;
    });
    
	////////////////////////////////////////////////////////////////////////////
	// Pageidaujamo vartojimo kredito skaičiuoklė / UsageCreditAction     //////
	////////////////////////////////////////////////////////////////////////////
	$("#ruc_main_table input[type=text]").change(function() {
		var thisCalc = new ReqUsageCreditTable();
		var fieldId = $(this)[0].id;
		
		if (fieldId == "credit") {
			validation.isIntInRangeField($(this), 1000, 80000, true);
		} else if (fieldId == "period") {
			validation.isIntInRangeField($(this), 4, 60, true);
		} else if (fieldId == "interest") {
			validation.isFloatInRangeField($(this), 0, Number.MAX_VALUE, true);
		}
		
		var isValid = $("#ruc_main_table .errorDiv:visible").length > 0 ? false : true;
		if (isValid) {
			thisCalc.calc();	
		} else {
			thisCalc.disable();
		}
	});
    $("#ruc_credit_send").click(function() {
		$("#req_form").submit();
		return false;
    });
    
    // Gyvybės draudimo skaičiuoklė / LifeInsuranceCalculatorAction
    $("#lic_main_table td.field input").keyup(function(event) {
    	if (event.keyCode == 9) {
    		return;
    	}
    	var thisCalc = new LifeInsuranceCalculator();
    	thisCalc.processField("#lic_main_table td.field #" + $(this)[0].id, true, true);
    });
    $("#lic_main_table #ins_exists_sum_tr").hide();
    $("#lic_main_table #ins_exist").click(function() {
    	if ($("#lic_main_table #ins_exists_sum_tr:visible").length == 1) {
    		$("#lic_main_table #ins_exists_sum_tr").hide();
    		$("#lic_main_table #ins_exist_sum").val(0);
    		var thisCalc = new LifeInsuranceCalculator();
    		thisCalc.processField("#lic_main_table td.field #ins_exist_sum", true, true);    		
    	} else {
    		$("#lic_main_table #ins_exists_sum_tr").show();
    	}
    });

	// Paraiška kreditui gauti / RequestCreditAction
	if ($("#rcf_main_table").length != 0) {
		new RequestCredit().spouseFieldVisibility($("#rcf_main_table #material_status"));
		new RequestCredit().spouseWorkField($("#rcf_main_table #s_works"));
		new RequestCredit().creditCity($("#rcf_main_table #credit_city"));
		new RequestCredit().creditPurpose($("#rcf_main_table #credit_purpose"));

		$("#rcf_main_table #credit_branch").selectOptions(creditBranchSelected);
		if (submitedForm) {
			new RequestCredit().submit();
		}
	}

	$("#rcf_main_table #first_name,#rcf_main_table #last_name," +
		"#rcf_main_table #s_first_name,#rcf_main_table #s_last_name").change(function() {
		var thisForm = new RequestCredit();
		thisForm.onlyAlphabet("#rcf_main_table #" + $(this)[0].id, true, true);
	});

	$("#rcf_main_table #person_code,#rcf_main_table #s_person_code").change(function() {
		var thisForm = new RequestCredit();
		thisForm.personCodeField("#rcf_main_table #" + $(this)[0].id, true, true);
	});

	$("#rcf_main_table #phone,#rcf_main_table #s_phone").change(function() {
		var thisForm = new RequestCredit();
		thisForm.phoneField("#rcf_main_table #" + $(this)[0].id, true, true);
	});

	$("#rcf_main_table #email,#rcf_main_table #s_email").change(function() {
		var thisForm = new RequestCredit();
		thisForm.emailField("#rcf_main_table #" + $(this)[0].id, true, false);
	});

	$("#rcf_main_table #position,#rcf_main_table #work_place," +
			"#rcf_main_table #s_position,#rcf_main_table #s_work_place").change(function() {
		var thisForm = new RequestCredit();
		thisForm.noEmptyField("#rcf_main_table #" + $(this)[0].id, true, true);
	});

	$("#rcf_main_table #member_count").change(function() {
		var thisForm = new RequestCredit();
		thisForm.intRangeField("#rcf_main_table #" + $(this)[0].id, true, true, 1, 99);
	});

	$("#rcf_main_table #credit_period").change(function() {
		var thisForm = new RequestCredit();
		thisForm.intRangeField("#rcf_main_table #" + $(this)[0].id, true, true, 1, 40);
	});
	
	$("#rcf_main_table #credit_period_month").change(function() {
		var thisForm = new RequestCredit();
		thisForm.intRangeField("#rcf_main_table #" + $(this)[0].id, true, true, 0, 12);
	});	
	
	$("#rcf_main_table #aside_term").change(function() {
		var thisForm = new RequestCredit();
		thisForm.intRangeField("#rcf_main_table #" + $(this)[0].id, true, true, 1, 36);
	});	

	$("#rcf_main_table #family_income,#rcf_main_table #fin_obligation," +
		"#rcf_main_table #credit_amount,#rcf_main_table #hostage").change(function() {
		var thisForm = new RequestCredit();
		thisForm.doubleField("#rcf_main_table #" + $(this)[0].id, true, true);
	});
	
	$("#rcf_main_table #material_status").change(function() {
		var thisForm = new RequestCredit();
		thisForm.spouseFieldVisibility($(this));
	});

	$("#rcf_main_table input[name=s_works]").click(function() {
		var thisForm = new RequestCredit();
		thisForm.spouseWorkField($(this));
	});
	
	$("#rcf_main_table input[name=answer_type]").click(function() {
		var thisForm = new RequestCredit();
		thisForm.answerTypeField($(this));
	});

	$("#req_credit_form").submit(function() {
		var validForm = new RequestCredit().submit();
		return validForm;
	});
	
	$("#rcf_main_table #credit_purpose").change(function() {
		var thisForm = new RequestCredit();
		thisForm.creditPurpose($(this));
	});

	$("#rcf_main_table #credit_city").change(function() {
		var thisForm = new RequestCredit();
		thisForm.creditCity($(this));		
	});

	// Susisiekite su mumis / ClientMailMessageAction
	$("#cms_table #email").attr("disabled", true);
	
	$("#cms_table #full_name").change(function() {
		validation.isAlphabetField($(this), false);
	});
	$("#cms_table #email").change(function() {
		validation.isEmailField($(this), true);
	});
	$("#cms_table #phone").change(function() {
		validation.isPhoneField($(this), true);
	});
	$("#cms_table #message").change(function() {
		validation.isNotEmptyField($(this));
	});	
	
	$("#cms_table input[name=answer_type]").change(function() {
		var selected = $("input[name=answer_type]:checked").val();
		if (selected == "2") {
			$("#phone").attr("disabled", false);
			$("#email").attr("disabled", true);
			validation.hideErrors($("#email"));
		} else if (selected == "1") {
			$("#phone").attr("disabled", true);
			$("#email").attr("disabled", false);
			validation.hideErrors($("#phone"));
		} else {
			$("#phone").attr("disabled", true);
			$("#email").attr("disabled", true);
			validation.hideErrors($("#email"));
			validation.hideErrors($("#phone"));			
		}
	});
	$("#cms_form").submit(function() {
		var validForm = validation.isAlphabetField($("#cms_table #full_name"), false);
		validForm = validation.isNotEmptyField($("#cms_table #message")) && validForm;
		
		var selected = $("input[name=answer_type]:checked").val();
		if (selected == "2") {
			validForm = validation.isPhoneField($("#cms_table #phone"), true) && validForm;
		} else if (selected == "1") {
			validForm = validation.isEmailField($("#cms_table #email"), true) && validForm;
		}
		return validForm;
	});


	// Finansinio lizingo skaičiuoklė / LeasingFinCalculatorAction
	$("#flc_main_table #include_monthly").click(function() {
		if ($(this)[0].checked) {
			$("#extra_fee,#car_reg_date,#car_reg").parent().parent().show();
		} else {
			$("#extra_fee,#car_reg_date,#car_reg").parent().parent().hide();
		}
	});
	
	$("#flc_main_table #type").change(function() {
		if ($(this)[0].selectedIndex == 0) {
			$("#flc_main_table #include_monthly").parent().parent().show();
			if ($("#flc_main_table #include_monthly").attr("checked")) {
				$("#extra_fee,#car_reg_date,#car_reg").parent().parent().show();		
			}
		} else {
			$("#extra_fee").attr("selectedIndex", 0).parent().parent().hide();
			$("#car_reg_date,#car_reg").parent().parent().hide();
			$("#flc_main_table #include_monthly").attr("checked", false).parent().parent().hide();
		}
	});
	
	$("#flc_main_table")
		.find("#price").change(function() {
			validation.isFloatField($(this), true);	
		}).end()
		.find("#paymentLt").change(function() {
			var thisForm = new LeasingFinCalculator();
			var isValid = validation.isFloatField($(this), true);
			if (isValid) {
				thisForm.calcPayment(false);		
			}
		}).end()		 
		.find("#paymentPerc").change(function() {
			var thisForm = new LeasingFinCalculator();
			var isValid = validation.isFloatInRangeField($(this), 0, 99, false);
			if (isValid) {
				thisForm.calcPayment(true);		
			}
		}).end()	
		.find("#period").change(function() {
			validation.isIntInRangeField($(this), 1, 240, true);
		}).end()
		.find("#year_interest").change(function() {
			validation.isFloatInRangeField($(this), 1, 20, true);
		}).end()
		.find("#extra_fee").change(function() {
			validation.isFloatInRangeField($(this), 0, 100, false);
		});		

	$("#flc_form").submit(function() {
		var thisForm = new LeasingFinCalculator();
		var isValid = thisForm.submit();
		return isValid;
	});
	
	if ($("#flc_main_table").length != 0) {
		new LeasingFinCalculator().checkType();
		new LeasingFinCalculator().checkIncludeMonthly();
	}
	
	////////////////////////////////////////////////////////////////////////////
	// Valiutų skaičiuoklė / CurrencyCount     /////////////////////////////////
	////////////////////////////////////////////////////////////////////////////
	$("#cc_table")
		.find("#sum").change(function() {
			if (!validation.isFloatInRangeField($(this), 0, Number.MAX_VALUE, true)) {
				$("#cc_cash,#cc_non_cash").html("&mdash;")
			}
		}).end()
		.find("#output").change(function() {
			if (!validation.isIntInRangeField($(this), 0, Number.MAX_VALUE, true)) {
				$("#cc_cash,#cc_non_cash").html("&mdash;")
			}
		}).end()
		.find("[name=answer]").click(function() {
			checkCurrCountFieldText();	
		});
	$("#currc_form input[name=submit]").click(function() {
		var isValid = validation.isFloatInRangeField($("#cc_table #sum"), 0, Number.MAX_VALUE, true);
		isValid = validation.isIntInRangeField($("#cc_table #output"), 0, Number.MAX_VALUE, true) && isValid;
		
		if (!isValid || $("#output").val() == "-1") {
			$("#cc_cash,#cc_non_cash").html("&mdash;");	
			return false;
		}
		currencyCalc();
		return false;
	});
	function currencyCalc() {
		var source = $("#source").val()
		var output = $("#output").val();
		var sum = parseFloat($("#sum").val().replace(",", "."));
		var answer = $("input[name=answer]")[0].checked ? 0 : 1;

		var success = false;
		if (sum != null && (answer == null || answer == 0)) {
			success = calcBuy(source, output, sum);
		}
		else if (sum != null) {
			success = calcSell(output, source, sum);
		}
		if (success) {
			$("#currCountErr").hide();
		} else {
			$("#currCountErr").show();
			$("#cc_cash,#cc_non_cash").html("&mdash;");
		}
	}

	function calcSell(sourceParam, outputParam, sum) {
		var nationalCurrencyId = $("#source")[0].options[0].value;
		var nonCashBuy = sum;
		var cashBuy = sum;

		if (sourceParam != nationalCurrencyId) {
			if (checkBadCurrCountParams(nonCash[sourceParam].buy) || checkBadCurrCountParams(nonCash[sourceParam].quantity)
				|| checkBadCurrCountParams(cash[sourceParam].buy) || checkBadCurrCountParams(cash[sourceParam].quantity)) {
				return false;
			}
			var nonCashSell = priceInNational(nonCash[sourceParam].buy, nonCash[sourceParam].quantity);
			nonCashBuy = nonCashBuy * nonCashSell;
			var cashSell = priceInNational(cash[sourceParam].buy, cash[sourceParam].quantity);
			cashBuy = cashBuy * cashSell;
		}

		if (outputParam != nationalCurrencyId) {
			if (checkBadCurrCountParams(nonCash[outputParam].sell) || checkBadCurrCountParams(nonCash[outputParam].quantity)
				|| checkBadCurrCountParams(cash[outputParam].sell) || checkBadCurrCountParams(cash[outputParam].quantity)) {
				return false;
			}
			nonCashBuy = nonCashBuy / priceInNational(nonCash[outputParam].sell, nonCash[outputParam].quantity);
			cashBuy = cashBuy / priceInNational(cash[outputParam].sell, cash[outputParam].quantity);
		}

		setCurrCountRez(cashBuy, nonCashBuy);
		return true;
	}

	function checkBadCurrCountParams(param) {
		if (isNaN(param) || param == 0 || param == null) {
			return true;
		}
		return false;
	}

	function priceInNational(currPrice, quantity) {
		var price = currPrice;
		if (quantity != null && quantity != 1 && quantity != 0) {
			price = 1.0 * currPrice / quantity;	// scale 10 
		}
		return price;
	}


	function calcBuy(sourceParam, outputParam, sum) {
		var nationalCurrencyId = $("#source")[0].options[0].value;
		var nonCashBuy = sum;
		var cashBuy = sum;

		if (outputParam != nationalCurrencyId) {
			if (checkBadCurrCountParams(nonCash[outputParam].sell) || checkBadCurrCountParams(nonCash[outputParam].quantity)
				|| checkBadCurrCountParams(cash[outputParam].sell) || checkBadCurrCountParams(cash[outputParam].quantity)) {
				return false;
			}
			nonCashBuy = nonCashBuy * priceInNational(nonCash[outputParam].sell, nonCash[outputParam].quantity);
			cashBuy = cashBuy * priceInNational(cash[outputParam].sell, cash[outputParam].quantity);
		}

		if (sourceParam != nationalCurrencyId) {
			if (checkBadCurrCountParams(nonCash[sourceParam].buy) || checkBadCurrCountParams(nonCash[sourceParam].quantity)
				|| checkBadCurrCountParams(cash[sourceParam].buy) || checkBadCurrCountParams(cash[sourceParam].quantity)) {
				return false;
			}
			var nonCashSell = priceInNational(nonCash[sourceParam].buy, nonCash[sourceParam].quantity);
			nonCashBuy = nonCashBuy / nonCashSell;
			var cashSell = priceInNational(cash[sourceParam].buy, cash[sourceParam].quantity);
			cashBuy = cashBuy / cashSell;
		}

		setCurrCountRez(cashBuy, nonCashBuy);
		return true;
	}

	function setCurrCountRez(cashParam, nonCashParam) {
		var valid = new SEBFinValidation();
		var math = new SEBFinMath();

		cashParam = math.roundFloat(cashParam, 2);
		nonCashParam = math.roundFloat(nonCashParam, 2);

		var cash = valid.floatStr(cashParam, 2);
		var nonCash = valid.floatStr(nonCashParam, 2);

		$("#cc_cash").html(cash);
		$("#cc_non_cash").html(nonCash);

	}

	function checkCurrCountFieldText() {
		if ($("#cc_table input[name=answer]")[1] != null && $("#cc_table input[name=answer]")[1].checked) {
			$("#cc_table #sell_sum,#cc_table #sell_output,#cc_table #sell_source").show();
			$("#cc_table #buy_sum,#cc_table #buy_output,#cc_table #buy_source").hide();
		} else {
			$("#cc_table #sell_sum,#cc_table #sell_output,#cc_table #sell_source").hide();
			$("#cc_table #buy_sum,#cc_table #buy_output,#cc_table #buy_source").show();		
		}
		$("#cc_cash,#cc_non_cash").html("&mdash;");
	}
	checkCurrCountFieldText();
	
	////////////////////////////////////////////////////////////////////////////
	// Trečios pakopos pensijų fondų skaičiuoklė / PensionFundCalculatorAction
	////////////////////////////////////////////////////////////////////////////
	$("#pfc_table")
		.find("#contribution,#income33").change(function() {
			validation.isIntInRangeField($(this), 1, Number.MAX_VALUE, true);
		}).end()
		.find("#income15").change(function() {
			validation.isIntInRangeField($(this), 0, Number.MAX_VALUE, true);
		}).end()
		.find("#returnValue").change(function() {
			validation.isIntInRangeField($(this), -4, 15, true);
		}).end()
		.find("#commissions").change(function() {
			validation.isIntInRangeField($(this), 0, 3, true);
		});
	$("#pfc_form").submit(function() {
		var isValid = $("#pfc_form .errorDiv:visible").length > 0 ? false : true;
		isValid = validation.isIntInRangeField($("#pfc_table #contribution"), 1, Number.MAX_VALUE, true) && isValid;
		isValid = validation.isIntInRangeField($("#pfc_table #income33"), 1, Number.MAX_VALUE, true) && isValid;
		isValid = validation.isIntInRangeField($("#pfc_table #income15"), 0, Number.MAX_VALUE, true) && isValid;
		isValid = validation.isIntInRangeField($("#pfc_table #returnValue"), -4, 15, true) && isValid;
		isValid = validation.isIntInRangeField($("#pfc_table #commissions"), 0, 3, true) && isValid;
		return isValid;
	});
	
	////////////////////////////////////////////////////////////////////////////
	// Rizikos skaičiuoklė / RiskCalculatorAction     //////////////////////////
	////////////////////////////////////////////////////////////////////////////
	$("#rc_table input[type=radio]").click(function() {
		var fieldName = $(this)[0].name;
		validation.isSelectedRadio(fieldName, true);
	});
	var riskCalculatorPrev = false;
	$("#rc_form input[name=submitPrev]").click(function() {
		riskCalculatorPrev = true;
	});
	$("#rc_form").submit(function() {
		if (riskCalculatorPrev) {
			return true;
		}
		var isValid = validation.isSelectedRadio($("#rc_table input[type=radio]")[0].name, true);
		return isValid;
	});

	////////////////////////////////////////////////////////////////////////////
	// Investicijų skaičiuoklė / InvestCalculatorAction     ////////////////////
	////////////////////////////////////////////////////////////////////////////
	// step 1
	$("#ic_table_1 select[name=goal]").change(function() {
		validation.isSelectedValue($(this), 1, true);
	});
	$("#ic_table_1 input[type=radio][name=iway],#ic_table_1 input[type=radio][name=payw]").click(function() {
		var fieldName = $(this)[0].name;
		validation.isSelectedRadio(fieldName, true);
	});
	$("#ic_form_1").submit(function() {
		var isValid = validation.isSelectedRadio($("#ic_table_1 input[type=radio][name=payw]")[0].name, true);
		isValid = validation.isSelectedRadio($("#ic_table_1 input[type=radio][name=iway]")[0].name, true) && isValid;
		isValid = validation.isSelectedValue($("#ic_table_1 select[name=goal]"), 1, true) && isValid;
		return isValid;
	});

	// step 2
	$("#ic_table_2 input[type=radio][name=calc]").click(function() {
		var fieldName = $(this)[0].name;
		validation.isSelectedRadio(fieldName, true);
	});
	var investCalculator2Prev = false;
	$("#ic_form_2 input[name=submitback]").click(function() {
		investCalculator2Prev = true;
	});
 	$("#ic_form_2").submit(function() {
		if (investCalculator2Prev) {
			return true;
		}
 		var isValid = validation.isSelectedRadio($("#ic_table_2 input[type=radio][name=calc]")[0].name, true);
		return isValid;
	});

	// step 3
	$("#ic_form_3 input[type!=hidden][name=pay]").change(function() {
		validation.isFloatInRangeField($(this), 1, Number.MAX_VALUE, true);
	});
	$("#ic_form_3 input[type!=hidden][name=pimsu]").change(function() {
		validation.isFloatInRangeField($(this), 0, Number.MAX_VALUE, true);
	});
	$("#ic_form_3 input[type!=hidden][name=rpay]").change(function() {
		validation.isFloatInRangeField($(this), 0.0000000001, Number.MAX_VALUE, true);
	});	
	
	var investCalculator3Prev = false;
	$("#ic_form_3 input[name=submitback]").click(function() {
		investCalculator3Prev = true;
	});
 	$("#ic_form_3").submit(function() {
		if (investCalculator3Prev) {
			return true;
		}
		var isValid = true;
		if ($("#ic_form_3 input[name=pay][type!=hidden]").length != 0) {
			isValid = validation.isFloatInRangeField($("#ic_form_3 input[name=pay][type!=hidden]"), 1, Number.MAX_VALUE, true) && isValid;
		}
		if ($("#ic_form_3 input[type!=hidden][name=pimsu]").length != 0) {
			isValid = validation.isFloatInRangeField($("#ic_form_3 input[type!=hidden][name=pimsu]"), 0, Number.MAX_VALUE, true) && isValid;
		}
		if ($("#ic_form_3 input[type!=hidden][name=rpay]").length != 0) {
			isValid = validation.isFloatInRangeField($("#ic_form_3 input[type!=hidden][name=rpay]"), 0.0000000001, Number.MAX_VALUE, true) && isValid;
		}

		return isValid;
	});

	// step 4
	$("#ic_form_4 input[type!=hidden][name=rpay]").change(function() {
		validation.isFloatInRangeField($(this), 1, Number.MAX_VALUE, true);
	});	
	$("#ic_form_4 input[type!=hidden][name=freq]").change(function() {
		validation.isIntInRangeField($(this), 1, Number.MAX_VALUE, true);
	});
	$("#ic_form_4 input[type!=hidden][name=freq_it]").change(function() {
		validation.isIntInRangeField($(this), 1, Number.MAX_VALUE, true);
	});
	
	var investCalculator4Prev = false;
	$("#ic_form_4 input[name=submitback]").click(function() {
		investCalculator4Prev = true;
	});

	$("#ic_form_4").submit(function() {
		if (investCalculator4Prev) {
			return true;
		}
		var isValid = true;
		if ($("#ic_form_4 input[type!=hidden][name=rpay]").length != 0) {
			isValid = validation.isFloatInRangeField($("#ic_form_4 input[type!=hidden][name=rpay]"), 1, Number.MAX_VALUE, true) && isValid;
		}
		if ($("#ic_form_4 input[type!=hidden][name=freq]").length != 0) {
			isValid = validation.isIntInRangeField($("#ic_form_4 input[type!=hidden][name=freq]"), 1, Number.MAX_VALUE, true) && isValid;
		}
		if ($("#ic_form_4 input[type!=hidden][name=freq_it]").length != 0) {
			isValid = validation.isIntInRangeField($("#ic_form_4 input[type!=hidden][name=freq_it]"), 1, Number.MAX_VALUE, true) && isValid;
		}

		return isValid;
	});
	
	// step 5
	$("#ic_form_5 input[type!=hidden][name=fret]").change(function() {
		validation.isFloatInRangeField($(this), 0, Number.MAX_VALUE, true);
	});
	$("#ic_form_5 input[type!=hidden][name=ipay]").change(function() {
		validation.isFloatInRangeField($(this), 0, 10.0, true);
	});

	var investCalculator5Prev = false;
	$("#ic_form_5 input[name=submitback]").click(function() {
		investCalculator5Prev = true;
	});

	$("#ic_form_5").submit(function() {
		if (investCalculator5Prev) {
			return true;
		}
		var isValid = true;
		isValid = validation.isFloatInRangeField($("#ic_form_5 input[type!=hidden][name=fret]"), 0, Number.MAX_VALUE, true) && isValid;
		isValid = validation.isFloatInRangeField($("#ic_form_5 input[type!=hidden][name=ipay]"), 0, 10.0, true) && isValid;
		return isValid;
	});

/**
 * RegisterConsultation
 */
 	function checkForSelBranch() {
 		if ($("#branch").selectedValues() == null || $("#branch").selectedValues() == "") {
			$("#branch_err").show();
		} else {
			$("#branch_err").hide();
			$("#frm1").submit();
		}	
 	}
 	function resetForStep2() {
 		$("#frm1 #step").val("2");
		$("#year,#month,#time").val("");
		$("#selectedDay").val("0");
		if ($("#cosultation_div input[name=choice]").length > 0) {
			$("#cosultation_div input[name=choice]")[0].checked = false;
			$("#cosultation_div input[name=choice]")[1].checked = false;
		}
 	}
 	function changeConsultCitiesBranch(changedCity) {
		$("#branch").removeOption(/./);

		var city = cities[$("#city").val()]
		if (city != null) {
			$("#branch").addOption(city);
		}

		if ($("#branch").containsOption("")) {
			$("#branch").selectOptions("");
		} else if (parseInt($("#step").val()) >= 2 && changedCity) {
			$("#branch")[0].selectedIndex = 0;
			resetForStep2();
			$("#frm1").submit();			
		}
		$("#branch_err").hide();
 	}

 	// startup
	if ($("#cosultation_div").length != 0) {
		changeConsultCitiesBranch(false);
		$("#branch").selectOptions(selectedBranch);
		$("#cosultation_div").show();
	}

	// step 1
	$("#cosultation_div #city").change(function() {
		changeConsultCitiesBranch(true);
	});
	$("#cosultation_div #branch").change(function() {
		resetForStep2();
		checkForSelBranch();
	});

	// step 2
	$("#cosultation_div input[name=choice]").click(function() {
		$("#frm1 #step").val("3");
		$("#frm1").submit();
	});
	
	// step 3
	$("#cosultation_div #manager").change(function() {
		$("#frm1 #step").val("4");
		$("#frm1").submit();
	});

	// step 4_1 - month change
	$("#cosultation_div #calendar th a.arrow_l").click(function() {
		$("#month").val(parseInt($("#month").val()) - 1);
		$("#selectedDay").val("0");
		$("#frm1 #step").val("4");
		$("#time").val("");
		checkForSelBranch();
		return false;
	});
	// step 4_1 - month change
	$("#cosultation_div #calendar th a.arrow_r").click(function() {
		$("#month").val(parseInt($("#month").val()) + 1);
		$("#selectedDay").val("0");
		$("#frm1 #step").val("4");
		$("#time").val("");
		checkForSelBranch();
		return false;
	});
	// step 4_1 - day selected
	$("#cosultation_div #calendar td a[href=#]").click(function() {
		$("#selectedDay").val(parseInt($(this).html()));
		$("#frm1 #step").val("4");
		$("#time").val("");
		checkForSelBranch();
		return false;		
	});

	$("#cosultation_div #timerange tr.free input").click(function() {
		$("#frm1 #step").val("5");
		checkForSelBranch();
	});

	$("#cosultation_div #frm1 input[type=submit]").click(function() {
		var isValid = true;
		if (!$("#cosultation_div #frm1 #accept")[0].checked) {
			$("#accept_err").show();
			isValid = false;
		} else {
			$("#accept_err").hide();
		}
		idValid = validation.isAlphabetField($("input[name=namesurname]"), true) && isValid;
		
		var emptyEmailAndPhone = $("input[name=email]").val() == "" && $("input[name=phone]").val() == "";
		if (emptyEmailAndPhone) {
			isValid = validation.isEmailField($("input[name=email]"), true) && isValid;
			isValid = validation.isPhoneField($("input[name=phone]"), true) && isValid;	
		} else {
			isValid = validation.isEmailField($("input[name=email]"), $("input[name=email]").val() != "") && isValid;	
			isValid = validation.isPhoneField($("input[name=phone]"), $("input[name=phone]").val() != "") && isValid;
		}

		if (!isValid) {
			return false;
		}
		$("#frm1 #step").val("6");
 		if ($("#branch").selectedValues() == null || $("#branch").selectedValues() == "") {
			$("#branch_err").show();
		} else {
			$("#branch_err").hide();
			$("#frm1").submit();
		}		
	});
	$("#cosultation_div #frm1 input[name=phone]").change(function() {
		validation.hideErrors($("#cosultation_div #frm1 input[name=email]"));
		var valid = validation.isPhoneField($(this), false);		
	});
	$("#cosultation_div #frm1 input[name=email]").change(function() {
		validation.hideErrors($("#cosultation_div #frm1 input[name=phone]"));
		var valid = validation.isEmailField($(this), false);		
	});	
	$("#cosultation_div #frm1 input[name=namesurname]").change(function() {
		var valid = validation.isAlphabetField($(this), true);		
	});	
	$("#cosultation_div #frm1 #accept").click(function() {
		$("#accept_err").hide();		
	});

});
