﻿// 使用 AJAX
// obj_jQuery = 您目前的jQuery物件 = Object
// me = Loading圖示要顯示在哪個DOM物件之後 = DOM Object
// u = 您的CFC檔案位置 = String
// param = 您的參數。(第一個要是method Name) = JSON Object
// Sample：
//		var u = "fnWeb/woodStatus/ajax.cfc";
//		var jsn = {};
//		jsn["method"] = "methodName"
//		jsn["???"] = ???
//		callAjax($, me, u,jsn);
function callAjax(obj_jQuery, me, u, param, cbs)
{
	var jQuery = obj_jQuery;

	// 顯示Loading icon
	if (me != null)
	{
		jQuery(me).after('<img alt="Loading" src="/images/Loder.gif" />');
		me = jQuery(me).next();
		//return;
	}
	
	jQuery.ajax(
	{
		type: "post",
		url: u,
		data: param,
		dataType: "json",	
		beforeSend: function() {
			if (me != null)
				me.show();
		},
		complete: function() {
			if (me != null)
			{
				me.hide();
				me.remove();	
			}
		}, 
		success: function(objResponse){
			cbs = cbs || ""; 
			var dcbs = eval(cbs);
			dcbs(objResponse);
		},
		error: function( xhr, ajaxOptions, thrownError ){
			alert(xhr.responseText);
		}
	});
}

// Dailog
function callDailog(title, msg, w, h, callBackOK, callBackNo)
{
	dlgHelper = new DialogHelper();

	// 設定
	dlgHelper.set_Title(title);
	dlgHelper.set_Msg(msg);
	dlgHelper.set_Width(w);
	dlgHelper.set_Height(h);
	dlgHelper.set_Buttons({
		'YES': function() {
			callBackOK = callBackOK || null; 
			var dcallBackOK = eval(callBackOK);
			dcallBackOK();
			$(this).dialog('close');
		},
		'NO': function() {
			callBackNo = callBackNo || null; 
			if (callBackNo != null) {
				var dcallBackNo = eval(callBackNo);
				dcallBackNo();
			}
			$(this).dialog('close');
		}
	});

	dlgHelper.open();
}

DialogHelper = function()
{
	var m_title = "";
	var m_msg = "";
	var m_width = "";
	var m_height = "";
	var m_btns = null;
	this.dlgDiv = $("<div><p></p></div>"); // 可自定義

	// 屬性設定
	this.set_Title = function(val) {
		this.m_title = val;
	}
	this.get_Title = function() {
		return this.m_title;
	}
	
	this.set_Msg = function(val) {
		this.m_msg = val;
	}
	this.get_Msg = function() {
		return this.m_msg;
	}
	
	this.set_Width = function(val) {
		this.m_width = val;
	}
	this.get_Width = function() {
		return this.m_width;
	}
	
	this.set_Height = function(val) {
		this.m_height = val;
	}
	this.get_Height = function() {
		return this.m_height;
	}
	
	this.set_Buttons = function(val) {
		this.m_btns = val;
	}
	this.get_Buttons = function() {
		return this.m_btns;
	}

	this.open = function() {
		$dlg = this.dlgDiv.clone();
		$dlg.children().filter("p").html(this.dlgDiv.children().filter("p").html() + this.get_Msg()); //增加自定义消息

		$dlg.dialog({
			autoOpen: true,
			position: 'center',
			height: this.get_Height(),
			width: this.get_Width(),
			modal: true,
			title: this.get_Title(),
			buttons: this.get_Buttons()
		});
	}
}
		
// Replace ISO-8859-1
function replaceISO88591(dStr) {
    var ISO88591 = new Array('&nbsp;', '&iexcl;', '&cent;', '&pound;', '&curren;', '&yen;', '&brvbar;', '&sect;', '&uml;', '&copy;', '&ordf;', '&laquo;', '&not;', '&shy;', '&reg;', '&macr;', '&deg;', '&plusmn;', '&sup2;', '&sup3;', '&acute;', '&micro;', '&para;', '&middot;', '&cedil;', '&sup1;', '&ordm;', '&raquo;', '&frac14;', '&frac12;', '&frac34;', '&iquest;', '&times;', '&divide;', '&hellip;');
    var raw = new Array(' ', '!', '￠', '￡', '?', '￥', '|', '§', '‥', 'c', 'a', '?', '?', '-', 'R', '¯', '°', '±', '2', '3', '’', 'μ', '?', '·', '﹐', '1', 'o', '?', '?', '?', '?', '?', '×', '÷', '...');
    for (var i = 0; i < raw.length; i++) {
        var patt = new RegExp(ISO88591[i], "g");
        dStr = dStr.replace(patt, raw[i]);
        var patt1 = new RegExp(ISO88591[i].replace("&", "&amp;"), "g");
        dStr = dStr.replace(patt1, raw[i]);
    }
    // Strip space between tags
    dStr = dStr.replace(/> *</g, "><");
    // Return
    // return removeMSWordTag(dStr);
    return dStr;
}



// SATRT: String to XML ---------------------------------------------
function strToXML(str)
{
    str = str.replace(/[\r\n]+/g, "");
    
    try //Internet Explorer
    {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(str);
        
        return xmlDoc
    }
    catch(e)
    {
        try //Firefox, Mozilla, Opera, etc.
        {
            var parser = new DOMParser();
            var xmlDoc = parser.parseFromString(str,"text/xml");
        
            return xmlDoc
        }
        catch(e)
        {
            alert(e.message);
            return;
        }
    }
}



// 取得檔案副檔名
function getFileExtension(val) {
    if (val.length == 0) return "";
    var dot = val.lastIndexOf(".");
    if (dot == -1) return "";
    var extension = val.substr(dot, val.length);
    return extension;
}

//替換全形空白和逗號
function replaceSpace(aa)
{
	var cc = "";
	cc = aa.replace(/，/g, ','); 
	cc = cc.replace(/ /gi, ''); 
	return cc;
}

// 顯示更多連結
function moreLink(theform, pageId)
{
	theform.pageId.value = pageId;
	theform.submit();
}

// 取得Browser可視區寬度和高度
function getClientBounds()
{
	var clientWidth;
	var clientHeight;
	
	if ($.browser.msie)
	{
				clientWidth = document.documentElement.clientWidth;
				clientHeight = document.documentElement.clientHeight;
	}
	else if ($.browser.safari)
	{
		clientWidth = window.innerWidth;
		clientHeight = window.innerHeight;
	}
	else if ($.browser.opera)
	{
		clientWidth = Math.min(window.innerWidth,
			document.body.clientWidth);
		clientHeight = Math.min(window.innerHeight,
			document.body.clientHeight);
	}
	else
	{
		clientWidth = Math.min(window.innerWidth,
			document.documentElement.clientWidth);
		clientHeight = Math.min(window.innerHeight,
			document.documentElement.clientHeight);
	}

	return { width : clientWidth, height : clientHeight };
}	

// 需要有jQuery
function showMsgBox(nbr)
{
	var title = "";
	var bgColor = "";
	
	switch (nbr)
	{
		case 0:
			title = "操作完成";
			bgColor = "#00CC00";
			break;
		case 1:
			title = "操作失敗";
			bgColor = "#FF0000";
			break;
		case 2:
			title = "✓";
			bgColor = "#00CC00";
			break;
	}
	
	$.blockUI({ message: '<h1>'+ title +'</h1>', css: { 
		border: 'none', 
		padding: '15px', 
		width: '15%',
		left: '40%',
		backgroundColor: bgColor, 
		'-webkit-border-radius': '10px', 
		'-moz-border-radius': '10px', 
		color: '#fff'
	},showOverlay: false}); 
	
	setTimeout($.unblockUI, 1500); 
}

// 需要有jQuery
function deleteTR(me)
{
	$(me).parent().parent().fadeOut(1000);
}

//移至結果頁 
function sheetOf(obj,No,pageID,caseBox,actions){
	theform = findForm(obj);
	theform.reset();
	if(pageID!= undefined){
		if(theform.pageID != undefined){
			theform.pageID.value=pageID;
			
		}
		if(theform.pageId != undefined){
			theform.pageId.value=pageID;
			
		}
	}
	if(pageID!= caseBox){
		if(theform.caseBox != undefined){
			theform.caseBox.value=caseBox;
		}
	}
	if(pageID!= actions){
		if(theform.actions != undefined){
			theform.actions.value=actions;
		}
	}
	//alert(theform.pageID.value);
	theform.sheetNo.value = No;
	//setTimeout("theform.submit()", 500);
	theform.submit();
}

//找出元件所在位置所使用之Form
function findForm(obj){
	theform = obj;
	while (theform.tagName.toUpperCase() != 'FORM'){
		theform = theform.parentNode;
	}
	return theform;
}
