/* this is our javascript file! */
var t;
var isRunning = false;

/* this will HIDE or UNHIDE the div element passed */
function h(did,de) {
	//log("h did=" + did + ", de=" + de);
    ha(de);
    dom = document.getElementById(did);
    dom.style.display = "inline";
	
	//Verify that the parent is shown!!
	dom = document.getElementById(de);
	dom.style.display = "inline";
	
	
    dom = document.getElementById('defaultMessage');
    dom.style.display="none";
}

function ha(de) {
    if (t) {
        clearTimeout(t);
        t=null;
        //log("ha: cleartimeout");
    }
    //log("ha: de=" + de);
    var eles = document.getElementById(de).getElementsByTagName("DIV");
    
    for (var i=0;i<eles.length;i++) {
        if (eles[i].className != "goButton") {
        	//log("ha: display: " + eles[i].id + " - " + eles[i].style.display);
            eles[i].style.display = "none";
        }
    }
    
    dom = document.getElementById('defaultMessage');
    dom.style.display = "inline";
    clearTimeout(t);
    t = null;
    //if (isRunning) isRunning = false;
}

function hw(de) {
	
    if (t) {
        clearTimeout(t);
        t = null;
        //log("hw: cleartimeout");
    }
	//log("hw: t " + t);
    t = window.setTimeout("ha('" + de + "')", 1500);
}

function hh(did) {
	//if (isRunning) return;
	if (t) {
	    clearTimeout(t);
	    t=null;
	}
}


function ch(dom) {
    if (dom.className.indexOf('hoverOn') == -1) {
        dom.className += ' hoverOn';
    } else {
        dom.className = dom.className.replace(new RegExp(" hoverOn"), "");  
    }
}

function log(msg) {
	d = document.getElementById("statusWindow");
	d.innerHTML = "-----------------<br />" + msg + "<br />" + d.innerHTML;
}
/* Functions for Tabs inside the products */

function changeTab(toTab) {
    // Currently we only have 3 tabs.. we will hide those
    tabArray = new Array('overviewContent', 'detailsContent','paContent');
    
    for (i=0; i<tabArray.length;i++) {
        if (tabArray[i].indexOf(toTab) == -1) { 
            d = document.getElementById(tabArray[i]);
            if (d != null) {
                //alert(d.innerHTML);
                d.style.display = "none";
            }
        }
    }
    
    d = document.getElementById(toTab + "Content");
    d.style.display = "inline";

    // Change the tab style
    var eles = document.getElementById("productTabs").getElementsByTagName("LI");
    for (var i=0; i < eles.length;i++) {
        d = eles[i];
        newId = '';
        if (d.id.indexOf(toTab) == -1) { // Not found
            if (d.id.indexOf("-on") != -1) { // Found
                newId = d.id.replace(/-on/, "");
            }
        } else { // Found {
            if (d.id.indexOf("-on") == -1) {// notfound, let's add
                newId = d.id + "-on";
            }
        }
        if (newId == '') newId = d.id;
        d.id = newId;
    }
    // END
}
function changeSearchTab(toTab) {
    tabArray = new Array('searchContent', 'mapContent', 'csContent');
    
    for (i=0; i<tabArray.length;i++) {
        if (tabArray[i].indexOf(toTab) == -1) { 
            d = document.getElementById(tabArray[i]);
            if (d != null) {
                //alert(d.innerHTML);
                d.style.display = "none";
            }
        }
    }
    
    d = document.getElementById(toTab + "Content");
    d.style.display = "inline";

    // Change the tab style
    var eles = document.getElementById("productTabs").getElementsByTagName("LI");
    for (var i=0; i < eles.length;i++) {
        d = eles[i];
        newId = '';
        if (d.id.indexOf(toTab) == -1) { // Not found
            if (d.id.indexOf("-on") != -1) { // Found
                newId = d.id.replace(/-on/, "");
            }
        } else { // Found {
            if (d.id.indexOf("-on") == -1) {// notfound, let's add
                newId = d.id + "-on";
            }
        }
        if (newId == '') newId = d.id;
        d.id = newId;
    }
    // END
}

function changeMainTab(toTab) {
    // Currently we only have 3 tabs.. we will hide those
    tabArray = new Array('productsContent', 'busneedContent','bustypeContent', 'productsServicesBox', 'busneedServicesBox','bustypeServicesBox');
    //alert("TO TAB: " + toTab);
    
    for (i=0; i<tabArray.length;i++) {
        if (tabArray[i].indexOf(toTab) == -1) { 
            d = document.getElementById(tabArray[i]);
            if (d != null) {
                //alert(d.innerHTML);
                d.style.display = "none";
            }
        }
    }
    
    d = document.getElementById(toTab + "Content");
    d.style.display = "inline";
    d = document.getElementById(toTab + "ServicesBox");
    d.style.display = "inline";

    // Change the tab style
    var eles = document.getElementById("mainTabs").getElementsByTagName("LI");
    for (var i=0; i < eles.length;i++) {
        d = eles[i];
        newId = '';
        if (d.id.indexOf(toTab) == -1) { // Not found
            if (d.id.indexOf("-on") != -1) { // Found
                newId = d.id.replace(/-on/, "");
            }
        } else { // Found {
            if (d.id.indexOf("-on") == -1) {// notfound, let's add
                newId = d.id + "-on";
            }
        }
        if (newId == '') newId = d.id;
        d.id = newId;
    }
    // END
}

function changeState(e) {
	if (e.className.indexOf("-over") == -1) {
		// We assume we want to change it
		e.className = e.className + "-over";
	} else {
		// We assume we are leaving
		e.className = e.className.replace(/-over/, "");
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		};
	}
}

/* Icon change for sitemap stuff */
function toggleIcon(iconID) {
	// Not sure how to get prototype to do img src's
	domObj = document.getElementById(iconID);
	
	if (domObj.src.indexOf('plus') != -1) { // Found
		domObj.src = '/mcldimages/images/minus.gif';
	} else {
		domObj.src = '/mcldimages/images/plus.gif';
	}
}

/* for benefits pop up window */
function popupBenefits( id ){
	detailWindow = window.open('/BenefitInfo.do?id=' + id,'productInfo','width=425,height=350,location=0,menubar=0,toolbar=0,status=0,scrollbars=1,directories=0,resizable=1');
	detailWindow.focus();
}

/* for content pop up window */
function popupContent( url ){
	detailWindow = window.open('/ContentPopup/' + url,'productInfo','width=525,height=350,location=0,menubar=0,toolbar=0,status=0,scrollbars=1,directories=0,resizable=1');
	detailWindow.focus();
}

/*** Auto Tab code ***/
var isNetscape = ( navigator.appName.indexOf( "Netscape" ) != -1 && navigator.appVersion.substring(0, 1) < 5);

function autoTab( inputField, fieldLength, fieldEvent )
{
    var keyCode     = ( isNetscape ) ? fieldEvent.which : fieldEvent.keyCode; 
    var filterArray = ( isNetscape ) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];

    if( inputField.value.length >= fieldLength && !containsElement( filterArray, keyCode ) )
    {        
        inputField.value = inputField.value.slice( 0, fieldLength );

        inputField.form[getNextIndex( inputField )].focus();
    }

    function containsElement( tempArray, tempElement )
    {
        var found = false;

        for( var index = 0; !found && index < tempArray.length; index++ )
	{
            if( tempArray[ index ] == tempElement )
	    {
                found = true;
	    }
        }
        
        return( found );
    }

    function getNextIndex( tempField )
    {
        var formIndex = -1;

        // find the elements index value
        for( var index = 0; index < tempField.form.length; index++ )
  	{
            if( tempField.form[index] == tempField )
	    {
                formIndex = index;

                break;
            }
        }

        // check to see if the next index is hidden if it is go to the following one
        for( var index = formIndex + 1; index < tempField.form.length; index++ )
        {
            if( tempField.form[index].type != "hidden" )
            {
                formIndex = index;
                
                break;
            }
        }

        return( formIndex );
    }

    return( true );
}

/**
 * This function will tab from a link in the middle of a form as to
 * "bypass" the link altogether.  Input the previous form field and "event".
 */
function autoTabFromLink( inputField, fieldEvent )
{	
    var keyCode     = ( isNetscape ) ? fieldEvent.which : fieldEvent.keyCode; 
    var filterArray = ( isNetscape ) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
    
    inputField.form[getNextIndex( inputField )+1].focus();

    function getNextIndex( tempField )
    {
        var formIndex = -1;

        // find the elements index value
        for( var index = 0; index < tempField.form.length; index++ )
  	{
            if( tempField.form[index] == tempField )
	    {
                formIndex = index;

                break;
            }
        }

        // check to see if the next index is hidden if it is go to the following one
        for( var index = formIndex + 1; index < tempField.form.length; index++ )
        {
            if( tempField.form[index].type != "hidden" )
            {
                formIndex = index;
                
                break;
            }
        }

        return( formIndex );
    }

    return( true );
}
/*** End Auto Tab code ***/

function popWindowToSize(url, name, width, height)
{
	window.open(url,name,'width='+width+','+'height='+height+',location=0,menubar=0,toolbar=0,status=0,scrollbars=1,directories=0,resizable=1')
}
