var ammount = 0;
var rates = {2:{3:0.8000,12:0.8000,13:0.8000,9:0.8000,8:0.8000,7:0.8000,4:0.9000},3:{10:0.9200,13:0.9300,4:0.9300,8:0.9300,7:0.9400,9:0.9400,2:0.9500,12:0.9500},4:{10:0.9300,13:0.9300,8:0.9500,2:0.9500,9:0.9600,7:0.9600,12:0.9600,3:0.9800},7:{10:0.9200,8:0.9400,4:0.9500,3:0.9500,9:0.9500,2:0.9600,12:0.9600,13:0.9600},8:{10:0.9000,3:0.9200,4:0.9200,7:0.9200,9:0.9200,12:0.9300,2:0.9300},9:{13:0.9200,8:0.9400,4:0.9500,3:0.9500,7:0.9500,2:0.9500,12:0.9600},10:{13:0.9700,12:0.9700,9:0.9700,7:0.9700,2:0.9800,4:0.9800,3:1.0000}};
var direction_unavailable = 'Sorry, this direction is not available.';
var too_little_amount = 'The amount is too small';
var contact_operator = 'Please contact operator for special conditions.';

var max_amount = 30000;
var min_amount = 10;
var old_direction = false;

function gen_uuid() {
    var uuid = ""
    for (var i=0; i < 32; i++) {
        uuid += Math.floor(Math.random() * 16).toString(16);
    }
    return uuid
}

function fix_src_select() {
    var si = document.forms[0].id_currency_from.selectedIndex;
    var from = $("form.order_form #id_currency_from").val();
    var to = $("form.order_form #id_currency_to").val();
    if (from == to) {
	document.forms[0].id_currency_from.selectedIndex = (document.forms[0].id_currency_from.selectedIndex == 0) ? 1 : 0;
	recalc(amount, 2);
	from = $("form.order_form #id_currency_from").val();
	to = $("form.order_form #id_currency_to").val();
    }
    if (!rates[from]) {
	$("#src_info").html(direction_unavailable);
	old_direction = false;
	return;
    }
    $("#src_info").html('');
}

function fix_dst_select() {
    var si = document.forms[0].id_currency_to.selectedIndex;
    var from = $("form.order_form #id_currency_from").val();
    var to = $("form.order_form #id_currency_to").val();
    if (from == to) {
	document.forms[0].id_currency_to.selectedIndex = (document.forms[0].id_currency_to.selectedIndex == 0) ? 1 : 0;
	recalc(amount, 1);
	from = $("form.order_form #id_currency_from").val();
	to = $("form.order_form #id_currency_to").val();
    }
    if (!rates[from]) {
	$("#dst_info").html(direction_unavailable);
	old_direction = true;
	return;
    }
    if (!rates[from][to]) {
        $("#dst_info").html(direction_unavailable);
	old_direction = true;
	return;
    }
    $("#dst_info").html('');
}

function validate_exchange() {
    var max_amount = 0;
    var from = $("form.order_form #id_currency_from").val();
    var to = $("form.order_form #id_currency_to").val();
    amount = $("form.order_form #id_amount_snd").val();
    var from = $("form.order_form #id_currency_from").val();
    
    if (amount >= max_amount && max_amount > 0) {
	alert(contact_operator);
	return false;
    }
    if (amount < min_amount) {
	alert(too_little_amount);
	return false;
    }
}

function recalc(in_amount, direction) {
    amount = in_amount;
    var from = $("form.order_form #id_currency_from").val();
    var to = $("form.order_form #id_currency_to").val();
    var rate = rates[from][to];
    var fee = 0;
    var res = 0;
    var fixed_rate = 0;
    if (!rate) {
	if (direction == 1) {
	    $("#dst_info").html(direction_unavailable);
	} else {
	    $("#src_info").html(direction_unavailable);
	}
        return false;
    }
    if (from == to) {
    	return false;
    }
    $("#dst_info").html('');
    $("#src_info").html('');
    if (direction == 1) {
        res = in_amount*rate;
    	if (res < 10)
            res = Math.ceil(res*100)/100;
    	else
            res = Math.round(res*100)/100;
	if (in_amount-res < 2) {
	    res -= (2-(in_amount-res));
	}
    	if (res < 0) res = 0;
    	$("form.order_form #id_amount_rcv").val(res);
    } else {
        res = in_amount/rate;
        if (res < 10)
            res = Math.ceil(res*100)/100;
    	else
            res = Math.round(res*100)/100;
	if (in_amount-res < 2) {
	    res += (2-(in_amount-res));
	}
    	if (res < 0) res = 0;
	$("form.order_form #id_amount_snd").val(res);
    }
    return true;
}

function start() {
    amount = 0;
    if($("form.order_form #id_amount_rcv").val() == '') {
	$("form.order_form #id_amount_rcv").val(amount);
    }
    if ($("form.order_form #id_amount_snd").val() == '') {
	$("form.order_form #id_amount_snd").val(amount);
    }
    fix_src_select();
    fix_dst_select();
}

$(document).ready(
        function(){
	    start();
	    $("form.order_form #id_amount_snd").keydown(
                function(e){
		    var n = $("form.order_form #id_amount_snd").val();
		    if (isFinite(n))
			amount = n;
		}
	    );
	    $("form.order_form #id_amount_rcv").keydown(
                function(e){
		    var n = $("form.order_form #id_amount_rcv").val();
		    if (isFinite(n))
			amount = n;
		}
	    );
	    $("form.order_form #id_amount_snd").keyup(
                function(e){
		    if ((e.keyCode < 32 && e.keyCode != 8) || e.keyCode == 44 || e.keyCode == 46)
			return false;
		    var from = $("form.order_form #id_amount_snd").val();
		    var lch = from.charAt(from.length-1);
		    if (lch == ',') {
		    	    from = from.substr(0, from.length-1)+'.';
			    $("form.order_form #id_amount_snd").val(from);
			    lch = '.';
		    }
		    if (isFinite(from) && from < 9999999) {
			if (lch == '.') return false;
		    } else {
    			from = amount;
		    }
		    if (from == amount)
		        return false;
		    if (from < 0)
    		        from = 0;
		    if (e.keyCode == 8 && from == '0')
		        from = '';
		    recalc(from, 1);
		}
	    );

	    $("form.order_form #id_amount_rcv").keyup(
                function(e){
		    if ((e.keyCode < 32 && e.keyCode != 8) || e.keyCode == 44 || e.keyCode == 46)
			return false;
		    var to = $("form.order_form #id_amount_rcv").val();
		    var lch = to.charAt(to.length-1);
		    if (lch == ',') {
		    	    to = to.substr(0, to.length-1)+'.';
			    $("form.order_form #id_amount_rcv").val(to);
			    lch = '.';
		    }
		    if (isFinite(to) && to < 9999999) {
			if (lch == '.') return false;
		    } else {
    			to = amount;
		    }
		    if (to == amount)
		        return false;
		    if (to < 0)
    		        to = 0;
		    if (e.keyCode == 8 && to == '0')
		        to = '';
		    $("form.order_form #id_amount_rcv").val(to);
		    recalc(to, 2);
		}
	    );

	    $("form.order_form #id_currency_from").change(
                function(){
		    fix_dst_select();
		    if (!old_direction) {
			recalc(amount, 1);
		    }
		}
	    );
	    $("form.order_form #id_currency_to").change(
                function(){
		    fix_src_select();
		    if (!old_direction) {
			recalc(amount, 1);
		    }
		}
	    );
	    $("form.order_form #id_clear").click(
                function(){
		    if (confirm(reset_confirm)) {
			document.forms[0].reset();
			start();
			return false;
		    }
		}
	    );

	    $("form.order_form #id_cancel").click(
                function(){
		    if(confirm(reset_confirm)) {
			location.replace("/order/cancel/"+ order_id +"/");
			return false;
		    }
		}
	    );
	    $("form.order_form #id_preview").click(
                function(){
		    return validate_exchange();
		}
	    );
	
	}
);

