function f_getDate(){
	var d = new Date();
	return (d.getMonth() + 1) + "-" + d.getDate() + "-" + d.getYear();
}


function Stack(){
	this.state  = [];
	this.length = 0;
	this.push = function( element){
		this.state[this.length] = element;
		this.length++;
		return element;
	}
	this.pop = function(){
		if( this.length == 0){
			return null;
		}
		this.length--;
		var tempState = this.state[this.length];
		delete this.state[this.length];
		return tempState;
	}
}

function DoubleLinkedList(){
	this.first				= null;
	this.last				= null;
	this.selectedNode		= null;
	this.selectedNode2	= null;
	this.length				= 0;
	this.items     		= {};
}

function DoubleLinkedListNode(value, id){
	this.next		= null;
	this.previous	= null;
	this.value		= value;
	this.id 			= id;
}

DoubleLinkedList.prototype.add = function( value, id){
	id = id || ("dlln" + this.length);
	var node = new DoubleLinkedListNode( value, id);
	if( this.length == 0){
		this.first = node;
		this.last = node;
	}
	else{
		node.previous = this.last;
		this.last.next	= node;
		this.last = node;
	}
	this.length++;
	this.items[id] = node;
	return node;
}

DoubleLinkedList.prototype.del = function( node){

	if( !node)
		return;

	if(this.first == node && this.last != node){
		this.first 				= node.next;
		this.first.previous 	= null;
	}
	else if(this.last == node && this.first != node){
		this.last 				= node.previous;
		this.last.next 		= null;
	}
	else if(this.first != node && this.last != node){
		node.next.previous 	= node.previous;
		node.previous.next 	= node.next;
	}

	node = null;
	this.length--;
}

DoubleLinkedList.prototype.getPrev = function(){
	if( this.selectedNode){
		if(this.selectedNode == this.first){
			return this.last;
		}
		else{
			return this.selectedNode.previous;
		}
	}
	else{
		return null;
	}
}

DoubleLinkedList.prototype.getNext = function(){
	if( this.selectedNode){
		if(this.selectedNode == this.last){
			return this.first;
		}
		else{
			return this.selectedNode.next;
		}
	}
	else{
		return null;
	}
}

DoubleLinkedList.prototype.moveToEnd = function( node){

	if( !node || node == this.last)
		return;

	if(this.first == node){
		this.first = this.first.next;
		this.first.previous  = null;
	}
	else{
		node.next.previous = node.previous;
		node.previous.next = node.next;
	}

	this.last.next = node;
	node.next		= null;
	node.previous  = this.last;
	this.last		= node;
}

DoubleLinkedList.prototype.resetEnumerate = function( node){
	if( node)
		this.selectedNode = node;
	else
		this.selectedNode = null;
}

DoubleLinkedList.prototype.setCur = DoubleLinkedList.prototype.resetEnumerate;

DoubleLinkedList.prototype.resetReverseEnumerate = function( node){
	if( node)
		this.selectedNode2 = node;
	else
		this.selectedNode2 = null;
}


DoubleLinkedList.prototype.enumerate = function(){
	if(this.length == 0)
		return null;

	if( this.selectedNode)
		this.selectedNode = this.selectedNode.next;
	else
		this.selectedNode = this.first;

	return this.selectedNode;
}

DoubleLinkedList.prototype.reverseEnumerate = function(){
	if(this.length == 0)
		return null;

	if( this.selectedNode2)
		this.selectedNode2 = this.selectedNode2.previous;
	else
		this.selectedNode2 = this.last;

	return this.selectedNode2;
}

DoubleLinkedList.prototype.rotate = function(){
	if(this.length == 0)
		return null;

	if( this.selectedNode)
		this.selectedNode = this.selectedNode.next;
	else
		this.selectedNode = this.first;

	if(!this.selectedNode)
		this.selectedNode = this.first;

	return this.selectedNode;
}

DoubleLinkedList.prototype.reverseRotate = function(){
	if(this.length == 0)
		return null;

	if( this.selectedNode2)
		this.selectedNode2 = this.selectedNode2.previous;
	else
		this.selectedNode2 = this.last;

	if(!this.selectedNode2)
		this.selectedNode2 = this.last;

	return this.selectedNode2;
}

function RGB_2_HEX(r, g, b){
	r = (r < 16) ? ("0" + r.toString(16)) : r.toString(16) + "";
	g = (g < 16) ? ("0" + g.toString(16)) : g.toString(16) + "";
	b = (b < 16) ? ("0" + b.toString(16)) : b.toString(16) + "";
	return "#" + r + g + b + "";
}

function HEX_2_RGB( color){
	if(color.indexOf("#") == -1)
		return [0,0,0];

	var r = parseInt( color.substring(1,3) ,16);
	var g = parseInt( color.substring(3,5) ,16);
	var b = parseInt( color.substring(5,7) ,16);

	return [r, g, b];
}

function HSV_2_RGB(h, s, v){
	h = h / 360;
	s = s / 100;
	v = v / 100;

	var r, g, b;
	var var_1, var_2, var_3, var_h, var_i, var_r, var_g, var_b;

	if ( s == 0 ){
	   r = Math.round( v * 255);
	   g = Math.round( v * 255);
	   b = Math.round( v * 255);
	}
	else{
	   var_h = h * 6
	   var_i = Math.floor( var_h );
	   var_1 = v * ( 1 - s );
	   var_2 = v * ( 1 - s * ( var_h - var_i));
	   var_3 = v * ( 1 - s * ( 1 - ( var_h - var_i)));

	   if ( var_i == 0){
   		var_r = v;
   		var_g = var_3;
   		var_b = var_1
	   }
	   else if ( var_i == 1){
	   	var_r = var_2;
	   	var_g = v;
	   	var_b = var_1;
	   }
	   else if ( var_i == 2){
	   	var_r = var_1;
	   	var_g = v;
	   	var_b = var_3;
	   }
	   else if ( var_i == 3){
	   	var_r = var_1;
	   	var_g = var_2;
	   	var_b = v;
	  	}
	   else if ( var_i == 4){
	   	var_r = var_3;
	   	var_g = var_1;
	   	var_b = v
	   }
	   else{
	   	var_r = v;
	   	var_g = var_1;
	   	var_b = var_2;
	   }

	   r = Math.round( var_r * 255);
	   g = Math.round( var_g * 255);
	   b = Math.round( var_b * 255);
   }

	return [Math.round(r), Math.round(g), Math.round(b)];
}

function RGB_2_HSV( r, g, b){
	var var_R = ( r / 255);
	var var_G = ( g / 255);
	var var_B = ( b / 255);
	var h, s, v;
	var del_Max, var_Max, var_Min, del_R, del_G, del_B;

	var_Min = min( var_R, var_G, var_B);
	var_Max = max( var_R, var_G, var_B);
	del_Max = var_Max - var_Min;

	v = var_Max;

	if ( del_Max == 0 ){
	   h = 0;
	   s = 0;
	}
	else{
   	s = del_Max / var_Max;

	   del_R = ((( var_Max - var_R) / 6) + ( del_Max / 2)) / del_Max;
	   del_G = ((( var_Max - var_G) / 6) + ( del_Max / 2)) / del_Max;
	   del_B = ((( var_Max - var_B) / 6) + ( del_Max / 2)) / del_Max;

	   if ( var_R == var_Max)
	   	h = del_B - del_G;
	   else if ( var_G == var_Max)
	   	h = ( 1/3) + del_R - del_B;
	   else if ( var_B == var_Max )
	   	h = ( 2/3) + del_G - del_R;

	   if ( h < 0 )
	   	h += 1;
	   if ( h > 1 )
	   	h -= 1;
	}

	function min(a, b, c){
		var min = a;
		if( b < min) min = b;
		if( c < min) min = c;
		return min;
	}
	function max(a, b, c){
		var max = a;
		if( b > max) max = b;
		if( c > max) max = c;
		return max;
	}

	return [Math.round(h * 360), Math.round(s * 100), Math.round(v * 100)];
}

function RGB_2_Lab(r, g, b){
	var var_R = r / 255;
	var var_G = g / 255;
	var var_B = b / 255;

	if ( var_R > 0.04045 ) var_R = Math.pow( ( var_R + 0.055 ) / 1.055 , 2.4);
	else                   var_R = var_R / 12.92;
	if ( var_G > 0.04045 ) var_G = Math.pow( ( var_G + 0.055 ) / 1.055 , 2.4);
	else                   var_G = var_G / 12.92;
	if ( var_B > 0.04045 ) var_B = Math.pow( ( var_B + 0.055 ) / 1.055 , 2.4);
	else                   var_B = var_B / 12.92;

	var_R = var_R * 100;
	var_G = var_G * 100;
	var_B = var_B * 100;

	var X = var_R * 0.4124 + var_G * 0.3576 + var_B * 0.1805;
	var Y = var_R * 0.2126 + var_G * 0.7152 + var_B * 0.0722;
	var Z = var_R * 0.0193 + var_G * 0.1192 + var_B * 0.9505;

	var var_X = X / 95.047;
	var var_Y = Y / 100;
	var var_Z = Z / 108.883;

	if ( var_X > 0.008856 ) var_X = Math.pow (var_X ,  1/3 );
	else                    var_X = ( 7.787 * var_X ) + ( 16 / 116 );
	if ( var_Y > 0.008856 ) var_Y = Math.pow (var_Y , 1/3 );
	else                    var_Y = ( 7.787 * var_Y ) + ( 16 / 116 );
	if ( var_Z > 0.008856 ) var_Z = Math.pow (var_Z , 1/3 );
	else                    var_Z = ( 7.787 * var_Z ) + ( 16 / 116 );

	var L = ( 116 * var_Y ) - 16;
	var a = 500 * ( var_X - var_Y );
	var b = 200 * ( var_Y - var_Z );
	return [L, a, b];
}

function f_getParams( paramName, wnd){
	wnd = wnd || window;
	var str = unescape( wnd.location.search.substr(1));
	var argums = [];
	var arg = "";
	var val = "";
	var delim = "=";
	for(var i=0; i< str.length; i++){
		if(str.substr(i, 1) != delim){
			if(delim == "=")
				arg += str.substr(i, 1);
			if(delim == "&")
				val += str.substr(i, 1);
		}
		else{
			if(delim == "="){
				delim = "&";
				continue;
			}
			if(delim == "&"){
				delim = "=";
				argums[arg] = val;
				arg = "";
				val = "";
				continue;
			}
		}
	}
	argums[arg] = val;

	if( paramName){
		return argums[paramName];
	}
	else{
		return argums;
	}
}

function f_getCookie( paramName){
	var str = document.cookie;
	str = str.replace(/\s+/gi, "")
	var args = [];
	var arg = "";
	var val = "";
	var delim = "=";
	for(var i=0; i< str.length; i++){
		if(str.substr(i, 1) != delim){
			if(delim == "=")
				arg += str.substr(i, 1);
			if(delim == ";")
				val += str.substr(i, 1);
		}
		else{
			if(delim == "="){
				delim = ";";
				continue;
			}
			if(delim == ";"){
				delim = "=";
				args[arg] = val;
				arg = "";
				val = "";
				continue;
			}
		}
	}
	args[arg] = val;

	if( paramName){
		return args[paramName];
	}
	else{
		return args;
	}
}


function f_c_Rectangle( w, h, x, y){
	this.x = x;
	this.y = y;
	this.width = w;
	this.height = h;
}

function isIP( ip, specialCase){
	//specialCase = true allows for leading 0 in an octet
	ip = ip || "";
	ip += "";

	if(ip.length == 0)
		return false;
	
	var ipArray = ip.split(".");
	if(ipArray.length != 4)	return false;
	if(ipArray[0] == 0) return false;
	if(isInRange(ipArray[0]) && 
		isInRange(ipArray[1]) &&
		isInRange(ipArray[2]) && 
		isInRange(ipArray[3]))
		return true;
	else
		return false;

	function isInRange( num){
		var c1="0";
		var c2="0";
		var c3="0";

		if(num.indexOf(":") != -1){
			num = num.substring(0, num.indexOf(":"));
		}
		if(specialCase){
			if(num.length > 3 || num.length == 0 )
				return false;
			if(num=="0" || num=="00" || num == "000")
				return true;
			c1 = num.substr(0,1);
			if(num.length > 1) c2 = num.substr(1,1);
			if(num.length == 3) c3 = num.substr(2,1);
			
			if(parseInt(c1) + "" != c1) return false;
			if(parseInt(c2) + "" != c2) return false;
			if(parseInt(c3) + "" != c3) return false;

			if(num.length == 3 && parseInt(c1) > 2 ||
				num.length == 3 && parseInt(c1) == 2 && parseInt(c2) > 5 ||
				num.length == 3 && parseInt(c1) == 2 && parseInt(c2) == 5 && parseInt(c3) > 5)
				return false;

			if(c1=="0" && c2=="0" && c3!="0" || c1=="0" && c2!="0" || c1!="0"){
				return true;
			}
		}
		else{
			if(parseInt(num) + "" != num)
				return false;
			if(num < 0 || num > 255)
				return false;
			else
				return true;
		}
	}
}

function getCookie( cookieName){
	if(!cookieName)
		return "";
	if (document.cookie.length > 0){
		cookieValueStart = document.cookie.indexOf( cookieName + "=");
		if (cookieValueStart != -1){ 
			cookieValueStart += cookieName.length + 1;
			cookieValueEnd = document.cookie.indexOf(";", cookieValueStart);
			if (cookieValueEnd == -1)
				cookieValueEnd = document.cookie.length
			return unescape(document.cookie.substring( cookieValueStart, cookieValueEnd));
		} 
	}
	return "";
}

function setCookie( cookieName, cookieValue, expiresMinutes, cookiePath, cookieDomain, cookieSecure){
	if(cookieName == "")
		return;
	cookieValue = cookieValue || "";
	var today_milisec = (new Date()).getTime();
	document.cookie = cookieName + "=" + escape(cookieValue) + 
		(expiresMinutes ? ("; expires=" + (new Date(today_milisec + parseInt(parseFloat(expiresMinutes)* 60 * 1000)).toGMTString())) : "" ) + 
		(cookiePath ? (";path=" + cookiePath) : "") +
		(cookieDomain ? (";domain=" + cookieDomain) : "") +
		(cookieSecure ? (";secure=" + cookieSecure) : "");
}

function getURLParam( p, url){
	url = url ? url.substring(url.indexOf("?"), url.length) : location.search;
	if(!p || url == "")
		return "";
	pValueStart = url.indexOf( p + "=");
	if (pValueStart != -1){ 
		pValueStart += p.length + 1;
		pValueEnd = url.indexOf("&", pValueStart);
		if (pValueEnd == -1)
			pValueEnd = url.length
		return unescape(url.substring( pValueStart, pValueEnd));
	}
	else{
		return "";
	}
}

getObjectCoords = function(htmlEl){
   var aY = htmlEl.offsetTop, 
   	aX = htmlEl.offsetLeft;   
   while((htmlEl = htmlEl.offsetParent) != null){
      aY += htmlEl.offsetTop;
      aX += htmlEl.offsetLeft;
   }
   return [aX, aY];
}

trim = function(string){
	return string.replace(/(^\s*) | \s*$/mg, "");
}