var photomove = 0;
var tmp;
var tmp_name;
var parent_tmp;
var parent_tmp_name;
var avia_chel = 1;
var file_photo = 1;
var interval = new Array();
var ax = ay = 0;
var ox = oy = 0;

function loadHTML(file, dest, param, add) 
{
  $.ajax(
  {
     url: file,
     type: 'POST',
     data: param,
     enctype: 'multipart/form-data',
     beforeSend: function()
     {
       if ( add != "+" && add != "-" && add != "s")
       {
         $("#"+dest).html("<img src='/images/wait.gif'/>")
                    .slideDown(700);;
       }
     },
     success: function(answ)
     {
       if ( add == "+" )
       {
         $("#"+dest).hide()  
                     .append(answ)
                     .slideDown(700);
       }
       else if ( add == "*" )
       {
         $("#"+dest).hide()  
                     .html(answ)
                     .slideDown(1);
       }
       else
       {
         if ( answ != '' )
         {
            $("#"+dest).hide()  
                        .html(answ)
                        .slideDown(700);
         }
         else
         {
            $("#"+dest).slideUp(700)
                       .html(answ)
                       ;
         }
         if ( add == 's' )
         {
            $("#"+dest).slideUp(700);
         }
       }
     }
  }
  );
}


function show( id )
{
  if ( $('#'+id).length )
  {
     var value = document.getElementById(id).innerHTML;
     if ( value != "" && value != "ok" )
     {
      $("#"+id).slideDown(10);
     }
  }
}
function hide( id )
{
   if ( $('#'+id).length )
   {
      $("#"+id).slideUp(10);
   }
}

function show_time( id, slow )
{
  if ( $('#'+id).length )
  {
     var value = document.getElementById(id).innerHTML;
     if ( value != "" && value != "ok" )
     {
      $("#"+id).slideDown(slow);
     }
  }
}
function hide_time( id, slow )
{
   if ( $('#'+id).length )
   {
      $("#"+id).slideUp(slow);
   }
}


function showpic(id, x, y)
{
   if ( !photomove )
   {
   $("#"+id).animate({ width: x, height: y }, 150);
   $("#"+id).attr("class", "hide photo");
   }
   photomove = 1;
}

function hidepic(id, x, y)
{
   $("#"+id).animate({ width: 30, height: 30 }, 150);
   $("#"+id).attr("class", "miniphoto");
   setTimeout('photomove = 0;', 50);
}

function showsub(id, parent_id)
{
   id = "sub"+id;
   parent_id = "sub"+parent_id;
   if ( tmp_name == id || tmp_name == parent_id )
   {
      clearTimeout(tmp);
   }
   if ( document.getElementById(id).innerHTML != '' )
   {
      $("#"+id).show(0);
   }
   
}

function hidesub(id, parent_id)
{
   id = "sub" + id;
   parent_id = "sub" + parent_id;
   
   tmp_name = id;
   parent_tmp_name = parent_id;
   
   tmp = setTimeout('$("#'+id+'").hide(0)', 10);
}

window.onload = function a1() 
{
   if ( $('#abser2').length )
   {
      $("#abser1").attr("style", "left:0px;top:0px;width:"+getAbsClientWidth()+"px;height:"+getAbsClientHeight()+"px;");
      $("#abser2").attr("style", "top:"+((ay - oy)+(getClientHeight()-20)/2)+"px;");
   }
   if ( $('#cont').length )
   {
        Calendar.setup({
              cont         : "cont",
              weekNumbers  : true,
              onSelect     : getnews
        });
   }
   if ( $('#text').length )
   {
        var oFCKeditor = new FCKeditor( 'text' ) ;
        oFCKeditor.BasePath = "/fckeditor/" ;
        oFCKeditor.Height = "150";
        oFCKeditor.ToolbarSet = "Basic";
        oFCKeditor.ReplaceTextarea() ;
   }
}

var photomove = 0;
function showpic(id, x, y)
{
   if ( !photomove )
   {
   $("#"+id).animate({ width: x, height: y }, 150);
   $("#"+id).attr("class", "hide photo");
   }
   photomove = 1;
}
function hidepic(id, x, y)
{
   $("#"+id).animate({ width: 61, height: 61 }, 150);
   $("#"+id).attr("class", "miniphoto");
   setTimeout('photomove = 0;', 50);
}
function validate(textform, length)
{
   text = eval("document.formchange." + textform + ".value")

   if ( text.length > (length-1) )
   {
      text = text.substring(0, (length-1));
      eval("document.formchange." + textform + ".value = '" + text + "'")
   }
}
function checkall(clas)
{
   $("."+clas).attr("checked","checked");
}
function uncheckall(clas)
{
   $("."+clas).attr("checked","");
}
function getkurort( cid, kid )
{
   id = $("#" + cid + " option:selected").val();
   loadHTML("/actions.php", kid, {action: 'getkurort',  id: id}, "*");
}
function get_okurort( cid, kid, mid )
{
   id = $("#" + cid + " option:selected").val();
   loadHTML("/actions.php", kid, {action: 'get_okurort',  id: id}, "*");
   $("#" + mid).html('<select name="hotel" style="width:97%"></select>');
}
function get_ohotel( cid, kid )
{
   id = $("#" + cid + " option:selected").val();
   loadHTML("/actions.php", kid, {action: 'get_ohotel',  id: id}, "*");
}
function get_action_text( id )
{
   if ( $("#full_action_text"+id).html() != "" )
   {
      unget_action_text( id );
   }
   else
   {
      $("#plus"+id).attr("src", "/images/minus.gif");
      $("#short_action_text"+id).hide();
      loadHTML("/actions.php", "full_action_text"+id, {action: 'get_action_text',  id: id}, "");
   }
}
function unget_action_text( id )
{
   $("#plus"+id).attr("src", "/images/plus.gif");
   $("#short_action_text"+id).show();
   $("#full_action_text"+id).html('');
}
function delchel( id )
{
   $("#chel"+id).remove();
}
function addchel()
{
   avia_chel++; 
   var html =  '<div id="chel'+avia_chel+'">'+
               '<table width="600px" cellpadding="0" cellspacing="0" class="tablereg stylish" border="0">'+
               '<tr>'+
             	' <td colspan="2">'+
               '  <a href="#" onclick="delchel('+avia_chel+');return false;"><img src="/admin/images/deleteall.gif" border="0" style="float: right"/></a>'+ 
               ' </td>'+
             	'</tr>'+
             	'<tr>'+
         		'  <td align="left">'+
             	'  <strong>ФИО пассажира:<font color="red">*</font></strong><br/>'+
             	'  <div style="width: 180px;"/>'+
             	'  </td>'+
             	'  <td align="left" width="100%">'+
               '      <input type="text" name="fio[]" value="" style="width: 100%"/><br/>'+
             	'	</td>'+
             	'</tr>'+
             	'<tr>'+
         		'	<td  align="left"  style="width: 100px">'+
         		'	<nobr><strong>Дата рождения пассажира</strong><font color="red">*</font></nobr>'+
             	'	</td>'+
             	'	<td  align="left" style="width: 100%">'+
               '      <input type="text" name="birthday[]" value="" style="width: 100px" id="birthday'+avia_chel+'"/>'+
               '      <input type="button" id="birthdayb'+avia_chel+'" value="..."/>'+
               '      <script>'+
               '          Calendar.setup({'+
               '              trigger    : "birthdayb'+avia_chel+'",'+
               '              inputField : "birthday'+avia_chel+'",'+
               '              dateFormat : "%d.%m.%Y",'+
               '              onSelect   : function() { this.hide() }'+
               '          });'+
               '      </script>'+
             	'	</td>'+
             	'</tr>'+
             	'<tr>'+
         		'	<td align="left">'+
             	'		<strong>№ паспорта РФ пассажира:<br/>'+
               '     <small>(или вид и № документа его заменяющего)</small><font color="red">*</font></strong>'+
             	'	</td>'+
             	'	<td align="left" valign="top">'+
               '       <input type="text" name="passport[]" value="" style="width: 100%"/><br/>'+
             	'	</td>'+
             	'</tr>'+
             	'<tr style="background: #fff">'+
             	' <td colspan="2"></td>'+
             	'</tr>'+
             	'</table>'+
             	'</div>';
   $("#chelov").append( html );
}

function avia_check()
{
   var html = '';
   var sel = $("#vid option:selected").val();
   if ( sel == 1 )
   {
      html = '';
      $("#dopparam").html(html);
   }
   else if ( sel == 2 )
   {
      html = '<tr>'+
      		 '	<td align="left" style="width: 100px">'+
      		 '	<nobr><strong>Дата обратного вылета</strong><font color="red">*</font></nobr><br/>'+
             '  <div style="width: 180px;"/>'+
          	 '	</td>'+
          	 '	<td  align="left" style="width: 100%">'+
             '      <input type="text" name="to" value="" style="width: 100px" id="to"/>'+
             '      <input type="button" id="tob" value="..."/>'+
             '      <script>'+
             '          Calendar.setup({'+
             '              trigger    : "tob",'+
             '              inputField : "to",'+
             '              dateFormat : "%d.%m.%Y",'+
             '              onSelect   : function() { this.hide() }'+
             '          });'+
             '      </script>'+
          	 '	</td>'+
          	 '</tr>'+
          	 '<tr>'+
      		 '	<td align="left">'+
          	 '		<strong>Время обратного вылета:<br/></strong>'+
          	 '	</td>'+
          	 '	<td align="left" valign="top">'+
             '       <input type="text" name="ovilet" value="" style="width: 100%"/><br/>'+
          	 '	</td>'+
          	 '</tr>'+
              '<tr>'+
      		 '	<td align="left">'+
          	 '		<strong>Авиакомпания:<br/></strong>'+
          	 '	</td>'+
          	 '	<td align="left" valign="top">'+
             '       <input type="text" name="oavia" value="" style="width: 100%"/><br/>'+
          	 '	</td>'+
          	 '</tr>';
      $("#dopparam").html(html);
   }
   else if ( sel == 3 )
   {
      html = '<tr>'+
      		 '	<td align="left">'+
          	 '		<strong>Пункт обратного вылета:<font color="red">*</font></strong>'+
          	 '	</td>'+
          	 '	<td align="left" valign="top">'+
             '       <input type="text" name="opunkt" value="" style="width: 100%"/><br/>'+
          	 '	</td>'+
          	 '</tr>'+
             '<tr>'+
      		 '	<td align="left" style="width: 100px">'+
      		 '	<nobr><strong>Дата обратного вылета</strong><font color="red">*</font></nobr><br/>'+
             '  <div style="width: 180px;"/>'+
          	 '	</td>'+
          	 '	<td  align="left" style="width: 100%">'+
             '      <input type="text" name="to" value="" style="width: 100px" id="to"/>'+
             '      <input type="button" id="tob" value="..."/>'+
             '      <script>'+
             '          Calendar.setup({'+
             '              trigger    : "tob",'+
             '              inputField : "to",'+
             '              dateFormat : "%d.%m.%Y",'+
             '              onSelect   : function() { this.hide() }'+
             '          });'+
             '      </script>'+
          	 '	</td>'+
          	 '</tr>'+
          	 '<tr>'+
      		 '	<td align="left">'+
          	 '		<strong>Время обратного вылета:<br/></strong>'+
          	 '	</td>'+
          	 '	<td align="left" valign="top">'+
             '       <input type="text" name="ovilet" value="" style="width: 100%"/><br/>'+
          	 '	</td>'+
          	 '</tr>'+
             '<tr>'+
      		 '	<td align="left">'+
          	 '		<strong>Авиакомпания:<br/></strong>'+
          	 '	</td>'+
          	 '	<td align="left" valign="top">'+
             '       <input type="text" name="oavia" value="" style="width: 100%"/><br/>'+
          	 '	</td>'+
          	 '</tr>'+
             '<tr>'+
      		 '	<td align="left">'+
          	 '		<strong>Пункт назначения при обратном вылете:<font color="red">*</font></strong>'+
          	 '	</td>'+
          	 '	<td align="left" valign="top">'+
             '       <input type="text" name="onpunkt" value="" style="width: 100%"/><br/>'+
          	 '	</td>'+
          	 '</tr>';
          	 
      $("#dopparam").html(html);
   }
}

function gd_check()
{
   var html = '';
   var sel = $("#vid option:selected").val();
   if ( sel == 1 )
   {
      html = '';
      $("#dopparam").html(html);
   }
   else if ( sel == 2 )
   {
      html = '<tr>'+
      		 '	<td align="left" style="width: 100px">'+
      		 '	<nobr><strong>Дата обратного отправления</strong><font color="red">*</font></nobr><br/>'+
             '  <div style="width: 180px;"/>'+
          	 '	</td>'+
          	 '	<td  align="left" style="width: 100%">'+
             '      <input type="text" name="to" value="" style="width: 100px" id="to"/>'+
             '      <input type="button" id="tob" value="..."/>'+
             '      <script>'+
             '          Calendar.setup({'+
             '              trigger    : "tob",'+
             '              inputField : "to",'+
             '              dateFormat : "%d.%m.%Y",'+
             '              onSelect   : function() { this.hide() }'+
             '          });'+
             '      </script>'+
          	 '	</td>'+
          	 '</tr>'+
          	 '<tr>'+
      		 '	<td align="left">'+
          	 '		<strong>Время обратного отправления:<br/></strong>'+
          	 '	</td>'+
          	 '	<td align="left" valign="top">'+
             '       <input type="text" name="ovilet" value="" style="width: 100%"/><br/>'+
          	 '	</td>'+
          	 '</tr>'+
              '<tr>'+
      		 '	<td align="left">'+
          	 '		<strong>№ поезда:<br/></strong>'+
          	 '	</td>'+
          	 '	<td align="left" valign="top">'+
             '       <input type="text" name="oavia" value="" style="width: 100%"/><br/>'+
          	 '	</td>'+
          	 '</tr>';
      $("#dopparam").html(html);
   }
}
////////звёздное голосование
function set_stars(kolvo, id, rate)
{
   clearTimeout(interval[id]);
   
   var text = '';
   for ( var i = 1; i <= 5; i++ )
   {
      if ( i <= kolvo )
      {
         text += '<a href="#" onclick="get_ovoice(\'' + id + '\', ' + i + '); return false;" onmousemove="set_stars(' + i + ', \'' + id + '\', ' + rate + ')" ><img src="/images/star.gif" border="0"/></a>';
      }
      else
      {
         text += '<a href="#" onclick="get_ovoice(\'' + id + '\', ' + i + '); return false;" onmousemove="set_stars(' + i + ', \'' + id + '\', ' + rate + ')" ><img src="/images/empty_star.gif" border="0"/></a>';
      }
   }
   $("#"+id).html(text);
   interval[id] = setTimeout("return_stars(" + rate + ", '" + id + "')", 2000);
}
function set_passive_stars(kolvo, id, letter)
{
  clearTimeout(interval[id]);
  
  document.getElementById(id).onmouseout = function fun(){}; 
   var text = '';
   for ( var i = 1; i <= 5; i++ )
   {
      if ( i <= kolvo )
      {
         text += '<a href="#" onclick="get_ovoice(\'' + id + '\', ' + i + '); return false;"><img src="/images/star.gif" border="0"/></a>';
      }
      else
      {
         text += '<a href="#" onclick="get_ovoice(\'' + id + '\', ' + i + '); return false;"><img src="/images/empty_star.gif" border="0"/></a>';
      }
   }
   $("#"+id).html(text);
}
function return_stars(rate, id)
{
   var text = '';
   for ( var i = 1; i <= 5; i++ )
   {
      if ( i <= rate )
      {
         text += '<a href="#" onclick="get_ovoice(\'' + id + '\', ' + i + '); return false;" onmousemove="set_stars(' + i + ', \'' + id + '\', ' + rate + ')" ><img src="/images/star.gif" border="0"/></a>';
      }
      else
      {
         if ( (rate - i) > -0.5 )
         {
            text += '<a href="#" onclick="get_ovoice(\'' + id + '\', ' + i + '); return false;" onmousemove="set_stars(' + i + ', \'' + id + '\', ' + rate + ')" ><img src="/images/star.gif" border="0"/></a>';
         }   
         else if ( (rate - i) > -1 )
         {
            text += '<a href="#" onclick="get_ovoice(\'' + id + '\', ' + i + '); return false;" onmousemove="set_stars(' + i + ', \'' + id + '\', ' + rate + ')" ><img src="/images/poluempty_star.gif" border="0"/></a>';
         }
         else 
         {
            text += '<a href="#" onclick="get_ovoice(\'' + id + '\', ' + i + '); return false;" onmousemove="set_stars(' + i + ', \'' + id + '\', ' + rate + ')" ><img src="/images/empty_star.gif" border="0"/></a>';
         }
      }
   }
   $("#"+id).html(text);
}
function get_ovoice(id, value)
{
   set_passive_stars(value, id);
   $("#"+id+"star").val(value);
}
////////звёздное голосование

function validate(textform, maxlength)
{
    var text = $("#"+textform).val();
    if ( text.length > maxlength )
    {
       text = text.substring(0, maxlength);
       $("#"+textform).val(text);
    }
    document.getElementById(textform+'_v').innerHTML = (maxlength - text.length) + " зн.";
}
function add_file()
{
   if ( file_photo < 10 )
   {
      html = '<input type="file" name="file[]" style="width: 100%"/><br/>';
      $("#files").append(html);
      file_photo++;
   }
   else
   {
      alert("Можно добавить не более 10 фотографий");
   }
}

function delete_photo( id )
{
   loadHTML("/actions.php", "file"+id, {action: 'delete_photo',  id: id}, "");
}
function show_big( src, x, y )
{
   html = '<div class="abserror" id="abser1" onclick="hide_time(\'abser2\', 200);hide_time(\'abser1\', 200);"></div>'+
          '    <center>'+
          '     <div class="abseerorramka" id="abser2" onclick="hide_time(\'abser2\', 200);hide_time(\'abser1\', 200);">'+
          '     </div>'+
          '     </center>';
   $('#photka').html(html);
   $("#abser2").html('<img src="/'+src+'" onclick="hide_time(\'abser2\', 200);hide_time(\'abser1\', 200);"/>');
   show_time('abser1', 200);
   show_time('abser2', 200);
   $("#abser1").attr("style", "left:0px;top:0px;width:"+getAbsClientWidth()+"px;height:"+getAbsClientHeight()+"px;");
   $("#abser2").attr("style", "top:"+((ay - oy)+(getClientHeight()-y)/2)+"px;");
}

document.onmousemove = mouseMove

function mouseMove(event){ 
    event = fixEvent(event)
    ax = event.pageX
    ay = event.pageY
    ox = event.x
    oy = event.y

    if ( event.clientX != null ) 
    {
       ox = event.clientX
       oy = event.clientY
    }
}

function fixEvent(e) {
    // получить объект событие для IE
    e = e || window.event
 
    // добавить pageX/pageY для IE
    if ( e.pageX == null && e.clientX != null ) {
        var html = document.documentElement
        var body = document.body
        e.pageX = e.clientX + (html && html.scrollLeft || body && body.scrollLeft || 0) - (html.clientLeft || 0)
        e.pageY = e.clientY + (html && html.scrollTop || body && body.scrollTop || 0) - (html.clientTop || 0)
    }
    else
    {
      
    }

    // добавить which для IE
    if (!e.which && e.button) {
        e.which = e.button & 1 ? 1 : ( e.button & 2 ? 3 : ( e.button & 4 ? 2 : 0 ) )
    }
 
    return e
}
    
function getAbsClientWidth()
{
  return  document.body.scrollWidth > document.body.offsetWidth ?	document.body.scrollWidth : document.body.offsetWidth;
}
function getAbsClientHeight()
{
  return document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight;
}
function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}
