$.fn.changeId = function(append) {
this.find("[id]").each(function(i,e){
$(e).attr("id", $(e).attr("id") + append);
});
this.find('[for="word-"]').text(append);
this.find("[for]").each(function(i,e){
$(e).attr("for", $(e).attr("for") + append);
});
return this;
};
$(function() {
$.getJSON("server.php", {action:"5", user:"foo", passwd:"bar"}, function (data) {
var numWord = 1;
var user = "foo";
var passwd = "bar";
var relations = data;
var nbWordMin = 3;
var wordsOK = new Array();
var centerOK = false;
var displayNWordLines = function (nb) {
for(var i=numWord; i')
.val(i)
.data("format", value)
.appendTo("#relations select");
});
$("#relation1, #relation2").change(function() {
if ($("#relation1").val() == $("#relation2").val())
displayError("Les relations doivent être différentes");
else
displayError("");
displayRelations();
});
displayRelations();
};
var displayButtons = function () {
$("#button").html('');
$("#addLine").click(function(){ displayNWordLines(1); });
$("#button").append('');
$("#validate").click(function(){ formOK(); });
};
var checkWord = function () {
updateRelationLabels();
var input = $(this);
var word = input.val();
input.parent("td, #center").removeClass("valid invalid");
if (word != "") {
$.ajax({
type: "GET",
url: "server.php?",
data: "action=4&word="+word+"&user="+user+"&passwd="+passwd,
success: function(msg){
input.parent("td, #center").addClass((msg == false) ? "invalid" : "valid");
wordsOK[input.attr("id")] = !(msg == false);
}});
}
};
var formOK = function() {
displayError("");
if ($("#relation1").val() == $("#relation2").val())
displayError("Les deux relation doivent être différents");
else if ($("#centralWord").val() == "")
displayError("Le mot central doit être renseigné.");
else if (badWord())
displayError("Il existe des mots incorrects");
else if (nbWordOK() < nbWordMin)
displayError("Le nuage doit contenir au moins "+nbWordMin+" mots valides.");
else if (!relationsOK())
displayError("Tout les mots ne sont pas liés à une relation");
else
sendGame();
return false;
};
var nbWordOK = function() {
var count = 0;
for (word in wordsOK)
if (wordsOK[word] == true)
count++;
return count;
};
var badWord = function() {
console.log(wordsOK);
for (word in wordsOK)
if ($("#"+word).val() != "" && wordsOK[word] == false)
return true;
return false;
}
var relationsOK = function() {
console.log(wordsOK);
for(i = 1; i < numWord; i++) {
if(wordsOK["word-"+i]) {
if(!$("#r1-"+i).is(":checked") && !$("#r2-"+i).is(":checked") && !$("#r3-"+i).is(":checked") && !$("#r4-"+i).is(":checked"))
return false;
}
}
return true;
}
var sendGame = function() {
var exit;
var cloud = "";
exit = {center:$("#centralWord").val(),
relations:[$("#relation1").val(),$("#relation2").val(),0,-1],
cloud:[]};
for(i=1;i