var urlparse = require('url').parse
  , http = require('http')
  , fs = require('fs');
var cp = require('child_process');
var iconv = require('iconv-lite');
var message=0;
var readedmessage=0;
var running;

function executeSql(sql,callback,nothingcallback){
	var sth=false;
	var es=cp.spawn("apps\\dossier\\main.exe",['db',sql]);
	var bufs = [];
	es.stdout.on('data', function (data) {
		bufs.push(data);
		sth=true;
	});
	es.stderr.on('data', function (data) {
		alert("错误:"+repairCharset(data),'danger');
		sth=true;
	});
	es.stdout.on('end', function(){
		var buf = Buffer.concat(bufs);
		var output=buf.toString();
		if (output.length>0&&typeof(callback)=="function"){
			callback(output);
		}
	});
	es.on('exit',function(){
		if (!sth && typeof(nothingcallback)=="function"){
			nothingcallback();
		}
	});
}

function getPlaneByPid(pid){
	for(var i in planesjson){
		var plane=planesjson[i];
		if(Number(plane.planeid)==Number(pid)){
			return plane;
		}
	}
	return null;
}

function repairCharset(oldstr){
	var str = iconv.decode(oldstr, 'gbk');
	return str;
}
function repairCharsetString(oldstr){
	var str = iconv.decode(new Buffer(oldstr,['binary']), 'gbk');
	return str;
}

function date2str(x,y) { 
var z ={y:x.getFullYear(),M:x.getMonth()+1,d:x.getDate(),h:x.getHours(),m:x.getMinutes(),s:x.getSeconds()}; 
return y.replace(/(y+|M+|d+|h+|m+|s+)/g,function(v) {return ((v.length>1?"0":"")+eval('z.'+v.slice(-1))).slice(-(v.length>2?v.length:2))}); 
} 

function IsNum(s)
{
    if (s!=null && s!="")
    {
        return !isNaN(s);
    }
    return false;
}

function refreshClass() {
	$(".double").each(function() {
		var val = Number($(this).text());
		if(isNaN(val)){
			val=0;
		}
		if (val != 0) {
			$(this).text(val.toFixed(2));
		} else {
			$(this).text(0);
		}
	});
	$(".toppercent").each(function() {
		var val = Number($(this).text());
		if(isNaN(val)){
			val=0;
		}
		$(this).text(val.toFixed(1) + '%');
	});
	$(".int").each(function() {
		var val = Number($(this).text());
		if(isNaN(val)||(val<0&&val>-1)){
			val=0;
		}
		$(this).text(val.toFixed(0));
	});
	$(".percent").each(function() {
		var val = Number($(this).text());
		if(isNaN(val)){
			val=0;
		}
		$(this).text((val * 100).toFixed(0) + '%');
	});
	$(".detailpercent").each(function() {
		var val = Number$((this).text());
		if(isNaN(val)){
			val=0;
		}
		$(this).text(Number(val * 100).toFixed(1) + '%');
	});
}
