function zavsmartz_money_format(val) {
	val = val + '';
	if (val == '') {
		return 'N/A';
	}
	
	// split it
	var pieces = val.split('.', 2);
	var whole = pieces[0];
	
	// format decimal
	var decimal = '00';
	if (pieces.length == 2) {
		decimal = pieces[1];
	}
	if (decimal.length == 1) {
		decimal += '0';
	}
	else if (decimal.length > 2) {
		decimal = decimal.substring(0, 2);
	}
	if (isNaN(decimal)) {
		return 'N/A';
	}
	
	// format whole
	if (isNaN(whole)) {
		return 'N/A';
	}
	if (whole.length > 3) {
		// get reversed
		var splitext = whole.split('');
		var revertext = splitext.reverse();
		var pos = 3;
		
		// add commas
		var num = parseInt(whole.length / 3);
		if (whole.length / 3 == num) {
			num--;
		}
		while (num > 0) {
			revertext.splice(pos, 0, ',');
			pos += 4;
			num--;
		}
		
		// unreverse
		splitext = revertext.reverse();
		whole = splitext.join('');
	}
	
	return '$' + whole + '.' + decimal;
}

function av_calculation() {
	if ($("#edit-meeting").val() != "" && $("#edit-package").val() != "" && $("#edit-days").val() != "" && $("#edit-region").val() != "") {
		// get variables
		var days = parseInt($("#edit-days").val());
		var region = parseInt($("#edit-region").val());
		var price = parseFloat($("#edit-product-kit-price-" + $("#edit-package").val()).val());
		var rooms = parseInt($("#edit-meeting").val());
		
		// calculate support days
		var support_days = days;
		
		// calculate the number of days
		var estimate_days = 0;
		if (days < 4) {
			// day one to four
			estimate_days = days;
		}
		else if (((days - 2) % 7) == 0) {
			// seventh day
			estimate_days = (days - 2) / 7;
			estimate_days = estimate_days + (estimate_days * 2) + 2;
		}
		else if (((days - 1) % 7) == 0) {
			// sixth day
			estimate_days = (days - 1) / 7;
			estimate_days = estimate_days + (estimate_days * 2) + 1;
		}
		else {
			// first to fifth days
			estimate_days = Math.ceil(days / 7);
			estimate_days = estimate_days + (estimate_days * 2);
		}
		
		// calculate percentage
		var percentage = 1;
		switch (region) {
			case 1:
			case 2:
			case 5:
				percentage = 1.2;
			break;
			case 4:
			case 6:
				percentage = 1.3;
			break;
		}
		
		// calculate support
		var support = Math.ceil(rooms / 5);
		
		var estimate = rooms * price * estimate_days * percentage;
		var technical = 0.0;
		var generalIDs = ['2446', '2447', '2452'];
		var otherIDs = [-1];
		
		if(jQuery.inArray($('#edit-package').val(), generalIDs) >= 0)
		{
			technical = estimate * .3;
		}
		else if(jQuery.inArray($('#edit-package').val(), otherIDs) >= 0)
		{
			technical = estimate * .4;
		}
		else
		{
			technical = support * 400 * support_days * percentage;
		}
		
		$("#edit-estimate").val(zavsmartz_money_format(estimate));
		$("#edit-support").val(zavsmartz_money_format(technical));
		$("#edit-total").val(zavsmartz_money_format(technical + estimate));
		$("#edit-estimate2").val(zavsmartz_money_format(estimate));
		$("#edit-support2").val(zavsmartz_money_format(technical));
		$("#edit-total2").val(zavsmartz_money_format(technical + estimate));
	}
}
	$(document).ready(function() {
		av_calculation();
			$("#zavsmartz-free-estimate-form select.form-select").change(function(){
				av_calculation();
				});
		});
        
        
        $(document).ready(function() {
         
			$("#zavsmartz-free-estimate-form select.form-select").change(function() {
           	
				if ($("#edit-meeting-1").val() != "" && $("#edit-package-1").val() != "" && $("#edit-days-1").val() != "" && $("#edit-region-1").val() != "") {
					
                    // get variables
					var days = parseInt($("#edit-days-1").val());
					var region = parseInt($("#edit-region-1").val());
					var price = parseFloat( $("#edit-product-kit-price-" + $("#edit-package-1").val() + "-1" ).val() );
					var rooms = parseInt($("#edit-meeting-1").val());
					
					// calculate support days
					var support_days = days;
					
					// calculate the number of days
					var estimate_days = 0;
					if (days < 4) {
						// day one to four
						estimate_days = days;
					}
					else if (((days - 2) % 7) == 0) {
						// seventh day
						estimate_days = (days - 2) / 7;
						estimate_days = estimate_days + (estimate_days * 2) + 2;
					}
					else if (((days - 1) % 7) == 0) {
						// sixth day
						estimate_days = (days - 1) / 7;
						estimate_days = estimate_days + (estimate_days * 2) + 1;
					}
					else {
						// first to fifth days
						estimate_days = Math.ceil(days / 7);
						estimate_days = estimate_days + (estimate_days * 2);
					}
					
					// calculate percentage
					var percentage = 1;
					switch (region) {
						case 1:
						case 2:
						case 5:
							percentage = 1.2;
						break;
						case 4:
						case 6:
							percentage = 1.3;
						break;
					}
					
					// calculate support
					var support = Math.ceil(rooms / 5);
					
					var estimate = rooms * price * estimate_days * percentage;
		var technical = 0.0;
		var generalIDs = ['2446', '2447', '2452'];
		var otherIDs = [-1];
		
		if(jQuery.inArray($('#edit-package').val(), generalIDs) >= 0)
		{
			technical = estimate * .3;
		}
		else if(jQuery.inArray($('#edit-package').val(), otherIDs) >= 0)
		{
			technical = estimate * .4;
		}
		else
		{
			technical = support * 400 * support_days * percentage;
		}
                    
                    
                    
					$("#edit-estimate-1").val(zavsmartz_money_format(estimate));
					$("#edit-support-1").val(zavsmartz_money_format(technical));
					$("#edit-total-1").val(zavsmartz_money_format(technical + estimate));
					$("#edit-estimate2-1").val(zavsmartz_money_format(estimate));
					$("#edit-support2-1").val(zavsmartz_money_format(technical));
					$("#edit-total2-1").val(zavsmartz_money_format(technical + estimate));
				}
			});
		});
