function removeDevice() {  
	new Ajax.Request(
		'?action=devices&mode=removeDevice', {
		method: 'post',
		onLoading: function() {
			$('phone_loader').style.display = '';
		},
		onComplete: function(transport) {
			viewHide('selected_device');
	        viewHide('no_device');
	        viewHide('remove_device');
	        $('phone_loader').style.display = 'none';
      	}
	});
	return false;
}

function removeFromCart(pid, id) {
	new Ajax.Request(
		'?action=products&mode=removeFromCart', {
		method: 'post',
		parameters: {pid:pid,id:id},
		onLoading: function() {
			$('cart_loader').style.display = '';
		},
		onComplete: function(transport) {
			var response = transport.responseText;
			$('cart_data').innerHTML = response;
			$('cart_loader').style.display = 'none';
      	}
	});
	return false;
}

function viewHide(id) {
	var div = document.getElementById(id);
	
	
	if (div.style.display == 'none')
		div.style.display = '';
	else if (div.style.display == '')
		div.style.display = 'none';
}


var ie=document.all;
var ns6=document.getElementById && !document.all;

function ietruebody()
{
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function phoneCheck(mode) {
	if (document.getElementById('caroussel'))
		document.getElementById('caroussel').style.visibility = 'hidden';
		
	if (mode != 'phonesReady') {
		createLayer('device_box','cart_manufdevice');
		
		
		winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight;
		winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;
	
	
		device_container = document.createElement('div');
		device_container.setAttribute('id', 'device_box');
		 
		device_container.style.width = '400px';
		device_container.style.height = '400px';
		device_container.style.zIndex = '1000';
		device_container.style.background = "#292929 ";
		device_container.style.position = 'absolute';
		var top = winheight/2 -200 + ((window.pageYOffset) ? window.pageYOffset : document.documentElement.scrollTop);
		var left = winwidth/2 -200;
		
		device_container.style.top = (top>0)?top + 'px':0;
		device_container.style.left = (left>0)?left + 'px':0;
		device_container.style.color = 'white';
		device_container.style.padding = '10px';
		
				
		document.body.appendChild(device_container);
	}
	else {
		var device_container = document.getElementById('device_box');
		
		var manufdevice = document.getElementById('manufdevice').cloneNode(true);
		document.body.removeChild(document.getElementById('manufdevice'));
		
		document.getElementById('device_box').innerHTML = '';
	}
	
	if (mode == 'phonesReady')
		device_container.innerHTML = '<div class="device_title"><h2 style="float:left;padding:0; margin:0;float:left;font-size:17px">Select your Device</h2> <img src="images/btn_close_g1.jpg" alt="" style="float:right; cursor:pointer;" onclick="closeLayer(\'device_box\',\'cart_manufdevice\')"><div style="clear:both"> </div></div>';
	else
		device_container.innerHTML = '<div class="device_title"><h2 style="float:left;padding:0; margin:0;float:left;font-size:17px">Select your Device</h2> <img src="images/btn_close_g1.jpg" alt="" style="float:right; cursor:pointer;" onclick="closeLayer(\'device_box\')"><div style="clear:both"> </div></div>';
	
	newdiv = document.createElement('div');
	newdiv.setAttribute('class', 'device_devices');
	
	newdiv.style.background = '#191919';
	newdiv.style.cssFloat = 'right';
	newdiv.style.styleFloat  = 'right';
	newdiv.style.height = '355px';
	newdiv.style.padding = '5px';
	newdiv.style.width = '170px';
	newdiv.style.overflow = 'auto';
	newdiv.innerHTML = '<strong>Devices</strong>';
	device_container.appendChild(newdiv);
	
	if (mode != 'addToCart') {
		var devices = document.getElementById('device_list').cloneNode(true);
		newdiv.appendChild(devices);
	}
	else
		newdiv.innerHTML +='<br /><br /><img src="images/ajax-loader.gif" style="height: 19px; width: 170px;" alt="" />';
	
	newdiv = document.createElement('div');
	newdiv.setAttribute('class', 'device_manuf');
	newdiv.style.background = '#191919';
	newdiv.style.cssFloat = 'left';
	newdiv.style.height= '190px';
	newdiv.style.padding = '5px';
	newdiv.style.width = '190px';
	newdiv.style.overflow = 'auto';
	newdiv.innerHTML = '<strong>Manufacturers</strong>';	
	device_container.appendChild(newdiv);
	
	if (mode != 'addToCart') {
		var manuf = document.getElementById('manufacturers').cloneNode(true);
		newdiv.appendChild(manuf);
	}	
	else
		newdiv.innerHTML +='<br /><br /><img src="images/ajax-loader.gif" style="height: 19px; width: 190px;" alt="" />';
		
	newdiv = document.createElement('div');
	newdiv.setAttribute('class', 'device_image');
	newdiv.setAttribute('id', 'device_image');
	newdiv.style.background = '#191919';
	newdiv.style.cssFloat = 'left';
	newdiv.style.padding = '5px';
	newdiv.style.height = '135px';
	newdiv.style.margin = '20px 0 0 0';
	newdiv.style.padding = '5px';
	newdiv.style.width = '190px';
	newdiv.style.textAlign = 'center';
	
	
	device_container.appendChild(newdiv);
	
	if (mode != 'phonesReady') {
		var manufdevice = document.getElementById('manufdevice').cloneNode(true);
		var del = document.getElementById('manufdevice');
		document.body.removeChild(del);
	}
	document.body.appendChild(manufdevice);
	
}

function currencySelection(url,query_string) {
	if (document.getElementById('caroussel'))
		document.getElementById('caroussel').style.visibility = 'hidden';
		
	createLayer('currency_box','cart_manufdevice');
	
	
	winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight;
	winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;


	currency_container = document.createElement('div');
	currency_container.setAttribute('id', 'currency_box');
	 
	currency_container.style.width = '200px';
	currency_container.style.height = '435px';
	currency_container.style.zIndex = '1000';
	currency_container.style.background = "#292929 ";
	currency_container.style.position = 'absolute';
	var top = winheight/2 -200 + ((window.pageYOffset) ? window.pageYOffset : document.documentElement.scrollTop);
	var left = winwidth/2 -200;
	
	currency_container.style.top = (top>0)?top + 'px':0;
	currency_container.style.left = (left>0)?left + 'px':0;
	currency_container.style.color = 'white';
	currency_container.style.padding = '10px';
	
			
	document.body.appendChild(currency_container);

	currency_container.innerHTML = '<div class="currency_title"><h2 style="float:left;padding:0; margin:0;float:left;font-size:17px">Select currency</h2> <img src="images/btn_close_g1.jpg" alt="" style="float:right; cursor:pointer;" onclick="closeLayer(\'currency_box\')"><div style="clear:both"> </div></div>';
	
	newdiv = document.createElement('div');
	newdiv.setAttribute('class', 'currencies');
	
	newdiv.style.background = '#191919';
	newdiv.style.height = '260px';
	newdiv.style.padding = '5px';
	newdiv.style.width = '190px';
	newdiv.style.marginTop = '10px';
	newdiv.style.overflow = 'auto';
	newdiv.innerHTML = '<strong>Currencies</strong>';
	
	params = query_string.split('&');
	currency_found = false;
	url += '?';
	if(query_string) {
		for(i=0;i<params.length;i++) {
			param = params[i];
			if(param.split('=')[0] == 'currency') {
				currency_found = true;
				break;
			}
			if(i == 0)
				url += param;
			else
				url += "&"+param;
		}
	}
	separator = ((params.length > 1 || !currency_found) && params[0] != '') ? '&' : '';
	
	currencies = "<br />" +
				 "<a href=\""+url+separator+"currency=EUR\">EUR</a><br />" +
				 "<a href=\""+url+separator+"currency=USD\">USD</a><br />" +
				 "<a href=\""+url+separator+"currency=JPY\">JPY</a><br />" +
				 "<a href=\""+url+separator+"currency=BGN\">BGN</a><br />" +
				 "<a href=\""+url+separator+"currency=CZK\">CZK</a><br />" +
				 "<a href=\""+url+separator+"currency=DKK\">DKK</a><br />" +
				 "<a href=\""+url+separator+"currency=EEK\">EEK</a><br />" +
				 "<a href=\""+url+separator+"currency=GBP\">GBP</a><br />" +
				 "<a href=\""+url+separator+"currency=HUF\">HUF</a><br />" +
				 "<a href=\""+url+separator+"currency=LTL\">LTL</a><br />" +
				 "<a href=\""+url+separator+"currency=LVL\">LVL</a><br />" +
				 "<a href=\""+url+separator+"currency=PLN\">PLN</a><br />" +
				 "<a href=\""+url+separator+"currency=RON\">RON</a><br />" +
				 "<a href=\""+url+separator+"currency=SEK\">SEK</a><br />" +
				 "<a href=\""+url+separator+"currency=SKK\">SKK</a><br />" +
				 "<a href=\""+url+separator+"currency=CHF\">CHF</a><br />" +
				 "<a href=\""+url+separator+"currency=ISK\">ISK</a><br />" +
				 "<a href=\""+url+separator+"currency=NOK\">NOK</a><br />" +
				 "<a href=\""+url+separator+"currency=HRK\">HRK</a><br />" +
				 "<a href=\""+url+separator+"currency=RUB\">RUB</a><br />" +
				 "<a href=\""+url+separator+"currency=TRY\">TRY</a><br />" +
				 "<a href=\""+url+separator+"currency=AUD\">AUD</a><br />" +
				 "<a href=\""+url+separator+"currency=BRL\">BRL</a><br />" +
				 "<a href=\""+url+separator+"currency=CAD\">CAD</a><br />" +
				 "<a href=\""+url+separator+"currency=CNY\">CNY</a><br />" +
				 "<a href=\""+url+separator+"currency=HKD\">HKD</a><br />" +
				 "<a href=\""+url+separator+"currency=IDR\">IDR</a><br />" +
				 "<a href=\""+url+separator+"currency=KRW\">KRW</a><br />" +
				 "<a href=\""+url+separator+"currency=MXN\">MXN</a><br />" +
				 "<a href=\""+url+separator+"currency=MYR\">MYR</a><br />" +
				 "<a href=\""+url+separator+"currency=NZD\">NZD</a><br />" +
				 "<a href=\""+url+separator+"currency=PHP\">PHP</a><br />" +
				 "<a href=\""+url+separator+"currency=SGD\">SGD</a><br />" +
				 "<a href=\""+url+separator+"currency=THB\">THB</a><br />" +
				 "<a href=\""+url+separator+"currency=ZAR\">ZAR</a><br />";
	newdiv.innerHTML += currencies;
	
	currency_container.appendChild(newdiv);

	currency_note = document.createElement('div');
	currency_note.style.cssFloat = 'left';
	currency_note.style.styleFloat  = 'left';
	currency_note.style.height = '60px';
	currency_note.style.padding = '5px';
	currency_note.style.width = '190px';
	currency_note.style.marginTop = '10px';

	currency_note.innerHTML = "Please note that you can view the prices of all software titles and your " +
							  "shopping basket in any chosen currency, but the actual invoicing is " +
							  "exercized in EUR currency. The exchange value of local currency against EUR " +
							  "is retrieved from central bank of Europe on a daily basis.";
	currency_container.appendChild(currency_note);
}

function createLayer(target,del) {
	var newdiv = document.createElement('div');
    newdiv.setAttribute('id', 'layer');
      
	newdiv.style.width = '100%';
	newdiv.style.height = document.body.clientHeight + 'px';
	newdiv.style.background = "#000000";
	newdiv.style.position = 'absolute';
	newdiv.style.top = 0;
	newdiv.style.left = 0;
	newdiv.style.opacity = '0.85';
	newdiv.style.filter = 'alpha(opacity=85)';
	if(del)
		newdiv.setAttribute('onclick','closeLayer("'+target+'","'+del+'");');
	else
		newdiv.setAttribute('onclick','closeLayer("'+target+'");');
	document.body.appendChild(newdiv);

}
var image;

function setDeviceImage(manuf, name) {
	if (name != '') {
		document.getElementById("device_image").innerHTML = "";
		document.getElementById("device_image").style.background = "white";
		
		image = 'images/phones/' + manuf + '/' + name;
		document.getElementById("device_image").innerHTML = '<img style="max-height: 135px" id="dev_img" src="' + image + '" alt=""/>';
		
		
		//preload_image = new Image();
		//preload_image.onload = showImages();
		//preload_image.src='images/phones/' + manuf + '/' + name;
	}
	else {
		document.getElementById("device_image").innerHTML = "";
		document.getElementById("device_image").style.background = "#191919";
	}
	
}
	


function closeLayer(id, del, caroussel) {
	var div = document.getElementById('layer');
	document.body.removeChild(div);
	div = document.getElementById(id);
	document.body.removeChild(div);
	
	if (del) {
		document.body.removeChild(document.getElementById(del));
	}
	if (document.getElementById('caroussel') && !caroussel)
		document.getElementById('caroussel').style.visibility = 'visible';
}

function addToCart(pid) {
	var product_id = pid;
	
	if ($('no_device').style.display != 'none') {

		new Ajax.Request(
			'?action=products&mode=getSupportedDevices', {
			method: 'post',
			parameters: {pid:product_id},
			onLoading: phoneCheck('addToCart'),
			onComplete: function(transport) {
				var response = transport.responseText;

				var newdiv = document.createElement('div');
				newdiv.setAttribute('id', 'cart_manufdevice');
		      	document.body.appendChild(newdiv);
		      	newdiv.innerHTML = response;
		      	
		      	phoneCheck('phonesReady');
		      	$('cart_manufdevice').style.display = 'none';
	      	}
		});
	  
	  return false;
	}
	
}

var firstPic = '';
var secondPic = '';
var thirdPic = '';
var fourthPic = '';
var picCount = 0;
	
function viewImages(id, s1, s2, s3, s4, mode) {
	
	var folder = '';
	if (mode == 'temp')
		folder = 'product_files/temp/' + id + '_';
	else 
		folder = 'product_files/images/' + id + '/';
		
	createLayer("image_preview");
	
	
	winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight;
	winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;


	device_container = document.createElement('div');
	device_container.setAttribute('id', 'image_preview');
	 
	device_container.style.width = '600px';
	device_container.style.height = '600px';
	device_container.style.zIndex = '1000';
	device_container.style.background = "#292929 ";
	device_container.style.position = 'absolute';
	var top = winheight/2 -300 + ((window.pageYOffset) ? window.pageYOffset : document.documentElement.scrollTop);
	var left = winwidth/2 -300;
	
	device_container.style.top = (top>0)?top + 'px':0;
	device_container.style.left = (left>0)?left + 'px':0;
	device_container.style.color = 'white';
	device_container.style.padding = '10px';
	device_container.innerHTML = '<div class="device_title"><h2 style="float:left;padding:0; margin:0;float:left;font-size:17px">Images</h2> <img src="images/btn_close_g1.jpg" alt="" style="float:right; cursor:pointer;" onclick="closeLayer(\'image_preview\')"><div style="clear:both"> </div></div>';
	document.body.appendChild(device_container);
	
	if (s1 != '') {
		firstPic = s1;
		picCount++;
	}
	
	if (s2 != '') {
		if (firstPic == '') firstPic = s2;
		else secondPic = s2;
		
		picCount++;
	}
	if (s3 != '') {
		if (firstPic == '') firstPic = s3;
		else if (secondPic == '') secondPic = s3;
		else thirdPic = s3;
		
		picCount++;
	}
	if (s4 != '') {
		if (firstPic == '') firstPic = s4;
		else if (secondPic == '') secondPic = s4;
		else if (thirdPic == '') thirdPic = s4;
		else fourthPic = s4;
		
		picCount++;
	}
	
		
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', 'img');
	newdiv.style.width = '590px';
	newdiv.style.height = '443px';
	newdiv.style.background = "#191919";
	newdiv.style.textAlign = 'center';
	newdiv.style.padding = '5px';
	if (picCount > 1)
		newdiv.innerHTML = '<img style="cursor:pointer" onclick="viewImage(\'' + folder + '\',' + id + ',\'' + secondPic +'\')" src="' + folder + firstPic +'" alt="" id="preview_img"/>';
	else
		newdiv.innerHTML = '<img src="' + folder + firstPic +'" alt="" id="preview_img"/>';
	device_container.appendChild(newdiv);
	

	var img_preview = document.getElementById('preview_img');
	img_preview.style.marginTop = 443/2 - img_preview.height/2 + 'px';
	
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', 'img');
	newdiv.style.width = '590px';
	newdiv.style.height = '93px';
	newdiv.style.background = "#191919";
	newdiv.style.textAlign = 'center';
	newdiv.style.padding = '5px';
	newdiv.style.marginTop = '10px'
	
	
	if (s1 != '')
		newdiv.innerHTML = '<img onclick="viewImage(\'' + folder + '\',' + id + ',\'' + s1+'\')" style="cursor:pointer;margin: 22px 10px 0 0" src="' + folder + s1 +'" alt="" height="50" />';
		
	if (s2 != '')
		newdiv.innerHTML += '<img onclick="viewImage(\'' + folder + '\',' + id + ',\'' + s2+'\')" style="cursor:pointer;margin: 22px 10px 0 0" src="' + folder + s2 +'" alt="" height="50"/>';
		
	if (s3 != '')
		newdiv.innerHTML += '<img onclick="viewImage(\'' + folder + '\',' + id + ',\'' + s3+'\')" style="cursor:pointer;margin: 22px 10px 0 0" src="' + folder + s3 +'" alt="" height="50"/>';
	
	if (s4 != '')
		newdiv.innerHTML += '<img onclick="viewImage(\'' + folder + '\',' + id + ',\'' + s4+'\')" style="cursor:pointer;margin: 22px 10px 0 0" src="' + folder + s4 +'" alt="" height="50"/>';
		
		
	device_container.appendChild(newdiv);
	
	
}

function centerImg() {
	alert(preload_image.height);
}

function viewImage(folder, id, pic) {
	var img = document.getElementById('img');
	var nextPic = '';
	if (pic == firstPic && picCount >1) {
		nextPic = secondPic
	}
	else if (pic == secondPic) {
		if (picCount >2)
			nextPic = thirdPic;
		else
			nextPic = firstPic;
	}
	
	else if (pic == thirdPic) {
		if (picCount >3)
			nextPic = fourthPic;
		else
			nextPic = firstPic;
	}
	else
		nextPic = firstPic;
	
	img.innerHTML  = '<img onclick="viewImage(\'' + folder + '\',' + id + ',\'' + nextPic+'\')" style="cursor:pointer" src="' + folder + pic +'" alt="" id="preview_img" />';
	var img_preview = document.getElementById('preview_img');
	img_preview.style.marginTop = 443/2 - img_preview.height/2 + 'px';
}

function DisplayProduct(title, category, description, id, price, discount, image, currency) {
	document.getElementById('caroussel').style.visibility = 'hidden';
	createLayer('product_details');
	
	
	winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight;
	winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;


	container = document.createElement('div');
	container.setAttribute('id', 'product_details');
	 
	container.style.width = '400px';
	//container.style.height = '235px';
	container.style.zIndex = '1000';
	container.style.background = "#292929 ";
	container.style.position = 'absolute';
	var top = winheight/2 -167 + ((window.pageYOffset) ? window.pageYOffset : document.documentElement.scrollTop);
	var left = winwidth/2 -200;
	
	container.style.top = (top>0)?top + 'px':0;
	container.style.left = (left>0)?left + 'px':0;
	container.style.color = 'white';
	container.style.padding = '10px';
	container.innerHTML = '<div class="device_title"><h2 style="float:left;padding:0; margin:0;float:left;font-size:17px">Product Details</h2> <img src="images/btn_close_g1.jpg" alt="" style="float:right; cursor:pointer;" onclick="closeLayer(\'product_details\')"><div style="clear:both"> </div></div>';
	document.body.appendChild(container);
	
	details = document.createElement('div');
	details.setAttribute('id', 'details');
	details.style.background = '#191919';
	//details.style.height = '190px';
	details.style.padding = '5px';
	details.style.width = '390px';
	
	var phone_check = '';
	if (document.getElementById('no_device').style.display == '') {
		phone_check = 'onclick="addToCart(' + id + ');closeLayer(\'product_details\',false, \'no\');return false;"'; 
	}
	
	if (discount != '0.00' && discount != price)
		details.innerHTML = '<h2 style="font-size:13px">' + title + '</h2>' +
							'<a href="?action=products&amp;mode=productDetails&amp;product_id=' + id + '" >' + 
							'<img src="'+ image + '" alt="' + title + '" style="float:left;margin-right:5px"/></a>' +
							'<span>' + category + '</span><br /><br />' +
							'<span>' + description + '</span><br /><br />' +
							'<a href="?action=products&amp;mode=productDetails&amp;product_id=' + id + '" >Read more</a>' + 
							'<a href="?action=products&amp;mode=addToCart&amp;product_id=' + id + '" ' + phone_check + '><img style="float: right; margin-top: 5px;" alt="" src="images/cart.jpg"/></a>' +
							'<div style="float:right;width:100px;margin: 0px 10px">' +
							'<span style="float:right;color:#A26262;text-decoration:line-through;">' + price + ' ' + currency + '</span><br />' +
							'<span style="float:right">' + discount + ' ' + currency + '</span></div><div style="clear:both"> </div>';
							
	else
		details.innerHTML = '<h2 style="font-size:13px">' + title + '</h2>' +
							'<a href="?action=products&amp;mode=productDetails&amp;product_id=' + id + '" >' + 
							'<img src="'+ image + '" alt="' + title + '" style="float:left;margin-right:5px"/></a>' +
							'<span>' + category + '</span><br /><br />' +
							'<span>' + description + '</span><br /><br />' +
							'<a href="?action=products&amp;mode=productDetails&amp;product_id=' + id + '" >Read more</a>' +
							'<a href="?action=products&amp;mode=addToCart&amp;product_id=' + id + '" ' + phone_check + '><img style="float: right; margin-top: 5px;" alt="" src="images/cart.jpg"/></a>' +
							'<span style="float: right;margin: 10px">' + price + ' ' + currency + '</span><div style="clear:both"> </div>'	;
	
	container.appendChild(details);
}

function checkUsernameAvailability() {
	var username = $('reg_username').value;
	if (username.length > 2) {
		new Ajax.Request(
			'?action=user&mode=isUsernameAvailable', {
			method: 'post',
			parameters: {username:username},
			onComplete: function(transport) {
				if (transport.responseText.match(1))
      				$('isAvailable').update('Username available').setStyle({ color: 'green' });
				else 
					$('isAvailable').update('Username already taken').setStyle({ color: 'red' });
			}
		});
	}
	else {
		$('isAvailable').innerHTML = "";
	}
}

function makeLicenseRequest(index) {
	var product_id = $("product_id").value;
	var url = $("licence_script").value;
	var imei = $("imei").value;
	var hash_string = $("hash_string").value;
	
	if (imei && url && product_id) {

		new Ajax.Request(
			'?action=extranet&mode=retrieveLicense', {
			method: 'post',
			parameters: {product_id:product_id,imei:imei,hash_string:hash_string,url:url},
			onLoading: function() {
				$("test_license_"+index).innerHTML = "<img src=\"images/ajax_loader_small.gif\" alt=\"\" />";
			},
			onComplete: function(transport) {
				var response = transport.responseText;
				$("response").innerHTML = response;
				$("test_license_"+index).innerHTML = "Test license script";
	      	}
		});
	  
	  return false;
	}
	else
		$("response").innerHTML = "All required fields are not filled: Script url and imei";
}

function showEditBox(e,target,id,constant) {
	createLayer('editBox',1);
	
	var editBox = document.createElement('div');
	editBox.setAttribute('id','editBox');

	editBox.style.width = 287+"px";
	editBox.style.height = 310+"px";
	
	editBox.style.position = 'absolute';
	
	winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight;
	winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;
	var top = winheight/2 -200 + ((window.pageYOffset) ? window.pageYOffset : document.documentElement.scrollTop);
	var left = winwidth/2 -200;
	
	editBox.style.top = (top>0)?top + 'px':0;
	editBox.style.left = (left>0)?left + 'px':0;
	
	editBox.style.background = "#000000";
	editBox.style.border = "2px solid #000";
	editBox.style.color = "#000000";
	
	editBox.innerHTML = "<input type='hidden' name='translation_id' id='translation_id' value='"+id+"' />"; 
	editBox.innerHTML += "<textarea id='new_text' rows='19' cols='38'>"+target.title+"</textarea><br />";
	
	if(constant == '')
		editBox.innerHTML += "<span onclick='updateTranslation(\"editBox\",\""+target.id+"\");'><img src='images/buttons/en/update_btn.jpg' alt='' /></span>";
	else if(constant != '' && id == '')
		editBox.innerHTML += "<span onclick='updateConstant(\"editBox\",\""+constant+"\");'><img src='images/buttons/en/update_btn.jpg' alt='' /></span>";
	else {
		editBox.innerHTML += "<input type='hidden' name='constant' id='constant' value='"+constant+"' />"; 
		editBox.innerHTML += "<span onclick='addTranslation(\"editBox\",\""+target.id+"\");'><img src='images/buttons/en/add_btn.jpg' alt='' /></span>";
	}
	document.body.appendChild(editBox);
	
	$("new_text").focus();
}

function showLanguageEditBox(event,target,lang_id) {
	createLayer('editBox',1);
	
	var editBox = document.createElement('div');
	editBox.setAttribute('id','editBox');

	editBox.style.width = 287+"px";
	editBox.style.height = 110+"px";
	
	editBox.style.position = 'absolute';
	
	winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight;
	winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;
	var top = winheight/2 -200 + ((window.pageYOffset) ? window.pageYOffset : document.documentElement.scrollTop);
	var left = winwidth/2 -200;
	
	editBox.style.top = (top>0)?top + 'px':0;
	editBox.style.left = (left>0)?left + 'px':0;
	
	editBox.style.background = "#000000";
	editBox.style.border = "2px solid #000";
	editBox.style.color = "#000000";
	
	editBox.innerHTML = "<input type='hidden' name='lang_id' id='lang_id' value='"+lang_id+"' />"; 
	editBox.innerHTML += "<input type='text' name='new_language' id='new_language' value='"+target.innerHTML+"' /><br />";
	editBox.innerHTML += "<input type='text' maxlength='2' size='2' name='new_abbreviation' id='new_abbreviation' value='"+target.title+"' /><br />";
	
	editBox.innerHTML += "<span onclick='updateLanguage(\"editBox\");'><img src='images/buttons/en/update_btn.jpg' alt='' /></span>";
	document.body.appendChild(editBox);
	
	$("new_language").focus();
}

function updateLanguage(target) {
	var new_language = $("new_language").value;
	var new_abbreviation = $("new_abbreviation").value;
	var id = $("lang_id").value;
	
	new Ajax.Request(
		'?action=maintenance&mode=lexicon&function=updateLanguage', {
		method: 'post',
		parameters: {new_abbreviation:new_abbreviation,new_language:new_language,id:id},
		onLoading: function() {
			createLexiconLayer();
		},
		onComplete: function(transport) {
			var response = transport.responseText;
			$("ajax_message").innerHTML = response;
			$("ajax_message").style.display = '';
			$("lang_"+id).innerHTML = new_language;
			$("lang_"+id).title = new_abbreviation;
			$("lang_"+id).setAttribute('onclick', "showLanguageEditBox(event,this,'"+id+"')");
			closeLayer("loader",1,0);
      	}
	});
	return false;
}

function createLexiconLayer() {
	document.body.removeChild($("editBox"));
	var loader = document.createElement('img');
	loader.setAttribute("src","images/ajax_loader_small.gif");
	loader.id = "loader";
	loader.style.position = "absolute";
	winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight;
	winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;
	var top = winheight/2 -200 + ((window.pageYOffset) ? window.pageYOffset : document.documentElement.scrollTop);
	var left = winwidth/2 -200;
	
	loader.style.top = (top>0)?top + 160 +'px':0;
	loader.style.left = (left>0)?left + 143 + 'px':0;
	document.body.appendChild(loader);
}

function updateConstant(target,constant) {
	var new_constant = $("new_text").value;

	new Ajax.Request(
		'?action=maintenance&mode=lexicon&function=updateConstant', {
		method: 'post',
		parameters: {new_constant:new_constant,constant:constant},
		onLoading: function() {
			createLexiconLayer();
		},
		onComplete: function(transport) {
			var response = transport.responseText;
			$("ajax_message").innerHTML = response;
			$("ajax_message").style.display = '';
			$(constant).innerHTML = new_constant;
			$(constant).title = new_constant;
			$(constant).setAttribute('onclick', "showEditBox(event,this,'','"+new_constant+"')");
			$(constant).id = new_constant;
			closeLayer("loader",1,0);
      	}
	});
	return false;
}

function updateTranslation(target,target_id) {
	var lexicon_id = $("translation_id").value;
	var translation = $("new_text").value;

	new Ajax.Request(
		'?action=maintenance&mode=lexicon&function=updateTranslation', {
		method: 'post',
		parameters: {lexicon_id:lexicon_id,translation:translation},
		onLoading: function() {
			createLexiconLayer();
		},
		onComplete: function(transport) {
			var response = transport.responseText;
			$("ajax_message").innerHTML = response;
			$("ajax_message").style.display = '';
			$(target_id).innerHTML = translation.truncate(10,'...');
			$(target_id).title = translation;
			closeLayer("loader",1,0);
      	}
	});
	return false;
}

function addTranslation(target,target_id) {
	var constant = $("constant").value;
	var translation = $("new_text").value;
	var lang_id = $("translation_id").value;

	new Ajax.Request(
		'?action=maintenance&mode=lexicon&function=addTranslation', {
		method: 'post',
		parameters: {constant:constant,translation:translation,lang_id:lang_id},
		onLoading: function() {
			createLexiconLayer();
		},
		onComplete: function(transport) {
			var response = transport.responseJSON;
			$("ajax_message").innerHTML = response.message;
			$("ajax_message").style.display = '';
			$(target_id).innerHTML = translation.truncate(10,'...');
			$(target_id).setAttribute('onclick', 'showEditBox(event,this,'+response.id+',"")');
			$(target_id).title = translation;
			closeLayer("loader",1,0);
      	}
	});
	return false;
}