
var indx = 1;

//function for addition of a row

function CAP_addSysRes(id)
{
  var x = document.getElementById(id).insertRow(indx+3);
  var cel1 = x.insertCell(0);
  var cel2 = x.insertCell(1);
  var cel3 = x.insertCell(2);
  var cel4 = x.insertCell(3);
  var cel5 = x.insertCell(4);
  var cel6 = x.insertCell(5);
  
  cel1.innerHTML = "<br><INPUT class=RightAlign maxLength=11 size=13 value='Server 1'><BR><select style='font-size:smaller'><option>CPU<option>Memory <option>Disk<option>Network &nbsp;&nbsp;&nbsp;&nbsp;</select>"
  
  cel2.innerHTML = "<select style='font-size:smaller' name='scaleVal' onChange ='chkVal()'><option>Scale-up<option>Scale-out </select>"
  
  cel3.innerHTML = "<INPUT type='text' class=RightAlign maxLength=7 size=7 name='obUtil'>"
  
  cel4.innerHTML = "<INPUT type='text' class=RightAlign maxLength=7 size=7 name='baseUtilLt' value='100' onChange = 'updVal()'>"
  
  cel5.innerHTML = "<INPUT type='text' class=RightAlign maxLength=7 size=7 name='tgtUtilLt' value='100'>"
  
  cel6.innerHTML = "<INPUT type='text'  style='text-align:center' disabled='true' class=RightAlign maxLength=10 size=10 name='capFtr'>"
  
  indx = indx+1;
  
}


//function for deletion of a row

function CAP_deleteSysRes(id)
{
	if(indx >= 2)
	{
		document.getElementById(id).deleteRow(indx+2);
		indx = indx-1;
	}
	else
	{
		alert("No rows to be deleted");
	}
}


//To disable or enable target utilization limit based on scale-up or scale-out

function chkVal()
{
	var n;
	var val;
	for(i=0;i<=indx;i++)
	{
		n = document.capacityfactor.scaleVal[i].selectedIndex;
		val = document.capacityfactor.scaleVal[i][n].text;
		if(val=="Scale-out")
		{
			document.capacityfactor.tgtUtilLt[i].disabled = true
			document.capacityfactor.tgtUtilLt[i].value = document.capacityfactor.baseUtilLt[i].value;
		}
		else
		{
			document.capacityfactor.tgtUtilLt[i].disabled = false;
		}
	
	}
}

function updVal()
{
	for(i=0;i<=indx;i++)
	{
		if(document.capacityfactor.tgtUtilLt[i].disabled==true)
		{
			document.capacityfactor.tgtUtilLt[i].value = document.capacityfactor.baseUtilLt[i].value;
		}
	}
}


//validation of input fields in screen1

function getValues(value)
{
	var fnlValue = "";
	for (k=0;k<(value.length-1);k++)
	{
		//removing % from input
		firstCh = value.charAt(k);
		if(isNaN(firstCh) && (firstCh!="."))
		{
			alert("Enter a positive value");
			return false;
		}
		else
		{
			fnlValue+=firstCh;
		}
	}
	return fnlValue;
}


function cptyFormValidate()
{
	
	tgtIncrLoad = document.capacityfactor.tgtIncrLoad.value;
	lastCh = tgtIncrLoad.charAt(tgtIncrLoad.length-1);
	if (tgtIncrLoad < 0 || isNaN(tgtIncrLoad) || (tgtIncrLoad==""))
	{
		if(lastCh!="%")
		{
			alert("Enter the increase expected in consolidated workload");
			document.capacityfactor.elements[0].value = "";
			document.capacityfactor.elements[0].focus();
			return false;
		}
	}
	
	var firstCh;
	var lastCh;
	for(i=0;i<=indx;i++)
	{
		obUtil = document.capacityfactor.obUtil[i].value;
		firstCh = obUtil.charAt(0);
		lastCh = obUtil.charAt(obUtil.length-1);
	
		if (obUtil<0 || obUtil>100 || isNaN(obUtil) || (obUtil=="") || (obUtil==null) || (firstCh == 0))
		{
			if(lastCh!="%")
			{
				alert("Enter a positive value");
				document.capacityfactor.obUtil[i].value = "";
				document.capacityfactor.obUtil[i].focus();
				return false;
			}
		}
	
		baseUtilLt = document.capacityfactor.baseUtilLt[i].value;
		firstCh = baseUtilLt.charAt(0);
		lastCh = baseUtilLt.charAt(baseUtilLt.length-1);
	
		if (baseUtilLt < 0 || baseUtilLt > 100 || isNaN(baseUtilLt) || (baseUtilLt=="") || (baseUtilLt==null) || (firstCh==0))
		{
			if(lastCh != "%")
			{
				alert("Enter a positive value");
				document.capacityfactor.baseUtilLt[i].value = "";
				document.capacityfactor.baseUtilLt[i].focus();
				return false;
			}
		}
		
		if(document.capacityfactor.tgtUtilLt[i].disabled == true)
		{
			document.capacityfactor.tgtUtilLt[i].value = document.capacityfactor.baseUtilLt[i].value;
		}
		else
		{
			tgtUtilLt = document.capacityfactor.tgtUtilLt[i].value;
			firstCh = tgtUtilLt.charAt(0);
			lastCh = tgtUtilLt.charAt(tgtUtilLt.length-1);
		
			if (tgtUtilLt < 0 || tgtUtilLt > 100 || isNaN(tgtUtilLt) || (tgtUtilLt=="") || (tgtUtilLt==null) || (firstCh==0))
			{
				if(lastCh!="%")
				{
					alert("Enter a positive value");
					document.capacityfactor.tgtUtilLt[i].value = "";
					document.capacityfactor.tgtUtilLt[i].focus();
					return false;
				}
			}
		}
	}
		
	cptyCalc()
}


//calculation of capacity factor

function cptyCalc()
{
	var tgtIncrLoad = "";
	value = document.capacityfactor.tgtIncrLoad.value;
	lastCh = value.charAt(value.length-1);
	if(lastCh=="%")
	{
		for (k=0;k<(value.length-1);k++)
		{
			//removing % from input
			firstCh = value.charAt(k);
			if(isNaN(firstCh) && (firstCh!="."))
			{
				alert("Enter the increase expected in consolidated workload");
				document.capacityfactor.elements[0].value = "";
				document.capacityfactor.elements[0].focus();
				return false;
			}
			else
			{
				tgtIncrLoad+= firstCh;
			}
		}
	}
	else
	{
		tgtIncrLoad = document.capacityfactor.tgtIncrLoad.value;
	}
			
	var value;
	for(i=0;i<=indx;i++)
	{
		
		value  = document.capacityfactor.obUtil[i].value;
		lastCh = value.charAt(value.length-1);
		if(lastCh == "%")
		{
			fnlValue = getValues(value);
			obUtil = fnlValue;
		}
		else
		{
			obUtil = document.capacityfactor.obUtil[i].value;
		}
		
		value = document.capacityfactor.baseUtilLt[i].value;
		lastCh = value.charAt(value.length-1);
		if(lastCh == "%")
		{
			fnlValue = getValues(value);
			baseUtilLt = fnlValue;
		}
		else
		{
			baseUtilLt = document.capacityfactor.baseUtilLt[i].value;
		}
		
		if(document.capacityfactor.tgtUtilLt[i].disabled == true)
		{
			document.capacityfactor.tgtUtilLt[i].value = document.capacityfactor.baseUtilLt[i].value;
		}
		else
		{
			value = document.capacityfactor.tgtUtilLt[i].value;
			lastCh = value.charAt(value.length-1);
			if(lastCh == "%")
			{
				fnlValue = getValues(value);
				tgtUtilLt = fnlValue;
			}
			else
			{
				tgtUtilLt = document.capacityfactor.tgtUtilLt[i].value;
			}
		}
		
		if(document.capacityfactor.tgtUtilLt[i].disabled == true)
		{
			tgtUtilLt = baseUtilLt;
		}
		
		var valCalc = (obUtil * (parseFloat(tgtIncrLoad)+100)) / 100 ;
		
		var n = document.capacityfactor.scaleVal[i].selectedIndex;
		var val = document.capacityfactor.scaleVal[i][n].text;
		
		if(valCalc <= baseUtilLt)
		{
			document.capacityfactor.capFtr[i].value = "No upgrade" ;
		}
		else
		{	
			if(val=="Scale-out")
			{
				document.capacityfactor.capFtr[i].value = roundOff(valCalc / tgtUtilLt);
			}
			else
			{
				if((roundOff(valCalc / tgtUtilLt))<=3)
				{
					document.capacityfactor.capFtr[i].value = roundOff(valCalc / tgtUtilLt);
				}
				else
				{
					document.capacityfactor.capFtr[i].style.color = "990000";
					document.capacityfactor.capFtr[i].value = "> 3*"
				}
			}
		}
		
	}
	
}

