﻿function GetClientUTC(elementId) {
    var now = new Date()
    var offset = -now.getTimezoneOffset() / 60;

    document.getElementById(elementId).value = offset;
}

function Toggle( targetID ) {
	if ( document.getElementById ) {
		target = document.getElementById( targetID );
		if ( target.style.display == 'none' ) {
			target.style.display = '';
		}
		else {
			target.style.display = 'none';
		}
	}
}

function TabSelected(sender, args) {
    var gridElement = $telerik.getChildrenByClassName(args.get_tab().get_pageView().get_element(), "RadGrid");
    if (gridElement.length > 0)
        setTimeout(function () { $find(gridElement[0].id).repaint(); }, 10);
}

function GetAjax( url, organizationId, type ) {
	var httpReq = false;
   
   // Mozilla/Safari
   if ( window.XMLHttpRequest ) {
		httpReq = new XMLHttpRequest();
   }
   
   // IE
   else if ( window.ActiveXObject ) {
      httpReq = new ActiveXObject( "Microsoft.XMLHTTP" );
   }
    
   httpReq.open( "GET", url + organizationId + '&UpdateCache=' + new Date().getTime(), true );
   httpReq.onreadystatechange = function() {
		if ( httpReq.readyState == 4 ) {
			UpdatePage( httpReq.responseText, organizationId, type );
      }
   }
   
   httpReq.send( null );
}
 
function UpdatePage( data, organizationId, type ) {
	document.getElementById( 'org' + organizationId + type ).innerHTML = data;
}

window.onload = function() {
    if ( self['LoadData'] ) {
	    LoadData();
	    setInterval( "LoadData()", 120000 );
	}
}

function autoComplete( field, select, property, forcematch )
{
    var found = false;
    for ( var i = 0; i < select.options.length; i++ ) {
        if ( select.options[i][property].toUpperCase().indexOf( field.value.toUpperCase() ) == 0 ) {
            found = true; break;
        }
    }

    if ( found ) { 
        select.selectedIndex = i;
    }
    else { 
        select.selectedIndex = -1;
    }

    if ( field.createTextRange ) {
        if ( forcematch && !found ) {
            field.value = field.value.substring( 0, field.value.length-1 );
            return;
        }

        var cursorKeys = "8;46;37;38;39;40;33;34;35;36;45;";
        if ( cursorKeys.indexOf( event.keyCode + ";" ) == -1 ) {
            var r1 = field.createTextRange();
            var oldValue = r1.text;
            var newValue = found ? select.options[i][property] : oldValue;
            if ( newValue != field.value ) {
                field.value = newValue;
                var rNew = field.createTextRange();
                rNew.moveStart( 'character', oldValue.length );
                rNew.select();
            }
        }
    }
}

var ctrl = "ctl00$cphMaster$txtSearch";  // Default
function handleFocus( targetID )
{
    var theform;
	var target;

	if ( window.navigator.appName.toLowerCase().indexOf("microsoft") > -1 ) {
		theform = document.form1;
	}
	else {
		theform = document.forms["form1"];
	}

	ctrl = targetID.id;

	return true;
}

function HighlightText( source, eventArgs ) {
    if ( source._currentPrefix != null ) {
        var list = source.get_completionList();
        var search = source._currentPrefix.toLowerCase();
        for ( var i = 0; i < list.childNodes.length; i++ ) {
            var text = list.childNodes[i].innerHTML;
            var index = text.toLowerCase().indexOf( search );
            if ( index != -1 ) {
                var value = text.substring( 0, index );
                value += '<span class="bold">';
                value += text.substr( index, search.length );
                value += '</span>';
                value += text.substring( index + search.length );
                list.childNodes[i].innerHTML = value;
            }
        }
    }
}

function ItemSelected( source, e ) {
    var node;
    var value = e.get_value();

    if ( value )
        node = e.get_item();
    else {
        value = e.get_item().parentNode._value;
        node = e.get_item().parentNode;
    }

    var text = (node.innerText) ? node.innerText : (node.textContent) ? node.textContent : node.innerHtml;
    source.get_element().value = text;
}
