/*
	functions.js 
	javascript code for featuresexec.com		
	Russell Michell 2003-2005, Adam Palmer 2006
*/

// Define some screen resoloutions:
var is_640480 = ((screen.width == 640) && (screen.height == 480));
var is_800600 = ((screen.width == 800) && (screen.height == 600));
var is_1024768 = ((screen.width == 1024) && (screen.height == 768));
var is_1152864 = ((screen.width == 1152) && (screen.height == 864));
var is_1280960 = ((screen.width == 1280) && (screen.height == 960));
var is_12801024 = ((screen.width == 1280) && (screen.height == 1024));

// void checkAll(checkBox,checkPrefix) Check/Uncheck Checkboxes:
// Arg1: string checkBox the name of the checkbox:
// Arg2: string checkPrefix the prefix-name given to the array of checkboxes: (Note - must be quoted i.e. 'name')
function checkAll(checkBox,checkPrefix) 
{
    var elem = checkBox.form.elements;
    for(var i=0; i < elem.length; i++)
    {
		if((elem[i].type == 'checkbox') && (elem[i].name.substr(0,checkPrefix.length) == checkPrefix))
		{
			elem[i].checked = checkBox.checked;
		}
    }
}

function checkAllPlus(checkBox,checkPrefix,checkId) 
{
    var elem = checkBox.form.elements;
    for(var i=0; i < elem.length; i++)
    {
		if((elem[i].type == 'checkbox') && (elem[i].name.substr(0,checkPrefix.length) == checkPrefix) && (elem[i].id == checkId))
		{
			elem[i].checked = checkBox.checked;
		}
    }
}

// void selectAll(FormName,SelectBoxID) Check/Uncheck Checkboxes:
// Arg1: string FormName the name of the given form:
// Arg2: string SelectBoxID the ID given to the <select> menu:
function selectAll(FormName,SelectBoxID)
{
	var obj = document.getElementById(SelectBoxID);
	for(i=0; i<(obj.length); i++)
	{
		if(obj.options[i].selected == true)
		{
			obj.options[i].selected = false;
		}
		else
		{
			obj.options[i].selected = true;
		}
    }
}

// void openSizedWin(url,name,tb,st,sb,rs,w,h):
function openSizedWin(url,name,tb,st,sb,rs,w,h)
{
	window.open(url,name,'toolbar='+tb+',status='+st+',scrollbars='+sb+',resizable='+rs+',width='+w+',height='+h);
}

// redirect to specified URL from a popup and focus to redirected window:
function close_and_redir(url)
{
	window.opener.location.href = url;
	window.opener.self.focus();
}

function getSelectIndx()
{
		// If there's a value of the download format <select> menu available - get & pass with the URL:
	var format = document.plcDownload.format.options[document.plcDownload.format.selectedIndex].value;
}

function r_u_sure(msg)
{
	var is_confirmed = confirm(msg);
    if (is_confirmed) {
        return true;
    }
	else{
		return false;
	}
}

/*
	Functions modified from examples at umm I've forgotton.com by Russell Michell 2005 russ@dwpub.com | russ@theruss.com
	1: Take an initial value of document.forms[0].is_reload (a hidden form field in the first form)
	2: Call handleBackButton()+checkForBackButton() from onload() in <body> (but only from specific listings pages)
	3: If document.forms[0].is_reload is modified to = 2 by the handleBackButton() function, reload the page,
	to show the padlock icons when re-visiting a publicatons listing page subsequent to a login.
*/
var x="1";
var isBack;
function handleBackButton()
{
   isBack = (x != document.forms[0].is_reload.value);
   document.forms[0].is_reload.value=2;
   document.forms[0].is_reload.value=2;
}
function isBackButtonUsed()
{
	return isBack;
}
function checkForBackButton()
{
	// An array of acceptable page-names to use this function on/in:
	var pages = new Array;
	pages[0] = "/search/catsearch.php";
	pages[1] = "/search/mainsearch.php";
	pages[2] = "/publications/atoz.php";
	pages[3] = "/publications/list_latest_updated.php";

	for(i=0;i<=pages.length;i++)
	{
		if(isBackButtonUsed() && pages[i] == window.location.pathname)
		{
			// refresh only if current page is a listings page as defined in 'regex':
			window.location = window.location;
			break;
		}
	}
}

function go2_URLCountry(object)
{
	window.location.href = '/index.php?countries='+object.options[object.selectedIndex].value;
}

/*
'Dynamically' display some instructions on Press Lists Express. 
Allow users to turn off the display and set a cookie never to see it again
*/

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * expires  Expiration date of the cookie
 */
function setCookie(name,value,expires)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expires);
	document.cookie=name+ "=" +escape(value)+";expires="+exdate.toGMTString()+";path=/";
}


/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

// Show hide tabbed-divs onclick:
function showHideBlock(pass,fail1,fail2,fail3) 
{
	// Only execute these functions in /express/search.php:
	var regex = new RegExp('[(express)|(express2)]\/search','gi');
	if(regex.exec(window.location))
	{
		//var divs = document.getElementById(fail1);
		var tabArray = new Array(4)
		tabArray[0]='ple-block-01';
		tabArray[1]='ple-block-02';
		tabArray[2]='ple-block-03';
		tabArray[3]='ple-block-04';

		for(i=0;i<tabArray.length;i++)
		{
			if(tabArray[i].match(pass))
			{
				// Show/Hide the various info blocks:
				document.getElementById(pass).className='hide-n';
			}
			else 
			{
				// Show/Hide the various info blocks:
				document.getElementById(fail1).className='hide-y';
				document.getElementById(fail2).className='hide-y';
				document.getElementById(fail3).className='hide-y';
			}
		}
	}
}

/*
// Show hide tabbed-divs onclick:
function showHideBlockPLE(pass,fail1,fail2,fail3) 
{
	// Only execute these functions in /express/search.php:
	var regex = new RegExp('[(express)|(express2)]\/search','gi');
	if(regex.exec(window.location))
	{
		//var divs = document.getElementById(fail1);
		var tabArray = new Array(4)
		tabArray[0]='ple-block-01';
		tabArray[1]='ple-block-02';
		tabArray[2]='ple-block-03';
		tabArray[3]='ple-block-04';

		for(i=0;i<tabArray.length;i++)
		{
			if(tabArray[i].match(pass))
			{
				// Show/Hide the various info blocks:
				document.getElementById(pass).className='hide-n';
			}
			else 
			{
				// Show/Hide the various info blocks:
				document.getElementById(fail1).className='hide-y';
				document.getElementById(fail2).className='hide-y';
				document.getElementById(fail3).className='hide-y';
			}
		}
	}
}
*/

// Show hide tabs onclick:
function showHideTab(pass,fail1,fail2) 
{
	// Only execute these functions in /express/search.php:
	var regex = new RegExp('[(express)|(express2)]\/search','gi');
	if(regex.exec(window.location))
	{
		//var asss = document.getElementsByTagName('a');
		var tabArray = new Array(3)
		tabArray[0]='ple-tab-01';
		tabArray[1]='ple-tab-02';
		tabArray[2]='ple-tab-03';
		for(i=0;i<tabArray.length;i++)
		{
			if(tabArray[i].match(pass))
			{
				// Show/Hide the various info blocks:
				document.getElementById(pass).className='tab-bg-on';
			}
			else 
			{
				// Show/Hide the various info blocks:
				document.getElementById(fail1).className='tab-bg-off';
				document.getElementById(fail2).className='tab-bg-off';
			}
		}
	}
}

function sar(url)
{
	window.opener.location.href=url;
	window.opener.focus();
	return false;
}

// Function limitOpts(): Russell Michell 2005 | russ@dwpub.com | russ@theruss.com
// Restrict the number of options a user can make from a <select> menu dependant upon type.
// It presumes the menu has 2 types of <option> (defined by differentiating between something peculiar to each value) 
// So that we can restrict xx selections of type 1 and xx selections of the other type - independantly of each other:
// Arg1: str menuID the id of the <select> menu.
// Arg2: str flag the string that differentiates a <select> menu option heading as in Category Groups in FE.
// Arg3: int MaxType 1 the max no. heading options allowed to be selected.
// Arg4: int MaxType 2 the max no. other options allowed to be selected.
// Arg5: str msg The message to display if MaxType 1 and/or MaxType 2 is exceeded.
function limitOpts(menuID,flag,MaxType1,MaxType2,msg)
{
	var ele = document.getElementById(menuID);
	var regex_diff = new RegExp(flag,'gi');
	var j=0;
	var k=0;
	for(i=0;i<=ele.length;i++)
	{
		// This will check if MaxType1 has been reached:
		if(regex_diff.exec(ele[i].value))
		{
			// Check if it's been selected:
			if(ele[i].selected == true)
			{
				j++;
			}
		}
		// This will check if MaxType2 has been reached:
		else
		{
			// Check if it's been selected:
			if(ele[i].selected == true)
			{
				k++;
			}
		}
		// Has MaxType1 or MaxType2 been reached?
		if(j > MaxType1)
		{
			ele[i].selected = false;
			return window.alert(msg);
		}
		if(k > MaxType2)
		{
			ele[i].selected = false;
			return window.alert(msg);
		}
	}
}


function hide_item(itemid) {
	var item = document.getElementById(itemid);

	if(item) {
		item.style.display = "none";
	}
}


/* Writing Flash demos through java script so they display properly in new update for IE -Adam */
function DisplayFlash(Dwidth,Dheight,Dfile)
{  
  document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width='"+Dwidth+"' height='"+Dheight+"' ID='Demo'>\n");
  document.write("<param name='movie' value='"+Dfile+".swf'>\n");
  document.write("<param name='quality' value='high'>\n");
  document.write("<param name='menu' value='false'>\n");
  document.write("<param name='loop' value='0'>\n");
  document.write("<embed src='"+Dfile+".swf' width='"+Dwidth+"' height='"+Dheight+"' loop='0' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' menu='false'></embed>\n");
  document.write("</object>\n");
}


// This function deletes a cookie
function delCookie(DWPubSession) {
document.cookie = DWPubSession + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
alert("Your old FeaturesExec cookie has been deleted");
} 


// hide block div
function hideThis(div)
{
	setTimeout(function() {
		$('#'+div).fadeOut('fast');
	}, 1000); // <-- time in milliseconds
}

function emailOther(theForm)
{
	show = document.getElementById('other_input');
	if (theForm.how.value=='Other')
	{
		show.style.display='inline';
		theForm.other_input.value='';
	}
	else
	{
		show.style.display='none';
		theForm.other_input.value='void';
	}
	
	show = document.getElementById('other_label');
	if (theForm.how.value=='Other')
	{
		show.style.display='inline';
		theForm.other_input.value='';
	}

	else
	{
		show.style.display='none';
		theForm.other_input.value='void';
	}
}

// hide or show something
// input - drop down menu id
// trigger - input option that changes on
// element - default show div
// element2 - optional show div (starts hidden)
// Note: div.hide(); or div2.show(); doesn't work in IE
function showHide(input,trigger,element,element2)
{
	var menu = document.getElementById(input);
	var div = document.getElementById(element);
	var div2 = document.getElementById(element2);

	if (menu.value==trigger)
	{
		div.style.display='none';
		div2.style.display='inline';
	}
	else
	{
		div.style.display='inline';
		div2.style.display='none';
	}
		
}

// used in FE+
function wopen(url,wwidth,wheight) 
{
	window.open(url,'_blank','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width='+wwidth+',height='+wheight);
	return false;
}

// get cookie data
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}


// is string empty, php eq empty()
function isEmpty( inputStr ) 
{
	if ( null == inputStr || "" == inputStr ) 
	{ 
		return true; 
	} 
	return false; 
}

// delete array key, like PHPs unset()
function removeKey(arrayName,key)
{
	var x;
	var tmpArray = new Array();
	for(x in arrayName)
	{
		if(x!=key) { tmpArray[x] = arrayName[x]; }
	}
	return tmpArray;
}

// return array key php eq array_search()
function arrKey(code, arrVal)
{
	for ( var i in arrVal )
	{
		//alert( arrVal[i]+" "+i );
		if(code==arrVal[i])
		{
			return i;
		}
	} 
}

function r_u_sure_ple_opts(msg,radioBut)
{
	var ele = document.getElementById(radioBut);
    if(ele.checked == true)
	{
		var is_confirmed = confirm(msg);
		if (is_confirmed) {
			return true;
		}
		else{
			return false;
		}
	}
}
