﻿// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

// Script objects that should be loaded before we run
var typeDependencies = ['AjaxControlToolkit.AutoCompleteBehavior'];

// Test Harness
var testHarness = null;

// Controls in the page
var tb1 = null;
var autoCompleteBehavior1 = null;

var tb2 = null;
var autoCompleteBehavior2 = null;

var tb3 = null;
var autoCompleteBehavior3 = null;

var tb4 = null;
var autoCompleteBehavior4 = null;

// Ensure the textbox is in its empty state
function checkEmptyState(autoCompleteList, textBox) {
    return function() {
        testHarness.fireEvent(textBox, 'onfocus');
        testHarness.assertEqual('', textBox.value, "TextBox's  text should be '' instead of '" + textBox.value + "'");
        var children = autoCompleteList.childNodes;
        testHarness.assertEqual(children.length, 0, "Div should be have no content when textbox is empty");
    };
}

// Test the initial state of the control
function testInitialState(autoCompleteList, textBox) {
    return function() {
        checkEmptyState(autoCompleteList, textBox);
    };
}

// reset content in textbox
function resetTextBox(textBox) {
    return function() {
        textBox.value = '';
    };
}

// Test adding text focus from full control
function setText(text, textBox) {
    return function() {
        textBox.value = text;
        testHarness.fireEvent(textBox, 'onfocus');
        testHarness.fireEvent(textBox, 'onkeydown');
    };
}

function checkAutoComplete(numMatches, autoCompleteList) {
    return function() {
        var children = autoCompleteList.childNodes;
        return (children.length == numMatches);
    };
}

function verifyAutoCompleteBehavior(text, numMatches, autoCompleteList) {
    return function() {
        var children = autoCompleteList.childNodes;
        testHarness.assertEqual(children.length, numMatches, "Div should have: " + numMatches + " when there are " + text.length + " characters in the textbox; Actual matches: " + children.length);
        if (children.length > 0) {
            for (var i = 0; i < numMatches; i++) {
                var child = children[i];
                var match = child.innerHTML;
                testHarness.assertEqual(match.indexOf(text), 0, "Every word should start with text: " + text + " Actual: " + match);
            }
        }
    };
}

function verifySelectedIndex(index, autoCompleteBehavior) {
    return function() {
        testHarness.assertEqual(autoCompleteBehavior._selectIndex, index, "Selected index should be: " + index + "; Actual: " + autoCompleteBehavior._selectIndex);
    };
}

function verifyLastWordStartsWith(delimiters, lastWordPrefix, autoCompleteList) {
    return function() {
        var children = autoCompleteList.childNodes;
        if (children.length > 0) {
            for (var i = 0; i < children.length; i++) {
                var child = children[i];
                var match = child.innerHTML;
                var tokens = match.split(delimiters);
                var lastToken = tokens[tokens.length - 1];
                testHarness.assertEqual(lastToken.indexOf(lastWordPrefix), 0, "The last word should start with text: " + lastWordPrefix + " Actual: " + lastToken);
            }
        }
    };
}

function verifyValues(autoCompleteList) {
    return function() {
        var children = autoCompleteList.childNodes;
        for (var i = 0; i < children.length; i++) {
            testHarness.assertTrue(i == children[i]._value, "The value of child " + i + " should be '" + i + "' instead of '" + children[i]._value + "'");
        }
    };
}
