function LoadScript( url ){
 document.write( '<script type="text/javascript" src="' + url + '" onerror="alert(\'Error loading \' + this.src);"><\/script>' ) ;
}

function LoadCss( url ){
 document.write( '<link href="' + url + '" type="text/css" rel="stylesheet" onerror="alert(\'Error loading \' + this.src);" />' ) ;
}

function SetValue(Key, Value, Type) {
  var Request = null;
  if( window.XMLHttpRequest )
    Request = new XMLHttpRequest();
  else if( window.ActiveXObject )
    Request = new ActiveXObject('MsXml2.XmlHttp');
  if( Request ) {
    Request.open('get', '/setvalue.html?Type='+Type+'&Key='+Key+'&Value='+Value+'&Active='+Math.random(), true);
    Request.send(null);
  }
}

function OpenPhoto( HRef, width, height ) {

 win = window.open(HRef,'_blank','statusbar=0,toolbar=0,location=0,scrollbars=1,resizable=0,width='+width+',height='+height);
 //win.document.body.style.margin = "0";
 //win.document.body.style.padding = "0";
}

//функция форматирования числа, по разрядам и с заданной точностью
function number_format(number, decimal, dec_point, th_sep){

	number = Math.round(number * Math.pow(10, decimal)) / Math.pow(10, decimal);
	str_number = number + '';
	arr_int = str_number.split('.');

	if(!arr_int[0]) arr_int[0] = '0';
	if(!arr_int[1]) arr_int[1] = '';
	if(arr_int[1].length < decimal){
		nachkomma = arr_int[1];

		for(i = arr_int[1].length + 1; i <= decimal; i++){
			nachkomma += '0';
		}
	  arr_int[1] = nachkomma;
	}

	if(th_sep != '' && arr_int[0].length > 3){

		Begriff = arr_int[0];
		arr_int[0] = '';

		for(j = 3; j < Begriff.length ; j += 3){

			Extrakt = Begriff.slice(Begriff.length - j, Begriff.length - j + 3);
			arr_int[0] = th_sep + Extrakt +  arr_int[0] + '';
	  }

		str_first = Begriff.substr(0, (Begriff.length % 3 == 0) ? 3 : (Begriff.length % 3));
	  arr_int[0] = str_first + arr_int[0];
	}

	return arr_int[0] + dec_point + arr_int[1];
}

// Функция формирования окончания на основании колличества и слова
function ending(Numeric, gender) {

  if(Object.isUndefined(gender))
    gender = "f";

  return GetStringEndingNumeric(Numeric, gender);
}

function GetStringEndingNumeric(Numeric, gender){

  if(Object.isUndefined(gender))
    gender = "f";

  Numeric = parseInt(Numeric);

  var Ending = {
    "k": {
      1: "а",
      2: "ы",
      3: ""
    },
    "m": {
      1: "ь",
      2: "я",
      3: "ей"
    },
    "f": {
      1: "ь",
      2: "и",
      3: "ей"
    },
    "a": {
      1: "е",
      2: "я",
      3: "й"
    },
    "b": {
      1: "",
      2: "а",
      3: "ов"
    },
    "g": {
      1: "",
      2: "о",
      3: "о"
    },
    "x": {
      1: "ий",
      2: "ия",
      3: "ев"
    },
    "day": {
      1: "день",
      2: "дня",
      3: "дней"
    },
    "month": {
      1: "месяц",
      2: "месяца",
      3: "месяцев"
    }
  };

  // для окончания числовых выражений
  // мужской (н.п. рубл[ь|я|ей]) | женский (н.п. запчаст[ь|и|ей]) род
  // и средний (н.п. объявлени[й|е|я])
  var str = "";
  switch(Numeric) {
   case 11:
   case 12:
   case 13:
   case 14: str = Ending[gender][3]; break;
  }

  if(!str) {
    var LastTen = Numeric % 10;
    switch(LastTen) {
      case  1: str = Ending[gender][1]; break;
      case  2:
      case  3:
      case  4: str = Ending[gender][2]; break;
      case  5:
      case  6:
      case  7:
      case  8:
      case  9:
      case  0: str = Ending[gender][3]; break;
    }
  }

  return str;
}

// считает кол-во переменных Объекта
// глючит - надо разобраться
function SetCount(oObj) {

  oObj.Count = 0;

  for(var i in oObj ) {

    if( typeof oObj[i] == "function" )
      continue;

    oObj.Count++;
  }
}

function array_keys(aSource) {

  var aTarget = new Array();

  for( var i in aSource )
    aTarget.push(i);

  return aTarget;
}

function print_r(Obj, Level) {

  var Text = "";

  if( typeof Level == "undefined" )
    Level = 0;

  if( typeof Obj == "number" )
    Text += Obj;

  else if( typeof Obj == "string" )
    Text += "\"" + Obj + "\"";

  else if( typeof Obj == "boolean" )
    Text += Obj ? "true" : "false";

  else if( typeof Obj == "array"
           || typeof Obj == "object" ) {

    Text += "{\n";

    for(var i = 0; i < Level; i++)
      Text += "  ";

    var n = 0;

    for(var Key in Obj) {

      if( n )
        Text += ",\n";

      for(var i = 0; i < Level; i++)
        Text += "  ";

      Text += Key + ":" + print_r(Obj[Key], Level + 1);
      n++;
    }

    Text += "\n";

    for(var i = 0; i < Level - 1; i++)
      Text += "  ";

    Text += "}";
  }
  else
   Text += typeof Obj;

  if( !Level )
    alert(Text);
  else
    return Text;
}

function Add2Favor(url, title) {

  /*if( navigator.appName == "Microsoft Internet Explorer"
      && parseFloat(navigator.appVersion) >= 4.0 )
    window.external.AddFavorite(location.href, document.title);
  else
    window.alert("Ваш браузер не поддерживает данную функцию. Нажмите Ctrl+D, чтобы добавить в Favorites.");*/

  if(!url)
    url = location.href;

  if(!title)
    title = document.title;

  //Gecko
  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function"))
     window.sidebar.addPanel(title, url, "");
  //IE4+
  else if (typeof window.external == "object")
    window.external.AddFavorite(url, title);
  //Opera7+
  else if (window.opera && document.createElement){
    var a = document.createElement('A');

    if(!a)
      return false; //IF Opera 6

    a.setAttribute('rel','sidebar');
    a.setAttribute('href',url);
    a.setAttribute('title',title);
    a.click();
  }
  else
    return false;

  return true;
}

function SetAsStartPage(HomePage){

  if(Object.isUndefined(HomePage))
    HomePage = location.href;

  // IE
  if(document.all&&!window.opera){

    var a = document.createElement('A');

    a.style.behavior = 'url(#default#homepage)';
    a.setHomePage(HomePage);
    return true;
  }
  else{
    // FF
    if(window.netscape&&window.netscape.security){
      netscape.security.PrivilegeManager.enablePrivilege('UniversalPreferencesRead');
      if(navigator.preference('browser.startup.homepage')!=HomePage){
        netscape.security.PrivilegeManager.enablePrivilege('UniversalPreferencesWrite');
        navigator.preference('browser.startup.homepage',HomePage);
        return true;
      }
    }
  }

  alert("Данная функция не поддерживается Вашим браузером!");
  return false;
}

function OpenPhoto( HRef, width, height ) {

 win = window.open(HRef,'_blank','statusbar=0,toolbar=0,location=0,scrollbars=0,resizable=0,width='+width+',height='+height);
 //win.document.body.style.margin = "0";
 //win.document.body.style.padding = "0";
}

function ShowSorry() {

 window.alert("Извините, но пока этот раздел не доступен...");
 return false;

}

var aMonths = new Array( 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь')
var aMonthsG = new Array( 'Января', 'Февраля', 'Марта', 'Апреля', 'Мая', 'Июня', 'Июля', 'Августа', 'Сентября', 'Октября', 'Ноября', 'Декабря')
var aDays = new Array('вс', 'пн', 'вт', 'ср', 'чт', 'пт', 'сб');

function FormatDate(dDate, sFormat) {

  if( !dDate )
    return "";

  var Str = "";

  if( sFormat == "dd.mm" )
    Str = FormatNumber(dDate.getDate()) + "." + FormatNumber((dDate.getMonth()+1));
  else if( sFormat == "ddd, d mmm" )
    Str = aDays[dDate.getDay()] + ", " + dDate.getDate() + " " + aMonths[dDate.getMonth()].substr(0, 3).toLowerCase();
  else if( sFormat == "ddmmyyyy" )
    Str = FormatNumber(dDate.getDate()) + FormatNumber((dDate.getMonth()+1)) + FormatNumber(dDate.getFullYear());
  else if( sFormat == "yyyy-mm-dd" )
    Str = FormatNumber(dDate.getFullYear()) + "-" + FormatNumber((dDate.getMonth()+1)) + "-" + FormatNumber(dDate.getDate());
  else
    Str = FormatNumber(dDate.getDate()) + "." + FormatNumber((dDate.getMonth()+1)) + "." + FormatNumber(dDate.getFullYear());

  return Str;
}

function FormatNumber(sNum) {

  re = /^(\d{0,1})(\d{1})$/
  aResult = re.exec(sNum)

  if( aResult
      && !aResult[1] )
    return "0" + parseInt(aResult[2]);

  return sNum;
}

function ChangeDateSelect(sName, iISO) {

  var oHidden = ElementByID(sName);
  var oDay = ElementByID(sName + "Day");
  var oMonth = ElementByID(sName + "Month");
  var oYear = ElementByID(sName + "Year");

  var dDate = new Date(oYear.value, oMonth.value-1, oDay.value);

  if( dDate.getDate() == parseInt(oDay.value*1)
      && dDate.getMonth()+1 == parseInt(oMonth.value*1)
      && dDate.getFullYear() == parseInt(oYear.value*1) ) {

    oHidden.value = FormatDate(dDate, (iISO)?"yyyy-mm-dd":"");

    //if( oHidden.onchange )
    //  SetValue(oHidden.name, oHidden.value, "string");

    window.status = "";
  }
  else {
    oHidden.value = "";
    window.status = "Внимание: выбрана неверная дата!";
    return;
  }

}

function ElementByID(ID) {

  return document.all ? document.all[ID] : document.getElementById(ID);
}

var galleryOptions = {};

function PrepareGallery(Mode){

	hs.graphicsDir = '/js/highslide/graphics/';
	hs.align = 'center';
	hs.transitions = ['expand', 'crossfade'];
	hs.outlineType = 'rounded-white';
	hs.fadeInOut = true;
	hs.numberOfImagesToPreload = 0;
	//hs.dimmingOpacity = 0.75;


  if(Mode && typeof Mode != 'undefined'
     && Mode == "text"){

    return;
  }

	// Add the controlbar
	if (hs.addSlideshow) hs.addSlideshow({
		slideshowGroup: 'group1',
		interval: 5000,
		repeat: false,
		useControls: true,
		fixedControls: 'fit',
		overlayOptions: {
			opacity: .75,
			position: 'bottom center',
			hideOnMouseOut: true
		}
	});
}

function AddGallery(Id){

  if(!Id)
    return;

  // Создаем группу настроек для новой галереи-группы.
  galleryOptions[Id] = {
  	slideshowGroup: 'gallery'+Id,
  	align: 'center',
  	transitions: ['expand', 'crossfade']
  };

  // Регистрируем новую группу как галерею
  if (hs.addSlideshow) hs.addSlideshow({
      slideshowGroup: 'gallery'+Id,
      interval: 5000,
      repeat: false,
      useControls: true,
  	  fixedControls: 'fit',
      overlayOptions: {
          opacity: .6,
          position: 'bottom center',
          hideOnMouseOut: true
  	}
  });
}

// Функция аккордеона
var slideMenu=function() {
  var sp,st,t,m,sa,l,w,sw,ot;
  return {
    build:function(sm,sw,mt,s,sl,h) {
      sp=s;
      st=sw;
      t=mt;

      m=document.getElementById(sm);
      sa=m.getElementsByTagName('li');

      l=sa.length;
      w=m.offsetWidth;
      sw=w/l;

      ot=Math.floor((w-st)/(l-1)); var i=0;

      for(i;i<l;i++) {
        s=sa[i]; s.style.width=sw+'px'; this.timer(s)
      }

      if(sl!=null) {
        m.timer=setInterval(function() {
          slideMenu.slide(sa[sl-1])
        },t)
      }
    },

    timer:function(s) {
      s.onmouseover=function() {
        clearInterval(m.timer);

        m.timer=setInterval(function(){
          slideMenu.slide(s)
        },t)
      }
    },

    slide:function(s){
      var cw=parseInt(s.style.width,'10');
      if(cw<st) {
        var owt=0; var i=0;
        for(i;i<l;i++){
          if(sa[i]!=s){
            var o,ow; var oi=0; o=sa[i]; ow=parseInt(o.style.width,'10');
            if(ow>ot) {
              oi=Math.floor((ow-ot)/sp); oi=(oi>0)?oi:1; o.style.width=(ow-oi)+'px'
            }
            owt=owt+(ow-oi)
          }
        }
        s.style.width=(w-owt)+'px';
      }

      else {
        clearInterval(m.timer)
      }
    }
  };
}();

function base64_decode (data) {
    // Decodes string using MIME base64 algorithm
    //
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/base64_decode
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Thunder.m
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   bugfixed by: Pellentesque Malesuada
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: utf8_decode
    // *     example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
    // *     returns 1: 'Kevin van Zonneveld'
    // mozilla has this native
    // - but breaks in 2.0.0.12!
    //if (typeof this.window['btoa'] == 'function') {
    //    return btoa(data);
    //}

    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, dec = "", tmp_arr = [];

    if (!data) {
        return data;
    }

    data += '';

    do {  // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));

        bits = h1<<18 | h2<<12 | h3<<6 | h4;

        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;

        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);

    dec = tmp_arr.join('');
    dec = this.utf8_decode(dec);

    return dec;
}

function utf8_decode ( str_data ) {
    // Converts a UTF-8 encoded string to ISO-8859-1
    //
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/utf8_decode
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Norman "zEh" Fuchs
    // +   bugfixed by: hitwork
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: utf8_decode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
    var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0;

    str_data += '';

    while ( i < str_data.length ) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }

    return tmp_arr.join('');
}
