var serverTransformRequired = false;
var siteRoot = GetSiteRoot();

//- Detect what OS and ServicePack the user has and pad the dialog to accommodate
//- for Windows theming or a possible status bar at the bottom of every dialog. -//
function CheckOS( originalHeight )
{
	//- This is how much larger the dialog needs to be. -//
	var heightOffset = originalHeight;

	//- See if the user is running in themes mode. If the cookie isn't returned, assume they are.
	//- If the result is TRUE, add 5 pixels to the heightOffset. -//
	var isThemed = GetCookie( 'isThemed' );
	isThemed = ( isThemed == '' ? true : eval( isThemed ) );
	heightOffset += isThemed ? 5 : 0;

	//- Get the ServicePack number. -//
	var spVer = window.navigator.appMinorVersion;
	if ( spVer.indexOf( ';SP' ) > -1 )
		spVer = parseInt( spVer.slice( spVer.indexOf( ';SP' ) + 3 ) );

	//- OS Check. -//
	var osVer = window.navigator.userAgent.toLowerCase();
	var browserVer = window.navigator.appVersion.toLowerCase();
	//- Windows XP SP2+ with StatusBar -//
	if ( osVer.indexOf( "nt 5.1" ) > -1 )
	{
	    if( browserVer.indexOf( "msie 6" ) && spVer > 1 )
		    heightOffset += 24;
	}
	//- Windows 2003 SP1+ with StatusBar -//
	else if ( osVer.indexOf( "NT 5.2" ) > -1 && spVer > 0 )
	{
		heightOffset += 24;
	}

	//- Return the final modified value. -//
	return heightOffset;
}

function DisplayAlert( alertMsg, numLines )
{
	var strArray = alertMsg.split( '\n' );
	var newAlert = new Array();
	var i = 0;

	do
	{
		newAlert.push( strArray[i] );
		i++;

		if( newAlert.length == numLines || i == strArray.length )
		{
			newAlert = newAlert.join( '' );
			alert( newAlert + '\n\nAlert #' );

			newAlert = new Array();
		}

	} while( i < strArray.length );

	window.clipboardData.setData( 'TEXT', alertMsg );
}

//- Takes a date string and formats it. -//
function FormatDateTime( date, format )
{
	format = format.toLowerCase();

	var date = new Date( date );
	var retVal = '';

	if ( format == 'date' )
	{
		retVal += date.getMonth() + 1 < 10 ? '0' + ( date.getMonth() + 1 ) : date.getMonth() + 1;
		retVal += '/';
		retVal += date.getDate() < 10 ? '0' + ( date.getDate() ) : date.getDate();
		retVal += '/';
		retVal += date.getFullYear()
	}
	else if ( format == 'hour' )
	{
		retVal = date.getHours();
		if ( retVal == 0 )
			retVal = 12;
		else if ( retVal > 12 )
			retVal -= 12;
	}
	else if ( format == 'minute' )
	{
		retVal = ( date.getMinutes() < 10 ? '0' : '' ) + date.getMinutes();
	}
	else if ( format == 'ampm' )
	{
		retVal = ( date.getHours() < 12 ? 'AM' : 'PM' );
	}

	return retVal;
}

//- Format the value of an input box to the specified precision. -//
function FormatNumber( input, precision )
{
	//- Get the value as a string. -//
	if ( typeof( input ) == 'object' && input.tagName == 'INPUT' )
		var val = input.value;
	else
		var val = String( input );

	//- Remove any currency symbols and commas. -//
	val = val.replace( /[$,]/gi, '' );

	//- Make sure it's a valid number. -//
	if ( val.length > 0 && isNaN( parseFloat( val ) ) )
	{
		alert( 'Please enter a numeric value' );
//		inputBox.focus();
		return;
	}

	//- If it's empty, fill with a zero to start with. -//
	if ( val.length == 0 )
		val = '0';

	//- Now format to the specified precision. -//
	if ( precision == 0 )
		val = parseInt( val );
	else
		val = parseFloat( val ).toFixed( precision );

	//- Set the formatted value back into the field. -//
	if ( typeof( input ) == 'object' && input.tagName == 'INPUT' )
		input.value = val;
	else
		return val;
}

//- Generic CheckKey function for dialogs. Include this file in the dialog and set body.onKeyDown="GenericCheckKey()". -//
function GenericCheckKey()
{
	//- The user pressed Enter, so save and close the form. -//
	if ( event.keyCode == 13 && event.srcElement.tagName != 'BUTTON' && event.srcElement.tagName != 'TEXTAREA' )
	{
		try
		{
			Save();
		}
		catch (e)
		{
			try
			{
				SaveAndClose();
			}
			catch (ex)
			{
			}
		}
	}
	//- The user pressed Esc, so just close the form. -//
	else if ( event.keyCode == 27 )
	{
		if ( typeof( top.winName ) == 'undefined' || top.winName == null || top.winName != 'topWin' )
			window.close();
	}
	//- User pressed Left or Up, so switch to the previous button.-//
	else if ( ( event.keyCode == 37 || event.keyCode == 38 ) && event.srcElement != null && event.srcElement.tagName == 'BUTTON' )
	{
		var btnObject = event.srcElement.previousSibling;
		while ( btnObject != null && btnObject != event.srcElement.parentElement && btnObject.tagName != 'BUTTON' && btnObject.disabled == false )
			btnObject = btnObject.previousSibling;
		if ( btnObject != null && btnObject.tagName == 'BUTTON' && btnObject.disabled == false )
			btnObject.focus();
	}
	//- User pressed Right or Down, so switch to the next button. -//
	else if ( ( event.keyCode == 39 || event.keyCode == 40 ) && event.srcElement != null && event.srcElement.tagName == 'BUTTON' )
	{
		var btnObject = event.srcElement.nextSibling;
		while ( btnObject != null && btnObject != event.srcElement.parentElement && btnObject.tagName != 'BUTTON' && btnObject.disabled == false )
			btnObject = btnObject.nextSibling;
		if ( btnObject != null && btnObject.tagName == 'BUTTON' && btnObject.disabled == false )
			btnObject.focus();
	}
}

// The following function returns a cookie value, given the name of
// the cookie. Notice the use of unescape to decode special characters
// in the cookie value.
function GetCookie( cookieName )
{
	var search = cookieName + '=';
	if ( document.cookie.length > 0 ) // if there are any cookies
	{
		offset = document.cookie.indexOf( search )
		if ( offset != -1 ) // if cookie exists
		{
			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf( ';', offset )
			// set index of end of cookie value
			if ( end == -1 )
				end = document.cookie.length
			return unescape( document.cookie.substring( offset, end ) )
		}
	}

	return '';
}

//- Retrieve the SITE ROOT of the site (removes //localhost/Jon from all code) -//
function GetSiteRoot()
{
	var tempXML = new ActiveXObject( 'MSXML.DOMDocument' );
	tempXML.async = false;
	tempXML.load( '/administration/scripts/nimbus.asmx/QuerySiteRoot' );

	siteRoot = tempXML.text + "/Jon";
	return siteRoot;
}

//- Retrieve the value from a field of an XML DOM. -//
function GetXMLValue( xmlDom, fieldName )
{
	if ( xmlDom == null )
		return '';

	var data = xmlDom.getElementsByTagName( fieldName );

	if ( data.length == 0 )
		return '';
	else
		return data[0].text;
}

/* query a set bit from inside the bitlist */
function query_bit( BW_bit, BW_list )
{
	var BW_t_list;
	if( typeof( BW_list ) == "undefined" )
	{
		if( typeof( bitList ) == null || typeof( bitList ) == "undefined" ) return;
		BW_t_list = bitList;
	}
	else BW_t_list = BW_list;
	return ( BW_t_list & BW_bit );
}

/* remove a set bit from inside the bitlist

	Arguments: BW_Bit:	integer containing bit to remove from bitlist
               BW_list: (optional) list containing bit structure
               if not set function will test for a global variable named bitList and use it

    Return:
    			integer:  modified bitlist */

function remove_bit( BW_bit, BW_list )
{
	var BW_t_list;
	if( typeof( BW_list ) == "undefined" )
	{
		if( typeof( bitList ) == null || typeof( bitList ) == "undefined" ) return;
		BW_t_list = bitList;
	}
	else BW_t_list = BW_list;
	BW_t_list -= BW_bit;
	return BW_t_list;
}

//- Set the value of a specific field in an XML DOM. -//
function SetXMLValue( xmlDOM, fieldName, fieldValue, isCDATA )
{
	var fieldNode = xmlDOM.getElementsByTagName( fieldName );
	if ( fieldNode.length == 0 )
	{
		fieldNode = xmlDOM.createElement( fieldName );
		xmlDOM.getElementsByTagName( 'root' )[0].appendChild( fieldNode );
	}
	else
	{
		fieldNode = fieldNode[0];
		fieldNode.text = '';
	}

	if ( typeof( isCDATA ) == 'undefined' || eval( isCDATA ) )
	{
		var cdo = xmlDOM.createCDATASection( fieldValue );
		fieldNode.appendChild( cdo );
	}
	else
		fieldNode.text = fieldValue;
}

/* Sniffer to test XML version required by user */
function sniffXSLMethod()
{
  	var xmlData = new ActiveXObject( 'MSXML.DOMDocument' );
  	var xmlMask = new ActiveXObject( 'MSXML.DOMDocument' );

  	xmlData.async = false;
  	xmlMask.async = false;

  	xmlData.loadXML( '<?xml version="1.0" encoding="ISO-8859-1"?><root><name>Steve</name></root>' );
  	xmlMask.loadXML( '<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:template match="/root"><xsl:value-of select="name" /></xsl:template></xsl:stylesheet>' );

  	var outData = xmlData.transformNode( xmlMask );
  	if( outData.indexOf( '<xsl:value-of' ) != -1 )
  	{
  	  serverTransformRequired = true;
  	}

	return;
}

function serverSideTransform(xmlQueryString)
{
	var xmlhttp = new ActiveXObject( 'Microsoft.XMLHTTP' );
	xmlhttp.Open( "POST", "/administration/scripts/nimbus.asmx/ServerTransform", false );
	xmlhttp.Send( xmlQueryString );
//	alert( xmlhttp.responseText );
	return xmlhttp.responseText;
}

/* set a bit inside of a bitlist
	Arguments: BW_Bit:	integer containing bit to set inside bitlist
               BW_list: (optional) list containing bit structure
               if not set function will test for a global variable named bitList and use it

    Return:
    			integer:  modified bitlist */

function set_bit( BW_bit, BW_list )
{
	var BW_t_list;
	if( typeof( BW_list ) == "undefined" )
	{
		if( typeof( bitList ) == null || typeof( bitList ) == "undefined" ) return;
		BW_t_list = bitList;
	}
	else BW_t_list = BW_list;
	BW_t_list |= BW_bit;
	return BW_t_list;
}

// Sets cookie values. Expiration date is optional. Notice the use of
// escape to encode special characters (semicolons, commas, spaces)
// in the value string. This function assumes that cookie names do
// not have any special characters.
function SetCookie ( name, value )
{
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = ( argc > 2 ) ? argv[2] : null;
	var path = ( argc > 3) ? argv[3] : null;
	var domain = ( argc > 4 ) ? argv[4] : null;
	var secure = ( argc > 5 ) ? argv[5] : false;
	document.cookie = name + "=" + escape( value ) +
		( ( expires == null ) ? "" : ( "; expires=" + expires.toGMTString() ) ) +
		( ( path == null ) ? "" : ( "; path=" + path ) ) +
		( ( domain == null ) ? "" : ( "; domain=" + domain ) ) +
		( ( secure == true ) ? "; secure" : "" );
}

//- Set the value of a specific field in an XML DOM. -//
function SetXMLValue( xmlDom, fieldName, fieldValue, isCDATA )
{
	//- Make sure the element passed was of nodeType 'document'. -//
	var xmlDoc;
	if ( xmlDom.nodeTypeString == 'document' )
	{
		xmlDoc = xmlDom;
		xmlDom = xmlDoc.documentElement;
	}
	else
		xmlDoc = xmlDom.ownerDocument;

	//- See if the field exists. -//
	var fieldNode = xmlDom.getElementsByTagName( fieldName );
	if ( fieldNode.length == 0 )
	{
		fieldNode = xmlDoc.createElement( fieldName );
		xmlDom.appendChild( fieldNode );
	}
	else
	{
		fieldNode = fieldNode[0];
		fieldNode.text = '';
	}

	//- Now insert the value. Use a CDATA node by default for the value. -//
	if ( typeof( isCDATA ) == 'undefined' || eval( isCDATA ) )
	{
		var cdo = xmlDoc.createCDATASection( fieldValue );
		fieldNode.appendChild( cdo );
	}
	else
		fieldNode.text = fieldValue;
}

function Transform( xmlDom, xslFile )
{
	if( serverTransformRequired )
	{
		var newNode = xmlDom.createNode( 1, "xsl_file", "" );
		newNode.text = xslFile;
		xmlDom.documentElement.appendChild( newNode );
		return serverSideTransform( xmlDom.xml );

	}
	else
	{
		var xmlMask = new ActiveXObject( 'Msxml.DOMDocument' );
		xmlMask.async = false;
		xmlMask.load( xslFile );
		return xmlDomtransformNode( xmlMask );
	}
}

window.get_cookie = GetCookie;