// JavaScript Document

// TYPES
/*
var OFFICE = 1;
var BEDROOM = 2;
var LIVINGROOM = 3;
var BATHROOM = 4;
var KITCHEN = 5;
var WASHROOM = 6;
*/
// Used to store objects that represent an appliance
var AppliancesSelected = new Array();

// Used to store settings for calculator
var CalculatorSettings = {Rate:21.2, Currency:"JMD"};

/*//////////////////////////////////////////////////////////////////////////

@param app_id: id of the particualr appliance
@param index: number used to refer to the particular elements that represent the appliance
@param room: the room that the item belongs to.

*//////////////////////////////////////////////////////////////////////////
function FindAppliance(app_id, index, room){
	for(var i = 0; i < AppliancesSelected.length; i++){
		if(	AppliancesSelected[i].AppID == app_id && AppliancesSelected[i].Room == room){
			RemoveAppliance(i);	
			return;
		}
	}
	AddAppliance(app_id, index, room);
}
/*////////////////////////////////////////////
@param 
*/////////////////////////////////////////////
function AddAppliance(app_id, index, room){
	AppliancesSelected.push({AppID: app_id, Index:index, Room:room, details:null});
	FetchApplianceDetails(app_id, AppIndex(app_id, room));
}
function AppIndex(app_id, room){
	for(var i = 0; i < AppliancesSelected.length; i++){
		if(	AppliancesSelected[i].AppID == app_id && AppliancesSelected[i].Room == room){	
			return(i);
		}
	}
}
function RemoveAppliance(index){
	AppliancesSelected.splice(index, 1);
}

function FetchApplianceDetails(app_id, index)
{
	var url = "CORE/fetchApplianceDetails.php";
	
	try{
	var ApplianceDetailsAJAX = new Ajax(url, 
		{
			method: 'post', 
			data: 'aid='+app_id+'&index='+index,
			onComplete: SetApplianceDetails
		}).request();
	
	}catch(e){alert(e);}
}
function SetApplianceDetails()
{
	var res = this.response;
	var obj = JSON.parse(res.text)
	AppliancesSelected[obj.index].details = obj;
	
	try{
		CalculateUsage(AppliancesSelected[obj.index].Room);
	}catch(e){alert(e);}
}
function FetchCaculatorSettings() 
{
	var url = "CORE/FetchCaculatorSettings.php";
	
	try{
	var ApplianceDetailsAJAX = new Ajax(url, 
		{
			method: 'post',
			onComplete: SetCaculatorSettings
		}).request();
	
	}catch(e){alert(e);}
}
function SetCaculatorSettings()
{
	var res = this.response;
	var obj = JSON.parse(res.text);
	CalculatorSettings = obj;
	
	document.getElementById('Rate').innerHTML = obj.Rate;
}

function CalculateUsage(room)
{
	var result = 0;
	var tmpUsage = 0;
	var AvgEnCost = CalculatorSettings.Rate;
	var gtotal = 0;
//	alert(room);
	for(var i = 0; i < AppliancesSelected.length; i++){
		if(AppliancesSelected[i].Room != room)continue;
		var app = AppliancesSelected[i].details;
		
		var id = 'qty_'+AppliancesSelected[i].Index;
		var qty = $(id).value;
		
		id = 'usage_'+AppliancesSelected[i].Index;
		var usage = $(id).value;
		
		id = 'tusage_'+AppliancesSelected[i].Index;
		var usageVal = Number($(id).value);
//		alert($(id).value);
		switch(usage){
			case 'hd':{
				var totalHours = ((usageVal * 7) * 4);
				break;
			}
			case 'hw':{
				var totalHours = (usageVal) * 4;
				break;
			}
			default:{var totalHours = usageVal;};
		}
		
		/*alert(qty+" "+usage+" "+totalHours);*/
		/*alert("Voltage: "+parseFloat(app.voltage));
		alert("Qty: "+Number(qty));*/
		var total = parseFloat(app.voltage) * Number(qty);
		/*alert(total);*/
		total = total * totalHours;
		//alert("Total hrs: "+total);
		total = total * parseFloat(app.Duty_Cycle);
		//alert("Duty: "+total);
		total = total * parseFloat(app.Load_Factor);
		//alert("Load: "+total);
		total = total * parseFloat(app.Ballast_Factor);
		//alert("Ballast (kWh/month): "+total);
		
		gtotal += total
	}     
	$('room_'+room).innerHTML = '$'+Math.round((gtotal * AvgEnCost)*100)/100;
	CalculateTotalUsage();
}

function CalculateTotalUsage()
{
	var result = 0;
	var tmpUsage = 0;
	var AvgEnCost = CalculatorSettings.Rate;
	var gtotal = 0;
//	alert(room);
	for(var i = 0; i < AppliancesSelected.length; i++){
		var app = AppliancesSelected[i].details;
		
		var id = 'qty_'+AppliancesSelected[i].Index;
		var qty = $(id).value;
		
		id = 'usage_'+AppliancesSelected[i].Index;
		var usage = $(id).value;
		
		id = 'tusage_'+AppliancesSelected[i].Index;
		var usageVal = Number($(id).value);
//		alert($(id).value);
		switch(usage){
			case 'hd':{
				var totalHours = ((usageVal * 7) * 4);
				break;
			}
			case 'hw':{
				var totalHours = (usageVal) * 4;
				break;
			}
			default:{var totalHours = usageVal;};
		}
		
		/*alert(qty+" "+usage+" "+totalHours);*/
		/*alert("Voltage: "+parseFloat(app.voltage));
		alert("Qty: "+Number(qty));*/
		var total = parseFloat(app.voltage) * Number(qty);
		/*alert(total);*/
		total = total * totalHours;
		//alert("Total hrs: "+total);
		total = total * parseFloat(app.Duty_Cycle);
		//alert("Duty: "+total);
		total = total * parseFloat(app.Load_Factor);
		//alert("Load: "+total);
		total = total * parseFloat(app.Ballast_Factor);
		//alert("Ballast (kWh/month): "+total);
		
		gtotal += total
	}     
	$('overall').innerHTML = '$'+Math.round((gtotal * AvgEnCost)*100)/100;
}