function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString(); 
	}else 
	var expires = "";
	document.cookie = name+"="+value+expires+"; path=/; domain=.realclearpolitics.com";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function zc(){

	document.getElementById('zipform').style.display='none';
	document.getElementById('processingtable').style.display='block';

	zip =	document.getElementById("weatherzipcode").value;
	setTimeout("ajaxcountzips('weather/countzips.php?zip='+zip, zip, 'home')", 100);
     	
}

function zc2(){	
	
	zip =	document.getElementById("weatherzipcode").value;
	ajaxcountzips('countzips.php?zip='+zip, zip, 'inner');		
}



var rootdomain="http://"+window.location.hostname;

function ajaxinclude(url) {
	var page_request = false;
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest();
	else if (window.ActiveXObject){ // if IE	
		try { page_request = new ActiveXObject("Msxml2.XMLHTTP"); } 	
		catch (e){
			try{ page_request = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (e){  }
		}
	}else
		return false;
		page_request.open('GET', url, false); //get page synchronously 
		page_request.send(null);
		handle = getpagecontent(page_request);
		processReturnData(handle); //this functions outputs everything on the page
}

function getpagecontent(page_request){
	if (window.location.href.indexOf("http")==-1 || page_request.status==200);
	//document.write(page_request.responseText)
	return page_request.responseText;
}

function processReturnData(z){
	var results = z.split("|");
	data1 = results[0]; //city
	data2 = results[1]; //temp
	data3 = results[2]; //icon
	data4 = results[3]; //zip
	data5 = results[4]; //failure
	
	if(data5==''){							
		/*@cc_on
		@if (@_jscript_version <= 5.6)
			document.getElementById("weatherstatus").innerHTML = '<a href="javascript:showForm();" >'+data1+'</a> &nbsp;<a href="http://realclearpolitics.com/weather/?zip='+data4+'" target="_parent">'+data2+'</a>';
		@else @*/
			//alert("above version 6");
			document.getElementById("weatherstatus").innerHTML = '<a href="javascript:showForm();" >'+data1+'</a> &nbsp;'+data2;			
			document.getElementById("weathericon").innerHTML = "<a href='http://www.realclearpolitics.com/weather/?zip="+data4+"' target='_parent'><img src='http://www.realclearpolitics.com/images/weathericons/"+data3+".png' border='0'></a>";
		/*@end @*/				
	}else if(data5!='' && data5!='serverdownfileexists'){			
		//showForm();
		document.getElementById('alerttable').style.display='block';
		document.getElementById('alerttext').innerHTML = data5;
	}
}


function showForm(){
	document.getElementById('zipform').style.display='block';
}

function ajaxcountzips(url,zip,loc){
	
	var page_request = false;
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest();
	else if (window.ActiveXObject){ // if IE	
		try { page_request = new ActiveXObject("Msxml2.XMLHTTP"); } 	
		catch (e){
			try{ page_request = new ActiveXObject("Microsoft.XMLHTTP"); }	
			catch (e){}
		}
	}else
		return false;
		page_request.open('GET', url, false); //get page synchronously 
		page_request.send(null);
		handle = getpagecontent(page_request);		
		
		processReturnData2(handle,zip,loc); //this functions outputs everything on the page
}

function processReturnData2(failure,zip,loc){

	if(failure!=0){ //FAILED
		document.getElementById('processingtable').style.display='none';
		document.getElementById('alerttable').style.display='block';
		document.getElementById('alerttext').innerHTML = failure;
	}else{
		if(loc=='home'){
			eraseCookie("rcpweather");
			createCookie("rcpweather",zip,500); //60646 is chicago 		
			
			window.location.href = "http://www.realclearpolitics.com/inc.homeweather.html"; 			
		}else if(loc=='inner'){
			/*var message = "Set "+zip+" as your default location?"; //message for box
			var return_value = confirm(message); //display confirm dialog box	
		
			if(return_value == true){			
				eraseCookie("rcpweather");
				createCookie("rcpweather",zip,500); 								
			}*/		
			window.location.href = "http://www.realclearpolitics.com/weather/?zip="+zip; 
		}
	}
}
	



	

