
//---------------------
//-     Functions     -
//---------------------
// Retrieve a named object
function GetObject(id)
{
    var object;

    if (document.getElementById)
    {
        object = document.getElementById(id);
    }
    else if (document.all)
    {
        object = document.all[id];
    }
    else if (document.layers)
    {
        object = document.layers[id];
    }
    
    return object;
}

// Show/Hide an object
function ShowObjectx(objectId, show)
{
    // Give up if DHTML is not supported
	if (!DHTML) return;
	
    var object = GetObject(objectId);
    
    if(show == null)
    {
	    if((object.style.visibility == 'visible') || (object.style.visibility == ''))
	    {
	        object.style.visibility = 'hidden';
	        object.style.display = 'none';
	    }
	    else
	    {
	        object.style.visibility = 'visible';
	        object.style.display = 'inline';
	    }
	}
	else
	{
        if(show)
	    {
	        object.style.visibility = 'visible';
	        object.style.display = 'inline';
	    }
	    else
	    {
	        object.style.visibility = 'hidden';
	        object.style.display = 'none';
	    }
	}
}

// Show/Hide an object
function ShowObject2(objectId, show)
{
    // Give up if DHTML is not supported
	if (!DHTML) return;
	
    var object = GetObject(objectId);
    
    if(show == null)
    {
	    if((object.style.visibility == 'visible'))
	    {
	        object.style.visibility = 'hidden';
	        object.style.display = 'none';
	    }
	    else
	    {
	        object.style.visibility = 'visible';
	        object.style.display = 'block';
	    }
	}
	else
	{
        if(show)
	    {
	        object.style.visibility = 'visible';
	        object.style.display = 'block';
	    }
	    else
	    {
	        object.style.visibility = 'hidden';
	        object.style.display = 'none';     
	    }
	}
}

// Resize master page elements
function CheckResize()
{
    try
    {
        var masterMain = GetObject("masterMain");
        var masterMainTable = GetObject("masterMainTable");
        var masterBreadcrumb = GetObject("masterBreadcrumb");
        var masterLinks = GetObject("masterLinks");
        var grid = GetObject("ctl00xmainContentxgridResultsxgrid1_main");
        
        var minWidth = 815;
        var minHeight = 400;
        var bannerHeight = 150;
        var navigatorWidth = 200;
        var mapPanelWidth = 290;
        var scrollBarWidth = 20;
        
        var dimension = getViewportDimensions();
        
        var newWidth = dimension.w - navigatorWidth;
                
        if(newWidth < minWidth) newWidth = minWidth;
            
        var newHeight = dimension.h - bannerHeight;
        
        if(newHeight < minHeight) newHeight = minHeight;
        
        masterMain.style.width = newWidth + "px";
        masterMain.style.height = newHeight + "px";
        masterMainTable.style.width = (newWidth - scrollBarWidth) + "px";
        masterMainTable.style.height = (newHeight - scrollBarWidth) + "px";
        masterBreadcrumb.style.width = (newWidth - mapPanelWidth - scrollBarWidth) + "px";
        
        if(grid != null)
        {
            grid.style.height = (newHeight - 206) + "px";
        }
        
        var pepsDataContent = GetObject("pepsDataContent");
        if(typeof(pepsDataContent) != "undefined")
        {
            pepsDataContent.style.height = newHeight + "px";
        }
    }
    catch(ex)
    {
      ; // Null Statement
    }
}

//disables right mouse click for IE
function disableRightClickIE() 
{
    if (document.all) 
    {
        return false;
    }
}
//disables right mouse click for Mozilla
function disableRightClickMoz(e) 
{
    if (document.layers||(document.getElementById&&!document.all)) 
    {
        if (e.which==2||e.which==3) 
        {
            return false;
        }
    }
}


// Validate Checked Items For a Radio Check Box group
function ValidateCheckedItems(checkBoxId, checkBoxListId, isListClicked)
{
	// Get the objects.
	var allItem = GetObject(checkBoxId);
	var list = GetObject(checkBoxListId);

    if((list != null) && (allItem != null))
	{
        // Are there selected items?
        var i = 0;
        var sltCount = 0;
	    
        do
        {
            var item = GetObject(checkBoxListId + '_' + i);
	        
            if((item != null) && item.checked)
                sltCount++;
	        
            i++;
	    
        } while(item != null)
        i--;
		
	    // List item has been clicked
	    if(isListClicked)
	    {
	        if(sltCount == 0)
	        {
                allItem.checked = true;
            }
            else if(sltCount == i)
            {
               // This functionality turned off - no wanted as there is a difference between All items and checking all the items.
               // This is because the items may change.
               // allItem.checked = true;
               // ResetCheckboxList(checkBoxListId, false);
            }
            else
            {
                allItem.checked = false;
            }
	    }
	    else
	    {
	        // The 'All' item has been clicked
	        allItem.checked = true;
	      
	        if(sltCount > 0)
	        {
	            if(allItem.checked)
	            {
                    ResetCheckboxList(checkBoxListId, false);
    	        }
    	    }
	    }
	}
}

// Reset Check Box List values
function ResetCheckboxList(checkBoxListId, value)
{
    var i = 0;

    do
    {
        var item = GetObject(checkBoxListId + '_' + i);

        if(item != null)
            item.checked = value;

        i++   
    } while(item != null)
}

// Validate Range From/To values entered
function ValidateRangeEntered(source, args)
{
    args.IsValid = true;
    
    var to = GetObject((source.id.substr(0, source.id.length) + "T").replace("vld", "txt"));   
       
    if ((args.Value == "" && to.value != "") || (args.Value != "" && to.value == ""))
    {
        args.IsValid = false;
    }
}

// Validate that both the Total Depth and the Total Depth Range are not entered at the same time
function ValidateDepthTotal(source, args)
{
    args.IsValid = true;
    
    var from = GetObject((source.id.substr(0, source.id.length) + "F").replace("cvl", "txt"));   
    var to = GetObject((source.id.substr(0, source.id.length) + "T").replace("cvl", "txt"));   
       
    if (args.Value != "" && (from.value != "" || to.value != ""))
    {
        args.IsValid = false;
    }
}

// Process Key Press events
function ProcessKeyPress(eventDetail)
{
    var iCode, nCode;
    var eventSource;

    // Get key press event info
    if(_browser.b == "ns")
    {
        iCode = 0;
        nCode = eventDetail.which;
        eventSource = eventDetail.target;
    }
    else
    {
        iCode = event.keyCode;
        nCode = 0;
        eventSource = event.srcElement;
    }

    // Ingore the Enter key in TEXTAREA's
    if(eventSource.tagName !="TEXTAREA" && eventSource.outerHTML.indexOf("onkeypress") == -1)
    {
        if(iCode==13||nCode==13)
        {
            try
            {          
                try
                {
                    // Cancel event bubbling
                    event.cancelBubble = true;
                    event.returnValue = false;
                }
                catch(ex)
                {
                    ;
                }
                
                if(!_validationSucceeded)
                {
                    // Call the ProcessEnterKeyPress function
                    // - this may or may not be defined on a page
                    ProcessEnterKeyPress();
                    _validationSucceeded = Page_IsValid;
                }
            }
            catch(ex)
            {
                ;
            }
        }
    }
}

// Determine the browser type
function BrowserCheck()
{
    if(navigator.userAgent.indexOf("Win") >- 1)
        this.win = true;
    else
        this.win = false;
        
    var b = navigator.appName;

    if(b == "Netscape")
        this.b = "ns";
    else if(b == "Microsoft Internet Explorer")
        this.b = "ie";
    else if(b == "Opera")
        this.b = "opera";
    else
        this.b = b;

    this.version = navigator.appVersion;
    this.v = parseInt(this.version);
    this.ns = (this.b == "ns" && this.v >= 4);
    this.ns4 = (this.b == "ns" && this.v == 4);
    this.ns5 = (this.b == "ns" && this.v == 5);
    this.ns6 = (this.b == "ns" && this.v == 6);
    this.ie = (this.b == "ie" && this.v >= 4);
    this.ie4 = (this.version.indexOf('MSIE 4') > 0);
    this.ie5 = (this.version.indexOf('MSIE 5') > 0);
    this.ie6 = (this.version.indexOf('MSIE 6') > 0);
    this.opera = (this.b == "opera" && this.v >= 6);
    this.opera6 = (this.b == "opera" && this.v == 6);
    this.min = (this.ns || this.ie);
}

// Name a saved Core.Catalog search
function NameSavedSearch(sender, Event)
{
    ReNameSavedSearch('');

    Event.cancel = true;
    return false;
}

function ReNameSavedSearch(id)
{
    var settings = "center:yes;resizable:no;status:no;dialogHeight:340px;dialogWidth:430px"

    var argument = window.showModalDialog("NameSearch.aspx?Id=" + id, null, settings);
  
    return argument;
}

// Pop up a DispalyPage window
function open_display(domainId, itemId)
{
    open_window("/DisplayPage.aspx?DomainId=" + domainId + "&ItemId=" + itemId, 600, 500);
}

// Pop up a Compile window
function open_compile(domainId, itemId)
{
	open_window("/CompileBasic.aspx?Domain=" + domainId + "&ItemIds=" + itemId, 700, 700);
}

// Pop up a peps report window
function open_peps_report(country, state, subject)
{
	open_window("/PEPSReport.aspx?country=" + country + "&state=" + state+ "&subject=" + subject, 700, 700);
}

// Pop up a data listing window
function open_datalisting(reporttype, period, country)
{
	open_window("/DataListingViewer.aspx?reporttype=" + reporttype + "&period=" + period + "&country=" + country, 1024, 800);
}

// Pop up a map thumbnail window
function open_mapthumbnail(itemId, allowSubscription)
{
	open_window("/MapThumbNail.aspx?ItemId=" + itemId + "&AllowSubscription=" + allowSubscription, 700, 600);
}

// Pop up a map thumbnail window
function open_download(itemId)
{
	open_window("/DownLoad.aspx?ItemId=" + itemId, 700, 600);
}

// Pop up a GOPFR window
function open_gopfr(regionCode, countryCode)
{
	open_window("/Gopfr/notes_route.aspx?rgn=" + regionCode + "&cty=" + countryCode, 1024, 500);
}

// General purpose window openner
function open_window(url, width, height)
{
    if(typeof(width) != "undefined" && typeof(height) != "undefined" && width > 0 && height > 0)
    {
        newwindow = window.open(url, "", "width=" + width + ",height=" + height + ",location=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1");
    }
    else
    {
        newwindow = window.open(url, "", "location=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1");
    }
	newwindow.focus();
}

// Open the help window
function open_help_window(url, width, height)
{
    if(typeof(width) != "undefined" && typeof(height) != "undefined" && width > 0 && height > 0)
    {
        newwindow = window.open(url, "Help", "width=" + width + ",height=" + height + ",location=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1");
    }
    else
    {
        newwindow = window.open(url, "Help", "location=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1");
    }
	newwindow.focus();
}

function open_report_window(url,width, height)
{
    if(typeof(width) != "undefined" && typeof(height) != "undefined" && width > 0 && height > 0)
    {
        newwindow = window.open(url, "", "width=" + width + ",height=" + height + ",location=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1");
    }
    else
    {
        newwindow = window.open(url, "", "location=10,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1");
    }
    
    newwindow.focus();
}

function open_fileerrors_window(url, width, height)
{
    if(typeof(width) != "undefined" && typeof(height) != "undefined" && width > 0 && height > 0)
    {
        newwindow = window.open(url, "FileErrors", "width=" + width + ",height=" + height + ",location=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1");
    }
    else
    {
        newwindow = window.open(url, "FileErrors", "location=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1");
    }
	newwindow.focus();
}


// CheckBoxListRequiredFieldValidator script
function checkBox_verify(sender, args) 
{
    var val = GetObject(GetObject(sender.id).controltovalidate);
    var col = val.all;

    if ( col != null ) 
    {
        for ( i = 0; i < col.length; i++ ) 
        {
            if (col.item(i).tagName == "INPUT") 
            {
                if ( col.item(i).checked ) 
                {
                    return true;
                }
            }
        }
        return false;
    }
}

// RadioButtonListRequiredFieldValidator script
function radioButton_verify(sender, args)
{
    var val = GetObject(GetObject(sender.id).controltovalidate);
    var col = val.all;

    if ( col != null ) 
    {
        for ( i = 0; i < col.length; i++ ) 
        {
            if (col.item(i).tagName == "INPUT") 
            {
                if ( col.item(i).checked ) 
                {
                    return true;
                }
            }
        }
        return false;
    }
}

// Get Radio Button List Selected value
function getRadioButtonValue(controlId)
{
    var val = GetObject(controlId);
    var col = val.all;

    if ( col != null ) 
    {
        for ( i = 0; i < col.length; i++ ) 
        {
            if (col.item(i).tagName == "INPUT") 
            {
                if ( col.item(i).checked ) 
                {
                    return col.item(i).value;
                }
            }
        }
        
        return "";
    }
}

// show calendar
function ShowCalendar(id, informId) 
{
    return SetCalendarVisibility('visible', id, informId);
}

// hide calendar
function HideCalendar(id, informId) 
{
    return SetCalendarVisibility('hidden', id, informId);
}

// Set Calendar Visibility a calendar
function SetCalendarVisibility(visibility, id, informId) 
{
    object = GetObject(id);
    if (object)
    {
        object.style.visibility = visibility;
        
        informObject = GetObject(informId);
        if (informObject)
        {
            informObject.value = object.style.visibility;
            
            // Suppress server action if successfull
            return false;
        }
    }
 
    return true;
}

// toggle the visibility of the calendar
function ToggleCalendar(id, informId) 
{
    object = GetObject(id);
    if (object)
    {
        if (object.style.visibility == 'visible')
        {
            object.style.visibility = 'hidden';
        }
        else
        {
            object.style.visibility = 'visible';
        }

        informObject = GetObject(informId);
        if (informObject)
        {
            informObject.value = object.style.visibility;
            // Suppress server action if successfull
            return false;
        }
    }
    
    return true;
}

// Disable a button until the input value is not empty
function DisableButtonWhileEmpty(buttonId, inputId)
{
    button = GetObject(buttonId);
    input = GetObject(inputId);
    
    if (input.value == "" || input.value.trim() == "")
    {
        button.disabled = true;
    }
    else
    {
        button.disabled = false;
    }
}

// Click a button if enter pressed in a field
function ClickButton(e, buttonId)
{ 
    button = GetObject(buttonId);
    
    if (typeof button == 'object')
    { 
        if (navigator.appName.indexOf("Netscape")>(-1))
        { 
            if (e.keyCode == 13)
            { 
                button.click(); 
                return false; 
            } 
        } 
        if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1))
        { 
            if (event.keyCode == 13)
            { 
                button.click(); 
                return false; 
            } 
        }
    } 
} 

// Pop up save search
function ControlSavedSearch(id)
{
    var settings = "center:yes;resizable:no;status:no;dialogHeight:340px;dialogWidth:430px"

    var argument = window.showModalDialog("NameSearch.aspx?Id=" + id, null, settings);
    
    // SM: Need to get window.location.reload working then can re-introduce some functionality
    if (argument == true)
    {
        window.location = "/MyReports.aspx";
    }

    return argument;
}

// DisplayPage menu click
function objMenu_ItemClick(menuId, itemId)
{
    if(itemId == "objMenu_1") //Print menu
    {
        var url = "/PrintBasic.aspx?ItemId=" + GetObject("hidItem").value + "&Domain=" + GetObject("hidDomain").value;
	    window.open(url,"","width=700px,height=600px,location=0,menubar=0,toolbar=0,status=0,scrollbars=1");
    }
    else if(itemId == "objMenu_2") //Feedback menu
    {
        GetObject("divDisplayPageFeedback").style.visibility = "visible";
        location.href = '#Feedback';
    }
}

// Timeout Dialog
function SessionTimeout()
{
    location.href = "/TimeOut.aspx";
}

function TestTest(id, id2)
{
    var obj = GetObject(id);
    var obj2 = GetObject(id2);

    window.location="Home.aspx?moo=" + obj.value + "&baa=" + obj2.value;
}

function ShowObject(id) 
{
    object = GetObject(id);
    if (object)
    {
        object.style.visibility = "visible";
    }
}

function ShowHideObject(anchor, target) 
{
    anchorObject = GetObject(anchor);
    targetObject = GetObject(target);
 
    if (anchorObject && targetObject)
    {
        if (anchorObject.innerText == "hide details")
        {
            anchorObject.innerText = "show details"
            targetObject.style.visibility = "hidden";
        }
        else
        {
            anchorObject.innerText = "hide details"
            targetObject.style.visibility = "visible";
        }
    }
}

function TransRedirect(queryString, ddlTransTypeID, ddlClassID, ddlStrengthID, ddlSiteID, ddlCompanyID, ddlLineID, rblDisplayUnitID)
{
    var ddlTransType = GetObject(ddlTransTypeID);
    var ddlClass = GetObject(ddlClassID);
    var ddlStrength = GetObject(ddlStrengthID);
    var ddlSite = GetObject(ddlSiteID);
    var ddlCompany = GetObject(ddlCompanyID);
    var ddlLine = GetObject(ddlLineID);
    var rblDisplayUnit = GetObject(rblDisplayUnitID);
    
    var rblDisplayUnitValue = 1;
    
    for(i=0;i<rblDisplayUnit.all.length;i++)
    {
        var obj = rblDisplayUnit.all[i];
        
        if (obj.checked == 1)
        {
            rblDisplayUnitValue = obj.value;
        }
    }
    
    
    if (ddlTransType.value == 1)
    {
        ddlStrength.value = -1;
    }

    window.location='TransDetails.aspx?' + queryString + 
        'tran=' + ddlTransType.value + 
        '&class=' + ddlClass.value +
        '&strength=' + ddlStrength.value +
        '&site=' + ddlSite.value +
        '&comp=' + ddlCompany.value +
        '&line=' + ddlLine.value +
        '&unit=' + rblDisplayUnitValue;
}

function ServerFeeding(args,  context)
{
}

function CheckBoxClicked(args, checkBoxID)
{
    var checkBox = GetObject(checkBoxID);
    
    args = args + '|' + checkBox.checked;
    
    callBack(args, 'ClientCallback');
}

function TextBoxBlur(args, id)
{
    var all = document.all;
    
    var textBox = null;
    
    for(i=0;i<all.length;i++)
    {
        var obj = all[i];
        
        if (obj.alt == id)
        {
            textBox = obj;
        }
    }
  
    args = args + '|' + textBox.value;
    
    callBack(args, 'ClientCallback');
}

function HideOnTypeDropDownValue(ddlID, targetID, showValue, lblID, lookup, imgID) 
{
    var ddl = GetObject(ddlID);
    var target = GetObject(targetID);
    var lbl = GetObject(lblID);
    var img = GetObject(imgID);
 
    if (ddl && target && lbl && img)
    {
        var list = new Array();
        list = lookup.split('|');
        
        for(i=0;i<list.length;i++)
        {
            var item = list[i];
            var itemsplit = new Array();
            itemsplit = item.split(',');
            
            if (itemsplit[0] == ddl.value)
            {
                lbl.innerText = itemsplit[1];
            }
        }
    
        if (ddl.value == showValue)
        {
            target.disabled = false;
            img.style.visibility = "";
        }
        else
        {
            target.disabled = true;
            target.value = -1;
            img.style.visibility = "hidden";
        }

    }
}

function HideOnAreaTypeDropDownValue(ddlID, targetTextBoxID, tdR1ID, tdR2ID, tdR3ID, showValue) 
{
    var ddl = GetObject(ddlID);
    var targetTextBox = GetObject(targetTextBoxID);
    var tdR1 = GetObject(tdR1ID);
    var tdR2 = GetObject(tdR2ID);
    var tdR3 = GetObject(tdR3ID);
 
    if (ddl && targetTextBox && tdR1 && tdR2 && tdR3)
    {
        if (ddl.value == showValue)
        {
            targetTextBox.style.visibility = "hidden";
            tdR1.style.visibility = "hidden";
            tdR1.style.display = "none";
            tdR2.style.visibility = "hidden";
            tdR2.style.display = "none";
            tdR3.style.visibility = "hidden";
            tdR3.style.display = "none";
            targetTextBox.value = '';
        }
        else
        {
            targetTextBox.style.visibility = "";
            tdR1.style.visibility = "";
            tdR1.style.display = "";
            tdR2.style.visibility = "";
            tdR2.style.display = "";
            tdR3.style.visibility = "";
            tdR3.style.display = "";
        }
    }
}