﻿/// <reference path="../jq/jquery.intellisense.js"/>

// disable IE ajax caching
$.ajaxSetup({ cache: false});

var _tmplCache = {}
this.parseTemplate = function(str, data) {
	/// <summary>
	/// Client side template parser that uses &lt;#= #&gt; and &lt;# code #&gt; expressions.
	/// and # # code blocks for template expansion.
	/// NOTE: chokes on single quotes in the document in some situations
	///       use &amp;rsquo; for literals in text and avoid any single quote
	///       attribute delimiters.
	/// </summary>    
	/// <param name="str" type="string">The text of the template to expand</param>    
	/// <param name="data" type="var">
	/// Any data that is to be merged. Pass an object and
	/// that object's properties are visible as variables.
	/// </param>    
	/// <returns type="string" />
	var err = "";
	
	try {
		var func = _tmplCache[str];
		if (!func) {
			var strFunc =
            "var p=[],print=function(){p.push.apply(p,arguments);};" +
                        "with(obj){p.push('" +
			//                        str
			//                  .replace(/[\r\t\n]/g, " ")
			//                  .split("<#").join("\t")
			//                  .replace(/((^|#>)[^\t]*)'/g, "$1\r")
			//                  .replace(/\t=(.*?)#>/g, "',$1,'")
			//                  .split("\t").join("');")
			//                  .split("#>").join("p.push('")
			//                  .split("\r").join("\\'") + "');}return p.join('');";

            str.replace(/[\r\t\n]/g, " ")
               .replace(/'(?=[^#]*#>)/g, "\t")
               .split("'").join("\\'")
               .split("\t").join("'")
               .replace(/<#=(.+?)#>/g, "',$1,'")
               .split("<#").join("');")
               .split("#>").join("p.push('")
               + "');}return p.join('');";

			func = new Function("obj", strFunc);
			_tmplCache[str] = func;
		}
		return func(data);
	} catch (e) { err = e.message; }
	return "< # ERROR: " + err + " # >";
}

function isValidEmail(str) {
	var regex = new RegExp("^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$");
	return regex.test(str);
}

function unval(reval) {
    $(this).attr('reval', $(this).val());
    $(this).val('');
    $(this).attr('unvalled', 'true');

    if (typeof (reval) == undefined)
        $(this).bind('blur', reval);

    $(this).unbind('focus', unval);
}

function reval() {
	if ($(this).val() == '') {
		$(this).val($(this).attr('reval'));
		$(this).bind('focus', unval);
	}

	$(this).unbind('blur', reval);
}

function unvalpw() {
	var pwfld = $('<input type="password" id="' + $(this).attr('id') + '" class="' + $(this).attr('class') + '" name="' + $(this).attr('name') + '" tabindex="' + $(this).attr('tabindex') + '" />');
	pwfld.attr('reval', $(this).val());
	pwfld.attr('unvalled', 'true');
	pwfld.bind('blur', revalpw);

	$(this).replaceWith(pwfld);

	pwfld.focus();
}

function revalpw() {
	if ($(this).val() == '') {
		var txtfld = $('<input type="text" id="' + $(this).attr('id') + '" class="' + $(this).attr('class') + '" name="' + $(this).attr('name') + '" tabindex="' + $(this).attr('tabindex') + '" />');
		
		$(txtfld).val($(this).attr('reval'));
		$(this).replaceWith(txtfld);		
		
		$(txtfld).bind('focus', unvalpw);
    }
}

function l(str) {
	return str;
}

function showLoading(msg) {
	$('.appstatus-block').removeClass('appstatus-error');
	$('.appstatus-block').removeClass('appstatus-succes');
	
	$('#apploader').html(msg);
	$('#close-apploader').hide();
	$('.appstatus').css('margin-left', '-' + $('.appstatus').width()/2 + 'px');
	$('.appstatus').fadeIn('normal');
}

function showSuccess(msg) {
    $('#apploader').html(msg);
    $('.appstatus-block').addClass('appstatus-succes');
    $('#close-apploader').show();
    $('.appstatus').css('margin-left', '-' + $('.appstatus').width() / 2 + 'px');
	$('.appstatus').fadeIn('normal');

	setTimeout("hideAppStatus()", 4000); 
}

function showError(msg) {
    $('#apploader').html(msg);
    $('.appstatus-block').addClass('appstatus-error');
    $('#close-apploader').show();
    $('.appstatus').css('margin-left', '-' + $('.appstatus').width() / 2 + 'px');
	$('.appstatus').fadeIn('normal');

	setTimeout("hideAppStatus()", 4000); 
}

function hideAppStatus() {
	$('.appstatus').fadeOut('slow');
}

function isInt(x) {
    var y = parseInt(x);
    if (isNaN(y)) return false;
    return x == y && x.toString() == y.toString();
}

function getExt(file) {
    return (/[.]/.exec(file)) ? /[^.]+$/.exec(file.toLowerCase()) : '';
}

function setCookie(cookie_name, value) {
	var date = new Date();
	date.setTime(date.getTime() + ((6 * 30) * 24 * 60 * 60 * 1000));
	var expires = "; expires=" + date.toGMTString();

	document.cookie = cookie_name + "=" + value + expires + "; path=/";
}

function readCookie(cookie_name) {
	var nameEQ = cookie_name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

(function($) {
    var contains = document.compareDocumentPosition ? function(a, b) {
        return a.compareDocumentPosition(b) & 16;
    } : function(a, b) {
        return a !== b && (a.contains ? a.contains(b) : true);
    },
        oldLive = $.fn.live,
        oldDie = $.fn.die;

    function createEnterLeaveFn(fn, type) {
        return jQuery.event.proxy(fn, function(e) {
            if (this !== e.relatedTarget && !contains(this, e.relatedTarget)) {
                e.type = type;
                fn.apply(this, arguments);
            }
        });
    }
    function createBubbleFn(fn, type) {
        return jQuery.event.proxy(fn, function(e) {
            var parent = this.parentNode;
            fn.apply(this, arguments);
            if (parent) {
                e.type = type;
                $(parent).trigger(e);
            }
        });
    }
    var enterLeaveTypes = {
        mouseenter: 'mouseover',
        mouseleave: 'mouseout'
    };

    $.fn.live = function(types, fn, bubble) {
        var that = this;
        $.each(types.split(' '), function(i, type) {
            var proxy = fn;
            if (bubble) {
                proxy = createBubbleFn(proxy, enterLeaveTypes[type] || type);
            }

            if (enterLeaveTypes[type]) {
                proxy = createEnterLeaveFn(proxy, type);
                type = enterLeaveTypes[type];
            }
            oldLive.call(that, type, proxy);
        });

        return this;
    };

    $.fn.die = function(type, fn) {
        if (/mouseenter|mouseleave/.test(t.type)) {
            if (type == 'mouseenter') {
                type = 'mouseover';
            } else {
                type = 'mouseout';
            }
        }
        oldDie.call(this, type, fn);

        return this;
    };

})(jQuery); 

String.prototype.stripHTML = function()
{
        // What a tag looks like
        var matchTag = /<(?:.|\s)*?>/g;
		
        // Replace the tag
        return this.replace(matchTag, "");
};