var inputMsg = ({
	side: 'right',
	boxName: 'boxEx',
	boxWidth: 200,
	boxType: 'error',
	hideTime: 3000,
	absLeft: 0,
	submitButton:'',
	loading: '',
	setColor:'false',
	inputColor:'#7F9DB9',
	errorColor:'#FF0000',
	arrow: 'true',
	scd:0,
	t:0,
	jalert:'',
    msgLines: 1,
	//創建提示窗
    createBox:function(inputId){
		if( $("#"+inputId).length > 0 ) {
			var offset =  $("#"+inputId).offset();
			var inputMsgBodyTop = offset.top - 2;
			if(this.absLeft>0){
				var inputMsgBodyLeft = offset.left + this.absLeft;
			} else {
				if(this.side=='right'){
                    if(inputId == 'verifyCode') {
					    var inputMsgBodyLeft = offset.left + $("#"+inputId).width() + 80;
                    } else {
					    var inputMsgBodyLeft = offset.left + $("#"+inputId).width() + 10;
                    }
				} else {
					var inputMsgBodyLeft = offset.left - this.boxWidth - 10;
				}
			}
			if (this.boxWidth == 0)	{
				var inputMsgBodyPosition = 'top:'+inputMsgBodyTop+'px;left:'+inputMsgBodyLeft+'px;';
			} else {
				var inputMsgBodyPosition = 'width:'+this.boxWidth+'px;top:'+inputMsgBodyTop+'px;left:'+inputMsgBodyLeft+'px;';
			}
			var inputMsgBodyHtml = '<div id="'+inputId+this.boxName+'" class="'+this.boxName+'" style="'+inputMsgBodyPosition+'"></div>';  
			$("body").append(inputMsgBodyHtml);
		}
    },
    
    //檢查提示窗是否存在
	chkBox:function(inputId) {
		var boxId = inputId+this.boxName;
        if( $("#"+boxId).length != 0 ) {
            this.clearBox(boxId);
		}
		this.createBox(inputId);
    },
 
    //清除提示窗
    removeBox:function(inputId){
    	var box= "#"+inputId+this.boxName;
		if( $(box).length > 0 ) {
            $(box).fadeOut("slow",function() {
				$(box).remove();
			}); 
        }
    },

    //清除視窗(無效果)
    clearBox:function(boxId){
		if( $("#"+boxId).length > 0 ) {
			$("#"+boxId).remove();
		}
    },

    showBoxLoading:function(inputId, btype, msgText, hidebox){
        $("#"+this.boxName+inputId+"Main").addClass('boxExNoticeLoading');
        $("#"+this.boxName+inputId+"Main").empty();
    },

    //顯示提示窗
    showBox:function(inputId, btype, msgText, hidebox){
 		this.removeLoading();
    	this.chkBox(inputId);

    	if(this.setColor=='true'){
			if(btype=='error'){
				$("#"+inputId).css("border-color",this.errorColor);
			} else if (btype=='correct'){
				$("#"+inputId).css("border-color",this.inputColor);
			}
		}
		var msgTextHtml = '';
		if (this.arrow == 'true') {
			msgTextHtml = '<div class="'+this.boxName+'Arrow '+this.boxName+'Arrow_'+btype+'_'+this.side+'"></div>';
		}
        msgTextHtml += '<div class="'+this.boxName+'Frame '+this.boxName+'Frame_'+this.side+' '+this.boxName+'Frame_'+btype+'"><div class="'+this.boxName+'Icon '+this.boxName+'Icon_'+this.side+' '+this.boxName+'Icon_'+btype+'"></div><div class="'+this.boxName+'Main_'+this.side+'" id="'+this.boxName+inputId+'Main">'+msgText+'</div></div>';
        $("#"+inputId+this.boxName).html(msgTextHtml);
        if(hidebox=='hide'){
        	window.setTimeout('inputMsg.removeBox(\''+inputId+'\');',this.hideTime);
    	}
    },

    //清除loadding圖和激活提交按鈕
    removeLoading:function(){
    	if(this.loading!=""){
    		$("#"+this.loading).remove();
    	}
        this.unlockForm();
    },
    
    //顯示loading圖，且使提交按鈕disable
    submit:function(inputId){
        this.lockForm(inputId)

        this.loading=inputId+this.boxName;
    	var offset =  $("#"+inputId).offset();
    	var lmargin = ($("#"+inputId).height()-18)/2;
		var inputMsgBodyLeft = offset.left + $("#"+inputId).width() + 8;
		var inputMsgBodyHtml = '<div id="'+this.loading+'" class="'+this.boxName+'Loading" style="top:'+offset.top+'px;left:'+inputMsgBodyLeft+'px;margin-top:'+lmargin+'px"></div>';
		$("body").append(inputMsgBodyHtml);
    },
    
    //鎖定表單
    lockForm:function(){
        if(this.submitButton!=''){
		    $("#"+this.submitButton).attr('disabled','true');
		    $("#"+this.submitButton).css('cursor','wait');
        }
    },

    //解鎖表單
    unlockForm:function(){
        if(this.submitButton!=''){
            $("#"+this.submitButton).removeAttr('disabled');
            $("#"+this.submitButton).css('cursor','pointer');
        }
    },

    //顯示結果窗口
    showResult:function(){
        this.lockForm();
  		if( $("#boxExResult").length > 0 ) {
            return false;
        } else {
            var frameTop = $(window).height() / 2 + $(window).scrollTop() - 80;
            var boxCss = 'height:'+$(document).height()+'px';
            var frameCss = 'margin-top:'+frameTop+'px';
            var boxHtml = '<div id="boxExResult" style="'+boxCss+'"><div id="boxExResult_frame" style="'+frameCss+'"><div id="boxExResult_loading"></div><div id="boxExResult_note">請稍候，數據處理中...</div></div></div>';
            $("body").append(boxHtml);
            return true;
        }
    },
    
    //關閉結果窗口
    clearResult:function(){
    	this.clearBox("boxExResult");
    },

    //清除提示窗
    removeResult:function(){
		if( $("#boxExResult").length > 0 ) {
            $("#boxExResult").fadeOut("slow",function() {
				$("#boxExResult").remove();
                inputMsg.removeLoading();
			}); 
        }
    },

    setWait:function(){
		if( $("#boxExResult").length > 0 ) {
            $("#boxExResult").fadeOut("slow",function() {
				$("#boxExResult").remove();
                if(this.loading!=""){
    		        $("#"+this.loading).remove();
    	        }
			}); 
        }
    },

    setWaitResult:function(msgText){
		$("#boxExResult_loading").css("background-image","url('')");
		$("#boxExResult_loading").html(msgText);
		$("#boxExResult_loading").css("color",'#008000');

		this.scd = 5;
        this.jalert = '等待提示窗關閉';
		$("#boxExResult_note").html(this.jalert+"("+this.scd+")");
		this.t = setInterval("inputMsg.printClose()",1000);
        window.setTimeout('inputMsg.setWait();',6000);
    },

    setResult:function(msgText, btype, deal){
    	if(btype=='correct'){
    		var alertColor = '#008000';
    	} else if (btype == 'error'){
    		var alertColor = '#FF0000';
    	}

		$("#boxExResult_loading").css("background-image","url('')");
		$("#boxExResult_loading").html(msgText);
		$("#boxExResult_loading").css("color",alertColor);

    	if(this.msgLines>1){
		    $("#boxExResult_loading").css("text-align", 'left');
		    $("#boxExResult_loading").css("margin-left", '10px');
		    $("#boxExResult_loading").css("margin-right", '10px');
            var addheight = this.msgLines * 18;
            $("#boxExResult_loading").css("height", addheight+'px');
            var bigheight = 82 + addheight;
            $("#boxExResult_frame").css("height", bigheight+'px');
            this.msgLines = 1;
        }

        if(deal == 'show') {
            $("#boxExResult_note").html('<input type="button" name="closeframe" value="確認" onclick="inputMsg.unlockForm();window.location.reload();">');
        } else if(deal == 'showhide') {
            $("#boxExResult_note").html('<input type="button" name="closeframe" value="確認" onclick="inputMsg.removeResult();">');
        } else {
            this.scd = this.hideTime / 1000;
            if(deal=='reload') {
                this.jalert = '等待頁面刷新';
            } else if (deal=='hide') {
                this.jalert = '等待提示窗關閉';
            } else {
                this.jalert = '等待頁面跳轉';
            }
            
            $("#boxExResult_note").html(this.jalert+"("+this.scd+")");
            this.t = setInterval("inputMsg.printClose()",1000);
            if(deal=='reload'){
                window.setTimeout('inputMsg.unlockForm();window.location.reload()',this.hideTime + 1000);
            } else if (deal=='hide'){
                window.setTimeout('inputMsg.removeResult();',this.hideTime + 1000);
            } else {
                window.setTimeout("inputMsg.unlockForm();window.location.replace('"+deal+"')",this.hideTime + 1000);
            }
        }
    },
    printClose:function(){
    	this.scd--;
    	if(this.scd>=0){
    		$("#boxExResult_note").html(this.jalert+"("+this.scd+")");
    	} else {
			clearInterval(this.t);
			this.scd=this.hideTime / 1000;
		}
    }
    
});

function inputGo(data){
	newData = data.split('|');
	if(newData[0]=='1'){
        inputMsg.unlockForm();
		location.replace(newData[1]);
	} else if (newData[0]=='system') {
        if(!inputMsg.showResult()) return false;
		inputMsg.setResult(newData[1], 'error', '/');
	} else if (newData[0]=='notice') {
        if(!inputMsg.showResult()) return false;
		inputMsg.setResult(newData[1], 'error', 'hide');
	} else if(newData[0]==data){
        if(!inputMsg.showResult()) return false;
		inputMsg.setResult(newData[0], 'error', '/');
	} else {
		inputMsg.showBox(newData[0], 'error', newData[1], 'hide');
		$("#"+newData[0]).select();
	}
}
function inputResult(data, deal){
	newData = data.split('|');
	if(newData[0]=='1'){
		if (deal == 'submit') {
			return true;
		} else {
			inputMsg.setResult(newData[1], 'correct', deal);
		}
	} else if (newData[0]=='system') {
		inputMsg.setResult(newData[1], 'error', '/');
	} else if (newData[0]=='notice') {
		inputMsg.setResult(newData[1], 'error', 'hide');
	} else if(newData[0]==data){
		inputMsg.setResult(newData[0], 'error', '/');
	} else { 
		inputMsg.clearResult();
		inputMsg.showBox(newData[0], 'error', newData[1], 'hide');
		$("#"+newData[0]).select();
	}
	return false;
}
