$(document).ready(function() {
	/* Used for uploading attachment */
	var btnUpload = $('#file');
	var status = $('#status');
	var upload;
	var file_id;

	if (btnUpload.length == 1) {
		upload = new AjaxUpload(btnUpload, {
			action: 'upload_email_attachment.asp',
			name: 'floor_attachment',
			responseType: 'text/html',
			autoSubmit: false,
			onSubmit: function(file, ext){
			},
			onChange: function(file, extension) {
				file_id = renameToIdFriendly(file)
				if (extension != "exe") {
					if ($("#"+file_id).length > 0) {
						status.append('<li style="color:red;">Filen "'+file+'" er allerede lastet opp</li>');
					} else {
						status.append('<li id="'+file_id+'" class="pending" style="color:blue;">Laster opp "'+file+'"</li>');
						upload.submit();
					}
				} else {
					status.append('<li id="'+file_id+'" style="color:red;">Filen "'+file+' er av typen exe-fil. Det er ikke tillatt og laste opp exe-filer"</li>');
				}
			},
			onComplete: function(file, response) {
				file_id = renameToIdFriendly(file)
				if (response === "error") {
					$("#"+file_id).html('Noe gikk feil med opplastningen av "'+file+'", muligens ugyldig tegn i filnavn').addClass('error').css("color", "red");
				} else {
					$("#"+file_id).html('Filen "'+file+'" er lastet opp').addClass('success').removeClass('pending').css("color", "green");
					if ($("#floor_attachment").val().length > 0) {
						$("#floor_attachment").val($("#floor_attachment").val()+",=?"+response);
					} else {
						$("#floor_attachment").val(response);
					}
				}
			}
		});
	}

	$(".submit").click(function(event) {
		event.preventDefault();
		if ($(".pending").length > 0) {
			$(".pdf").after('<div style="float:left;clear:left;">Det lastes fremdeles opp vedlegg, venligst vent til opplasting er ferdig</div>');
		} else {
			validate_input();
			//$(".floor_form").submit();
		}
	});

	$(".plumber input[type='text'], .site input[type='text']").change(function() {
		if ($(this).val() != "") {
			$(this).css("background", "#fff");

		}
	})
	$(".site input[type='radio']").change(function() {
		if ($(this).val() != "") {
			var name = $(this).attr("name");
			$("input[name='"+name+"']").parent().css("background", "#fff");
		}
	})
	$(".plumber input, .site input").keydown(function() {
		if ($(this).val() != "") {
			$(this).css("background", "#fff");
		}
	});
});

/* Change to whitelist */
function renameToIdFriendly(file) {
	var valid_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
	var file_id = "";

	for (i=0; i<file.length; i++) {
		if (valid_characters.indexOf(file.charAt(i)) < 0) {
			file_id += "_";
		} else {
			file_id += file.charAt(i);
		}
	}
	return file_id;
}

function validate_input() {
	var valid = 1

	if ($("#floor_plumber_firm").val() == "") {
		valid = 0;
		$("#floor_plumber_firm").css("background","#ffd6d6");
	}
	if ($("#floor_plumber_address").val() == "") {
		valid = 0;
		$("#floor_plumber_address").css("background","#ffd6d6");
	}
	if ($("#floor_plumber_postnr").val() == "") {
		valid = 0;
		$("#floor_plumber_postnr").css("background","#ffd6d6");
	}
	if ($("#floor_plumber_contact").val() == "") {
		valid = 0;
		$("#floor_plumber_contact").css("background","#ffd6d6");
	}
	if ($("#floor_plumber_phone").val() == "") {
		valid = 0;
		$("#floor_plumber_phone").css("background","#ffd6d6");
	}
	if ($("#floor_plumber_return").val() == "") {
		valid = 0;
		$("#floor_plumber_return").css("background","#ffd6d6");
	}
	if ($("#floor_plumber_email").val() == "") {
		valid = 0;
		$("#floor_plumber_email").css("background","#ffd6d6");
	}
	if ($("#floor_site_name").val() == "") {
		valid = 0;
		$("#floor_site_name").css("background","#ffd6d6");
	}
	if ($("#floor_site_address").val() == "") {
		valid = 0;
		$("#floor_site_address").css("background","#ffd6d6");
	}
	if ($("#floor_site_municipality").val() == "") {
		valid = 0;
		$("#floor_site_municipality").css("background","#ffd6d6");
	}
	if ($("input[name='floor_site_year']:checked").length == 0) {
		valid = 0;
		$("input[name='floor_site_year']").parent().css("background","#ffd6d6");
	}
	if ($("input[name='floor_site_rehab']:checked").length == 0) {
		valid = 0;
		$("input[name='floor_site_rehab']").parent().css("background","#ffd6d6");
	}
	if ($("#floor_efficiency_needs").val() == "") {
		valid = 0;
		$("#floor_efficiency_needs").css("background","#ffd6d6");
	}

	if (valid == 1) {
		$("form.floor_form").submit();
	} else {
		scroll(0,0);
		if ($(".error").length == 0) {
			$("form.floor_form").prepend('<span class="error">Du må fylle ut alle de røde feltene før skjemaet kan sendes</span>');
		}
	}
}

