var nSaveClicks = 0;
var nCancelClicks = 0;
 
var ADD = 1;
var EDIT = 0;
var VIEW_DETAILS = 2;
var MinInt = -(Math.pow(2, 31));
//change made here to window opening of movement editor http://tracker.kleinsystems.ca/browse/ZSP-441
//Browser versions
var IS_IE = navigator.userAgent.toLowerCase().indexOf('msie') > -1;
var IS_FIREFOX = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
var IS_OPERA = navigator.userAgent.toLowerCase().indexOf('opera') > -1;

function OneCheckBoxSelected(MainID, ChildID){
    var mainItem = document.getElementById(MainID);
    var childItem = document.getElementById(ChildID);
    if(mainItem && childItem){
        if(mainItem.checked && childItem.checked){
            childItem.checked = false;
        }
    }
    
}

function NewWindowWithParams(Url,ModuleName, RootID, Fields, Action)
{
    var fieldNames = Fields.split(',');
    var rezult='';
    if(Action == ADD && fieldNames.length != 0){
        for(var i = 0; i < fieldNames.length; i++)
        {
            /*
            if(fieldNames[i] == 'ID')
            {
                rezult += fieldNames[i]+ '=' + MinInt + '&';
                continue;
            }
            */
            var item = document.getElementById(RootID+'_'+fieldNames[i]);
            if(item)
            {
                rezult += fieldNames[i] + '=' + escape(item.value)+'&';
            }
            else
            {
                rezult += fieldNames[i] + '=&';
            }
        }
    }
    else if((Action == EDIT || Action == VIEW_DETAILS) && fieldNames.length != 0){
        for(var i = 0; i < fieldNames.length; i++)
        {
            var item = document.getElementById(RootID+'_'+fieldNames[i]);
            if(item)
            {
                rezult += fieldNames[i] + '=' + escape(item.value)+'&';
            }
        }
    }
    NewWindow(Url+'?'+rezult+'ACTION='+Action, ModuleName);
}

function NewWindowWithParamsAfterSelected(Url,ModuleName, RootID, Fields, Action)
{
    var fieldNames = Fields.split(',');
    var rezult='';
    if(Action == ADD && fieldNames.length != 0){
        for(var i = 0; i < fieldNames.length; i++)
        {
            /*
            if(fieldNames[i] == 'ID')
            {
                rezult += fieldNames[i]+ '=' + MinInt + '&';
                continue;
            }
            */
            var item = document.getElementById(RootID+'_'+fieldNames[i]);
            if(item)
            {
                rezult += fieldNames[i] + '=' + escape(item.value)+'&';
            }
            else
            {
                rezult += fieldNames[i] + '=&';
            }
        }
    }
    else if((Action == EDIT || Action == VIEW_DETAILS) && fieldNames.length != 0){
        for(var i = 0; i < fieldNames.length; i++)
        {
            var item = document.getElementById(RootID+'_'+fieldNames[i]);
            if(item)
            {
                rezult += fieldNames[i] + '=' + escape(item.value)+'&';
            }
        }
    }
    NewWindow(Url+'?'+rezult+'ACTION='+Action+'&BUTTON=AfterSelected', ModuleName);
}
function NewWindowWithParamsAsMakeFirst(Url,ModuleName, RootID, Fields, Action)
{
    var fieldNames = Fields.split(',');
    var rezult='';
    if(Action == ADD && fieldNames.length != 0){
        for(var i = 0; i < fieldNames.length; i++)
        {
            /*
            if(fieldNames[i] == 'ID')
            {
                rezult += fieldNames[i]+ '=' + MinInt + '&';
                continue;
            }
            */
            var item = document.getElementById(RootID+'_'+fieldNames[i]);
            if(item)
            {
                rezult += fieldNames[i] + '=' + escape(item.value)+'&';
            }
            else
            {
                rezult += fieldNames[i] + '=&';
            }
        }
    }
    else if((Action == EDIT || Action == VIEW_DETAILS) && fieldNames.length != 0){
        for(var i = 0; i < fieldNames.length; i++)
        {
            var item = document.getElementById(RootID+'_'+fieldNames[i]);
            if(item)
            {
                rezult += fieldNames[i] + '=' + escape(item.value)+'&';
            }
        }
    }
    NewWindow(Url+'?'+rezult+'ACTION='+Action+'&BUTTON=MakeFirst', ModuleName);
}

/***********************************************************************************************/
function NewWindowWithParamsForJobFromVoyage(Url,ModuleName, RootVoyageID,RootID, Fields, Action)
{
    var fieldNames = Fields.split(',');
    var rezult='';
    if(Action == ADD && fieldNames.length != 0){
        for(var i = 0; i < fieldNames.length; i++)
        {
            if(fieldNames[i] == 'ID')
            {
                var itemID = document.getElementById(RootID + '_' + 'ID');
                if(itemID)
                    rezult += fieldNames[i] + '=' + ((itemID.value != '')?itemID.value:MinInt)+'&';
                else
                    rezult += fieldNames[i]+ '=' + MinInt + '&';
                continue;
            }
            if(fieldNames[i] == 'VOYAGE_ID')
                var item = document.getElementById(RootVoyageID+'_'+'ID');

            if(item)
                rezult += fieldNames[i] + '=' + escape(item.value)+'&';
            else
                rezult += fieldNames[i] + '=&';
        }
    }
    else if((Action == EDIT || Action == VIEW_DETAILS) && fieldNames.length != 0){
        for(var i = 0; i < fieldNames.length; i++)
        {
            var item = document.getElementById(RootID+'_'+fieldNames[i]);
            if(item)
            {
                rezult += fieldNames[i] + '=' + escape(item.value)+'&';
            }
        }
    }
    NewWindow(Url+'?'+rezult+'ACTION='+Action, ModuleName);
}

/*************************************************************************************************************/
function NewWindowWithParamsForJobFromVoyageAfterSelected(Url,ModuleName, RootVoyageID,RootID, Fields, Action)
{
    var fieldNames = Fields.split(',');
    var rezult='';
    if(Action == ADD && fieldNames.length != 0){
        for(var i = 0; i < fieldNames.length; i++)
        { 
            //changes made to the below code because the query string
            //was being constructed so that the vessel id was the voyage id.
            //jlush July 3, 2007 Issue # 87250   
            item = null;         
            if(fieldNames[i] == 'ID')
            {
                var itemID = document.getElementById(RootID + '_' + 'ID');
                if(itemID)
                    rezult += fieldNames[i] + '=' + ((itemID.value != '')?itemID.value:MinInt)+'&';
                else
                    rezult += fieldNames[i]+ '=' + MinInt + '&';
                continue;
            }
            if(fieldNames[i] == 'VOYAGE_ID')
                item = document.getElementById(RootVoyageID+'_'+'ID');                
   
            if(item)
                rezult += fieldNames[i] + '=' + escape(item.value)+'&';
            else
                rezult += fieldNames[i] + '=&';
        }
    }
    else if((Action == EDIT || Action == VIEW_DETAILS) && fieldNames.length != 0){
        for(var i = 0; i < fieldNames.length; i++)
        {
            var item = document.getElementById(RootID+'_'+fieldNames[i]);
            if(item)
            {
                rezult += fieldNames[i] + '=' + escape(item.value)+'&';
            }
        }
    }
    NewWindow(Url+'?'+rezult+'ACTION='+Action+'&BUTTON=AfterSelected', ModuleName);
}

/**********************************************************************/
function NewWindowWithParamsForJobFromVoyageAsMakeFirst(Url,ModuleName, RootVoyageID,RootID, Fields, Action)
{
    var fieldNames = Fields.split(',');
    var rezult='';
    if(Action == ADD && fieldNames.length != 0){
        for(var i = 0; i < fieldNames.length; i++)
        {
            //changes made to the below code because the query string
            //was being constructed so that the vessel id was the voyage id.
            //jlush July 3, 2007 Issue # 87250  
            item = null;
            if(fieldNames[i] == 'ID')
            {
                var itemID = document.getElementById(RootID + '_' + 'ID');
                if(itemID)
                    rezult += fieldNames[i] + '=' + ((itemID.value != '')?itemID.value:MinInt)+'&';
                else
                    rezult += fieldNames[i]+ '=' + MinInt + '&';
                continue;
            }
            if(fieldNames[i] == 'VOYAGE_ID')
                var item = document.getElementById(RootVoyageID+'_'+'ID');

            if(item)
                rezult += fieldNames[i] + '=' + escape(item.value)+'&';
            else
                rezult += fieldNames[i] + '=&';
        }
    }
    else if((Action == EDIT || Action == VIEW_DETAILS) && fieldNames.length != 0){
        for(var i = 0; i < fieldNames.length; i++)
        {
            var item = document.getElementById(RootID+'_'+fieldNames[i]);
            if(item)
            {
                rezult += fieldNames[i] + '=' + escape(item.value)+'&';
            }
        }
    }
    NewWindow(Url+'?'+rezult+'ACTION='+Action+'&BUTTON=MakeFirst', ModuleName);
}

/**********************************************************************/
function ClearFieldsForSearch(fields, portIndex)
{
    var arrayFields =  fields.split('~');
    
    for(var i = 0; i < arrayFields.length; i++)
    {
        var item = document.getElementById(arrayFields[i]);
        if(item)
        {
            switch (item.type)
            {
                case 'text':
                    item.value = '';
                    break;
                case 'checkbox':
                    item.checked = false;
                    break;
                case 'hidden':
                    if (unescape(item.value).indexOf('=') != -1)
                        item.value = '';
                    else
                        item.value = '-2147483648';
                    break;
                case 'select-multiple':
                {
                    //Issue # 87355 The multi-select has to be cleared.
                    //We need to determine if it's a user-filled multi
                    //select or a system filled multiselect. -jlush
                    if (item.id.indexOf("DirectorySwitcher") > 0)
                    {
                        //we have a user filled multi-select
                        var count = 0;                    
                        for (count = 0; count < item.length; count ++)
                            item.children[count].innerHTML = "";
                    }
                    else
                    {
                         item.selectedIndex = -1;
                    }               
                    break;
                 }
                case 'select-one':
                {
                    //gy ZSP-1399 Special Case
                    if (item.id.indexOf("PortOccupancyAllSearchFrame_fldPort") > 0)
                    {
                        if (portIndex != null) 
                            item.selectedIndex = portIndex;
                        else
                            item.selectedIndex = 0;
                    }
                    else
                        item.selectedIndex = 0;
                    break;
                }
                case 'radio':
                {
                
                     //this specific control on the search fram was not working
                     //with the defaultChecked attribute and we needed to hard code
                     //this for a release.
                    if (item.id.indexOf("voyageAllSearchFrame_radHours") > 0)
                        item.checked = false;
                    if (item.id.indexOf("voyageAllSearchFrame_radRange") > 0)
                        item.checked = true;
                        
                    if(item.defaultChecked == true)
                        item.checked = true;                       


                    break;
                }
            }
        }
    }
//this Validator is not using any more since ASP.NET 2.0
//    if (ValidatorCommonOnSubmit)
//    {
//        ValidatorCommonOnSubmit();
//    }
}

function DisabledTimeField(timeID, chkID){
    var itemTime = document.getElementById(timeID);
    var itemChk = document.getElementById(chkID);
    if(item && itemChk){
        if(itemChk.checked){
            itemTime.className = 'disabled'; 
            itemTime.disabled = true;
            //itemTime.value = '';
        } 
        else{
            itemTime.className = ''; 
            itemTime.disabled = false;
        }
    }   
}

function FillMultiSelList(hidId, listId, hidSelId)
{
    try
    {
        var hidList = document.getElementById(hidId);
        var refList = document.getElementById(listId);
        var hidSelList = document.getElementById(hidSelId);
        
        if (hidList && refList && hidSelList)
        {
            var hidStr = unescape(hidList.value);
            var hidSelStr = unescape(hidSelList.value);
            
            var arrCouple = hidStr.split("&");
            if (arrCouple.length > 0)
            {
                var resStr = "";
                var TEXT = 0;
                var VALUE = 1;
                refList.length=0;
                for (var i=0; i<arrCouple.length; i++)
                {
                    var strItem = arrCouple[i];
                    var arrResult = strItem.split("=");
                    if (arrResult.length == 2)
                    {
                        if (hidSelStr.indexOf(strItem) != -1)
                        {
                            refList.options[refList.length] = new Option(arrResult[TEXT], arrResult[VALUE], true, true);
                            resStr += arrResult[TEXT] + "=" + arrResult[VALUE] + "&";
                        }
                        else
                            refList.options[refList.length] = new Option(arrResult[TEXT], arrResult[VALUE], false, false);
                    }
                }
                if (resStr.length > 0 && resStr.indexOf("&", resStr.length - 1) == (resStr.length - 1))
                    resStr = resStr.substring(0, resStr.length - 1);
                hidSelList.value = escape(resStr);
            }
        }
    }
    catch(e){
        alert(e.message);
    }
}

function SetValues(hidId, str)
{
    try{
        var hidList = document.getElementById(hidId);
        if (hidList)
            hidList.value = str;
    }
    catch(e){
        alert(e.message);
    }
}

function ChangeSelectedValues(obj, listId, hidSelId)
{
    try
    {
        var resStr = "";
        var refList = document.getElementById(listId);
        var hidSelList = document.getElementById(hidSelId);
        if (refList && hidSelList)
        {
            var count = refList.length;
            for (var i=0; i<count; i++)
            {
                if (refList.options[i].selected)
                    resStr += refList.options[i].text + "=" + refList.options[i].value + "&";
            }
            if (resStr.length > 0 && resStr.indexOf("&", resStr.length - 1) == (resStr.length - 1))
                resStr = resStr.substring(0, resStr.length - 1);
            hidSelList.value = escape(resStr);
        }
    }
    catch(e){
        alert(e.message);
    }
}

function NewWindow(url, editorName)
{
    var w;
    try{
        var width = 800;
        var height = 600;
        var windowName = 'win';       
        
        switch (editorName)
        {
            case "PilotEditor":
                width = 926;
                height = 632;
                windowName = 'PilotEditor';
            break; 
            case "TugEditor":
                width = 550;
                height = 450;
                windowName = 'TugEditor';
            break; 
            case "ResourceEditor":
                width = 615;
                height = 500;
                windowName = 'ResourceEditor';
            break; 
            case "ResourceAllEditor":
                width = 936;
                height = 700;
                windowName = 'ResourceAllEditor';
            break; 
            case "MovementEditor":
                width = 970;
                height = 610;
                windowName = 'MovementEditor';
            break;
            case "VoyageEditor":
                width = 940;
                height = 562;
                windowName = 'VoyageEditor';
            break;        
            case "ActivityEditor":
                width = 900;
                height = 438;
                windowName = 'ActivityEditor';
            break;
            case "CargoEditor":
                width = 445;
                height = 535;
                windowName = 'CargoEditor';
            break;
            case "CargoListEditor":
                width = 660;
                height = 500;
                windowName = 'CargoEditor';
            break;
            case "BargeEditor":
                width = 454;
                height = 361;
                windowName = 'BargeEditor';
            break;
            case "VesselEditor":
                width = 936;
                height = 481;
                windowName = 'VesselEditor';
            break;
            case "ColumnSwitcher":
                width = 650;
                height = 400;
                windowName = 'ColumnSwitcher';
            break;
            case "VesselDefectEdit":
                width = 603;
                height = 323;
                windowName = 'VesselDefectEdit';
            break;
            case "VesselDefectRepair":
                width = 646;
                height = 556;
                windowName = 'VesselDefectRepair';
            break;
            case "WorkPermit":
                width = 850;
                height = 300;
                windowName = 'WorkPermit';
            break;
            case "DGEditor":
                width = 450;
                height = 550;
                windowName = 'DGEditor';
            break;     
            case "ExemptionsEditor":
                width = 600;
                height = 350;
                windowName = 'ExemptionsEditor';
            break;
            case "UpdateConvoy":
                width = 1036;
                height = 400;
                windowName = 'UpdateConvoy';
            break;             
            case "WasteDisposal_DeclarationEditor":
                width = 800;
                height = 500;
                windowName = 'WasteDisposal_DeclarationEditor';
            break;    
            case "WasteDisposal_ActualEditor":
                width = 800;
                height = 610;
                windowName = 'WasteDisposal_ActualEditor';
            break;  
            case "CargoManifestHeaderEditor":
                width = 700;
                height = 790;
                windowName = 'CargoManifestHeaderEditor';
            break;   
            case "CargoManifestDetailEditor":
                width = 750;
                height = 830;
                windowName = 'CargoManifestDetailEditor';
            break;      
            case "ExportGridData":
                width = 150;
                height = 150;
                windowName = 'ExportGridData';
            break;           
        }
        //SM - 19/03/2008
        //This querystring var is used so that the codebehind knows to register a startup script that will
        //Auto resize the popup window with SetNewWindowSize()
        var ResizeSwitch;
        if(url.indexOf('?') == -1)
            ResizeSwitch = '?ReSize=1';
        else
            ResizeSwitch = '&ReSize=1';
        
        //ZSP-782 CargoManifestDetail Editor is now also NOT resizeable.  jlush 17-JULY-2008
        if (( editorName == 'MovementEditor') || ( editorName == 'CargoManifestDetailEditor'))
        {
            w = window.open(url + ResizeSwitch, windowName,'channelmode=0,directories=0,location=0,menubar=0,resizable=0,scrollbars=1,status=0,toolbar=0,width=' + width + ',height=' + height);
        }
        else
        {
            w = window.open(url + ResizeSwitch, windowName,'channelmode=0,directories=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0,width=' + width + ',height=' + height);
        }
    }
    catch(e){
        if (w == "failed" || w == null)
            alert("This web site requires enabled pop-ups. \nAdjust your browser settings or contact your system administrator for support.");
        else
            alert(e.message);
    }
}

function getRefToDivMod( divID, oDoc ) {
  if( !oDoc ) { oDoc = document; }
  if( document.layers ) {
    if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
      for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
        y = getRefToDivMod(divID,oDoc.layers[x].document); }
      return y; } }
  if( document.getElementById ) { return oDoc.getElementById(divID); }
  if( document.all ) { return oDoc.all[divID]; }
  return document[divID];
}

//Method to resize a popup window to be the size of the contents
function SetNewWindowSize()
{

    try
    {  
      if (IS_IE)
      {
          if ((screen.availWidth -document.documentElement.clientWidth)==21)
          {
             if((screen.availHeight -document.documentElement.clientHeight)==-4)
             {
                //alert('ie');
                 return; 
                 }
             if((screen.availHeight -document.documentElement.clientHeight)==30)
             {
                //alert('ie');
                 return; 
                 }
          }
       }
       else if (IS_FIREFOX)
       {
          //alert(screen.availWidth);
          //alert(document.documentElement.clientWidth);
          //alert(screen.availHeight);
          //alert(document.documentElement.clientHeight);
          if ((screen.availWidth -document.documentElement.clientWidth)==0)
             if((screen.availHeight -document.documentElement.clientHeight)==77)
             {
                 //alert('ff');
                 return;
                 }
       }
    }
    catch(e)
    {
        //catching and ignoring exceptions from the window resise
        //attempts because some unknown enviroment.
    }
     
    //STEVEM - 18/03/2008 The above code was not handling the resizing well, it has been
    //re-written to correctly resize all popups in any browser type.
    try
    {    
        var idOfDiv;    
        if(window.document.body.id)
            idOfDiv = window.document.body.id;
        else
           return;
           
        var oH = getRefToDivMod( idOfDiv ); if( !oH ) { return false; }
        var oW = oH.clip ? oH.clip.width : oH.offsetWidth;
        var oH = oH.clip ? oH.clip.height : oH.offsetHeight; if( !oH ) { return false; }
        var x = window; x.resizeTo( oW + 200, oH + 200 );
        var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;
        if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }
        else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }
        else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }
        if( window.opera && !document.childNodes ) { myW += 16; }
        //gy ZSP-1669
        //if (IS_FIREFOX) {myH -= 36;}
        
        x.resizeTo( oW + ( ( oW + 200 ) - myW ), oH + ( (oH + 200 ) - myH ) );
    }
    catch(e)
    {
        //catching and ignoring exceptions from the window resise
        //attempts because some pop-ups have resize=false
    }
}


function SetLoading(ImageSrc)
{
    try
    {
        //added code to make sure the element is not null.
        //code has been added in the application to disable the 
        //loading image in certain scenarios and it was causing
        //exceptions in this function.  Jason Lush Oct 23, 2007
        //Issue # 79479     
        if (document.getElementById("divLoading") == null)
                return

            
        document.getElementById("divLoading").style.display = "";
        document.body.style.cursor = "wait";
        setTimeout('document.images["progressImg"].src="' + ImageSrc + '"', 200); 
    }
    catch(e){
        alert(e.message);
    }
}

var gridNames = new Array();

function AddGridInfo(name)
{
    gridNames[gridNames.length] = name;
}

function Resize()
{
    try{
        for (var item = 0; item < gridNames.length; item++)
        {
            var agt=navigator.userAgent.toLowerCase();
            var tmpDivScrollingData = document.getElementById(gridNames[item] + '_divScrollingData');    
            
            if (agt.indexOf('opera')==-1 && tmpDivScrollingData)
                tmpDivScrollingData.style.overflowX = "scroll";
            else if (tmpDivScrollingData)
                tmpDivScrollingData.style.overflow = "scroll";
            
            if (tmpDivScrollingData)
                tmpDivScrollingData.style.width = "1px";
               
        }

        for (var item = 0; item < gridNames.length; item++)
        {
            var tmpDivButtons = document.getElementById(gridNames[item] + '_divButtons');
            var tmpDivScrollingData = document.getElementById(gridNames[item] + '_divScrollingData');    
            
            if (tmpDivScrollingData)
                if(tmpDivButtons.offsetWidth > 2)
                    tmpDivScrollingData.style.width = (tmpDivButtons.offsetWidth - 2) + "px"; 
        }
        
        var tmpDivLoading = document.getElementById("divLoading");
        var tmpIframeLoading = document.getElementById("iframeLoading");
        var tmpBody = document.getElementById("body");
        
        if (tmpDivLoading && tmpIframeLoading && tmpBody)
        {
            tmpDivLoading.style.width = "1px";
            tmpDivLoading.style.height = "1px";
            tmpDivLoading.style.width = tmpBody.clientWidth;
            tmpDivLoading.style.height = tmpBody.clientHeight;
            
            tmpIframeLoading.style.left = tmpBody.clientWidth / 2 - 64;
            tmpIframeLoading.style.top = tmpBody.clientHeight / 2 - 15;

            tmpIframeLoading.style.width = "127px"; // LOADING image width
            tmpIframeLoading.style.height = "29px"; // LOADING image height
            tmpIframeLoading.style.zIndex = tmpDivLoading.style.zIndex-1;
        }
    }
    catch(e)
    {
        alert(e.message);
    }
}

function SetHandlerResize()
{
    try{
        window.onresize = Resize;
        Resize();
    }
    catch(e){
        alert(e.message);
    }
}

function SwitchColumns(url, captions)
{
    var w;
    try
    {
        w = window.open(url + captions,'win' + (Math.round(Math.random() * 1000000)),'channelmode=0,directories=0,location=0,menubar=0,resizable=1,scrollbars=0,status=0,toolbar=0,width=800,height=430');
    }
    catch(e)
    {
        if (w == "failed" || w == null)
            alert("This web site requires enabled pop-ups. \nAdjust your browser settings or contact your system administrator for support.");
        else
            alert(e.message);
    }
}

function ConvertToPositiveNumber(strNumber)
{
    /*
    var re = /^\d+(\,\d+)?$/;
    if (strNumber.search(re) == 0)
        return parseFloat(strNumber.replace(",", "."));    
    else
        return 0;
    */
    var re = /^\d+(\.\d+)?$/;
    if (strNumber.search(re) == 0)
        return strNumber * 1;//return parseFloat(strNumber);    
    else
        return 0;
}

function SetMaxValue(outControl, inpControl1, inpControl2, inpControl3)
{
    try{
        var refOutControl = document.getElementById(outControl);
        var refInpControl1 = document.getElementById(inpControl1);
        var refInpControl2 = document.getElementById(inpControl2);
        var refInpControl3 = document.getElementById(inpControl3);
        if (refOutControl && refInpControl1 && refInpControl2 && refInpControl3)
        {
            var a = ConvertToPositiveNumber(refInpControl1.value);
            var b = ConvertToPositiveNumber(refInpControl2.value);
            var c = ConvertToPositiveNumber(refInpControl3.value);
            var result;
            
            if (a > b)
            {
                if (a > c)  result = a;
                else        result = c;
            }
            else
            {
                if (b > c)  result = b;
                else        result = c;
            }
            refOutControl.value = result;
        }
    }
    catch(e){
        alert(e.message);
    }
}

function SetTrim(outControl, inpControl1, inpControl2)
{
    try{
        var refOutControl = document.getElementById(outControl);
        var refInpControl1 = document.getElementById(inpControl1);
        var refInpControl2 = document.getElementById(inpControl2);
        if (refOutControl && refInpControl1 && refInpControl2)
        {
           if(refInpControl1.value != "" && refInpControl2.value != "")
            {
                var a = ConvertToPositiveNumber(refInpControl1.value);
                var b = ConvertToPositiveNumber(refInpControl2.value);
                var result;
                result = b - a;
                refOutControl.value = Math.round(result * 1000) / 1000;
           }
           else
            refOutControl.value = "";
        }
    }
    catch(e){
        alert(e.message);
    }
}

function SetCalcDraft(inpControl, outControl1, outControl2)
{
    try{
        var refInpControl = document.getElementById(inpControl);
        var refOutControl1 = document.getElementById(outControl1);
        var refOutControl2 = document.getElementById(outControl2);
        if (refInpControl && refOutControl1 && refOutControl2)
        {
            var draft = ConvertToPositiveNumber(refInpControl.value);
            refOutControl1.value = Math.round((draft - 1) * 1000) / 1000;
            refOutControl2.value = Math.round((draft + 1) * 1000) / 1000;
        }
    }
    catch(e){
        alert(e.message);
    }
}

function JE_Service(serviceControl, companyControl)
{
    this.ServiceControl = serviceControl;
    this.CompanyControl = companyControl;
}

function JE_DisableCompany(arrServiceControls)
{
    try{
        for (var i=0; i<arrServiceControls.length; i++)
        {
            var sName = arrServiceControls[i].ServiceControl;
            var cName = arrServiceControls[i].CompanyControl;
            
            var refServiceControl = document.getElementById(sName);
            var refCompanyControl = document.getElementById(cName);
            
            if (refServiceControl != null && refCompanyControl != null)
            {
                if (refServiceControl.type == "text" && refServiceControl.value != "0" && refServiceControl.value != "")
                {
                    refCompanyControl.className = "combobox";
                    if (refCompanyControl.type && refCompanyControl.type != "text")
                        refCompanyControl.disabled = false;
                    else
                        refCompanyControl.readOnly = false;
                }
                else
                {
                    refCompanyControl.className = "comboboxdisabled";
                    if (refCompanyControl.type && refCompanyControl.type != "text")
                        refCompanyControl.disabled = true;
                    else
                        refCompanyControl.readOnly = true;
                }
                if (refServiceControl.type == "checkbox")
                {
                    if (refServiceControl.name != "ctl00$Content$JobEditor$fldPROPELLER_FULLY_IMMERSED$chkCheckBox")
                    {
                        if (refServiceControl.checked)
                            refCompanyControl.className = "combobox";
                        else
                            refCompanyControl.className = "comboboxdisabled";
                        
                        if (refCompanyControl.type && refCompanyControl.type != "text")
                            refCompanyControl.disabled = !refServiceControl.checked;
                        else
                            refCompanyControl.readOnly = !refServiceControl.checked;
                    }
                    else
                    {
                        if (!refServiceControl.checked)
                            refCompanyControl.className = "combobox";
                        else
                            refCompanyControl.className = "comboboxdisabled";
                        
                        if (refCompanyControl.type && refCompanyControl.type != "text")
                            refCompanyControl.disabled = refServiceControl.checked;
                        else
                        {
                            refCompanyControl.readOnly = refServiceControl.checked;
                            if (refServiceControl.checked == true)
                                refCompanyControl.value = '';
                        }
                    }
                }
            }
        }
    }
    catch(e){
        alert(e.message);
    }
}

function JE_Disable_ETA_ETD(arrControls)
{
    try{
        for (var i=0; i<arrControls.length; i++)
        {
            var sName = arrControls[i].ServiceControl;
            var cName = arrControls[i].CompanyControl;
            
            var refServiceControl = document.getElementById(sName);
            var refCompanyControl = document.getElementById(cName);
            
            if (refServiceControl != null && refCompanyControl != null)
            {       
                if (refServiceControl.type == "checkbox")
                {
                    if (!refServiceControl.checked)
                    {
                        refCompanyControl.className = "txtbox";
                        refCompanyControl.readOnly = false;
                        var refLink = document.getElementById(cName.substring(0, cName.length - 7)  + "_aaa");
                        if (refLink)
                            refLink.style.display = "";
                    }
                    else
                    {
                        refCompanyControl.value = "";
                        refCompanyControl.className = "txtboxdisabled";
                        refCompanyControl.readOnly = true;
                        var refLink = document.getElementById(cName.substring(0, cName.length - 7) + "_aaa");
                        if (refLink)
                            refLink.style.display = "none";
                    }
                }
            }
        }
    }
    catch(e){
        alert(e.message);
    }
}

function DisableManifestDetailOptions(controlID1, controlID2)
{
    var control1 = document.getElementById(controlID1);
    var control2 = document.getElementById(controlID2);
    
    
}

function Disable_Pilot_Required(jobTypeControlId, pilotCountControlId, pilotRequiredControlId)
{
    try{
        var refJobType = document.getElementById(jobTypeControlId);
        var refPilotCount = document.getElementById(pilotCountControlId);
        var refPilotRequired = document.getElementById(pilotRequiredControlId);
        if (refJobType && refJobType.type == "select-one" && 
        refPilotCount && refPilotCount.type == "text" && 
        refPilotRequired && refPilotRequired.type == "checkbox")
        {
            if (refJobType.value == "310" && (refPilotCount.value * 1) > 0)
                refPilotRequired.disabled = false;
            else
                refPilotRequired.disabled = true;
        }
    }
    catch(e){
        alert(e.message);
    }
}

function OperatedInput(inputID, mask)
{
    var input = document.getElementById(inputID);
    input.onkeypress = check;
    function check(e)
    {
        var evt = (e) ? e : window.event;
        var code = (document.all) ? evt.keyCode:evt.charCode;
        if ((code == 0) || (code == 8)){return true;}

        var bool = false;
        function doRule(Rnamber)
        {
            var rule = mask.charAt(Rnamber);

            switch (rule){
	            case '_':
		            return true;
		            break;
	            case '`':
	                var val = input.value;
		            if ((code < 48) || (code > 57))
		            {
			            bool = true;
			            return false;
		            }
		            else
		            {
		                if (Rnamber == 0 && (code >= 48 && code <= 50))
		                {
		                    bool = false;
			                return true;
		                }
		                else if (Rnamber == 1 && val.length > 0)
		                {
		                    var chr = parseInt(val.substring(0, 1), 10);
		                    if ((chr == 0 || chr == 1) && (code >= 48 && code <= 57))
		                    {
		                        bool = false;
			                    return true;
		                    }
		                    else if (chr == 2 && (code >= 48 && code <= 51))
		                    {
		                        bool = false;
			                    return true;
		                    }
		                    else
		                    {
		                        bool = true;
			                    return false;
		                    }
		                }
		                else if (Rnamber == 3 && (code >= 48 && code <= 53))
		                {
		                    bool = false;
			                return true;
		                }
		                else if (Rnamber == 4 && val.length > 3)
		                {
		                    var chr = parseInt(val.substring(3, 4), 10);
		                    if ((chr >= 0 && chr <= 5) && (code >= 48 && code <= 57))
		                    {
		            	        bool = false;
			                    return true;
			                }
			                else
			                {
			                    bool = true;
			                    return false;
			                }    
		                }
		                else
			            {
			                bool = true;
			                return false;
			            }
		            }
		            break;
	            case '':
	                if(code >= 48 && code <= 51)
	                {
		                return true;
		                break;
		            }
		            else
		            {
		                return false;
		                break;
		            }
	            default:
		            input.value += rule;
		            if(input.setSelectionRange) 
		            {
			            var end = input.value.length;
			            input.setSelectionRange(end,end);
		            }
		            doRule(Rnamber+1);
		            if(bool){return false;}
		            else{return true;}
		            break;
            }
        }
        var num = input.value.length;
        return doRule(num);
    }
}

function RefreshTBA(ETD_Id, ETD_TIME_Id, ETD_TBA_Id, ETA_Id, ETA_TIME_Id, ETA_TBA_Id)
{
    try
    {
        var refETD = document.getElementById(ETD_Id);
        var refETD_TIME = document.getElementById(ETD_TIME_Id);
        var refETD_TBA = document.getElementById(ETD_TBA_Id);
        
        var refETA = document.getElementById(ETA_Id);
        var refETA_TIME = document.getElementById(ETA_TIME_Id);
        var refETA_TBA = document.getElementById(ETA_TBA_Id);
        
        if (refETD && refETD_TIME && refETD_TBA && refETA && refETA_TIME && refETA_TBA)
        {
            var regexpDate = /^([0][1-9]|[12]\d|[3][01])\/([0][1-9]|[1][0-2])\/\d{4}$/; // Date format
            var regexpTime = /^([01]\d|2[0-3]):[0-5]\d$/; // Time format
            
            //ETD
            var isETD_TBA = false;
            
            var etdDateValue = refETD.value;
            if(etdDateValue == "" || !regexpDate.test(etdDateValue))
            {
                refETD.value = "";
                isETD_TBA = true;
            }
            
            var etdTimeValue = refETD_TIME.value;
            if(etdTimeValue == "" || !regexpTime.test(etdTimeValue))
            {
                refETD_TIME.value = "";
                isETD_TBA = true;
            }
                
            refETD_TBA.checked = isETD_TBA;  
            
            //ETA
            var isETA_TBA = false;
            
            var etaDateValue = refETA.value;
            if(etaDateValue == "" || !regexpDate.test(etaDateValue))
            {
                refETA.value = "";
                isETA_TBA = true;
            }
            
            var etaTimeValue = refETA_TIME.value;
            if(etaTimeValue == "" || !regexpTime.test(etaTimeValue))
            {
                refETA_TIME.value = "";
                isETA_TBA = true;
            }
                
            refETA_TBA.checked = isETA_TBA;  
        }
    }
    catch(e)
    {
        alert(e.message);
    }
}        

function cls() { window.close(); }

//gy QT-1355 Tooltips for listBox
var dw_event = {
  
  add: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.addEventListener) obj.addEventListener(etype, fp, cap);
    else if (obj.attachEvent) obj.attachEvent("on" + etype, fp);
  }, 

  remove: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.removeEventListener) obj.removeEventListener(etype, fp, cap);
    else if (obj.detachEvent) obj.detachEvent("on" + etype, fp);
  }, 

  DOMit: function(e) { 
    e = e? e: window.event;
    e.tgt = e.srcElement? e.srcElement: e.target;
    
    if (!e.preventDefault) e.preventDefault = function () { return false; }
    if (!e.stopPropagation) e.stopPropagation = function () { if (window.event) window.event.cancelBubble = true; }
        
    return e;
  }
  
}

var Tooltip = {
    followMouse: true,
    offX: 8,
    offY: 12,
    tipID: "tipDiv",
    showDelay: 100,
    hideDelay: 200,
    
    ready:false, timer:null, tip:null, 

    init: function() {  
        var isIE/*@cc_on=1@*/;
        if ( document.createElement && document.body && typeof document.body.appendChild != "undefined" ) {
            if ( !document.getElementById(this.tipID) ) {
                var el;
                if (isIE)
                {
                    el=document.createElement ("<div id='" + this.tipID + "' >");
                    document.body.appendChild(el);
                }
                else
                {
                    el = document.createElement("DIV");
                    el.id = this.tipID; document.body.appendChild(el);
                }
            }
            this.ready = true;
        }
    },
   
    show: function(e, msg) {
        if (this.timer) { clearTimeout(this.timer);	this.timer = 0; }
        this.tip = document.getElementById( this.tipID );
        if (this.followMouse) // set up mousemove 
            dw_event.add( document, "mousemove", this.trackMouse, true );
        this.writeTip("");  // for mac ie
        this.writeTip(msg);
        viewport.getAll();
        this.positionTip(e);
        this.timer = setTimeout("Tooltip.toggleVis('" + this.tipID + "', 'visible')", this.showDelay);
    },
    
    writeTip: function(msg) {
        if ( this.tip && typeof this.tip.innerHTML != "undefined" ) this.tip.innerHTML = msg;
    },
    
    positionTip: function(e) {
        if ( this.tip && this.tip.style ) {
            // put e.pageX/Y first! (for Safari)
            var x = e.pageX? e.pageX: e.clientX + viewport.scrollX;
            var y = e.pageY? e.pageY: e.clientY + viewport.scrollY;
    
            if ( x + this.tip.offsetWidth + this.offX > viewport.width + viewport.scrollX ) {
                x = x - this.tip.offsetWidth - this.offX;
                if ( x < 0 ) x = 0;
            } else x = x + this.offX;
        
            if ( y + this.tip.offsetHeight + this.offY > viewport.height + viewport.scrollY ) {
                y = y - this.tip.offsetHeight - this.offY;
                if ( y < viewport.scrollY ) y = viewport.height + viewport.scrollY - this.tip.offsetHeight;
            } else y = y + this.offY;
            
            this.tip.style.left = x + "px"; this.tip.style.top = y + "px";
        }
    },
    
    hide: function() {
        if (this.timer) { clearTimeout(this.timer);	this.timer = 0; }
        this.timer = setTimeout("Tooltip.toggleVis('" + this.tipID + "', 'hidden')", this.hideDelay);
        if (this.followMouse) // release mousemove
            dw_event.remove( document, "mousemove", this.trackMouse, true );
        this.tip = null; 
    },

    toggleVis: function(id, vis) { // to check for el, prevent (rare) errors
        var el = document.getElementById(id);
        if (el) el.style.visibility = vis;
    },
    
    trackMouse: function(e) {
    	e = dw_event.DOMit(e);
     	Tooltip.positionTip(e);
    }
    
}

 
var viewport = {
  getWinWidth: function () {
    this.width = 0;
    if (window.innerWidth) this.width = window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth) 
  		this.width = document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth) 
  		this.width = document.body.clientWidth;
  },
  
  getWinHeight: function () {
    this.height = 0;
    if (window.innerHeight) this.height = window.innerHeight - 18;
  	else if (document.documentElement && document.documentElement.clientHeight) 
  		this.height = document.documentElement.clientHeight;
  	else if (document.body && document.body.clientHeight) 
  		this.height = document.body.clientHeight;
  },
  
  getScrollX: function () {
    this.scrollX = 0;
  	if (typeof window.pageXOffset == "number") this.scrollX = window.pageXOffset;
  	else if (document.documentElement && document.documentElement.scrollLeft)
  		this.scrollX = document.documentElement.scrollLeft;
  	else if (document.body && document.body.scrollLeft) 
  		this.scrollX = document.body.scrollLeft; 
  	else if (window.scrollX) this.scrollX = window.scrollX;
  },
  
  getScrollY: function () {
    this.scrollY = 0;    
    if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
  		this.scrollY = document.documentElement.scrollTop;
  	else if (document.body && document.body.scrollTop) 
  		this.scrollY = document.body.scrollTop; 
  	else if (window.scrollY) this.scrollY = window.scrollY;
  },
  
  getAll: function () {
    this.getWinWidth(); this.getWinHeight();
    this.getScrollX();  this.getScrollY();
  }
  
}
//gy^