// JavaScript Document

errorArray = Array();

// Function for loading functions  to be excuted on window.load
function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined'){
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined'){
		document.addEventListener('load', fn, false);
	} else if (typeof window.attachEvent != 'undefined'){
		window.attachEvent('onload', fn);
	} else {
		var oldfn = window.onload;
		if(typeof window.onload != 'function'){
			window.onload = fn;
		} else {
			window.onload = function()
			{
				oldfn();
				fn;
			};
		}
	}
}

document.onclick = function(e)
{
	var target = e ? e.target : window.event.srcElement;
	while(target && !/^(a|body)$/i.test(target.nodeName))
	{
		target = target.parentNode;
	}
	
	if(target && target.getAttribute('rel') && target.rel == 'new')
	{
		var external = window.open(target.href);
		return external.closed;
	}
}

function loadFlash(galID) {
	 var so = new SWFObject("ALU_flash/photoGallery.swf", "sotester", "954", "418", "8", "#FFFFFF");
	 so.addParam("quality", "high");
	 so.addParam("scale", "noscale");
	 so.addParam("wmode", "transparent");
	 so.addParam("salign", "lt");
	 so.addVariable("galID", galID);
	 so.write("galleryDisplay");
}

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}


function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function musicOff(){
	var current_date = new Date;
    var cookie_year = current_date.getFullYear ( ) + 1;
    var cookie_month = current_date.getMonth ( );
    var cookie_day = current_date.getDate ( );
    set_cookie ("music", "off", cookie_year, cookie_month, cookie_day, '/');	
}

function musicOn(){
	var current_date = new Date;
    var cookie_year = current_date.getFullYear ( ) + 1;
    var cookie_month = current_date.getMonth ( );
    var cookie_day = current_date.getDate ( );
    set_cookie ("music", "on", cookie_year, cookie_month, cookie_day, '/');	
}

function checkCookie(){
	var x = get_cookie ( "music" );
	document.write(x);
}

function toggleLogin( status ){
	var d = document.getElementById('loginForm');
	var e = document.getElementById('navMemBar');
	if(d){
		if(status == 'show'){
			d.style.display = 'block';
		} else {
			d.style.display = 'none';
		}
	}
	if(e){
		if(status == 'show'){
			e.style.display = 'block';
		} else {
			e.style.display = 'none';
		}
	}
}

function addError(error){
	if(error.length > 0){
		errorArray.push(error);
	}
}

function showError(){
	var errorMsg = '';
	if(errorArray.length > 0){
		errorMsg = "The following errors occurred:\n";
		for(var i = 0; i < errorArray.length; i++){
			errorMsg += errorArray[i]+"\n";
		}
		alert(errorMsg);
		return true;
	} else {
		return true;
	}
}

function showHideField( field, showOnValue, changeField ) 
{ 
    // the name of the other field (same as the selectfield only with "_other" behind it) 
    var name = changeField; 
	

    // get the object of the "other" field 
    if( document.getElementById ) 
    { 
        var other = document.getElementById(name); 
    } 
    else if( document.all ) 
    { 
        var other = document.all[ name ]; 
    } 
    else 
    { 
        var other = null; 
    } 

    // did we got the object of the other field ? 
    if( other ) 
    { 
        // now, show or hide the field 
        other.style.display = ( field.value == showOnValue) ? 'block' : 'none'; 
    } 
    // we could not retrieve the object of the other field.. 
    // display warning message 
    else 
    { 
        alert( 
          'Error, could not display the field for a user ' + 
          'specific value. Please use a newer/better browser...' 
        ); 
    } 
}

								
addLoadListener(showError);
