function fcGetOrientation(file, callback) {
	var reader = new FileReader();
	
	reader.onload = function(e) {
		var view = new DataView(e.target.result);
		
		if (view.getUint16(0, false) != 0xFFD8) return callback(-2);
		
		var length = view.byteLength, offset = 2;
		
		while (offset < length) {
		
			var marker = view.getUint16(offset, false);
			
			offset += 2;
			
			if (marker == 0xFFE1) {
			
				if (view.getUint32(offset += 2, false) != 0x45786966) return callback(-1);
				
				var little = view.getUint16(offset += 6, false) == 0x4949;
				
				offset += view.getUint32(offset + 4, little);
				
				var tags = view.getUint16(offset, little);
				
				offset += 2;
				
				for (var i = 0; i < tags; i++)
				
				if (view.getUint16(offset + (i * 12), little) == 0x0112)
				
				return callback(view.getUint16(offset + (i * 12) + 8, little));
				
			}
			
			else if ((marker & 0xFF00) != 0xFF00) break;
			
			else offset += view.getUint16(offset, false);
		}
		
		return callback(-1);
	};
	
	reader.readAsArrayBuffer(file.slice(0, 64 * 1024));
	
}
function fcResetElement(element) {
	element.find(".uploaded-image").css("transform", "rotate(0deg)").removeClass("compensated");
	element.find("input[type=file]").val("");
	element.find(".uploaded-image").css("background-image", "none");
}
function fcValidateEmail(email) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}
function fcNotify(message) {
	$("#fcNotifier").fadeOut(250, function () {
	
		$("#fcNotifier").html("
"+message+"
").show();
		
		$("#fcNotifier p").css("margin-top", (($("#fcNotifier").height()/2)-($("#fcNotifier p").height()/2))+"px");
		
		$("#fcNotifier").hide().fadeIn(250).delay(1500).fadeOut(250);
	
	});
}
function fcSubmitImage() {
	$("#fcLoader").fadeIn(250);
	
	var formData = new FormData($("#fcUploadForm")[0]);
		
	$.ajax({
		url : "https://www.feedcowboy.com/asiakkaat/slicefi/update/fc.fiddle.php",
		type : "POST",
		data : formData,
		processData: false,
		contentType: false,
		error : function (request, status, error) {$("#fcLoader").fadeOut(250); fcNotify("Virhe tiedon käsittelyssä");},
		success : function(data) {
	
			$("#fcLoader").fadeOut(250);
		
			if (data.status == "ok") {
		
				$("#fcUploadForm").hide();
				
				$("#fcConfirmation").show();
				
				
Notice:  Undefined index: fcLanguage in /var/www/webroot/ROOT/asiakkaat/slicefi/update/js/e.slice.js.php on line 106
					fcNotify("Image was sent successfully.");
						
			} else {
		
				fcNotify(data.message ? data.message : "Virhe tiedon käsittelyssä");
		
			}
					
		}
	});
}
function eCheckForm() {
	var error = false;
		
	$("#fcUploadForm input[type=text], #fcUploadForm input[type=email]").each(function () {
	
		if ($(this).val() == "" && $(this).hasClass("mandatory")) {error = true;}
	
	});
	
	$(".checker").each(function () {
	
		if (!$(this).hasClass("checked")) {error = true;}
	
	});
	
	if (!error && $("#fcImageUpload input[type=file]").val() != "") {
	
		$(".form-additionals").show();
	
	} else {
	
		$(".form-additionals").hide();
	
	}
}
$(document).ready(function() {
	$("#fcUploadForm input[type=text], #fcUploadForm input[type=email]").on("keyup", function () {
		
		eCheckForm();
	
	});
	$(".checker").on("click", function () {
	
		$(this).toggleClass("checked");
	
		eCheckForm();
	
	});
	
	$("#fcLanguageForm select").change(function () {
	
		$("#fcLanguageForm").submit();
	
	});
		
	$("#fcContactTypeForm select").change(function () {
	
		$("#fcContactTypeForm").submit();
	
	});
	$(".contact-type-selector").click(function () {
	
		if ($(this).attr("contact-type") != null) {
		
			$("#fcContactTypeForm #fcContactType").val($(this).attr("contact-type"));
			
			$("#fcContactTypeForm").submit();
		
		}
	
	});
	$("#fcImageUpload input[type=file]").change(function() {
		var element = $(this).parent();
		var file = this.files[0];
		if (file.size < 10000000) {
			if (["image/jpeg", "image/png"].includes(file.type)) {
				if (navigator.userAgent.toLowerCase().indexOf("firefox") > -1) {
			
					fcGetOrientation(file, function(orientation) {
										
						if (orientation == 3) {element.find(".uploaded-image").css("transform", "rotate(180deg)").removeClass("compensated");}
						else if (orientation == 6) {element.find(".uploaded-image").css("transform", "rotate(90deg)").addClass("compensated");}
						else if (orientation == 8) {element.find(".uploaded-image").css("transform", "rotate(270deg)").addClass("compensated");}
						else {element.find(".uploaded-image").css("transform", "rotate(0deg)").removeClass("compensated");}
						
					});
				}
		
				function fcImageIsLoaded(file) {
					
                  	$("#fcLoader").fadeOut(250);
                  
					element.find(".uploaded-image").css("background-image", "url('"+file.target.result+"')");
				
					var image = new Image();
					image.src = file.target.result;
					image.onload = function() {
						                      
						eCheckForm();
										
					};
				
				}
		
              	$("#fcLoader").fadeIn(250);
              
				var reader = new FileReader();
				reader.onload = fcImageIsLoaded;
				reader.readAsDataURL(file);
				
			} else {
					
									fcNotify("File type is not valid.");
								
				fcResetElement(element);
				$(".form-additionals").hide();
				
			}
		
		} else {
				
							fcNotify("File size exceeds 10 MB.");
					
			fcResetElement(element);
			$(".form-additionals").hide();
		
		}
		
	});
});