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;
}

var aIndex = 0;
var ajaxReq = new Array();
ajaxReq[0] = GetXmlHttpObject();  //request for posing comments - set aIndex to 0
ajaxReq[1] = GetXmlHttpObject();  //request for getting views - set aIndex to 1
ajaxReq[2] = GetXmlHttpObject();  // request for getting comments - set aIndex to 2
ajaxReq[3] = GetXmlHttpObject();  //add items to playlist - set aIndex to 3;
ajaxReq[4] = GetXmlHttpObject();  // use to register users to mailing list
ajaxReq[5] = GetXmlHttpObject(); 

var newsletnotice = document.createElement('div');
function maillistadd(theform)
{
	var emailregex = /^[^@\s<&>]+@([-a-z0-9A-Z]+\.)+[a-zA-Z]{2,}$/;
	
	if (!theform.newsletter.value.match(emailregex))
	{
		alert("Email address for mailing list appears to be invalid.  Please correct and try again.");
	}
	else
	{
	var url="mailinglist.php";
	
	var parameters = "email=" + encodeURI(theform.newsletter.value);
	
	ajaxReq[4].onreadystatechange = function(){
		
		if (ajaxReq[4].readyState==4)
		{
			
		
		newsletnotice.setAttribute('id','notificationbox');
		newsletnotice.style.backgroundColor = '#0d2d77';
		newsletnotice.style.border = "1px white solid";
		newsletnotice.style.width='450px';
		newsletnotice.style.position = 'absolute';
		newsletnotice.style.zIndex = '4';
		newsletnotice.style.padding ='10px';
		newsletnotice.style.marginLeft = '75px';
		newsletnotice.style.clear = 'both';
		newsletnotice.style.top = '400px';
		newsletnotice.style.marginTop='80px';
		newsletnotice.style.textAlign = 'center';
		newsletnotice.innerHTML ="Thank you for signing up to our mailing list.  You will receive an email shortly confirming your registration with instructions on how to unregister, if you wish to stop receiving our Newsletter.  We hope you enjoy your time on the Afrogist website.  <br><br> <a href=\"javascript:removeElement3(newsletnotice);\"><img src='cclose.png' border='0'></a></div>";	
		
		document.getElementById("content").appendChild(newsletnotice);
		}
		
		}
	
	ajaxReq[4].open("POST",url,true);
	ajaxReq[4].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajaxReq[4].setRequestHeader("Content-length", parameters.length);
    ajaxReq[4].setRequestHeader("Connection", "close");
	ajaxReq[4].send(parameters);
		
	}
	
}

function removeElement3(obj)
{
	var pagecontent = document.getElementById("content");
	pagecontent.removeChild(obj);
}

function postcom(theform)
{
aIndex = 0;
if (ajaxReq[aIndex]==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }

var parameters = '';

for (var i=0; i<theform.elements.length; i++)
{
    if(theform.elements[i].name !='submit')
    {
        if (i>0)
        {
        parameters+='&';    
        }
    parameters+=theform.elements[i].name+'='+theform.elements[i].value;
    }
    
}

var url='comments.php?'+parameters;
//document.write(url);
ajaxReq[aIndex].onreadystatechange= function(){
	if (ajaxReq[aIndex].readyState==4)
		{
		document.getElementById("comments").innerHTML=ajaxReq[aIndex].responseText;
	
	for (var i=0; i<theform.elements.length; i++)
	{
    	if((theform.elements[i].name =='author')||(theform.elements[i].name =='comment'))
    	{	
	    theform.elements[i].value = null;
		}
    }
	
		}	
}
ajaxReq[aIndex].open("GET",url,true);
//ajaxReq[aIndex].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
ajaxReq[aIndex].send(null);
}


function getViews(vid_type, id)
{
    aIndex = 1;
    var url='getViews.php?';
    var parameters = 'vid_type='+vid_type+'&videoid='+id;
    url+=parameters;
    ajaxReq[aIndex].onreadystatechange=function(){
		if (ajaxReq[1].readyState==4)
			{ 
			document.getElementById("views").innerHTML=ajaxReq[1].responseText;
			}
	}
    ajaxReq[aIndex].open("GET",url,true);
    ajaxReq[aIndex].send(null); 
}

function updatelist()
{
var audioplayer = getFlashMovieObject();
var callresult = audioplayer.reloadlist();
}

function getFlashMovieObject()
{ var movieName = "mp3player";
if(document.embeds[movieName])
return document.embeds[movieName];
if(window.document[movieName])
return window.document[movieName];
if(window[movieName])
return window[movieName];
if(document[movieName])
return document[movieName];
return null;
}


function addtolist(ID)
{
aIndex = 3;
var url = "playlists.php?action=add&ID="+ID;
ajaxReq[aIndex].onreadystatechange=alertuser;
ajaxReq[aIndex].open("GET",url,true);
ajaxReq[aIndex].send(null);

updatelist();
}

/*
function alertuser()
{
	if (ajaxReq[aIndex].readyState==4)
	{ 
		if ((indexOf('musicv',location.href)<0)&&(indexOf('musica',location.href)<0))
		{
			alert('Added to playlist');
		//write code to refresh flash playlist
		}
	}
}*/

function getComments(vid_type, id)
{
    var url='comments.php?';
    var parameters = 'vid_type='+vid_type+'&videoid='+id;
    url+=parameters;
   ajaxReq[2].onreadystatechange=function() //stateChanged;
{
	if (ajaxReq[2].readyState==4)
	{
	document.getElementById("comments").innerHTML=ajaxReq[2].responseText;	
	}
}
    ajaxReq[2].open("GET",url,true);
    ajaxReq[2].send(null);
	
    //set video number for form
    document.usercomments.videoid.value=id;
    getViews(vid_type, id);
	updatembcode(id, vid_type);
	location.hash = id;
}

function updatembcode(ID,sm)
{
	 var embedobj = "<object width='490' height='410'><param name='allowScriptAccess' value='Always' /><param name='allowFullScreen' value='true' /><param name='FlashVars' value='videoid="+ID+"&sm="+sm+"' /><param name='movie' value='http://www.afrogist.tv/embplayer.swf' /><param name='wmode' value='transparent' /><embed src='http://www.afrogist.tv/embplayer.swf' FlashVars='videoid="+ID+"&sm="+sm+"' bgcolor='#000000' width='490' height='410' name='player' align='middle' allowScriptAccess='Always' allowFullScreen='true' wmode='transparent' type='application/x-shockwave-flash'/></object>";

document.getElementById('emdcode').value = embedobj;
}

function listchange(value)
{

var keywordfield = document.searchform.keyword;
switch(value)
	{
		case 'music': keywordfield.value = 'Enter artist name or song tile';
					break;
		case 'interviews': keywordfield.value = 'Enter artist';
					break;	
		case 'performances': keywordfield.value = 'Enter artist or event';
					break;	
		case 'shows': keywordfield.value = 'Enter show';
					break;
		default:	keywordfield.value = '';
					break;
	}
}


function tabnav(tabIndex, parentObj)
{
pobj = document.getElementById(parentObj);
var obj = pobj.getElementsByTagName('li');
	for (var i=0; i<obj.length; i++)
	{
		obj[i].className = 'notselected';
	}

tabIndex.className = 'selected';
//load page
var url ='';

	if(tabIndex.innerHTML == 'Search')
	{
	url='searchform.html';
	}
	else if(tabIndex.innerHTML == 'Headlines')
	{
	url='news.php';
	}
	else if(tabIndex.innerHTML == 'Comments')
	{
	url='comments.php';
	}
	else if(tabIndex.innerHTML == 'Playlist')
	{
	url='search form.html';
	}
	else if(tabIndex.innerHTML == 'Recent Blog')
	{
	url='recentblog.php';
	}
	else if(tabIndex.innerHTML == 'Headlines')
	{
	url='/newsapp/googlenewsrss.php';
	}
ajaxloader(url,'utilscontainer');
}


function ajaxloader(url, retcont) 
{
	ajaxReq[5].onreadystatechange=function(){
		
		if (ajaxReq[5].readyState==3)
			{
			document.getElementById(retcont).innerHTML = '<img src=\'ajax-loader.gif\'>';
			document.getElementById(retcont).style.textAlign = 'center';
			}
		
		if (ajaxReq[5].readyState==4)
			{
			document.getElementById(retcont).innerHTML="<div id='result'>" + ajaxReq[5].responseText + "</div>";
			document.getElementById(retcont).style.textAlign = 'left';
			contresizer(document.getElementById('result'), document.getElementById(retcont));
			}
	}

	ajaxReq[5].open("GET",url,true);
	ajaxReq[5].send(null); 
}


function ajaxsearch(theform)
{
var category = theform.cat.value;
var criteria = theform.keyword.value;
var parameters = "category="+category+"&criteria="+criteria;
var url = '';


ajaxReq[6].onreadystatechange=function(){
		
		if (ajaxReq[6].readyState==3)
			{
			document.getElementById(retcont).innerHTML = '<img src=\'ajax-loader.gif\'>';
			document.getElementById(retcont).style.textAlign = 'center';
			}
			
		if (ajaxReq[6].readyState==4)
			{ 
			document.getElementById(retcont).innerHTML=ajaxReq[6].responseText;
			document.getElementById(retcont).style.textAlign = 'left';
			
			}

}
ajaxReq[6].open("POST",url,true);
ajaxReq[6].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxReq[6].setRequestHeader("Content-length", parameters.length);
ajaxReq[6].setRequestHeader("Connection", "close");
ajaxReq[6].send(parameters);
}

function contresizer(childcont, parentcont)
{
	parentcont.style.height = childcont.scrollHeight + 20 + 'px'; 	
}
