﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

var m_logging = false;
var m_logged = false;

$(document).ready(function() {
    $("#divDateOfBirth input").one("focus", function() { $(this).val(""); });
    if ($("[id$=m_hidGender]").val() == "female")
    {
        selectGender(false);
    }
    else if ($("[id$=m_hidGender]").val() == "male")
    {
        selectGender(true);
    }
    
    $('[id$=spanLogin]').bind('click', openLogin);
    $('[id$=spanLogOut]').click(logout);
    $('#formLogin').submit(login);
    $('#inSubmit').removeAttr('onclick');
});

var cultivars = {
    less: function(){
        $(this).hide()
            .prev().show()
            .prev().find('li.cul-min').hide();
        sliderInit.destroy();
        sliderInit.vertical();
    },
    more: function(){
        $(this).hide()
            .next().show()
            .prev().prev().find('li.cul-min').show();
        sliderInit.destroy();
        sliderInit.vertical();
    }
};

function openLogin(e) {
    if ($('.log .shadow-box').css('display') == 'none') {
        $('.log .shadow-box').show();
        $('.log .shadow-box-back').show();
        $('[id$=spanLogin]').addClass('log-on')
            .unbind('click', openLogin)
            .bind('click', closeLogin);
        return false;
    }
};
function closeLogin(e) {
    if ($('.log .shadow-box').css('display') != 'none') {
        $('.log .shadow-box').hide();
        $('.log .shadow-box-back').hide();
        $('[id$=spanLogin]').removeClass('log-on')
            .unbind('click', closeLogin)
            .bind('click', openLogin);
        //return false;
    }
};

function login() {
    if (m_logged) {
        return;
    }
    if (!m_logging) {
        m_logging = true;
        $.ajax({
            type: "POST",
            url: "/Login.ashx",
            dataType: "json",
            success: onLogSuccess,
            error: onErrorResponse,
            complete: function() { m_logging = false; },
            data: {
                type: "login",
                login: $('#userLogin').val(),
                password: MD5($('input#password').val()),
                remember: $('#remember')[0].checked
            }
        });
        $('#inPassword').val("");
    }
    return false;
};

function onLogSuccess(response, statusText) {
    if (response != null) {
        if (response.ok) {
            m_logged = true;
            $('#formLogin').submit();
        }
        else {
            stat.showError(response.status);
        }
    }
};

function logout() {
    if (!m_logging) {
        m_logging = true;
        $.ajax({
            type: "POST",
            url: "/Login.ashx",
            dataType: "json",
            success: function(){ location.reload();},
            error: onErrorResponse,
            complete: function() { m_logging = false; },
            data: {
                type: "logout"
            }
        });
    }
    return false;
};

function loadingIcon(container, show) {
    if (show) {
        container.append("<div class='loading' style='height:"
    + container.height() + "px; width:"
    + container.width() + "px'></div>");
    }
    else {
        container.find('.loading').remove();
    }
};

var search = {
    itemsPerPage : 11,
    wrapper: null,
    nextButton: null,
    optionsKeys: null,
    optionsValues: null,
    prevButton: null,
    page: 0,
    position: 0,
    count: 0,
    countReceived: 0,
    loading: false,
    clear: function(){
        if(!search.loading){
            $('.sea-pnl .sea-opt-ch').removeClass('sea-opt-ch');
            $('.sea-opts').hide();
            $('#seaQuery').val('');
            search.page = 0; 
            if (search.setOptions()){
                search.getResults();
                $('#seaClear').hide();
            }
        }         
    },
    getResults: function() {
        if (!search.loading) {
            search.loading = true;
            loadingIcon(search.wrapper.parent(), true);
            var data={
                type: 'search',
                query: $('input.sea-query').val(),
                keys: search.optionsKeys,
                page: search.page
            };
            if (search.optionsKeys != null){
                for(var i=0; i < search.optionsKeys.length; i++){
                    data[search.optionsKeys[i]] = search.optionsValues[i];
                }
            }
            search.setClearVisibility();
            $.ajax({
                type: "POST",
                url: './Actions.ashx',
                dataType: "json",
                success: search.onSearchSuccess,
                error: onErrorResponse,
                complete: search.onSearchComplete,
                data: data
            });
        }
    },
    hideInfoCallback: function(eventObject) {
        search.hiding = false;
        $(this).parent().css("z-index", "0");
        if (search.nextToShow != null) {
            search.showWineInfo(search.nextToShow);
            search.nextToShow = null;
        }
    },
    hideOpts: function(eventObject) {
        if ($(this).find('.sea-opt-ch').length == 0) {
            $(this).find('.sea-opts').hide();
        }
    },
    hiding: false,
    init: function(a_count, a_countReceived) {   
        $('.sea-opt').click(function() {
            if(!search.loading){
                $(this).toggleClass('sea-opt-ch');
                search.page = 0; 
                if (search.setOptions()){
                    search.getResults();
                }
            } 
        });

        $('.sea-param').hover(search.showOpts, search.hideOpts);
        $('#seaClear').click(search.clear);

        $('input[id=seaSubmit]').click(function() { 
            search.page = 0; 
            if(search.setOptions()){
                search.getResults(); 
            }
        });
        $("input[id=seaQuery]").keypress(function(e) {
            if (e.which == 13) {
                $('input[id=seaSubmit]').click();
                return false;
            }
        });

        search.prevButton = $('.sea-res-l').click(search.prevPage);
        search.nextButton = $('.sea-res-r').click(search.nextPage);
        search.wrapper = $('.sea-res-wra');

        $('.sea-opts').each(function() {
            var optHeight = 15;
            var maxOptsToView = 10;
             var container = $(this).children('.sli-cont');
            var wrapper = container.children('.sli-wra');
            var countOpts = wrapper.children().length;
            if (countOpts > maxOptsToView) {
                container.css('height', maxOptsToView * optHeight + 'px');
                sliderInit.vertical(container, wrapper, maxOptsToView * optHeight, countOpts * optHeight);
            }
        });
        
        if(search.setOptionsFromUrl()){
            search.getResults();
        }
        else{
            search.count = a_count;
            search.countReceived = a_countReceived;
            search.initResults();
        }
    },
    initResults: function() {
        $('.sea-res .wine').unbind().hover(search.wineIn, search.wineOut);
    },
    nextToShow: null,
    nextPage: function() {
        if (!search.loading) {
            search.page += 1;
            if (search.position + search.itemsPerPage * 2 > search.countReceived && search.countReceived < search.count) {
                search.getResults();
            }
            else {
                var diff = search.count - search.position;
                if (diff < search.itemsPerPage * 2 + 1) {
                    search.nextButton.hide();
                }
                if (diff > search.itemsPerPage) {
                    if (diff > search.itemsPerPage * 2) {
                        diff = search.itemsPerPage;
                    }
                    else {
                        diff = diff - search.itemsPerPage;
                    }
                }
                search.position += diff;
                search.marginDefault -= diff * 78;
                search.wrapper.animate({ marginLeft: search.marginDefault }, 2000 * (Math.abs(diff) / search.itemsPerPage));
                search.prevButton.show();
            }
        }
    },
    onSearchComplete: function() {
        search.loading = false;
        loadingIcon(search.wrapper.parent(), false);
    },
    onSearchSuccess: function(response, statusText) {
        if (response.ok) {
            search.count = response.count;
            if (search.page == null || search.page == 0) {
                search.page = 0;
                search.countReceived = response.countReceived;
                search.wrapper.css('width', ((search.countReceived + 3) * 78) + search.itemsPerPage + 'px');
                search.wrapper.html(response.results);
                search.position = 0;
                search.prevButton.hide();
                search.marginDefault = 0;
                search.wrapper.css('margin-left', search.marginDefault);
            }
            else {
                search.countReceived += response.countReceived;
                search.wrapper.css('width', ((search.countReceived + 3) * 78) + search.itemsPerPage + 'px');
                search.wrapper.append(response.results);
                search.position += response.countReceived;
                search.marginDefault = -search.position * 78;
                search.prevButton.show();
                search.wrapper.animate({ marginLeft: (search.marginDefault) }, 2000 * (response.countReceived / search.itemsPerPage));
            }

            if (search.position + search.itemsPerPage >= search.count) {
                search.nextButton.hide();
            }
            else {
                search.nextButton.show();
            }

            search.initResults();
        }
        else {
            stat.showError(response.status);
        }
    },
    prevPage: function() {
        if (!search.loading) {
            search.page -= 1;
            var diff = 0;
            if (search.position <= search.itemsPerPage) {
                diff = search.position;
                search.prevButton.hide();
            }
            else {
                diff = search.itemsPerPage;
            }
            search.position -= diff;
            search.marginDefault += diff * 78;
            search.wrapper.animate({ marginLeft: search.marginDefault }, 2000 * (Math.abs(diff) / search.itemsPerPage));
            search.nextButton.show();
        }
    },
    setClearVisibility: function(){
        var query = $('#seaQuery').val();
        if(search.optionsKeys != null && search.optionsKeys.length > 0 || (query != null && query.length > 0)){
            $('#seaClear').show();
        }
        else{
            $('#seaClear').hide();
        }
    },
    setOptions: function(){
        var options = $.map($('.sea-pnl .sea-opt-ch'), function(opt) {
            return opt.id;
        });
        
        search.optionsKeys = new Array();
        search.optionsValues = new Array();
        var option;
        var index;
        var values;
        for(var i=0; i < options.length; i++){
            option = options[i].split('_');
            index = null;
            
            for(var j=0; j < search.optionsKeys.length; j++){
                if(search.optionsKeys[j] == option[0]){
                    index = j;
                }
            }
            if(index == null) {
                search.optionsKeys.push(option[0]);
                values = new Array();
                search.optionsValues.push(values);
            }
            else{
                values = search.optionsValues[index];
            }
            values.push(option[1]);
        }
        
        var params = '';
        for(var i=0; i < search.optionsKeys.length; i++){
            if(i > 0){
                params += '&';
            }
            params += search.optionsKeys[i];
            values = search.optionsValues[i];
            for(var j=0; j < values.length; j++){
                if (j == 0){
                    params += '=';
                }
                else{
                    params += '+';
                }
                params += values[j];
            }
        }
        var query = $('#seaQuery').val();
        if(query != null && query.length > 0){
            if(params.length > 0){
                params += '&';
            }
            params += "q=" + query;
        }
        
        var match = window.location.href.match("our-wines(/.+)");
        if(match != null){
            window.location.href = window.location.href.replace(match[1], '#' + params);
            return false;
        }
        else{
            window.location.hash = params;
            return true;
        }
    },
    setOptionsFromUrl: function(){
        var bWere = false;
        search.optionsKeys = new Array();
        search.optionsValues = new Array();
        var match = window.location.href.match("/our-wines(/.+)");
        var options;
        if(match != null){
            options = match[1];
        }
        var bSearch = options != null && options.length > 2;
        if(!bSearch){
            options = window.location.hash;
        }
        if(options != null && options.length > 0){
            options = options.substr(1, options.length-1);
            if(bSearch){
                options = options.split('_');
            }
            else{
                options = options.split('&');
            }
            for(var i=0; i < options.length; i++){
                var pair = options[i];
                if(bSearch){
                    pair = pair.split('-');
                }
                else{
                    pair = pair.split('=');
                }
                if(pair.length == 2){
                    if(!bSearch){
                        bWere = true;
                    }
                    var key = pair[0];
                    var values = pair[1];
                    if(bSearch){
                        values = values.split(',');
                    }
                    else{
                        values = values.split('+');
                    }
                    if(key != 'q'){
                        var valuesToAdd = new Array();
                        var optsOpened = false;
                        for(var j=0; j< values.length; j++){
                            var option = $('#' + key + '_' + values[j]);
                            if (option.length == 1){
                                option.addClass('sea-opt-ch');
                                valuesToAdd.push(values[j]);
                                if(!optsOpened){
                                    optsOpened = true;
                                    option.parents('.sea-opts').show();
                                }
                            }
                        }
                        if(valuesToAdd.length > 0){
                            search.optionsKeys.push(key);
                            search.optionsValues.push(valuesToAdd);
                        }
                    }
                    else {
                        $('#seaQuery').val(values);
                    }
                }
            }
        }
        return bWere;
    },
    showWineInfo: function(wine) {
        var prevCount = wine.prevAll().length;
        var desc = wine.children('.wine-desc');
        wine.css("z-index", "1");
        if (prevCount - search.position > 6) {
            //var winesToHide = wine.prevAll(':visible:gt(' + (prevCount - 4) + ')');
            //search.wrapper.animate({ marginLeft: (search.marginDefault - winesToHide.length * 78) }, 900);
            desc.css('left', 'auto').css('right', '74px');
        }
        else{
            desc.css('right', 'auto').css('left', '74px');
        }
        //wine.animate({ width: '296' }, 900);
        desc.animate({ width: 'show' }, 950, function(){
                var container = wine.find('.wine-desc-wra .sli-cont');
                var wrapper = container.children('.sli-wra');
                sliderInit.vertical(container, wrapper);
            });
    },
    showOpts: function(eventObject) {
        $(this).find('.sea-opts').show();
    },
    marginDefault: 0,
    wineIn: function(eventObject) {
        var wine = $(this);
        if (!search.hiding) {
            search.showWineInfo(wine);
        }
        else {
            search.nextToShow = wine;
        }
    },
    wineOut: function(eventObject) {
        search.hiding = true;
        var wine = $(this);
        if (search.nextToShow != null && search.nextToShow[0] == this) {
            search.nextToShow = null;
        }
        else {
            //search.wrapper.animate({ marginLeft: search.marginDefault }, 900);
            //wine.children('.wine-desc').animate({ width: 'hide' }, 850);
            //wine.animate({ width: '74' }, 900, search.hideInfoCallback);
            
            wine.children('.wine-desc').animate({ width: 'hide' }, 850, search.hideInfoCallback)
        }
    }
};

var sliderInit = {
    destroy: function(){
        var sli = $('.sli');
        sli.prev().children().css('margin-top', '0');
        sli.slider('destroy')
            
    },
    vertical: function(a_container, a_wrapper, a_contHeight, a_wraHeight) {
        var wrapper = a_wrapper;
        var container = a_container;

        if (wrapper == null) {
            wrapper = $('#sliWra');
        }
        if (container == null) {
            container = $('#sliCont');
        }

        var wraHeight = a_wraHeight;
        if (wraHeight == null) {
            wraHeight = wrapper.height();
        }
        var contHeight = a_contHeight;
        if (contHeight == null) {
            contHeight = container.height();
        }
        var diff = wraHeight - contHeight;
        if (diff > 0) {
            var sli = container.next('.sli');
            if (sli.length > 0) {
                wrapper.css('margin-top', '0');
                sli.slider('destroy');
            }
            else{
                var contMargin = container.css('margin-top');
                if (contMargin != null){
                    contMargin = parseInt(contMargin.substr(0, contMargin.length - 2));
                }
                else{
                    contMargin = 0;
                }
                sli = container.after("<div class='sli'></div>")
                    .next('.sli')
                    .css('height', contHeight - 12 + 'px')
                    .css('margin-top', contMargin + 6 + 'px')
                    .css('margin-bottom', '6px');
            }
            container.addClass('sli-vis');
            sli.slider({
                orientation: "vertical",
                min: -diff,
                max: 0,
                slide: function(event, ui) {
                    wrapper.css('margin-top', ui.value + 'px');
                }
            });
        }
    }
}

function onErrorResponse(xhr, textStatus, errorThrown) {
    stat.showError(textStatus);
};

function selectGender(a_bGender) {
    if (a_bGender == true) {
        if ($("[id$=m_labMale]").hasClass("select")) {
            $("[id$=m_labMale]").removeClass("select");
            $("[id$=m_hidGender]").val("");
        }
        else {
            $("[id$=m_labMale]").attr("class", "select");
            $("[id$=m_hidGender]").val("male");
        }
        $("[id$=m_labFemale]").removeClass("select");
    }
    else {
        if ($("[id$=m_labFemale]").hasClass("select")) {
            $("[id$=m_labFemale]").removeClass("select");
            $("[id$=m_hidGender]").val("");
        }
        else {
            $("[id$=m_labFemale]").attr("class", "select");
            $("[id$=m_hidGender]").val("female");
        }
        $("[id$=m_labMale]").removeClass("select");
    }
}

function validateContact()
{
    var name = jQuery.trim($("[id$=m_inName]").val());
    var nameInfo = $("[id$=m_spanWrongName]");
    var company = jQuery.trim($("[id$=m_inCompany]").val());
    var companyInfo = $("[id$=m_spanWrongCompany]");
    var subject = jQuery.trim($("[id$=m_inSubject]").val());
    var subjectInfo = $("[id$=m_spanWrongSubject]");
    var email = jQuery.trim($("[id$=m_inEmail]").val());
    var emailInfo = $("[id$=m_spanWrongEmail]");
    var desc = jQuery.trim($("[id$=m_taDesc]").val());
    var descInfo = $("[id$=m_spanWrongDesc]");
    
    var bReturn = true;
    if (email.length < 1)
    {
        emailInfo.text("Musisz podać email!");
        bReturn = false;  
    }
    else if(!isValidEmailAddress(email)){  
         emailInfo.text("Niepoprawny email!");
         bReturn = false;  
    }
    else
    {
        emailInfo.text("");  
    }  
    
    if (name.length > 255)
    {
        nameInfo.text("Nazwa użytkownika może mieć maksymalnie 255 znaków");
        bReturn = false;  
    }
    else
    {
        nameInfo.text("");
    }
    
    if (subject.length < 1)
    {
        subjectInfo.text("Musisz podać temat");
        bReturn = false;  
    }
    else if(subject.length > 255){  
        subjectInfo.text("Temat może mieć maksymalnie 255 znaków");
        bReturn = false;  
    }
    else
    {
        subjectInfo.text("");  
    }  
    
    if (desc.length < 1)
    {
        descInfo.text("Musisz podać opis");
        bReturn = false;  
    }
    else if(subject.length > 255){  
        descInfo.text("Opis może mieć maksymalnie 2000 znaków");
        bReturn = false;  
    }
    else
    {
        descInfo.text("");  
    }  
    
    if (company.length > 255)
    {
        companyInfo.text("Nazwa firmy może mieć maksymalnie 255 znaków");
        bReturn = false;  
    }
    else
    {
        companyInfo.text("");
    }
    
    return bReturn;
}

function validateRegister()
{
    var firstName = jQuery.trim($("[id$=m_inFirstName]").val());
    var firstNameInfo = $("[id$=m_spanWrongFirstName]");
    var secondName = jQuery.trim($("[id$=m_inSecondName]").val());
    var secondNameInfo = $("[id$=m_spanWrongSecondName]");
    var email = jQuery.trim($("[id$=m_inEmail]").val());
    var emailInfo = $("[id$=m_spanWrongEmail]");
    var year = jQuery.trim($("[id$=m_inYear]").val());
    var month = jQuery.trim($("[id$=m_inMonth]").val());
    var day = jQuery.trim($("[id$=m_inDay]").val());
    var dateOfBirthInfo = $("[id$=m_spanWrongDate]");
    var male = $("[id$=m_labMale]");
    var female = $("[id$=m_labFemale]");
    var phone = jQuery.trim($("[id$=m_inPhone]").val());
    var phoneInfo = $("[id$=m_spanWrongPhone]");
    var comName = jQuery.trim($("[id$=m_inComName]").val());
    var comNameInfo = $("[id$=m_spanWrongComName]");
    var pass = jQuery.trim($("[id$=m_inPassword]").val());
    var pass2 = jQuery.trim($("[id$=m_inPassword2]").val());
    var passInfo = $("[id$=m_spanWrongPass]");
    var oldPass = $("[id$=m_inOldPass]").val();
    var oldPassInfo = $("[id$=m_spanWrongOldPass]");
    var gender = jQuery.trim($("[id$=m_hidGender]").val());
    var genderInfo = $("[id$=m_spanWrongGender]");
    var chbAccept = $("[id$=m_chbAccept]");
    var acceptInfo = $("[id$=m_spanNoAcccept]");
    
    var sStreet = $("[id$=m_inStreet]").val();
    var sPostCode = $("[id$=m_inPostCode]").val();
    var sCountry = $("[id$=m_inCountry]").val();
    var sRegion = $("[id$=m_inRegion]").val();
    var sCity = $("[id$=m_inCity]").val();
    var sMobile = $("[id$=m_inMobile]").val();
    var sNip = $("[id$=m_inNIP]").val();
    
    var bReturn = true;
    
    if (comName.length > 1 && sNip.length < 1 )
    {
        $("[id$=m_spanWrongNip]").text("Jeśli podaleś firmę musisz podać nip");
        bReturn = false;
    }
    else 
    {
       $("[id$=m_spanWrongNip]").text("");
    }
    if(sNip.length > 1 && sNip.length !=  10)
    {
        $("[id$=m_spanWrongNip]").text("Nieprawidłowy numer NIP");
        bReturn = false;
    }
    else 
    {
       $("[id$=m_spanWrongNip]").text("");
    }
    
    if(sMobile.length > 100)
    {
        $("[id$=m_spanWrongMobile]").text("Pole może mieć maksymalnie 100 znaków");
        bReturn = false;
    }
    else 
    {
       $("[id$=m_spanWrongMobile]").text("");
    }
    
    if (sStreet.length < 1)
    {
        $("[id$=m_spanWrongStreet]").text("Musisz podać ulicę!");
        bReturn = false;  
    }
    else if(sStreet.length > 100)
    {
        $("[id$=m_spanWrongStreet]").text("Pole może mieć maksymalnie 100 znaków");
        bReturn = false;  
    }
    else 
    {
       $("[id$=m_spanWrongStreet]").text("");
    }
    
    if (sPostCode.length < 1)
    {
        $("[id$=m_spanWrongPostCode]").text("Musisz podać kod pocztowy!");
        bReturn = false;  
    }
    else if(sPostCode.length > 100)
    {
        $("[id$=m_spanWrongPostCode]").text("Pole może mieć maksymalnie 100 znaków");
        bReturn = false;  
    }
    else 
    {
       $("[id$=m_spanWrongPostCode]").text("");
    }
    
    if (sCountry.length < 1)
    {
        $("[id$=m_spanWrongCountry]").text("Musisz podać kraj!");
        bReturn = false;  
    }
    else if(sCountry.length > 100)
    {
        $("[id$=m_spanWrongCountry]").text("Pole może mieć maksymalnie 100 znaków");
        bReturn = false;  
    }
    else 
    {
       $("[id$=m_spanWrongCountry]").text("");
    }
    
    if (sRegion.length < 1)
    {
        $("[id$=m_spanWrongRegion]").text("Musisz podać region!");
        bReturn = false;  
    }
    else if(sRegion.length > 100)
    {
        $("[id$=m_spanWrongRegion]").text("Pole może mieć maksymalnie 100 znaków");
        bReturn = false;  
    }
    else 
    {
       $("[id$=m_spanWrongRegion]").text("");
    }
    
    if (sCity.length < 1)
    {
        $("[id$=m_spanWrongCity]").text("Musisz podać miasto!");
        bReturn = false;  
    }
    else if(sCity.length > 100)
    {
        $("[id$=m_spanWrongCity]").text("Pole może mieć maksymalnie 100 znaków");
        bReturn = false;  
    }
    else 
    {
       $("[id$=m_spanWrongCity]").text("");
    }
    
    
    
    if (email.length < 1)
    {
        emailInfo.text("Musisz podać email!");
        bReturn = false;  
    }
    else if(!isValidEmailAddress(email)){  
         emailInfo.text("Niepoprawny email!");
         bReturn = false;  
    }
    else
    {
        emailInfo.text("");  
    }  
    
    if (firstName.length < 1)
    {
        firstNameInfo.text("Musisz podać imię!");
        bReturn = false;  
    }
    else if(firstName.length > 100)
    {
        firstNameInfo.text("Imię może mieć maksymalnie 100 znaków");
        bReturn = false;  
    }
    else
    {
        firstNameInfo.text("");
    }
    
    if (secondName.length < 1)
    {
        secondNameInfo.text("Musisz podać nazwisko!");
        bReturn = false;
    }
    else if(secondName.length > 100)
    {
        secondNameInfo.text("Nazwisko może mieć maksymalnie 100 znaków");
        bReturn = false;  
    }
    else
    {
        secondNameInfo.text("");
    }
    
    if ($('[id$=m_divPass2]').is(':visible'))
    {
        if (pass.length < 1)
        {
            passInfo.text("Musisz podać hasło!");
            bReturn = false;
        }
        else if(!isValidPassword(pass))
        {
            passInfo.text("Niepoprawne hasło!");
            bReturn = false;
        }
        else if(pass != pass2)
        {
            passInfo.text("Hasła muszą być identyczne!");
            bReturn = false;
        }
        else
        {
            passInfo.text("");
        }
        if($('[id$=m_divOldPass]').is(':visible'))
        {
            if (oldPass.length < 1)
            {
                oldPassInfo.text("Musisz podać stare hasło!");
                bReturn = false;
            }
            else
            {
                oldPassInfo.text("");
            }
        }
    }
    

    
    if(comName.length > 100)
    {
        comNameInfo.text("Nazwa może mieć maksymalnie 100 znaków");
        bReturn = false;  
    }
    else
    {
        comNameInfo.text("");
    }
    
    if(phone.length < 1)
    {
        phoneInfo.text("Musisz podać numer telefonu!");
        bReturn = false;
    }
    else if(phone.length > 100)
    {
        phoneInfo.text("Telefon może mieć maksymalnie 100 znaków");
        bReturn = false;
    }
    else
    {
        phoneInfo.text("");
    }
    
    if (gender.length < 1)
    {
        genderInfo.text("Musisz wybrać płeć!");
        bReturn = false;
    }
    else
    {
        genderInfo.text("");
    }
    
    if ((day.length < 1 || day == "dzień") && (month.length < 1 || month == "miesiąc") && (year.length < 1 || year == "rok"))
    {
        dateOfBirthInfo.text("Musisz podać datę urodzenia!");
        bReturn = false;
    }
    else
    {
        dateOfBirthInfo.text("");
    }
    
    
    if(chbAccept.length > 0 && !(chbAccept.attr('checked')))
    {
        acceptInfo.text("Musisz zaakceptować regulamin!");
        bReturn = false;
    }
    else
    {
        acceptInfo.text("");
    }

    return bReturn;
}

function isValidPassword(password)
{
    var pattern = new RegExp(/^[0-9a-zA-Z]{6,12}$/);
    return pattern.test(password);
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^([\w.])([\w-\.])+\@(([\w-])+\.)[a-z]{2,}/);
	return pattern.test(emailAddress);
}  

var autoRequest = {
    timer: null,
    start: function() {
        autoRequest.timer = setTimeout('autoRequest.doSomething();', 60000);
    },
    stop: function() {
        clearTimeout(autoRequest.timer);
    },
    doSomething: function() {
        $.ajax({
            type: "POST",
            url: "/Default.aspx?auto=true",
            success: autoRequest.success,
            error: autoRequest.error
        });
    },
    success: function(response, statusText) {
        autoRequest.start();
    },
    error: function(xhr, textStatus, errorThrown) {
        autoRequest.start();
    }
};

var stat =
{
    box: $('#statBox'),
    title: $('#statTitle'),
    value: $('#statValue'),
    defaultErrorText: '',
    errorText: '',
    infoText: '',
    hide: function() {
        stat.box.hide();
    },
    init: function(infoTitle, errorTitle, defaultErrorText) {
        stat.infoText = infoTitle;
        stat.errorText = errorTitle;
        stat.defaultErrorText = defaultErrorText;
        stat.box.click(function() { stat.hide(); });
    },
    show: function() {
        stat.box.css("left", ($(window).width() - stat.box.width()) / 2 + $(window).scrollLeft() + "px");
        stat.box.show();
    },
    showError: function(txt) {
        stat.box.removeClass('stat-info').removeClass('stat-error');
        stat.box.addClass('stat-error');
        stat.title.text(stat.errorText);
        if (txt != null) {
            stat.value.text(txt);
        }
        else {
            stat.value.text(stat.defaultErrorText);
        }
        stat.show();
    },
    showInfo: function(txt) {
        stat.box.removeClass('stat-info').removeClass('stat-error');
        stat.box.addClass('stat-info');
        stat.title.text(stat.infoText);
        stat.value.text(txt);
        stat.show();
    }
}
