
var ua=navigator.userAgent;

function check_ua() {
    /*
	ua.indexOf("Gecko") returns:
	    52 for Linux Firefox
	    67 for Mac OS X Camino
	    74 for Mac OS X Safari
     */

    var css_mswin_at = 0;
    var css_others_at = 0;

    result = "  Gecko:" + ua.indexOf("Gecko") +
	     ", MSIE 5.6:" + ua.indexOf("MSIE 5.6") + 
	     ", MSIE 5.5:" + ua.indexOf("MSIE 5.5") + 
	     ", MSIE 6:" + ua.indexOf("MSIE 6") + 
	     ", MSIE 7:" + ua.indexOf("MSIE 7");
    /*
	 alert( navigator.appVersion.toLowerCase().indexOf("windows") );
    */
    n = document.styleSheets.length;
    for( i = 0; i < n; i++ ) {
	href = document.styleSheets[i].href;
	pos_mswin  = href.indexOf('-mswin.css');
	pos_others = href.indexOf('-others.css');
	if( pos_mswin != -1 ) {
	    css_mswin_at = i;
	}
	if( pos_others != -1 ) {
	    css_others_at = i;
	}
    }
    if( navigator.appVersion.toLowerCase().indexOf("mac") > 0 ) {
	if( ua.indexOf("Gecko") == -1 ) {
	    document.styleSheets[css_mswin_at].disabled = false;
	    document.styleSheets[css_others_at].disabled = true;
	} else {
	    document.styleSheets[css_mswin_at].disabled = true;
	    document.styleSheets[css_others_at].disabled = false;
	}
	document.styleSheets[css_mswin_at].disabled = true;
	document.styleSheets[css_others_at].disabled = true;
    } else {
	if( ua.indexOf("MSIE 5.6") > 0 || ua.indexOf("MSIE 5.5") > 0 || ua.indexOf("MSIE 6") > 0 || ua.indexOf("MSIE 7") > 0 ) {
	    document.styleSheets[css_mswin_at].disabled = false;
	    document.styleSheets[css_others_at].disabled = true;
	} else {
	    if( ua.indexOf("Gecko") == -1 ) {
		document.styleSheets[css_mswin_at].disabled = false;
		document.styleSheets[css_others_at].disabled = true;
	    } else {
		document.styleSheets[css_mswin_at].disabled = true;
		document.styleSheets[css_others_at].disabled = false;
	    }
	}
    }
}

function Popup( url, windowName, w, h ) {

    w = window.open( url, windowName,
		     'width=' +w+ ', height='+h+',scrollbars=yes,menubar=no,toolbar=no,locationbar=no,status=yes' );
    if( window.focus ) w.focus();
}

function InitTabs(tabs,classname) {

    var item = 'selected_tab';
    var tabname = tabs[0];

    /*  Look for cookie value of "selected_tab"  */
    if (document.cookie.length>0) {
	start=document.cookie.indexOf(item + "=")
	if (start != -1) {
	    start = start + item.length + 1
	    end = document.cookie.indexOf(";",start)
	    if (end == -1)
		end = document.cookie.length
	    tabname = unescape(document.cookie.substring(start,end))
	}
    }

    /*  tabname could be value of "selected_tab" cookie value or tabs[0]  */
    var tabobj = document.getElementsByName(tabname);
    var exists_flag = 1;

    /*  tabname is not found, look for the first one that can be found in tabs list.  */
    if (tabobj.item(0) == null ) {
	exists_flag = 0;
	for( var i = 0; i < tabs.length; i++ ) {
	    tabobj = document.getElementsByName(tabs[i]);
	    if (tabobj.item(0) != null ) {
		exists_flag = 1;
		tabname = tabs[i];
		break;
	    }
	}
    }
    else {
	exists_flag = 0;
	for( var i = 0; i < tabs.length; i++ ) {
	    if (tabname == tabs[i]) {
		exists_flag = 1;
	    }
	}
    }

    /*  If none could be found in tabs list, no turn on or off.  */
    if (exists_flag == 1 ) {
	if( classname == null ) {
	    classname = 'tab-active';
	}
	TurnsOnOffByName(tabname,classname);
    }
}

function TurnsOnOffByName(objname,classname) {
    var tabobj = document.getElementsByName(objname);
    if (tabobj.item(0) != null) {
	tabobj.item(0).className = classname;
    }
}

/*		*/

function ActivateBtnById(id,classname) {
    TurnsOnOffById(id,classname);
}


function TurnsOnTab(tab,tabset,classname) {
  for( var i=0; i<tabset.length; i++ ) {
    cn = ( tabset[i] == tab ) ? classname +'-active' : classname;
    TurnsOnOffById(tabset[i],cn);
  }
}

function TurnsOnBlock(block,blockset) {
  for( var i=0; i<blockset.length; i++ ) {
    t = document.getElementById(blockset[i]);
    t.style.display = ( blockset[i] == block ) ? 'block' : 'none';
  }
}

function TurnsOnOffById(id,classname) {
    var e = document.getElementById(id);
    e.className = classname;
}

/*		*/

function SelectAllCb(name) {
    TurnsAllCbOnOff(name,true);
}

function DisselectAllCb(name) {
    TurnsAllCbOnOff(name,false);
}

function TurnsAllCbOnOff(name,value) {
  var cb = document.getElementsByTagName('input');
  for ( var i=0; i<cb.length; i++ ) {
    if( cb.item(i).getAttribute('type') == 'checkbox' ) {
	pos = cb.item(i).getAttribute('name').indexOf('[');
	if( cb.item(i).getAttribute('name').substring(0,pos) == name ) {
	    cb.item(i).checked = value;
	}
    }
  }
}

function SelectAllCbByName(name) {
  var cb = document.getElementsByName(name);
  for ( var i=0; i<cb.length; i++ ) {
    cb[i].checked = true;
  }
}

function ButtonSubmit(anchor, btn_name, btn_val ) {

    var y = getElementById(anchor);
    var p = y.parentNode;
    var e = document.createElement('input');
    e.name  = btn_name;
    e.value = btn_val;
    e.type  = 'hidden';
    p.insertBefore(e,y);
    document.f.submit();
}

// this function gets the cookie, if it exists
function GetCookie( name ) {
	
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;

    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
	return null;
    }

    if ( start == -1 ) return null;

    var end = document.cookie.indexOf( ";", len );

    if ( end == -1 ) end = document.cookie.length;

    return unescape( document.cookie.substring( len, end ) );
}

function SetCookie( name, value, expires, path, domain, secure ) 
{
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    /*
    if the expires variable is set, make the correct 
    expires time, the current script below will set 
    it for x number of days, to make it for hours, 
    delete * 24, for minutes, delete * 60 * 24
    */

    if ( expires ) {
	expires = expires * 1000 * 60 * 60 * 24;
    }

    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
			( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
			( ( path ) ? ";path=" + path : "" ) + 
			( ( domain ) ? ";domain=" + domain : "" ) +
			( ( secure ) ? ";secure" : "" );
}

// this deletes the cookie when called

function DeleteCookie( name, path, domain ) {

    if ( GetCookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function PostViaButton( scriptname, windowname, windowopt )
{
    document.forms[0].action = scriptname;
    document.forms[0].target = windowname; "_blank"; // Open in a new window
    document.forms[0].submit();                     // Submit the page
    return true;
}

function Mailto( to ) {
    m  = "mailto:?to=" + to;
    location.href = m;
}


/*		*/

function toggle_onoff_block( class_1, class_2, element, btnid, imgid ) {

    btn = document.getElementById(btnid);
    block = document.getElementById(element);
    img = document.getElementById(imgid);

    if( btn.className == class_1 ) {
	btn.className = class_2;
	img.src = "/fyp-archives-alpha/i/triangle.gif";
	// btn.value = "&#187;";

	/*
	 *	Special handling for MSIE: No z-index can be set for "select", so we need to hide it.
	 */
	if( ua.indexOf("Gecko") == -1 ) {
	    aos = document.getElementById('aos-block');
	    if( aos ) 
		aos.style.visibility = "visible";
	}

    } else {
	btn.className = class_1;
	img.src = "/fyp-archives-alpha/i/opentriangle.gif";

	/*
	 *	Special handling for MSIE: No z-index can be set for "select", so we need to hide it.
	 */
	if( ua.indexOf("Gecko") == -1 ) {
	    aos = document.getElementById('aos-block');
	    if( aos ) 
		aos.style.visibility = "hidden";
	}
    }

    if( block.className == 'inactive-block-plain' ) {
	block.className = 'active-block-plain';
    } else {
	block.className = 'inactive-block-plain';
    }
}

function NYearsAway( yr, delta ) {
    var result;
    a = parseInt(yr, 10);
    b = parseInt(delta, 10);
    if ( a == 0 && b < 0 ) {
	result = 0;
    } else {
	result = a + b;
    }
    return result;
}

function SelectedDate(obj, yyyy, mm, dd) {
    obj.value = dd + "/" + mm + "/" + yyyy;
    window.close();
}
