function attachResize(gridName, toolTipArray, widthOffset)
{
	var resize = function()
	{
	    //alert(gridName);
	    var gridDomObject = document.getElementById(gridName + '_dom');
		if (gridDomObject)
		{		    		    
		    gridDomObject.style.display = 'none';
		    var calculatedWidth = document.body.clientWidth - widthOffset;		    
		    gridDomObject.style.width = parseInt(calculatedWidth) + 'px';
		    gridDomObject.style.display = 'block';
		}
	    
	    var objectNameMatches = gridName.match(/[^_]+$/);
	    if (objectNameMatches && objectNameMatches[0])
	    {
	        var gridObject = document.getElementById(objectNameMatches[0]);
		    
		    if (gridObject)
			    gridObject.Render();
	    }
	}
	
	setInterval("attachToolTips('" + gridName + "', ['" + toolTipArray.join("','") + "'])", 1000);

	window.onresize = resize;

	resize();
}

function attachToolTips(gridName, toolTipArray)
{
	var topRow = document.getElementById(gridName + '_top_head');
    var currentElement;

	if (topRow && toolTipArray && toolTipArray.length > 0)
	{
		// All the td's
		for (var x = 0; x < topRow.childNodes.length; x++)
		{
            if (toolTipArray[x])
            {
		        currentElement = topRow.childNodes[x];
			    // Current td
			    if (currentElement.tagName.toLowerCase() == 'td')
			    {
				    // The first div
				    if (isChild(currentElement, 0, 'div'))
				    {
				        currentElement = currentElement.childNodes[0];
    				    
					    // The inner table
					    if (isChild(currentElement, 0, 'table'))
					    {
    				        currentElement = currentElement.childNodes[0];
                            
                            // The inner tbody
					        if (isChild(currentElement, 0, 'tbody'))
                            {
        				        currentElement = currentElement.childNodes[0];
                                
                                // The inner tr
					            if (isChild(currentElement, 0, 'tr'))
                                {
        				            currentElement = currentElement.childNodes[0];

                                    // The inner td                                
					                if (isChild(currentElement, 0, 'td'))
                                    {
        				                currentElement = currentElement.childNodes[0];
                                        
                                        currentElement.title = toolTipArray[x];
                                    }					    
                                }					    
                            }					    
					    }
				    }
			    }
			}
		}
	}
}

function SetFocus(controlToFocus)
{
document.all(controlToFocus).focus();
}

function clientSideOnSelect()
{
    return false;
}

function confirm_delete()
{
    return confirm("Are you sure you want to delete this item?");
}

function confirm_generic(verbAndDirectObject)
{
    return confirm("Are you sure you want to " + verbAndDirectObject + "?");
}

function canAddCharacter(textarea, maxChars) 
{ 
     if(typeof(textarea.onkeypress.arguments[0]) != 'undefined') 
          var keyCode = textarea.onkeypress.arguments[0].keyCode; 
     else 
     { 
          if(document.selection.createRange().text.length != 0) return true; 
          var keyCode = event.keyCode; 
     } 

     var allowedChars = new Array(8, 37, 38, 39, 40, 46);     //Backspace, delete and arrow keys 
     for(var x=0; x<allowedChars.length; x++) if(allowedChars[x] == keyCode) return true; 

     if(textarea.value.length < maxChars) return true; 

     return false; 
}

function trimLength(textarea, maxChars) 
{ 
     if(textarea.value.length <= maxChars) return; 

     textarea.value = textarea.value.substr(0, maxChars) 
}

function openWindow(url) 
{ 
var newWin = window.open(url, 'newWin'); 
}

if (window.WebForm_FireDefaultButton != "undefined")
{
    window.WebForm_FireDefaultButton = function(event, target)
    {
        var element = event.target || event.srcElement;
        if (event.keyCode == 13 && !(element && (element.tagName.toLowerCase() == "textarea")))
        {
            var defaultButton;
            if (__nonMSDOMBrowser)
            {
                defaultButton = document.getElementById(target);
            }
            else
            {
                defaultButton = document.all[target];
            }
            
            if (defaultButton && typeof(defaultButton.click) != "undefined")
            {
                __defaultFired = true;
                defaultButton.click();

                event.cancelBubble = true;

                if (event.stopPropagation)
                    event.stopPropagation();

                return false;
            }
        }
        return true;
    }
}

function popupWindow(url, width, height)  
{  
    debugger;
    if(window.opener == null)
    {
        var win;
        win = window.open(  
             url,  
             '_blank',  
             'status=1,toolbar=0,menubar=0,location=0,resizable=1,scrollbars=1,height=' + height + ',width=' + width);    
        win.focus();
        
        return win;
    }
    else
    {
        window.location.href = url;
    }
}

function popupWindowNoScroll(url, width, height) {
    debugger;
    if (window.opener == null) {
        var win;
        win = window.open(
             url,
             '_blank',
             'status=1,toolbar=0,menubar=0,location=0,resizable=1,scrollbars=0,height=' + height + ',width=' + width);
        win.focus();

        return win;
    }
    else {
        window.location.href = url;
    }
}

function popupWindow(url, width, height, target)  
{  
    var win;
    win = window.open(  
         url,  
         target,  
         'status=1,toolbar=0,menubar=0,location=0,resizable=1,scrollbars=1,height=' + height + ',width=' + width);

    // required for addressing a bug in Chrome 3.0 to 4.0
    if (typeof (win) != 'undefined' && !win.closed) {
        win.blur();
    }
    
    win.focus();
    
    return win;
}

function popupWindowNoReference(url, width, height, target)  
{  
    var win;
    win = window.open(  
         url,  
         target,
         'status=1,toolbar=0,menubar=0,location=0,resizable=1,scrollbars=1,height=' + height + ',width=' + width);

    // required for addressing a bug in Chrome 3.0 to 4.0
    if (typeof (win) != 'undefined' && !win.closed) {
        win.blur();
    }
    
    win.focus();
}

function popupNamedWindow(url, guid, width, height)
{
    if(!guid)
        guid = '';

    var windowName = guid.replace(/-/g, '');

    return popupWindow(url, width, height, windowName);
}

function isChild(element, childNumber, tagName)
{
    return (element.childNodes && element.childNodes[childNumber]
        && element.childNodes[childNumber].tagName
        && element.childNodes[childNumber].tagName.toLowerCase() == tagName);
}

function radGridCreated() 
{ 
    var scrollArea = document.getElementById(this.ClientID + "_GridData"); 
    scrollArea.style.height = this.MasterTableView.Control.clientHeight + 30 + "px"; 
} 
function RadItemsLoaded(combo)
{
    if (combo.Items.length > 0)
    {
        combo.SetText(combo.Items[0].Text);
    }
    combo.ShowDropDown();
}

function redirectWithUTCOffset()
{
    if (document.location.href.indexOf('UTCOffset=') == -1)
    {
        var offset = (new Date().getTimezoneOffset() / 60) * -1;
        if (document.location.href.indexOf('?') == -1)
        {
            document.location.href = document.location.href + '?UTCOffset=' + offset;
        }
        else
        {
            document.location.href = document.location.href + '&UTCOffset=' + offset;
        }
    }
}

// If current window was popped up, will close the child window and redirect the parent window
// to the URL specified in the parameter, if the parent window is not already there.
// If the current window was not popped up, then it will redirect itself to the passed in parameter.
// The URL should be passed in as a relative link, e.g. CompanySearch.aspx or RadControls/Menu/
function CloseOrRedirect(redirectURL)
{
    if(window.opener != null)
    {
        if(redirectURL != null && (window.opener.location.href.indexOf(redirectURL) < 0))
        {
            window.opener.location.href = redirectURL;
        }
        
        self.close();
    }
    else
    {
        if(redirectURL != null)
        {
            window.location.href = redirectURL;
        }
        else
        {
            window.location.href = '/';
        }
    }
}

// javascript to retrieve a cookie -- taken from w3schools.org
function getCookie(c_name)
{
    if (document.cookie.length>0)
        {
            c_start=document.cookie.indexOf(c_name + "=");
            if (c_start!=-1)
            { 
                c_start=c_start + c_name.length+1; 
                c_end=document.cookie.indexOf(";",c_start);
                if (c_end==-1) c_end=document.cookie.length;
                return unescape(document.cookie.substring(c_start,c_end));
            } 
        }
    return "";
}

function displayRemaining(textarea, maxChars, targetDiv) {
    elem = document.getElementById(targetDiv);
    var em = '<span style=\'color:red;font-weight:bold\'>';
    var slashem = '</span>';

    if (maxChars - textarea.value.length == 1)
        elem.innerHTML = em + '1' + slashem + ' character remaining';
    else if (maxChars - textarea.value.length >= 0 && maxChars - textarea.value.length <= 5)
        elem.innerHTML = em + (maxChars - textarea.value.length) + slashem + ' characters remaining';
    else if (maxChars - textarea.value.length >= 0)
        elem.innerHTML = '' + (maxChars - textarea.value.length) + ' characters remaining';
    else
        trimLength(textarea, maxChars);
}

function GetRadWindow()
{
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement && window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    return oWindow;
}

function createCookie(name, value, seconds) {
    if (seconds) {
        var date = new Date();
        date.setTime(date.getTime() + (seconds * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

Number.prototype.formatMoney = function (c, d, t)
{
    var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

function popupWindowLimitToScreen(url, width, height, target) {
    var actualWidth = width;
    var actualHeight = height;

    if (actualHeight > screen.availHeight) {
        actualHeight = screen.availHeight;
    }
    if (actualWidth > screen.availWidth) {
        actualWidth = screen.availWidth;
    }

    popupWindow(url, actualWidth, actualHeight, target);
}

function popupWindowToFullScreen(url, target) {
    var actualWidth = screen.availWidth;
    var actualHeight = screen.availHeight;

    popupWindow(url, actualWidth, actualHeight, target);
}
