//for calendar start
function calendar_hide(e)
{
  //скрыть календарь
  e.hide();             
  //теряем фокус    
  var i;
  for (i=0;i < theForm.elements.length;i++) {
      if (theForm.elements[i].id == e._element.id) 
      {
          theForm.elements[i].blur();
          return;
      }    
  }
}
//for calendar end

//for windows start
function GetRadWindow()
{
        var oWindow = null;
        if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
        else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;//IE (and Moz az well)                
        return oWindow;
}

function RadWindowCloseAndClickButton(BtnId)
{
        GetRadWindow().Close();
        if(BtnId != ''){
                s = parent.ge(BtnId).href;                
                subs = s.substring(s.indexOf(':') + 1, s.length);
                s= "parent." + subs;
                eval(s);                                
        }
}                       
               
function RadWindowCloseAndSetRadComboBox(rcb, text, value)
{
        GetRadWindow().Close();
        rcb = eval('parent.'+rcb);
        if(rcb != null){
          rcb.SetText(text);
          rcb.SetValue(value);          
        }
}                       

function ShowRadWindow(targetId, url, WinId, BtnId)
{
        window.radopen(url+"?targetId=" + targetId +"&ButtonID="+BtnId, WinId);
        return false; 
}

function ShowRadWindowGet(getParams, url, WinId)
{
        window.radopen(url+"?"+getParams, WinId);
        return false; 
}

function RadWindowSetSize(width , height)
{
        oWindow = GetRadWindow();
        if(oWindow != null)
        {
                oWindow.SetSize(width, height);  
        } 
}

//for windows end

//for hint
function hideHint(id,timer)
{
    clearTimeout(timer);
    return setTimeout('hide(\'' +id+ '\')', 200);
}

//moveType => bottom = 0, right = 1 
function showHint(id,timer, sender, needPosition, moveType )
{
        clearTimeout(timer);
        
        if(needPosition !== false){
        var coord = findPos(sender); // находим координаты 
/*        xmove = moveType==0 ? sender.clientHeight : 30;
        ymove = moveType==1 ? sender.clientWidth : 20;*/
        xmove = 30;
        ymove = 20;

        ge(id).style.left = coord[0] + ymove + 'px';
        ge(id).style.top = coord[1] + xmove+ 'px';
        }

    return setTimeout('showinline(\'' +id+ '\')', 200);
}

//for hint end

//for TimeLine
        function onLoadForTimeLine(now,xml,divId) {
                  var eventSource = new Timeline.DefaultEventSource();
                  var bandInfos = [
                    Timeline.createBandInfo({
                        eventSource:    eventSource,
                        date:           now,
                        width:          "70%", 
                        intervalUnit:   Timeline.DateTime.HOUR, 
                        intervalPixels: 30
                    }),
                        Timeline.createBandInfo({
                        showEventText:  false,
                        trackHeight:    0.5,
                        trackGap:       0.5,
                                eventSource:    eventSource,
                        date:           now,
                        width:          "15%", 
                        intervalUnit:   Timeline.DateTime.DAY, 
                        intervalPixels: 100
                    }),
                    Timeline.createBandInfo({
                                showEventText:  false,
                        trackHeight:    0.5,
                        trackGap:       0.5,
                        eventSource:    eventSource,
                        date:           now,
                        width:          "15%", 
                        intervalUnit:   Timeline.DateTime.WEEK, 
                        intervalPixels: 200
                    })
                  ];
                  bandInfos[1].syncWith = 0;
                  bandInfos[1].highlight = true;
                  bandInfos[2].syncWith = 0;
                  bandInfos[2].highlight = true;
                  
                  eval('tl'+divId +'= Timeline.create(document.getElementById(divId), bandInfos)');
                  Timeline.loadXML(xml, function(xml, url) { eventSource.loadXML(xml, url); });
        }        
        

        //var resizeTimerIDtlDiv = null;
        function onResizeForTimeLine(divId) {
            eval('if (resizeTimerID'+divId+' == null) {'
                +' resizeTimerID'+divId+' = window.setTimeout(function() {'
                   +' resizeTimerID'+divId+' = null;'
                    +'tl'+divId+'.layout();}, 300)}');
        }
//for TimeLine end

//for RadComboBox
function OnItemsRequestedShowDropDown(rcb){
    if (rcb.Items.length != 0) rcb.ShowDropDown();
}

function rcbValidate_OnFocus(rcb)
{        
    rcb.InputDomElement.style.backgroundColor = '#fff';
    if(rcb.TextHidden.value == 'Select...') rcb.SetText("");
}    


function rcbRequestItems(rcb, Value, showLoading)
{ 
  if(showLoading !== false)
  {
          rcb.SetText("Loading...");
          rcb.SetValue('');
  }
  rcb.RequestItems(Value, false);
}
function rcbRequestItemsWithFilter(rcb, Value,filter,showLoading)
{ 
  if((filter == null) ||(filter === false )){ 
        rcbRequestItems(rcb, Value+'_',showLoading)
  }else{
        rcbRequestItems(rcb, Value+'_'+filter,showLoading)
  }                             
}
function RadComboBoxItemsRequested(combo){
    if (combo.Items.length == 0){
        combo.SetText("No record found");
        combo.SetValue("");
    }                
}                                            
//for RadComboBox end

function TopPageSelectClick(e)
{
    if(e.value != "")
    {           
        try{
                eval(e.value);
        }catch(err){        
                alert(err);
        }
    }
    e.selectedIndex = 0;
}

function EmulatorClickButton(buttonID)
{
    btn = ge(buttonID)
    if(btn != null)
     s = btn.href.replace(/%20/g,' ').replace(/%22/g,'\"').replace(/\n/g,' ');
    else
     alert(buttonID + ' is not object');
    try{
            eval(s);
    }catch(err){        
            alert(err);
    }
}

function beginRequestWidget(sender, args)
{
  ShowHideDisableDiv(args._postBackElement, true);
}
                
function endRequestWidget(sender, args)
{
  for(var i=0; i < sender._updatePanelClientIDs.length; i++)
    ShowHideDisableDiv(ge(sender._updatePanelClientIDs[i]), false);        

  if(args._error != null) alert(args._error.message);  

/*  if(args._error == null){
    for(var i=0; i< sender._panelsToRefreshIDs.length; i++)
    {
      id = sender._panelsToRefreshIDs[i].replace(/\$/g, "_") 
      ShowHideDisableDiv(ge(id), false);
    }
  }else
  {
    for(var i=0; i < sender._updatePanelClientIDs.length; i++)
      ShowHideDisableDiv(ge(sender._updatePanelClientIDs[i]), false);
        
    alert(args._error.message);
  }
*/
  backgroundOnmouseover();        
  document.getElementById('footer').style.top = getPageSizeWithScroll()- FooterMoveFromBotton + 'px';
  document.body.style.height = getPageSizeWithScroll() + 'px';
}

function ShowHideDisableDiv(div, show)
{
 try{
  while(div != null)
  {                       
    if((div.nodeName=='DIV')&&(div.id.indexOf('WidgetContainerDiv') != -1))
    {
            break;        
    }
    div = div.parentNode;
  }
  if(div != null)
  {
    DisDiv = null;
    for(i=0; i< div.childNodes.length; i++)
    {
      //DIV
      if((div.childNodes[i].nodeName == 'TABLE')&&(div.childNodes[i].id.indexOf('disableDiv') != -1))
      {
              DisDiv = div.childNodes[i];
              break;
      }        
    }        
    if(DisDiv != null)
    {                        
      if(show){
        DisDiv.style.width = div.offsetWidth + 'px';
        DisDiv.style.height = div.offsetHeight + 'px';
        DisDiv.style.display = '';
        DisDiv.style.visibility = '';
      }else DisDiv.style.display = 'none';
    }
  }
 }catch(err){}
}

function MyValidateAction(validationGroup, obj){  
        //IsValid = Page_ClientValidate(validationGroup);
        var controlForFocus = null;
        if (typeof(Page_Validators) == "undefined")  return true;
        var i;
        for (i = 0; i < Page_Validators.length; i++) {
                val = Page_Validators[i];
                val.isvalid = true;
                if ((typeof(val.enabled) == "undefined" || val.enabled != false) && IsValidationGroupMatch(val, validationGroup)) {
                        if (typeof(val.evaluationfunction) == "function") 
                                val.isvalid = val.evaluationfunction(val);        
                
                }                
        }        
        var arr = new Array();

        //controltocompare      

        for (i = 0; i < Page_Validators.length; i++) {                        
                try{
                        textbox = ge(Page_Validators[i].controltovalidate);
                        compareTextbox = ge(Page_Validators[i].controltocompare);                        
                        if ((textbox != null) && (
                                (typeof(obj) == "undefined")  || (obj == textbox) || (obj == compareTextbox))){
                                val = Page_Validators[i];                                
                                if(!val.isvalid){                                                                                             
                                        textbox.className = val.attributes['ErrorCss'].value;
                                        ValidatorUpdateDisplay(val);
                                        arr[val.controltovalidate] = 'err';
                                        if(controlForFocus == null ) controlForFocus = textbox;
                                }else
                                {
                                       if (typeof( arr[val.controltovalidate]) == "undefined")  textbox.className = "";
                                }
                        }                         
                        
                        if ((compareTextbox != null) && (
                                (typeof(obj) == "undefined")  || (obj == textbox) || (obj == compareTextbox))){
                                val = Page_Validators[i];
                                if(!val.isvalid){                                                     
                                        compareTextbox.className = val.attributes['ErrorCss'].value;
                                        ValidatorUpdateDisplay(val);
                                        arr[val.controltovalidate] = 'err';
                                        if(controlForFocus == null ) controlForFocus = textbox;
                                }else
                                {
                                       if (typeof( arr[val.controltovalidate]) == "undefined")  compareTextbox.className = "";
                                }
                        }       
                }catch(err){        
                        alert(err);
                }       
        }
        if((typeof(obj) == "undefined") &&  controlForFocus != null)  try{ controlForFocus.focus();}  catch(e){}

        return AllValidatorsValid(Page_Validators);
}

function RequiredValidateRCB(rcb)
{
  try{
    if(rcb.ValueHidden.value == '') 
    {
        rcb.InputDomElement.focus();
        rcb.SetText('Select...');
        rcb.InputDomElement.style.backgroundColor = '#ffcccc';
        return false;
    }
    return true;
  }catch (err){}
}

function askSetTextInDDL(ddlID, text, question){
        ddl = ge(ddlID);
        for(i=0; i< ddl.options.length; i++)
        {
                if(ddl.options[i].text == text)
                {
                        item = ddl.options[i];
                        break;
                }
        }
        if(item != null)
          if(question!= null && confirm(question))
            item.selected = true;
}

function calendarOnKeyDown(sender,evt)
{
    var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) :  evt.keyCode;
    if((keyCode == 46) ||(keyCode == 8)){
        sender.value = '';
        return true;
    }else return false;   
}

function InitHint() {
  $('.__jqHintArray').each(function() {
      el = $('#' + $(this).attr('ControlID'));
      el.attr('hint',$(this).html());
      el.tooltip({
          track: true,
          bodyHandler: function() {
              return $(this).attr('hint');
          }
      })
  })
}

