var chooseYourTools = 0;
var identifyNum = 0;
var messages = [
        "Well done. You've identified them.",
        "Well done. You've identified the instruction words.",
        "Well done. You've identified the content words.",
        "Well done. You've identified the relationship words.",
        "Well done. You’ve identified the search terms."
        ];

// check if answer is correct
// if there are more than one right answer, separate all right answer with *
/**
function checkAnswer(formItem, answer, feedbackIfRight, feedbackIfWrong) {
    var answers = answer.split("*");
    var isRight = false;
    for (var i = 0; i < answers.length; i++) {
        if (formItem.value == answers[i]) {
            if (answers.length > 1) {
                if(feedbackIfRight == "")
                alert("Correct. (There is more than one right answer)");
                else
                alert(feedbackIfRight);
            } else {
                if(feedbackIfRight == "")
                alert("Correct");
                else
                alert(feedbackIfRight);
            }
            isRight = true;
        }
    }
    if (!isRight) {
        if(feedbackIfWrong == "")
            alert("Have another go");
        else
            alert(feedbackIfWrong);
    }
    attempt++;
}

*/


// when click on an object, change the style of this object
/**
 * parameters:
 *  obj: the object to be identified
 *  color: the color which this object will change to
 *  num: total number to be identified
 *  mid: message id for which message to display when done
 **/
function identify(obj, color, num, mid) {
    if (obj) {
        // new style here
        obj.style.color = color;
        obj.style.fontWeight = "bold";
        //obj.style.backgroundColor = "#ff6600";
        // disable onclick event
        obj.onclick = "return false;";
        identifyNum++;
        if (num == identifyNum) {
            if (mid > messages.length)
                mid = 0;
            alert(messages[mid]);
        }
    }
    return false;
}

// highlight the selected text
// works fine for IE, not very good for Firefox
function highlight(obj) {
    var txt;
    if (window.getSelection) {
        txt = window.getSelection() + "";
        ff(obj, txt);
    } else if (document.getSelection) {
        txt = document.getSelection() + "";
        ff(obj, txt);
    } else if (document.selection) {
        var rng = document.selection.createRange();
        txt = rng.text;
        var newHTML = "<a style='background-color: yellow;'>" + rng.text + "</a>";
        rng.pasteHTML(newHTML);
    }
}

// highlight selected text for firefox
function ff(obj, txt) {
    childNum = obj.childNodes.length;
    for (i = 0; i < childNum; i++) {
        child = obj.childNodes[i];
        if (child.nodeType == 3) { // text node
            content = obj.childNodes[i].nodeValue;
            if (childNum == 1)
                content = tidy(content);
            ii = content.indexOf(txt);
            if (ii != -1) {
                before = content.substring(0, ii);
                after = content.substring(ii + txt.length);
                child.nodeValue = before;
                newNode = document.createElement("span");
                // add text node for new node
                newNode.appendChild(document.createTextNode(txt));
                //newNode.style.color = "red";
                newNode.style.backgroundColor = "yellow";
                txtNode = document.createTextNode(after);
                // if this not the last child
                if (childNum > (i + 1)) {
                    child.parentNode.insertBefore(txtNode, child.nextSibling);
                    child.parentNode.insertBefore(newNode, child.nextSibling);
                } else { // last child
                    child.parentNode.appendChild(newNode);
                    child.parentNode.appendChild(txtNode);
                }
            }
        }
    }
}

function tidy(txt) {
    temp = txt.split("\n");
    newStr = "";
    for (i = 0; i < temp.length; i++) {
        newStr += temp[i] + " ";
    }
    temp = newStr.split("\r");
    newStr = "";
    for (i = 0; i < temp.length; i++) {
        newStr += temp[i] + " ";
    }
    return newStr;
}

function choose(formItem) {
    chooseYourTools = chooseYourTools + 1;
}

function unveil() {
    if (chooseYourTools < 5) {
        alert("Please choose your answer first.");
    } else {
        show('answer');
    }
}

// if this two items are overlapped
function overlap(obj1, obj2) {
    minX = Math.min(obj1.x, obj2.x);
    maxX = Math.max(obj1.x, obj2.x);
    minY = Math.min(obj1.y, obj2.y);
    maxY = Math.max(obj1.y, obj2.y);
    //alert("minX = " + minX + " maxX = " + maxX + " minY = " + minY + " maxY = " + maxY + " w = " + obj1.w + " h = " + obj1.h);
    if (( (maxX - minX) <= (obj1.w / 2) ) && ( (maxY - minY) <= (obj1.h / 2) )) {
        //alert("overlap : " + obj1.name + " " + obj2.name);
        return true;
    } else {
        return false;
    }
}

// re-arrange all items
function rearrange(itemArray) {
    row = 1;
    for (i = 1; i < itemArray.length; i++) {
        // alert("x = " + itemArray[i].x + " y = " + itemArray[i].y);
        if (itemArray[i - 1].y == itemArray[i].y) {
            x = itemArray[i - 1].x + itemArray[i - 1].w;
            itemArray[i].moveTo(x, itemArray[i - 1].y);
        } else {
            row++;
        }
    }
    obj = getObj("dragdrop");
    //alert("row = " + row);
    //obj.height = (row*20) + "px";
    obj.style.height = (row * 20) + "px";
}

function tried(times) {
    if (attempt >= times) {
        return true;
    } else {
        alert("Have a go first");
        return false;
    }
}

function answered(nz, ni) {
    // if( (nz.value.split(' ').length > 2) && (ni.value.split(' ').length > 2) )
    if ((nz.value.length > 0) && (ni.value.length > 0))
        return true;
    alert('You need to type something in each box before you can see the suggestions.');
    return false;
}

function GoToPage(page) {
    // replace the current page with the page specified
    window.location.replace(page);
}
