﻿function GetBaseUrl() {
    return $("#BaseUrlHiddenField").val();
}

function MaskElement(elementId, format) {
    $("#" + elementId).mask(format);
}

function StripResponse(response) {
    response = response.substring(response.indexOf('<div id="JQueryPage"'));
    response = response.replace('</form></body></html>', '');
    return response;
}

function ShowResults(divControlNameToShow) {
    var currentDate = new Date();
    var dummy = currentDate.getTime();

    $("div#" + divControlNameToShow).html("<img class=\"loadingImage\" src=\"" + GetBaseUrl() + "/infomat/images/ajax-loader.gif\" alt=\"Loading...\" /> De resultaten worden opgehaald").show();

    $.get(GetBaseUrl() + "/infomat/modules/Competition_results.aspx?dummy=" + dummy, function(response) {
        $("div#" + divControlNameToShow).html(StripResponse(response)).show();
    });
}

function GetSelectedvalueFromDropDownList(dropDownList) {
    var returnValue = dropDownList.options[dropDownList.selectedIndex].value;
    return returnValue;
}

function SetMatchDate(dateFromElement, dateToElement, seasonDropDownListId, allSeasonDatesElementId, lastWeekendDatesElementId, mode) {
    if (mode + "" == "FullSeason") {
        var allSeasonDatesDropDownList = document.getElementById(allSeasonDatesElementId);
        var seasonDropDownList = document.getElementById(seasonDropDownListId);

        var selectedSeasonId = "-1";
        selectedSeasonId = GetSelectedvalueFromDropDownList(seasonDropDownList);

        for (i = 0; i < allSeasonDatesDropDownList.options.length; i++) {
            var seasonDateSplitter = allSeasonDatesDropDownList.options[i].value.split("|")

            if (seasonDateSplitter[0] + "" == selectedSeasonId) {
                $("#" + dateFromElement).val(seasonDateSplitter[1]);
                $("#" + dateToElement).val(seasonDateSplitter[2]);
            }
        }
    }
    else {
        if (mode + "" == "LastWeekend") {
            $("#" + dateFromElement).val($("#" + lastWeekendDatesElementId).val().split('|')[0]);
            $("#" + dateToElement).val($("#" + lastWeekendDatesElementId).val().split('|')[1]);
        }
    }
}

function ShowRankings(divControlNameToShow) {
    var currentDate = new Date();
    var dummy = currentDate.getTime();

    $("div#" + divControlNameToShow).html("<img class=\"loadingImage\" src=\"" + GetBaseUrl() + "/infomat/images/ajax-loader.gif\" alt=\"Loading...\" /> De klassementen worden opgehaald").show();

    $.get(GetBaseUrl() + "/infomat/modules/Competition_rankings.aspx?show=normal&dummy=" + dummy, function(response) {
        $("div#" + divControlNameToShow).html(StripResponse(response)).show();
    });
}

function ShowRankingsReserve(divControlNameToShow) {

    var currentDate = new Date();
    var dummy = currentDate.getTime();

    $("div#" + divControlNameToShow).html("<img class=\"loadingImage\" src=\"" + GetBaseUrl() + "/infomat/images/ajax-loader.gif\" alt=\"Loading...\" /> De klassementen worden opgehaald").show();

    $.get(GetBaseUrl() + "/infomat/modules/Competition_rankings.aspx?show=reserve&dummy=" + dummy, function(response) {
        $("div#" + divControlNameToShow).html(StripResponse(response)).show();
    });

}

function IsNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }

    return IsNumber;
}

function AutoCompleteClubTextBox(TextBoxControlId, TextBoxWidth) {
    var currentDate = new Date();
    var dummy = currentDate.getTime();

    $().ready(function() {
        $("#" + TextBoxControlId).flushCache();
        var fullpath = GetBaseUrl() + '/Infomat/Modules/GetClubsForAutocomplete.aspx?dummy=' + dummy;

        $.get(fullpath, function(response) {
            var dataArray = response.split("||");

            $("#" + TextBoxControlId).autocomplete(dataArray, {
                matchContains: true,
                minChars: 1,
                mustMatch: false,
                max: 50,
                width: TextBoxWidth
            });
        });
    });
}

function AutoCompleteCityTextBox(TextBoxControlId) {
    $().ready(function() {
        $("#" + TextBoxControlId).flushCache();
        var fullpath = GetBaseUrl() + '/Infomat/Modules/GetCitiesForAutocomplete.aspx';

        $.get(fullpath, function(response) {
            var dataArray = response.split("||");
            $("#" + TextBoxControlId).autocomplete(dataArray, {
                matchContains: true,
                minChars: 1,
                mustMatch: false,
                max: 50
            });
        });
    });
}

var digits = "0123456789";
var phoneNumberDelimiters = "()\/-. ";

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function trim(s) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function CheckPhoneNumber10(sender, args) {
    var strPhone = args.Value;
    strPhone = trim(strPhone)

    s = stripCharsInBag(strPhone, phoneNumberDelimiters);

    var validPhoneNumber = (isInteger(s) && s.length >= 10);
    if (validPhoneNumber != true) {
        args.IsValid = false;
    }

    return;
}

function CheckPhoneNumber9(sender, args) {
    var strPhone = args.Value;
    strPhone = trim(strPhone)

    s = stripCharsInBag(strPhone, phoneNumberDelimiters);

    var validPhoneNumber = (isInteger(s) && s.length >= 9);
    if (validPhoneNumber != true) {
        args.IsValid = false;
    }

    return;
}

function saveScroll() {
    var sScroll;
    if (document.documentElement && document.documentElement.scrollTop)
        sScroll = document.documentElement.scrollTop;
    else if (document.body)
        sScroll = document.body.scrollTop;
    else {
        sScroll = 0;
    }

    $(".ScrollPositionTextBox").val(sScroll);
}

function restoreScroll() {
    var sScroll = $(".ScrollPositionTextBox").val();
    if (sScroll > 0) {
        if (document.documentElement && document.documentElement.scrollTop)
            document.documentElement.scrollTop = sScroll;
        else if (document.body) {
            if (window.navigator.appName == 'Netscape')
                window.scroll(0, sScroll);
            else
                document.body.scrollTop = sScroll;
        }
        else {
            window.scroll(0, sScroll);
        }
        // here is setting absolute positioning panel, if you need, set correct ID  and uncomment follow 2 lines (and add needed lines/setting for all your panels)
        //if (document.getElementById('pnlNewItem') != null )
        //document.getElementById('pnlNewItem').style.top = sScroll + 'px';
    }
}








