function GetXmlHttpObject(){
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	}
	if (window.ActiveXObject){// code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function rssTimeout(uwid,auto_refresh_interval){
	window['xmlhttpRSS_'+uwid].abort();
	document.getElementById("rss_"+uwid).innerHTML="<div class='error'>Connection has timed out <input type='button' class='ImagesRefresh' onclick='getFeed("+uwid+", "+auto_refresh_interval+")' /> <input type='button' class='ImagesSettings' onclick='showSettingsRSS("+uwid+")' /></div>";
}

function hideElement(id){
	document.getElementById(id).style.display="none";	
}

function showElement(id){
	document.getElementById(id).style.display="inline";	
}

function showSettingsRSS(uwid){
	document.getElementById("dimBackground").style.display="block";
	document.getElementById("settings").style.display="block";
	//document.getElementById("settings").innerHTML="settings";
	
	window['xmlhttpRSS_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpRSS_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_rss/ajax/showSettings.php";
	url=url+"?uwid="+uwid+"&sid="+Math.random();
	window['xmlhttpRSS_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpRSS_'+uwid].readyState!=4){
			document.getElementById("settings").innerHTML="<div class='load'></div>";
		}
		if (window['xmlhttpRSS_'+uwid].readyState==4){
			document.getElementById("settings").innerHTML=window['xmlhttpRSS_'+uwid].responseText;
		}
	};
	window['xmlhttpRSS_'+uwid].open("GET",url,true);
	window['xmlhttpRSS_'+uwid].send(null);
}

function hideSettingsRSS(){
	document.getElementById("settings").style.display="none";
	document.getElementById("dimBackground").style.display="none";
}

function updateFieldRSS(uwid,field_name,value){
	window['xmlhttpRSS_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpRSS_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	value=encodeURIComponent(value);
	
	var url="widget_rss/ajax/updateField.php";
	url=url+"?uwid="+uwid+"&field_name="+field_name+"&value="+value;
	url=url+"&sid="+Math.random();
	window['xmlhttpRSS_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpRSS_'+uwid].readyState!=4){
			document.getElementById("update_field_status").innerHTML="<div class='load'></div>";
		}
		/*if (window['xmlhttpRSS_'+uwid].readyState==4){
			document.getElementById("update_field_status").innerHTML=window['xmlhttpRSS_'+uwid].responseText;
			showSettingsRSS(uwid);
		}*/
		if (window['xmlhttpRSS_'+uwid].readyState==4){
			document.getElementById("update_field_status").innerHTML="";//remove loading icon
			if(window['xmlhttpRSS_'+uwid].responseText==1){//if data was valid and settings have been saved...
				document.getElementById("validate_"+field_name).setAttribute("class", "success");
				document.getElementById("validate_"+field_name).style.display="inline";
				document.getElementById("validate_"+field_name).innerHTML='<span class="pointer">&nbsp;</span>'+'OK';
				$("#validate_"+field_name).delay(1000).fadeOut(1000);
				
				if(field_name=="auto_refresh"&&value==1){//if auto refresh has been enabled, change the onclick event of the exit button
					auto_refresh_interval=document.getElementById("auto_refresh_interval").value;
					document.getElementById("exit_"+uwid).setAttribute("onclick", "hideSettingsRSS(); getFeed("+uwid+", "+auto_refresh_interval+");");
				}
				if(field_name=="auto_refresh"&&value==0){//if auto refresh has been disabled, change the onclick event of the exit button
					document.getElementById("exit_"+uwid).setAttribute("onclick", "hideSettingsRSS(); getFeed("+uwid+", 0);");
				}
				if(field_name=="auto_refresh_interval"){//if auto refresh interval has changed...
					if(document.getElementById("auto_refresh_1").checked)//.and auto refresh is enabled, change the onclick event of the exit button
						document.getElementById("exit_"+uwid).setAttribute("onclick", "hideSettingsRSS(); getFeed("+uwid+", "+value+");");
				}
			}
			else if(window['xmlhttpRSS_'+uwid].responseText==0){//...else, if data was valid but settings could not be saved...
				document.getElementById("validate_"+field_name).setAttribute("class", "error");
				document.getElementById("validate_"+field_name).style.display="inline";
				document.getElementById("validate_"+field_name).innerHTML='<span class="pointer">&nbsp;</span>'+'Error';
				$("#validate_"+field_name).delay(3000).fadeOut(1000);
			}
			else{//...else, data was not valid...
				document.getElementById("validate_"+field_name).setAttribute("class", "error");
				document.getElementById("validate_"+field_name).style.display="inline";
				document.getElementById("validate_"+field_name).innerHTML='<span class="pointer">&nbsp;</span>'+window['xmlhttpRSS_'+uwid].responseText;
				$("#validate_"+field_name).delay(3000).fadeOut(1000);
			}
		}
	};
	window['xmlhttpRSS_'+uwid].open("GET",url,true);
	window['xmlhttpRSS_'+uwid].send(null);
}

function enableDisableAutoRefresh(id,value){
	if(value==1)
		document.getElementById(id).removeAttribute("disabled");
	else
		if(value==0)
			document.getElementById(id).setAttribute("disabled", "disabled");
}

function downloadFeed(uwid,auto_refresh_interval){

	window['xmlhttpRSS_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpRSS_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_rss/ajax/downloadFeed.php";
	url=url+"?uwid="+uwid+"&sid="+Math.random();
	window['xmlhttpRSS_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpRSS_'+uwid].readyState!=4){
			//document.getElementById("rss_"+uwid).innerHTML="<div class='load'></div>";
		}
		if (window['xmlhttpRSS_'+uwid].readyState==4){
			if(window['xmlhttpRSS_'+uwid].responseText==1)//new feeds have been added, refresh the getFeed()
				getFeed(uwid,auto_refresh_interval);	
		}
	}
	window['xmlhttpRSS_'+uwid].open("GET",url,true);
	window['xmlhttpRSS_'+uwid].send(null);
}

function getFeed(uwid,auto_refresh_interval){
	
	if(typeof(window['auto_refresh_rss_'+uwid])!="undefined"){
		clearTimeout(window['auto_refresh_rss_'+uwid]);
	}
	
	window['xmlhttpRSS_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpRSS_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_rss/ajax/getFeed.php";
	url=url+"?uwid="+uwid+"&sid="+Math.random();
	window['xmlhttpRSS_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpRSS_'+uwid].readyState!=4){
			document.getElementById("rss_"+uwid).innerHTML="<div class='load'></div>";
		}
		if (window['xmlhttpRSS_'+uwid].readyState==4){
			clearTimeout(window['rss_'+uwid+'_timeout']);
			document.getElementById("rss_"+uwid).innerHTML=window['xmlhttpRSS_'+uwid].responseText;
			if(auto_refresh_interval!=0)
				window['auto_refresh_rss_'+uwid]=setTimeout('getFeed(' + uwid + ', ' + auto_refresh_interval + ')', (auto_refresh_interval*1000));//auto-refresh function
			downloadFeed(uwid,auto_refresh_interval);//when db feed load finishes, search for new feeds and download them	
		}
	}
	window['xmlhttpRSS_'+uwid].open("GET",url,true);
	window['xmlhttpRSS_'+uwid].send(null);
	
	window['rss_'+uwid+'_timeout']=setTimeout("rssTimeout("+uwid+", "+auto_refresh_interval+")", 7000);
}

function feedDetail(uwid,auto_refresh_interval,random_key){
	
	if(typeof(window['auto_refresh_rss_'+uwid])!="undefined"){
		clearTimeout(window['auto_refresh_rss_'+uwid]);
	}
	
	window['xmlhttpRSS_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpRSS_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_rss/ajax/feedDetail.php";
	url=url+"?uwid="+uwid+"&random_key="+random_key;
	url=url+"&sid="+Math.random();
	window['xmlhttpRSS_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpRSS_'+uwid].readyState!=4){
			document.getElementById("rss_"+uwid).innerHTML="<div class='load'></div>";
		}
		if (window['xmlhttpRSS_'+uwid].readyState==4){
			clearTimeout(window['rss_'+uwid+'_timeout']);
			document.getElementById("rss_"+uwid).innerHTML=window['xmlhttpRSS_'+uwid].responseText;
		}
	}
	window['xmlhttpRSS_'+uwid].open("GET",url,true);
	window['xmlhttpRSS_'+uwid].send(null);
	
	window['rss_'+uwid+'_timeout']=setTimeout("rssTimeout("+uwid+", "+auto_refresh_interval+")", 7000);
}

function elementInfoRSS(uwid,element){
	
	document.getElementById("validate_"+element).removeAttribute("class"); //remove previous errors (if any) and show the normal tooltip
	
	window['xmlhttpRSS_'+uwid+'_elementInfo']=GetXmlHttpObject();
	
	if (window['xmlhttpRSS_'+uwid+'_elementInfo']==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_rss/ajax/elementInfoRSS.php";
	url=url+"?"+element+"&sid="+Math.random();
	
	window['xmlhttpRSS_'+uwid+'_elementInfo'].open("GET",url,false);
	window['xmlhttpRSS_'+uwid+'_elementInfo'].send(null);
	
	document.getElementById("validate_"+element).style.display="inline";
	document.getElementById("validate_"+element).innerHTML='<span class="pointer">&nbsp;</span>'+window['xmlhttpRSS_'+uwid+'_elementInfo'].responseText;
	$("#validate_"+element).delay(3000).fadeOut(1000);
}
