// routine that allows "rosrc" attribute to be used in IMG	
// tag, to specify the rollover image						
//															
// 		eg: <img src="b.gif" rosrc="a.gif">					
//															
// Browsers that support DOM (IE5,NS6)						
// ModeZero Ltd 2002										

// temp store for currently rolled-over image
var g_oBaseImgSrc;	

// will be set as the mouseover routine	
//
function roIn() 
{
  g_oBaseImgSrc = this.getAttribute('src');
  this.setAttribute('src',this.getAttribute('rosrc'))
}

// will be set as the mouseout routine	
//
function roOut() 
{
	if (g_oBaseImgSrc != null)
	{
		this.setAttribute('src',g_oBaseImgSrc)
	}
	else
	{
		this.setAttribute('src',this.getAttribute('src'))
	}
	
}
			
// Init the roll over support (call in OnLoad of document)
//
function roinit(bPreload)
{
	// no point if no DOM	
	if (document.getElementById) 
  	{
  		// get all the IMG tags in the doc for iteration
		roAddRolloversToElements( document.getElementsByTagName('img'), bPreload );
		roAddRolloversToElements( document.getElementsByTagName('input'), bPreload ); // for image submit buttons
	}
}

function roAddRolloversToElements( rElements, bPreload )
{
	var oImgPreload;
	// go through each looking for any with 'rosrc'	
	
	for (var i = 0; i < rElements.length; i++) 
	{
	  if (rElements[i].getAttribute('rosrc')) 
	  {
			// preload images if requested
			if (bPreload)
			{
				oImgPreload = new Image();
				oImgPreload.src = rElements[i].getAttribute('rosrc');
			}
				
			rElements[i].onmouseover = roIn
			rElements[i].onmouseout = roOut
	    }
	}
}

// highlight and select content of input field
function mfu_hisel(theField) 
{
  if (theField.value.charAt(0) == ' ') // its a help prompt, so remove it
  {
	theField.value = '';
  }
  else // its user text, so just highlight
  {
	theField.focus();
	theField.select();
  }
}

function mfu_hiselex(theField, sClearText) 
{
	if (theField.value == sClearText)
	{
		theField.value = "";
	}
	else
	{	
		theField.focus();
		theField.select();
	}
}

// clear the input field if it contains default (prompting) text
function mfu_ClearDefaults(sFormName)
{
	// var arg list of field names
	for  (var n = arguments.length-1; n > 0; n--)
	{
		var oEntryField = eval( "document." + sFormName + "." + arguments[n])
		
		if (oEntryField && oEntryField.value.length > 0 && oEntryField.value.charAt(0) == ' ')
		{
			oEntryField.value = "";
		}
	}
}

// clear the input field if it contains default (prompting) text
function mfu_deldef(oField, fieldDefault) 
{
	if (oField.value == fieldDefault)
	{
		oField.value = '';
	}
	return true;
}

function mfu_GetSelectedRadio(oRadioGroup)
{

	var sRc = ""
	var nRadiosCount = oRadioGroup.length

	for (var n = 0; n < nRadiosCount; n++) 
	{
		if (oRadioGroup[n].checked) 
		{
			sRc = oRadioGroup[n].value;
		}
	}
	
	return( sRc );
}


// expand the personal pane area
//
function mfu_LoadAjaxPane( sElementName, sUrl )
{
	new Ajax.Updater($(sElementName), sUrl, {evalScripts:true})
	return(false);
}

function mfu_LoadAjaxPanePost( sElementName, sUrl, sPostBody )
{
	new Ajax.Updater($(sElementName), sUrl, {evalScripts:true,method:'post',postBody:sPostBody})
	return(false);
}

function mfu_InOutFxDefaultBlock(oBlock, sNormalClass,sOnClass)
{
	document.mfuDefaultRolloverBlock		= oBlock;
	document.mfuDefaultRolloverBlockOnClass = sOnClass
	document.mfuDefaultRolloverBlockClass	= sNormalClass
}

// shorthand to CrossFade call	
function mfu_InOutFx(oBlock,bOnClass)
{	
	if (false) /* removed it requested, DDP */
	{
		if (document.mfuDefaultRolloverBlock)
		{		
			if (oBlock != document.mfuDefaultRolloverBlock)
			{
				MZFXCrossFade.BlockFn(document.mfuDefaultRolloverBlock,"FFFFFF","000000", document.mfuDefaultRolloverBlockOnClass,document.mfuDefaultRolloverBlockClass);
				
				document.mfuDefaultRolloverBlock = null;
				document.mfuDefaultRolloverBlockOnClass = null;
				document.mfuDefaultRolloverBlockClass = null;
			}
		}
		else
		{	
			MZFXCrossFade.AddFaderBlock(oBlock,"FFFFFF","000000",oBlock.className , bOnClass ? oBlock.className + "_on" : oBlock.className)
		}
	}
}

//
// use prototype.js to make an Ajax call back for film list		
//

//
// setup the various states of search options (on FilmFinder and Home)
//
function mfu_SetSearchPromptStates()
{
	if (document.formFilmFinder && document.formFilmFinder.keyword2 && document.formFilmFinder.postcode )
	{
		var efKeyword   = document.formFilmFinder.keyword2;				 
		var efPostcode  = document.formFilmFinder.postcode;
						
		var bNeedKeyPrompt		 = (efKeyword.value  == ""||efKeyword.value.charAt(0) ==" ");
		var bNeedPostCodePrompt  = (efPostcode.value == ""||efPostcode.value.charAt(0) ==" ");
						
		switch(mfu_GetSelectedRadio(document.formFilmFinder.periodcode))
		{
			case "incdvd":
				Element.hide("btn_drop_down_film_list"); 
				Element.show("postcode_box");  			
				if (bNeedKeyPrompt)		 efKeyword.value=" Enter a title";
				if (bNeedPostCodePrompt) efPostcode.value="  Enter a town or postcode";
				break;
								
			case "on":
				Element.show("btn_drop_down_film_list"); 
				Element.show("postcode_box");  							  															
				if (bNeedKeyPrompt)		 efKeyword.value=" Enter or select a film title"
				if (bNeedPostCodePrompt) efPostcode.value="  Enter a town or postcode";
				break;
								
			default: // future release "fr"
				Element.show("btn_drop_down_film_list"); 
				Element.hide("postcode_box");  							  															
				if (bNeedKeyPrompt) efKeyword.value=" Enter or select a film title"
		}
	}
}				

//
// global (yuk) to hold the loaded/cached list of films from which the filtered view is built	
// (this dom is populated from Ajax call on first request)										
//
var g_oFilmSearchBarListDom = null;
var g_bBarLoadInProgress    = false;

//
// A film has been selected in the dynamic filtered popup list box, copy selection and hide	
//
function mfu_FilmSelected(oListBox, sEditBoxId)
{
	// get the selected film name	
	var ctrlTextBox = document.getElementById(sEditBoxId);

	if (ctrlTextBox)
	{
		if (oListBox)
		{
			ctrlTextBox.value = oListBox.options[oListBox.selectedIndex].value;
			if ( !IsSafari() )
			{
				// if we continue safari crashes (sometimes), so dont auto-hide the drop box
				// hide the box	
				oListBox.style.display = "none";
			}
			else
			{
				// doing it in "just a moment" seems to be less safari-crash-happy
				setTimeout( function(){oListBox.style.display = "none"} , 10 );			
			}
		}
		else
		{
			alert("listbox not found")
		}
	}
	else
	{
		alert( "Control:" + ctrlTextBox + ", not found" );
	}		
	
	return(true);
}

// some (very little) special code when safari
function IsSafari()
{
	return( navigator.userAgent.indexOf("Safari") >= 0)
}

//
// Show the dynamic filtered list of films in box provided (or hide if no entries)	
//
function mfu_FilmListFilterXml( sListBoxId, oSelectList, sPrefix, oPeriodCodeRadioBtns )
{
	// if we're loading background info, nothing to do here
	if (g_bBarLoadInProgress) return(true)

	// if the period code has not been selected, empty string will cover it
	var sPeriodCode
	sPeriodCode = mfu_GetSelectedRadio( oPeriodCodeRadioBtns )
	
	// Jan08 : changed so "all" means all in cinema and empty (which is all Incl. dvd) doesn't list anything
	if (sPeriodCode == "all") sPeriodCode = "";

	// we need to load this behind the scenes on first time through	
	if (g_oFilmSearchBarListDom == null)
	{
		g_bBarLoadInProgress = true;
		mfu_LoadSearchBar(sListBoxId, oSelectList, sPrefix, oPeriodCodeRadioBtns);
		return(true);
	}

	// clean out the list ready for population	
	oSelectList.options.length = 0;

	
	// as long as something to filter on...
	if (sPrefix.length > 0)
	{			
	  var pattern = "^" + sPrefix + ".";
	  var Flags	  = "i";
	  
	  try
	  {
			var regexp = new RegExp(pattern, Flags);
		}
		catch(e)
		{
			// some input strings like "[" would create an invalid reg exp
			regexp = null;
		}
		
		
		if (regexp != null)
		{
			// look for all film titles that match the starting letters
			//
			var nSelectListIndex = 0;
			
			var nodelist = g_oFilmSearchBarListDom.getElementsByTagName("Film")
			for(var n=0; n < nodelist.length; n++ )
			{
				var sFilmName = nodelist.item(n).firstChild.nodeValue
				if (regexp.test(sFilmName))
				{
					if (sPeriodCode == "" || nodelist.item(n).getAttribute(sPeriodCode) != null)
					{
						oSelectList.options[nSelectListIndex++] = new Option(sFilmName, sFilmName);					
					}
				}
			}		
		}
	}
		
	// so long as something is in the box, show it
	var elemtListBox = document.getElementById(sListBoxId);
	if (elemtListBox)
	{
		if (oSelectList.options.length > 0) elemtListBox.style.display = "block";
		else elemtListBox.style.display = "none";
	}
	
	return(true);
}
	
	
//
// use prototype.js to make an Ajax call back for film list		
//
function mfu_LoadSearchBar(sListBoxId, oSelectList, sPrefix, oPeriodCodeRadioBtns )
{
		var myAjax = new Ajax.Request("flash/ajax_searchbarfilms.asp", 
										{
											method: 'get', 
											onComplete: function(oResponse) 
												{
													g_oFilmSearchBarListDom = oResponse.responseXML.documentElement
													g_bBarLoadInProgress = false;
													if (g_oFilmSearchBarListDom != null)
													{
														mfu_FilmListFilterXml( sListBoxId, oSelectList, sPrefix, oPeriodCodeRadioBtns )
													}
												}
										});		
}

//
// load the search bar, even if it hasn't been requested yet 
//
function mfu_PreloadSearchBar()
{
	if (g_oFilmSearchBarListDom == null)
	{
		var myAjax = new Ajax.Request("flash/ajax_searchbarfilms.asp", 
										{
											method: 'get', 
											onComplete: function(oResponse) 
												{
													g_oFilmSearchBarListDom = oResponse.responseXML.documentElement
												}
										});		
	}
}

// focus has gone from search bar keyword field	
// so long as its not moved to the list, hide the list
function mfu_KeywordBlur()
{
	setTimeout( "if (!document.formFilmFinder.filmslist.IsInFocus) { document.formFilmFinder.filmslist.IsInFocus = false; Element.hide('filmslist') }", 200 );
}

//
// MZFX function to fade an image to given opacity level
//
var MZFadeTo = 
{
	Final: function( oImg, nFadeStep, nFinalOpacity )
	{
		// if not started, or fading the other way, then start our one	
		if (!oImg.FadeStep || oImg.FadeStep != nFadeStep)
		{	
			// if dont know, assume not opaque	
			if (!oImg.CurrentOpacity) oImg.CurrentOpacity = 100;
			
			oImg.FadeStep			= nFadeStep;		
			oImg.FadeFinalOpacity	= nFinalOpacity;
			
			MZFadeTo.callbackFadeStep(oImg);
		}		
	},

	callbackFadeStep: function (oImg)
	{		
		if ((oImg.FadeStep < 0 && oImg.CurrentOpacity > oImg.FadeFinalOpacity) ||
		    (oImg.FadeStep > 0 && oImg.CurrentOpacity < oImg.FadeFinalOpacity) )
		{
			oImg.CurrentOpacity += 	oImg.FadeStep
		
			oImg.style.filter="alpha(opacity="+oImg.CurrentOpacity+")"	// for IE		
			oImg.style.opacity = oImg.CurrentOpacity/100;				// for others	
					
			setTimeout( function() { MZFadeTo.callbackFadeStep(oImg) }, 10 )
		}
	}
}

// open an offers terms and conditions information
function mfu_OpenOfferTC( nOfferId )
{
	var PopUp = window.open( "offer-terms-conditions.asp?id=" + nOfferId,	
							 "MyFilmsOffersTsAndCs", 
							 "status=0,toolbar=0,menubar=0,directories=0,resizable=1,scrollbars=1,width=900,height=600")
	PopUp.focus();	
	return false;
}

// make all boxes of the same class name the same height (as the tallest)
function mfu_EqualizeBoxHeight( sEqualToTargetName )
{
	var nlDivs = document.getElementsByTagName("div")
	var nMaxHeight = 0;
	var sEqualToName
	
	for(var n=0; n < nlDivs.length; n++)
	{	
		sEqualToName = nlDivs[n].getAttribute('eqheight')
		if (sEqualToName && sEqualToName == sEqualToTargetName)
		{
			if (nlDivs[n].offsetHeight > nMaxHeight) nMaxHeight = nlDivs[n].offsetHeight; 
		}
	}

	for(var n=0; n < nlDivs.length; n++)
	{	
		sEqualToName = nlDivs[n].getAttribute('eqheight')
		if (sEqualToName && sEqualToName == sEqualToTargetName)
		{
			nlDivs[n].style.height = (nMaxHeight)+"px";
			
			// check that we are the right height (padding might mean we need an adjustment)
			if (nlDivs[n].offsetHeight > nMaxHeight) 
			{
				nlDivs[n].style.height = (nMaxHeight - (nlDivs[n].offsetHeight - nMaxHeight)) + 'px';
			}			
		}
	}
}

//
// what was the overall response to our ajax backend call ?
//
function mfu_CheckFlashcallStatus( domStatusResponse )
{
	var sRc = "";
	
	// get the xml from server
	var docResponse = domStatusResponse.responseXML.documentElement
	if (docResponse)
	{
		// check theres a status node
		var nodeStatus = docResponse.getElementsByTagName("Status")
		if (nodeStatus != null && nodeStatus.length > 0)
		{
			// if its no ok, return why information
			var sStatusCode = nodeStatus[0].getAttribute("Code")
			if (sStatusCode == null)
			{
				sRc = "Error:Unexpected response from server"
			}
			else
			{
				if (sStatusCode != "Ok")
				{	
					sRc = sStatusCode + ":" + nodeStatus[0].childNodes[0].nodeValue;
				}
			}				
		}
		else
		{
			sRc = "Error:No response from server"
		}
	}
	
	return(sRc);
}

// popup window utils
// global function 
function mfu_MZPIPClose( sPaneId )
{
	if (sPaneId == "mw_popinwindow" || sPaneId == "mw_popinregister")
	{
		MZPopInPane.popOut()
	}
	else
	{
		Element.hide(sPaneId)
	}
}

function mfu_Rate(sId, bIsGood)
{
	document.ForumRateEntry.ratingid.value = sId;
	document.ForumRateEntry.rating.value = bIsGood?1:-1;
	document.ForumRateEntry.submit();
	return false;
}

/*
Form field Limiter script- By Dynamic Drive
For full source code and more DHTML scripts, visit http://www.dynamicdrive.com
This credit MUST stay intact for use

----- STARTS -----
*/

function restrictinput(maxlength,e,placeholder)
{
	var ns6=document.getElementById&&!document.all
	if (window.event&&event.srcElement.value.length>=maxlength)
	{
		return false
	}
	else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength)
	{
		var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
		if (pressedkey.test(String.fromCharCode(e.which)))
		{
			e.stopPropagation()
		}
	}
}

function countlimit(maxlength,e,placeholder)
{
	var ns6=document.getElementById&&!document.all
	var theform=eval(placeholder)
	var lengthleft=maxlength-theform.value.length
	var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
	
	if (window.event||e.target&&e.target==eval(placeholder))
	{
		if (lengthleft<0)
		{
			theform.value=theform.value.substring(0,maxlength)
			lengthleft = 0
		}
		placeholderobj.innerHTML=lengthleft
	}
}


function mfu_displaylimit(thename, theid, thelimit)
{
	var ns6=document.getElementById&&!document.all
	var theform=theid!=""? document.getElementById(theid) : thename
	
	/*
	var limit_text='<div class="charcount"><b><span id="'+theform.toString()+'">'+thelimit+'</span></b> out of ' + thelimit + ' characters left</div>'
	
	if (document.all||ns6)
	{
		document.write(limit_text)
	}
	*/
	
	if (document.all)
	{
		eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
		eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
	}
	else if (ns6)
	{
		document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
		document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true); 
	}
}

/*
----- DD Script : ENDS -----
*/
