var dom1=(document.getElementById)?1:0;
var ie4plus=(document.all)?1:0;
var ns4=(document.layers)?1:0;
var safari=(navigator.appVersion.indexOf("Safari") != -1)?1:0;
var mac=(navigator.appVersion.indexOf("Macintosh") != -1)?1:0;
var lowerUserAgent=navigator.userAgent.toLowerCase();
var opera = (lowerUserAgent.indexOf('opera')!=-1)?true:false;	

function confirmDeletion(delUrl,delMessage){
	if(confirm(delMessage))document.location.href=delUrl;
}

function hideDiv(divid){
	div2hide=getElementRef(divid);
	if(div2hide)if(div2hide.style){
		div2hide.style.display='none';
	}
}
function showDiv(divid){
	div2show=getElementRef(divid);
	if(div2show)if(div2show.style){
		div2show.style.display='block';
	}
}

function act(imgN){
	if(document.images)document[imgN].src=eval(imgN+'over.src');
}
function inact(imgN){
	if(document.images)document[imgN].src=eval(imgN+'off.src');
}
if(document.images){
/*
	var plusImage=new Image();plusImage.src="/img/btnPlus.gif";
	var minusImage=new Image();minusImage.src="/img/btnMinus.gif";
	var gooff=new Image();gooff.src="/img/butt_reddoublearrows_off.gif";
	var goover=new Image();goover.src="/img/butt_reddoublearrows_over.gif";
*/
}
function arrowOver(imgN){
	if(document.images)if(document[imgN])document[imgN].src=goover.src;
}
function arrowOff(imgN){
	if(document.images)if(document[imgN])document[imgN].src=gooff.src;
}
function getElementRef(id){
	if (dom1) return document.getElementById(id);
	else if (ie4plus) return document.all[id];
	else if (ns4) return document.layers[id];
	else return null;
}

function generalOpenWin(url,winName,winFeatures){
	var winName=window.open(url,winName,winFeatures);
	if(window.focus)winName.focus();
}

function createImgObj(id) {
	return eval('document.images["' + id + '"]');
}

function updateFooterLocation(){
	if(document.all)return;
	footer=getElementRef('footer');
	locatorImg=createImgObj('endofcontent');
	//debugInfoDiv=getElementRef('debuginfo');
	if(footer){
		//var computedTop = document.defaultView.getComputedStyle(locatorImg,null).top;
		//alert("computedTop="+computedTop+"\nlocatorImg.y="+locatorImg.y)
		//alert("footer.offsetTop="+footer.offsetTop+"\nlocatorImg.y="+locatorImg.y)
		if(footer.offsetTop < (locatorImg.y)){
			//alert('footer.offsetTop < locatorimg.y');
			footer.style.top=(locatorImg.y + 50) +"px";	
			//alert('footer repositioned')
			str="footer repositioned:\n content y:"+locatorImg.y+"\n footer.style.top="+footer.style.top;
			maintable=getElementRef('maintable');
			if(maintable.style)maintable.style.height=locatorImg.y - 50 + "px";
			if(maintable)str+=" maintable.height="+ maintable.style.height;
			//debugInfoDiv.innerHTML=str;
			//alert(str)
		}
	}
}

function toggleBlock(divid,controlimgid){
	var divtocontrol=getElementRef(divid);
	var imgtocontrol=document.images[controlimgid];
	if(divtocontrol){
		if(divtocontrol.style){
			if(divtocontrol.style.display=='none') {
				divtocontrol.style.display='block';
				if(imgtocontrol){
					imgtocontrol.src=minusImage.src;
					imgtocontrol.alt="-";
				}
				updateFooterLocation();	
			} else {
				divtocontrol.style.display='none';
				if(imgtocontrol){
					imgtocontrol.src=plusImage.src;
					imgtocontrol.alt="+";
				}
			}		
		}
	}
}
function toggleBlockIfNotChecked(divid,controlimgid){
	//like toggleBlock except collapsing not allowed if there is a checked checkbox in target div
	var targetDiv=document.getElementById(divid);
	if(targetDiv){
		var aChecks=targetDiv.getElementsByTagName("input");
		var containsCheckedCheckboxes=false;
		for(n=0;n < aChecks.length;n++){
			if(aChecks[n].type=="checkbox") {
				if(aChecks[n].checked)containsCheckedCheckboxes=true;
			}
		}
		if(targetDiv.style){
			//dont allow toggle (close) if model(s) are checks in the section
			if(targetDiv.style.display=='none' || (targetDiv.style.display=='block' && !containsCheckedCheckboxes)) toggleBlock(divid,controlimgid);
		}
	}
}

function toggleDetails(fid){
	var featuredetailsdiv=getElementRef("fdetails"+fid);
	var featuredetailsimg=document.images["control"+fid];
	if(featuredetailsdiv){
		if(featuredetailsdiv.style){
			if(featuredetailsdiv.style.display=='none') {
				featuredetailsdiv.style.display='block';
				if(featuredetailsimg){
					featuredetailsimg.src=minusImage.src;
					featuredetailsimg.alt="-";
				}
				updateFooterLocation();	
			} else {
				featuredetailsdiv.style.display='none';
				if(featuredetailsimg){
					featuredetailsimg.src=plusImage.src;
					featuredetailsimg.alt="+";
				}
			}
		}
	}
}
function showModels(divName){
	var modelsdiv=getElementRef(divName);
	var modelslink=getElementRef(divName+'link');
	if(modelsdiv){
		if(modelsdiv.style){
			if(modelsdiv.style.display=='none') {
				modelsdiv.style.display='block';
				modelslink.innerHTML="hide models";
			} else {
				modelsdiv.style.display='none';
				modelslink.innerHTML="show models";
			}
		}
	}
}
function showDefinitions(divName){
	var definitionsdiv=getElementRef(divName);
	var definitionslink=getElementRef(divName+'link');
	if(definitionsdiv){
		if(definitionsdiv.style){
			if(definitionsdiv.style.display=='none') {
				definitionsdiv.style.display='block';
				definitionslink.innerHTML="hide definitions";
			} else {
				definitionsdiv.style.display='none';
				definitionslink.innerHTML="show definitions";
			}
		}
	}
}


initalNoteFieldText="(Add optional notes about this update here.  These are only visible within CMS.)";
function removeNoteInitialTxt(){
	var f=(document.modelForm)?document.modelForm:(document.featureForm)?document.featureForm:false;
	if(f){
		oNotedTextField=f.elements["notes"];
		if(oNotedTextField.value==initalNoteFieldText)oNotedTextField.value="";
	}
}				
function restoreNoteInitialTxt(){
	var f=(document.modelForm)?document.modelForm:(document.featureForm)?document.featureForm:false;
	if(f){
		oNotedTextField=f.elements["notes"];
		if(isEmptyOrWhitespace(oNotedTextField.value)) oNotedTextField.value=initalNoteFieldText;
	}
}

Number.prototype.toDecimals=function(n){
    n=(isNaN(n))?
        2:
        n;
    var
        nT=Math.pow(10,n);
    function pad(s){
            s=s||'.';
            return (s.length>n)?
                s:
                pad(s+'0');
    }
    return (isNaN(this))?
        this:
        (new String(
            Math.round(this*nT)/nT
        )).replace(/(\.\d*)?$/,pad);
}

/* Computed Styles */

// Get the computed css property
function getStyle( element, cssRule )
{
  if( document.defaultView && document.defaultView.getComputedStyle )
  {
    var value = document.defaultView.getComputedStyle( element, '' ).getPropertyValue( 
      cssRule.replace( /[A-Z]/g, function( match, char ) 
      { 
        return "-" + char.toLowerCase(); 
      } ) 
    );
  }
  else if ( element.currentStyle ) var value = element.currentStyle[ cssRule ];
  else                             var value = false;
  return value;
}

// Set a style property
function setStyle( element, cssRule, value )
{
  var original = getStyle( element, cssRule );
  if ( !element.styleHistory )            
    element.styleHistory = new Array();
  if ( !element.styleHistory[ cssRule ] ) 
    element.styleHistory[ cssRule ] = new Array();
  element.styleHistory[ cssRule ].push( original );
  element.style[ cssRule ] = value;
  return value;
}

// Undo a style property change
function undoSetStyle( element, cssRule )
{
  if ( 
    this.styleHistory && 
    this.styleHistory[ cssRule ] && this.styleHistory[ cssRule ].length 
  ) {
    var oldValue = this.styleHistory[ cssRule ].pop();
    this.style[ cssRule ] = oldValue;
  }
}

// Revert one or more style properties
function revertStyle( element, cssRules )
{
  var stylesChanged = 0;
  if( element.styleHistory )
  {
    if ( cssRules == '*' ) 
    { 
      cssRules = new Array(); 
      for( var a in element.styleHistory ) cssRules.push( a ); 
    }
    else if ( typeof cssRules != "Array" ) cssRules = Array( cssRules );

    for( var a = 0; a < cssRules.length; a++ )
    {
      var cssRule = cssRules[a];
      if ( 
        element.styleHistory[ cssRules[a] ] && 
        element.styleHistory[ cssRules[a] ][0] 
      ) {
        element.style[ cssRules[a] ] = element.styleHistory[ cssRules[a] ][0];
        element.styleHistory[ cssRules[a] ] = new Array();
        stylesChanged++;
      }
    }
  }
  return stylesChanged;
}

// Add push() and pop() if necessary
if( typeof Array.prototype.push == 'undefined' )
{
  Array.prototype.push = function( element ) { this[this.length] = element; }
}
if( typeof Array.prototype.pop == 'undefined' )
{
  Array.prototype.pop = function () 
  { 
    var last = this[ this.length - 1 ]; this.length--; return last; 
  }
}

//Array Extentions from http://erik.eae.net/playground/arrayextras/

// Mozilla 1.8 has support for indexOf, lastIndexOf, forEach, filter, map, some, every
// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:lastIndexOf
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function (obj, fromIndex) {
		if (fromIndex == null) {
			fromIndex = 0;
		} else if (fromIndex < 0) {
			fromIndex = Math.max(0, this.length + fromIndex);
		}
		for (var i = fromIndex; i < this.length; i++) {
			if (this[i] === obj)
				return i;
		}
		return -1;
	};
}

// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:lastIndexOf
if (!Array.prototype.lastIndexOf) {
	Array.prototype.lastIndexOf = function (obj, fromIndex) {
		if (fromIndex == null) {
			fromIndex = this.length - 1;
		} else if (fromIndex < 0) {
			fromIndex = Math.max(0, this.length + fromIndex);
		}
		for (var i = fromIndex; i >= 0; i--) {
			if (this[i] === obj)
				return i;
		}
		return -1;
	};
}


// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:forEach
if (!Array.prototype.forEach) {
	Array.prototype.forEach = function (f, obj) {
		var l = this.length;	// must be fixed during loop... see docs
		for (var i = 0; i < l; i++) {
			f.call(obj, this[i], i, this);
		}
	};
}

// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter
if (!Array.prototype.filter) {
	Array.prototype.filter = function (f, obj) {
		var l = this.length;	// must be fixed during loop... see docs
		var res = [];
		for (var i = 0; i < l; i++) {
			if (f.call(obj, this[i], i, this)) {
				res.push(this[i]);
			}
		}
		return res;
	};
}

// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:map
if (!Array.prototype.map) {
	Array.prototype.map = function (f, obj) {
		var l = this.length;	// must be fixed during loop... see docs
		var res = [];
		for (var i = 0; i < l; i++) {
			res.push(f.call(obj, this[i], i, this));
		}
		return res;
	};
}

// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:some
if (!Array.prototype.some) {
	Array.prototype.some = function (f, obj) {
		var l = this.length;	// must be fixed during loop... see docs
		for (var i = 0; i < l; i++) {
			if (f.call(obj, this[i], i, this)) {
				return true;
			}
		}
		return false;
	};
}

// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:every
if (!Array.prototype.every) {
	Array.prototype.every = function (f, obj) {
		var l = this.length;	// must be fixed during loop... see docs
		for (var i = 0; i < l; i++) {
			if (!f.call(obj, this[i], i, this)) {
				return false;
			}
		}
		return true;
	};
}

Array.prototype.contains = function (obj) {
	return this.indexOf(obj) != -1;
};

Array.prototype.copy = function (obj) {
	return this.concat();
};

Array.prototype.insertAt = function (obj, i) {
	this.splice(i, 0, obj);
};

Array.prototype.insertBefore = function (obj, obj2) {
	var i = this.indexOf(obj2);
	if (i == -1)
		this.push(obj);
	else
		this.splice(i, 0, obj);
};

Array.prototype.removeAt = function (i) {
	this.splice(i, 1);
};

Array.prototype.remove = function (obj) {
	var i = this.indexOf(obj);
	if (i != -1)
		this.splice(i, 1);
};