/*
* Common JS function 
* Konstantin kolev kkolev@dotmedia.bg http://www.dotmedia.bg
*/
//firefox pach
if(typeof HTMLElement !="undefined" && !HTMLElement.prototype.insertAdjacentElement) {
    HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode) {
		switch (where) {
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this)
			break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
		case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (this.nextSibling) {
				this.parentNode.insertBefore(parsedNode,this.nextSibling);
         } else {
         	this.parentNode.appendChild(parsedNode);
         }
			break;
		} 
	} //function

	HTMLElement.prototype.insertAdjacentHTML = function (where,htmlStr)	{
        var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}


	HTMLElement.prototype.insertAdjacentText = function (where,txtStr) {
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}
}
function SetHtmlElementByName( id, newValue ) {

	if( document.getElementsByName )	{
		var htmlElm = document.getElementsByName( id );
		if( htmlElm && htmlElm != undefined && htmlElm.length > 0 ) {
			for( i=0;i<htmlElm.length;i++ ) {
				htmlElm[i].innerHTML = newValue;
			}
		}
	}
}
//---------------------------------------------------------
function FormatFloat(X) {
	with (new Object(Math.round(100*X)+'')) {
    	return substring(0,length-2) + '.' + substring(length-2,length)
	}
}
//---------------------------------------------------------
//old !! Cookie functionsn
function deleteDocumentCookie( oDoc, sName ) {
	oDoc.cookie = sName + "=";
}
function fetchDocumentCookie( oDoc, sName ) {
	var sValue = "";
	var index = 0;
	if( oDoc.cookie ) {
		index = oDoc.cookie.indexOf( sName + "=" );
	} else {
		index = -1;
	}
	if ( index < 0 ) {
		sValue = "";
	} else {
		var countbegin = (oDoc.cookie.indexOf( "=", index ) + 1);
		if ( 0 < countbegin ) {
			var countend = oDoc.cookie.indexOf( ";", countbegin );
			if ( countend < 0 )
					countend = oDoc.cookie.length;
			sValue = oDoc.cookie.substring( countbegin, countend );
		} else {
			sValue = "";
		}
	}
	return sValue;
}
/*-------------------------------------------------------------------------*/
//tool functions
function replaceAll( str, from, to ) {
  	var idx = str.indexOf( from );
 	while ( idx > -1 ) {
     	str = str.replace( from, to );
     	idx = str.indexOf( from );
	}
   return str;
}
function _escape(str) {
	str = replaceAll(str, ":", "&dw");
	str = replaceAll(str, "[", "&ls");
	str = replaceAll(str, "]", "&ds");
	str = replaceAll(str, "~", "&wl");
	//str = str.replace(":", "&dw");
	//:,[,],~
   return str;
}
function _unescape(str) {
  	str = replaceAll(str, "&dw", ":");
	str = replaceAll(str, "&ls", "[");
	str = replaceAll(str, "&ds", "]");
	str = replaceAll(str, "&wl", "~");
	return str;
}
function isObject(o) {
	return (typeof(o)=="object");	
}
function isArray(o) {
	return ((!o) || (!o.length) )?false: (isObject(o) && (o.length) &&(!isString(o)));	
}
function isFunction(o) {
	return (typeof(o)=="function");	
}
function isString(o) {
	return (typeof(o)=="string");	
}
//------------------------------------------------------
function Logout() {
	document.getElementById("method").value = "logout";
	document.forms[0].submit();
}
function Login() {
	document.getElementById("method").value = "login";
	document.forms[0].submit();
}
function openWindow(url,windowName,additionals) {
	window.open(url,windowName,additionals);
}
function funcname(f) {
 var s = f.toString().match(/function (\w*)/)[1];
 if ((s == null) || (s.length==0)) return "anonymous";
 return s;
}
function stacktrace() {
 var s = "";
 for (var a = arguments.caller; a !=null; a = a.caller) {
   s += "->"+funcname(a.callee) + "\n";
   if (a.caller == a) {s+="*"; break;}
 }
 return s;
}
function urlDecode(str){
    str=str.replace(new RegExp('\\+','g'),' ');
    return unescape(str);
}
function urlEncode(str){
    str=escape(str);
    str=str.replace(new RegExp('\\+','g'),'%2B');
    return str.replace(new RegExp('%20','g'),'+');
}

