/**
 * Page list generation
 */
function PageList(cFunctionName, oFuncParams, nLimit, nOffset, nFound, nHits)
{
    // List of page numbers
    var cList = "";
    // Max page number items counting forwards from given page number
    var nPageListItems = 10;
    // Function parameter list
    var cParamList = "";
    
    
    // Generate parameter list
    for(cKey in oFuncParams)
    {
        cParamList += "'" + oFuncParams[cKey] + "',";
    }
    
    // Previous page link
    if(nOffset > 0)
    {
        // First page
        nSearchOffset = 0;
        cList   += '<a href="javascript:void(0)" '
                + ' onClick="'+ cFunctionName +'('+ cParamList +'\''+ nSearchOffset +'\');">'+ cFirstPageImg +'</a>';
        
        // Previous page
        nSearchOffset = nOffset-nLimit;
        cList   += '<a href="javascript:void(0)" '
                + ' onClick="'+ cFunctionName +'('+ cParamList +'\''+ nSearchOffset +'\');">'+ cPrevPageImg +'</a>';
    }
    
    // First showing page number
    var nBegin = 1;
    if((nOffset/nLimit) > 5)
    {
        nBegin = (nOffset/nLimit) - 5;
    }
    
    // Last showing page number
    var nEnd = nBegin + nPageListItems;
    if(nEnd*nLimit > nHits)
    {
        nEnd = (nHits/(nLimit+1)) + 1;
    }
    
    // Generate page numbers in given interval
    for(nI=nBegin;nI<=nEnd;nI++)
    {
        if((nOffset/nLimit + 1) == nI)
        {
            // Current page
            cList += '<span class="plitem current">'+nI+'</span>';
        }
        else
        {
            // Other pages
            nSearchOffset = nI*nLimit - nLimit;
            cList   += '<a class="plitem" href="javascript:void(0);" '
                    + ' onClick="'+ cFunctionName +'('+ cParamList +'\''+ nSearchOffset +'\');">'+ nI +'</a>';
        }
    }
    
    // Next page link
    if((nFound+nOffset) < nHits)
    {
        // Next page
        nSearchOffset = nOffset + nLimit;
        cList   += '<a href="javascript:void(0);" '
                + ' onClick="'+ cFunctionName +'('+ cParamList +'\''+ nSearchOffset +'\');">'+ cNextPageImg +'</a>';
        
        // Last page
        nLastPage = (nHits/nLimit) | 0;
        nSearchOffset = nLastPage*nLimit;
        nSearchOffset = nHits > nSearchOffset ? nSearchOffset : nSearchOffset - nLimit;
        cList   += '<a href="javascript:void(0)" '
                + ' onClick="'+ cFunctionName +'('+ cParamList +'\''+ nSearchOffset +'\');">'+ cLastPageImg +'</a>';
    }
    
    //$("#pagelist").html(cList);
    $(".pagelist").html(cList);
}
/**
 * Limit text lenght
 */
function LimitText(oLimitField, nLimitNum)
{
    if(oLimitField.value.length > nLimitNum)
    {
        oLimitField.value = oLimitField.value.substring(0, nLimitNum);
    } 
}
// EZPZ Hint v1.1.1; Copyright (c) 2009 Mike Enriquez, http://theezpzway.com; Released under the MIT License
(function($){
	$.fn.hint = function(options){
		var defaults = {
			hintClass: 'blur',
			hintName: 'hint'
		};
		var settings = $.extend(defaults, options);
		
		return this.each(function(){
			var hint;
			var dummy_input;
			
			// grab the input's title attribute
			text = $(this).attr('title');
			
			// create a dummy input and place it before the input
			$('<input type="text" name="temp" value="" />').insertBefore($(this));
			
			// set the dummy input's attributes
			hint = $(this).prev('input:first');
			hint.attr('class', $(this).attr('class'));
			hint.attr('size', $(this).attr('size'));
			hint.attr('name', settings.hintName);
			hint.attr('autocomplete', 'off');
			hint.attr('tabIndex', $(this).attr('tabIndex'));
			hint.addClass(settings.hintClass);
			hint.val(text);
			
			// hide the input
			$(this).hide();
			
			// don't allow autocomplete (sorry, no remember password)
			$(this).attr('autocomplete', 'off');
			
			// bind focus event on the dummy input to swap with the real input
			hint.focus(function(){
				dummy_input = $(this);
				$(this).next('input:first').show();
				$(this).next('input:first').focus();
				$(this).next('input:first').unbind('blur').blur(function(){
					if ($(this).val() == '') {
						$(this).hide();
						dummy_input.show();
					}
				});
				$(this).hide();
			});
			
			// swap if there is a default value
			if ($(this).val() != ''){
				hint.focus();
			};
			
			// remove the dummy inputs so that they don't get submitted
			$('form').submit(function(){
				//$('.' + settings.hintName).remove();
				$('input[name=' + settings.hintName + ']').each(function()
                {
                    $(this).next('input:first').show();
                    $(this).remove();
                });
			});
		});
		
	};
})(jQuery);
/**
 * Hint aplied to text box
 * 
 * @author Remy Sharp
 * @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
 */
(function ($) {
    $.fn.hint_textbox = function (blurClass) {
      if (!blurClass) { 
        blurClass = 'blur';
      }
        
      return this.each(function () {
        // get jQuery version of 'this'
        var $input = $(this),
        
        // capture the rest of the variable to allow for reuse
          title = $input.attr('title'),
          $form = $(this.form),
          $win = $(window);

        function remove() {
          if ($input.val() === title && $input.hasClass(blurClass)) {
            $input.val('').removeClass(blurClass);
          }
        }

        // only apply logic if the element has the attribute
        if (title) { 
          // on blur, set value to title attr if text is blank
          $input.blur(function () {
            if (this.value === '') {
              $input.val(title).addClass(blurClass);
            }
          }).focus(remove).blur(); // now change all inputs to title
          
          // clear the pre-defined text when form is submitted
          $form.submit(remove);
          $win.unload(remove); // handles Firefox's autocomplete
        }
      });
    };
})(jQuery);

// jquery.escape 1.0 - escape strings for use in jQuery selectors
// http://ianloic.com/tag/jquery.escape
// Copyright 2009 Ian McKellar <http://ian.mckellar.org/>
// Just like jQuery you can use it under either the MIT license or the GPL
// (see: http://docs.jquery.com/License)
(function() {
escape_re = /[#;&,\.\+\*~':"!\^\$\[\]\(\)=>|\/\\]/;
jQuery.escape = function jQuery$escape(s) {
  var left = s.split(escape_re, 1)[0];
  if (left == s) return s;
  return left + '\\' + 
    s.substr(left.length, 1) + 
    jQuery.escape(s.substr(left.length+1));
}
})();

/**
 * Loading popup with jQuery
 *
 * @param oAttr Object of attributes: {width:200, height:100, title:"", content:""}
 */
function LoadPopup(oAttr)
{
    //loads popup only if it is disabled
    if(nPopupStatus==0)
    {
        // Set title and content if exists
        if(oAttr.title)
        {
            $(cPopupTitleId).html(oAttr.title);
        }
        if(oAttr.content)
        {
            $(cPopupContentId).html(oAttr.content);
        }
        
        $(cPopupId).css({
            "width": "auto",
            "height": "auto"
        });
        
        
        var nWidth  = $(cPopupId).width();
        var nHeight = $(cPopupId).height();
        
        // Set popup dimensions if specified
        if(oAttr.width)
            nWidth = oAttr.width;
        if(oAttr.height)
            nHeight = oAttr.height;
        $(cPopupId).css({
            "width": nWidth,
            "height": nHeight
        });
        
        // Center popup
        CenterPopup();
        
        
        // Set background transparency
        $(cPopupBgId).css({
            "opacity": "0.7"
        });
        // Show popup and its background
        $(cPopupBgId).fadeIn("fast");
        $(cPopupId).fadeIn("fast");
        
        // Change status - make it enabled
        nPopupStatus = 1;
    }
}
//disabling popup with jQuery magic!
function DisablePopup()
{
    //disables popup only if it is enabled
    if(nPopupStatus==1){
        $(cPopupBgId).fadeOut("fast");
        $(cPopupId).fadeOut("fast");
        nPopupStatus = 0;
    }
}
//Set popup status
function SetPopupStatus(nStatus)
{
    nPopupStatus = nStatus;
}
// Allow popup overflow
function LetPopupOverwrite()
{
    SetPopupStatus(0);
}
//centering popup
function CenterPopup()
{
    //request data for centering
    var nWindowWidth, nWindowHeight;
    /**
     * Window dimensions
     */
    // Width
    nWindowWidth = $(window).width();
    // Height
    nWindowHeight = $(window).height();
    /**
     * Popup dimensions
     */
    var nPopupHeight = $(cPopupId).height();
    var nPopupWidth = $(cPopupId).width();
    
    //centering
    $(cPopupId).css({
        "position": "absolute",
        "top": nWindowHeight/2-nPopupHeight/2 + $(window).scrollTop(),
        "left": nWindowWidth/2-nPopupWidth/2
    });
    //only need force for IE6

    $(cPopupBgId).css({
        "height": nWindowHeight
    });
}
/**
 * Resize active popup
 * 
 * @param nNewWidth New Width of the popup
 * @param nNewHeight New Height of the popup
 * return void
 */
function ResizePopup(nNewWidth, nNewHeight)
{
    /**
     * Change popup dimensions and reposition the popup
     */
    $(cPopupId).css({
        "width": nNewWidth,
        "height": nNewHeight
    });
    
    CenterPopup();
}
/**
 * Trim whitespace
 * 
 * @param string cText
 * return string
 */
function Trim(cText)
{
	return cText.replace(/^\s+|\s+$/g,"");
}
/**
 * Show custom alert
 * 
 * @param string cText
 * return void
 */
function CustomAlert(cText)
{
    cTitle = "";
    cContent = ''
            + '<div id="CustomAlert">'
            + ' <div id="CustomAlertMessage">'
            +       cText
            + ' </div>'
            + ' <div id="CustomAlertActions">'
            + '     <input id="CustomAlertOk" class="Button" type="button" value="Ok" />'
            + ' </div>'
            + '</div><br/>';
    oAttr = {width:400,height:"auto",title:cTitle,content:cContent};
    
    LoadPopup(oAttr);
    
    $("#CustomAlertOk", "#CustomAlert").click(function()
    {
        DisablePopup();
    });
}


// Figure out what browser is being used
var userAgent = navigator.userAgent.toLowerCase();
jQuery.browser = {
    version: (userAgent.match( /.+(?:rv|it|ra|ie|me)[\/: ]([\d.]+)/ ) || [])[1],
    chrome: /chrome/.test( userAgent ),
    safari: /webkit/.test( userAgent ) && !/chrome/.test( userAgent ),
    opera: /opera/.test( userAgent ),
    msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
    mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
};

function nl2br(cString) {
	return cString.replace(/(\r\n|\r|\n)/g, "<br />\n");
}

function OpenUrlNewWindow(cUrl)
{
    // Client window width and height
    var nWindowWidth, nWindowHeight;
    // Width
    nWindowWidth = $(window).width();
    // Height
    nWindowHeight = $(window).height();

    window.open(cUrl ,'_blank','scrollbars=1,toolbar=1,location=1,menubar=1,resizable=1,width='+(nWindowWidth-100)+',height='+(nWindowHeight-100)+',top=50,left=50');

}