function zoom(file){
  var p=window.open('zoom.plx?file='+file,'preview_window',"width=10,height=10,top=0,left=0,location=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,fullscreen=0");
  if(p)p.focus();
  return false;
}

function zoom_image(file, id, type){
  if( !type )
  {
    type ='';
  }
  var p=window.open('zoom.plx?document_image_id=' + id + '&amp;type=' +  type + '&amp;file='+file,'preview_window',"width=10,height=10,top=0,left=0,location=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,fullscreen=0");
  if(p)p.focus();
  return false;
}

function ua(){
  var n=window.navigator,an=n.appName.toLowerCase(),ag=n.userAgent.toLowerCase(),k=0;
  uWin=(ag.indexOf("win")>=0)
  uOpera=uNav=uIE=0;
  if((k=ag.indexOf("opera"))>=0) uOpera=parseFloat(ag.substring(k+6));
    else if((k=ag.indexOf("msie"))>=0) uIE=parseFloat(ag.substring(k+5));
      else if(an.indexOf("netscape")>=0) uNav=parseFloat(n.appVersion);
  uNav4=(uNav>=4.08 && uNav<5 && uWin);
  uMoz=(uNav>=5 && uWin);
  uIE4=(uIE>=4 && uWin);
}

ua()


function getPos(obj){
  if(!obj)
    return 0;
  if(uNav4)
    return [obj.x,obj.y];
  else if(uIE4||uMoz||(uOpera>=5)){
    for(var x=0,y=0;obj;obj=obj.offsetParent){
      x+=obj.offsetLeft;
      y+=obj.offsetTop;
    }
    return [x,y];
  } else return 0;
}

function resizeWnd(x,y){
  var pos=getPos(document.images?(document.images.LP):0);
  if(!pos||!pos[0])
    pos=[x||600,y||400];
  window.resizeTo(pos[0]+(uNav4?10:35),pos[1]+(uNav4?20:55));
  window.focus();
}

/*
function resizeWnd(x,y){
  var pos=getPos(document.images?(document.images.LP):0);

  var lastElement = document.getElementById( 'lastElement' );

  var posLast = getPos( lastElement );

  if(!pos||!pos[0])
    pos=[x||600,y||400];
  //alert(pos[0]+(uNav4?10:35) + ', ' + pos[1]+(uNav4?20:55));
  //window.resizeTo(pos[0]+(uNav4?10:35),pos[1]+(uNav4?20:55));

  if( posLast && posLast[1] )
  {
    y = posLast[1] + 35;
  }

  if( uMoz ) {
    y += 20;
  }

  window.resizeTo(x,y);
  window.focus();
}

*/

function changeImage( id )
{
  var imgs = document.getElementById( id);
  if( /plus/.test(imgs.src) )
  {
    imgs.src = '/i/minus.gif';
    imgs.alt = 'Скрыть параметры';
  }else
  {
    imgs.src = '/i/plus.gif';
    imgs.alt = 'Показать параметры';
  }
}

function toggle (id) {
  if (document.getElementById) {
    var obj = document.getElementById(id);
    if (obj) {
      if (obj.style.display == '') {
        obj.style.display = 'none';
        return false;
      } else {
        obj.style.display = '';
        return true;
      }
    }
  }
}

function checkEmail(str)
{
  if (/^([\w-~_]+\.)*[\w-~_]+@([\w-_]+\.){1,3}\w{2,4}$/.test(str))
  {
    return true;
  }
  else 
  {
    alert("Неправильный e-mail");
    return false;
  }
}

function checkNumber(obj, text)
{
  if( !text ){
    text = 'Неправильное число';
  }

  var str = obj.value;

  if (/^\s*([\d\,\.])*\s*$/.test(str) )
  {
    return true;
  }
  else 
  {
    alert(text);
    obj.focus();
    return false;
  }
}

function checkSelect(obj, text)
{ 
  var str = obj.value;

  if(str == '0') 
  { 
    alert(text);
    obj.focus();

    return false;
  }

  return true;
}


function checkVAL(obj)
{
  var str = obj.value;

  if(str == '') 
  { 
    alert('Не все обязательные поля заполнены');
    obj.focus();

    return false;
  }

  return true;
}

function checkPhone( obj )
{
  var str = obj.value;

  var count_number = 0;

  for( var i = 0; i < str.length; i++ )
  {
    var code = str.charCodeAt(i);
    if( code >= 48 && code <= 57 ) {// это цифра
      count_number++;
    }

  }

  if ( count_number < 7 )
  { 
    alert('Некорректно указан номер телефона');
    obj.focus();

    return false;
  }

  return true;
}

function setForm( formId )
{
  var form = document.getElementById(formId);
  if( form )
  {
    if( form.noForm.value == 1 )
    {
      form.noForm.value = 0;
    }else
    {
      form.noForm.value = 1;
    }
  }
}

function new_window(url)
{
  var p=window.open(url,'external_window','');
  if (p) p.focus();
  return false;
}


function checkCheckboxArray( arr )
{
  for( var i = 0; i < arr.length; i++ )
  {
    var inputs = document.getElementById(arr[i]);
    inputs.checked = true;
  }
}

function uncheckCheckboxArray( arr )
{
  for( var i = 0; i < arr.length; i++ )
  {
    var inputs = document.getElementById(arr[i]);
    inputs.checked = false;
  }
}

function advancedSearchSetCheckbox()
{
  var all = document.getElementById('all'); 
  if( all.checked )
  {
    checkCheckboxArray( new Array('archive', 'news', 'creator', 'interior', 'land', 'exh', 'style', 'metr', 'glossary') );
  }
}

function advancedSearchSetCheckAll()
{
  var all = document.getElementById('all'); 
  if( all.checked )
  {
    checkCheckboxArray( new Array('archive', 'news', 'creator', 'interior', 'land', 'exh', 'style', 'metr', 'glossary') );
  }
}

function advancedSearchUnSetCheckAll()
{
  var all = document.getElementById('all'); 
  all.checked  = false;
}


// Загрузить информацию из скрипта
function getFromScript(url)
{
  var p;
  try { p = new XMLHttpRequest();} catch (e) { p = new ActiveXObject("Msxml2.XMLHTTP");}
  p.open("GET", url,false);
  p.send(null);

  var ret = p.responseText;
  var re = new RegExp("\n", "ig");
  ret = ret.replace(re, "");
  
  re = new RegExp("<a>(.*)</a>");

  var arr = re.exec(ret); 

  return arr[1];
}

// Подгрузить города для выбранной страны
function updateCitySend( country )
{
  var country_id = country.options[country.selectedIndex].value;

  var sdate = new Date();

  var q = 'SELECT city_id, name FROM interior_send_city WHERE country_id=? ORDER BY ordering';

  var ret = getFromScript("/upload.plx?query="+q+"&param="+country_id+"&time="+sdate.getTime());
  var obj = document.getElementById('city');
  if(obj){
    if( country.options[country.selectedIndex].value == -1 )
    {
      obj.innerHTML = "<select style='width:370px;' name='city_id' onChange='showOther( this, \"other_city\");' id='city_select'><option value=0>-Выберите город-</option>"+ret+'<option value="-1" select="1">Другой...</option></select>';
    }else
    {
      obj.innerHTML = "<select style='width:370px;' name='city_id' onChange='showOther( this, \"other_city\");' id='city_select'><option value=0>-Выберите город-</option>"+ret+'</select>';
    }
//    obj.innerHTML = '<option value=0>-Выберите город-</option>'+ret;
  }
}

function state_block(id, state) 
  {    
    if (document.getElementById) 
  {
      var obj = document.getElementById(id);
      if (obj) 
    {
        if (state == 'hidden') 
    {
          obj.style.display = 'none';
          return false;
        } 
    else 
    {
          obj.style.display = '';
          return true;
        }
      }
    }
  }

function showOther(obj, div_name)
{
  if (obj.options[obj.selectedIndex].value != -1) {   
    state_block(div_name, 'hidden'); }
  else { 
   state_block(div_name, 'visible');}
}

function checkCountry(obj, text)
{ 
  var str = obj.value;

  var form = document.getElementById('form_id');

  if( str == '-1' && form.other_country.value == '')
  {
    alert(text);
    form.other_country.focus();

    return false;
  }

  if(str == '0') 
  { 
    alert(text);
    obj.focus();

    return false;
  }

  return true;
}

function checkCity(obj, text)
{ 
  var str = obj.value;

  var form = document.getElementById('form_id');

  if( str == '-1' && form.other_city.value == '')
  {
    alert(text);
    form.other_city.focus();

    return false;
  }

  if(str == '0') 
  { 
    alert(text);
    obj.focus();

    return false;
  }

  return true;
}

function checkType(obj, text)
{ 
  var str = obj.value;

  var form = document.getElementById('form_id');

  if( str == '-1' && form.other_type.value == '')
  {
    alert(text);
    form.other_type.focus();

    return false;
  }

  if(str == '0') 
  { 
    alert(text);
    obj.focus();

    return false;
  }

  return true;
}

function window_open ( url ) {
  window.open(url);
}

function getValue(id)
{
 var obj = document.getElementById(id);
 return (obj.options[obj.selectedIndex].value);	   
}
function win_popup(name,w,h)
{
  var p=window.open(name,'pop_win',"width="+w+",height="+h+",top=0,left=0,location=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,fullscreen=0");
  if(p)p.focus();
  return false;
}
