	var type_here;

$(document).ready(function() {
	var valid_input;
	if (typeof(console) != "undefined") {
		console.log($(this).attr("title") + " - initiated");
		console.log("Language - "+language);
	}
	
	if (language == "en") {
		type_here = "Type here";
	} else {
		type_here = "Skriv her"
	}
	//swfobject.embedSWF("/scripts/logo_banner.swf", "flash", "300", "150", "9.0.0");
	
	$language = $("#current_language").html();
	
	var vars = {
		clickURL: '/',
		language: $language
	};

	var params = { 
		quality: 'best', 
		scale: 'noScale', 
		salign: 'lt', 
		menu: 'false', 
		allowScriptAccess: 'always', 
		allowFullScreen: 'true'
	};

	var attributes = { 
		id: 'master', 
		name: 'master' 
	};

	swfobject.embedSWF('/scripts/logo_banner_v2.swf', 'flash', '300', '150', '9.0.0', 'javascript/expressInstall.swf', vars, params, attributes);
	
	

	$('input[type="text"], textarea').focusin(function(event) {
		if ($(this).val() == type_here) {
			$(this).val("");
		}
	}).blur(function(event) {
		if ($(this).val() == "") {
			$(this).val(type_here);
		}
	});
	
	$('#agree').click(function(event) {
		valid_input = validate_input($(this).attr("id"));

		if (valid_input == -1) {
			$(this).insertAfter("<label class=\"error_"+$(this).attr("id")+"\" for=\""+$(this).attr("id")+"\">*</label>")
		} else {
			$(".error_"+$(this).attr("id")).remove();
		}
	});
	
	$('#overskrift, #beskjed, #navn, #epost').blur(function(event) {
		valid_input = validate_input($(this).attr("id"));
		if (valid_input == -1) {
			if ($(".error_"+$(this).attr("id")).length == 0) {
				$(this).after("<label class=\"error error_"+$(this).attr("id")+"\" for=\""+$(this).attr("id")+"\" title=\"Dette feltet er påkrevd\">*</label>")
			}
		} else {
			$(".error_"+$(this).attr("id")).remove();
		}
	});
	
	
	/* Upload */
	var btnUpload=$('.share_image');
	var status=$('#status');
	var upload;
	if (btnUpload.length == 1) {
		upload = new AjaxUpload(btnUpload, {
			action: 'upload.asp',
			//Name of the file input box
			name: 'uploadfile',
			responseType: 'text/html',
			autoSubmit: false,
			onSubmit: function(file, ext){
				if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))) {
					// check for valid file extension
					status.text('Only JPG, PNG or GIF files are allowed');
					return false;
				}
				status.text('Laster opp...');
				this.disable();
			},
			onChange: function(file, extension){
				$("#status").html(file);
				$("#it_smallvarchar1").val(file);
			},
			onComplete: function(file, response){
				//On completion clear the status
				status.text('');
				//Add uploaded file to list
				if(response==="error"){
					$("#status").html("Noe gikk feil med opplastningen, muligens ugyldig tegn i filnavn").addClass('error');
				} else {
					$("#status").html('<img src="'+response+'" alt="Lastet opp av bruker" /><br />'+file).addClass('success');
					$("#it_smallvarchar1").val(response);
					$(".form-submit").submit();
				}
			}
		});
	}
	
	$("#add_comment").click(function(event) {
		event.preventDefault();
		if ($("#it_smallvarchar1").val().length > 0) {
			upload.submit();
		} else {
			$(".form-submit").submit();
		}
	});
});

function validate_input(id) {
	var valid = 1;
	var return_value = 1

	if ($('#overskrift').val().length == 0 || $('#overskrift').val() == type_here) {
		valid = 0;
		if (id == "overskrift") {
			return_value = -1;
		}
	}

	if ($('#beskjed').val().length == 0 || $('#beskjed').val() == type_here) {
		valid = 0;
		if (id == "beskjed") {
			return_value = -1;
		}
	}

	if ($('#navn').val().length == 0 || $('#navn').val() == type_here) {
		valid = 0;
		if (id == "navn") {
			return_value = -1;
		}
	}
	
	
	valid_email = validate_email($('#epost').val());
	if ($('#epost').val().length == 0 || $('#epost').val() == type_here || valid_email == null) {
		valid = 0;
		if (id == "epost") {
			return_value = -1;
		}
	}

	if (!($("#agree").is(':checked'))) {
		if (valid == 1) {
			$("#agree_label").css("color", "red");
		}
		valid = 0;
	} else {
		$("#agree_label").css("color", "#6D757A");
	}

	if (valid == 1) {
		$("#add_comment").attr("disabled", "");
		$("#add_comment").css("border", "0px");
		
	} else {
		$("#add_comment").attr("disabled", "disabled");
		$("#add_comment").css("border", "1px dotted red");
		return return_value;
	}
}

function validate_email(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 email.match(re) 
}
