/* $Id: stdlib.js,v 1.1 2005/04/07 21:01:45 tsakayev Exp $ */

var debug = true;
var selectedRegionMap;
var selectedRegionDiv;
var selectedRegion;
var selectedAction = 'view';
var reportWindow = false;

var imgSet = new Array();
var divSet = new Array();




function getBrowserInfo()
{
    var scr_width = screen.width;
    var scr_height = screen.height;
    var cook_en = navigator.cookieEnabled;
    var OS = navigator.platform
    var browser = navigator.userAgent
        
    if (document.getElementById("taken").value == "false") {
        
        
        document.getElementById("browser").value    = browser;
        document.getElementById("os").value             = OS;
        document.getElementById("scr_height").value     = scr_height;
        document.getElementById("cook_en").value        = cook_en;
        document.getElementById("scr_width").value      = scr_width;
        document.getElementById("taken").value          ='true';
        document.forms.nav_info.submit();
    }
}

function init()
{
	window.status = 'Application initialized.';

	return true;
}



function hsObj(objName)
{
	this.name = objName;
	this.highlighted = false;
	this.selected = false;

	return this;
}



function gethsObj(which)
{
	for (i = 0; i < imgSet.length; i++) {
		if (imgSet[i].name == which) {
			return imgSet[i];
		}
	}

	return null;
}



function gethsDivObj(which)
{
	for (i = 0; i < divSet.length; i++) {
		if (divSet[i].name == which) {
			return divSet[i];
		}
	}

	return;
}



function getSelectedhsDivObj()
{
	for (i = 0; i < divSet.length; i++) {
		if (divSet[i].selected == true) {
			return divSet[i];
		}
	}
}



/**
 *  Hides region summary information div.
 *
 *  @param none
 *
 *  @return none
 *
 *  @author Chad
 */
function hideRegionDivs()
{
	// hide all regionStats divs
	for (i = 0; i < divSet.length; i++) {
		curDiv = document.getElementById(divSet[i].name);
		curDiv.className = 'regionStatsHidden';
	}

	return;

} //-> End function hideRegionDivs.



function selectCategory(which, category, event)
{

	if (!selectedRegion) {
		alert('Please select a region from the map.');
		return false;
	}

	for (i = 0; i < imgSet.length; i++) {
		imgSet[i].selected = false;
		thisName = imgSet[i].name;
		try {
			thisImg = document.getElementById(thisName);
			thisImg.src = "images/icon" + thisName + "-off.gif";
		} catch (err) {
			;
		}
	}

	currenthsObj = gethsObj(which);
	currenthsObj.selected = true;

	image = document.getElementById(which);


	image.src = "images/icon" + which + "-on.gif";
	target = document.getElementById('selectReport');
	if ( category == 'individual') {
		target.src = 'individual_analyst.php?client=' + client
		+ '&category=' + category
		+ '&lookback=' + self.document.lookback.value
		+ '&region=' + selectedRegion.name;
		
	} else if ( category == 'stockcharts') {
		target.src = 'stockcharts.php?client=' + client
		+ '&category=' + category
		+ '&lookback=' + self.document.lookback.value
		+ '&region=' + selectedRegion.name;
	} else {
		target.src = 'select_report.php?client=' + client
		+ '&category=' + category
		+ '&lookback=' + self.document.lookbackPeriod.lookback.value
		+ '&region=' + selectedRegion.name;
	}
	

	clear = document.getElementById('availableDates');
	clear.src = 'available_dates.php';

	return;

} //-> End function select category.



// select (onClick) a region
function selectRegion(which)
{
	clearSelections(); 											//-> "Reset" iframes to their default state.
	currenthsObj = gethsDivObj(which);

	// set all other regions to unselected
	for (i = 0; i < divSet.length; i++) {
		divSet[i].selected=false;
	}

	currenthsObj.selected = true;

	selectedRegion = currenthsObj;

	window.status = 'currenthsObj = ' + currenthsObj.name;
}



function rollIcon(id, direction, e)
{
	currenthsObj = gethsObj(id);
	image = document.getElementById(id);

	if (!e) {
		e = (window.event) ? window.event : window.Event;
	}

	if (e.currentTarget) {
		src = e.currentTarget;
		src.style.cursor = 'pointer';
	} else if (e.srcElement) {
		src = event.srcElement;
		src.style.cursor = 'hand';
	}

	if (currenthsObj.selected == false)
	{
		if (direction=="on")
		{
			image.src = "images/icon" + id + "-on.gif";	
		}
		else
		{
			image.src = 'images/icon' + id + "-off.gif";
		}
	}
}

// changes to highlight image when selecting or mousing over a region
// shows/hides the relevant region stats <DIV>
function rollMap(which)
{
	image = document.getElementById("regionMap");
	
	if (which == 'currentSelection') {
		selectedRegion = getSelectedhsDivObj();

		if (selectedRegion == undefined) {
			image.src = 'images/maps/' + client + '-basemap.gif';
			hideRegionDivs();
			div = document.getElementById('default');
			div.className="regionStatsVisible";
		} else {
			image.src = 'images/maps/' + client + '-' + selectedRegion.name + '.gif';
			hideRegionDivs();
			div = document.getElementById(selectedRegion.name);
			div.className = "regionStatsVisible";
		}

		return;
	}
	
	currenthsObj = gethsDivObj(which);
	div = document.getElementById(which);
	
	// change map image
	if (currenthsObj.selected == false)
	{
		image.src = 'images/maps/' + client + '-' + which + '.gif';
	}
	hideRegionDivs();
	
	// show regionStats div
	div.className="regionStatsVisible";
}



/**
 *  Clears selected report and available date iframes.
 *
 *  @param none
 *
 *  @return none
 *
 *  @author Henry Rivera
 */
function clearSelections()
{
	var d = document;

	/*
	selectReport = d.getElementById('selectReport');
	selectReport.src = 'select_report.php';

	availableDates = d.getElementById('availableDates');
	availableDates.src = 'available_dates.php';
	*/

	return;

} //-> End function clearSelections.



function submitClickAction(source)
{
	checkForm = document.getElementById("clickAction");
	selectedAction = source.value;

	//setcookie();

	/* <debug> */
	window.status = selectedAction;
	/* </debug> */

	return;

} //-> End function submitClickAction.



function selectDate(url, reportID)
{
	var d = window.parent.document;
	//var target;
	//var d = document;


	/* Because the documents being opened by this function are 
	 * pdf or excel documents, a window.location assignment fails due to
	 * an IE bug. --henroc */
	if (selectedAction == 'view') {
		reportWindow = window.open(url, 'viewReport')
		reportWindow.focus();
	} else {
		downloadFolder = d.getElementById('downloadFolder');
		d.getElementById('downloadFolder').src = 'download_folder.php?report=' + reportID;

		//downloadFolder.src = 'download_folder.php?report=' + reportID;
		//downloadFolder.location.reload();
		// do download folder stuff here.
	}
}


