//Check operating system version.
var strUserAgent = navigator.userAgent;
var isWin = (strUserAgent.indexOf("Win") != -1);
var isMac = (strUserAgent.indexOf("Mac") != -1);

//Check browser version.
var strAppName = navigator.appName;
var isIE = (strAppName == "Microsoft Internet Explorer");
var isNav = (strAppName == "Netscape");
var isSafari = (strUserAgent.indexOf("Safari") != -1);

//Check for browser/OS combinations.
var isMacIE = isMac && isIE

//Output OS/Browser/etc. information.
//window.alert("strUserAgent = " + strUserAgent + "\nisWin = " + isWin + "\nisMac = " + isMac + "\n\nstrAppName = " + strAppName + "\nisIE = " + isIE + "\nisNav = " + isNav);

//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------

//Not all of the standard array functions are implemented in all browsers. However, new 
//methods can be added through the prototype property, with a few well chosen instructions.
// -- Standard functions

// Array.concat() - Join two arrays
if( typeof Array.prototype.concat==='undefined' ) {
 Array.prototype.concat = function( a ) {
  for( var i = 0, b = this.copy(); i<a.length; i++ ) {
   b[b.length] = a[i];
  }
  return b;
  };
}

// Array.copy() - Copy an array
if( typeof Array.prototype.copy==='undefined' ) {
 Array.prototype.copy = function() {
  var a = [], i = this.length;
  while( i-- ) {
   a[i] = typeof this[i].copy!=='undefined' ? this[i].copy() : this[i];
  }
  return a;
 };
}

// Array.pop() - Remove and return the last element of an array
if( typeof Array.prototype.pop==='undefined' ) {
 Array.prototype.pop = function() {
  var b = this[this.length-1];
  this.length--;
  return b;
 };
}

// Array.push() - Add an element to the end of an array, return the new length
if( typeof Array.prototype.push==='undefined' ) {
 Array.prototype.push = function() {
  for( var i = 0, b = this.length, a = arguments, l = a.length; i<l; i++ ) {
   this[b+i] = a[i];
  }
  return this.length;
 };
}

// Array.shift() - Remove and return the first element
if( typeof Array.prototype.shift==='undefined' ) {
 Array.prototype.shift = function() {
  for( var i = 0, b = this[0], l = this.length-1; i<l; i++ ) {
   this[i] = this[i+1];
  }
  this.length--;
  return b;
 };
}

// Array.slice() - Copy and return several elements
if( typeof Array.prototype.slice==='undefined' ) {
 Array.prototype.slice = function( a, c ) {
  var i, l = this.length, r = [];
  if( !c ) { c = l; }
  if( c<0 ) { c = l + c; }
  if( a<0 ) { a = l - a; }
  if( c<a ) { i = a; a = c; c = i; }
  for( i = 0; i < c - a; i++ ) { r[i] = this[a+i]; }
  return r;
 };
}

// Array.splice() - Remove or replace several elements and return any deleted elements
if( typeof Array.prototype.splice==='undefined' ) {
 Array.prototype.splice = function( a, c ) {
  var i = 0, e = arguments, d = this.copy(), f = a, l = this.length;
  if( !c ) { c = l - a; }
  for( i; i < e.length - 2; i++ ) { this[a + i] = e[i + 2]; }
  for( a; a < l - c; a++ ) { this[a + e.length - 2] = d[a - c]; }
  this.length -= c - e.length + 2;
  return d.slice( f, f + c );
 };
}

// Array.unshift() - Add an element to the beginning of an array
if( typeof Array.prototype.unshift==='undefined' ) {
 Array.prototype.unshift = function() {
  this.reverse();
  var a = arguments, i = a.length;
  while(i--) { this.push(a[i]); }
  this.reverse();
  return this.length;
 };
}

// -- 4umi additional functions

// Array.forEach( function ) - Apply a function to each element
Array.prototype.forEach = function( f ) {
 var i = this.length, j, l = this.length;
 for( i=0; i<l; i++ ) { if( ( j = this[i] ) ) { f( j ); } }
};

// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
Array.prototype.indexOf = function( v, b, s ) {
 for( var i = +b || 0, l = this.length; i < l; i++ ) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// Array.insert( index, value ) - Insert value at index, without overwriting existing keys
Array.prototype.insert = function( i, v ) {
 if( i>=0 ) {
  var a = this.slice(), b = a.splice( i );
  a[i] = v;
  return a.concat( b );
 }
};

// Array.lastIndexOf( value, begin, strict ) - Return index of the last element that matches value
Array.prototype.lastIndexOf = function( v, b, s ) {
 b = +b || 0;
 var i = this.length; while(i-->b) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// Array.random( range ) - Return a random element, optionally up to or from range
Array.prototype.random = function( r ) {
 var i = 0, l = this.length;
 if( !r ) { r = this.length; }
 else if( r > 0 ) { r = r % l; }
 else { i = r; r = l + r % l; }
 return this[ Math.floor( r * Math.random() - i ) ];
};

// Array.shuffle( deep ) - Randomly interchange elements
Array.prototype.shuffle = function( b ) {
 var i = this.length, j, t;
 while( i ) {
  j = Math.floor( ( i-- ) * Math.random() );
  t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
  this[i] = this[j];
  this[j] = t;
 }
 return this;
};

// Array.unique( strict ) - Remove duplicate values
Array.prototype.unique = function( b ) {
 var a = [], i, l = this.length;
 for( i=0; i<l; i++ ) {
  if( a.indexOf( this[i], 0, b ) < 0 ) { a.push( this[i] ); }
 }
 return a;
};

// Array.walk() - Change each value according to a callback function
Array.prototype.walk = function( f ) {
 var a = [], i = this.length;
 while(i--) { a.push( f( this[i] ) ); }
 return a.reverse();
};

//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------

//-------------------------------------------------------------------
// isBlank(value)
//   Returns true if value only contains spaces
//-------------------------------------------------------------------
function isBlank(val)
{
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
		}
	return true;
}

//-------------------------------------------------------------------
// isDigit(value)
//   Returns true if value is a 1-character digit
//-------------------------------------------------------------------
function isDigit(num)
{
	if (num.length>1){return false;}
	var string="1234567890";
	if (string.indexOf(num)!=-1){return true;}
	return false;
}

//-------------------------------------------------------------------
// isInteger(value)
//   Returns true if value contains all digits
//-------------------------------------------------------------------
function isInteger(val)
{
	if (isBlank(val)){return false;}
	for(var i=0;i<val.length;i++){
		if(!isDigit(val.charAt(i))){return false;}
		}
	return true;
}

//-------------------------------------------------------------------
// isNumeric(value)
//   Returns true if value contains a positive float value
//-------------------------------------------------------------------
function isNumeric(val){return(parseFloat(val,10)==(val*1));}

//-------------------------------------------------------------------
// isSpecialChar(character)
//   Returns true if it's a special character
//-------------------------------------------------------------------
function isSpecialChar(val)
{
	inputStr = val.toString();
	var  intAsc = inputStr.charCodeAt(0);
	if (((intAsc >=33) && (intAsc <= 47)) || ((intAsc >=58) && (intAsc <= 64)) || ((intAsc >=91) && (intAsc <= 96)) || ((intAsc >=123) && (intAsc <= 126)))
	{
		return true;
	}
	return false;
}

//-------------------------------------------------------------------
// isValidDate(int year, int month, int day)
//   Returns true if the date is valid
//-------------------------------------------------------------------
function isValidDate(year, month, day)
{
	var dteDate;
	//javascript months start at 0 (0-11 instead of 1-12)
	month = month - 1;

	//set up a Date object based on the day, month and year arguments
	dteDate=new Date(year,month,day);

	/*
	Javascript Dates are a little too forgiving and will change the date to a reasonable guess if it's invalid. 
	'We'll use this to our advantage by creating the date object and then comparing it to the details we put it. 
	'If the Date object is different, then it must have been an invalid date to start with...
	*/

	//alert(dteDate);
	return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}

//-------------------------------------------------------------------
// ignoreSpaces(string)
//   Returns string without spaces.
//-------------------------------------------------------------------
function ignoreSpaces(string)
{
	var temp = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	temp += splitstring[i];
	return temp;
}

//-------------------------------------------------------------------
// trimSpaces(string)
//   Returns string that has spaces removed at the beginning and end.
//-------------------------------------------------------------------
function trimSpaces(string)
{
	var temp = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	{
		alert(i + ". [" + splitstring[i] + "]")
		//temp += splitstring[i];
		if ((i == 0) && (splitstring[i] != ""))
			temp += splitstring[i];
			//temp += " ";
	}
	return temp;
}

//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------

//Switches image source.
function switchImage(element, on, off)
{
	if(element.src == on.src)
		element.src = off.src;
	else
		element.src = on.src;
}

//Switches class.
function switchTopRowClass(td, on, off)
{
	var element = document.getElementById(td);
	if(element.className == on)
		element.className = off;
	else
		element.className = on;
}

//Shows a hidden <div> tag or hides a visible <div> tag.
function doShow(div)
{
	var element = document.getElementById(div);
	
	if(element)
	{
		if (element.style.display == 'none' ) 
		{
			element.style.display = '';
		} else 
		{
			element.style.display = 'none';	
		}
	}
}

//Shows a <div> tag.
function doShowOnly(div)
{
	var element = document.getElementById(div);
	element.style.display = '';
}

//Hides a <div> tag.
function doHideOnly(div)
{
	var element = document.getElementById(div);
	element.style.display = 'none';
}

//Enables (grays out) a <div> tag.
function doEnableOnly(div)
{
	var element = document.getElementById(div);
	element.className = "";
}

//Disables (un-grays out) a <div> tag.
function doDisableOnly(div)
{
	var element = document.getElementById(div);
	element.className = "steps_off";
}

function EnterClicked(LinkToClick)
{
	if((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) 
	{
		ClickButton(event,LinkToClick);
		return(false);
	}
}

function ClickButton(e, LinkToClick) 
{ 
	var i = 0;

	while (i < document.forms(0).elements.length) 
	{
		if (document.forms(0).elements(i).name.indexOf(LinkToClick) > -1) 
		{
			document.forms(0).elements(i).click(); 
			return (true); 
		} 
		i++; 
	} 

	i=0; 

	while (i < document.links.length) 
	{
		if (document.links.item(i).id.toString().indexOf(LinkToClick) > -1) 
		{
			document.links.item(i).click(); 
			return (true); 
		} 
		i++; 
	} 
	return (false); 
} 

//Posts a form.
function formPost (myForm, actionUrl, queryStr)
{
	var redirectUrl = "";
	if(queryStr.length <= 0)
	{
		redirectUrl = actionUrl;
	}
	else
	{
		redirectUrl = actionUrl + "?" + queryStr;
	}
	myForm.action = redirectUrl;
	//Defaults target="_self" in case formPostTarget() set it to something else.
	myForm.target = "_self";
	myForm.submit();
}

//Posts a form with a specific browser target.
function formPostTarget (myForm, actionUrl, queryStr, target)
{
	var redirectUrl = "";
	if(queryStr.length <= 0)
	{
		redirectUrl = actionUrl;
	}
	else
	{
		redirectUrl = actionUrl + "?" + queryStr;
	}
	myForm.action = redirectUrl;
	myForm.target = target;
	myForm.submit();
}

//Changes a combo box selection depending on what is typed into a text box.
function changeSelection(txtVal, comboBox)
{
	//var comboBox = document.all?eval("document.all."+ comboName):document.getElementById(comboName);
	if(comboBox && comboBox.options)
	{
		for( i = 0; i < comboBox.options.length; i++)
		{
			if(comboBox.options[i].text.toUpperCase().substr(0, txtVal.length) == txtVal.toUpperCase())
			{	
				comboBox.options.selectedIndex = i;
				break;			
			}
		}
	}
	return true;
}

//Dynamically creates a new form field.
function createFormField (myForm, inputType, inputName, inputId, inputValue)
{
	var input;
	if (document.createElement && (input = document.createElement('input')))
	{
		input.type = inputType; 
		input.name = inputName; 
		input.id = inputId;
		input.value = inputValue;
		myForm.appendChild(input);
	}
}

//Dynamically writes HTML code inside any element.
function writeInnerHtml(divTag, strHtml)
{
	var obj = document.getElementById(divTag);
	obj.innerHTML = obj.innerHTML + strHtml;
}

//Dynamically creates an HTML tag.
function writeInnerHidden(divTag, ctrlName, ctrlVal)
{
	var obj = document.getElementById(divTag);
	obj.innerHTML = obj.innerHTML + "<input type='hidden' name='" + ctrlName + "' value='" + ctrlVal + "'/>";
}

//Dynamically overwrites an HTML tag.
function overwriteInnerHidden(divTag, ctrlName, ctrlVal)
{
	var obj = document.getElementById(divTag);
	obj.innerHTML = "<input type='hidden' name='" + ctrlName + "' value='" + ctrlVal + "'/>";
}

//Opens "Important Notes" window.
function windowImportantNotes(url)
{
	window.open(url,"windowImpNotes","toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes");
}

//Opens "Glossary" window.
function windowGlossary(term, lang, glossaryURL)
{
	window.open(glossaryURL + '?TermID=|' + term +'|&Language=' + lang,'Glossary','left=10,top=0,height=350,width=500,status=no,titlebar=no,toolbar=no,menubar=no,location=no');
}

//opens "Tab Help" window.
function windowTabHelp(server, term, lang)
{
	window.open( server + '/MPPF/static/tabhelp.asp?activeTab=2&term=' + term +'&Language=' + lang,'Glossary','left=10,top=0,height=350,width=500,status=no,titlebar=no,toolbar=no,menubar=no,location=no');
}