<!--//////////////////////////////////////////////////

/*************************************************
 ROICalculatorfunctionality.js
 YS 5-24-2002
 Javascript functionalities for ROI Calculator
 *************************************************/

function formatDollar (Val, DollarSign)  {
	
	var digitGroupingSymbol;
	var j = window.document.the_form.currency.selectedIndex;
	digitGroupingSymbol = currency_options[j][4];
		
	Val=""+Val;
	if (Val.indexOf (".", 0)!=-1) {
		Dollars = Val.substring(0, Val.indexOf (".", 0));
		Cents = Val.substring(Val.indexOf (".", 0)+1, Val.indexOf (".",0)+3);
		if (Cents.length==0)
			Cents="00";
		if (Cents.length==1)
			Cents=Cents+"0";
	}
	else {
		Dollars = Val;
		Cents = "00";
	}

	OutString="";
	len=Dollars.length;
	if (len>=3) {
		while (len>0) {
			TempString=Dollars.substring(len-3, len)
					   if (TempString.length==3) {
				OutString=digitGroupingSymbol+TempString+OutString;
						  len=len-3;
			}
					   else {
						   OutString=TempString+OutString;
									 len=0;
					   }
		}

		if (OutString.substring(0, 1)==digitGroupingSymbol)
			Dollars=OutString.substring (1, OutString.length);
		else
				Dollars=OutString;
	}

	return (Dollars);
}


function formatDecimal(argvalue, addzero, decimaln) {
	
	var decimalSymbol;
	var j = window.document.the_form.currency.selectedIndex;
	decimalSymbol = currency_options[j][3];
	
	var numOfDecimal = (decimaln == null) ? 1 : decimaln;
	var number = 1;
	number = Math.pow(10, numOfDecimal);
	argvalue = Math.round(parseFloat(argvalue) * number) / number;
	argvalue = "" + argvalue;
    var argvalue_temp = "";
	var i = 0;
	if ((decimalSymbol != ".") && (argvalue.indexOf(".") != -1))
	{
		for(i=0;i< argvalue.length; i++)
		{
			var char_temp = argvalue.charAt(i);
			if (isNumeric(char_temp))
				argvalue_temp = argvalue_temp + char_temp;
			else
				argvalue_temp = argvalue_temp + decimalSymbol;
		}
	}

	if ( argvalue_temp != "")
		argvalue = argvalue_temp;
	

	if (argvalue.indexOf(decimalSymbol) == 0)
		argvalue = "0" + argvalue;

	if (addzero == true) {
		if (argvalue.indexOf(decimalSymbol) == -1)
			argvalue = argvalue + decimalSymbol;

		while ((argvalue.indexOf(decimalSymbol) + 1) > (argvalue.length - numOfDecimal))
			argvalue = argvalue + "0";
	}
	
	if (decimaln == 1 && argvalue.indexOf(decimalSymbol) == -1)
		argvalue = argvalue + decimalSymbol + "0";
	
	return argvalue;
}

////YS: here is new edition for this new version of ROI
function multiply(vform)
{	
	//define local variables
	var footage = "";
	var staff   = "";
	var footage_temp = window.document.the_form.o_footage.value;
	var staff_temp	 = window.document.the_form.o_staff.value;
	var i = 0;
	
	//removing characters from user's inputs
	for(i=0;i< footage_temp.length; i++)
	{
		var char_temp = footage_temp.charAt(i);
		if (isNumeric(char_temp))
			footage = footage + char_temp;
	}
	footage_temp = footage;
	
	for(i=0;i< staff_temp.length; i++)
	{
		var char_temp_1 = staff_temp.charAt(i);
		if (isNumeric(char_temp_1))
			staff = staff + char_temp_1;
	}
	
	//If form is left blank, inform user of required fields
	verifyInput(footage, staff);
	
	staff_temp = staff;
	
	var Str_basedOnImpl				= "";
	var saving_perSqFt				= 0.00;
	var totalSWCost_perUser			= 0.00;
	var totalTrainingCost_perUser	= 0.00;
	var TotalImplCost_perSqFt		= 0.00;
	var TotalCost_perSqFt			= 0.00;
	var TotalCost_perUser			= 0.00;
	
	var Total_Annual_Savings		= 0.00;
	var Moths_To_Payout_Investment	= 0.00;
	var Return_On_Investment		= 0.00;
	var Loss_Sustained_Each_Q		= 0.00;

	//check if users select to use Sq. Meters,
	//if Yes, convertion it into Sq. Ft
	footage = footage * 10.7639;
	//the calculation factors will depend on user's inputs: area
	//managed (footage) by AFM and staff number (staff) to use AFM
	if (footage <= 100000 && staff == 1 )
	{
		//AFM-Express
		Str_basedOnImpl				= String_AFM_Express;
		totalSWCost_perUser			= AFM_Express_TotalSWCostPerUser ;
		totalTrainingCost_perUser	= AFM_Express_TotalTrainingCostPerUser ;
		TotalImplCost_perSqFt		= AFM_Express_TotalImplCostPerSquareFoot;
		saving_perSqFt				= AFM_Express_SavingPerSquareFoot;
	}
	else if (footage <= 500000 && staff <= 3)
	{
		//AFM-Facil-o-tor
		Str_basedOnImpl				= String_AFM_Facil_o_Tor;
		totalSWCost_perUser			= AFM_Facil_o_Tor_TotalSWCostPerUser ;
		totalTrainingCost_perUser	= AFM_Facil_o_Tor_TotalTrainingCostPerUser ;
		TotalImplCost_perSqFt		= AFM_Facil_o_Tor_TotalImplCostPerSquareFoot;
		saving_perSqFt				= AFM_Facil_o_Tor_SavingPerSquareFoot;
	}
	else if (footage <= 2000000)
	{
		//AFM-Interprise (I)
		Str_basedOnImpl				= String_AFM_Enterprise;
		totalSWCost_perUser			= AFM_Enterprise_TotalSWCostPerUser_1 ;
		totalTrainingCost_perUser	= AFM_Enterprise_TotalTrainingCostPerUser_1 ; 
		TotalImplCost_perSqFt		= AFM_Enterprise_TotalImplCostPerSquareFoot_1;
		saving_perSqFt				= AFM_Enterprise_SavingPerSquareFoot_1;
	}
	else if (footage <= 10000000)
	{
		//AFM-Interprise (II)
		Str_basedOnImpl				= String_AFM_Enterprise;
		totalSWCost_perUser			= AFM_Enterprise_TotalSWCostPerUser_2 ;
		totalTrainingCost_perUser	= AFM_Enterprise_TotalTrainingCostPerUser_2 ;
		TotalImplCost_perSqFt		= AFM_Enterprise_TotalImplCostPerSquareFoot_2;
		saving_perSqFt				= AFM_Enterprise_SavingPerSquareFoot_2;
	}
	else
	{
		//AFM-Interprise (III)
		Str_basedOnImpl				= String_AFM_Enterprise;
		totalSWCost_perUser			= AFM_Enterprise_TotalSWCostPerUser_3 ;
		totalTrainingCost_perUser	= AFM_Enterprise_TotalTrainingCostPerUser_3; 
		TotalImplCost_perSqFt		= AFM_Enterprise_TotalImplCostPerSquareFoot_3;
		saving_perSqFt				= AFM_Enterprise_SavingPerSquareFoot_3;

	}
	//according to user's currency selection, make cost or saving calculation
	//depending on the related currency exchange rate
	var currency_exchange_rate;
	currency_exchange_rate = window.document.the_form.currency.options[window.document.the_form.currency.selectedIndex].value;
	currency_exchange_rate = currency_exchange_rate.substring(currency_exchange_rate.lastIndexOf(";")+1);
	currency_exchange_rate = parseFloat(currency_exchange_rate);


	//calculate the data depending on the user's inputs, business
	//rules, and currency exchange rate
	Total_AFM_Implementation_Cost = (TotalImplCost_perSqFt * footage) + (totalSWCost_perUser * staff) + (totalTrainingCost_perUser * staff);
	Total_AFM_Implementation_Cost = Total_AFM_Implementation_Cost * currency_exchange_rate;
	
	if (footage != 0.00)
		TotalCost_perSqFt = Total_AFM_Implementation_Cost/footage;

	if (staff != 0.00)
		TotalCost_perUser = Total_AFM_Implementation_Cost/staff;

	Total_Annual_Savings = saving_perSqFt * footage;
	Total_Annual_Savings = Total_Annual_Savings * currency_exchange_rate;
						  
	if (Total_Annual_Savings != 0.00)
		Moths_To_Payout_Investment = (Total_AFM_Implementation_Cost/Total_Annual_Savings) * 12;

	if(Total_AFM_Implementation_Cost != 0.00)								 
		Return_On_Investment = 		(Total_Annual_Savings/Total_AFM_Implementation_Cost)*100	;				 

	
	Loss_Sustained_Each_Q	= Total_Annual_Savings/4;


	Threshold = 9999999999999999999999;

	//format the data
	if (Loss_Sustained_Each_Q < Threshold) { Loss_Sustained_Each_Q= formatDollar(Loss_Sustained_Each_Q); }
	Return_On_Investment = formatDecimal(Return_On_Investment, false, 0);
	if (Total_AFM_Implementation_Cost < Threshold) { Total_AFM_Implementation_Cost = formatDollar(Total_AFM_Implementation_Cost); }
	if (Total_Annual_Savings < Threshold) {	Total_Annual_Savings = formatDollar(Total_Annual_Savings); }
	Moths_To_Payout_Investment = formatDecimal(Moths_To_Payout_Investment, false, 1);
	
	
	///write the data into HTMl Form Fields
	if(footage > 0.00 && staff > 0)
	{
		
		window.document.the_form.impl_type.value			= Str_basedOnImpl;
		window.document.the_form.invest_return.value		= Return_On_Investment + "%";
		window.document.the_form.o_ann_savings.value		= Total_Annual_Savings;
		window.document.the_form.o_months_to_payout.value	= Moths_To_Payout_Investment;
		
	}
	else
	{
		
		window.document.the_form.impl_type.value			= "";
		window.document.the_form.invest_return.value		= "";
		window.document.the_form.o_ann_savings.value		= "";
		window.document.the_form.o_months_to_payout.value	= "";
		

	}

	////format user's inputs by digit grouping symbol
	window.document.the_form.o_footage.value			= formatDollar(footage_temp);	  
	window.document.the_form.o_staff.value				= formatDollar(staff_temp);

	///write user's selected currency sign to html
	var currency_money_sign;
	currency_money_sign = window.document.the_form.currency.options[window.document.the_form.currency.selectedIndex].value;
	currency_money_sign = currency_money_sign.substring(0, currency_money_sign.indexOf(";"));
	//IE
	if(document.all)
	{
		document.all["AFM_Cost"].innerText		= currency_money_sign;
		document.all["AFM_Saving"].innerText	= currency_money_sign;
		document.all["AFM_Lost"].innerText		= currency_money_sign;
	}
	//Netscape6 0r higher
	else if(!document.all && document.getElementById)
	{
		
	}
	//if Netscape4, doing nothing

}

///check if an input is a numeric
function isNumeric(value) 
{
	if (value == "")  { return false }
	if (value.charAt(0) == "-")
		start = 1;
	else
		start = 0;
	for (i=start; i<value.length; i++)
	{
		if (value.charAt(i) < "0") { return false; }
		if (value.charAt(i) > "9") { return false; }
	}
	return true;
}

function verifyInput(sqft, nusers)
{
	//IE
	if(document.all)
	{
		if(sqft == "")
		{
			document.all["Total_Area"].src		= "http://www.asc-ro.com/templates/Archibus/images/exclaimredb.gif";
			document.all["Req_Image"].src		= "http://www.asc-ro.com/templates/Archibus/images/exclaimredb.gif";
			document.all["Req_Message"].innerText		= "  Indica campul (campurile) obligatorii.";
		}
		else
		{
			document.all["Total_Area"].src		= "http://www.asc-ro.com/templates/Archibus/images/backcolor.gif";
		}
		
		if(nusers == "")
		{
			document.all["Num_Users"].src		= "http://www.asc-ro.com/templates/Archibus/images/exclaimredb.gif";
			document.all["Req_Image"].src		= "http://www.asc-ro.com/templates/Archibus/images/exclaimredb.gif";
			document.all["Req_Message"].innerText		= "  Indica campul (campurile) obligatorii.";
		}
		
		else
		{
			document.all["Num_Users"].src		= "http://www.asc-ro.com/templates/Archibus/images/backcolor.gif";
		}
		
		if(!(sqft=="") && !(nusers==""))
		{
			document.all["Req_Image"].src		= "http://www.asc-ro.com/templates/Archibus/images/backcolor.gif";
			document.all["Req_Message"].innerText		= " ";
		}
	}
	
	//Netscape6 0r higher
	else if(!document.all && document.getElementById)
	{
		if(sqft == "")
		{
			document.getElementById("Total_Area").src		= "http://www.asc-ro.com/templates/Archibus/images/exclaimredb.gif";
			document.getElementById("Req_Image").src		= "http://www.asc-ro.com/templates/Archibus/images/exclaimredb.gif";
			document.getElementById("Req_Message").innerHTML		= "  Indica campul (campurile) obligatorii.";
		}
		else
		{
			document.getElementById("Total_Area").src		= "http://www.asc-ro.com/templates/Archibus/images/backcolor.gif";
		}
		
		if(nusers == "")
		{
			document.getElementById("Num_Users").src		= "http://www.asc-ro.com/templates/Archibus/images/exclaimredb.gif";
			document.getElementById("Req_Image").src		= "http://www.asc-ro.com/templates/Archibus/images/exclaimredb.gif";
			document.getElementById("Req_Message").innerHTML		= "  Indica campul (campurile) obligatorii.";
		}
		
		else
		{
			document.getElementById("Num_Users").src		= "http://www.asc-ro.com/templates/Archibus/images/backcolor.gif";
		}
		
		if(!(sqft=="") && !(nusers==""))
		{
			document.getElementById("Req_Image").src		= "http://www.asc-ro.com/templates/Archibus/images/backcolor.gif";
			document.getElementById("Req_Message").innerHTML		= " ";
		}
	}
}

function requestDetail() {
	if (document.getElementById("invest_return").value!="") {
		var x1 = document.getElementById("inwhite1").value;
		var x2 = document.getElementById("inwhite2").value;
		window.location.href="http://www.asc-ro.com/informatii-suplimentare.html?s="+x1+"&n="+x2;
		return true;
		}
	else {
	alert("Va rugam completati campurile de calcul");
	return false;
	}
}
// ending -->

