var user_agent = null;
var oX = 10;
var oY = 15;
var img_height = null;
var curEditedElement = null;
switch(true)
{
	case navigator.userAgent.indexOf("Opera")>-1 :
		user_agent = "Opera";
		window.captureEvents(Event.MOUSEMOVE);
		window.onmousemove = function(e){mouseX = e.pageX; mouseY = e.pageY};
		break;
	case navigator.userAgent.indexOf("MSIE")>-1 :
		user_agent = "IE";
		break;
	case navigator.userAgent.indexOf("Gecko")>-1 :
		user_agent = "Gecko";
		window.captureEvents(Event.MOUSEMOVE);
		window.onmousemove = function(e){mouseX = e.pageX; mouseY = e.pageY};
		break;
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}


function uriHashRewrite()
{
	var query	= location.href;
	var hash	= location.hash;
	
	if (!hash)
	{
		return false;
	}
	
	query 		= query.replace(hash, '');
	hash 		= hash.replace('#', '')
	
	if (query.split('?').length > 1)
	{
		query += '&__hash=' + hash;
	}
	else
	{
		query += '?__hash=' + hash;
	}
	
	return query;
}
function uriHashRm()
{
	var query	= location.href;
	var hash	= location.hash;
	
	if (!hash)
	{
		return false;
	}
	
	query = query.replace(hash, '');
	
	return query;
}
function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=")
		
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1 
			c_end=document.cookie.indexOf(";",c_start)
			if (c_end==-1) c_end=document.cookie.length
				return unescape(document.cookie.substring(c_start,c_end))
		} 
	}
	return ""
}

function uriHashToCookie()
{
	var hash = location.hash;
	
	if (hash)
	{
		//setCookie('__hash', hash);
		
		return true;
	}
	
	return false;
}

 
function historyChange(newLocation, historyData) 
{
	if (historyData)
	{
		get(historyData, true);
	}
	else
	{
		//get(location.href, true);
		//location.href = location.href;
	}
}
	

function otherShow()
{
	document.getElementById('_bullets').style.display = 'none';
	document.getElementById('_other').style.display = 'block';
}
function otherHide()
{
	document.getElementById('_bullets').style.display = 'block';
	document.getElementById('_other').style.display = 'none';
	document.getElementById('r6').value = '';
	document.getElementById('r7').value = '';
}
var sd = Array();
function showDown(id, url_)
{
	if (!sd[url_])
	{
		rget(url_, id);
		sd[url_] = true;
	}
	document.getElementById(id).style.visibility = 'visible';
}
function hideDown(id)
{
	document.getElementById(id).style.visibility = 'hidden';
}
function hintLoad(id, x, y, img_id)
{
	
	//var img = document.getElementById(img_id);
	//img_height = img.height;
	
	
	var hint = document.getElementById('drop_down');
	var source = document.getElementById(id);
	if (x) {oX = x} else {oX = 10}
	if (y) {oY = y} else {oY = 15}
	hint.innerHTML = source.innerHTML;
	/*
	source.onload = function (e) {
		hint.innerHTML = 'loaded';
	}
	*/
}
function hintShow()
{
	if (!isBlocked)
	{		
		var hint = document.getElementById('drop_down');
		
		var hintHeight = hint.clientHeight;
		
		if (isIE) {mouseX = window.event.x /*- document.body.scrollLeft*/}
		if (isIE) {mouseY = window.event.y + (document.documentElement?document.documentElement.scrollTop:document.body.scrollTop);}

		var limit = document.body.clientHeight + window.pageYOffset;
		var bottom = hintHeight + mouseY + oY;
	
		var hint = document.getElementById('drop_down');
		hint.style.left = (oX + mouseX) + 'px';
		if ((bottom)<limit)
		{
			hint.style.top = (oY + mouseY) + 'px';	
		}
		else
		{
			hint.style.top = limit - hintHeight + 'px'; 
		}		
	}
}
function hintHide()
{
	try
	{
		var hint = document.getElementById('drop_down');
		hint.style.left = '-10000px';
		hint.style.top = '-10000px';
	} catch (e) {}
}
function clearElem(id)
{
	try
	{
		document.getElementById(id).innerHTML = '';
		document.getElementById(id).style.display = 'none';
	}
	catch (e)
	{
		// debug here
	}
}
function setElem(id, text)
{
	document.getElementById(id).innerHTML = text;
	document.getElementById(id).style.display = 'block';
}
function onLoad(noHistory)
{
	// History

if (!noHistory){
		window.historyStorage.init();
		window.dhtmlHistory.create();

		try{	
			dhtmlHistory.initialize();
			dhtmlHistory.addListener(historyChange);

			if (dhtmlHistory.isFirstLoad()){}
		} catch (e) {}

	}

	var jsSrc = '';
	try
	{
		customOnload();
		var clear = document.getElementsByName('button_clear');
		clear[0].style.display = 'inline';
	} catch (e) {}
	// Errors mop-up
	if (document.body.getElementsByTagName('FORM'))
	{
	//alert(e');
		var error = document.body.getElementsByTagName('DIV');
		for (var i=0; i<error.length; i++)
		{
			if (error.item(i).getAttribute('name') == 'error')
			{
/*				if (error.item(i).innerHTML == '--!--' || error.item(i).innerHTML == '')
				{
					error.item(i).innerHTML = '';
					if (true || isIE) {error.item(i).style.display = 'none'}
				}
	*/		}
			if (error.item(i).getAttribute('name') == 'file_ajax')
			{
				error.item(i).style.display = 'inline';
			}
			if (error.item(i).getAttribute('name') == 'file_red')
			{
				error.item(i).style.display = 'none';
				error.item(i).innerHTML = '';
			}
			if(error.item(i).getAttribute('name') == 'embeded_script')
			{
				jsSrc += error.item(i).innerHTML;
			}
		}
	}
	// Textarea mop-up
	var textA = document.body.getElementsByTagName("TEXTAREA");
	for (var i=0; i<textA.length; i++)
	{
		if (textA.item(i).value == '--!--' || textA.item(i).value == '<!---->')
		{
			textA.item(i).value = '';
		}
	}
	
	// Password save for IE
	/*try 
	{
		if (isIE)
		{
			var elements = document.forms['login'].elements;
			elements['login'].value = getCookie('login');
			elements['psw'].value = decode64(getCookie('password')); //alert(elements['psw'].value);
		}	
	}
	catch (e) {}*/
	// --
	
	try {eval(jsSrc)} catch (e) {alert("Error in embeded script:" + e + '  (' + e.description + ')')};
	document.close();
	
}
function killMessage()
{
	if (document.body.getElementsByTagName('FORM'))
	{
		var error = document.body.getElementsByTagName('DIV');
		for (var i=0; i<error.length; i++)
		{
			if (error.item(i).className == 'message')
			{
				error.item(i).style.display = 'none';
				style.display = 'inline';
			}
		}
	}
}
function fileUpload(id)
{
	var src = document.getElementById('copy');
	var dest = document.getElementById('dest');
	dest.innerHTML = src.innerHTML;
}
function onIframeLoading(id, loadingShow)
{
	if (loadingShow)
	{
		var loading = document.getElementById(id + '_loading');
		loading.style.display = 'block';
		var done = document.getElementById(id + '_done');
		done.style.display = 'none';
	}
	if (isIE)
	{
		try
		{
			var iframe =  document.frames[id+'_frame'].document.getElementById('good');		
			var result = iframe.getAttribute('name');
			var loading = document.getElementById(id + '_loading');
			loading.style.display = 'none';
			var done = document.getElementById(id + '_done');
			done.style.display = 'block';
			var tmp = document.getElementById(id + '_tmp');
			tmp.value = result;
			if (pr = document.getElementById('img-preview'))
			{
				pr.src = '/' + result + '.preview.jpg';
			}
			var name = iframe.innerHTML;
			var show = document.getElementById(id + '_show');
			show.innerHTML = name;
			iframe.id = '0o0o0o';
		/*	
			s = '';
			for (i in iframe)
			{
				if (i.indexOf('on') != 0)
				s += '\n' + i;
			}
			document.write(s);
			document.close();*/
		} catch (e)
		{
			//alert(e.description);
			window.setTimeout('onIframeLoading("'+id+'")', 200);
		}
	}
	else
	{
		try
		{
			var iframe = top.document.getElementById(id+'_frame').contentDocument.getElementById('good');
			var result = iframe.getAttribute('name');
			var loading = document.getElementById(id + '_loading');
			loading.style.display = 'none';
			var done = document.getElementById(id + '_done');
			done.style.display = 'block';
			var name = iframe.innerHTML;
			var show = document.getElementById(id + '_show');
			show.innerHTML = name;
			iframe.id = '0o0o0o';

			if (pr = document.getElementById('img-preview'))
			{
				pr.src = '/' + result + '.preview.jpg';
			}

			if (tmp = document.getElementById(id + '_tmp'))
			{
				tmp.value = result;
			}
			else
			{
				var ul	= document.getElementById(id + '_ul');
				var li		= document.createElement('LI');
				var c		= parseInt(document.getElementById(id + '_c').value) + 1;
				document.getElementById(id + '_c').value = c;
				var re	= new RegExp("^[^_]+\_","ig");
				var iname = id.replace(re, '');

				li.innerHTML = name + '<input type="hidden" value="' + result + '" id="' + id + c + '_tmp" name="' + iname + c + '_uploadtmp" />';
				ul.appendChild(li);

				if (max = document.getElementById(id + '_max'))
				{
					if (parseInt(document.getElementById(id + '_max').value) <= c)
					{
						document.getElementById(id + '_block').style.display = 'none';
					}
				}
			}

		} catch (e)
		{
//			alert(e);
			window.setTimeout('onIframeLoading("'+id+'")', 200);
		}
	}
}
function activeStyle(obj)
{
	var item = document.body.getElementsByTagName('DIV');
	var currStyle	= obj.className;
	for (var i=0; i<item.length; i++)
	{
		if (item[i].getAttribute('name') == 'menu')
		{
			item[i].className = currStyle;
		}
	}
	obj.className	= currStyle + ' active';
}

function s(id)
{
	document.getElementById(id).style.display='block';
}

function h(id)
{
	document.getElementById(id).style.display='none';
}

function show(id)
{
	//	id.arguments
	if (id.constructor == Array)
	{
		for (var i=0; i<id.length; i++)
		{
			try {document.getElementById(id[i]).style.display = 'block';} catch (e) {};
		}
	}
	else
	{
			try {document.getElementById(id).style.display = 'block';} catch (e) {};
	}	
	
}
function hide(id)
{
	if (id.constructor == Array)
	{
		for (var i=0; i<id.length; i++)
		{
			try {document.getElementById(id[i]).style.display = 'none';} catch (e) {};
		}
	}
	else
	{
			try {document.getElementById(id).style.display = 'none';} catch (e) {};
	}	
	
}
function style(obj, stl)
{
	obj.className = stl;
}

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
		
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}
