<?php
if ($_GET['op']=="5") {
session_start();
$_SESSION['setpogoda'] = 1;
$_SESSION['setpogoda1'] = $_GET['id'];
$_SESSION['setpogoda2'] = $_GET['si'];
echo '<script>window.location.href = \'index.php?name=Pogoda\'</script>';
		die();
}

	Header("Last-Modified: ".gmdate("D, d M Y H:i:s", strtotime("-1 day"))." GMT");
	Header("Content-Type: text/html; charset=windows-1251");
	if (!empty($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache/2')) {
		Header("Cache-Control: no-cache, pre-check=0, post-check=0");
	} else {
		Header("Cache-Control: private, pre-check=0, post-check=0, max-age=0");
	}
  Header("Expires: 0");
  Header("Pragma: no-cache");


function readservis($link) {
	$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $link);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 6.0)");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $res = curl_exec($ch);
    curl_close($ch);
	return iconv("UTF-8", "CP1251", $res);
}

function echo_sc ($mass, $opi, $si=0) {
echo "<div id=s2>";
foreach($mass[2] as $key => $val) {
	echo '<span style="cursor: pointer;" id='.$mass[1][$key].'>'.$mass[2][$key].'</span><br>';
}
echo "</div>";

if ($si) {
	$sistr = "+'&si='+kwick.innerHTML";
} else {
	$sistr = "";
}

echo "
<script>
var kwicks = $$('#s2 span');
kwicks.each(function(kwick, i) {
	kwick.addEvent(\"click\", function(event) {
		var o = {};
	
	var url = 'ajax/Pogoda/index.php?op=".$opi."&id='+kwick.id".$sistr.";
	var ajax41set = new Ajax(url, { 
		update: $('Log1'),
		method: 'get',
		evalScripts: 'true'
	});
	ajax41set.request();

	});
});
</script>
";
}

switch ($_GET['op']) {

	case "1":
$out = readservis("http://www.gismeteo.ru/city/catalog/regions/");
preg_match_all("#<li><a href=\"/city/catalog/countries/(.*?)/\">(.*?)</a></li>#si",$out,$tabl);
echo_sc ($tabl, 2);
    break;

	case "2":
$out = readservis("http://www.gismeteo.ru/city/catalog/countries/".$_GET['id']."/");
preg_match_all("#city/catalog/cities/\?country=(.*?)\">(.*?)</a>#si",$out,$tabl);
echo_sc ($tabl, 3);
	break;

	case "3":
$out = readservis("http://www.gismeteo.ru/city/catalog/cities/?country=".$_GET['co']."&id=".$_GET['id']."");
if ($_GET['id']=="156") {
	//Россия
	preg_match_all("#city/catalog/cities/\?country=(.*?)\">(.*?)</a>#si",$out,$tabl);
	echo_sc ($tabl, 4);
} else {
	//Другие
	preg_match_all("#<li><a href=\"/city/daily/(.*?)/\">(.*?)</a></li>#si",$out,$tabl);
	echo_sc ($tabl, 5, 1);
}
	break;

	case "4":
$out = readservis("http://www.gismeteo.ru/city/catalog/cities/?country=ALL&id=156&district=".$_GET['district']."");
preg_match_all("#<li><a href=\"/city/daily/(.*?)/\">(.*?)</a></li>#si",$out,$tabl);
echo_sc ($tabl, 5, 1);
	break;

}
?>