document.onmouseover=hideStatus;
document.onmouseout=hideStatus;

function ppocheck(value) 
{
	if (value == "0") 
	{
	    openWindow('Modules/PPO/ppoStop.aspx', '', 600, 765);	
		return true;
    }

}

function plangroupcheck(MyArgs)
{
    var WinSettings = "center:yes;resizable:no;dialogHeight:275px;dialogWidth:510px";
    var retVal = window.showModalDialog("Modules/Proposals/Maintenance/userGroupPlanDifferences.aspx", MyArgs, WinSettings);

    return retVal;
}
    
function UpdateRunningTotals(E, ES, EC, F, S, C, SC)
{
	RTE = parseFloat(E);
	RTES = parseFloat(ES);
	RTEC = parseFloat(EC);
	RTF = parseFloat(F);
	RTS = parseFloat(S);
	RTC = parseFloat(C);
	RTSC = parseFloat(SC);
	
	_RTE.value = formatCurrency(RTE);
	_RTES.value = formatCurrency(RTES);
	_RTEC.value = formatCurrency(RTEC);
	_RTF.value = formatCurrency(RTF);
	_RTS.value = formatCurrency(RTS);
	_RTC.value = formatCurrency(RTC);
	_RTSC.value = formatCurrency(RTSC);
}
					
function Cbo_Error(status, statusText, responseText)
{
	alert(responseText);
}

function filterItems(numEmployed, planValue, ddlMax) 
{
    if (numEmployed == '4' && planValue == 'Plan A') 
    {
        if (ddlMax.options[0].value == '250') 
        {
            ddlMax.remove(0);
            ddlMax.selectedIndex = 0;
            return false;
        }
    }
    else 
    {
        if (ddlMax.options[0].value != '250') 
        {
            var opt = document.createElement("option");
            ddlMax.insertBefore(opt, ddlMax.options[0]);
            opt.text = '$250';
            opt.value = '250';
            ddlMax.selectedIndex = 0;
            return false;
        }
    }
}

function getRate(tableName, control1Value, control2Value)
{
	stayTop();
	document.getElementById('divWait').style.display = 'block';
	document.body.style.overflow = 'auto';
	Cbo.OnComplete = getRateComplete;
	Cbo.DoCallBack('Rate', tableName + ';' + control1Value + ';' + control2Value);
}

function getRateComplete(responseText, responseXML)
{
	var aStr = responseText.split('\t');	
	if (aStr.length == 0)
	{
		return;
	}	
	else
	{	
		for (i = 0; i < 10; i++)
		{
			if (aStr[i] != ';')
			{
				var keyValuePair = aStr[i].split(';');
				ChangeControlInnerText(keyValuePair[0], keyValuePair[1]);
			}
		}
		
		for (i = 10; i < 45; i++)
		{
			if (aStr[i] != ';')
			{
				keyValuePair = aStr[i].split(';');
				ChangeControlValue(keyValuePair[0], keyValuePair[1]);
			}
		}

		if (aStr[45] != ';' && aStr[46] != ';' && aStr[47] != ';' && aStr[48] != ';' && aStr[49] != ';' && aStr[50] != ';' && aStr[51] != ';'){
		UpdateRunningTotals(aStr[45], aStr[46], aStr[47], aStr[48], aStr[49], aStr[50], aStr[51]);}
						
		document.getElementById('divWait').style.display = 'none';		
		document.body.style.overflow = 'auto';
	}
}

function RateDecision(tableName, dropdown1, dropdown2)
{
	if (tableName == "HMCDIH")
	{
		if (ddlDIH1 != GetDropDownValue(dropdown1))
		{	
			var dropdown1value;
			var dropdown2value;
			var dropdown3value;
			if (dropdown1 == undefined)
			{
				dropdown1 = '';
			}
			if (dropdown1 != '') 
			{	
				dropdown1value = GetDropDownValue(dropdown1);
				if (dropdown2 != '') 
				{
					if (dropdown2 == undefined)
					{
						dropdown2 = '';
					}
					else	
					{
						dropdown2value = GetDropDownValue(dropdown2);
					}
				}
			}							
			
			ddlDIH1 = dropdown1value;
		}
	    
	    //make sure a benefit amount was selected
		if (dropdown1value != 0) 
		{
			getRate(tableName, dropdown1value, dropdown2value);
		}
	}		
}	
			
function openWindow(url, name, width, height)
{
	var xOffset;
	var yOffset;
	
	if (document.all)
		var xMax = screen.width, yMax = screen.height;
	else
		if (document.layers)
			var xMax = window.outerWidth, yMax = window.outerHeight;
		else
			var xMax = 640, yMax=480;
				
	if (width || height)
	{						
		xOffset = (xMax - width)/2;
		yOffset = (yMax - height)/2;
	}
	else
	{
		xOffset = 0;
		yOffset = 0;
		width = xMax - 10;
		height = yMax - 65;
	}
	var popUp = window.open(url, name,'width='+width+', height='+height+',dependent=1,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'');
	return popUp;
}		

function AlertUser(prompt)
{
	alert(prompt);
}
	
function ClearRadioButtonGroup(controlGroup)
{	
	var controlArray = document.getElementsByName(controlGroup)
	for (i = 0; i < controlArray.length; i++)
	{
		controlArray[i].checked = false;
	}
}

function ChangeDisplay(control, displayType)
{
	document.getElementById(control).style.display = displayType;
}

function RadioButtonGroupIsChecked(controlGroup)
{
	var valid = false;
	var controlArray = document.getElementsByName(controlGroup)
	for (i = 0; i < controlArray.length; i++)
	{
		if (controlArray[i].checked)
		{
			valid = true;
			break;
		}
	}
	return valid;
}

function EnableRadioButtonGroup(controlGroup)
{
	var controlArray = document.getElementsByName(controlGroup)
	for (i = 0; i < controlArray.length; i++)
	{
		controlArray[i].disabled = false;
	}
}

function DisableRadioButtonGroup(controlGroup)
{
	var controlArray = document.getElementsByName(controlGroup)
	for (i = 0; i < controlArray.length; i++)
	{
		controlArray[i].disabled = true;
	}
}

function CheckBoxGroupIsChecked(controlName, startValue, endValue, incrementValue)
{
	var valid = false;
	for (i = startValue; i < endValue + 1; i = i + incrementValue)
	{
		if (document.getElementById(controlName + i).checked)
		{
			valid = true;
			break;
		}
	}
	return valid;
}

function GetNumCheckBoxesChecked(controlName, startValue, endValue, incrementValue)
{
	var numChecked = 0;
	for (i = startValue; i < endValue + 1; i = i + incrementValue)
	{
		if (document.getElementById(controlName + i).checked)
		{
			numChecked = numChecked + 1;
		}
	}
	return numChecked;
}

function CheckControl(control)
{
	document.getElementById(control).checked = true;
}

function UnCheckControl(control)
{
	document.getElementById(control).checked = false;
}	

function EnableControl(control)
{
	document.getElementById(control).disabled = false;
}	

function DisableControl(control)
{
	document.getElementById(control).disabled = true;
}				

function UncheckControls(controlName, startValue, endValue, incrementValue)
{
	for (i = startValue; i < endValue + 1; i = i + incrementValue)
	{
		document.getElementById(controlName + i).checked = false;
	}
}

function EnableControls(controlName, startValue, endValue, incrementValue)
{
	for (i = startValue; i < endValue + 1; i = i + incrementValue)
	{
		document.getElementById(controlName + i).disabled = false;
	}
}
	
function DisableControls(controlName, startValue, endValue, incrementValue)
{	
	for (i = startValue; i < endValue + 1; i = i + incrementValue)
	{
		document.getElementById(controlName + i).disabled = true;
	}
}

function ChangeControlValue(control, value)
{
	document.getElementById(control).value = value;
}

function ChangeControlInnerText(control, innerText)
{
	document.getElementById(control).innerText = innerText;
}

function hideStatus()
{
	window.status='';
	return true;
}

function formatCurrency(num) 
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function round(number,X) 
{
	// rounds number to X decimal places, defaults to 2
  X = (!X ? 2 : X);

  var sValue, sTemp, i;

  sValue = Math.round(number*Math.pow(10,X))/Math.pow(10,X);
  sValue = sValue.toString();

  i = sValue.indexOf(".");
  sTemp = sValue.substr(i + 1);

  if(sTemp.length < X) 
	{
  	while(sTemp.length < X) 
		{
    	sTemp += "0";
    	sValue += "0";
    }
	}
  return sValue;
}

function ClearAll()
{
	// the objects
	var oPrincipal = document.all.item("principal");
	var oFuture = document.all.item("futurevalue");
	var oRate = document.all.item("calcrate");
	var oTime = document.all.item("calctime");

	oPrincipal.value = ""
	oFuture.value = ""
	oRate.value = ""
	oTime.value = ""
}

function Clear()
{
	var oResult = document.getElementById("result")
	oResult.value = "";
}

function solve_equation() 
{
	// the objects
	var oPrincipal = document.all.item("principal");
	var oFuture = document.all.item("futurevalue");
	var oRate = document.all.item("calcrate");
	var oTime = document.all.item("calctime");

	// final value
	var fVal;

	// temp vars;
	var fPrincipal, fFuture, fRate, fTime;

	if(oPrincipal.value.length == 0) 
	{
		// solving for principal
		if(oFuture.value.length == 0 || oRate.value.length == 0 || oTime.value.length == 0) 
		{
			return;
		}

		fFuture = parseFloat(oFuture.value);
		fRate = parseFloat(oRate.value);
		fTime = parseFloat(oTime.value);
		fVal = fFuture / (1 + (fRate * fTime));
		oPrincipal.value = fVal.toFixed(2);
	}

	if(oFuture.value.length == 0) 
	{
		// solving for future value
		if(oPrincipal.value.length == 0 || oRate.value.length == 0 || oTime.value.length == 0) 
		{
			return;
		}

		fPrincipal = parseFloat(oPrincipal.value);
		fRate = parseFloat(oRate.value);
		fTime = parseFloat(oTime.value);
		fVal = fPrincipal * (1 + (fRate * fTime));
		oFuture.value = fVal.toFixed(2);
	}

	if(oRate.value.length == 0) 
	{
		// solving for rate
		if(oPrincipal.value.length == 0 || oFuture.value.length == 0 || oTime.value.length == 0) 
		{
			return;
		}

		fPrincipal = parseFloat(oPrincipal.value);
		fFuture = parseFloat(oFuture.value);
		fTime = parseFloat(oTime.value);
		fVal = (fFuture - fPrincipal) / (fPrincipal * fTime);
		oRate.value = round(fVal, 2) + '%';
	}

	if(oTime.value.length == 0) 
	{
		// solving for time
		if(oPrincipal.value.length == 0 || oFuture.value.length == 0 || oRate.value.length == 0) 
		{
			return;
		}

		fPrincipal = parseFloat(oPrincipal.value);
		fFuture = parseFloat(oFuture.value);
		fRate = parseFloat(oRate.value);
		fVal = (fFuture - fPrincipal) / (fPrincipal * fRate);
		oTime.value = fVal.toFixed(2);
	}
}

function getPercentage(sValue) 
{
	var i, sTemp;

	i = sValue.indexOf("%");
	if(i == -1) 
	{
		return parseFloat(sValue);
	}

	sTemp = sValue.substr(0, i);
	return parseFloat(parseFloat(sTemp) / 100);
}

function GetDropDownValue(clientId)
{
	var value = '';
	value = document.getElementById(clientId).options[document.getElementById(clientId).selectedIndex].value;
	return value;
}