<?

if ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) {
    define( 'ROOT_DIR', $_SERVER['DOCUMENT_ROOT'] );
    session_start();
    header( 'Content-type: text/html; charset=windows-1251' );
    include_once ROOT_DIR . '/engine/config.php';
    include_once ROOT_DIR . '/engine/function.php';
} else {
    if ( !defined( 'MISOFT' ) ) { exit(); }
}

if ( !isset( $_SESSION['iduser'] ) ) {
    echo '<div style="margin-left: -10px;" class="alert alert-info alert-dismissible fade in" role="alert">
			<button type="button" class="close" data-dismiss="alert" aria-label="Close">
				<span aria-hidden="true">&times;</span>
			</button>
			<h5 class="alert-heading"><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> Внимание!</h5>
			<p style="font-size: 14px;">Вы не авторизованы. Доступ к этой странице разрешен только авторизованным пользователям. <br>Пожалуйста, воспользуйтесь формой авторизации.</p>
			<p style="font-size: 14px;" class="m-b-0">Для перехода в форму авторизации нажмите <a class="a_link" href="/login" >назад</a>.</p>
		  </div>';
} else {
    
	$proverka = mysql_query( "SELECT * FROM autodynlink WHERE iduser = '" . $_SESSION['iduser'] . "' and id = '" . ( int ) $_GET['id'] . "'" );
    $pr_row = mysql_fetch_array( $proverka );
    
    if ( mysql_num_rows( $proverka ) == 0 ) {
        echo '<div style="margin-left: -10px;" class="alert alert-info alert-dismissible fade in" role="alert">
				<button type="button" class="close" data-dismiss="alert" aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
				<h5 class="alert-heading">Ошибка!</h5>
				<p style="font-size: 14px;">Рекламная площадка не существует или удалена. </p>
				<p style="font-size: 14px;" class="m-b-0">Для возврата на предыдущую страницу нажмите <a class="a_link" href="/mdyn" >назад</a>.</p>
			  </div>';
    } else
    if ( $pr_row['paused'] == 1 ) {
        echo '<div style="margin-left: -10px;" class="alert alert-info alert-dismissible fade in" role="alert">
				<button type="button" class="close" data-dismiss="alert" aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
				<h5 class="alert-heading">Внимание!</h5>
				<p style="font-size: 14px;">Перед редактированием необходимо приостановить рекламную площадку, а затем уже приступать к редактированию.</p>
				<p style="font-size: 14px;" class="m-b-0">Для возврата на предыдущую страницу нажмите <a class="a_link" href="/mdyn" >назад</a>.</p>
			  </div>';
    } else {
        switch ( $pr_row['timer'] ) {
            case 0: $timer = 'autodyn10'; break;
            case 1: $timer = 'autodyn20'; break;
            case 2: $timer = 'autodyn30'; break;
            case 3: $timer = 'autodyn40'; break;
            case 4: $timer = 'autodyn50'; break;
            case 5: $timer = 'autodyn60'; break;
        }
        ?>
        <script type="text/javascript">
          $(document).ready(function(){
            $('.help_input').tipTip({defaultPosition:'right'});
            
            $('select, input').styler({width:'290', 'maxHeight':160});
			
			$('#geo_ru').styler();
			$('#geo_ua').styler();
			$('#geo_by').styler();
			$('#geo_kz').styler();
			$('#geo_ge').styler();
			$('#geo_md').styler();
			$('#geo_am').styler();
			$('#geo_tj').styler();
			$('#geo_az').styler();
			$('#geo_uz').styler();
			$('#geo_pt').styler();
			$('#geo_es').styler();
			$('#geo_ee').styler();
			$('#geo_lv').styler();
			$('#geo_lt').styler();
			$('#geo_us').styler();
			$('#geo_gb').styler();
			$('#geo_fr').styler();
			$('#geo_de').styler();
			$('#geo_cn').styler();
			$('#geo_all').styler();
			$('#geo_it').styler();
          });
        
          function PlanChange() {
            var lprice = 0;
            if ($('#timer').val() == 0) { lprice = <?=$config['autodyn10']?>; } else
            if ($('#timer').val() == 1) { lprice = <?=$config['autodyn20']?>; } else
            if ($('#timer').val() == 2) { lprice = <?=$config['autodyn30']?>; } else
            if ($('#timer').val() == 3) { lprice = <?=$config['autodyn40']?>; } else
            if ($('#timer').val() == 4) { lprice = <?=$config['autodyn50']?>; } else
            if ($('#timer').val() == 5) { lprice = <?=$config['autodyn60']?>; }
            
            $('#summa').html(number_format(lprice, 4, '.', ''));
          }
        
          function number_format(number, decimals, dec_point, thousands_sep) {
            var i, j, kw, kd, km;
            if (isNaN(decimals = Math.abs(decimals))) { decimals = 2; }
            if (dec_point == undefined) { dec_point = ","; }
            if (thousands_sep == undefined) { thousands_sep = "."; }
            i = parseInt(number = (+number || 0).toFixed(decimals)) + "";
            if ((j = i.length) > 3) { j = j % 3; } else { j = 0; }
            km = (j ? i.substr(0, j) + thousands_sep : "");
            kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
            kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
            return km + kw + kd;
          }
        
          function updyn() {
            $.ajax({
                type: 'POST',
                url: 'ajax.php?func=upautodyn',
                data: ({id: <?=$pr_row['id']?>, title: $('#title').val(), url: $('#url').val(), timer: $('#timer').val(), geo_ru: $('#geo_ru:checked').val(), geo_ua: $('#geo_ua:checked').val(), geo_by: $('#geo_by:checked').val(), geo_kz: $('#geo_kz:checked').val(), geo_ge: $('#geo_ge:checked').val(), geo_md: $('#geo_md:checked').val(), geo_am: $('#geo_am:checked').val(), geo_tj: $('#geo_tj:checked').val(), geo_az: $('#geo_az:checked').val(), geo_uz: $('#geo_uz:checked').val(), geo_pt: $('#geo_pt:checked').val(), geo_es: $('#geo_es:checked').val(), geo_ee: $('#geo_ee:checked').val(), geo_lv: $('#geo_lv:checked').val(), geo_lt: $('#geo_lt:checked').val(), geo_us: $('#geo_us:checked').val(), geo_gb: $('#geo_gb:checked').val(), geo_fr: $('#geo_fr:checked').val(), geo_de: $('#geo_de:checked').val(), geo_cn: $('#geo_cn:checked').val(), geo_all: $('#geo_all:checked').val(), geo_it: $('#geo_it:checked').val()}),
                dataType: 'html',
                beforeSend: function() {
                    $('.btn_box').css({display:'none'});
                    $('.pajax').css({display:'block'});
                    if ( parseInt($('#timer').val()) >= 0 && $('#title').val() != '' && $('#url').val() != '' ) {
                        $('.pajax').css({display:'block'}).html('проверка на вирусы - [ Dr.web ]');
                    }
                },
                success: function(data) {
                    if(data == 1) {
                        $('.pajax').css({display:'none'});
                       document.location.href = 'mautodyn';
                    } else
					if(data == 2) {
						$('#error_title_form2').fadeIn();
						$('.div_form_eror_title').fadeIn().html('Вы не указали описание ссылки!');
						$('.inputStart_description').css({border: '1px solid #E85854'});
						$('.pajax').css({display:'none'});
						upScroll();
					} else 
					if(data == 3) {
						$('#error_title_form3').fadeIn();
						$('.div_form_eror_title').fadeIn().html('Вы не указали url ссылки!');
						$('.inputStart_url').css({border: '1px solid #E85854'});
						$('.pajax').css({display:'none'});
						upScroll();
					} else {
                        $('.erroralert').fadeIn(500).html(data);
                        setTimeout(function() {
                            $('.erroralert').fadeOut(500).html('&nbsp;');
                        }, 4000);
                        $('.pajax').css({display:'none'});
                    }
                }
            });
            return false;
          }
		
		  function show_geo() {
			if ($('.show_geo').css('display') == 'none') {
				$('.show_geo').fadeIn(500);
			} else {
				$('.show_geo').fadeOut(300);
			}
		  }
		  
		  ///Функция возврата каретки в верх окна.
		  var timeScrollUP; 
		  var top = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
		  function upScroll() {
		    var top = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
		    if(top > 0) {
			  window.scrollBy(0,-100);
			  timeScrollUP = setTimeout('upScroll()',10);
		    } else clearTimeout(timeScrollUP);
		    return false;
		  }
	
		</script>
        <link media="all" type="text/css" rel="stylesheet" href="/theme/css/input_form_fix.css" />
		
		<nav class="breadcrumb breadcrumb_stabilizator" style="background: #F7F7F7; border-radius: 1px;">
			<a class="breadcrumb-item" href="/mautodyn">Управление рекламой</a>
			<span class="breadcrumb-item active">Редактирование</span>
		</nav>
		
		<div class="content_bar">
			<article class="postBox">
				
				<h5><a>Редактирование площадки № <?=$pr_row['id']?></a></h5><br>
				
				<div class="div_form">
					<table class=" table_form">
						<tr>
							<td class="td_form">Описание ссылки:</td>
							<td class="" >
								<input class="inputStart_description" type="text" placeholder="укажите краткое описание рекламируемого ресурса" id="title"  value="<?=$pr_row['title']?>" maxlength="55" />
								<div style="display:none;" id="error_title_form2">
									<div class="eror_form"></div>
									<div class="div_form_eror_title">Внимание, ссылка указанна не верно!</div>
								</div>
							</td>
						</tr>
                
						<tr>
							<td class="td_form">URL сайта:</td>
							<td class="" >
								<input class="inputStart_url" type="text" placeholder="обязательно укажите ссылку начиная с http:/https:" value="<?=$pr_row['url']?>" id="url" />
								<div style="display:none;" id="error_title_form3">
									<div class="eror_form"></div>
									<div class="div_form_eror_title">Внимание, ссылка указанна не верно!</div>
								</div>
							</td>
						</tr>
						<tr>
							<td class="td_form">Время просмотра ссылки:</td>
							<td class=""><select id="timer" style="width: 100px; height: 22px; display: none;" onchange="PlanChange(); return false;">
							<option>-- Выберите --</option>
							<option value="0" <? if( $pr_row['timer'] == 0 ) echo 'selected';?>>10 секунд&nbsp;&nbsp;(<?=$config['autodyn10']?> <?=$config['valutaname'] ?>) - Mini</option>
							<option value="1" <? if( $pr_row['timer'] == 1 ) echo 'selected';?>>20 секунд&nbsp;&nbsp;(<?=$config['autodyn20']?> <?=$config['valutaname'] ?>)</option>
							<option value="2" <? if( $pr_row['timer'] == 2 ) echo 'selected';?>>30 секунд&nbsp;&nbsp;(<?=$config['autodyn30']?> <?=$config['valutaname'] ?>)</option>
							<option value="3" <? if( $pr_row['timer'] == 3 ) echo 'selected';?>>40 секунд&nbsp;&nbsp;(<?=$config['autodyn40']?> <?=$config['valutaname'] ?>)</option>
							<option value="4" <? if( $pr_row['timer'] == 4 ) echo 'selected';?>>50 секунд&nbsp;&nbsp;(<?=$config['autodyn50']?> <?=$config['valutaname'] ?>)</option>
							<option value="5" <? if( $pr_row['timer'] == 5 ) echo 'selected';?>>60 секунд&nbsp;&nbsp;(<?=$config['autodyn60']?> <?=$config['valutaname'] ?>)</option>
							</select></td>
						</tr>
						<tr>
							<td class="td_form" style="vertical-align: top;">Геотаргетинг:</td>
							<td >
								<div class="margin_add_geo"><a class="add_geo" onclick="show_geo();return false" href="#">скрыть / показать</a></div>
								<div class="show_geo" style="">
									<table class="table_geo_addtext">
										<tr>
											<td style="width: 50%;"><input type="checkbox" id="geo_ru" value="ru" <? if( $pr_row['geo_ru'] == 'ru' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/ru.png" class="flagcountry" alt="" />&nbsp;&nbsp;Россия</label></td>
											<td style="width: 50%;"><input type="checkbox" id="geo_ua" value="ua" <? if( $pr_row['geo_ua'] == 'ua' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/ua.png" class="flagcountry" alt="" />&nbsp;&nbsp;Украина</label></td>
										</tr>
										<tr>
											<td style="width: 50%;"><input type="checkbox" id="geo_by" value="by" <? if( $pr_row['geo_by'] == 'by' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/by.png" class="flagcountry" alt="" />&nbsp;&nbsp;Белоруссия</label></td>
											<td style="width: 50%;"><input type="checkbox" id="geo_ge" value="ge" <? if( $pr_row['geo_ge'] == 'ge' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/ge.png" class="flagcountry" alt="" />&nbsp;&nbsp;Грузия</label></td>
										</tr>
										<tr>
											<td style="width: 50%;"><input type="checkbox" id="geo_md" value="md" <? if( $pr_row['geo_md'] == 'md' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/md.png" class="flagcountry" alt="" />&nbsp;&nbsp;Молдова</label></td>
											<td style="width: 50%;"><input type="checkbox" id="geo_kz" value="kz" <? if( $pr_row['geo_kz'] == 'kz' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/kz.png" class="flagcountry" alt="" />&nbsp;&nbsp;Казакстан</label></td>
										</tr>
										<tr>
											<td style="width: 50%;"><input type="checkbox" id="geo_am" value="am" <? if( $pr_row['geo_am'] == 'am' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/am.png" class="flagcountry" alt="" />&nbsp;&nbsp;Армения</label></td>
											<td style="width: 50%;"><input type="checkbox" id="geo_tj" value="tj" <? if( $pr_row['geo_tj'] == 'tj' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/tj.png" class="flagcountry" alt="" />&nbsp;&nbsp;Таджикистан</label></td>
										</tr>
										<tr>
											<td style="width: 50%;"><input type="checkbox" id="geo_az" value="az" <? if( $pr_row['geo_az'] == 'az' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/az.png" class="flagcountry" alt="" />&nbsp;&nbsp;Азербайджан</label></td>
											<td style="width: 50%;"><input type="checkbox" id="geo_uz" value="uz" <? if( $pr_row['geo_uz'] == 'uz' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/uz.png" class="flagcountry" alt="" />&nbsp;&nbsp;Узбекистан</label></td>
										</tr>
										<tr>
											<td style="width: 50%;"><input type="checkbox" id="geo_pt" value="pt" <? if( $pr_row['geo_pt'] == 'pt' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/pt.png" class="flagcountry" alt="" />&nbsp;&nbsp;Португалия</label></td>
											<td style="width: 50%;"><input type="checkbox" id="geo_es" value="es" <? if( $pr_row['geo_es'] == 'es' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/es.png" class="flagcountry" alt="" />&nbsp;&nbsp;Испания</label></td>
										</tr>
										<tr>
											<td style="width: 50%;"><input type="checkbox" id="geo_ee" value="ee" <? if( $pr_row['geo_ee'] == 'ee' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/ee.png" class="flagcountry" alt="" />&nbsp;&nbsp;Эстония</label></td>
											<td style="width: 50%;"><input type="checkbox" id="geo_lv" value="lv" <? if( $pr_row['geo_lv'] == 'lv' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/lv.png" class="flagcountry" alt="" />&nbsp;&nbsp;Латвия</label></td>
										</tr>
										<tr>
											<td style="width: 50%;"><input type="checkbox" id="geo_lt" value="lt" <? if( $pr_row['geo_lt'] == 'lt' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/lt.png" class="flagcountry" alt="" />&nbsp;&nbsp;Литва</label></td>
											<td style="width: 50%;"><input type="checkbox" id="geo_us" value="us" <? if( $pr_row['geo_us'] == 'us' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/us.png" class="flagcountry" alt="" />&nbsp;&nbsp;Америка</label></td>
										</tr>
										<tr>
											<td style="width: 50%;"><input type="checkbox" id="geo_gb" value="gb" <? if( $pr_row['geo_gb'] == 'gb' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/gb.png" class="flagcountry" alt="" />&nbsp;&nbsp;Англия</label></td>
											<td style="width: 50%;"><input type="checkbox" id="geo_fr" value="fr" <? if( $pr_row['geo_fr'] == 'fr' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/fr.png" class="flagcountry" alt="" />&nbsp;&nbsp;Франция</label></td>
										</tr>
										<tr>
											<td style="width: 50%;"><input type="checkbox" id="geo_de" value="de" <? if( $pr_row['geo_de'] == 'de' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/de.png" class="flagcountry" alt="" />&nbsp;&nbsp;Германия</label></td>
											<td style="width: 50%;"><input type="checkbox" id="geo_cn" value="cn" <? if( $pr_row['geo_cn'] == 'cn' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/cn.png" class="flagcountry" alt="" />&nbsp;&nbsp;Китай</label></td>
										</tr>
										<tr>
											<td style="width: 50%;"><input type="checkbox" id="geo_it" value="it" <? if( $pr_row['geo_it'] == 'it' ) echo 'checked="selected"';?> /> <label>&nbsp;<img src="/theme/images/flags/it.png" class="flagcountry" alt="" />&nbsp;&nbsp;Италия</label></td>
											<td style="width: 50%;"><input type="checkbox" id="geo_all" value="all" <? if( $pr_row['geo_all'] == 'all' ) echo 'checked="selected"';?> /> <label>Другие страны</label></td>
										</tr>
									</table>
								</div>
							</td>
						</tr>
						<tr>
							<td class="td_form">Стоимость просмотра:</td>
							<td class=""><div class="formPrice"><span id="summa"><?=$config[$timer]?></span> <?=$config['valutaname']?></div></td>
						</tr>
						<tr>
							<td class="td_form"><img src="/theme/images/preload.gif" class="pajax" style="display: none;"/></td>
							<td class="">
								<br><div class="more-link"><a href="#" onclick="updyn(); return false;">Сохранить</a></div><br><br>
							</td>
						</tr>
						<tr>
							<td class="td_form"></td>
							<td class="td_form" colspan="2"><div class="erroralert" ></div></td>
						</tr>
					</table>
				</div>
				<br>
			</article>
		</div>
				
        
        <?
    }
}
?>