var systemHost = ''
var systemOs = ''
var systemBrowser = ''
var systemVersion = ''
var debug = 0
var nextSlideNumber = 1
var winPlayerStatus = 0
var raPlayerStatus = 0
var currentStream = ''
var autoPlay = 'no' // Added 12-02-2009 - triggers autoplay function from onload in configureSystem()
var autoSearch = 'no' // Added 12-3-2009 - triggers autosearch function from onload in configureSystem()
var domain = 'en'
var rollingAds = 'no'
var rollingQuotes = 'no'

var registeredUser=0
var forceUpdate = 'none' // Used to force an update of user config content on return from set/unset
var forceUpdateId = 'none'

var preseletedFormId = 0

var xmlHttp
var xmlHttpBackground // Additional AJAX handle to prevent clashes with the auto update functions
var xmlHttpControl // Additional AJAX handle to prevent clashes when updateing filter controls
var xmlHttpTopFive // Additional AJAX handle to prevent clashes when updateing Top 5 Lists
var xmlHttpUserData // Additional AJAX handle to prevent clashes when Switching User Data
var xmlHttpFormData
var xmlHttpListings
var xmlHttpEpisodes
var xmlHttpPlayer
var xmlHttpNewsListings // Given it's own handle as it's auto refreshed and could cause a clash
var xmlHttpTrafficListings // Given it's own handle as it's auto refreshed and could cause a clash
var xmlHttpTicker // Given it's own handle as it's auto refreshed and could cause a clash
var xmlHttpUserData // Given it's own handle as it's auto refreshed and could cause a clash

var radioCountry = 0
var radioCategory = 0
var radioText = ''
var radioResultsPage = 1

var podLang = 0
var podProvider = 0
var podCategory = 0
var podText = ''
var podResultsPage = 1
var podListId = 0
var expandedPodBox = 0

var newsLang = 99
var newsProvider = 0
var newsFeed = 0
var newsText = ''
var newsRefreshTimerId = 0

var currentPage = 1

var tickerLang = 99
var tickerSource = 1
var tickerRefreshTimerId = 0
var tickerRate = 20000

var predictiveSearchTimerId = 0
var predictivePodSearchTimerId = 0
var predictiveTrafficSearchTimerId = 0
var timeIndex = 0

var currentPlayer = 'windows'
var currentRadioId = 0
var currentPodId = 0

var traffic1 = ''
var traffic2 = ''
var traffic3 = ''
var trafficRefreshTimerId = 0
var trafficRefreshTimerRate = 300000 // 300000 = 5 minutes

var wxRegion = 0

var tickerTopFiveTimerId = 0
var topFiveRefreshRate = 600000

function GetXmlHttpObject(){
	var objXMLHttp=null
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest()
	}else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}


function changeRadioCountry(data){ radioCountry=data; radioResultsPage=1; updateRadioListings();}
function changeRadioCategory(data){ radioCategory=data; radioResultsPage=1;	updateRadioListings();}
function changeRadioResultsPage(data){ radioResultsPage=data; updateRadioListings();}

function changePodLanguage(data){ podLang=data; podProvider=0; podResultsPage=1; updatePodControls();}
function changePodProvider(data){ podProvider=data; podResultsPage=1; updatePodListings();}
function changePodCategory(data){ podCategory=data; podResultsPage=1; updatePodListings();}
function changePodResultsPage(data){ podResultsPage=data; updatePodListings();}

function changeNewsLanguage(data){ newsLang=data; tickerLang=data; changeNewsProvider(0); newsProvider = 0; newsFeed = 0; updateNewsControls();}
function changeNewsProvider(data){ newsProvider=data; newsFeed = 0; updateNewsControls();}
function changeNewsFeed(data){ newsFeed=data; updateNewsListings();}

function overrideRadioSearchFilters(setCountry,setCategory,setText){
	radioCountry = setCountry
	radioCategory = setCategory
	radioText = setText
	radioResultsPage=1
	updateRadioListings()
}

function overridePodSearchFilters(setSource,setCategory,setText){
	podProvider = setSource
	podCategory = setCategory
	podText = setText
	podResultsPage=1
	updatePodListings()
}

function updateRadioListings(){
	var date = new Date();
	var timestamp = date.getTime();
	document.body.style.cursor='wait'
	xmlHttpListings=GetXmlHttpObject()
	var url="processors/search_radio.php"
	encodedRadioText = encodeURIComponent(radioText)
	url=url+"?country="+radioCountry+"&category="+radioCategory+"&page="+radioResultsPage+"&time="+timestamp+"&debug="+debug+"&domain="+domain+"&text="+encodedRadioText
	xmlHttpListings.onreadystatechange=refreshRadioListings
	xmlHttpListings.open("GET",url,true)
	xmlHttpListings.send(null)
}

function refreshRadioListings() {
	if (xmlHttpListings.readyState==4 || xmlHttpListings.readyState=="complete"){
		document.getElementById("RadioEditorial").style.display='none'
		document.getElementById("RadioListings").innerHTML=xmlHttpListings.responseText
		scroll(0,0);
		document.body.style.cursor='default'
		updateDebug()
		showPage(2)
		document.getElementById('filter_text').focus()
	}
}

function updatePodListings(){
	var date = new Date();
	var timestamp = date.getTime();
	document.body.style.cursor='wait'
	xmlHttpListings=GetXmlHttpObject()
	var url="processors/search_podcasts.php"
	url=url+"?provider="+podProvider+"&category="+podCategory+"&text="+podText+"&page="+podResultsPage+"&lang="+podLang+"&time="+timestamp+"&domain="+domain
	xmlHttpListings.onreadystatechange=refreshPodListings
	xmlHttpListings.open("GET",url,true)
	xmlHttpListings.send(null)
}

function refreshPodListings() {
	if (xmlHttpListings.readyState==4 || xmlHttpListings.readyState=="complete"){
		document.getElementById("PodEditorial").style.display='none'
		document.getElementById("PodListings").innerHTML=xmlHttpListings.responseText
		scroll(0,0);
		document.body.style.cursor='default'
		updateDebug()
		showPage(3)
		document.getElementById('pod_filter_text').focus()
	}
}


function fetchPodEpisodes(){
	var date = new Date();
	var timestamp = date.getTime();
	document.body.style.cursor='wait'
	xmlHttpEpisodes=GetXmlHttpObject()
	var url="processors/fetch_podcast_episodes.php"
	url=url+"?podid="+podListId+"&time="+timestamp+"&domain="+domain
	xmlHttpEpisodes.onreadystatechange=renderPodEpidsodeData
	xmlHttpEpisodes.open("GET",url,true)
	xmlHttpEpisodes.send(null)
}

function renderPodEpidsodeData() {
	if (xmlHttpEpisodes.readyState==4 || xmlHttpEpisodes.readyState=="complete"){
		var podEpidsodeDiv = "PodEpisodes_"+podListId
		document.getElementById(podEpidsodeDiv).innerHTML=xmlHttpEpisodes.responseText
		document.body.style.cursor='default'
		updateDebug()
	}
}



function updatePodControls(){
	var date = new Date();
	var timestamp = date.getTime();
	document.body.style.cursor='wait'
	xmlHttpControl=GetXmlHttpObject()
	var url="controls/fetch_pod_controls.php"
	url=url+"?lang="+podLang+"&time="+timestamp+"&domain="+domain
	//alert(url)
	xmlHttpControl.onreadystatechange=refreshPodControls
	xmlHttpControl.open("GET",url,true)
	xmlHttpControl.send(null)
}

function refreshPodControls() {
	if (xmlHttpControl.readyState==4 || xmlHttpControl.readyState=="complete"){
		document.getElementById("DynamicPodControls").innerHTML=xmlHttpControl.responseText
		scroll(0,0);
		document.body.style.cursor='default'
		updatePodListings()
	}
}



function updateNewsControls(){
	var date = new Date();
	var timestamp = date.getTime();
	document.body.style.cursor='wait'
	xmlHttpControl=GetXmlHttpObject()
	if (xmlHttpControl==null){ alert ("Browser does not support HTTP Request"); return;}
	var url="controls/fetch_news_controls.php"
	url=url+"?lang="+newsLang+"&provider="+newsProvider+"&feed="+newsFeed+"&time="+timestamp+"&domain="+domain
	//alert(url)
	xmlHttpControl.onreadystatechange=refreshNewsControls
	xmlHttpControl.open("GET",url,true)
	xmlHttpControl.send(null)
}

function refreshNewsControls() {
	if (xmlHttpControl.readyState==4 || xmlHttpControl.readyState=="complete"){
		document.getElementById("DynamicNewsControls").innerHTML=xmlHttpControl.responseText
		scroll(0,0);
		document.body.style.cursor='default'
		updateDebug()
		resetNewsRefreshTimer()
		updateNewsListings()
	}
}






function updateNewsListings(){
	var date = new Date();
	var timestamp = date.getTime();
	document.body.style.cursor='wait'
	xmlHttpNewsListings=GetXmlHttpObject()
	var url="processors/search_news.php"
	url=url+"?lang="+newsLang+"&provider="+newsProvider+"&feed="+newsFeed+"&text="+newsText+"&time="+timestamp+"&domain="+domain
	//alert(url)
	xmlHttpNewsListings.onreadystatechange=refreshNewsListings
	xmlHttpNewsListings.open("GET",url,true)
	xmlHttpNewsListings.send(null)
}

function refreshNewsListings() {
	if (xmlHttpNewsListings.readyState==4 || xmlHttpNewsListings.readyState=="complete"){
		document.getElementById("NewsEditorial").style.display='none'
		document.getElementById("NewsListings").innerHTML=xmlHttpNewsListings.responseText
		scroll(0,0);
		document.body.style.cursor='default'
		updateDebug()
		resetNewsRefreshTimer()
	}
}


function updateTrafficControls(){
	var date = new Date();
	var timestamp = date.getTime();
	document.body.style.cursor='wait'
	xmlHttpControl=GetXmlHttpObject()
	var url="controls/fetch_traffic_controls.php"
	url=url+"?country="+trafficCountry+"&region="+trafficRegion+"&time="+timestamp+"&domain="+domain
	//alert(url)
	xmlHttpControl.onreadystatechange=refreshTrafficControls
	xmlHttpControl.open("GET",url,true)
	xmlHttpControl.send(null)
}

function refreshTrafficControls() {
	if (xmlHttpControl.readyState==4 || xmlHttpControl.readyState=="complete"){
		document.getElementById("DynamicTrafficControls").innerHTML=xmlHttpControl.responseText
		scroll(0,0);
		document.body.style.cursor='default'
	}
}


function updateTrafficListings(){

	traffic1 = document.getElementById('road1').value
	traffic2 = document.getElementById('road2').value
	traffic3 = document.getElementById('road3').value

	var date = new Date();
	var timestamp = date.getTime();
	document.body.style.cursor='wait'
	xmlHttpTrafficListings=GetXmlHttpObject()
	var url="processors/search_traffic.php"
	url=url+"?road1="+traffic1+"&road2="+traffic2+"&road3="+traffic3+"&time="+timestamp+"&domain="+domain
	//alert(url)
	xmlHttpTrafficListings.onreadystatechange=refreshTrafficListings
	xmlHttpTrafficListings.open("GET",url,true)
	xmlHttpTrafficListings.send(null)
}

function refreshTrafficListings() {
	if (xmlHttpTrafficListings.readyState==4 || xmlHttpTrafficListings.readyState=="complete"){
		document.getElementById("TrafficEditorial").style.display='none'
		document.getElementById("TrafficListings").innerHTML=xmlHttpTrafficListings.responseText
		scroll(0,0);
		document.body.style.cursor='default'
		resetTrafficRefreshTimer()
	}
}






function updateSiteInfo(pageId,formId){
	preselectedFormId = formId
	var date = new Date();
	var timestamp = date.getTime();
	document.body.style.cursor='wait'
	xmlHttp=GetXmlHttpObject()
	var url="processors/site_info.php"
	url=url+"?id="+pageId+"&rpuid="+registeredUser+"&form_id="+formId+"&time="+timestamp+"&domain="+domain
	if(formId == 98) url=url+"&pod_id="+currentPodId
	if(formId == 99) url=url+"&station_id="+currentRadioId
	xmlHttp.onreadystatechange=refreshSiteInfo
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function refreshSiteInfo() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		document.getElementById("Page0").innerHTML=xmlHttp.responseText
		scroll(0,0);
		document.body.style.cursor='default'
		if(preselectedFormId !=0) fetchContactForm(preselectedFormId)
		preselectedFormId = 0
		showPage(0)
	}
}

function updateWeatherData(regionId){
	if(regionId!=0){
		var date = new Date();
		var timestamp = date.getTime();
		document.body.style.cursor='wait'
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null){ alert ("Browser does not support HTTP Request"); return;}
		var url="processors/search_weather.php"
		url=url+"?region="+regionId+"&time="+timestamp+"&domain="+domain
		xmlHttp.onreadystatechange=refreshWeather
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}else{
		document.getElementById("WeatherEditorial").style.display='block'
		document.getElementById("WeatherListings").style.display='none'
	}
}

function refreshWeather() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		document.getElementById("WeatherListings").innerHTML=xmlHttp.responseText
		document.getElementById("WeatherEditorial").style.display='none'
		document.getElementById("WeatherListings").style.display='block'
		document.body.style.cursor='default'
	}
}


function configureSystem(debugSwitch,loginUser,noneUserstartPage,startDomain,autoPlaySwitch,autoSearchSwitch,rollingAdsSwitch,rollingQuoteSwitch){
	domain = startDomain
	if(domain == 'fr'){
		newsLang = 114;
		tickerLang = 114;
	}
	if(domain == 'nl'){
		newsLang = 273;
		tickerLang = 273;
	}	
	registeredUser = loginUser
	debug = debugSwitch
	autoPlay = autoPlaySwitch
	autoSearch = autoSearchSwitch
	rollingAds = rollingAdsSwitch
	rollingQuotes = rollingQuoteSwitch

	if(noneUserstartPage!=0){
		currentPage = noneUserstartPage // Forces specified page to open e.g. login error or external link
		showPage(currentPage)
	}
	var date = new Date()
	var timestamp = date.getTime()
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){ alert ("Browser does not support HTTP Request (AJAX 370)"); return;}
	var url="modules/config.php"
	url=url+"?time="+timestamp+"&domain="+domain
	xmlHttp.onreadystatechange=initConfigData
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)

}

function initConfigData() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		//alert(xmlHttp.responseText)
		data = xmlHttp.responseText.split('|')
		systemHost = data[0]
		systemOs = data[1]
		systemBrowser = data[2]
		systemVersion = data[3]
		systemEnv = data[4]
		resetTickerTimer()
		resetTopFiveTimer()


		if(registeredUser!=0){
			fetchUserConfigSettings()
		}else{
			updateDebug()
		}


		if(systemOs=='WIN' && systemBrowser=='MSIE'){
			loadWinIePlayers()
		}
		if(systemOs=='WIN' && systemBrowser=='FIREFOX'){
			loadWinFFPlayers()
		}
		if(systemOs=='MAC' && systemBrowser=='SAFARI'){
			loadMacSafariPlayers()
		}
		if(systemOs=='MAC' && systemBrowser=='FIREFOX'){
			loadMacFFPlayers()
		}


		// Chrome Test
		if(systemOs=='WIN' && systemBrowser=='CHROME'){
			loadWinChromePlayers()
		}

		//

		// Initial Missing Plugin in message -- Removed 04/11/08
		//if(systemBrowser!="CHROME"){
		//
		//	if(winPlayerStatus==0 || raPlayerStatus==0){
		//		alert("Please be aware you may be missing one or more required components.\n\nThis means you will be unable to listen to some of the content available from Radiopaq.\n\nPlease contact our Technical Support for help and advice.")
		//	}
		//}
		if(autoPlay !='no'){
			//alert('Autoplay dtected'+autoPlay);
			bits = autoPlay.split('|')
			switchStream(bits[0],bits[1],bits[2],bits[3])
		}
		if(autoSearch!='no'){
			//alert('Switching To Radio Search Page')
			radioText = autoSearch
			updateRadioListings()
			document.getElementById('filter_text').value=radioText
		}

		if(rollingAds == 'yes') runAds()
		if(rollingQuotes == 'yes') runQuotes()
	}

}

function fetchUserConfigSettings(){
	var date = new Date()
	var timestamp = date.getTime()
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){ alert ("Browser does not support HTTP Request (AJAX 435)"); return;}
	var url="modules/fetch_user_details.php"
	url=url+"?rpuid="+registeredUser+"&time="+timestamp+"&domain="+domain
	xmlHttp.onreadystatechange=applyUserConfigSettings
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function applyUserConfigSettings(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		//alert(xmlHttp.responseText)
		var configData = new Array()
		configData = xmlHttp.responseText.split(',')
		radioCountry = configData[0]
		currentPage = configData[1]
		podLang = configData[2]
		newsLang = configData[3]
		tickerLang = configData[3]

		showPage(currentPage)
		updateDebug()
	}
}

function updateDebug(){
	var debugString = 'Debug Data <br /><br />'
	debugString=debugString+'User Id = '+registeredUser+'<br />'
	debugString=debugString+'Host = '+systemHost+'<br />'
	debugString=debugString+'OS = '+systemOs+'<br />'
	debugString=debugString+'Browser = '+systemBrowser+'<br />'
	debugString=debugString+'Version = '+systemVersion+'<br />'
	debugString=debugString+'Windows Player State = '+winPlayerStatus+'<br />'
	debugString=debugString+'Real Audio Player State = '+raPlayerStatus+'<br /><br />'
	debugString=debugString+'Current Stream = '+currentStream+'<br /><br />'
	debugString=debugString+'Current Player = '+currentPlayer+'<br /><br />'
	debugString=debugString+'Viewing Page = '+currentPage+'<br /><br />'
	debugString=debugString+'R Country = '+radioCountry+'<br />'
	debugString=debugString+'R Category = '+radioCategory+'<br />'
	debugString=debugString+'R ResultsPage = '+radioResultsPage+'<br /><br />'
	debugString=debugString+'P Provider = '+podProvider+'<br />'
	debugString=debugString+'P Category = '+podCategory+'<br />'
	debugString=debugString+'P ResultsPage = '+podResultsPage+'<br /><br />'
	debugString=debugString+'N Lang = '+newsLang+'<br />'
	debugString=debugString+'N Provider = '+newsProvider+'<br />'
	debugString=debugString+'N Feed = '+newsFeed+'<br /><br />'
	debugString=debugString+'T Lang = '+tickerLang+'<br />'
	debugString=debugString+'T Source = '+tickerSource+'<br />'
	document.getElementById('Debug').innerHTML=debugString
}

function resetTickerTimer(){
	clearTimeout(tickerRefreshTimerId)
	tickerRefreshTimerId = setTimeout("refreshTickerData()",tickerRate)
}

function resetTopFiveTimer(){
	clearTimeout(tickerTopFiveTimerId)
	tickerTopFiveTimerId = setTimeout("refreshTopFiveData()",topFiveRefreshRate)
}

function resetNewsRefreshTimer(){
	clearTimeout(newsRefreshTimerId)
	newsRefreshTimerId = setTimeout("updateNewsListings()",600000)
}

function setSearchTimer(){
	clearTimeout(predictiveSearchTimerId)
	predictiveSearchTimerId = setTimeout("doPredictiveSearch()",800)
}

function doPredictiveSearch(){
	radioResultsPage=1
	radioText = document.getElementById('filter_text').value
	updateRadioListings()
}

function setPodSearchTimer(){
	clearTimeout(predictivePodSearchTimerId)
	predictivePodSearchTimerId = setTimeout("doPredictivePodSearch()",800)
}

function doPredictivePodSearch(){
	podResultsPage=1
	podText = document.getElementById('pod_filter_text').value
	updatePodListings()
}

function setTrafficSearchTimer(){
	clearTimeout(predictiveTrafficSearchTimerId)
	predictiveTrafficSearchTimerId = setTimeout("doPredictiveTrafficSearch()",800)
}

function resetTrafficRefreshTimer(){
	clearTimeout(trafficRefreshTimerId)
	trafficRefreshTimerId = setTimeout("updateTrafficListings()",trafficRefreshTimerRate)
}



function doPredictiveTrafficSearch(){
	//trafficText = document.getElementById('traffic_filter_text').value
	updateTrafficListings()
}

function updatePlayerInfo(id,type){
	var date = new Date();
	var timestamp = date.getTime();
	xmlHttpPlayer=GetXmlHttpObject()
	var url="modules/"+type+"_player_info.php"
	url=url+"?type="+type+"&id="+id+"&rpuid="+registeredUser+"&time="+timestamp+"&domain="+domain
	xmlHttpPlayer.onreadystatechange=refreshPlayerInfo
	xmlHttpPlayer.open("GET",url,true)
	xmlHttpPlayer.send(null)
	if(type=='podcast'){
		timeIndex = setInterval(refreshPlayerTimer, 1000);
	}else{
		clearInterval(timeIndex);
	}
}
function refreshPlayerInfo() {
	if (xmlHttpPlayer.readyState==4 || xmlHttpPlayer.readyState=="complete"){
		document.getElementById("Player").innerHTML=xmlHttpPlayer.responseText
	}
}

function refreshPlayerTimer() {
	if(document.getElementById('PlayerTime')){ // odd intermittent error when switching to radio from pocasts options
		if(systemOs=='WIN'){
			if(document.WMPlayer.controls.currentPositionString.length>1){
				document.getElementById('PlayerTime').innerHTML=document.WMPlayer.controls.currentPositionString
			}else{
				document.getElementById('PlayerTime').innerHTML='00:00'
			}
		}else{
			var nowMarker = document.QTPlayer.GetTime()
			var timeScale = document.QTPlayer.GetTimeScale()
			var nowTime = parseInt(nowMarker / timeScale)
			var minVar = Math.floor(nowTime/60);
			if(minVar<10)minVar='0'+minVar
			var secVar = nowTime % 60;
			if(secVar<10)secVar='0'+secVar
			document.getElementById('PlayerTime').innerHTML=minVar+':'+secVar
		}
	}
}


function refreshTickerData(){
	var date = new Date()
	var timestamp = date.getTime()
	xmlHttpTicker=GetXmlHttpObject()
	var url="processors/fetch_ticker_data.php?lang="+tickerLang+"&source="+tickerSource
	url=url+"&time="+timestamp+"&domain="+domain
	xmlHttpTicker.onreadystatechange=updateTicker
	xmlHttpTicker.open("GET",url,true)
	xmlHttpTicker.send(null)
	tickerSource++
	if(tickerSource == 7)tickerSource = 1
	updateDebug()
}

function updateTicker(){
	if (xmlHttpTicker.readyState==4 || xmlHttpTicker.readyState=="complete"){
		fadeTickerDown(100)
		//alert(xmlHttpTicker.responseText)
	}
}


function refreshTopFiveData(){
	var date = new Date()
	var timestamp = date.getTime()
	xmlHttpTopFive=GetXmlHttpObject()
	if (xmlHttp==null){ alert ("Browser does not support HTTP Request AJAX Refresh Top 5"); return;}
	var url="processors/refresh_speeds_and_feeds.php?time="+timestamp+"&domain="+domain
	xmlHttpTopFive.onreadystatechange=updateTopFive
	xmlHttpTopFive.open("GET",url,true)
	xmlHttpTopFive.send(null)
}

function updateTopFive(){
	if (xmlHttpTopFive.readyState==4 || xmlHttpTopFive.readyState=="complete"){
		bits = xmlHttpTopFive.responseText.split('####')
		document.getElementById('TopRadio').innerHTML=bits[0]
		document.getElementById('TopPod').innerHTML=bits[1]
		document.getElementById('TopNews').innerHTML=bits[2]
		resetTopFiveTimer()
	}
}


function saveUserData(userFunc,userData){
	//alert(userFunc)
	if(userFunc == 'delete_radio_fav') forceUpdate = userFunc // set the user data upadet trigger
	if(userFunc == 'save_radio_fav') forceUpdate = userFunc // set the user data upadet trigger
	if(userFunc == 'delete_podcast_fav') forceUpdate = userFunc // set the user data upadet trigger
	if(userFunc == 'save_podcast_fav') forceUpdate = userFunc // set the user data upadet trigger
	if(userFunc == 'save_default_wx_region') forceUpdate = userFunc // set the user data upadet trigger
	//alert(forceUpdate)
	var date = new Date()
	var timestamp = date.getTime()
	xmlHttpUserData=GetXmlHttpObject()
	var url="processors/"+userFunc+".php?rpuid="+registeredUser+"&data="+userData
	url=url+"&time="+timestamp+"&domain="+domain
	xmlHttpUserData.onreadystatechange=confirmUserDataUpdate
	xmlHttpUserData.open("GET",url,true)
	xmlHttpUserData.send(null)
}

function confirmUserDataUpdate(){
	if (xmlHttpUserData.readyState==4 || xmlHttpUserData.readyState=="complete"){
		if(xmlHttpUserData.responseText!='')alert(xmlHttpUserData.responseText)// report the error
		if(forceUpdate!='none'){
			var date = new Date()
			var timestamp = date.getTime()
			xmlHttpUserData=GetXmlHttpObject()

			if(forceUpdate == 'delete_radio_fav' || forceUpdate == 'save_radio_fav'){
				//forceUpdateId = 'UserData2' // My Radio Content Object
				//alert(forceUpdateId)
				var url="processors/refresh_my_radio.php?rpuid="+registeredUser
				url=url+"&time="+timestamp+"&domain="+domain
			}

			if(forceUpdate == 'delete_podcast_fav' || forceUpdate == 'save_podcast_fav'){
				//forceUpdateId = 'UserData3' // My Podcast Content Object
				//alert(forceUpdateId)
				var url="processors/refresh_my_podcast.php?rpuid="+registeredUser
				url=url+"&time="+timestamp+"&domain="+domain
			}

			if(forceUpdate == 'save_default_wx_region'){
				var url="processors/refresh_my_weather.php?rpuid="+registeredUser
				url=url+"&time="+timestamp+"&domain="+domain
			}

			xmlHttpUserData.onreadystatechange=updateUserData
			xmlHttpUserData.open("GET",url,true)
			xmlHttpUserData.send(null)
		}
	}
}

function updateUserData(){
	if (xmlHttpUserData.readyState==4 || xmlHttpUserData.readyState=="complete"){
		document.getElementById('UserDataWrapper').innerHTML=xmlHttpUserData.responseText
		forceUpdate = 'none' // reset the update trigger once the upadte is complete
	}
}

function addPodFav(){
	saveUserData('save_podcast_fav',currentPodId)
}
function addRadFav(){
	saveUserData('save_radio_fav',currentRadioId)
}
function delRadFav(data){
	if(confirm('Are you sure?'))saveUserData('delete_radio_fav',data)
}
function delPodFav(data){
	if(confirm('Are you sure?'))saveUserData('delete_podcast_fav',data)
}

function saveDefaultRoadData(){
	var newRoadData = document.getElementById('myroad1').value+'|'+document.getElementById('myroad2').value+'|'+document.getElementById('myroad3').value
	saveUserData('save_default_road_data',newRoadData)
}

function nextSlide(){
	nextSlideNumber++
	if(nextSlideNumber==9)nextSlideNumber=1
	document.getElementById('SlideShow').innerHTML='<img src="slideshow/'+nextSlideNumber+'.jpg" onclick="nextSlide();" /><br />Comin Soon!'
}

function nextSlide_fr(){
	nextSlideNumber++
	if(nextSlideNumber==9)nextSlideNumber=1
	document.getElementById('SlideShow').innerHTML='<img src="slideshow/fr/'+nextSlideNumber+'.jpg" onclick="nextSlide_fr();" /><br />'
}


function log_java_clickthrough(descriptor,data){
	var date = new Date()
	var timestamp = date.getTime()
	xmlHttpBackground=GetXmlHttpObject()
	if (xmlHttpBackground==null){ alert ("Browser does not support HTTP Request AJAX Log Click"); return;}
	var url="processors/register_java_clickthrough.php?desc="+descriptor+"&data="+data
	url=url+"&time="+timestamp+"&domain="+domain
	xmlHttpBackground.onreadystatechange=checkErrors
	xmlHttpBackground.open("GET",url,true)
	xmlHttpBackground.send(null)
}

function registerStreamClick(streamType,streamId){
	//alert('Registering')
	var date = new Date()
	var timestamp = date.getTime()
	xmlHttpBackground=GetXmlHttpObject()
	if (xmlHttp==null){ alert ("Browser does not support HTTP Request AJAX Register Stream"); return;}
	var url="processors/register_stream_click.php?type="+streamType+"&data="+streamId
	url=url+"&time="+timestamp+"&domain="+domain
	xmlHttpBackground.onreadystatechange=checkErrors
	xmlHttpBackground.open("GET",url,true)
	xmlHttpBackground.send(null)
}
function checkErrors(){
	if (xmlHttpBackground.readyState==4 || xmlHttpBackground.readyState=="complete"){
		if(xmlHttpBackground.responseText !='') alert(xmlHttpBackground.responseText)
	}
}

function registerNewsClick(encodedNewsURL,newsURL){
	//alert(encodedNewsURL+' '+newsURL)
	window.open(newsURL)
	var date = new Date()
	var timestamp = date.getTime()
	xmlHttpBackground=GetXmlHttpObject()
	if (xmlHttp==null){ alert ("Browser does not support HTTP Request AJAX Register Stream"); return;}
	var url="processors/register_stream_click.php?type=news&data="+encodedNewsURL
	url=url+"&time="+timestamp+"&domain="+domain
	xmlHttpBackground.onreadystatechange=checkErrors
	xmlHttpBackground.open("GET",url,true)
	xmlHttpBackground.send(null)
}


function fetchContactForm(formId){
	if(formId>0){
		var data = ''
		var date = new Date()
		var timestamp = date.getTime()
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null){ alert ("Browser does not support HTTP Request AJAX Register Stream"); return;}
		if (formId == 2){ // build system report for Technical Support
			data='Operating System = '+systemOs+'|Browser = '+systemBrowser+'|Version = '+systemVersion+'|WMA Player = '+winPlayerStatus+'|RA Player = '+raPlayerStatus
		}

		var url="modules/render_contact_form.php?formid="+formId+"&data="+data
		url=url+"&time="+timestamp+"&domain="+domain
		xmlHttp.onreadystatechange=renderContactForm
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}else{
		document.getElementById('ContactFormHolder').innerHTML=''
	}
}

function renderContactForm(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		document.getElementById('ContactFormHolder').innerHTML=xmlHttp.responseText
		if(document.getElementById('contact_name'))document.getElementById('contact_name').focus();
	}
}


function sendTAFForm(){
	document.body.style.cursor='wait'
	var contactData = ''
	contactData = contactData+document.getElementById('from_name').value
	contactData = contactData+'|'+document.getElementById('from_email').value
	contactData = contactData+'|'+document.getElementById('taf_name').value
	contactData = contactData+'|'+document.getElementById('taf_email').value
	contactData = contactData+'|'+document.getElementById('apType').value
	contactData = contactData+'|'+document.getElementById('apId').value
	var date = new Date()
	var timestamp = date.getTime()
	xmlHttpFormData=GetXmlHttpObject()
	var url="processors/send_taf_form.php?contactdata="+contactData
	url=url+"&time="+timestamp+"&domain="+domain
	xmlHttpFormData.onreadystatechange=updateTAFDisplay
	xmlHttpFormData.open("GET",url,true)
	xmlHttpFormData.send(null)
	//alert(contactData)
}

function updateTAFDisplay(){
	if (xmlHttpFormData.readyState==4 || xmlHttpFormData.readyState=="complete"){
		document.getElementById('ContactFormHolder').innerHTML=xmlHttpFormData.responseText
		//alert(xmlHttpFormData.responseText)
		document.body.style.cursor='default'
	}
}


function sendContactForm(formNumber){
	document.body.style.cursor='wait'

	if(formNumber == 1){ // Build Feedback Data
		var contactData = '1|'
		contactData = contactData+document.getElementById('contact_name').value
		contactData = contactData+'|'+document.getElementById('contact_email').value
		contactData = contactData+'|'+document.getElementById('comment').value
	}

	if(formNumber == 2){ // Build Technical Support Data
		var contactData = '2|'
		contactData = contactData+document.getElementById('contact_name').value
		contactData = contactData+'|'+document.getElementById('contact_email').value
		contactData = contactData+'|'+document.getElementById('comment').value
		contactData = contactData+'|'+document.getElementById('system_report').value
	}

	if(formNumber == 3){ // Build Sales Data
		var contactData = '3|'
		contactData = contactData+document.getElementById('contact_name').value
		contactData = contactData+'|'+document.getElementById('contact_email').value
		contactData = contactData+'|'+document.getElementById('contact_company').value
		contactData = contactData+'|'+document.getElementById('contact_role').value
		contactData = contactData+'|'+document.getElementById('contact_number').value
		contactData = contactData+'|'+document.getElementById('comment').value
	}

	if(formNumber == 4){ // Build Business Data
		var contactData = '4|'
		contactData = contactData+document.getElementById('contact_name').value
		contactData = contactData+'|'+document.getElementById('contact_email').value
		contactData = contactData+'|'+document.getElementById('contact_company').value
		contactData = contactData+'|'+document.getElementById('contact_role').value
		contactData = contactData+'|'+document.getElementById('contact_number').value
		contactData = contactData+'|'+document.getElementById('comment').value
	}

	if(formNumber == 5){ // Build Press Data
		var contactData = '5|'
		contactData = contactData+document.getElementById('contact_name').value
		contactData = contactData+'|'+document.getElementById('contact_email').value
		contactData = contactData+'|'+document.getElementById('media_title').value
		contactData = contactData+'|'+document.getElementById('contact_number').value
		contactData = contactData+'|'+document.getElementById('comment').value
		if(document.getElementById('press_list').checked){
			contactData = contactData+'|y'
		}else{
			contactData = contactData+'|n'
		}
	}

	if(formNumber == 6){ // Build Editorial Data
		var contactData = '6|'
		contactData = contactData+document.getElementById('contact_name').value
		contactData = contactData+'|'+document.getElementById('contact_email').value
		contactData = contactData+'|'+document.getElementById('contact_company').value
		contactData = contactData+'|'+document.getElementById('contact_role').value
		contactData = contactData+'|'+document.getElementById('contact_number').value
		contactData = contactData+'|'+document.getElementById('comment').value
	}

	if(formNumber == 8){ // Build New Submission Data
		var contactData = '8|'
		contactData = contactData+document.getElementById('contact_name').value
		contactData = contactData+'|'+document.getElementById('contact_email').value
		contactData = contactData+'|'+document.getElementById('contact_company').value
		contactData = contactData+'|'+document.getElementById('contact_role').value
		contactData = contactData+'|'+document.getElementById('contact_number').value
		contactData = contactData+'|'+document.getElementById('new_web_address').value
		contactData = contactData+'|'+document.getElementById('new_genre').value
		contactData = contactData+'|'+document.getElementById('new_lang').value
		contactData = contactData+'|'+document.getElementById('new_country').value
		if(document.getElementById('add_radio').checked){
			contactData = contactData+'|y'
		}else{
			contactData = contactData+'|n'
		}
		if(document.getElementById('add_podcast').checked){
			contactData = contactData+'|y'
		}else{
			contactData = contactData+'|n'
		}
		if(document.getElementById('agree_terms').checked){
			contactData = contactData+'|y'
		}else{
			contactData = contactData+'|n'
		}
	}

	if(formNumber == 9){ // Build Listing Correction Data
		var contactData = '9|'
		contactData = contactData+document.getElementById('contact_name').value
		contactData = contactData+'|'+document.getElementById('contact_email').value
		contactData = contactData+'|'+document.getElementById('contact_company').value
		contactData = contactData+'|'+document.getElementById('contact_role').value
		contactData = contactData+'|'+document.getElementById('contact_number').value
		contactData = contactData+'|'+document.getElementById('station_name').value
		contactData = contactData+'|'+document.getElementById('comment').value
	}

	if(formNumber == 10){ // Build Removal Request Data
		var contactData = '10|'
		contactData = contactData+document.getElementById('contact_name').value
		contactData = contactData+'|'+document.getElementById('contact_email').value
		contactData = contactData+'|'+document.getElementById('contact_company').value
		contactData = contactData+'|'+document.getElementById('contact_role').value
		contactData = contactData+'|'+document.getElementById('contact_number').value
	}

	//alert(contactData)

	var date = new Date()
	var timestamp = date.getTime()
	xmlHttpFormData=GetXmlHttpObject()
	var url="processors/send_contact_form.php?contactdata="+contactData
	url=url+"&time="+timestamp+"&domain="+domain
	xmlHttpFormData.onreadystatechange=updateFormSubmissionReport
	xmlHttpFormData.open("GET",url,true)
	xmlHttpFormData.send(null)

}

function updateFormSubmissionReport(){
	if (xmlHttpFormData.readyState==4 || xmlHttpFormData.readyState=="complete"){
		document.getElementById('contactFormSelector').selectedIndex = 0
		document.getElementById('ContactFormHolder').innerHTML=xmlHttpFormData.responseText
		document.body.style.cursor='default'
	}
}

function updateUserProfile(){
	document.body.style.cursor='wait'
	var userGender = document.getElementById('user_gender').options[document.getElementById('user_gender').selectedIndex].value
	var userDOB = document.getElementById('bday').value+'-'+document.getElementById('bmonth').value+'-'+document.getElementById('byear').value
	var userCountry = document.getElementById('user_country').options[document.getElementById('user_country').selectedIndex].value
	var userCity = document.getElementById('user_city').value
	var userHome = document.getElementById('user_home').options[document.getElementById('user_home').selectedIndex].value
	var userUpdates = document.getElementById('user_updates').options[document.getElementById('user_updates').selectedIndex].value
	var userLogin = document.getElementById('user_login').options[document.getElementById('user_login').selectedIndex].value

	var newUserData = registeredUser+'|'+userGender+'|'+userDOB+'|'+userCountry+'|'+userCity+'|'+userHome+'|'+userUpdates+'|'+userLogin

	var date = new Date()
	var timestamp = date.getTime()
	xmlHttpFormData=GetXmlHttpObject()
	var url="processors/update_user_profile.php?data="+newUserData
	url=url+"&time="+timestamp+"&domain="+domain
	xmlHttpFormData.onreadystatechange=confirmDataSaved
	xmlHttpFormData.open("GET",url,true)
	xmlHttpFormData.send(null)
}

function confirmDataSaved(){
	if (xmlHttpFormData.readyState==4 || xmlHttpFormData.readyState=="complete"){
		alert(xmlHttpFormData.responseText)
		document.body.style.cursor='default'
	}
}
