var xmlHttp

function showPollution(city_compare,city_reference,init,no_cookie)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="/goodies/comparator/"; 
url=url+"compare_pollution.php";
if (!city_compare && init!=1) var city_compare=document.getElementById("city_compare").options[document.getElementById("city_compare").selectedIndex].value;
if (!city_reference && init!=1) var city_reference=document.getElementById("city_reference").options[document.getElementById("city_reference").selectedIndex].value;

var params="";
if (city_compare) params=params+"city_compare="+city_compare;
if (city_compare && city_reference) params=params+"&";
if (city_reference) params=params+"city_reference="+city_reference;

document.getElementById("pollution_content").innerHTML="<img src='http://www.pollution-china.com/goodies/comparator/waiting.gif'/><em>Refreshing...</em>";

xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);

//Send the proper header information along with the request
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");

xmlHttp.send(params);

if (init!=1)
{
Set_Cookie( 'pollution_city_compare', city_compare, '30', '/', '', '' );
Set_Cookie( 'pollution_city_reference', city_reference, '30', '/', '', '' );
}
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("pollution_content").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
var today = new Date();
today.setTime( today.getTime() );
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
function Get_Cookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false;

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		a_temp_cookie = a_all_cookies[i].split( '=' );


		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

if ( Get_Cookie( 'pollution_city_compare' ) ) var cookie_city_compare= Get_Cookie('pollution_city_compare');
if ( Get_Cookie( 'pollution_city_reference' ) ) var cookie_city_reference= Get_Cookie('pollution_city_reference');

showPollution(cookie_city_compare,cookie_city_reference,1,1);
