// width to resize large images to
var maxWidth=328;
// height to resize large images to
var maxHeight=328;
// valid file types
var fileTypes=["png","jpg","jpeg","gif",""];
// the id of the preview image tag
var outImage="previewField";
// what to display when the image is not valid
var defaultPic="img/errorImage.png";
 
function preview(what)
{ 
var source=what.value;
var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
for (var i=0; i<fileTypes.length; i++) if (fileTypes[i]==ext) break;
globalPic=new Image();
if (i<fileTypes.length)
    globalPic.src=source;
else 
{
//    globalPic.src=errorPic;
    globalPic.src=defaultPic;
//    alert("Dette er ikke et gyldigt filformat!\nVælg et image af en af disse typer:\n\n"+fileTypes.join(", "));
//    alert("Dette er ikke et tilladt filformat!");
}
setTimeout("applyChanges()",200);

}

var globalPic;

function applyChanges() 
{

var field=document.getElementById(outImage); 
var x=parseInt(globalPic.width);

var y=parseInt(globalPic.height);if (x>maxWidth) 
{
y*=maxWidth/x;
x=maxWidth;
}

if (y>maxHeight) 
{
x*=maxHeight/y;
y=maxHeight;
}

field.style.display=(x<1 || y<1)?"none":""; 
field.src=globalPic.src; 

field.width=x;
field.height=y;
}
// End -->

