
function scrollPage( tid ){
    var obj = document.getElementById(tid);
    obj.scrollIntoView();
}

function openWin( url, set ){
    var winID = window.open('blank.htm','', 'top=50,left=600,scrollbars=1,resizable=yes,' + set );
    winID.location.href=url;
    if( winID.opener==null )
        winID.opener = window;
    winID.opener.name = 'opener';
}

//SORT
function Sort(SortKey){
	var F=document.query_form;
	//更換
	if (F.order_by.value.indexOf(SortKey) == -1){
		F.order_by.value = SortKey;
		F.dec.value = 'ASC';
	}
	else if (F.dec.value.indexOf('DESC') == -1){
		F.order_by.value = SortKey;
		F.dec.value = 'DESC';
	}
	else{
		F.order_by.value = SortKey;
		F.dec.value = 'ASC';
	}
	F.submit();
}

function checkIsNum(s){
   var patrn=/^[-,+]{0,1}[0-9]{0,}$/;
   if (!patrn.exec(s))
    return false;
    return true;
}
function returnInt(obj){
   s = obj.value;
   var patrn=/^[-,+]{0,1}[0-9]{0,}$/;
   if (!patrn.exec(s))
        obj.value = 0;
   else
        obj.value = parseInt(s);
}

function parseInt2(s){
   var num = (s+'').replace( ',', '' );
   var patrn=/^[-,+]{0,1}[0-9|\.]{0,}$/;
   if (!patrn.exec(num))return 0;
   //去掉第一個零
   num = num.replace( /^0/, '' );
   if( num=='' )num = 0;
   return parseInt(num);
}

function returnNull( obj ){
   if( parseInt(obj.value)==0 )obj.value = '';
}

//XAJAX用清空選項內容
function clearOption(selectId){
   document.getElementById(selectId).length = 0;
   //addOption(selectId,'請選擇...','');
}

function addOption(selectId,txt,val){
    var objOption = new Option(txt,val);
    document.getElementById(selectId).options.add(objOption);
}

function setEmpClass( obj, emp_class ){
}


function returnFloat(s){
   if( s=='' )return 0;
   var patrn=/^[-,+]{0,1}[0-9.]{0,}$/;
   if (!patrn.exec(s))
        return 0;
   else
        return Math.round(s*10000)/10000;
}

function returnFloat2(s){
   if( s=='' )return 0;
   var patrn=/^[-,+]{0,1}[0-9.]{0,}$/;
   if (!patrn.exec(s))
        return 0;
   else
        return Math.round(s*100)/100;
}

function trim( str ){
	re = /^[ ]+|[ ]+$/g;
	return str.replace(re,"");
}

//若物件中字串超過則自動截短
function auto_substr( obj, max, is_alarm ){
   if( !obj )return;
   if( obj.value=='' )return;
   if( max==0 )return;

   var str = obj.value + '';
   if( str.length <= max )return;

   obj.value = str.substring(0,max);
   if( is_alarm==null || is_alarm==1 )alert( 'max ' + max + ' chars' );
}

//將輸入值存入SESSION
function save_temp_input( name, value ){	
	var now = new Date();
	var tt = now.getTime();
	new Ajax.Request( 'ajax_save_session_value.php?time=' + tt, { method:'get',
		parameters:{ 'item':'save', 'name':name, 'value':value },
		onSuccess: function(transport){
			var rs = transport.responseText;
		}
	});
}

//新增至我的購物車
function quick_add_to_cart( from_type, prod_id, sale_id, qty, from_ID ){
	var now = new Date();
	var tt = now.getTime();
	new Ajax.Request( '?pid=cart&item=add_to_cart&time=' + tt, { method:'get',
		parameters:{ 'from_type':from_type, 'prod_id':prod_id, 'sale_id':sale_id, 'qty':qty },
		onSuccess: function(transport){
			
			var rs = transport.responseText.evalJSON();
			if( rs.is_ok==0 ){
				alert( rs.msg );
				return;
			}
			if( from_type==1 ){
			//產品清單
				alert( 'Add to Cart Success.' + "\r\n\r\n" + rs.qty + ' ' + rs.unit + ' In Your Cart.' );
				var showObj = document.getElementById( from_ID );
				showObj.innerHTML = '<B>' + rs.qty + '</B> ' + rs.unit + ' in cart';
				
			}else{
				alert( 'Add to Cart Success.' );
				var showObj = document.getElementById( from_ID );
				showObj.innerHTML = 'Now <B>' + rs.qty + '</B> ' + rs.unit + ' in your cart';
			}
		}
	});
}
//==============================================================================
function setCookie(name, value, expires, path, domain, secure){
	var now = new Date( );
　  now.setTime( now.getTime( ) + 1000*expires );
    document.cookie= name + "=" + escape(CodeCookie(value)) +
        ((expires) ? "; expires=" + now.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name){ 　 
	var strArg=name+"="; 
	var nArgLen=strArg.length; 
	var nCookieLen=document.cookie.length; 
	var nEnd; 
	var i=0; 
	var j; 

	while( i<nCookieLen ){ 
		j=i+nArgLen; 
		if (document.cookie.substring(i,j)==strArg){ 
			nEnd=document.cookie.indexOf (";",j); 
			if (nEnd==-1) nEnd=document.cookie.length; 
			return DecodeCookie(unescape(document.cookie.substring(j,nEnd))); 
		} 
		i=document.cookie.indexOf(" ",i)+1; 　 
		if (i==0) break; 
	} 
	return null; 
} 

function deleteCookie(name, path, domain){
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

//COOKIE中文編碼
function CodeCookie(str){ 
　 var strRtn=""; 

　for (var i=str.length-1;i>=0;i--) 
　 { 
　　 strRtn+=str.charCodeAt(i); 
　　 if (i) strRtn+="a"; //用a作分隔符 
　 } 
　 return strRtn; 
} 


function DecodeCookie(str){ 
　 var strArr; 
　 var strRtn=""; 

　strArr=str.split("a"); 

　for (var i=strArr.length-1;i>=0;i--) 
　 strRtn+=String.fromCharCode(eval(strArr[i])); 

　return strRtn; 
} 


function CheckDate( sDate ) {
    sDate = sDate.replace( /\./g, "-" );
    sDate = sDate.replace( /\//g, "-" );

    //把未補0的數字補齊
    var sDate2 = sDate.split("-");
    var str2 = sDate2[0];
    for( var i=1; i<sDate2.length; i++ ){
         if( sDate2[i]=='' )continue;
         if( sDate2[i].length==1 )
             str2 += '0' + sDate2[i];
         else
             str2 += sDate2[i];
    }
    //把所有的非數字符號踢掉
    var str = str2.replace( /[^\d ]+/g, "" );

    len = str.length;
    var cur_date = '';
    if( len==6 ){
        //民國年處理( 小於100 )
        year = str.substr(0,2);
        month = str.substr(2,2);
        day = str.substr(4,2);

        cur_date = parseInt(year)+1911 + "-" + month + "-" + day;
    }else if( len==7 ){
        //民國年處理( 100年以後 )
        year = str.substr(0,3);
        month = str.substr(3,2);
        day = str.substr(5,2);

        cur_date = parseInt(year)+1911 + "-" + month + "-" + day;
    }else if( len==8 ){
        //西元年處理
        year = str.substr(0,4);
        month = str.substr(4,2);
        day = str.substr(6,2);

        cur_date = year + "-" + month + "-" + day;
    }
    return cur_date;
}

function getEmpCode( obj ){
    var str = obj.options[obj.selectedIndex].text;
    var idx = str.indexOf(" ");
    if( idx==-1 )return '';
    return  str.substr(0,idx);
}
function getEmpName( obj ){
    var str = obj.options[obj.selectedIndex].text;
    var idx = str.indexOf(" ");
    if( idx==-1 )return '';
    return  str.substr(idx+1,str.length);
}


//-------------------------------------------------------------------------------------------------------------//
ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false
//keys tracking and actions script
var emp_name_str = '';
var preEmpList = '';
var listObj = null;
function move_list( obj, emp_name, emp_list, obj_name, obj_code, obj_id, seq ) { //crossbrowsercode DD-JD
   var e = window.event;
   if (ns4) {
       var nKey=e.which; var ieKey=0
   }
   if (ie4) {
       var ieKey=event.keyCode; var nKey=0
   }
   if( preEmpList!=emp_list )emp_name_str = '';
   preEmpList = emp_list;

   if( ieKey==38 ){
       move_next( 1, emp_list, obj_name, obj_code, obj_id );  //向上
   }else if( ieKey==40 ){
       move_next( 2, emp_list, obj_name, obj_code, obj_id );  //向下
   }else if( ieKey==13 ){    //Enter
       document.getElementById(emp_list).style.display = 'none';
   }else{
       if( emp_name_str!=emp_name && emp_name!='' ){
           xajax_autoGetEmp( emp_name, emp_list, obj_name, obj_code, obj_id, seq );
       }
   }
   try{
       move_ok( emp_name, emp_list, obj_name, obj_code, obj_id, seq );
   }catch( e ){}
   emp_name_str = emp_name;
}

function init_drop_down_obj( emp_list, emp_name ){
   try{
      listObj = new Object(); //下拉物件
      listObj.curItem = null; //目前選到的
      listObj.curItemIdx = -1;  //目前選到的Index
      listObj.items = (document.getElementById( emp_list ).childNodes[0]).childNodes;
      listObj.maxItemIdx = -1;
      if( listObj.items )listObj.maxItemIdx = listObj.items.length;
      emp_name_str = emp_name;
    }catch( e ){
      document.getElementById('showMsg').innerText = e.message;
    }
}

function move_next( act, emp_list, obj_name, obj_code, obj_id  ){
   if( listObj==null )return;
   if( listObj.maxItemIdx==-1 )return;
   // Move Up
   if( act==1 && listObj.curItemIdx > 0 ){
       if( listObj.curItemIdx!= -1 )
           listObj.items[listObj.curItemIdx].style.backgroundColor='#FFFFFF';

       listObj.curItemIdx = listObj.curItemIdx-1;
       listObj.curItem = listObj.items[listObj.curItemIdx];
       listObj.curItem.style.backgroundColor = '#D4E6F8';

       document.getElementById(obj_name).value =  document.form1[emp_list+'_'+listObj.curItemIdx+'[emp_name]'].value;
       document.getElementById(obj_code).value =  document.form1[emp_list+'_'+listObj.curItemIdx+'[emp_code]'].value;
       document.getElementById(obj_id).value =  document.form1[emp_list+'_'+listObj.curItemIdx+'[emp_id]'].value;
   }
   //Move Down
   else if( act==2 && listObj.curItemIdx < listObj.maxItemIdx-1 ){
       if( listObj.curItemIdx!= -1 )
           listObj.items[listObj.curItemIdx].style.backgroundColor='#FFFFFF';

       listObj.curItemIdx++;
       listObj.curItem = listObj.items[listObj.curItemIdx];
       listObj.curItem.style.backgroundColor = '#D4E6F8';
       
       document.getElementById(obj_name).value =  document.form1[emp_list+'_'+listObj.curItemIdx+'[emp_name]'].value;
       document.getElementById(obj_code).value =  document.form1[emp_list+'_'+listObj.curItemIdx+'[emp_code]'].value;
       document.getElementById(obj_id).value =  document.form1[emp_list+'_'+listObj.curItemIdx+'[emp_id]'].value;
   }
}

function autoSetOrgId( idx ){
   if( idx==0 )return;
   var formObj = document.form1;
   fieldName = '[org_id]';
   for (var i = 0; i < formObj.length; i++) {
        if( !formObj[i].name )continue;
        if( formObj[i].tagName=='SELECT' && formObj[i].name.indexOf(fieldName) > -1 && formObj[i].selectedIndex==0 ){
             formObj[i].selectedIndex = idx;
        }
   }
}
//---------------------------------------------------------------------------------------------------------//


// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  //取得滑鼠座標
  var IE = document.all?true:false

  // If NS -- that is, !IE -- then set up for mouse capture
  if (!IE) document.captureEvents(Event.MOUSEMOVE)


  // Temporary variables to hold mouse x-y pos.s
  var tempX = 0
  var tempY = 0

  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  
    
  return new Array( tempX, tempY );
}

