﻿/**
* Copyright (c) 2009 Web Revolution s. r. o.
*/

var Widgets = {
    // Submit survey vote via AJAX
    surveyVote: function(_blockId, _surveyId, _voteId, _ctrlHash) {
        var _langId = __langId;
        $.ajax({
            type: "POST",
            url: "/Services/Widgets.asmx/SurveyVote",
            data: "{langId: '" + _langId + "', surveyId: '" + _surveyId + "', voteId: '" + _voteId + "', ctrlHash: '" + _ctrlHash + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                $("#survey_" + _blockId).html(msg.d);
            }
        });
    },

    // Format price according to global value __priceFormat
    formatPrice: function(value) {
        var _priceFormat = __priceFormat;
        var result = "";
        value = parseFloat(value);

        for (var i = 0; i < _priceFormat.length; i++) {
            if (_priceFormat.charAt(i) == '#') {
                result += this.formatNumber(value);
            }
            else if (_priceFormat.charAt(i) == ',' || _priceFormat.charAt(i) == '.') {
                var cnt = 0;
                var fPart = value - value.toFixed(0);
                for (i++; i < _priceFormat.length && _priceFormat.charAt(i) == '0'; i++) {
                    cnt++;
                }
                i--;
                if (fPart > 0.0) {
                    result += _priceFormat.charAt(i) + (fPart.toFixed(cnt) + "");
                }
            } else {
            result += _priceFormat.charAt(i);
            }
        }

        return result;
    },

    // Format number
    formatNumber: function(value) {
        var nStr = value + "";
        x = nStr.split('.');
        x1 = x[0];
        x2 = x.length > 1 ? '.' + x[1] : "";
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ' ' + '$2');
        }
        return x1 + x2;
    },
    
    // Parse date in format dd.MM.yyyy
    parseDate: function(value) {
        var nums = value.split(".");
        if (nums.length == 3) {
            var year = parseInt(nums[2]);
            var month = parseInt(nums[1].charAt(0) == "0" ? nums[1].substr(1, nums[1]) : nums[1]);
            var day = parseInt(nums[0].charAt(0) == "0" ? nums[0].substr(1, nums[0]) : nums[0]);

            if (!isNaN(year) && !isNaN(month) && !isNaN(day)) {
                var d = new Date();
                d.setFullYear(year, month - 1, day);

                return d;
            }
        }
        return null;
    }
}

jQuery.fn.DefaultValue = function(text) {
    return this.each(function() {
        //Make sure we're dealing with text-based form fields
        if (this.type != 'text' && this.type != 'password' && this.type != 'textarea')
            return;

        //Store field reference
        var fld_current = this;

        //Set value initially if none are specified
        if (this.value == '') {
            this.value = text;
        } else {
            //Other value exists - ignore
            return;
        }

        //Remove values on focus
        $(this).focus(function() {
            if (this.value == text || this.value == '')
                this.value = '';
        });

        //Place values back on blur
        $(this).blur(function() {
            if (this.value == text || this.value == '')
                this.value = text;
        });

        //Capture parent form submission
        //Remove field values that are still default
        $(this).parents("form").each(function() {
            //Bind parent form submit
            $(this).submit(function() {
                if (fld_current.value == text) {
                    fld_current.value = '';
                }
            });
        });
    });
};