function hide(hideitem)
{
    document.getElementById(hideitem).style.display="none";
}

function show(showitem)
{
    document.getElementById(showitem).style.display="block";
}

function IsEngorInt(v)
{
	var code = window.event != null ? v.keyCode : v.which;
	// 0 ~ 9 (code = 48 ~ 57) ; A ~ Z (code = 65 ~ 90) ; a ~ z (code = 97 ~ 122) ; 13 is enter ; 8 is backspace	
    if (!((code>=48 && code<=57) || (code>=65 && code<=90) || (code>=97 && code<=122) || code==13 || code==8 || code==0))
    {
        alert("Please input Alphabetic Characters or Figures.\n(Can not include space or symbol.)");
        
		if (window.event) //IE
        {
        	v.returnValue = false;
		}
		else //Firefox
		{
			v.preventDefault();
		}
    }
}

function trim(strText)
{
    while (strText.substring(0,1) == ' ')
        strText = strText.substring(1, strText.length);
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);
    return strText;
}

function amsDetail()
{
    winContent = window.open("","amsDetail","left=50,top=80,width=800,height=350,toolbar=no,menubar=yes,scrollbars=yes,resizable=yes");
    document.frmAMSDetail.submit();
    winContent.focus();
}

function frmCntrMoveDetail(c)
{
    document.frmCntrMove.cntr_no.value = c;
    winContent = window.open("","CntrMoveWin","left=50,top=80,width=800,height=380,toolbar=no,menubar=yes,scrollbars=yes,resizable=yes");
    document.frmCntrMove.submit();
    winContent.focus();
}

function frmCargoSubmit(v)
{
	var code = window.event != null ? v.keyCode : v.which;
	
	// 由於「TDB1_TrackingList.jsp」在submit時會傳入「13」的定字，而使得code值為「underfined」因此需增加v==13的判斷式
    if (v==13 || code==13)
    {
        _chkFormat();
        document.frmCargo.submit();
    }
}

function frmTrackSubmit(type,bl,cntr,bk)
{
    document.frmTrack.TYPE.value = type;
    document.frmTrack.BL.value = bl;
    document.frmTrack.CNTR.value = cntr;
    document.frmTrack.bkno.value = bk;
    document.frmTrack.PRINT.value = "YES";
    winContent = window.open("","TrackingPrintWin","left=50,top=80,width=800,height=420,toolbar=no,menubar=yes,scrollbars=yes,resizable=yes");
    document.frmTrack.submit();
    winContent.focus();
    //document.frmTrack.submit();
}

function frmPrint()
{
    winContent = window.open("","TrackingPrintWin","left=50,top=80,width=800,height=420,toolbar=no,menubar=yes,scrollbars=yes,resizable=yes");
    document.frmPrint.submit();
    winContent.focus();
}

function backForBL()
{
    top.location.href = "/servlet/TDB1_CargoTracking.do";
}

function _addMyFavCookie(sValue)
{
    var date = new Date((new Date()).getTime() + 2592000000);
    document.cookie = "TDB1_Function_Type=" + escape(sValue) + "; expires=" + date.toGMTString();
}

function _chkFormat()
{
    var x = document.getElementsByName('NO');
    for (i=0;i<x.length;i++) x[i].value = trim(x[i].value).toUpperCase();
}

function _isQueryNOInput()
{
    var cnt = 0;
    for (var i=0;i<5;i++)
        if(trim(document.frmCargo.NO[i].value).length==0)
            cnt++;
    if (cnt==5)
        return false;
    else
        return true;
}

function changeView(v)
{
    if (v=="quick"){
        hide("multiple");
        show("quick");
        document.getElementsByName('SEL')[3].checked = true;
    }else if(v=="multiple"){
        hide("quick");
        show("multiple");
        document.getElementsByName('SEL')[0].checked = true;
    }
    _addMyFavCookie(v);
}

function frmSubmit(v,c)
{
	var code = window.event != null ? v.keyCode : v.which;
    if (v==13 || code==13)
    {
        isSubmit = false;
        addValue = 0 ;
        if (document.frmCargo.SEL.length == 6)  addValue = 3;
        if (document.frmCargo.SEL[0+addValue].checked || document.frmCargo.SEL[1+addValue].checked || document.frmCargo.SEL[2+addValue].checked)
        {
            var no ;
            if (document.frmCargo.NO.length > 1 ){
                for (var i=0 ; i < 5 ; i++) document.frmCargo.NO[i].value = "";
                no = trim(document.frmCargo.NO[2+addValue].value).toUpperCase();
            }else{
                no = trim(document.frmCargo.NO.value).toUpperCase();
            }

            if (document.frmCargo.SEL[0+addValue].checked){
                if (no.length!=12){
                    alert("B/L NO. is not valid, please check again, thank you.");
                }else{
                    document.frmCargo.BL.value = no;
                    document.frmCargo.TYPE.value = "BL";
                    isSubmit = true;
                }
            }else if (document.frmCargo.SEL[1+addValue].checked){
                if (no.length!=11){
                    alert("Container NO. is not valid, please check again, thank you.");
                }else{
                    document.frmCargo.CNTR.value = no;
                    document.frmCargo.TYPE.value = "CNTR";
                    isSubmit = true;
                }
            }else if (document.frmCargo.SEL[2+addValue].checked){
                if (no.length!=12 && no.length!=9 && no.length!=8){
                    alert("Booking NO. is not valid, please check again, thank you.");
                }else{
                    document.frmCargo.bkno.value = no;
                    document.frmCargo.TYPE.value = "BK";
                    isSubmit = true;
                }
            }
        }else{
            document.frmCargo.NO[5].value = "";
            var noInput = !_isQueryNOInput();
            if (document.frmCargo.SEL[0].checked && noInput){
                alert("Please input at least one B/L NO. and try again, thank you.");
            }else if (document.frmCargo.SEL[1].checked && noInput){
                alert("Please input at least one Container NO. and try again, thank you.");
            }else if (document.frmCargo.SEL[2].checked && noInput){
                alert("Please input at least one Booking NO. and try again, thank you.");
            }else{
                document.frmCargo.TYPE.value = "MultipleTracking";
                isSubmit = true;
            }
        }
        if (isSubmit){
            _chkFormat();
            document.frmCargo.submit();
        }
        if (c==1) 
		{
			if (window.event) //IE
			{
				v.returnValue = false;
			}
			else //Firefox
			{
				v.preventDefault();
			}
		}
    }
}

function querybk(b,r,d,s,p)
{
    document.frmCargo.TYPE.value = "BK";
    document.frmCargo.bkno.value = trim(document.frmCargo.NO.value).toUpperCase();
    document.frmCargo.query_bkno.value = b;
    document.frmCargo.query_rvs.value = r;
    document.frmCargo.query_docno.value = d;
    document.frmCargo.query_seq.value = s;
    document.frmCargo.PRINT.value = p;
    document.frmCargo.submit();
}