
Array.prototype.swap = function(from,to){
	try{
		if( from < 0 || to < 0 || from >= this.length || to >= this.length )
			return false;
		var temp = this[to];
		this[to] = this[from];
		this[from] = temp;
		return true;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Array.swap]</B> ' + e + '</FONT>' ); return false; } 
}

Array.prototype.move = function(from,to, replace){
	try{
		var temp = this[from];
		if ( from < to ) to --;
		this.splice(from,1);
		this.splice(to,( replace ? 1 : 0),temp);
		return to;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Array.move]</B> ' + e + '</FONT>' ); return false; } 
}


Object.prototype.clone = function(deep) {
	try{
		var objectClone = new this.constructor();
		for (var property in this)
		if (!deep)
		  objectClone[property] = this[property];
		else if (this[property] != null && typeof this[property] == 'object')
		  objectClone[property] = this[property].clone(deep);
		else
		  objectClone[property] = this[property];
		return objectClone;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Object.clone]</B> ' + e + '</FONT>' ); return false; } 
}
/*
Object.prototype.toString = function() {
	try{
		var ret = "Object{"
		for (var prop in this)
			if ( typeof(this[prop]) != "function" )
				ret += " " + prop + ": " + this[prop] + ";\n"
		return ret + " }";
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Object.toString]</B> ' + e + '</FONT>' ); return false; }
}
*/

							/*********  Decimal / Hexa Convertions *********/

function decToHexa(d) {
	var hD="0123456789ABCDEF";
	var h = hD.substr(d&15,1);
	while(d>15) {
		d>>=4;h=hD.substr(d&15,1)+h;
	}
	return h;
}

function percentToHexa(p) {
	return decToHexa ( ( p * 255 ) / 100 );
}


function hexaToDec(h){
	return parseInt(h,16);
}

							/*********  Rotations *********/

function rotateX(x,y,ox,oy,rota,w,h){
	rota = ( (rota % 360)<0 ? (360 + (rota % 360)) : (rota % 360) );
	var radians = (rota * Math.PI)/180;
	var dotX = x - ox;
	var dotY = y - oy;
	var dotRo = ro(dotX,dotY);
	var dotPhi = phi(dotX,dotY);
	var rotatedDotRo = dotRo;
	var rotatedDotPhi = dotPhi + radians;

	var translation = 0;
	if ( radians <= Math.PI/2 ) // 0 - 90
		translation = (h * Math.sin(radians));
	else if ( radians < Math.PI ) // 91 - 159
		translation = (Math.sqrt((h * h) + (w * w)) * Math.sin(Math.atan(h/w) + (radians - (Math.PI/2))));
	else if (radians <= 3 * Math.PI / 2 ) // 180 - 270
		translation = w * Math.cos(radians - Math.PI);

	return polarToX(rotatedDotRo,rotatedDotPhi) + translation + ox;
}

function rotateY(x,y,ox,oy,rota,w,h){
	rota = ( (rota % 360)<0 ? (360 + (rota % 360)) : (rota % 360) );
	var radians = (rota * Math.PI)/180;
	var dotX = x - ox;
	var dotY = y - oy;
	var dotRo = ro(dotX,dotY);
	var dotPhi = phi(dotX,dotY);
	var rotatedDotRo = dotRo;
	var rotatedDotPhi = dotPhi + radians;

	var translation = 0;
	if ( radians > 3 * Math.PI/2 ) // 270 - 359
		translation = w * Math.sin( 2 * Math.PI - radians)
	else if ( radians > Math.PI ) // 181 - 269
		translation = Math.sqrt((h * h) + (w * w)) * Math.sin( Math.atan(h/w) + radians - Math.PI);
	else if ( radians > Math.PI/2 ) // 91 - 180
		translation = h * Math.sin( radians - (Math.PI/2) );

	return polarToY(rotatedDotRo,rotatedDotPhi) + translation + oy;
}


function phi(x,y){
	return ( ( y==0 && x== 0 ) ? Math.atan(Infinity) : Math.atan(y/x));
}

function ro(x,y){
	return Math.sqrt(x*x + y*y);
}

function polarToX(ro,phi){
	return ro * Math.cos(phi);
}

function polarToY(ro,phi){
	return ro * Math.sin(phi);
}

function getOffsetLeft(obj) {
	var pos = obj.offsetLeft;
	while ((obj = obj.offsetParent) != null) 
		pos += obj.offsetLeft;
	return pos;
}

function getOffsetTop(obj) {
	var pos = obj.offsetTop;
	while((obj = obj.offsetParent) != null) 
		pos += obj.offsetTop;
	return pos;
}

function trim(foo) {
	foo += ' ';
	foo = foo.replace(/\s+$/,"");
	foo = foo.replace(/^\s+/,"");
	return foo;
}

function replace(str, find_what, replace_with) {
	if ( find_what.length == 0 ) return str;
	var rc = ""; var prev_pos = cur_pos = 0;
	while ( (cur_pos = str.indexOf(find_what, prev_pos)) > -1 ) {
		rc += str.substring(prev_pos, cur_pos) + replace_with;
		prev_pos = cur_pos + find_what.length;
	}
	rc += str.substring(prev_pos);
	return rc;
}

function setOpacity(Obj, iOpacity){
	if (oBrowserSniffer.isMsie == true) {
		Obj.style.filter = 'alpha(opacity=' + iOpacity + ')';
	} else if (oBrowserSniffer.isNetscape == true && oBrowserSniffer.isMac == true) {
		Obj.style.MozOpacity = (iOpacity/100);
	} else {
		Obj.style.opacity = (iOpacity/100);
	}
}

function cmbSelectIndex(Obj, Id) {
	for (i=0;i<Obj.options.length;i++ )	{
		if (Obj.options[i].value == Id) {
			Obj.options[i].selected = true;
			break;
		}
	}
}

function isChild(ObjChild, ObjParent) {
	if (ObjChild == null) { return false; }
	while (ObjChild.parentNode != null) {		
		if (ObjChild == ObjParent) {
			return true;
			break;
		}
		ObjChild = ObjChild.parentNode;
	}
	return false;
}


function setPositionAbsoluteTop(obj, iTop) { 
	if (oBrowserSniffer.isMsie == true || oBrowserSniffer.isSafari == true) {
		objParent = obj.offsetParent;
		if (objParent != null) {
			if (objParent.tagName.toLowerCase() == 'fieldset') {
				for(var i=0;i<objParent.childNodes.length;i++){
					if (objParent.childNodes[i].nodeName.toLowerCase() == 'legend') {
						iTop += objParent.childNodes[i].offsetHeight;
						break;
					}
				}
			}
		}
	}
	obj.style.top = iTop + 'px';
}

function getIndexInArray(aTmp, sValue) {
	for(var i=0;i<aTmp.length;i++){
		if (trim(aTmp[i]) == trim(sValue)) { return i; }
	}
	return -1;
}

function cursorOverObj(e, Obj) {
	if (e.clientX < getOffsetLeft(Obj)) { return false; }
	if (e.clientX > (Obj.offsetWidth + getOffsetLeft(Obj))) { return false; }
	if (e.clientY < getOffsetTop(Obj)) { return false; }
	if (e.clientY > (Obj.offsetHeight + getOffsetTop(Obj))) { return false; }
	return true;
}

function getOffsetLeftInFieldSet(obj) {
	var pos = obj.offsetLeft;
	while ((obj = obj.offsetParent) != null) {
		pos += obj.offsetLeft;
		if (!isNaN(parseInt(obj.style.borderLeftWidth, 10))) { pos += parseInt(obj.style.borderLeftWidth, 10); }
		//TODO:
		if (obj.tagName.toLowerCase() == 'fieldset' && oBrowserSniffer.isFirefox == true) { pos += 2; }
		if (obj.tagName.toLowerCase() == 'fieldset' && oBrowserSniffer.isSafari == false) { pos += 2; }
	}
	return pos;
}
	
function getOffsetTopInFieldSet(obj) {
	var pos = obj.offsetTop;
	while((obj = obj.offsetParent) != null) {
		pos += obj.offsetTop;
		if (!isNaN(parseInt(obj.style.borderTopWidth, 10))) { pos += parseInt(obj.style.borderTopWidth, 10); }
		if (obj.tagName.toLowerCase() == 'fieldset' && oBrowserSniffer.isFirefox == true ) {
			for(var i=0;i<obj.childNodes.length;i++){
				if (obj.childNodes[i].nodeName.toLowerCase() == 'legend') {
					pos += obj.childNodes[i].offsetHeight + 2;
					break;
				}				
			}
			 
		}
	}
	return pos;
}

