function sort_by( field ){
	var f = document.forms['search_options_form'];
	f.sort_by.value = field;
	f.submit();
}

function date_range( field ){
	var f = document.forms['search_options_form'];
	f.date_option.value = field;
	f.submit();
}

function change_page( page_no ){
	var f = document.forms['search_options_form'];
	f.page_no.value = page_no;
	f.submit();
}

function scroll_nav(){ // IE only
	// If the page has a search
	if ( document.forms.length > 0 ){
		var sd = document.getElementById('scroller');
		var st = sd.scrollTop;
		var nd = document.getElementById('sidebar');
		nd.style.top = st;
		var nb = document.getElementById('sidebarbackground');
		nb.style.top = st;
	}
}

function reformatHTML() // HOMEPAGE
{
	// Open the links in the parent window
	var A = document.getElementsByTagName('A');
	for ( var i = 0; i < A.length; i ++ )
	{
		// If it's not a javascript link
		if ( A[i].href.indexOf('javascript:') == -1 )
		{
		    A[i].href = 'javascript:popup("' + A[i].href + '", 640, 480 );';
		    A[i].target = '_self';
		}
		// If it's an article link
		if ( A[i].className && ( A[i].className == 'articlelink' ))
		{
			// Then change the text
			A[i].innerHTML = '...MORE';
		}
	}
	// Append the date to the end if the title
	var D = document.getElementsByTagName('DIV');
	for ( var i = 0; i < D.length; i ++ ){
		if ( D[i].className == 'date' ){
			var date = reformatDate( D[i].innerHTML.toString() );
		    D[i+1].innerHTML += '<br /><span class="date">' + date + '</span>';
			i ++; // skip the title div
		}
	}
}

function reformatSearchHTML() // PRESS CENTRE
{
	// Open the links in the parent window
	var A = document.getElementsByTagName('A');
	for ( var i = 0; i < A.length; i ++ )
	{
		// If it's not a javascript link
		if ( A[i].href.indexOf('javascript:') == -1 )
		{	        
		    A[i].href = 'javascript:popup("' + A[i].href + '", 656, 480 );';
		    if ( A[i].getAttribute('target') )
		    {
		        A[i].removeAttribute('target');
		    }
		}
		// If we have a class name
		if ( A[i].className )
		{
		    // If it's an article link
		    if ( A[i].className == 'articlelink' )
		    {
			    // Then change the text
			    A[i].innerHTML = 'read full description in a popup window';
		    }
		}
	}
	// Append the date to the end if the title
	var D = document.getElementsByTagName('DIV');
	for ( var i = 0; i < D.length; i ++ )
	{
	    // Append the date
		if ( D[i].className == 'date' )
		{
		    // The date
		    var date = D[i].innerHTML.toString();
		    
		    // Get the title and re-format it
		    var t = D[i+1].innerHTML;
		    t = t.split('Dimplex - ').join('');
		    t += '<br /><span class="date">' + date + '</span>';
		    D[i+1].innerHTML = t;
		    
//		    do { // Recursively replace double spaces with singles until there are none
//		        date = date.split('  ').join(' ');
//	        } while ( date.indexOf('  ') != -1 );
//	        date = date.split(' '); // Split up the string
//	        // Check for a html being in the date - inconsistent bug
//	        if ( date.join('').indexOf('href="/') == -1 ){
//	            D[i+1].innerHTML += '<br /><span class="date">' + date[2] + ' ' + date[3] + ' ' + date[4] + '</span>';
//	        }
		}
	}	
}

function reformatDate( d ){
	d = d.split('\n').join(''); // Remove new lines
	d = d.split('\r').join(''); // Remove carriage returns
	d = ' ' + d; // Ensure it has a preceding space for IE
	var month, day, date;
	var md = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	var mn = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	do { // Recursively replace double spaces with singles until there are none
		d = d.split('  ').join(' ');
	} while ( d.indexOf('  ') != -1 );
	date = d.split(' '); // Split up the string
	day = date[2];
	for ( var i = 0; i < 12; i ++ ){ // months
		if ( date[3] == md[i] ){ // if match
			month = mn[i]; // get month name
			break;
		}
	}
	// return formatted date
	return day + ' ' + month + ' ' + date[4];
}

function popup( win, w, h )
{
  if ( window.pop ){ window.pop.close(); } // close an open one
  pop = window.open( win,'news_popup','toolbar=no,location=no,directories=no,status=yes,scrollbars=yes,resizable=no,copyhistory=no,locationbar=no,width=' + w + ',height=' + h + ',screenX=0,screenY=0,top=0,left=0');
}

window.onload = function()
{
	// If the page has a search
	if ( document.forms.length > 0 )
	{
		// Reformat search html
		reformatSearchHTML();
		// Scroll the nav in IE
		if (document.all){ scroll_nav(); }
	}
	else // Reformat the HTML for the navigation
	{
		// Then swap to the other css 
		if ( document.location.toString().indexOf('localhost') != -1 ) // If local then relative
	    {
	        document.getElementsByTagName('link')[0].href = 'schema/stylesheets/home_news.css';
		}
		else
		{
			document.getElementsByTagName('link')[0].href = '/schema/stylesheets/home_news.css';
		}
		reformatHTML();
	}
}


