function fnAddOption(elSel, value, text)
{	
  var elOptNew = document.createElement('option');
  elOptNew.text = text;
  elOptNew.value = value;
  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}
function fnChooseIT(name, value, size, classe, cond) {
	var s = "";
	if(cond == "") {
		s = fnRITI(name, value);
	}
	else {
		s = fnRITT(name, value, size, classe) +  cond;
	}
	return s;
}
function fnClearSelectBox(selectbox) {
	var a = selectbox.options.length;
	for (var i = 0; i < a;) {
		selectbox.remove(a-1);
		a = selectbox.options.length;
	}
}
function fnConvertPercentageToDecimal(p) {
	return (p/100);
}
function fnConvertDecimalToPercentage(d) {
	return (Math.round(d*100*100)/100);	  
}
function fnDeleteTableRows(tableId,firstRowToDelete) {
	var table = document.getElementById(tableId);
	var len = table.rows.length;
	for (var i = firstRowToDelete; i < len;) {
			table.deleteRow(i);
			len = table.rows.length;
	}
}
function fnInsertRowInTable(tableName, rowIndex, arrayOfCells) {
	var table = document.getElementById(tableName);
	var row = table.insertRow(rowIndex);
  	for (var i = 0; i < arrayOfCells.length; i++) {
		var cell = row.insertCell(-1);
		cell.innerHTML = arrayOfCells[i];
		cell.id = "cell_"+i+"_"+rowIndex;
		if (i == 3) {
			//cell.id = "cell_3_"+rowIndex;
			cell.className = "tdc_ciclosemanal";
		}
   }
}

function fnIsElementInArray(array, element) {
	var b = false;
	for (var i = 0; i < array.length; i++) {
		if (array[i].id == element) {
			b = true;
		}
	}
	return b;
}
function fnIsUndefined(e) {
	if (typeof(e) == "undefined") {
		return true;
	}
	else {
		return false;
	}
}
function fnIfIsUndefReturnEmpty(d) {
	if(fnIsUndefined(d)) {
		return "";
	}
	else {
		return d;
	}
}
function fnIsChecked(cboxname) {
	return document.getElementById(cboxname).checked; 
}
function fnGetElementValue(elementname) {
	try {
		return document.getElementById(elementname).value;
	}
	catch (e) {
		return "";
	}
}
function fnReturnChecked(x) {
	if (x == true) {
		return "checked";
	}
	else {
		return "";
	}
}
// Render Input Type text


//class="calc_form" id="apa_3" size="3" maxlength="3" onFocus="hL(this)" onBlur="dL(this)"
// Render Input Type Text
function fnRITT(name, value, size, classe) {
	var i = "";
	i += '<input type="text" id="'+name+'" name="'+name+'" ';
	i += 'value="'+value+'" size="'+size+'" class= "'+classe+'" maxlength="5" onFocus="hL(this)" onBlur="dL(this)"/>';	
	return i;
}
// Render Input Type CheckBox
function fnRITC(name, value, checked, classe) {
	var i = "";
	i += '<div align="center"><input type="checkbox" id="'+name+'" name="'+name+'" ';
	i += 'value="'+value+'" '+checked+' class= "'+classe+'" /></div>';	
	return i;
}
// Render Input Type Hidden text fiels
function fnRITI(name, value) {
	var i = "";
	i += '<input type="hidden" id="'+name+'" name="'+name+'" ';
	i += 'value="'+value+'" />';	
	return i;
}
function fnSortArrayByPropName(a, b) {
    var x = a.name.toLowerCase();
    var y = b.name.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function rGQS(s) {
	return (formData2QueryString(document.forms[s]));
}
function DeviceList(id, name, funcionamento, horasvaziosemanal, horasvaziodiario, type, horaspontasemanal, horaspontadiario, perctransf1, perctransf2, perctransf3, perctransf4, perctransf5) {
	this.id = id;
	this.name = name;
	this.funcionamento = funcionamento;
	this.horasvaziosemanal = horasvaziosemanal;
	this.horasvaziodiario = horasvaziodiario;
	this.type = type;
	this.horaspontasemanal = horaspontasemanal;
	this.horaspontadiario = horaspontadiario;
	this.perctransf1 = perctransf1;
	this.perctransf2 = perctransf2;
	this.perctransf3 = perctransf3;
	this.perctransf4 = perctransf4;
	this.perctransf5 = perctransf5;
}
function NewDevice(id, name, numeroequipamentos, horasfuncionamento, funcionamento, horasvaziosemanal, horasvaziodiario, simultaneo, horaspontasemanal, horaspontadiario, perctransf1, perctransf2, perctransf3, perctransf4, perctransf5)
{
	this.id = id;
	this.name = name;
	this.numeroequipamentos = numeroequipamentos;
	this.horasfuncionamento = horasfuncionamento;
	this.funcionamento = funcionamento;
	this.horasvaziosemanal = horasvaziosemanal;
	this.horasvaziodiario = horasvaziodiario;
	this.simultaneo = simultaneo;
	this.horaspontasemanal = horaspontasemanal;
	this.horaspontadiario = horaspontadiario;	
	this.perctransf1 = perctransf1;
	this.perctransf2 = perctransf2;
	this.perctransf3 = perctransf3;
	this.perctransf4 = perctransf4;
	this.perctransf5 = perctransf5;
}

function AddDevice(array,id, name, numeroequipamentos, horasfuncionamento, funcionamento, horasvaziosemanal, horasvaziodiario, simultaneo, horaspontasemanal, horaspontadiario, perctransf1, perctransf2, perctransf3, perctransf4, perctransf5) {
	
	if (!fnIsElementInArray(array, id)) {
		array[array.length] = new NewDevice(id, name, numeroequipamentos, horasfuncionamento, funcionamento, horasvaziosemanal, horasvaziodiario, simultaneo, horaspontasemanal, horaspontadiario, perctransf1, perctransf2, perctransf3, perctransf4, perctransf5);		
	}
}

function AlterDevice(array, index, id, name, numeroequipamentos, horasfuncionamento, funcionamento, horasvaziosemanal, horasvaziodiario, simultaneo, horaspontasemanal, horaspontadiario, perctransf1, perctransf2, perctransf3, perctransf4, perctransf5)
{	
	array[index].numeroequipamentos = numeroequipamentos;
	array[index].horasfuncionamento = horasfuncionamento;
	array[index].funcionamento = funcionamento;
	array[index].horasvaziosemanal = horasvaziosemanal;
	array[index].horasvaziodiario = horasvaziodiario;
	array[index].simultaneo = simultaneo;
	array[index].horaspontasemanal = horaspontasemanal;
	array[index].horaspontadiario = horaspontadiario;
	array[index].perctransf1 = perctransf1;
	array[index].perctransf2 = perctransf2;
	array[index].perctransf3 = perctransf3;
	array[index].perctransf4 = perctransf4;
	array[index].perctransf5 = perctransf5;
}
function UpdateDevice(array) {
	for (var j = 0; j < array.length; j++) {
		var id 		= array[j].id;
		var name	= array[j].name;
		var nr		= fnGetElementValue("nr_"+array[j].id);
		var fn		= fnGetElementValue("fn_"+array[j].id);
		var fu		= array[j].funcionamento;
		var vs 		= fnConvertPercentageToDecimal(fnGetElementValue("vs_"+array[j].id));
		var vd		= fnConvertPercentageToDecimal(fnGetElementValue("vd_"+array[j].id));
		var ck 		= fnIsChecked("ck_"+array[j].id)
		var ps 		= fnConvertPercentageToDecimal(fnGetElementValue("ps_"+array[j].id));
		var pd		= fnConvertPercentageToDecimal(fnGetElementValue("pd_"+array[j].id));		
		var cks1 	= "cks1_"+array[j].id;
		var cks2 	= "cks2_"+array[j].id;
		var cks3 	= "cks3_"+array[j].id;
		var cks4 	= "cks4_"+array[j].id;
		var cks5 	= "cks5_"+array[j].id;
		
		AlterDevice(array,j,id,name,nr,fn,fu,vs,vd,ck,ps,pd,cks1,cks2,cks3,cks4,cks5);
	}
}




