function resizeAvatar(image) {
	if(image.width > 150) {
		image.height = image.height * 150 / image.width;
		image.width = 150;
	}
}

function checkmapform ( form )
{
	if(form.description.value.length > 500) {
		alert("the description cannot be longer than 500 characters");
    		form.description.focus();
    		return false ;
	}

	if(form.map.value == "") {
		alert("select a map to upload");
    		return false ;
	} else {
		if(getfileextension(form.map.value) != "pk3") {			
			alert("only pk3 files are allowed");
    			return false ;
		}
		if(checkMap(form.map.value)) {
			alert("there is already a map called " + form.map.value + " in the database");
    			return false ;
		}
	}	

	return true;
}

function getfileextension(filename) 
{
	if( filename.length == 0 ) return ""; 
	var dot = filename.lastIndexOf("."); 
	if( dot == -1 ) return ""; 
	var extension = filename.substr(dot+1,filename.length); 
	return extension; 
}

function checkMap( map ) {
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest(); // Mozilla, Safari, Opera
	} else if (window.ActiveXObject) {
		try {
			request = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
		} catch (e) {
			try {
				request = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
			} catch (e) {}
		}
	}

	// überprüfen, ob Request erzeugt wurde
	if (!request) {
		return false;
	} else {
		var url = "mapdepot/checkmap.php?map=" + map;
		// Request öffnen
		request.open('get', url, false);
		// Request senden
		request.send(null);
		// Request auswerten
		if(request.responseText == "true") {
			return true;
		}
	}
	return false;
}

function checkMapEditForm( form ) {
	if(form.description.value.length > 500) {
		alert("the description cannot be longer than 500 characters");
    		form.description.focus();
    		return false ;
	}
	return true ;
}

function sortmaps(form){
    var sorting = form.sort.options[form.sort.selectedIndex].value ;
    if (sorting != ""){
            location.href = "mapdepot/index.php?order="+sorting ;
    }
}

function checkcommentform(form){
	if(form.comment.value.length > 200 || form.comment.value.length == 0 ) {
		alert("the comment cannot be empty or longer than 200 characters");
    		form.comment.focus();
    		return false ;
	}

	return true;
}