/**
* @version $Id: mypms_common.js 286 2006-09-14 17:08:29Z danial $
* Clexus Private Messaging Component
* @package ClexusPM
* @Copyright (C) 2003 - 2006 Danial Taherzadeh All rights reserved
* @license Released as a commercial component
* @link http://www.clexus.com
**/

(function () {
    var m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        s = {
            array: function (x) {
                var a = ['['], b, f, i, l = x.length, v;
                for (i = 0; i < l; i += 1) {
                    v = x[i];
                    f = s[typeof v];
                    if (f) {
                        v = f(v);
                        if (typeof v == 'string') {
                            if (b) {
                                a[a.length] = ',';
                            }
                            a[a.length] = v;
                            b = true;
                        }
                    }
                }
                a[a.length] = ']';
                return a.join('');
            },
            'boolean': function (x) {
                return String(x);
            },
            'null': function (x) {
                return "null";
            },
            number: function (x) {
                return isFinite(x) ? String(x) : 'null';
            },
            object: function (x) {
                if (x) {
                    if (x instanceof Array) {
                        return s.array(x);
                    }
                    var a = ['{'], b, f, i, v;
                    for (i in x) {
                        v = x[i];
                        f = s[typeof v];
                        if (f) {
                            v = f(v);
                            if (typeof v == 'string') {
                                if (b) {
                                    a[a.length] = ',';
                                }
                                a.push(s.string(i), ':', v);
                                b = true;
                            }
                        }
                    }
                    a[a.length] = '}';
                    return a.join('');
                }
                return 'null';
            },
            string: function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            }
        };

    Object.prototype.toJSONString = function () {
        return s.object(this);
    };

    Array.prototype.toJSONString = function () {
        return s.array(this);
    };
})();

String.prototype.parseJSON = function () {
    try {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
            eval('(' + this + ')');
    } catch (e) {
        return false;
    }
};



var ie  = document.all  ? 1 : 0;
function submitenter(myfield,e,m_a,m_b)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13)
	{
		fetchUsers(m_a, m_b);
		return false;
	}
	else
	return true;
}

function CheckAll() {
	var fmobj = document.mutliact;
	for (var i=0;i<fmobj.elements.length;i++) {
		var e = fmobj.elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox') && (!e.disabled)) {
			e.checked = fmobj.allbox.checked;
		}
	}
}

function setStatus(s,n, type) {
	//$('statusContainer').style.visibility = "visible";
	$('mypms_topmessage').innerHTML = "<div class=\""+ type +"\">" + s + "</div>";
	new Effect.Appear('mypms_topmessage');
	setTimeout( "new Effect.Fade('mypms_topmessage');", n );
}


Array.prototype.contains = function (element) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == element) {
			return true;
		}
	}
	return false;
};

Array.prototype.walk = function( f ) {
	var a = [],
	i = this.length;
	while(i--) {
		a.push( f( this[i] ) );
	}
	return a.reverse();
};

function ShowAlertBox () {
	centerBox('clexus-alertbox');
	Element.show('clexus-alertbox');

}

function increaseNotesHeight(thisTextarea, add) {
	if (thisTextarea) {
		newHeight = parseInt(thisTextarea.style.height) + add;
		thisTextarea.style.height = newHeight + "px";
	}
}

function decreaseNotesHeight(thisTextarea, subtract) {
	if (thisTextarea) {
		if ((parseInt(thisTextarea.style.height) - subtract) > 150) {
			newHeight = parseInt(thisTextarea.style.height) - subtract;
			thisTextarea.style.height = newHeight + "px";
		}
		else {
			newHeight = 150;
			thisTextarea.style.height = "150px";
		}
	}
}

/*
Popup
*/

function popup_addContactToList (id, lay , randd, st){
	var url     = 'index2.php';
	var uparams = 'option=com_mypms&task=popupmanagecontact&no_html=1&conid='+id+"&status="+st;
	new Ajax.Updater(lay, url,  {
		method: 'post', postBody: uparams, asynchronous: true,
		onLoading: function(request){
			Element.show('loading_popup_' + randd);
		}
		, onComplete: function(request){
			Element.hide('loading_popup_' + randd);
			setTimeout( "cxGetPopupMenu ("+id+", "+randd+");", 3000 );
		}
	});
}


function popup_manageBlock (id, lay , randd){
	var url     = 'index2.php';
	var uparams = 'option=com_mypms&task=popupmanageblock&no_html=1&conid='+id;
	new Ajax.Updater(lay, url,  {
		method: 'post', postBody: uparams, asynchronous: true,
		onLoading: function(request){
			Element.show('loading_popup_' + randd);
		}
		, onComplete: function(request){
			Element.hide('loading_popup_' + randd);
			setTimeout( "cxGetPopupMenu ("+id+", "+randd+");", 3000 );
		}
	} );
}


function popup_addContactToFav (id, state, lay , randd){
	var url     = 'index2.php';
	var uparams = 'option=com_mypms&task=favcontact&no_html=1&conid='+id+"&fav="+state;
	new Ajax.Updater(lay, url,  {
		method: 'post', postBody: uparams, asynchronous: true,
		onLoading: function(request){
			Element.show('loading_popup_' + randd);
		},
		onComplete: function(request){
			Element.hide('loading_popup_' + randd);
			setTimeout( "cxGetPopupMenu ("+id+", "+randd+");", 3000 );
		}
	});
}

function cxGetPopupMenu (id, randd){
	var url 	= 'index2.php';
	var uparams = 'option=com_mypms&task=getpopupmenu&no_html=1&conid='+id+'&rand='+ randd;
	new Ajax.Updater("cxpopupcontent_" + randd, url,  {
		method: 'post', postBody: uparams, asynchronous: true,
		onLoading: function(request){
			Element.show('loading_popup_' + randd);
		},
		onComplete: function(request){
			Element.hide('loading_popup_' + randd);
		}
	});
}

centerBox = function(element)
{
    try
    {
        element = $(element);
    }
    catch(e)
    {
        return;
    }

    var my_width  = 0;
    var my_height = 0;

    if ( typeof( window.innerWidth ) == 'number' )
    {

        my_width  = window.innerWidth;
        my_height = window.innerHeight;
    }
    else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
    {

        my_width  = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }
    else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
    {

        my_width  = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }

    element.style.position = 'absolute';
    element.style.display  = 'block';
    element.style.zIndex   = 99;

    var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop )
    {
        scrollY = document.documentElement.scrollTop;
    }
    else if ( document.body && document.body.scrollTop )
    {
        scrollY = document.body.scrollTop;
    }
    else if ( window.pageYOffset )
    {
        scrollY = window.pageYOffset;
    }
    else if ( window.scrollY )
    {
        scrollY = window.scrollY;
    }

    var elementDimensions = Element.getDimensions(element);

    var setX = ( my_width  - elementDimensions.width  ) / 2;
    var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + "px";
    element.style.top  = setY + "px";
}