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;
         }
     }

     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 UTF8UrlEncode(input)
{
    var output = "";
    var currentChar = '';
    
    for(var counter = 0; counter < input.length; counter++) 
    {
        currentChar = input.charCodeAt(counter);
        
        if((48 <= currentChar) && (currentChar <= 57))
            output = output + input.charAt(counter);
        else if((65 <= currentChar) && (currentChar <= 90))
            output = output + input.charAt(counter);
        else if((97 <= currentChar) && (currentChar <= 122))
            output = output + input.charAt(counter);
        else
            output =  output + UTF8UrlEncodeChar(currentChar); 
    }
    return output;
}

function UTF8UrlEncodeChar(input)
{
	if(input <= 0x7F) return "%" + input.toString(16);

	var leadByte = 0xFF80; var hexString = ""; var leadByteSpace = 5;
	while(input > (Math.pow(2, leadByteSpace + 1) - 1))
	{
		hexString = "%" + ((input & 0x3F) | 0x80).toString(16) + hexString;
		leadByte = (leadByte >> 1);
		leadByteSpace--;
		input = input >> 6;
	}
	return ("%" + (input | (leadByte & 0xFF)).toString(16) + hexString).toUpperCase();
}

function GSplit(input, start, end)
{
	var ret = input.split(start);
	var i;
	for(i = 1; i < ret.length; i++)
	{
		ret[i] = ret[i].split(end)[0];
	}
	ret.splice(0, 1);
	return ret;
}

function SubString(input, length, dot)  
{
	var newLength = 0;  
	var newStr = "";  
	var chineseRegex = /[^\x00-\xff]/g;  
	var singleChar = "";
	var strLength = input.replace(chineseRegex, "**").length;  
	for(var i = 0; i < strLength; i++)  
	{  
		singleChar = input.charAt(i).toString();  
		if(singleChar.match(chineseRegex) != null)  
		{
			newLength += 2;  
		}
		else
		{
			newLength++;  
		}
		if(newLength > length)  
	        {  
        		break;  
	        }
		newStr += singleChar;  
	}
	if(dot && strLength > length)  
	{
		newStr += "бн";  
	}
	return newStr;  
}

function GetCurrentStyle(obj, prop) {      
	if (obj.currentStyle) {         
		return obj.currentStyle[prop];      
	}       
	else if (window.getComputedStyle) {         
		propprop = prop.replace (/([A-Z])/g, "-$1");            
		propprop = prop.toLowerCase ();         
	return document.defaultView.getComputedStyle (obj,null)[prop];      
	}       
	return null;    
}

function GetColor(color) {
	if(color.indexOf(',')> -1) {
		color = color.replace(/[^0-9,]*/ig, '');
		var colors = color.split(',');
		colors[0] = parseInt(colors[0], 10).toString(16);
		colors[0] = colors[0].length == 2 ? colors[0] : "0" + colors[0];
		colors[1] = parseInt(colors[1], 10).toString(16);
		colors[1] = colors[1].length == 2 ? colors[1] : "0" + colors[1]; 
		colors[2] = parseInt(colors[2], 10).toString(16);
		colors[2] = colors[2].length == 2 ? colors[2] : "0" + colors[2]; 
		color = colors[0] + colors[1] + colors[2];
		color = "#" + color;
	}
	return color;
}

function HTMLEncode(html) {
	var temp = document.createElement ("div");
	(temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html);
	var output = temp.innerHTML;
	temp = null;
	return output;
}

function HTMLDecode(text)
{
	var temp = document.createElement("div");
	temp.innerHTML = text;
	var output = temp.innerText || temp.textContent;
	temp = null;
	return output;
}
