String.prototype.replaceAll = function(search, replace){
  return this.split(search).join(replace);
}
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}

array_has=function(a, v,i){
	for (var j=0;j<a.length;j++){
		if (a[j]==v) return (!i ? true : j);
	}
	return false;
}

array_clone=function(a) {
	return a.slice(0);
}

object_to_source = function(obj, check) {
	if(obj==null) return 'null';
	
    var con = obj.constructor;
    if(con == String) {
        return '"' + obj + '"';
    } else if(con == Number) {
        return obj;
    } else if(con == Array) {
        var res = '[';
        for(var i=0,len=obj.length;i<len;i++) {
            if(i == len-1)
                res += object_to_source(obj[i]);
            else
                res += object_to_source(obj[i]) + ', ';
        }
        res += ']';
        return res;
    } else if(con == RegExp) {
        return obj;
    } else if(con == Object) {
        var res = '{';
        var i=0;
        for(var j in obj) {
            if(j != 'object_to_source') {
                if(i == 0) {
                    res += j + ':' + object_to_source(obj[j], 1);
                } else {
                    res += ', ' + j + ':' + object_to_source(obj[j], 1);
                }
                i++;
            }
        }
        res += '}';
        if(check) {
            return res;
        } else {
            return '(' + res + ')';
        }
    }
}

function serialize( mixed_value ) {
    var _getType = function( inp ) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            if (match = cons.match(/(\w+)\(/)) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';
    
    switch (type) {
        case "function": 
            val = ""; 
            break;
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            val = "s:" + mixed_value.length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") { 
                    continue; 
                }
                
                okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key);
                vals += serialize(okey) +
                        serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") val += ";";
    return val;
}

function ResizeFrame(id_frame, height){
	if(id_frame==null || id_frame=='undefined')	return;
	res_height = window.frames[id_frame].document.body.scrollHeight;
	if (res_height>height) height = res_height;
	frame_obj = document.getElementById(id_frame);
	frame_obj.height = height+30;
	frame_obj.onreadystatechange=ResizeFrame;
	window.last_frame=id_frame;
	window.last_height=height;
}

function affPopupWindow(url, type, id){
	var win=window.open(url, id, 'width=997, height=600, resizable=yes, scrollbars=yes, menubar=no, status=no');
	win.focus();
}

function getCaret(el) { 
  if (el.selectionStart) { 
    return el.selectionStart; 
  } else if (document.selection) { 
    el.focus(); 

    var r = document.selection.createRange(); 
    if (r == null) { 
      return 0; 
    } 

    var re = el.createTextRange(), 
        rc = re.duplicate(); 
    re.moveToBookmark(r.getBookmark()); 
    rc.setEndPoint('EndToStart', re); 

    return rc.text.length; 
  }  
  return 0; 
}

function setSelRange(inputEl, selStart, selEnd) { 
	if (inputEl.setSelectionRange) { 
		inputEl.focus(); 
		inputEl.setSelectionRange(selStart, selEnd); 
	} else if (inputEl.createTextRange) { 
		var range = inputEl.createTextRange(); 
		range.collapse(true); 
		range.moveEnd('character', selEnd); 
		range.moveStart('character', selStart); 
		range.select(); 
	} 
}

//Cookie functions
function setCookie(c_name,value,expiredays,path){
	if(!path) path='/';
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+'; path='+path;
}