//------------------------------------------------------------------------------
/** @author Бреславский А.В. (Hi-tech Research Group) */
//------------------------------------------------------------------------------
function Debug($Message){
  //----------------------------------------------------------------------------
  try{
    console.debug($Message);
  }catch(e){}
}
//------------------------------------------------------------------------------
function GetURL($URL,$Window){
  //----------------------------------------------------------------------------
  ShowProgress('Загрузка страницы');
  //----------------------------------------------------------------------------
  document.location = $URL;
}
//------------------------------------------------------------------------------
function eStack($Exception){
  //----------------------------------------------------------------------------
  var $Exceptions = [];
  //----------------------------------------------------------------------------
  do{
    //--------------------------------------------------------------------------
    $Exceptions.push($Exception.String);
    //--------------------------------------------------------------------------
    $Exception = $Exception.Parent;
    //--------------------------------------------------------------------------
  }while($Exception);
  //----------------------------------------------------------------------------
  var $Result = document.createElement('SPAN');
  //----------------------------------------------------------------------------
  var $Div = document.createElement('DIV');
  $($Div).attr('className','Exception');
  $($Div).html($Exceptions.shift());
  //----------------------------------------------------------------------------
  $Result.appendChild($Div);
  //----------------------------------------------------------------------------
  if($Exceptions.length > 0){
    //--------------------------------------------------------------------------
    var $Ul = document.createElement('UL');
    $($Ul).attr('className','Standard');
    //--------------------------------------------------------------------------
    for(var $i=0;$i<$Exceptions.length;$i++){
      //------------------------------------------------------------------------
      var $Li = document.createElement('LI');
      $($Li).html($Exceptions[$i]);
      //------------------------------------------------------------------------
      $Ul.appendChild($Li);
    }
    //--------------------------------------------------------------------------
    $Result.appendChild($Ul);
  }
  //----------------------------------------------------------------------------
  return $($Result).html();
}
//------------------------------------------------------------------------------
function jClone(obj){
  //----------------------------------------------------------------------------
  function Clone(){}
  Clone.prototype = obj;
  //----------------------------------------------------------------------------
  var c = new Clone();
  c.constructor = Clone;
  //----------------------------------------------------------------------------
  return c;
}
//------------------------------------------------------------------------------
function SPrintF($String){
  //----------------------------------------------------------------------------
  var $Result = '';
  //----------------------------------------------------------------------------
  for(var $i=0,$j=1;$i<$String.length;$i++){
    //--------------------------------------------------------------------------
    $Char = $String.charAt($i);
    //--------------------------------------------------------------------------
    if($Char == '%'){
      //------------------------------------------------------------------------
      switch($String.charAt($i+1)){
        case 's':
          //--------------------------------------------------------------------
          $Result += String(arguments[$j++]);
          //--------------------------------------------------------------------
          $i++;
        break;
        case 'u':
          //--------------------------------------------------------------------
          $Result += Number(arguments[$j++]);
          //--------------------------------------------------------------------
          $i++;
        break;
        default:
          $Result += $Char;
      }
    }else
      $Result += $Char;
  }
  //----------------------------------------------------------------------------
  return $Result;
}
//------------------------------------------------------------------------------
function UniqID($Prefix){
  //----------------------------------------------------------------------------
  return $Prefix+Math.round(Math.random()*1000000);
}
//------------------------------------------------------------------------------
Array.prototype.IsExists = function($Value){
  //----------------------------------------------------------------------------
  for(var $i in this){
    //--------------------------------------------------------------------------
    if(this[$i] == $Value)
      return true;
  }
  //----------------------------------------------------------------------------
  return false;
}
//------------------------------------------------------------------------------
Date.prototype.ToStringDate = function(){
  //----------------------------------------------------------------------------
  var $MonthNames = ['января','февраля','марта','апреля','мая','июня','июля','августа','сентября','октября','ноября','декабря'];
  //----------------------------------------------------------------------------
  return SPrintF('%s %s %s г.',this.getDate(),$MonthNames[this.getMonth()],this.getFullYear());
};
//------------------------------------------------------------------------------
function GetForm($Form){
  //----------------------------------------------------------------------------
  var $Inners = [];
  //----------------------------------------------------------------------------
  $Childs = $Form.getElementsByTagName('*');
  //----------------------------------------------------------------------------
  for(var $i=0;$i<$Childs.length;$i++){
    //--------------------------------------------------------------------------
    var $Child = $Form[$i];
    //--------------------------------------------------------------------------
    if(typeof($Child) != 'object' || $Child == null || !$Child.nodeName)
      continue;
    //--------------------------------------------------------------------------
    Debug(SPrintF('%s:%s',$Child.nodeName,$Child.name));
    //--------------------------------------------------------------------------
    $Inners.push($Child);
  }
  //----------------------------------------------------------------------------
  var $Result = [];
  //----------------------------------------------------------------------------
  for(var $i=0;$i<$Inners.length;$i++){
    //--------------------------------------------------------------------------
    var $Child = $Inners[$i];
    //--------------------------------------------------------------------------
    $nodeName = $Child.nodeName.toLowerCase();
    //--------------------------------------------------------------------------
    Debug(SPrintF('Обработка элемента формы %s:%s',$nodeName,$Child.name));
    //--------------------------------------------------------------------------
    switch($nodeName){
      case 'input':
        //----------------------------------------------------------------------
        if(['button','submit','file'].IsExists($Child.type))
          break;
        //----------------------------------------------------------------------
        if(['checkbox','radio'].IsExists($Child.type) && !$Child.checked){
          //--------------------------------------------------------------------
          Debug(SPrintF('checkbox, radio [%s] не выбран',$Child.name));
          //--------------------------------------------------------------------
          break;
        }
        //----------------------------------------------------------------------
        $Result.push({Name:$Child.name,Value:$Child.value});
      break;
      case 'textarea':
        $Result.push({Name:$Child.name,Value:$Child.value});
      break;
      case 'select':
        //----------------------------------------------------------------------
        if($Child.multiple){
          //--------------------------------------------------------------------
          for(var $j=0;$j<$Child.options.length;$j++){
            //------------------------------------------------------------------
            $Option = $Child.options[$j];
            //------------------------------------------------------------------
            if($Option.selected)
              $Result.push({Name:$Child.name,Value:$Option.value});
          }
        }else
          $Result.push({Name:$Child.name,Value:$Child.value});
      break;
      default:
        Debug(SPrintF('Тип тега (%s) не поддерживается',$nodeName));
    }
  }
  //----------------------------------------------------------------------------
  return $Result;
}
//------------------------------------------------------------------------------
function MaxZIndex(){
  //----------------------------------------------------------------------------
  $Elements = document.getElementsByTagName('*');
  //----------------------------------------------------------------------------
  $MaxZIndex = -1;
  //----------------------------------------------------------------------------
  for(var $i=0;$Elements.length;$i++){
    //--------------------------------------------------------------------------
    if($Elements[$i] == undefined)
      break;
    //--------------------------------------------------------------------------
    var $Element = $Elements[$i];
    //--------------------------------------------------------------------------
    if($Element.style)
      $MaxZIndex = Math.max($MaxZIndex,$Elements[$i].style.zIndex);
  }
  //----------------------------------------------------------------------------
  return $MaxZIndex;
}
//------------------------------------------------------------------------------
function ShowProgress($Text){
  //----------------------------------------------------------------------------
  $Progress = document.createElement('TABLE');
  $($Progress).attr('id','Progress');
  $($Progress).attr('cellspacing',5);
  $($Progress).attr('cellpadding',0);
  $($Progress).attr('title','Действие');
  //----------------------------------------------------------------------------
  var $html  = '<TR><TD>';
  $html += '<IMG alt="Загрузка" align="left" height="50" width="50" src="/styles/root/Images/Icons/Progress.gif" />';
  $html += SPrintF('</TD><TD>%s<DIV id="ProgressLine"/></TD></TR>',$Text?$Text:'Пожалуйста, подождите');
  //----------------------------------------------------------------------------
  $($Progress).html($html);
  //----------------------------------------------------------------------------
  document.body.appendChild($Progress);
  //----------------------------------------------------------------------------
  $($Progress).dialog({width:$($Progress).width()+30,modal:true,minHeight:50,close:function(){HideProgress();}});
}
//------------------------------------------------------------------------------
function SetProgress($Current,$Total){
  //----------------------------------------------------------------------------
  $('#ProgressLine').progressbar({value:Math.round(($Current/$Total)*100)});
}
//------------------------------------------------------------------------------
function HideProgress(){
  //----------------------------------------------------------------------------
  $('#Progress').dialog('destroy');
  $('#Progress').remove();
}
//------------------------------------------------------------------------------
function ShowAlert($Text,$TypeID){  //----------------------------------------------------------------------------  $Alert = document.createElement('TABLE');
  $($Alert).attr('id','Alert');
  $($Alert).attr('cellspacing',5);
  $($Alert).attr('cellpadding',0);
  $($Alert).attr('title','Предупреждение');
  //----------------------------------------------------------------------------
  var $html  = '<TR><TD>';
  $html += SPrintF('<IMG alt="Загрузка" align="left" height="50" width="50" src="/styles/root/Images/Icons/%s.gif" />',$TypeID);
  $html += SPrintF('</TD><TD>%s</TD></TR>',$Text?$Text:'Пожалуйста, ждите');
  //----------------------------------------------------------------------------
  $($Alert).html($html);
  //----------------------------------------------------------------------------
  document.body.appendChild($Alert);
  //----------------------------------------------------------------------------
  $($Alert).dialog({buttons:{'Продолжить':function(){ $(this).dialog('close'); }},width:Math.min($($Alert).width()+30,300),modal:true,close:function(){HideAlert();}});
}
//------------------------------------------------------------------------------
function HideAlert(){
  //----------------------------------------------------------------------------
  $('#Alert').dialog('destroy');
  $('#Alert').remove();
}
//------------------------------------------------------------------------------
function ShowConfirm($Text,$Callback){
  //----------------------------------------------------------------------------
  $Confirm = document.createElement('TABLE');
  $($Confirm).attr('id','Confirm');
  $($Confirm).attr('cellspacing',5);
  $($Confirm).attr('cellpadding',0);
  $($Confirm).attr('title','Подтверждение');
  //----------------------------------------------------------------------------
  var $html  = '<TR><TD>';
  $html += '<IMG alt="Загрузка" align="left" height="50" width="50" src="/styles/root/Images/Icons/Warning.gif" />';
  $html += SPrintF('</TD><TD>%s</TD></TR>',$Text);
  //----------------------------------------------------------------------------
  $($Confirm).html($html);
  //----------------------------------------------------------------------------
  document.body.appendChild($Confirm);
  //----------------------------------------------------------------------------
  $($Confirm).dialog({buttons:{'Нет':function(){$(this).dialog('close');},'Да':function(){$Callback();$(this).dialog('close');}},width:$($Confirm).width()+30,maxWidth:300,modal:true,close:function(){HideAlert();}});
}
//------------------------------------------------------------------------------
function HideConfirm(){
  //----------------------------------------------------------------------------
  $('#Confirm').dialog('destroy');
  $('#Confirm').remove();
}
//------------------------------------------------------------------------------
function ShowPrompt($Title,$Text,$Callback){
  //----------------------------------------------------------------------------
  $Prompt = document.createElement('TABLE');
  $($Prompt).attr('id','Prompt');
  $($Prompt).attr('cellspacing',5);
  $($Prompt).attr('cellpadding',0);
  $($Prompt).attr('title',$Title);
  //----------------------------------------------------------------------------
  var $UniqID = UniqID();
  //----------------------------------------------------------------------------
  var $html  = '<TR><TD>';
  $html += SPrintF("<TEXTAREA id='%s' rows='5' cols='50'>%s</TEXTAREA>",$UniqID,$Text);
  $html += '</TD></TR>';
  //----------------------------------------------------------------------------
  $($Prompt).html(SPrintF($html,$UniqID));
  //----------------------------------------------------------------------------
  document.body.appendChild($Prompt);
  //----------------------------------------------------------------------------
  $(SPrintF('#%s',$UniqID)).val($Text);
  //----------------------------------------------------------------------------
  $($Prompt).dialog({buttons:{'Продолжить':function(){ $Callback($(SPrintF('#%s',$UniqID)).val()); $(this).dialog('close');}},width:$($Prompt).width()+30,modal:true,minHeight:50,close:function(){HidePrompt();}});
}
//------------------------------------------------------------------------------
function HidePrompt(){
  //----------------------------------------------------------------------------
  $('#Prompt').dialog('destroy');
  $('#Prompt').remove();
}
//------------------------------------------------------------------------------
function ShowTick($Text){
  //----------------------------------------------------------------------------
  $('#Tick').remove();
  //----------------------------------------------------------------------------
  var $Tick = document.createElement('DIV');
  $($Tick).attr('id','Tick');
  $($Tick).html($Text);
  //----------------------------------------------------------------------------
  $($Tick).css({position:'absolute',zIndex:MaxZIndex()+1});
  //----------------------------------------------------------------------------
  document.body.appendChild($Tick);
  //----------------------------------------------------------------------------
  var $Width = Math.min(400,$($Tick).width());
  //----------------------------------------------------------------------------
  $($Tick).css({width:SPrintF('%upx',$Width),top:$(document).scrollTop()+30,left:SPrintF('%upx',($(document).width() - $Width)/2)});
  //----------------------------------------------------------------------------
  var $UniqID = UniqID('ID');
  //----------------------------------------------------------------------------
  window[$UniqID] = window.setInterval(function(){window.clearInterval(window[$UniqID]);$('#Tick').fadeOut('slow');},2000);
}
//------------------------------------------------------------------------------
function Translite($String){
  //----------------------------------------------------------------------------
  var $Templates = "аaбbвvгgдdеeёjoжzhзzиiйjкkлlмmнnоoпpрrсsтtуuфfхkhцcчchшshщshhъ''ыyь'эehюjuяjaАAБBВVГGДDЕEЁJoЖZhЗZИIЙJjКKЛLМMНNОOПPРRСSТTУUФFХKhЦCЧChШShЩShhЪ''ЫYЬ'ЭEhЮJuЯJa";
  //----------------------------------------------------------------------------
  $Templates = $Templates.replace(/([а-яёЁ])([a-z']+)/gi,'.replace(/$1/g,"$2")');
  //----------------------------------------------------------------------------
  return eval("$String"+$Templates);
}