﻿var PW={
    Settings:{
        Root:'..',//'http://210.67.12.99:88',
        StockAotoComp:'/info/loadSymbolList.ashx?g=1',
        WarrantAutoComp:'/info/loadSymbolList.ashx?g=2',    
        SecurityURL:'',
        DataTableLanguageFile:'../js/sys.devp/datatable.language/zh_TW.txt',//'http://www.money-link.com.tw/themes/js/publics/datatable.language/zh_TW.txt',
        FundRank:'',
        divDataTable:'dynamic',
        Target:'_self'
    },
    jsInclude:function($,modules){
        var pathtojsfiles = PW.Settings.Root+"/js/"; 
        var minver = true;    
        var filename;
        for(var i=0;i<modules.length; i++)
        {
             filename = pathtojsfiles+modules[i].incfile; 
	         PW.IncludeJavaScript($,filename);
        }  
    },
    IncludeJavaScript:function($,jsFile)
    {
        if($.browser.safari || $.browser.msie ) {
	        $.ajax({url:jsFile,dataType:'script', async:false, cache: true});
        } else 
        {
            var oHead = document.getElementsByTagName('head')[0];
            var oScript = document.createElement('script');
            oScript.type = 'text/javascript';
            oScript.charset = 'utf-8';
            oScript.src = jsFile;
            oHead.appendChild(oScript);        
        }
    },
    cssInclude:function($,modules){
        var pathtocssfiles = PW.Settings.Root+"/css/"; 
        var minver = true;
        var filename;
        for(var i=0;i<modules.length; i++)
        {
      	    filename = pathtocssfiles+modules[i].incfile;  
	        PW.IncludeCSS($,filename);
        }
    },
    IncludeCSS:function($,cssFile)
    {
        var style = document.createElement('link');
        style.href = cssFile;
        style.rel = 'stylesheet';
        style.type = 'text/css';
        document.getElementsByTagName('HEAD').item(0).appendChild(style);
    },
   RemoveJSCSSFile:function($,filename, filetype)
   { 
         var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" ;
         var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" ;
         var allsuspects=document.getElementsByTagName(targetelement);
         for (var i=allsuspects.length; i>=0; i--){
              if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)>=0) 
                { allsuspects[i].parentNode.removeChild(allsuspects[i]);} 
        } 
    },
    load_aspx : function(params){				
	    // parameters
	    var div = params.div;
	    var url = params.url;
	    var data = params.data;
	    $("#"+div).html("");
        $.ajax({type: "POST",
                url: url,
                data: data,
                success: function(html){$("#"+div).append(html);}});
    },
    load_html : function(params){				
	    // parameters
	    var div = params.div;
	    var url = params.url;
	    //var data = params.data;
        var cache= params.cache;
        $("#"+div).html("");
        $.ajax({url: url,
                cache: cache,
                success: function(html){$("#"+div).append(html);}});
    },
    load_cachefile : function(params){				
	    // parameters
	    var div = params.div;
	    var url = params.url;
        var cache= false;
        $("#"+div).html("");
        $.ajax({url: url,
                cache: cache,
                success: function(html){$("#"+div).append(html);}});
    },  
    init:function(params){
        PW.Settings=$.extend(PW.Settings,params);
        if(PW.Settings.SecurityURL.indexOf("?")<=0)
            PW.Settings.SecurityURL+="?";
     }
}
var jsmodules = [
        { incfile: 'jquery.ajaxQueue.js' },
        { incfile: 'sys.devp/jquery.autocomplete.js' }, 
//        { incfile:'jquery.autocomplete.min.js'}, 
        { incfile:'sys.devp/jquery.form-defaults.js'}
    ];
var cssmodules = [
        { incfile:'sys.devp/jquery.autocomplete.css'}
    ];    
PW.jsInclude($,jsmodules);
PW.cssInclude($,cssmodules);
/*datatable pipeline*/
var oCache = {
	iCacheLower: -1
};
//Setting for DataTable
function fnSetKey( aoData, sKey, mValue )
{
	for ( var i=0, iLen=aoData.length ; i<iLen ; i++ )
	{
		if ( aoData[i].name == sKey )
		{
			aoData[i].value = mValue;
		}
	}
}
function fnGetKey( aoData, sKey )
{
	for ( var i=0, iLen=aoData.length ; i<iLen ; i++ )
	{
		if ( aoData[i].name == sKey )
		{
			return aoData[i].value;
		}
	}
	return null;
}
function fnDataTablesPipeline ( sSource, aoData, fnCallback ) {
	var iPipe = 10; /* Ajust the pipe size */
	
	var bNeedServer = false;
	var iRequestStart = fnGetKey(aoData, "iDisplayStart");
	var iRequestLength = fnGetKey(aoData, "iDisplayLength");
	var iRequestEnd = iRequestStart + iRequestLength;
	oCache.iDisplayStart = iRequestStart;
	//alert(oCache.iCacheLower +","+ iRequestStart+","+ iRequestEnd +","+oCache.iCacheUpper);
	/* outside pipeline? */
	if ( oCache.iCacheLower <= 0 || iRequestStart <= oCache.iCacheLower || iRequestEnd > oCache.iCacheUpper )
	{
		bNeedServer = true;
	}	
	/* sorting etc changed? */
	if ( oCache.lastRequest && !bNeedServer )
	{
		for( var i=0, iLen=aoData.length ; i<iLen ; i++ )
		{
			if ( aoData[i].name != "iDisplayStart" && aoData[i].name != "iDisplayLength" )
			{
				if ( aoData[i].value != oCache.lastRequest[i].value )
				{
					bNeedServer = true;
					break;
				}
			}
		}
	}	
	/* Store the request for checking next time around */
	oCache.lastRequest = aoData.slice();
	
	if ( bNeedServer )
	{
		if ( iRequestStart < oCache.iCacheLower )
		{
			iRequestStart = iRequestStart - (iRequestLength*(iPipe-1));
			if ( iRequestStart < 0 )
			{
				iRequestStart = 0;
			}
		}		
		oCache.iCacheLower = iRequestStart;
		oCache.iCacheUpper = iRequestStart + (iRequestLength * iPipe);
		oCache.iDisplayLength = fnGetKey( aoData, "iDisplayLength" );
		fnSetKey( aoData, "iDisplayStart", iRequestStart );
		fnSetKey( aoData, "iDisplayLength", iRequestLength*iPipe );
		$.getJSON( sSource, aoData, function (json) { 
			/* Callback processing */
			oCache.lastJson = jQuery.extend(true, {}, json);
			if ( oCache.iCacheLower != oCache.iDisplayStart )
			{
				json.aaData.splice( 0, oCache.iDisplayStart-oCache.iCacheLower );
			}
			json.aaData.splice( oCache.iDisplayLength, json.aaData.length );
							
			fnCallback(json)
		} );
	}
	else
	{		
		json = jQuery.extend(true, {}, oCache.lastJson);
		json.aaData.splice( 0, iRequestStart-oCache.iCacheLower );
		json.aaData.splice( iRequestLength, json.aaData.length );
		fnCallback(json);
		return;
	}
}
/* Get the rows which are currently selected */
function fnGetSelected( oTableLocal )
{
	var aReturn = new Array();
	var aTrs = oTableLocal.fnGetNodes();
	for ( var i=0 ; i<aTrs.length ; i++ )
	{
		if ( $(aTrs[i]).hasClass('tr_over') )
		{
			aReturn.push( aTrs[i] );
		}
	}
	return aReturn;
}
//load Flex Chart in which div
function loadChart(div,chartID,flashvars,width,height)
{
    newdiv=div+chartID;
    $("#"+div).html("");
    $("#"+div).append("<div id='"+newdiv+"'/>");
    //flashvars  = {"SymId": symid,"MarketId":"10","Lang":"zh_TW"};
    if (typeof(flashvars) != "object")  
        flashvars={"flashvars":flashvars};
    params     = {"bgcolor": "#869ca7", "allowscriptaccess": "always", "wmode": "transparent"};
    attributes = {"name": chartID, "id": chartID};
    swfobject.embedSWF("/flex/"+chartID+".swf"
        , newdiv
        , width, height
        , "9.0.0", "/flex/expressInstall.swf"
        , flashvars, params, attributes);      
}
//load list pagination of all stock news
function loadListPagination(symbolID)
{
   $("#divStockNews").html("<div id='newsPage'></div>");    
   $("div#newsPage").ListPagination({
   sAjaxSource:"http://210.67.120.18/news/RetrieveStockNews.json.ashx?symid="+symbolID+"&jsoncallback=?",//+symbolID,
   sListId:"listPages",
  iPageLength:5,
   oColumns:{id:"FLD_MAIN_SEQNO",title:"FLD_TITLE",date:"FLD_YMD",time:"FLD_TRANSFERTIME",url:"../info/news_content.aspx?symid="+symbolID+"&sn="}
   }); 
};
//load list pagination of all stock news which can render each row 
function loadListPagination_oc(symbolID,clickfunction)
{
   $("#divStockNews").html("<div id='newsPage'></div>");    
  $("div#newsPage").ListPagination({
   sAjaxSource:"http://210.67.120.18/news/RetrieveStockNews.json.ashx?symid="+symbolID+"&jsoncallback=?",//+symbolID,
   sListId:"listPages",
  //sListClass:"stock_news",
  iPageLength:5,
   oColumns:{id:"FLD_MAIN_SEQNO",title:"FLD_TITLE",date:"FLD_YMD",time:"FLD_TRANSFERTIME",onclick:clickfunction+"('id','sn')"}
   }); 
};
var WarrantInfo;
//insert Member Options to DB
function addMemberOption(wId){
     $.ajax({type: "POST",
            url:PW.Settings.Root+ "/info/addmylist.aspx?act=I&wid="+wId,
            data: "",
            success  : function(html){
		       alert(html); 
            },
            error:function(html){
                alert("網路連線失敗,請稍候再試!!");
            }
    });
};
function sessionCheck(rdtURL)
{
    $.ajax({type: "POST",
                url: "../index/sessionhandle.ashx",               
                cache: false,
                success: function(html){
                if(html=="OK") 
                window.location.href=rdtURL; 
                else 
                alert(html);
                }
          });
}
//取得權證基本資料並載入試算器頁面中
function getWarrantQuote(wid,caculate){var url=PW.Settings.Root+"/info/getwarrantinfo.js.ashx?wid="+wid+"&page=calculator&head=false&div=";$.get(url, "",function(data){ caculate();},"script");}
function getWarrantQuote_HK(wid, caculate) {
    var url = PW.Settings.Root + "/info/getwarrantinfo_hk.js.ashx?wid=" + wid + "&page=calculator&head=false&div=";
    $.get(url, "", function (data) { 
        caculate(); }, "script"); 
    }
//截取URL的參數值
function getURLParam(name ){  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  var regexS = "[\\?&]"+name+"=([^&#]*)";  var regex = new RegExp( regexS );  var results = regex.exec( window.location.href );  if( results == null )    return "";  else    return results[1];}
//日期格式轉換,如20100301=>2010/03/01
function getDate_ymd(sdate,split)
{return sdate.substr(0,4)+split+sdate.substr(4,2)+split+sdate.substr(6,2);}
//時間格式轉換,如1330=>13:30
function getTime_ms(stime,split)
{return stime.substr(0,2)+split+stime.substr(4,2);}
