/* String */
String.prototype.getElementType=function() {
	var out=null;
	var element=this.substring(0,1);
	if (element.isCharacter()) {
		element=element.toUpperCase();
		for (var i=0; i<Elements.length; i++) {
			if (Elements[i][0]==element) {
				out=element;
				break;
			}
		}
	}
	return(out);
	
	out=null;
	element=null;
};
String.prototype.isNull=function(alt) {
	return((this==null) ? alt : this);
}
String.prototype.isNumeric=function() {
	return (this.search(/^\d+$/) != -1);
	//return (this.match(/\d/g)!=null);
};
String.prototype.getNumbersOnly=function() {
	var out=this.match(/[\d\.]+/g);
//	alert(this);
//	alert(out.length);
	out=out.join('');
//	alert(out);
	return(out);
	
	out=null;
};
String.prototype.isCharacter=function() {
	return (this.search(/^[a-zA-Z]+$/) != -1);
};
String.prototype.trim = function() {
	return(this.replace(/^\s+|\s+$/g, ''));
};
String.prototype.isDrygood = function() {
	return(this=='3');
};
String.prototype.isFish = function() {
	return(this=='1');
};
String.prototype.isInvert = function() {
	return(this=='2');
};
String.prototype.asMsgLoading=function() {
	return(['<div id="MsgLoading">',this,'<div>'].join(' '));
};
String.prototype.asMsgLoadingAsDiv=function() {
	var div=document.createElement('div');
	div.id='MsgLoading';
	div.innerHTML=this;
	return(div);
	
	div=null;
};
String.prototype.asMsgLoadingAsDocFrag=function() {
	var doc=document.createDocumentFragment();
	doc.appendChild(this.asMsgLoadingAsDiv());
	return(doc);

	doc=null;
};
String.prototype.withFixedDigits=function(digitCount) {
	alert([this,this.length,digitCount,digitCount-this.length].join('\n'))
	var additionalDigits=Math.max(0,digitCount-this.length);
	var out=(additionalDigits>0) ? ['0'.repeat(additionalDigits),this].join('') : this;
	return(out);
	
	additionalDigits=null;
	out=null;
};
String.prototype.repeat=function(count) {
	var array=[];
	for (var i=1; i<=count; i++) {
		array.push(this);
	}
	return(array.join(''));
	
	array=null;
};
String.prototype.isNull=function() {
	return( (this==null) ? '' : this );
};

/* Array */
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(elt /*, from*/) {
		var len = this.length;
		var from = Number(arguments[1]) || 0;

		from = (from < 0) ? Math.ceil(from) : Math.floor(from);
		if (from < 0) from += len;

		for (; from < len; from++) {
			if (from in this && this[from] === elt) return from;
		}
		return -1;
		
		len=null;
		from=null;
	};
}
if (!Array.prototype.size) {
	Array.prototype.size = function () {
		var l=0;
		for (var k in this) { l++; }
		return l;
		
		l=null;
	};
}
if (!Array.prototype.forEach) {
	Array.prototype.forEach = function(fun /*, thisp*/) {
		var len = this.length;
		//if (typeof fun != "function") throw new TypeError();
		if (typeof fun == "function") {
		var thisp = arguments[1];
//		for (var i = 0; i < len; i++) {
//			if (i in this) fun.call(thisp, this[i], i, this);
//		}
		for (var i in this) {
			fun.call(thisp, this[i], i, this);
		}
		
		len=null;
		thisp=null;
		}
	};
}
if (!Array.prototype.add) {
	Array.prototype.add = function(obj) {
		this[this.length]=obj;
	};
}

/* Function */
Function.prototype.extendsFrom = function(Super) {
   var Self = this;
   var Func = function() {
      Super.apply(this, arguments);
      Self.apply(this, arguments);
   };
   Func.prototype = new Super();
   return Func;
   
   Self=null;
   Func=null;
};
/*Function.prototype.log = function(details) {
	if (!details) {
		details=[];
		for (var i in arguments) {
			details.push([i,arguments[i]].join('='));
		}
		details=details.join('; ');
	}	
	System.Log.push(new LogEntry(arguments.caller,arguments.callee,details));
};*/
if (!Function.prototype.length) {
	Function.prototype.length = function () {
		var l=0;
		for (var k in this) { l++; }
		return l;
		
		l=null;
	};
}
/*Function.prototype.getChildElementById=function(id) {
	var out=null;
	var nodes=this.childNodes;
	for (var i in nodes) {
		if (nodes[i].id==id) {
			out=nodes[i];
			break;
		}
		if (out==null) {
			out=getChildElementById(nodes[i],id);
		}
	}
	return(out);
}*/
function hasParentWithClassName(obj,className) {
	var out=false;
	try {
		var node=obj.parentNode;
		if (node) {
			if (node.className==className) { out=true; }
			else { out=hasParentWithClassName(node,className); }
		}

		node=null;
	} catch(e) { alert(['hasParentWithClassName',e].join('\n')); }
	return(out);
	
	out=null;
};
function hasParentWithID(obj,id) {
	var out=false;
	try {
		var node=obj.parentNode;
		if (node) {
			if (node.id==id) { out=true; }
			else { out=hasParentWithID(node,className); }
		}

		node=null;
	} catch(e) { alert(['hasParentWithID',e].join('\n')); }
	return(out);
	
	out=null;
}
function getParentWithTagName(obj,tagName) {
	var out=null;
	try {
		var node=obj.parentNode;
//		alert([node.nodeName,node.id,node.innerHTML].join('\n'))
		if (node) {
			if (node.nodeName==tagName) { out=node; }
			else { out=getParentWithTagName(node,tagName); }
		}
	} catch(e) { alert(['getParentWithTagName',e].join('\n')); }
	return(out);
	
	node=null;
	out=null;
}
function getChildElementByClassName(obj,className) {
	var out=null;
	try {
		var nodes=obj.childNodes;
		for (var i in nodes) {
			if (nodes[i].className==className) {
				out=nodes[i];
				break;
			}
			if (out==null) {
				out=getChildElementById(nodes[i],className);
			}
		}
		
		nodes=null;
	} catch(e) { ; }
	return(out);

	out=null;
}
function getChildElementById(obj,id) {
	var out=null;
	try {
		var nodes=obj.childNodes;
		for (var i in nodes) {
			if (nodes[i].id==id) {
				out=nodes[i];
				break;
			}
			if (out==null) {
				out=getChildElementById(nodes[i],id);
			}
		}
		
		nodes=null;
	} catch(e) { ; }
	return(out);

	out=null;
}
function getElementsByClassName(className, startElement, elements) {
	if (!elements) elements=[];
	if (!startElement) startElement=document.body;
	var nodes=startElement.childNodes;
	for (var i in nodes) {
		var node=nodes[i];
		if (node.className) {
			if (node.className==className) {
				elements.push(node);
			}
		}
		if (node.hasChildNodes()) {
			elements=getElementsByClassName(className, node, elements);
		}
	}
	nodes=null;
	return(elements);
}
function $$(id) {
	return(document.getElementById(id));
}
function getHTTPObject() {
	//if (System.KeepLog) System.Log.push(new LogEntry(arguments));
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } 
		catch (e) {
			try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } 
			catch (E) { xmlhttp = false; }
		}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try { xmlhttp = new XMLHttpRequest(); } 
		catch (e) { xmlhttp = false; }
	}
	return xmlhttp;

	xmlhttp=null;
}
function serialize(object) {
    this.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();
            match = cons.match(/(\w+)\(/);
            if (match) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    this.serialize=function(object) {
        var out=[];
        for (var i in object) {
            try {
                out.push('"'+i+'":"'+object[i].replace(/\\/gi,'\\\\').replace(/\"/gi,'\\"').replace(/\n/gi,' ')+'"');
            } 
            catch(e) { 
                out.push('"'+i+'":'+this.serialize(object[i])+'');
            }
            //out.push('"'+i+'":"'+object[i].replace(/\"/gi,'\\"')+'"');
        }
        return('{'+out.join(',')+'}');
    };
    return(this.serialize(object));
}
function serializeV1( mixed_value ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Arpad Ray (mailto:arpad@php.net)
    // +   improved by: Dino
    // +   bugfixed by: Andrej Pavlovic
    // +   bugfixed by: Garagoth
    // +      input by: DtTvB (http://dt.in.th/2008-09-16.string-length-in-bytes.html)
    // +   bugfixed by: Russell Walker (http://www.nbill.co.uk/)
    // %          note: We feel the main purpose of this function should be to ease the transport of data between php & js
    // %          note: Aiming for PHP-compatibility, we have to translate objects to arrays
    // *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
    // *     example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});
    // *     returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'
 
    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();
            match = cons.match(/(\w+)\(/);
            if (match) {
                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 "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:" + encodeURIComponent(mixed_value).replace(/%../g, 'x').length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            /*
            if (type == "object") {
                var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/);
                if (objname == undefined) {
                    return;
                }
                objname[1] = serialize(objname[1]);
                val = "O" + objname[1].substring(1, objname[1].length - 1);
            }
            */
            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, 10) : key);
                vals += serialize(okey) +
                        serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
        case "undefined": // Fall-through
        default: // if the JS object has a property which contains a null value, the string cannot be unserialized by PHP
            val = "N";
            break;
    }
    if (type != "object" && type != "array") {
        val += ";";
    }
    return val;
}