function updateCharacter(item)
{
	var ajaxData = {"item":item};
	$.ajax({
		type:'GET',
		url:"injector_uni4.php",
		data:ajaxData,
		success:function(data){
			$("#character").html(data);
		}
	});
}

function unequipAll(){
	if(!confirm("Are you sure you want to unequip ALL items?")) return;

	var ajaxData = {"unequipAll":true};
	$.ajax({
		type:'POST',
		url:"injector_uni4.php",
		data:ajaxData,
		success:function(data){
			$("#character").html(data);
		}
	});
}

function confirmSellback(url,price){
	if(confirm("Are you sure you want to sellback this item?\nIt is currently valued at "+price+" auros.")){
		window.location=url;
	}
}

/* Start Banking functions */
function balancePoll(){
	var postStr = "";
	$.each($(".bank-balance"),function(){
		postStr += "&accounts[]=" + $(this).attr('id').split('-')[1];
	});
	
	if(postStr=="") return; //no available accounts
	
	postStr = "balancePoll=true"+postStr;
	$.ajax({
		type:'POST',
		url:'uni-data/bank/bankproxy.php',
		data:postStr,
		dataType:'json',
		global:false,
		success:function(data){
			if(data.error){
				alert(data.error);
				return;
			}
			
			$.each(data,function(i,val){
				$("#balance-"+i).text(val);
			});
		}
	});
}

function expandAccount(acct_id){
	if($("#account-moreinfo-"+acct_id).is(":visible")){
		$("#account-moreinfo-"+acct_id).hide();
	}
	else{
		$("#account-moreinfo-"+acct_id).fadeIn('slow');
	}
}

function doTransaction(which,acct_id){
	var amt = Number($("#"+which+"-auro-"+acct_id).attr('value'));

	if(amt<=0 || isNaN(amt)){
		alert("Invalid amount entered!");
		return;
	}
	
	var postData = {"trans":true,"which":which,"amt":amt,"acct_id":acct_id};
	$.ajax({
		type:"POST",
		url:'uni-data/bank/bankproxy.php',
		data:postData,
		dataType:'json',
		success:function(data){
			if(data.error){
				alert(data.error);
				return;
			}
			$("#"+which+"-auro-"+acct_id).attr('value','')
			$("#balance-"+acct_id).text(data.balance);
		}
	})
}

function bankPlanBox(){
	$("#bank-plan-box").dialog({
	    title: 'Savings Plans',
        resizable: true,
        height: 450,
        width: 600,
        close:function(){
        	$(this).dialog('destroy');
        },
        open:function(){
            $(this).show();
        }
	});
}

function createAccountBox(){
	$("#create-account-box").dialog({
	    title: 'Create an Account',
        resizable: true,
        height: 400,
        width: 600,
        close:function(){
        	$(this).dialog('destroy');
        },
        open:function(){
            $(this).show();
        }
	});
}

function createAccount(){
	var name = $("#new-account-name").attr('value');
	var plan = $("#new-account-plan").attr('value');
	var balance = $("#new-account-balance").attr('value');
	var holders = "";
	$.each($('.new-account-share'),function(){
		if($(this).attr('value')!=""){
			holders += "&holders[]="+$(this).attr('value');
		}
	});
	
	$.ajax({
		type:'POST',
		url:'uni-data/bank/bankproxy.php',
		data:"createacct=true&name="+name+"&plan="+plan+"&balance="+balance+holders,
		dataType:'json',
		success:function(data){
			if(data.error){
				alert(data.error);
				return;
			}
			
			window.location.reload();
		}
	});
}

function addShareUserInput(){
	$("#new-account-holders").append('<li><input type="text" class="osimo_usernamesearch new-account-share" /></li>');
	
	$.each($('.osimo_usernamesearch'),function(){
		if(!$(this).hasClass('ac_input')){
			$(this).autocomplete(
				'os-includes/ajax/usersearch.php',{
				selectFirst: false,
				minChars: 2
			});
		}
	});
}

function renameBankAccount(acct_id){
	var name = prompt("Enter the new name for this account.","");

	if(name=="" || name==null) return;
	
	var postData = {"renameAcct":true,"acct_id":acct_id,"name":name};
	$.ajax({
		type:'POST',
		url:'uni-data/bank/bankproxy.php',
		data:postData,
		dataType:'json',
		success:function(data){
			if(data.error){
				alert(data.error);
				return;
			}
			$("#bank-account-name-"+acct_id).text(name);
		}
	});
}

function deleteAccount(acct_id){
	if(confirm("Are you sure you want to delete this account? This cannot be undone.\nThe account's contents will be put into your wallet")){
		var postData = {"deleteAcct":true,"acct_id":acct_id};
		$.ajax({
			type:'POST',
			url:'uni-data/bank/bankproxy.php',
			data:postData,
			dataType:'json',
			success:function(data){
				if(data.error){
					alert(data.error);
					return;
				}
				
				window.location.reload();
			}
		});
	}
}

function showRestrictions(acct_id,user){
	if($('#user-restrictions').length == 0){
		$('body').prepend("<div id='user-restrictions'></div>");
	}
	
	$("#user-restrictions").dialog({
	    title: 'Account Restrictions',
        resizable: true,
        height: 300,
        width: 600,
        close:function(){
        	$(this).dialog('destroy');
        },
        open:function(){
        	var postData = {"showRestrict":true,"acct_id":acct_id,"user":user};
            $.ajax({
            	type:'POST',
            	url:'uni-data/bank/bankproxy.php',
            	data:postData,
            	dataType:'json',
            	success:function(data){
            		if(data.error){
            			alert(data.error);
            			return;
            		}
            		
            		$("#user-restrictions").html(data.html).show();
            	}
            });
        }
	});
}

function setRestrictions(acct_id,user){
	var max_withdraw = $("#max-withdraw-amt").attr('value');
	var max_withdraw_daily = $("#max-withdraw-daily").attr('value');
	var approve_withdraw = $("#approve-withdraw").is(":checked");
	
	if(max_withdraw==""){ max_withdraw = 0; }
	if(max_withdraw_daily==""){ max_withdraw_daily = 0; }
	if(approve_withdraw==true){ approve_withdraw = 1; }
	else{ approve_withdraw = 0; }
	
	var postData = {
		"setRestrict":true,
		"acct_id":acct_id,
		"user":user,
		"max_withdraw":max_withdraw,
		"max_withdraw_daily":max_withdraw_daily,
		"approve_withdraw":approve_withdraw
	};
	$.ajax({
		type:'POST',
		url:'uni-data/bank/bankproxy.php',
		data:postData,
		dataType:'json',
		success:function(data){
			if(data.error){
				alert(data.error);
				return;
			}
			else{
				alert(data.status);
				$('#user-restrictions').dialog('destroy');
			}
		}
	});
}

function processPending(action,trans_id,acct_id){
	if(!confirm("Are you sure you want to "+action+" this transaction?")) return;
	
	var postData = {"processPending":true,"action":action,"trans_id":trans_id,"acct_id":acct_id};
	$.ajax({
		type:'POST',
		url:'uni-data/bank/bankproxy.php',
		data:postData,
		dataType:'json',
		success:function(data){
			if(data.error){
				alert(data.error);
				return;
			}
			
			if(action=='accept'){
				$("#pending-action-"+trans_id).html('Accepted!');
			}
			else{
				$("#pending-action-"+trans_id).html('Denied!');
			}
		}
	});
}

function addAcctHolder(acct_id){
	var user = $("#add-acct-holder-"+acct_id).attr('value');
	if(user=="") return;
	if(!confirm("Are you sure you wish to add "+user+" to this account? This cannot be undone (yet)!")) return;
	
	var postData = {"addAcctHolder":true,"acct_id":acct_id,"user":user};
	$.ajax({
		type:'POST',
		url:'uni-data/bank/bankproxy.php',
		data:postData,
		dataType:'json',
		success:function(data){
			if(data.error){
				alert(data.error);
				return;
			}
			
			window.location.reload();
		}
	});
}

function upgradeAccount(acct_id){
	if(!confirm("Ok to upgrade your account?")) return;
	
	var postData = {"upgradeAcct":true,"acct_id":acct_id};
	$.ajax({
		type:'POST',
		url:'uni-data/bank/bankproxy.php',
		data:postData,
		dataType:'json',
		success:function(data){
			if(data.error){
				alert(data.error);
				return;
			}
			
			window.location.reload();
		}
	});
}
/* End banking functions */

/* Shop character preview functions */
function showCharPreview(refresh){
	if($('#shop-char-preview').length == 0){
		$('body').prepend("<div id='shop-char-preview'></div>");
	}
	
	if(refresh){
		$.ajax({
			type:'POST',
			url:'uni-data/shop/preview.php',
			data:'showPreview=true',
			dataType:'json',
			success:function(data){
				if(data.error){
					alert(data.error);
				}
				else{
					$('#shop-char-preview').html(data.html);
					showCharPreview();
				}
			}
		});
	}
	
	$('#shop-char-preview').dialog({
	    bgiframe: true,
	    title:'Dressing Room',
	    resizable: true,
	    height:600,
	    width:450,
	    modal: false,
	    buttons: {
	    	'Reset Preview to Current': function() {
	    		resetCharPreview('current');
	    	},
	    	'Reset Preview to Default': function() {
	    		resetCharPreview('default');
	    	},
	    	'Close': function() {
	    		$("#shop-char-preview").dialog('destroy');
	    	}
	    },
	    close:function(){
	    	$("#shop-char-preview").dialog('destroy');
	    }
	});
}

function addToCharPreview(itemID){
	$.ajax({
		type:'POST',
		url:'uni-data/shop/preview.php',
		data:'addPreview=true&item='+itemID,
		dataType:'json',
		success:function(data){
			if(data.error){
				alert(data.error);
			}
			else{
				if(charPreviewIsOpen()){
					$('#shop-char-preview-img').html(data.img);
					$('#shop-char-preview-items').html(data.items);
				}
				else{
					showCharPreview(true);
				}
			}
		}
	});	
}

function removeFromCharPreview(itemID){
	$.ajax({
		type:'POST',
		url:'uni-data/shop/preview.php',
		data:'removePreview=true&item='+itemID,
		dataType:'json',
		success:function(data){
			if(data.error){
				alert(data.error);
			}
			else{
				if(charPreviewIsOpen()){
					$('#shop-char-preview-img').html(data.img);
					$('#shop-char-preview-items').html(data.items);
				}
				else{
					showCharPreview(true);
				}
			}
		}
	});
}

function resetCharPreview(type){
	$.ajax({
		type:'POST',
		url:'uni-data/shop/preview.php',
		data:'resetChar=true&type='+type,
		dataType:'json',
		success:function(data){
			if(data.error){
				alert(data.error);
			}
			else{
				if(charPreviewIsOpen()){
					$('#shop-char-preview').html(data.html);
				}
				else{
					showCharPreview(true);
				}
			}
		}
	});	
}

function charPreviewIsOpen(){
	return $('#shop-char-preview:visible').length;
}

function confirmBuyOutfit(){
	var cost = previewOutfitCost();
	if(confirm("Are you sure you want to buy this entire outfit?\nThis purchase will cost you "+cost+" auro.\n(This will not repurchase items you already own)")){
		buyOutfit();
	}
}

function previewOutfitCost(){
	return $('#preview-needed-cost').text();
}

function buyOutfit(){
	$.ajax({
		type:'POST',
		url:'uni-data/shop/preview.php',
		data:'buyOutfit=true',
		dataType:'json',
		success:function(data){
			if(data.error){
				alert(data.error);
			}
			else{
				alert(data.html);
			}
		}
	});
}
/* End shop character preview functions */