﻿/// <reference path="jquery-1.4.4.js" />

function gup(name, href) {
    
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(href);
    if (results == null)
        return "";
    else
        return results[1];
}

$.fn.trzRotator = function (options) {
    var defaults = {
        delay: 5000
    };
    var opts = $.extend({}, defaults, options);
    var ul = this;
    var timer = setInterval(function () {
        $(ul).find('li:last').fadeOut(function () {
            $(this).show().insertBefore($(ul).find('li:first'));
        });
    }, opts.delay);
}

$.fn.trzWatermark = function () {
    var input = $(this);
    var text = input.attr('title');
    //if (input.val().length != 0) {
        input.css('color', 'gray').val(text);
    //}
    input.bind('focus blur', function (evt) {
        switch (evt.type) {
            case 'focus':
                if (this.value == text) {
                    $(this).val('').css({ 'color': 'black' });
                }
                break;
            case 'blur':
                if (this.value == '') {
                    this.value = text;
                    $(this).css({ 'color': 'gray' });
                }
                break;
        }
    });
}

$.fn.trzOnBlur = function () {
    $(this).blur;
}

$.fn.trzValidate = function (options) {
    var bool;
    var input = $(this);
    var defaults = { type: 'empty' };
    var opts = $.extend({}, defaults, options);

    //input.blur(Validate);
    return Validate();

    function Validate() {
        if (!Validator(input, opts.type)) {
            $(input).addClass('error');
            strError += '<span>*</span> Please input product name <br>';
            return false;
        }
        else {
            $(input).removeClass('error');
            return true;
        }
    }


    function Validator(elem, type) {
        var bool = true;
        text = $.trim($(elem).val());
        var j = new RegExp();
        switch (type) {
            case 'empty':
                if (text.length == 0) {
                    bool = false;
                }
                if (text == elem.attr('title')) {
                    bool = false;
                }
                break;
            case 'numeric':
                j.compile("^[1-9]+$");
                break;
            case 'decimal':
                j.compile("^[0-9]+(\.[0-9]{1,2})?$");
                break;
            case 'option':
                if ($(elem).find('option:selected').attr('id') == 0) {
                    bool = false;
                }
                return bool;
                break;
            case 'url':
                text = text.toUpperCase();
                if (!((text.substr(0, 7) == 'HTTP://') || (text.substr(0, 8) == 'https://'))) {
                    bool = false;
                }
                if (text == 'HTTP://' || text == 'HTTPS://') {
                    bool = false;
                }
                break;
        }
        if (!j.test(text)) {
            bool = false;
        }
        return bool;
    }





}
