/*
	JAVASCRIPT FUNCTION INCLUDE
	TerrorFreeCalculator.com - Scottish
	04/18/2008
	River City Studio
*/
//http://www.javascriptobfuscator.com/

var d = window.document;

//////////////////////////////////////
//  GENERIC RETURN ELEMENT FUNCTION //
//////////////////////////////////////

function getE( v ) {
  var d = window.document;
  e = false;
  if ( d.getElementById ) {
    e = d.getElementById( v );
  }
  else if ( d.all ) {
    e = d.all[ v ];
  }
  return e;
}




///////////////////////
// AJAX BASICS       //
///////////////////////

var http = getHTTPObject(); // We create the HTTP Object
var isWorking = false;

function getHTTPObject()
{ //http://www.webpasties.com/xmlHttpRequest/xmlHttpRequest_tutorial_1.html
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
    	try
    	{
    		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    	}
    	catch (e)
    	{
    		try
    		{
    			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		catch (E)
    		{
    			xmlhttp = false;
    		}
    	}
    @else xmlhttp = false;
    @end
    @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
    {
    	try
    	{
    		xmlhttp = new XMLHttpRequest();
    	}
    	catch (e)
    	{
    		xmlhttp = false;
    	}
    }
    return xmlhttp;
}

///////////////////////
// AJAX - THIS SITE  //
///////////////////////

window.document.start=0;
window.document.searchstr = '';
window.document.limit=30;
window.document.nextstart = window.document.limit;
window.document.previousResults = '';

function searchFunds(searchstr,start)
{
	var resList = getE('resList');
	if(!start) start = 0;

	var mutualFundSearchBox = getE('mutualFundSearchBox');
	if(mutualFundSearchBox)
		searchstr = mutualFundSearchBox.value;

	window.document.start= start;
	window.document.searchstr = searchstr;
	window.document.nextstart = (window.document.start + window.document.limit);

	if (!isWorking && http)
	{
		isWorking = true;
		var url="/_svc/fs.php";
		var params = "searchstr="+searchstr+'&start='+start;
		http.open("POST", url , true);
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
		http.onreadystatechange = handleHttpSearchResponse;
		http.send(params);

		if(start == 0)
		{
			resList.innerHTML = '';
			window.document.previousResults = '';
		}
		//http.send(null);
		return false;
	}
	else
		setTimeout("searchFunds('"+searchstr+"',"+start+")",200);
}

function handleHttpSearchResponse()
{
	var resList = getE('resList');
	if (http.readyState == 4)
	{
		isWorking = false;
	    var funds = eval(http.responseText);
	    var colorClass = '';
		resList.innerHTML = window.document.previousResults;
		for(i=0;i<funds.length;i++)
		{
			if(i%2 == 0)
				colorClass = 'green';
			else
				colorClass = 'white';

			if(funds[i].co == 'TFCMORE' )
			{
				resList.innerHTML = resList.innerHTML + '<li class="'+colorClass+
					'" style="height:25px;"><a href="#" onClick="searchFunds(\''+window.document.searchstr+'\','+
					window.document.nextstart+');return false;"><img src="/_img/more.png" alt="Show More Results" border="0" id="moreResults" /></a></li>'+"\n";
			}
			else if(funds[i].co=='NONE')
			{
				resList.innerHTML = '<li class="'+colorClass+'">No funds match your search criteria.</li>'+"\n";
			}
			else
			{
				var thisLine = '<li class="'+colorClass+
					'"><img src="/_img/add.png" alt="Add Fund" onClick="selectFund('+funds[i].id+',\''+
					funds[i].co.replace(/'/,"\\\'")+'\')" />'+
					funds[i].co + '</li>'+"\n";

				resList.innerHTML = resList.innerHTML + thisLine;
				window.document.previousResults = window.document.previousResults + thisLine;
			}
		}
	}
}


window.document.fundsSelected = new Array();
window.document.fundsSelNames = new Array();

function fundArrayToString()
{
	var fundString = getE('fundString');
	fundString.value = '';
	for(i=0;i<window.document.fundsSelected.length;i++)
	{
		if(fundString.value)
			fundString.value = fundString.value + ',';
		fundString.value = fundString.value + window.document.fundsSelected[i];
	}
	//alert(fundString.value);
}

function fundArrayToDisplay()
{
	var selPane = getE('selPane');
	selPane.innerHTML = '';
    //selPane.innerHTML = "\n"+'<ul>'+"\n";
	for(i=0;i<6;i++)
	{
	    if(i%2 == 0)
	    	colorClass = 'white';
	    else
	    	colorClass = 'green';

	    if(window.document.fundsSelected[i])
	    {

	    	selPane.innerHTML = selPane.innerHTML + "<li class='"+colorClass+
	    		"'><img src='/_img/delete.png' alt='Remove Fund' onClick='removeFund("+window.document.fundsSelected[i]+")' />"+
	    		window.document.fundsSelNames[i]+"</li>\n";
	    }
	    else
	    {
	    	selPane.innerHTML = selPane.innerHTML + "<li class='"+colorClass+"'>&nbsp;</li>\n";
	    }
	}
    //selPane.innerHTML = selPane.innerHTML + '</ul>'+"\n";
}

function selectFund(fundid,coname)
{
	var already_present = 0;
	var fundString = getE('fundString');
	for(i=0;i<window.document.fundsSelected.length;i++)
	{
		if(window.document.fundsSelected[i] == fundid)
		{
			already_present = 1;
		}
	}
	if(already_present==0)
	{
		//alert('Before: '+window.document.fundsSelected.length);
		if(window.document.fundsSelected.length < 6)
		{	
			window.document.fundsSelected[window.document.fundsSelected.length]=fundid;
			window.document.fundsSelNames[window.document.fundsSelNames.length]=coname;
		}
		else
			alert('Only 6 funds at a time, please.');
		
		//alert('After: '+window.document.fundsSelected.length);
	}
	//alert(window.document.fundsSelected.toString());
	fundArrayToString();
	fundArrayToDisplay();
}

function removeFund(fundid)
{
	var newFundList = new Array();
	var newFundNames = new Array();
	for(i=0;i<window.document.fundsSelected.length;i++)
	{
		if(window.document.fundsSelected[i] != fundid)
		{
			newFundList[newFundList.length]=window.document.fundsSelected[i];
			newFundNames[newFundNames.length]=window.document.fundsSelNames[i];
		}
	}
	while(window.document.fundsSelected.length)
		trash = window.document.fundsSelected.pop();
	while(window.document.fundsSelNames.length)
		trash = window.document.fundsSelNames.pop();
	//window.document.documentsSelNames = new Array();
	window.document.fundsSelected = newFundList;
	window.document.fundsSelNames = newFundNames;

	fundArrayToString();
	fundArrayToDisplay();
}

function checkTOS()
{
	var buttonTOS = getE('agreeTOS');
	if(buttonTOS.checked)
	{
		thisForm = getE("calculateForm");
		//alert(thisForm.id);
		thisForm.submit();
	}
	else
	{
		alert('Please review and agree to the Terms of Service.');
		return false;
	}

}



