function viewBiggerPicture(src, div)
{
	//<div id="shade"></div>
	document.getElementById(div).innerHTML = '<div id="biggerImage"><iframe id="frameImage" src="'+fileLocation+'main/popupImage.php?image='+src+'"></iframe></div>';
}

function editDiv(type)
{
	var div = document.getElementById('editDiv');
	if(type == 'true')
	{
		div.style.display = 'block';
	}
	else
	{
		div.style.display = 'none';
		div.innerHTML = '';
	}
}

function deleteRecord(page, type, id)
{
	var result = confirm('Are you sure you want to delete '+type+' with id = '+id);
	if(result)
	{
		processAjaxPost('editOutput', '../admin/'+page, 'ajaxId='+id);
	}
}

function greyout(div, type)
{
	if(type == 'true')
	{
		var greyDiv = document.getElementById(div);
		//changed to handel the bloody flash
		//var shade = document.createElement('div');
		var shade = document.createElement('iframe');
		shade.style.backgroundColor = "#7A7A7A";
		
		//get the page width and height
	    	var width='100%';
	    	var height='100%';
		
		shade.style.height = width;
		shade.style.width = height;
  
		shade.style.opacity= .7;                      
		shade.style.MozOpacity= .7;                   
		shade.style.filter='alpha(opacity='+70+')';
		
		shade.style.zIndex = 99;
		shade.style.position = "fixed";
		shade.style.top = "0px";
		shade.style.left = "0px";
		shade.id= "shade";
		shade.style.display='block';
		greyDiv.appendChild(shade);
		//document.getElementById(div).innerHTML = '<div id="shade"></div>'
		
	}
	else
	{
		//jumps out of the iframe to the window then gets the parent and finds the div(that contains the iframe) to empty
		var iframe = window.parent.document.getElementById(div).innerHTML = '';
	}
}

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 processAjax(div, page)
{
	var xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
	    {
			if(xmlHttp.status == 200)
			{
				document.getElementById(div).innerHTML = xmlHttp.responseText; 
			}
		}
	}
	xmlHttp.open("get",page, true);
	xmlHttp.send(null);
}

function processAjaxAppened(div, page)
{
	var xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
	    {
			if(xmlHttp.status == 200)
			{
				var contents = document.getElementById(div).innerHTML;
				document.getElementById(div).innerHTML = contents + ' ' + xmlHttp.responseText; 
			}
		}
	}
	xmlHttp.open("get",page, true);
	xmlHttp.send(null);
}

function processAjaxPost(div, page, variables)
{
	//alert('div '+div+' page '+page+' var '+variables);
	var xmlHttp = GetXmlHttpObject();
	
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
	    {
			if(xmlHttp.status == 200)
			{
				document.getElementById(div).innerHTML = xmlHttp.responseText; 
			}
		}
	}
	xmlHttp.open("post",page, true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", variables.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(variables);
}

function processAjaxRedirect(div, page, target)
{
	var xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
	    {
			if(xmlHttp.status == 200)
			{
				if(xmlHttp.responseText != 'SUCCESS')
				{
					document.getElementById(div).innerHTML = xmlHttp.responseText; 
				}
				else
				{
					window.location = target;
				}
			}
		}
	}
	xmlHttp.open("get",page, true);
	xmlHttp.send(null);
}

function processAjaxPostRedirect(div, page, variables, target)
{
	var xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
	    {
			if(xmlHttp.status == 200)
			{
				if(xmlHttp.responseText != 'SUCCESS')
				{
					document.getElementById(div).innerHTML = xmlHttp.responseText; 
				}
				else
				{
					window.location = target;
				}
			}
		}
	}
	xmlHttp.open("post",page, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", variables.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(variables);
	
}

//handel hashes in the url for episode content
window.onload= function()
{
    handleHistory(); 
}

function handleHistory()
{
  if ( window.location.hash != '')
  {
		var expectedHash = window.location.hash;
		var hashVal = expectedHash.substring(1);
		if(hashVal.indexOf("displayepisodeDetails") >= 0)
		{
			var ele = document.getElementById(hashVal.replace("display", ""));
			var link = document.getElementById(hashVal);
			//then show this div
			ele.style.display = "block";
			//alert("hide");
			link.innerHTML = '-';
		}
		else if(hashVal.indexOf("displayepisodeContent") >= 0)
		{
			var ele = document.getElementById(hashVal.replace("display", ""));
			var seriesData = hashVal.replace("displayepisodeContent", 'episodeDetails').split('-');
			var series = document.getElementById(seriesData[0]);
			//alert(series);
			var link = document.getElementById(hashVal);
			//then show this div
			series.style.display = "block";
			ele.style.display = "block";			
			//alert("hide");
			link.innerHTML = '-';
		}
		else
		{
			//alert(hashVal);
		}	
  }
}

function numbers(e)
{
	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	
	if((keynum < 58) && (keynum > 47) || (keynum == 13))
	{
		return true;
	}
	else if(keynum == 8)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function postcode(e)
{
	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	
	if((keynum < 58) && (keynum > 47))
	{
		return true;
	}
	else if((keynum < 123) && (keynum > 96) || (keynum < 91) && (keynum > 64) || (keynum == 8) || (keynum == 32) || (keynum == 13))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function email(e)
{
	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	
	if((keynum < 123) && (keynum > 96) || (keynum < 91) && (keynum > 64) || (keynum == 8) || (keynum == 64) || (keynum == 46) || (keynum == 13))
	{
		return true;
	}
	else if((keynum < 58) && (keynum > 47) || (keynum = 45) || (keynum == 95))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function letter(e)
{
	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	
	if((keynum < 123) && (keynum > 96) || (keynum < 91) && (keynum > 64) || (keynum == 8) || (keynum == 32) || (keynum == 13))
	{
		return true;
	}
	else
	{
		//alert(keynum);
		return false;
	}
}

function password(e)
{
	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	
	// # " ' ; & 
	if((keynum == 35) || (keynum == 38) || (keynum == 34) || (keynum == 39) || (keynum == 59))
	{
		return false;
	}
	else if(keynum == 8)
	{
		return true;
	}
	else
	{
		return true;
	}
}

function passwordLogin(e)
{
	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	
	// # " ' ; & 
	if((keynum == 35) || (keynum == 38) || (keynum == 34) || (keynum == 39) || (keynum == 59))
	{
		return false;
	}
	else if(keynum == 8)
	{
		return true;
	}
	else if(keynum == 13)
	{
		processAjaxPostRedirect('login', '../register/login.php', 'usernameLogin='+document.getElementById('usernameLogin').value+'&passwordLogin='+document.getElementById('passwordLogin').value, '/');
	}
	else
	{
		return true;
	}
}


function ratingColour(number, type)
{
	var i = 1;
	var num = number;
	num++;
	
	if(type == 'in')
	{
		while(i < num)
		{
			document.getElementById('rate'+i).style.background = "url('../images/main/starRating.png') 0 -2px no-repeat";
			i++;
		}
	}
	else
	{
		while(i < num)
		{
			document.getElementById('rate'+i).style.background = "url('../images/main/starRating.png') 0 -25px no-repeat";
			i++;
		}
	}
}

function toggleEpisodes(div)
{
	var ele = document.getElementById(div);
	var link = document.getElementById('display'+div);
	
	if(ele)
	{
		if(ele.style.display == "block")
		{
			//then hide this div
			ele.style.display = "none";
			//alert("hide");
			link.innerHTML = '+';
		}
		else
		{
			//display this div
			//alert("show");
			ele.style.display = "block";
			link.innerHTML = '-';
		}		
	}
}

function ampersand(el)
{
	var ele = document.getElementById(el);
	if(ele)
	{
		var str = ele.value;
		//alert(str);
		var amper = /&/g;
		
		if(str.indexOf("&") != -1)
		{
			str = str.replace(amper, "-AMPERSAND-");
			//alert(str);
			ele.value = str;
		}
	}
}

function blankText(ele)
{
	var box = document.getElementById(ele);
	
	if(box)
	{
		if(box.value == "Enter your search term...")
		{
			box.value = "";
		}
	}
}

var bannerTimer;

function autoBanner(num, page)
{
	x = num;
	x++;
	//if(x > 10)
	if(x > 5)
	{
		x = 0;
	}
	
	//if(num == 10)
	if(num == 5)
	{
		num = 0;
		x = 1;
	}
	//bannerTimer = setTimeout("processAjaxPost('bannerContainer', '../includes/banner.php', 'bannerNum="+num+"&page="+page+"'); autoBanner("+x+", '"+page+"');" , 25000);
	//reduced from 25000 to 2000
	bannerTimer = setTimeout("processAjaxPost('bannerContainer', '../includes/newsBanner.php', 'bannerNum="+num+"&page="+page+"'); autoBanner("+x+", '"+page+"');" , 3000);
}

function stopAutoBanner()
{
	clearTimeout(bannerTimer);
}

function schedView()
{
	var div = document.getElementById('schedWrapper');
	var link = document.getElementById('shedView');
	
	//show full height
	if(div.style.height != '100%')
	{
		div.style.height='100%';
		link.innerHTML = 'View Less';
	}
	else
	{
		div.style.height='400px';
		link.innerHTML = 'View More';
	}
}

function futureSchedule()
{
	var div = document.getElementById('scheduleFull');
	var link = document.getElementById('futureSchedule');
	var linkOld = document.getElementById('olderSchedule');
	
	var left = div.style.left;
	left = left.replace("px", "");
	if(left == '0')
	{		
		div.style.left = '-600px';
		linkOld.style.display = 'inline';
	}
	
	if((left == '') || (left == '-600'))
	{
		div.style.left = '-1000px';
		link.style.display = 'none';
	}
}

function olderSchedule()
{
	var div = document.getElementById('scheduleFull');
	var link = document.getElementById('futureSchedule');
	var linkOld = document.getElementById('olderSchedule');
	
	var left = div.style.left;
	left = left.replace("px", "");
	if(left == '-1000')
	{		
		div.style.left = '-600px';
		link.style.display = 'inline';
	}
	
	if((left == '') || (left == '-600'))
	{
		div.style.left = '0px';
		linkOld.style.display = 'none';
	}
}

function getTvDbUpdate(tvdbid)
{
	greyout('greyOut', 'true'); 
	editDiv('true');
	document.getElementById('editDiv').innerHTML = 'Updating<br /><img src="/images/main/ajax-loader.gif" alt="Updating Data.."/>';
	var xmlHttp = GetXmlHttpObject();
	
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
	    {
			if(xmlHttp.status == 200)
			{
				document.getElementById('editDiv').innerHTML = xmlHttp.responseText; 
			}
		}
	}
	xmlHttp.open("post",'../includes/tvdbShowUpdate.php', true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", tvdbid.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send('tvdbid='+tvdbid);
}

function dynamicSearch()
{
	displayDynamicSearch();
	var searchString = document.getElementById('searchBox').value;
	var searchBox = document.getElementById('search');
	var dynamicSearchWrapper = document.getElementById('dynamicSearchWrapper');
	dynamicSearchWrapper.innerHTML = 'Searching<br /><img src="/images/main/ajax-loader.gif" alt="Loading Data.."/>';
	var xmlHttp = GetXmlHttpObject();
	
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
	    {
			if(xmlHttp.status == 200)
			{
				dynamicSearchWrapper.innerHTML = xmlHttp.responseText; 
			}
		}
	}
	xmlHttp.open("post",'../includes/dynamicSearch.php', true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", searchString.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send('search='+searchString);
}

function displayDynamicSearch()
{
	var dynamicSearchWrapper = document.getElementById('dynamicSearchWrapper');
	if(dynamicSearchWrapper != null)
	{
		dynamicSearchWrapper.style.display='block';
	}
}

function openDic()
{
	var dic = new Array();
	//this would have to run an ajax call to server file
	return dic;
}
	
function passwordStrength(element, Div, pos)
{
	var password = document.getElementById(element).value;
	var c = password.length;
	var n = 0;
	var lL = 0;
	var lU = 0;
	var s = 0;
	var x;
	var y;
	//run the dictionary function to open a file and return the whole dictionary
	//var dic = openDic();
	var dic = new Array('the', 'and', 'password', 'testing', 'test', 'hello');
	var easy = new Array('qwerty', '12345678910');
	var lettersUp = new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
	var lettersLow = new Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
	var numbers = new Array('0','1','2','3','4','5','6','7','8','9');
	var specials = new Array(' ','!','£','$','%','^','&','*','(',')','-','_','+','=','[', ']', '{', '}','#','@','~',';',':',',','.','<','>','/','\\','?','|','`','¬','"','\'','');
	var passArray = new Array();
	
	passArray = password.split('');
	
	//check characters in password
	for(x in passArray)
	{
		//check against lettersLow
		for(y in lettersLow)
		{
			if(passArray[x] == lettersLow[y])
			{
				lL++;
			}
		}
		//check against lettersUp
		for(y in lettersUp)
		{
			if(passArray[x] == lettersUp[y])
			{
				lU++;
			}
		}
		//check against numbers
		for(y in numbers)
		{
			if(passArray[x] == numbers[y])
			{
				n++;
			}
		}
		//check against specials
		for(y in specials)
		{
			if(passArray[x] == specials[y])
			{
				s++;
			}
		}
	}
	
	//check for all characters no longer used can interfere with return
	//if((s + n + lL + lU) < c)
	//{
		//alert("ERROR " +); //please do not use spaces :" + s + " s " + n + " n " + lL + " lL " + lU + " lU " + c);
	//}
	
	//check for the password in the dictionary
	for(x in dic)
	{
		if(dic[x] == password)
		{
			c = 0;
		}
	}
	
	//check for the password in the easy list 
	for(x in easy)
	{
		if(easy[x] == password)
		{
			c = 0;
		}
	}
	
	if(c > 15)
	{
		//if they have used lL, lU, S and N green if three or two yellow if less then red
		if((lL != 0) && (lU != 0) && (n != 0) && (s != 0))
		{
			code = 100;
		}
		else if((((lL != 0) && (lU != 0)) && (n != 0)) || (((lL != 0) && (s != 0)) && (n != 0)) || (((lL != 0) && (lU != 0)) && (s != 0)) || ((n != 0) && ((s != 0) && (lU != 0))))
		{
		    code = 70;
		}
		else if(((lL != 0) && (lU != 0)) || ((n != 0) && (s != 0)) || ((lL != 0) && (s != 0)) || ((n != 0) && (lL != 0)) || ((n != 0) && (lU != 0)) || ((s != 0) && (lU != 0)))
		{
			code = 50;
		}
		else if((lL != 0) || (lU != 0) || (n != 0) || (s != 0))
		{
		    code = 30;
		}
		else
		{
			code = 10
		}
	}
	else if(c > 10)
	{
		//if they have used lL, lU, S and N green if three yellow or two orange if less then red
		if((lL != 0) && (lU != 0) && (n != 0) && (s != 0))
		{
			code = 70;
		}
		else if((((lL != 0) && (lU != 0)) && (n != 0)) || (((lL != 0) && (s != 0)) && (n != 0)) || (((lL != 0) && (lU != 0)) && (s != 0)) || ((n != 0) && ((s != 0) && (lU != 0))))
		{
		    code = 50;
		}
		else if(((lL != 0) && (lU != 0)) || ((n != 0) && (s != 0)) || ((lL != 0) && (s != 0)) || ((n != 0) && (lL != 0)) || ((n != 0) && (lU != 0)) || ((s != 0) && (lU != 0)))
		{
			code = 30;
		}
		else
		{
			code = 10;
		}
	}
	else if(c > 7)
	{
		//if they have used lL, lU, S and N green if three or two yellow if less then red
		if((lL != 0) && (lU != 0) && (n != 0) && (s != 0))
		{
			code = 50;
		}
		else if(((lL != 0) && (lU != 0)) || ((n != 0) && (s != 0)) || ((lL != 0) && (s != 0)) || ((n != 0) && (lL != 0)) || ((n != 0) && (lU != 0)) || ((s != 0) && (lU != 0)))
		{
			code = 30;
		}
		else
		{
			code = 10;
		}
	}
	else if(c == 0)
	{
	    code = 0;
	}
	else
	{
		code = c+5;
	}

	setDivColour(code, pos);
}

function setDivColour(code, pos)
{
	var divToStyleA = document.getElementById('passA'+pos);
	var divToStyleB = document.getElementById('passB'+pos);
	var divToStyleC = document.getElementById('passC'+pos);
	var divToStyleD = document.getElementById('passD'+pos);
	var divToStyleE = document.getElementById('passE'+pos);
	
	divToStyleE.style.backgroundColor = "#ffffff";
	divToStyleD.style.backgroundColor = "#ffffff";
	divToStyleC.style.backgroundColor = "#ffffff";
	divToStyleB.style.backgroundColor = "#ffffff";
	divToStyleA.style.backgroundColor = "#ffffff";
	
	//only for the best passwords create a red, orange, yellow, green and blue blocks
	if(code > 80)
	{
		document.getElementById('pStrength'+pos).style.display = "block";
		divToStyleE.style.backgroundColor = "#0080FF";
		divToStyleD.style.backgroundColor = "#00ff00";
		divToStyleC.style.backgroundColor = "#ffff00";
		divToStyleB.style.backgroundColor = "#ff8000";
		divToStyleA.style.backgroundColor = "#ff0000";
	}
	//create a red, orange, yellow and green blocks
	else if(code > 60)
	{
		document.getElementById('pStrength'+pos).style.display = "block";
		divToStyleD.style.backgroundColor = "#00ff00";
		divToStyleC.style.backgroundColor = "#ffff00";
		divToStyleB.style.backgroundColor = "#ff8000";
		divToStyleA.style.backgroundColor = "#ff0000";
	}
	//create a red, orange and yellow blocks
	else if(code > 40)
	{
		document.getElementById('pStrength'+pos).style.display = "block";
		divToStyleC.style.backgroundColor = "#ffff00";
		divToStyleB.style.backgroundColor = "#ff8000";
		divToStyleA.style.backgroundColor = "#ff0000";
	}
	//create a red and orange block
	else if(code > 20)
	{
		document.getElementById('pStrength'+pos).style.display = "block";
		divToStyleB.style.backgroundColor = "#ff8000";
		divToStyleA.style.backgroundColor = "#ff0000";
	}
	//remove the strength checker
	else if(code == 0)
	{
	    document.getElementById('pStrength'+pos).style.display = "none";
		divToStyleA.style.backgroundColor = "#ffffff";
	}
	//set the starting div to red
	else
	{
		document.getElementById('pStrength'+pos).style.display = "block";
		divToStyleA.style.backgroundColor = "#ff0000";
	}
}

//this functions checks that the passwords match
function passSame(first, second, pos)
{
	var password1 = document.getElementById(first).value;
	var password2 = document.getElementById(second).value;
	
	if((password1 == password2) && ((password1 != '') && (password2 != '')))
	{
		document.getElementById('passMatch'+pos).style.display = "inline";
		document.getElementById('passMatch'+pos).style.backgroundColor = "#ffffff";
		document.getElementById('passMatch'+pos).innerHTML = '<img width="15px height="15px" src="../images/main/tick.gif" alt="an image of a blue tick." title="Passwords match">';
	}
	else
	{
		document.getElementById('passMatch'+pos).style.display = "inline";
		document.getElementById('passMatch'+pos).style.backgroundColor = "#ffffff";
		document.getElementById('passMatch'+pos).innerHTML = '<img width="15px height="15px" "src="../images/main/cross.gif" alt="an image of a red x." title="Pbannerasswords do not match">';
	}
}
