http = getHTTPObject();
 
function getHTTPObject()
{
  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;
}

function aum_val(form, fund)
{	
	if(fund.name.substr(0,4) == 'fund')
  {
	  var i = fund.name.substr(4);
		if(fund.value == 0)
	  {
	  	alert('You must select a fund from this row before entering a market value');
	  	//form.fund1aum.value = '0';
			form.elements['fund' + i + 'aum'].value = '0';
	  	return;
	  }  
  }
}

function ror_val(form, fund)
{	
	if(fund.name.substr(0,4) == 'fund')
  {
	  var i = fund.name.substr(4);
		if(fund.value == 0)
	  {
	  	alert('You must select a fund from this row before entering a rate of return');
	  	//form.fund1aum.value = '0';
			form.elements['fund' + i + 'ror'].value = '0.0000';
	  	return;
	  }  
  }
}

function policy_percentage(form, policy, extrafunds)
{	
  var url = "/site/backend.php?op=add";
  //alert(policy.value);
  //alert(policy.name)
	if(policy.name.substr(0,6) == 'policy')
  {
	  var i = policy.name.substr(6);
		if(policy.value != 0)
	  {
    	for (j = 1; j <= extrafunds; j++)
    	{
			 	url += "&num" + j + "=" + form.elements['policy' + j + 'wgt'].value;
			}
		}
		else
		{
	  	alert('You must select a policy from this row before entering a percentage');
			form.elements['policy' + i + 'wgt'].value = '0.00';
	  	return;
	  }  
  }
 	
  http.open("GET", url, true);
  http.onreadystatechange = handleHttpResponsePolicy;
 
  http.send(null);
}
 
function handleHttpResponsePolicy()
{
  if(http.readyState == 4)
	{
    document.step1.policytotalwgt.value = http.responseText;
  }
}
//next 3 functions display the date from the as at date drop list in the text below in step 3
function makeTxt(id,txt)
{
	var obj = document.getElementById(id);
	obj.firstChild?obj.firstChild.data=txt:obj.appendChild(document.createTextNode(txt))
}

function disp_text(form)
{
  var w = form.selectedIndex;
  var selected_text = form.options[w].text;
  if(selected_text == 'Select a date')
	{
		selected_text = '(please select an as at date from step1)';
  }   
  
  makeTxt('nav',selected_text);
}
//this one is when a asatdate has been selected and the form has been posted
function changeText(divID , text)
{
  if(text == 'Select a date' || text == '')
	{
		text = '(please select an as at date from step1)';
  }  	
	divEl = document.getElementById(divID);
	if (divEl)
	{
		divEl.innerHTML = text;
	}
}
//this to redirect to the same page but with astadate val in the url in order to requery and populate the form
function redirect_asatdate(asatdate, portfolio_holder, url_date)
{
	if(portfolio_holder == null)
	{
		alert('You must create portfolio to use the date selector');
		return;
	}
  var w = asatdate.selectedIndex;
  var x = portfolio_holder.selectedIndex;
    
  var date = asatdate.options[w].text;
  var product = portfolio_holder.options[x].value;  

  if(date == 'Select a date' || product == '' && url_date != '')
	{
		//window.location = "?selected_asatdate=";		
		alert('You must select a date and a product before clicking "Go"');
		return;
  }	
  if(url_date == '' && (date == 'Select a date' || product == ''))
	{
  	return;
  }
	window.location = "?selected_asatdate=" + date + "&selected_prod=" + product;		
}


//calculate totals for portfolio holding
function calc_portfolio_holding_totals(form, extrafunds)
{
	var total_di = 0;
	var final_total_ror = 0;
	var total_dollars_invested = 0;
	for (i = 1; i <= extrafunds; i++)
	{
	 	var fundaum = Number(removeCommas(form.elements['fund' + i + 'aum'].value));
		var fundror = Number(removeCommas(form.elements['fund' + i + 'ror'].value));
		total_di += fundaum;
		total_dollars_invested += fundaum;
		final_total_ror += fundaum * fundror;
	}
	if(total_di == 0)
	{
		alert('Total dollars invested cannot be 0')
		return;
	}
	final_total_ror = final_total_ror / total_di;
	form.total_ror.value = final_total_ror.toFixed(4);
	
	form.total_dollars_invested.value = total_dollars_invested.toFixed(2);
	form.total_dollars_invested.value = addCommas(form.total_dollars_invested.value);
}

function redirect_by_letter(get_var, letter)
{
	window.location = "?" + get_var + "=" + letter;		
}

function confirm_holding_deletion(portfolio_id)
{
	if(portfolio_id == '')
	{
		alert('You must select a porfolio to delete');
		return false;
	}
	else
	{
		var agree = '';
		agree=confirm("You will be deleting an entire holding including all months worth of data.  Do you wish to proceed?");
		if (agree)
		{		
			return true;
		}		
		else
		{
			return false;
		}		
	}
}

//for building a query string with products for the data squirt file
function create_query_string(theform, perfdate, cover_page)
{
	var cbResults ='';
	var flag = 0;
	for (var i = 0; i < theform.elements.length; i++ )
	{
  	if (theform.elements[i].type == 'checkbox')
		{
    	if (theform.elements[i].checked == true)
			{
    		var checkname = '';
    		checkname = theform.elements[i].name;
    		var hold = checkname.substr(0, 7);
    		if(hold == 'tickme_')
				{
					flag++; 
					if(flag == 1)
					{
						cbResults += '/site/excel_manager.php?selected_asatdate=' + perfdate + '&productid__' + flag + '=' + theform.elements[i].value;					
					} 		
					else
					{
						cbResults += '&productid__' + flag + '=' + theform.elements[i].value;									
					}    			
    		}	
      }
    }
	}
  if(!flag)
	{
  	alert('You must select at least one product');
  	return;
  }
  else
	{
  	if(flag > 6)
		{
  		alert('You can select a maximum of 6 products');  	
  		return;  			
  	}
	
		if (cover_page === undefined )
		{ 	
			parent.location=cbResults;  	
		}
		else
		{
			cbResults += '&cover_page' + '=1'; 	
			parent.location=cbResults; 		
		}
		//alert(cbResults);	
  }
}
//for building a query string with products for the data squirt file when for only one product
function create_query_string_single(prodid, perfdate)
{ 
	var cbResults ='';	
  if(prodid > 0)
	{
		cbResults += '/site/excel_manager.php?selected_asatdate=' + perfdate + '&productid__1' + '=' + prodid + '&cover_page' + '=1';					
  }
	else
	{
  	alert('There is no product selected');
  	return;		
	}  
	parent.location=cbResults; 	
}	

//this is to prevent ie submitting the form when hits enter
function prevent_submit(evt)
{
	if(evt.keyCode == 13)
  	return false;
  else
		return true;
}

function addCommas(nStr)
{
	nStr= nStr.replace(/,/gi, "");

	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1))
	{
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function removeCommas(nStr)
{
	nStr = nStr.replace(/,/gi, "");
	return nStr;
}

function checkIt(evt)  //make sure only number related characters can be entered
{
//    evt = (evt) ? evt : window.event
//    var charCode = (evt.which) ? evt.which : evt.keyCode
//    //alert(charCode);
//    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
//    		if(charCode != 46 && charCode != 45) {
//	        alert("This field accepts numbers only.");
//	        return false;    			
//    		}
//    }
//    status = ""
//    return true
}
//
//fund
//SELECT defaultbench, reguniverse, assetclass
//FROM `product`
//WHERE `productid` =2149
//LIMIT 0 , 30 
//fund
//1182  	c  	e
//
//bmk
//SELECT prodname, reguniverse, assetclass
//FROM `product`
//WHERE `productid` =1182
//91 day Canada T Bills  	c  	m
//examples of custom bmks
//http://www.global-m-r.com/site/index.php/gmrcompprofile?thecoid=228
//http://www.global-m-r.com/site/index.php/gmrcompprofile?thecoid=286