var server_root = "/";
var max_selected = 100;

$(document).ready(function() {
	var lang = $("#lang").attr("lang");
	var url_cities = server_root + "quotation_request_ajax_" + lang + "/cities/";
	$.getJSON(url_cities, function(data) {
		var options = "";

		$.each(data, function(i, province) {
			options += "<option title=\"" + province.name + "\" value=\"" + province.id + "\">" + province.name + "</option>";
		});
		$("#munis").empty();
		if (lang == "fi") {
			$("#regions").empty();
			$("#provinces").html(options);
		}
		else if (lang == "et") {
			$("#regions").html(options);
			$("#provinces").empty();
		}
	});
	
	var url_categories = server_root + "quotation_request_ajax_" + lang + "/categories/";
	$.getJSON(url_categories, function(data) {
		var options = "";

		$.each(data, function(i, group) {
			options += "<option title=\"" + group.name + "\" value=\"" + group.groupid + "\">" + group.name + "</option>";
		});
		$("#categories").empty();
		$("#groups").html(options);
	});
});

$(document).ready(function() {
	$("#provinces").change(function () {
		var selected_provinces = "";
		$("#provinces :selected").each(function(i, selected){
			selected_provinces += $(selected).val() + ",";
		});
		var url = server_root + "quotation_request_ajax_fi/cities/" + selected_provinces + "/";
		$.getJSON(url, function(data) {
			var options = "";
			$.each(data, function(i, region) {
				options += "<option title=\"" + region.name + "\" value=\"" + region.id + "\">" + region.name + "</option>\n";
			});
			$("#munis").empty();
			$("#regions").html(options);
		});
	});
	
	$("#regions").change(function () {
		var selected_regions = "";
		
		$("#regions :selected").each(function(i, selected){
			selected_regions += $(selected).val() + ",";
		});
		
		if (selected_regions != "") {
			var url = server_root + "quotation_request_ajax_fi/cities/0/" + selected_regions + "/" ;
			$.getJSON(url, function(data) {
				var options = "";
				var selected_munis = [];
				$("#selected_munis option").each(function(i, selected){
					selected_munis[i] = parseInt($(selected).val());
				});
				
				$.each(data, function(i, muni) {
					if (muni.name != null && jQuery.inArray(parseInt(muni.id), selected_munis) == -1) {
						options += "<option title=\"" + muni.name + "\" value=\"" + muni.id + "\">" + muni.name + "</option>\n";
					}
				});
				$("#munis").html(options);
			});
		}
	});
	
	$('#add_loc').click(function() {
		var munis_selected = $('#munis option:selected').size();
		var selected_munis_size = $('#selected_munis option').size();
		var total = munis_selected + selected_munis_size;
		if (total <= max_selected) {
			$('#munis option:selected').remove().appendTo('#selected_munis');
		}
		else {
			var lang = $("#lang").attr("lang");
            if (lang == "fi") {
                alert("Valittuja kategorioita voi olla enintään " + max_selected + " kpl");
            }
            else if (lang == "et") {
                alert("Valitud valdasid võib olla max " + max_selected + " tk.");
            }
		}
		
		if ($('#selected_munis option').size() >= max_selected) {
			$('#add_loc').hide();
		}
		
		count_companies();
	});
	
	$('#remove_loc').click(function() {
		$('#selected_munis option:selected').remove();
		$("#regions").trigger("change");
		var size = $('#selected_munis option').size();
		if (size < max_selected) {
			$('#add_loc').show();
		}
		count_companies();
	});

});

$(document).ready(function() {
	$("#groups").change(function () {
		var selected_groups = "";
		$("#groups :selected").each(function(i, selected){
			selected_groups += $(selected).val() + ",";
		});
		if (selected_groups != "") {
			var lang = $("#lang").attr("lang");
			var url = server_root + "quotation_request_ajax_" + lang + "/categories/" + selected_groups + "/";
			$.getJSON(url, function(data) {
				var options = "";
				var selected_categories = [];
				$("#selected_categories option").each(function(i, selected){
					selected_categories[i] = parseInt($(selected).val());
				});
				$.each(data, function(i, category) {
					if (category.name != null && jQuery.inArray(parseInt(category.categoryid), selected_categories) == -1) {
						options += "<option title=\"" + category.name + "\" value=\"" + category.categoryid + "\">" + category.name + "</option>\n";
					}
				});
				$("#categories").html(options);
			});
		}
	});
	
	$('#add_cat').click(function() {
		var categories_selected = $('#categories option:selected').size();
		var selected_categories_size = $('#selected_categories option').size();
		var total = categories_selected + selected_categories_size;
		if (total <= max_selected) {
			$('#categories option:selected').remove().appendTo('#selected_categories');
		}
		else {
            var lang = $("#lang").attr("lang");
            if (lang == "fi") {
                alert("Valittuja kategorioita voi olla enintään " + max_selected + " kpl");
            }
            else if (lang == "et") {
                alert("Valitud kategooriaid võib olla max " + max_selected + " tk.");
            }
		}
		
		if ($('#selected_categories option').size() >= max_selected) {
			$('#add_cat').hide();
		}
		count_companies();
	});
	
	$('#remove_cat').click(function() {
		$('#selected_categories option:selected').remove();
		
		$("#groups").trigger("change");
		var size = $('#selected_categories option').size();
		if (size < 5) {
			$('#add_cat').show();
		}
		count_companies();
	});
});


$(document).ready(function() {
	
	$('#submit').click(function() {
		var hidden = "";
		$('#selected_munis option').attr("selected", true);
		$('#selected_munis_hidden').attr("value", "");
		$('#selected_munis option').each(function(i, selected) {
			hidden += $(selected).val() + ":" + $(selected).text() + "|";
		});
		$('#selected_munis_hidden').attr("value", hidden);
		
		var hidden = "";
		$('#selected_categories option').attr("selected", true);
		$('#selected_categories_hidden').attr("value", "");
		$('#selected_categories option').each(function(i, selected) {
			hidden += $(selected).val() + ":" + $(selected).text() + "|";
		});
		$('#selected_categories_hidden').attr("value", hidden);
	});
});

$(document).ready(function() {
	if ($('#requester_type_company').attr("checked") == true) {
		$("#private_name").hide();
		$("#company_name").show();
		$("#contact_person").show();
	}
	else if ($('#requester_type_private').attr("checked") == true) {
		$("#company_name").hide();
		$("#private_name").show();
		$("#contact_person").hide();
	}
	
	var lang = $("#lang").attr("lang");
	if (lang == "en") {
		lang = "";
	}
	
	$("#validity_end").datepicker($.extend({}, 
	$.datepicker.regional[lang], {
		showStatus: true,
		numberOfMonths: 2,
		showOn: "both",
		minDate: "2w",
		maxDate: "4w",
		changeMonth: false, 
		changeYear: false,
		buttonImage: "../images/calendar_icon.gif", 
		buttonImageOnly: true 
	}));
	
	$('textarea#qr_content').autogrow({
		lineHeight: 16
	});
	
	
	$("#requester_type_company").click(function() {
		$("#private_name").hide();
		$("#company_name").show();
		$("#contact_person").show();
	});
	$("#requester_type_private").click(function() {
		$("#company_name").hide();
		$("#private_name").show();
		$("#contact_person").hide();
	});
	
	if ($.cookie("quotation_request")) {
		$("#enable_quotation_request").hide();
		$("#disable_quotation_request").show();
	}
	else {
		$("#disable_quotation_request").hide();
		$("#enable_quotation_request").show();
	}
	
	$("#eqr").click(function() {
		$.cookie("quotation_request", 'true', { path: '/', expires: 1 });
		$.cookie("quotation_request_ids", '', { path: '/', expires: 1 });
		$("#enable_quotation_request").hide();
		$("#disable_quotation_request").show();
	});
	
	$("#dqr").click(function() {
        $.cookie("quotation_request", null, { path: '/', expires: 1 });
        $.cookie("quotation_request_ids", null, { path: '/', expires: 1 });
        $("#disable_quotation_request").hide();
		$("#enable_quotation_request").show();
		$(".qr_companies").remove();
		$("#selected_companies").hide();
		$("#selections").show();
		$("#select_more_companies_show").hide();
		count_companies();
	});
	
	$("#seqr").click(function() {
		$.cookie("quotation_request", 'true', { path: '/', expires: 1 });
		$("#enable_quotation_request").hide();
		$("#disable_quotation_request").show();
	});
	
	$(".remove").click(function() {
		var value = $(this).attr("id");
		prevItem = $(this).parent().prev(".qr_companies");
		$(this).animate({
			opacity: "hide",
			height: "hide"
		}, 100, "linear", function() {
			prevItem.animate({
				height: "-=2px"
			}, 50, "swing", function() {
				prevItem.animate({
					height: "+=2px"
				}, 100, "swing"); 
			}); 
		}); 
		$(this).parent().parent().remove();
		removeCompany(value);
		
		if (!$('#selected_companies').children().hasClass("qr_companies")) {
			$("#selected_companies").hide();
			$("#select_more_companies_show").hide();
			$("#selections").show();
		}
		
		if (count_selected_companies() < 100) {
			$("#select_more_companies_show").show();
		}
		
		if (count_selected_companies() < 1) {
			$(".select_more_companies").hide();
		}
		
		count_companies();
	});
	
	$("#select_more_companies_hide").hide();
	
	$("#select_more_companies_show").click(function() {
		$("#select_more_companies_show").hide();
		$("#select_more_companies_hide").show();
		$("#selections").show();
	});
	
	$("#select_more_companies_hide").click(function() {
		$("#select_more_companies_hide").hide();
		$("#selections").hide();
		$("#select_more_companies_show").show();
	});
	
	count_companies();
	
	if (count_selected_companies() >= 100) {
		$(".select_more_companies").hide();
	}
	
});


$(document).ready(function() {
	if (!$.cookie("quotation_request_helps")) {
		$.cookie("quotation_request_helps", "1|0|0", { path: '/', expires: 30 });
	}
	
	var qr_helps_array = get_quotation_request_helps();
	
	if (qr_helps_array[0] == 0) {
		click("tender");
	}
	if (qr_helps_array[1] == 0) {
		click("how_work");
	}
	if (qr_helps_array[2] == 0) {
		click("qr_leave");
	}
	
	
	$("#tender").click(function() {
		set_quotation_request_helps("tender");
	});
	
	$("#how_work").click(function() {
		set_quotation_request_helps("how_work");
	});
	
	$("#qr_leave").click(function() {
		set_quotation_request_helps("qr_leave");
	});
});


$(document).ready(function() {
	$("#captcha").click(function() {
		$(this).attr("src", "/captcha_image.cgi?" + Math.random('now'));
	});
});


function get_quotation_request_helps() {
	var qr_helps = $.cookie("quotation_request_helps");
	return qr_helps.split("|");
}

function set_quotation_request_helps(help) {
	var qr_helps_array = get_quotation_request_helps();
	
	if (help == "tender") {
		qr_helps_array[0] = change_bool(qr_helps_array[0]);
	}
	else if (help == "how_work") {
		qr_helps_array[1] = change_bool(qr_helps_array[1]);
	}
	else if (help == "qr_leave"){
		qr_helps_array[2] = change_bool(qr_helps_array[2]);
	}
	
	
	var new_helps = qr_helps_array.join("|");
	$.cookie("quotation_request_helps", new_helps, { path: '/', expires: 30 });
	
}

function change_bool(value) {
	if (value == 0) {
		return 1;
	}
	else {
		return 0;
	}
}

function count_companies() {
	var lang = $("#lang").attr("lang");
	var selected_categories = "";
	var selected_munis = "";
	
	$("#selected_categories option").each(function(i, selected){
		selected_categories += parseInt($(selected).val()) + ",";
	});
	
	$("#selected_munis option").each(function(i, selected){
		selected_munis += $(selected).text() + ",";
	});
	
	var url = server_root + "quotation_request_ajax_" + lang + "/count/" + selected_categories + "/" + selected_munis + "/";
	
	var selected_webids = get_selected_companies();
	
	$.getJSON(url, function(data) {
		//count += parseInt(data['count']);
		
		$.each(data, function(i, webid) {
			selected_webids.push(webid);
		});
		
		var unique_webids = new Array();
		$.each(selected_webids, function(i, webid) {
			if ($.inArray(webid, unique_webids) == -1) {
				unique_webids.push(webid);
			}
		});
		
		var count = unique_webids.length;
		
		if (count > 0) {
			$("#count").removeClass("red").addClass("green");
		}
		else {
			$("#count").removeClass("green").addClass("red");
		}
		$("#count").html("<strong>" + count + "</strong>");
		
		$("#hid_count").attr("value", count);
	});
}
