
var PF_Debug = {
   log:   function() {
       this._postError.apply(this, arguments);
   },
   error: function() {
       this._postError.apply(this, arguments);
   },
   warn:  function() {
       this._postError.apply(this, arguments);
   },
   _postError: function() {
//       var req, params,
//           message = [].splice.call(arguments, 0).join("\n\n");
//
//       params = {
//           com:     'core',
//           task:    'ajax',
//           process: 'PF_logJsError',
//           message: message
//       };
//
//       req = new Ajax.Request('/ajax.php', {
//           method:     'post',
//           parameters: params
//       });
//
//       alert('An error has occured.  Please try again or contact support.');
   }
};

if ('undefined' != typeof(Ajax) && ('Responders' in Ajax)) {
    Ajax.Responders.register({
       onComplete: function(responder, resObj) {
           if (resObj.responseText.match(/id=["']login_box["']/)) {
               window.location.href = "/login";
           }
       }
    });
}

/*Pass unlimted [integer] key arrays to be merged and returned as a single array*/
function array_merge()
{
    var args = array_merge.arguments;
    var new_array = new Array();
    for(x=0;x<args.length;x++) {
        for(y=0;y<args[x].length;y++) {
            new_array[new_array.length] = args[x][y];
        }
    }
    return new_array;
}

//float (a)number [, int (b)decimals [, string (c)dec_point, string (d)thousands_sep]]
function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b); e = a + ''; f = e.split('.'); if (!f[0]) { f[0] = '0'; } if (!f[1]) { f[1] = ''; } if (f[1].length < b) { g = f[1]; for (i=f[1].length + 1; i <= b; i++) { g += '0'; } f[1] = g; } if(d != '' && f[0].length > 3) { h = f[0]; f[0] = ''; for(j = 3; j < h.length; j+=3) { i = h.slice(h.length - j, h.length - j + 3); f[0] = d + i +  f[0] + ''; } j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3)); f[0] = j + f[0]; } c = (b <= 0) ? '' : c; return f[0] + c + f[1];
}

function addslashes(str) {
    str=str.replace(/\'/g,'\\\'');
    str=str.replace(/\"/g,'\\"');
    str=str.replace(/\\/g,'\\\\');
    str=str.replace(/\0/g,'\\0');
    return str;
}

function stripslashes(str) {
    str=str.replace(/\\'/g,'\'');
    str=str.replace(/\\"/g,'"');
    str=str.replace(/\\\\/g,'\\');
    str=str.replace(/\\0/g,'\0');
    return str;
}

function insertAtCaret (textElement, text) {
    /* Requires Prototype Library to locate object by string call */
    if (typeof textElement != 'object') { textElement = $(textElement); }
    if (typeof textElement != 'object') { return; }
    if (document.selection) {
        textElement.focus();
        sel = document.selection.createRange();
        sel.text = text;
        textElement.focus();
    } else if (textElement.selectionStart || textElement.selectionStart == '0') {
        var startPos = textElement.selectionStart;
        var endPos = textElement.selectionEnd;
        textElement.value = textElement.value.substring(0, startPos) + text + textElement.value.substring(endPos, textElement.value.length);
        textElement.focus();
        textElement.selectionStart = startPos + text.length;
        textElement.selectionEnd = startPos + text.length;
    } else {
        textElement.value = textElement.value + '' + text;
        textElement.focus();
    }
}

function PF_getStyle(el,moz_styleProp,ie_styleProp)
{
    var x = $(el);
    if (x.currentStyle) {
        var y = x.currentStyle[ie_styleProp];
    } else if (window.getComputedStyle) {
        var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(moz_styleProp);
    }
    return y;
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function PF_scale_dims(orig_w, orig_h, max_w, max_h)
{
    var scale = PF_scale_rate(orig_w, orig_h, max_w, max_h);
    var new_w = Math.round(scale*orig_w); new_h = Math.round(scale*orig_h);
    if (new_w < 1) { new_w = 1; } if (new_h < 1) { new_h = 1; }
    return Array(new_w, new_h);
}

function PF_scale_rate(orig_w, orig_h, max_w, max_h) {
    var scale1 = 0; var scale2 = 0;
    if (orig_w > max_w) { scale1 = (orig_w - max_w) / orig_w; }
    if (orig_h > max_h) { scale2 = (orig_h - max_h) / orig_h; }
    var scale = (scale1>scale2)?scale1:scale2;
    return (1-scale);
}

var PF_cssmenu_hover = function(container_id)
{
    var lists = $(container_id).getElementsByTagName('li');
    var length = lists.length;
    for(i = 0; i < length; i++) {
        if(lists[i].className.indexOf(' sub') >= 0){
            lists[i].onmouseover = function() {
                $(this.getElementsByTagName('ul')[0]).addClassName('active_sub_menu');
            };
            lists[i].onmouseout = function() {
                $(this.getElementsByTagName('ul')[0]).removeClassName('active_sub_menu');
            };
        }
    }
};

var PF_str_replace = function(search, replace, subject, count)
{
    // ORIGINAL function str_replace(search, replace, subject, count) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'

    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
};

function PF_openWindow (window_name, url, width, height, scroll, resizable, locationbar, status, toolbar, menubar) {
    var x = parseInt(screen.width / 2.0) - (width / 2.0);
    var y = parseInt(screen.height / 2.0) - (height / 2.0);
    if (y<0) { y = 0; }
    if (x<0) { x = 0; }
    if (typeof(scroll) == "undefined") { scroll = "no"; }
    if (isIE) {
        width += 15;
        height += 35;
    }
    var win = window.open(url, window_name, "top=" + y + ",left=" + x + ",scrollbars="+ scroll +",modal=yes,width=" + width + ",height=" + height + ",resizable=" + resizable + ',status='+status + ',toolbar='+toolbar + ',location='+locationbar+ ',menubar='+menubar);
    win.focus();
}

