var exclude = 1;
var agt = navigator.userAgent.toLowerCase();
var win = 0; var mac = 0; var lin = 1;
if (agt.indexOf('win') != -1) { win = 1; lin = 0; }
if (agt.indexOf('mac') != -1) { mac = 1; lin = 0; }
var lnx = 0; if (lin) { lnx = 1; }
var ice = 0;
var ie = 0; var ie4 = 0; var ie5 = 0; var ie6 = 0; var ie7 = 0; var com = 0; var dcm;
var op5 = 0; var op6 = 0; var op7 = 0;
var ns4 = 0; var ns6 = 0; var ns7 = 0; var mz7 = 0; var kde = 0; var saf = 0;
if (typeof navigator.vendor != "undefined" && navigator.vendor == "KDE") {
    var thisKDE = agt;
    var splitKDE = thisKDE.split("konqueror/");
    var aKDE = splitKDE[1].split("; ");
    var KDEn = parseFloat(aKDE[0]);
    if (KDEn >= 2.2) {
        kde = 1;
        ns6 = 1;
        exclude = 0;
    }
}
else if (agt.indexOf('webtv') != -1) { exclude = 1; }
else if (typeof window.opera != "undefined") {
    exclude = 0;
    if (/opera[\/ ][5]/.test(agt)) { op5 = 1; }
    if (/opera[\/ ][6]/.test(agt)) { op6 = 1; }
    if (/opera[\/ ][7-9]/.test(agt)) { op7 = 1; }
}
else if (typeof document.all != "undefined" && !kde) {
    exclude = 0;
    ie = 1;
    if (document.documentElement && typeof document.documentElement.style.maxHeight != "undefined") {
        ie7 = 1;
    }
    else if (typeof document.getElementById != "undefined") {
        ie5 = 1;
        if (agt.indexOf("msie 6") != -1) {
            ie6 = 1;
            dcm = document.compatMode;
            if (dcm != "BackCompat") { com = 1; }
        }
    }
    else { ie4 = 1; }
}
else if (typeof document.getElementById != "undefined") {
    exclude = 0;
    if (agt.indexOf("netscape/6") != -1 || agt.indexOf("netscape6") != -1) { ns6 = 1; }
    else if (agt.indexOf("netscape/7") != -1 || agt.indexOf("netscape7") != -1) { ns6 = 1; ns7 = 1; }
    else if (agt.indexOf("gecko") != -1) { ns6 = 1; mz7 = 1; }
    if (agt.indexOf("safari") != -1 || (typeof document.childNodes != "undefined" && typeof document.all == "undefined" && typeof navigator.taintEnabled == "undefined")) { mz7 = 0; ns6 = 1; saf = 1; }
}
else if ((agt.indexOf('mozilla') != -1) && (parseInt(navigator.appVersion) >= 4)) {
    exclude = 0;
    ns4 = 1;
    if (typeof navigator.mimeTypes['*'] == "undefined") {
        exclude = 1;
        ns4 = 0;
    }
}
if (agt.indexOf('escape') != -1) { exclude = 1; ns4 = 0; }
if (typeof navigator.__ice_version != "undefined") { exclude = 1; ie4 = 0; }

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
   
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

 
function busyPopup(message, width, height, imageFile, backgroundImageFile)
{
    // Set default args if not passed
    var message = (typeof message == 'undefined') ? 'Please wait' : message;
    var width = (typeof width == 'undefined') ? 262 : width;
    var height = (typeof height == 'undefined') ? 112 : height;
    var imageFile = (typeof imageFile == 'undefined') ? './shared/images/Searching_Animation.gif' : imageFile;

    // Determine the bounds of the window
    var screenWidth = 0;
    var screenHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        screenWidth = window.innerWidth;
        screenHeight = window.innerHeight;
        }
    else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        screenWidth = document.documentElement.clientWidth;
        screenHeight = document.documentElement.clientHeight;
        }
    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        screenWidth = document.body.clientWidth;
        screenHeight = document.body.clientHeight;
        } 
    var boxTop = (screenHeight - height) / 2;
    var boxLeft = (screenWidth - width) / 2; 
    
    // Setup DIV for message and animated GIF
    var mybox = document.createElement("div");
    
    var html = '<iframe width="262" height="112" border="0" style="z-index:1; position:absolute; left:'+boxLeft+'px; top:'+boxTop+'px;' + '"></iframe>' +
			'<div id="divBusy" style="z-index:100; ' +
            'font-size:12pt; font-weight:bold; text-align:center; '+
            'position:absolute; left:'+boxLeft+'px; top:'+boxTop+'px;' +
            'width:' + width + 'px; height:' + height + 'px; ';
    //if (typeof backgroundImageFile == 'undefined')
    //    html += 'background-color: #DDDDDD; border:double;';
    //else
    //    html += 'background-image: url(\'' + backgroundImageFile + '\'); background-repeat: no-repeat;';
        
    html += '"><h3>Searching...</h3>' +
            '<img id="imgBusy" src="' + imageFile + '" border="0" width="178" height="37" />' +
            '</div>';
    mybox.innerHTML = html;
    document.body.insertBefore(mybox, document.body.firstChild);
 
    // Some browsers will stop GIF animation on postback. This forces the animation to continue
    //if (ie)
        setTimeout("UpdateImg('imgBusy', '" + imageFile + "');",50);
    }
 
function UpdateImg(ctrl,imgsrc) {
    document.getElementById(ctrl).src = imgsrc;
}

/* Fix IE Background Image Flicker */
if (ie6) {
    try {
        document.execCommand("BackgroundImageCache", false, true);
    } catch (e) { }
}

/* Adds hover style to input buttons for IE6 */
function hover(control, onState) {
    if (ie6) {
        try {
            if (onState)
                control.style.backgroundPosition = '0 -' + control.offsetHeight + 'px';
            else
                control.style.backgroundPosition = '0 0';
        } catch (e) { }
    }
}    
/******************* COOKIE FUNCTIONS *******************************/

// Get a cookie's value  
function Get_Cookie( check_name )
{
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f
	
    for ( i = 0; i < a_all_cookies.length; i++ )
    {
	    // now we'll split apart each name=value pair
	    a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
	    // and trim left/right whitespace while we're at it
	    cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
	    // if the extracted name matches passed check_name
	    if ( cookie_name == check_name )
	    {
		    b_cookie_found = true;
		    // we need to handle case where cookie has no value but exists (no = sign, that is):
		    if ( a_temp_cookie.length > 1 )
		    {
			    cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
		    }
		    // note that in cases where cookie is initialized but no value, null is returned
		    return cookie_value;
		    break;
	    }
	    a_temp_cookie = null;
	    cookie_name = '';
    }
    if ( !b_cookie_found )
    {
	    return null;
    }
}			

// this deletes the cookie when called
function Delete_Cookie( name, path, domain )
{
    if ( Get_Cookie( name ) ) document.cookie = name + "=" +
        ( ( path ) ? ";path=" + path : "") +
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// This sets a cookie. Expires is time in minutes from now
function Set_Cookie( 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
*/
if ( expires )
{
expires = expires * 1000 * 60;      // Minutes
}
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" : "" );
}

/* Expand/Collapse Project Details functions */

var containerIds;

// close all divs in the array, then toggle the top link and all links
function toggleCollapseAll(topLink) {
    for (var i = 0; i < containerIds.length; i++) {
        $('#' + containerIds[i]).toggle($(topLink).hasClass("closed"));
    }
    toggleCollapseLink(topLink);
    toggleCollapseAllLinks(topLink);
}

// collapses div
function toggleCollapse(link, controlIndex) {
    $('#' + containerIds[controlIndex]).toggle("normal", toggleCollapseLink(link));
}

// toggles link's class
function toggleCollapseLink(link) {
    $(link).toggleClass("closed");
    toggleCollapseLinkText(link)
}

// toggle link's text
function toggleCollapseLinkText(link) {
    if ($(link).hasClass("closed")) {
        $(link).html($(link).html().replace("Close", "Open"));
    }
    else {
        $(link).html($(link).html().replace("Open", "Close"));
    }
}

// set class of all links equal to that of Toplink, then calles toggle text function
function toggleCollapseAllLinks(topLink) {
    var topLinkClass = $(topLink).attr("class");
    $(".collapseLinkWrapper:not(.allDetails) > a").each(function() {
        $(this).attr("class", topLinkClass);
        toggleCollapseLinkText($(this));
    });
}


/* PROJECT LIST */

// collapses div by unique class name
function toggleCollapseByUniqueClassName(link, uniqueClassName) {
    $('div.' + uniqueClassName).toggle("normal", toggleCollapseLink(link));
}


// close all divs inside the div w/ class of projectid , then toggle the top link and all links
function toggleCollapseAllByProjectId(topLink, projectId) {
    $("div#" + projectId + " div.projectDetailBlock").each(function() {
        $(this).toggle($(topLink).hasClass("closed"));
    });
    toggleCollapseLink(topLink);
    toggleCollapseAllLinksByProjectId(topLink, projectId);
}

// set class of all links inside projectid equal to that of Toplink, then calles toggle text function
function toggleCollapseAllLinksByProjectId(topLink, projectId) {
    var topLinkClass = $(topLink).attr("class");
    $("div#" + projectId + " .collapseLinkWrapper:not(.allDetails) > a").each(function() {
        $(this).attr("class", topLinkClass);
        toggleCollapseLinkText($(this));
    });
}
