//Launch
String.prototype.endsWith = function(str) 
{return (this.match(str+"$")==str)}
window.onload = init;

//This function will run on page load.
function init() {
	//Apply event to form.
	formulario = document.getElementById("qc_form");
	formulario.onsubmit = function () {
        if (document.URL.endsWith("qc.html"))
            return check(this, "Rellene los campos obligatorios, por favor.");
        else
            return check(this, "Please, fill in all mandatory fields.");
	}
}

function check(form, kErrorString) {
	if (!isFilled(form.language)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.knowledge)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.used_benchmarks)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.system)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.translated)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.difficulty)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.fonts)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.usability)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.help)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.integration)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.execution_info)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.colours)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.icons)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.structure)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.utility)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.satisfaction)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.response_time)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.problems)) {
		alert(kErrorString);
		return false;
	}
	if (!isChecked(form.results)) {
		alert(kErrorString);
		return false;
	}
	return true;
}

function isFilled(field) {
	if (field.value == "" || field.value == null) {
		return false;
	} else {
		return true;
	}
}

function isChecked(button) {
	var radioLength = button.length;
	if(radioLength == undefined) {
		return button.checked;
	}
	for(var i = 0; i < radioLength; i++) {
		if(button[i].checked) {
			return true;
		}
	}
	return false;
}

