/* -----------------------------------------------------------------------
	Fuel Industries
	Common Javascript Library
----------------------------------------------------------------------- */

/* Browser Detect
============================================================= */

var agt = navigator.userAgent.toLowerCase();
var ie  = (agt.indexOf("msie") != -1);
var ns  = (navigator.appName.indexOf("Netscape") != -1);
var win = ((agt.indexOf("win")!=-1) || (agt.indexOf("32bit")!=-1));
var mac = (agt.indexOf("mac")!=-1);


/* Mouse Over
   loops through all images looking for class="ro"
============================================================= */

function set_rollovers(){
    var path, img;

    if(document.getElementsByTagName)
    {
        var isie = /MSIE ((5\.5)|[6789])/.test(navigator.userAgent) && navigator.platform == "Win32" && !/Opera/.test(navigator.userAgent);
        //var els = document.getElementsByTagName("IMG");
		var els = getElementsByClass(document,'ro','*');
        for(var i=0,j=els.length;i<j;i++)
        {
            if(els[i].className == "ro")
            {
                // preload image
                path = els[i].src;
                img = new Image();
                img.src = path.substr(0,path.length-4) + '_o.' + path.substr(path.length-3,3);

                // attach event handlers
                els[i].onmouseover = function(){
                    var path;
                        path = this.src;
                        this.osrc = path;
                        this.src = path.substr(0,path.length-4) + '_o.' + path.substr(path.length-3,3);

                };
                els[i].onmouseout = function() {
                	if(this.osrc)this.src = this.osrc;
                };
            }
        }
    }
}

/* Pop Up Window Script
============================================================= */

var newWin = null;
function popup(strURL, strHeight, strWidth) {
	var stroptions="";
	stroptions="resizable,scrollbars,status,height="+strHeight+",width="+strWidth;
	
	if (newWin) newWin.close();
	
	newWin = window.open(strURL, 'newWin', stroptions);
	newWin.focus();
}


/* Select Box Navigation Script
============================================================= */

function selectnav(obj){
	destination = obj.options[obj.selectedIndex].value;

	if(destination.lastIndexOf(100) != -1){
		confirmpop('Is there a signed contract for this forcast?', destination);
		return
	}
	
	if(destination){ 
		location.href = destination;
	}

}

/* Better Confirm
============================================================= */

function confirmpop(msg, loc){
	if(confirm(msg)) window.location = loc;
}

/* Skip to Field
============================================================= */

function skiptofield(field){
	$(field).focus();
}

/* Cookies
============================================================= */

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 ) {
	var today = new Date();
	today.setTime( today.getTime() );
	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() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

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';
}

/* Event Handling
============================================================= */

function addEvent( obj, type, fn ) {
   if ( obj.attachEvent ) {
     obj['e'+type+fn] = fn;
     obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
     obj.attachEvent( 'on'+type, obj[type+fn] );
   } else
     obj.addEventListener( type, fn, false );
}

function removeEvent( obj, type, fn ) { 
  if ( obj.detachEvent ) { 
    obj.detachEvent( 'on'+type, obj[type+fn] ); 
    obj[type+fn] = null; 
  } else 
    obj.removeEventListener( type, fn, false ); 
} 

/* Get Elements By Class
============================================================= */

function getElementsByClass(node,searchClass,tag) {
	var classElements = new Array();
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("\\b"+searchClass+"\\b");
	
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/* Toggle
============================================================= */

function toggle(obj) {
	var el = $(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

/* Insert After
============================================================= */

function insertAfter(parent, node, referenceNode) {
	parent.insertBefore(node, referenceNode.nextSibling);
}

/* Prototype function $
============================================================= */

function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

/* Opacity
============================================================= */

function setOpacity(elm, value) {
	var elm = document.getElementById(elm);
	elm.style.opacity = value/10;
	elm.style.filter = 'alpha(opacity=' + value*10 + ')';
}

/* External Links
============================================================= */

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
}


