//(c) 2009 Joren Lauwers - JSO
//upload form
var RecaptchaOptions = {
   theme : 'clean'
};
var fields = ["u","u","v","v","v","u","u"];
var valid = ["v","v","v","v","v","v","v"];
var restype = "";
var allowedExtensions = "";

resdap_ajax_init();

function checkForm(forminput) {
	switch(forminput) {
		//resource type in seperate function
		case 0: //category
			if (document.getElementById('rescategory').value == "none") {
				fields[0] = "u";
			} else {
				fields[0] = "v";
			}
			document.getElementById('rescategory_icon').src = "/files/images/form_"+fields[0]+".png";
			break;			
		case 1: //title
			if (document.getElementById('restitle').value.length>2 && document.getElementById('restitle').value.length<50) {
				fields[1] = "v";
			} else if (document.getElementById('restitle').value=="") {
				fields[1] = "u";
			} else {
				fields[1] = "i";
			}
			document.getElementById('restitle_icon').src = "/files/images/form_"+fields[1]+".png";
			break;
		/*case 2: //user
			fields[2] = "u";
			if (document.getElementById('resuser').value!="") {
				//Check if the user exists on scratch.mit.edu
				fields[2] = resdap_ajax_get('checkscratchruser&n='+document.getElementById('resuser').value);
				alert(fields[2]);
				//Oldfashioned way :)
				// window.frames["usercheck"].location = "checkuser.php?u=" + document.getElementById('resuser').value;
			}
			document.getElementById('resuser_icon').src = "/files/images/form_"+fields[2]+".png";
			break;*/
		case 5: //file
			resfile_src = document.uploadform.resfile.value;
			if (resfile_src=="") {
				fields[5] = "u";
			} else {
				resfile_extension = resfile_src.substr(resfile_src.lastIndexOf("."));
				if (allowedExtensions.indexOf(resfile_extension.toLowerCase())>=0) {
					fields[5] = "v";
				} else {
					fields[5] = "i";
				}
			}
			document.getElementById('resfile_icon').src = "/files/images/form_"+fields[5]+".png";
			break;
		case 6: //source
			fields[6] = "v";
			break;			
	}
	if (fields.toString()==valid.toString()) {
		document.getElementById('submit').disabled = false;
	} else {
		document.getElementById('submit').disabled = true;
	}
}
function setResType(restypevalue) {
	fields[0] = "u";
	document.getElementById('rescategory_icon').src = "/files/images/form_"+fields[0]+".png";
	restype = restypevalue;
	//document.getElementById('uploadform_infobox').style.visibility = 'visible';
	document.getElementById('uploadform_infobox').style.display = 'inline-block';
	document.uploadform.rescategory.options.length=0; //clear options
	switch(restypevalue) {
		case "sprite":
			msg = '<img src="/files/images/icon_sprite.png"></img>';
			msg += '<p>It is sometimes hard to see the difference between a sprite and a script, but this is our guide: if your script(s) on the sprite are also involving or relying on costumes (e.g. color sensors), it is concidered a sprite.<br><br>While we know it is possible, please do not share 1 sprite 1 script projects on Scratch resources in a sprite.<br><br>Be sure to use only <b>local variables</b> (for this sprite only) as globals do not work because of a Scratch bug.</p>';
			allowedExtensions = ".sprite";
			document.uploadform.rescategory.options[0]=new Option("Select a category...", "none", true, false);
			document.uploadform.rescategory.options[1]=new Option("Animals", "animals", false, false);
			document.uploadform.rescategory.options[2]=new Option("Fantasy", "fantasy", false, false);
			document.uploadform.rescategory.options[3]=new Option("Letters", "letters", false, false);
			document.uploadform.rescategory.options[4]=new Option("People", "people", false, false);
			document.uploadform.rescategory.options[5]=new Option("Things", "things", false, false);
			document.uploadform.rescategory.options[6]=new Option("Transportation", "transportation", false, false);
			break;
		case "script":
			msg = '<img src="/files/images/icon_script.png"></img>';
			msg += '<p>It is sometimes hard to see the difference between a script and a sprite, but this is our guide: if your scripts are also involving or relying on costumes (e.g. color sensors) , it is concidered a sprite.<br><br>Be sure to use only <b>local variables</b> (for this sprite only) as globals do not work because of a Scratch bug.</p>';
			allowedExtensions = ".sprite";
			document.uploadform.rescategory.options[0]=new Option("Select a category...", "none", true, false);
			document.uploadform.rescategory.options[1]=new Option("Movement", "movement", false, false);
			document.uploadform.rescategory.options[2]=new Option("Math", "math", false, false);
			document.uploadform.rescategory.options[3]=new Option("Lists", "lists", false, false);
			document.uploadform.rescategory.options[4]=new Option("Misc", "misc", false, false);
			break;
		case "sound":
			msg = '<img src="/files/images/icon_sound.png"></img>';
			msg += '<p>We are really sorry to say that we do not yet support the .wav format. Please upload only mp3 sounds.<br><br>We recommend that your sound is shorter then 1 minute, as Scratch is not able to handle large sound files properly. (Neither is our storage ;) )</p>';
			allowedExtensions = ".mp3"; //no wav at the moment!!!
			document.uploadform.rescategory.options[0]=new Option("Select a category...", "none", true, false);
			document.uploadform.rescategory.options[1]=new Option("Animal", "animal", true, false);
			document.uploadform.rescategory.options[2]=new Option("Effects", "effects", false, false);
			document.uploadform.rescategory.options[3]=new Option("Electronic", "electronic", false, false);
			document.uploadform.rescategory.options[4]=new Option("Human", "human", false, false);
			document.uploadform.rescategory.options[5]=new Option("Instruments", "instruments", false, false);
			document.uploadform.rescategory.options[6]=new Option("Music Loops", "loops", false, false);
			document.uploadform.rescategory.options[7]=new Option("Percussion", "percussion", false, false);
			document.uploadform.rescategory.options[8]=new Option("Vocals", "vocals", false, false);
			break;
		case "background":
			msg = '<img src="/files/images/icon_background.png"></img>';
			msg += '<p>Images you upload are always resized to 480x360, for best quality, you should resize them yourself. Your image might be stretched to fit this size.<br><br>We are sorry to say that we do not yet support the (ancient) .BMP format.</p>';
			allowedExtensions = ".jpg .jpeg .gif .png";
			document.uploadform.rescategory.options[0]=new Option("Select a category...", "none", true, false);
			document.uploadform.rescategory.options[1]=new Option("Indoors", "indoors", false, false);
			document.uploadform.rescategory.options[2]=new Option("Outdoors", "outdoors", false, false);
			document.uploadform.rescategory.options[3]=new Option("Nature", "nature", false, false);
			document.uploadform.rescategory.options[4]=new Option("Sports", "sports", false, false);
	}
	msg += '<br>';
	document.getElementById('uploadform_infobox').innerHTML = msg;
	checkForm(5) //check if file still matches resource type.
}
function userValidated() {
	fields[2] = "v";
	document.getElementById('resuser_icon').src = "/files/images/form_v.png";
}
function userNotRecognized() {
	fields[2] = "i";
	document.getElementById('resuser_icon').src = "/files/images/form_i.png";	
}
