var wait_and_copy_interval;
var wait_and_copy_src;
var wait_and_copy_dest;

var mouse_is_inside_popup = false;

$(document).ready(function()
{
	init_popup_menu_handler();
});


function init_popup_menu_handler()
{
	$('.pm').hover(function() { mouse_is_inside_popup = true; },
					function() { mouse_is_inside_popup = false; });

    $('body').mouseup(function(){ if(!mouse_is_inside_popup) hide_all_popups(); });
}

function wait_and_copy(src, dest)
{
	wait_and_copy_src = src;
	wait_and_copy_dest = dest;
	wait_and_copy_interval = window.setInterval(function() {
	    var t = $('#' + wait_and_copy_src)[0].contentWindow.document.body.innerHTML;
		
		if (t != '')
		{
			window.clearInterval(wait_and_copy_interval);
			
			var m = /^\!(.*)/.exec(t);
			if (m)
				$('#' + wait_and_copy_dest).append("<div class='errline'>" + m[0] + "</div>");
			else
				$('#' + wait_and_copy_dest).append("<div class='linkline'>" + t + "</div>");
		}
	}, 1000);
	
	return true;
}

function popup_menu(menu, ref)
{
	var menu_id = '#' + menu;
	
	hide_all_popups();
	
	var el = ref;
	for (var lx=0, ly=0;
	         el != null;
	         lx += el.offsetLeft, ly += el.offsetTop, el = el.offsetParent);
	
	$(menu_id)[0].style.left = (lx - 30) + 'px';
	$(menu_id)[0].style.top = (ly + 40) + 'px';
	$(menu_id).slideDown(150);
	
	return false;
}

function hide_all_popups()
{
	$('.pm').hide();
}

function lkoption(sel)
{
	var i = sel.selectedIndex;
	var t = parseInt(sel.options[i].value);
	
	if (t >= 10 && t < 15)
		$('#evallimit').removeAttr('disabled');
	else
		$("#evallimit").attr('disabled', 'disabled');
}

function lkproduct(sel, optsel)
{
	var i = sel.selectedIndex;
	var prod_id = parseInt(sel.options[i].value);
	var prod_options = $('#p' + prod_id)[0].value;
	
	var j = optsel.selectedIndex;
	var opt = parseInt(optsel[j].value);
	
	if (prod_options =~ /m/ && (opt < 10 || opt == 15))
	{
		$('#users').removeAttr('disabled');
		$('#channels').removeAttr('disabled');
	}
	else
	{
		$('#users').attr('disabled', 'disabled');
		$('#channels').attr('disabled', 'disabled');
	}
	
	if (opt == 10 || opt == 11)
		$('#evallimit').removeAttr('disabled');
	else
		$('#evallimit').attr('disabled', 'disabled');
}

// Store functions

function price_string(price)
{
	var ps = ('' + price).replace(/\.(\d)$/, '.$1' + '0');
	
	if (ps.indexOf('.') == -1)
		ps += '.00';
	
	return ps.replace(/(\.\d\d)\d*$/, '$1');
}

function currency_conversion(from, to, value)
{
	var conversion = from + to;
	var scale = to == from ? 1.0 : parseFloat($('#' + conversion).val());
	var price = parseFloat(value) * scale;
	
	return price_string(price) + ' ' + to;
}

function set_currency(currency)
{
	// Update displayed prices.
	$('#price').each(function(index) {
		$(this)[0].innerHTML = currency_conversion($('#currency').val(), currency, $('#totalprice').val());
	});
	
	// Save the new currency in hidden fields in forms.
	$('.selcur').each(function(index) {
		$(this).val(currency);
	});
}

function update_user_channel_price()
{
	var users = parseInt($('#users').val());
	var channels = $('#channels').val();
	var matrix = $('#pricing_' + users).val().split(' ');
	var currency = $('#currency').val();
	
	var channel_list = $('#channellist').val().split(' ');
	var channel_index = 0;

	for (var c = 0; c < channel_list.length; c ++)
	{
		if (channel_list[c] == channels)
			break;
			
		channel_index ++;
	}

	// Set the total in the quoted currency.
	$('#totalprice').val(matrix[channel_index]);
	
	// Converted from the quoted currency to the selected currency.
	var selected_currency = $('#selectedcurrency').val();
	$('#price')[0].innerHTML = currency_conversion(currency, selected_currency, $('#totalprice').val());
}

function update_upgrade_price()
{
	var ppcString = $('#priceperchannel').val();
	var pricePerChannel = parseFloat(ppcString);
	var adminCharge = parseFloat($('#admincharge').val());

	if (ppcString != '')
	{
		var users = $('#users')[0].selectedIndex;
		var userBase = $('#userbase').val();
		var userBaseIndex = parseInt($('#userbaseindex').val());
		var newUsers = users + userBaseIndex - 1;
		var channelBase = parseInt($('#channelbase').val());
		var channels = parseInt($('#channels').val());
		var matrix = $('#pricing_' + userBase).val().split(' ');
		var userPrice = parseFloat(newUsers > 0 ? matrix[newUsers] : '0');
		var channelPrice = (channels - channelBase) * pricePerChannel;
		
		$('#totalprice').val((userPrice + channelPrice) * (1.0 + adminCharge));
//		alert($('#totalprice').val() + '/' + users + '/' + userBaseIndex + '/' + newUsers + '/' + userPrice + '/' + channelPrice);
	}
	else
	{
		var userBase = $('#userbase').val();
		var channelBase = parseInt($('#channelbase').val());
		var priceBase = userBase == '0' ? '0' : $('#pricing_' + userBase).val().split(' ')[channelBase - 1];
		
		var newUsers = $('#users').val();
		var newChannels = $('#channels').val();
		var newPrice = $('#pricing_' + newUsers).val().split(' ')[newChannels - 1];
		
		var upgradePrice = parseFloat(newPrice.replace(/[\s\,]/, '')) - parseFloat(priceBase.replace(/[\s\,]/, ''))
		$('#totalprice').val(upgradePrice * (1.0 + adminCharge));
	}

	// Converted from the quoted currency to the selected currency.
	var currency = $('#currency').val();
	var selected_currency = $('#selectedcurrency').val();
	$('#price')[0].innerHTML = currency_conversion(currency, selected_currency, $('#totalprice').val());
}

function update_volume_price()
{
	var base_price = $('#baseprice').val();
	var admin = 0.0;
	var volume = $('#volume').val();
	var price = ($('#vp_' + volume).val() - base_price) * (1 + admin);
	$('#totalprice').val(price);
	
	// Converted from the quoted currency to the selected currency.
	var currency = $('#currency').val();
	var selected_currency = $('#selectedcurrency').val();
	$('#price')[0].innerHTML = currency_conversion(currency, selected_currency, $('#totalprice').val());
}

