$.ajaxSetup({
	beforeSend: function(xhr) {
		var currentURL = document.location.href;
		if( currentURL.lastIndexOf( "?" ) >= 0 ) {
			currentURL = currentURL.substr( 0, currentURL.lastIndexOf( "?" ) );
		}
		xhr.setRequestHeader("Referer", currentURL );
    },
	global: true,
	type: "POST"
});
function ajax(aurl,adata,aid) {
	$.ajax({
		url: aurl,
		data: adata,
		success: function(msg){
			$(aid).html(msg);
		}
	});
}
function buildkey(num,obj) {
	var select = '';
	$.getJSON('js/area.json', function(data){
		$.each(data['item'+num], function(key,value){
			select += '<option value="'+value['Zip']+'">'+value['Area']+' '+value['Zip']+'</option>';
		});
		$(obj).empty();
		$(select).appendTo(obj);
		$(obj).selectedIndex = 0;
	});
}
function ajaxFileUpload(act,id) {
	$.ajaxFileUpload({
		url:'doajaxfileupload.php',
		secureuri:false,
		fileElementId:'fileToUpload',
		dataType: 'json',
		success: function (data, status) {
			if(typeof(data.error) != 'undefined') {
				if(data.error != '') {
					alert(data.error);
				} else {
					if(act=='view') {
						$.ajax({
							url: 'view_showbox.php',
							data: 'id='+id,
							success: function(msg){
								$("#Showbox").html('').html(msg).css('visibility','visible');
							}
						});
					} else {
						$("#loading").hide();
						location.href='list.php';
						return false;
					}
				}
			}
			$("#loading").hide();
		}, error: function (data, status, e) {
			alert(e);
		}
	})
	return false;
}
function okla(act) {
	$.ajax({
		url: 'okla.php',
		data: $('#tform').serialize()+'&act='+act,
		success: function(msg){
			var tmp = msg.split("@");
			if(tmp[1]=='UN') {
				alert(tmp[0]);
			} else {
				$("#loading").show();
				ajaxFileUpload(act,tmp[0]);
			}
		}
	});
}
function view_okla(act) {
	$.ajax({
		url: 'view_okla.php',
		data: $('#tform').serialize(),
		success: function(msg){
			location.href='list.php';
			return false;
		}
	});
}
function del() {
	$.ajax({
		url: 'del.php',
		data: ''
	});
}
function show(id,page,key,show_id) {
	var keyword='';
	if('請輸入暱稱或編號搜尋'!=$('#keyword').val()) {
		keyword = $('#keyword').val();
	}
	$.ajax({
		url: 'showbox.php',
		data: $('#sort_form').serialize()+'&keyword='+keyword+'&id='+id+'&page='+page+'&key='+key+'&show_id='+show_id,
		success: function(msg){
			$("#Showbox").html(msg).css('visibility','visible');
		}
	});
}
function show1(id) {
	$.ajax({
		url: 'showbox1.php',
		data: 'id='+id,
		success: function(msg){
			$("#Showbox").html(msg).css('visibility','visible');
		}
	});
}
function search1() {
	var keyword = $('#keyword').val();
	if( keyword=='') {
		alert('請輸入暱稱或編號搜尋！');
		return false;
	} else {
		$.ajax({
			url: 'list_ajax.php?act=ajax',
			data: $('#sort_form').serialize()+'&keyword='+$('#keyword').val(),
			success: function(msg){
				$("#list").html(msg);
			}
		});
	}
}
function vote(id) {
	if(id<=4) {
		alert('該作品為範例！');
	} else if(id==7) {
		alert('該作品為範例！');
	} else if(id==29) {
		alert('該作品為範例！');
	} else {
		$.ajax({
			url: 'vote.php',
			data: 'id='+id,
			success: function(msg){
				if(msg=='UN') {
					alert('您今日已投過票！');
				} else {
					alert('投票成功！');
					$("#Showbox").css('visibility','hidden');
					ajax('list_ajax.php?act=ajax',$('#sort_form').serialize(),'#list')
				}
			}
		});
	}
}
