function init(){
	//Load the Data Collection
	//loadXML();
	//Create Dropdown HTML
	drawLabels();
	if (window.ActiveXObject){
		loadIESelect();
	}else if (document.implementation && document.implementation.createDocument){// code for Mozilla, etc.
		loadIESelect();
	}
}


function loadIESelect(){
	var strSelect = '';
	strSelect += '<SELECT NAME="Exercise" id="Exercise" SIZE="0" onchange="computeForm(this.form)">\n';
	for(var i = 0; i < colActivities.Count(); i++){
		
		strSelect += '<OPTION value="' + colActivities.Item(i).value + '">' + colActivities.Item(i).option + '</OPTION>';
		
	}
	strSelect += '</SELECT>'
	document.getElementById('tdActivities').innerHTML = strSelect;
}

function loadFFSelect(){
	var vSel = false;
	//alert(colActivities.Count());
	//alert(document.getElementById('Exercise').childNodes.length);
	for(var i = 0; i < colActivities.Count(); i++){
		if(i == 0){
			vSel = true;
		}else{
			vSel = false;
		}
		var vOptNode = new Option(colActivities.Item(i).option, colActivities.Item(i).value, vSel)
		document.getElementById('Exercise').appendChild(vOptNode);
	}
}

function validateActivity(){
	var vMessage = '';
	if((document.getElementById('weight').value == 0) || (document.getElementById('weight').value == '')){
		vMessage += 'Please input your weight (lbs), ';
	}
	
	if(document.getElementById('Exercise').value == ''){
		vMessage += 'Select an activity from the dropdown list, ';
	}
	
	if((document.getElementById('mins').value == 0) || (document.getElementById('mins').value == '')){
		vMessage += 'Input the duration of the activity (minutes), ';
	}
	
	if(vMessage != ''){
		vMessage += 'Press Log Activity.';
		document.getElementById('dMsg').innerHTML = vMessage
		return false;
	}else{
		return true;
	}

}
	

function addLog(){
	
	var vValid = validateActivity();
	
	if(vValid == true){
		//alert(document.getElementById('Exercise').selectedIndex)
		var vActivitySel = document.getElementById('Exercise').selectedIndex;
		var vWeight = document.getElementById('weight').value;
		var vMins = document.getElementById('mins').value;
		var vCalories = document.getElementById('exercisepoints').value;
		var vActivityObj = colActivities.Item(vActivitySel);
		vActivityObj.minutes = vMins;
		vActivityObj.burn = vCalories;
		var objIdx = colMyActivity.Add(vActivityObj.value,vActivityObj);
		drawLog();
	}else{
		return;
	}
}
function computeForm(form) {
    i2=0; 
	exercise="";
    //getExercise(form.Exercise);
    if ((form.mins.value == null || form.mins.value.length == 0)) {
        //alert("Invalid entry in Minutes field! Re-Enter.");
       // form.mins.focus(0);
        return;
    }
	if ((form.weight.value == null || form.weight.value.length == 0)) {
        //alert("Invalid entry in Weight field! Re-Enter.");
        //form.weight.focus(0);
       return;
    }

    var str = form.mins.value;
    for (var i = 0; i < str.length; i++) {
        var ch = str.substring(i, i + 1)
        if ((ch < "0" || "9" < ch) && ch != '.') {
            alert("Non numeric data in Minutes field! Re-Enter");
            form.mins.focus(0);
           return;
        }

        }

    //if (!checkNumber(form.mins, .1, 999, "mins")) {
    //    form.mins.focus();
   //     return;
   // }

//this is where calculation occurs
//document.getElementById('dMsg').innerHTML = Math.round(((colActivities.Item(form.Exercise.selectedIndex).calories * form.weight.value) * form.mins.value));
form.exercisepoints.value = Math.round(((colActivities.Item(form.Exercise.selectedIndex).calories * form.weight.value) * form.mins.value));
}

function clearForm(form) {
    form.mins.value = "";
    form.exercisepoints.value = "";
    exercise = "";
    i2 = 0;
	document.getElementById('Exercise').selectedIndex = 0;
}
function deleteLog(pId){
	colMyActivity.Remove(pId);
	drawLog()
}

function drawLabels(){
	var vLblStr = '<table align="center" border="0" width="100%" cellspacing="0" cellpadding="3">\n';
	    vLblStr += '<tr><td align="center" class="cLabel" width="35">#</td><td width="270" class="cLabel">Activity</td><td align="center" width="110" class="cLabel">Duration <br>(mins)</td><td align="center" class="cLabel" width="110">Calorie Burn</td><td width="80" class="cLabel"></td></tr>';
	    vLblStr += '</table>';
	document.getElementById('dLabels').innerHTML = vLblStr;
}

function drawLog(){
	var vCalories = 0;
	var vDuration = 0;
	var vTblStr = '<table align="center" border="0" width="100%" cellspacing="0" cellpadding="3">\n';
		//alert(colMyActivity.Count());
		for(var i=0; i < colMyActivity.Count(); i++){
			var activity = colMyActivity.Item(i).option;
			var minutes = colMyActivity.Item(i).minutes;
			var burn = colMyActivity.Item(i).burn;
			//alert(activity + ' - ' + minutes + ' - ' + burn);
			vTblStr += '<tr><td align="center" class="cLog" width="35">' + (i+1) + '</td><td width="270" class="cLog">' + activity + '</td><td align="center" width="110" class="cLog">' + minutes + '</td><td align="center" width="110"  class="cLog">' + burn + '</td><td width="50" class="cLog" align="right"><input style="width:50px;height:20px;color:ffffff;background-color:000000;border-color:#CCCCCC;font-family:arial;font-size:12px;border-style:solid;border-width:1px;" type="button" value="Delete" onclick="deleteLog(' + i + ')"></INPUT></td></tr>';
			vCalories += Number(burn);
			vDuration += Number(minutes);
		}
		vTblStr += '<tr><td></td><td class="cTotal">Totals</td><td align="center" class="cTotal">' + vDuration + '</td><td align="center" class="cTotal">' + vCalories + '</td><td width="50"></td></tr>';
		vTblStr += '</table>';
	
	document.getElementById("dLog").style.backgroundColor = "#ffffff";
	document.getElementById('dLog').innerHTML = vTblStr;
	document.getElementById('print').style.visibility = 'visible';
}

function printLog(){
	var vCalories = 0;
	var vDuration = 0;
	var vTblStr = '<html><head><style>.cLog{font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold;color:#000000;border-bottom:1px solid #000000;}';
		vTblStr +='.cLabel{font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold;color:#000000;}';
		vTblStr +='.cTotal{font-family:Arial, Helvetica, sans-serif;font-size:14px;font-weight:bold;color:#000000;</style></head><body>';
		vTblStr +=' <table align="center" width="800"  border="0" cellspacing="0" cellpadding="0">\n';
		vTblStr +='<tr><td width="800"><div align="right" class="style1"><img src="images/yves_logo_white.jpg" ></div></td></tr>';
		vTblStr +='<tr><td><table align="center" width="100%" border="0" cellspacing="0" cellpadding="0">';

		vTblStr += '<tr style="background-color:#CCCCCC;"><td align="center" class="cLabel">#</td><td class="cLabel">Activity</td><td align="center" class="cLabel">Duration (mins)</td><td align="center" class="cLabel">Calorie Burn</td><td class="cLabel">&nbsp;</td></tr>';
		//alert(colMyActivity.Count());
		for(var i=0; i < colMyActivity.Count(); i++){
			var activity = colMyActivity.Item(i).option;
			var minutes = colMyActivity.Item(i).minutes;
			var burn = colMyActivity.Item(i).burn;
			//alert(activity + ' - ' + minutes + ' - ' + burn);
			vTblStr += '<tr><td align="center" class="cLog">' + (i+1) + '</td><td class="cLog">' + activity + '</td><td align="center" class="cLog">' + minutes + '</td><td align="center" class="cLog">' + burn + '</td><td class="cLog"></td></tr>';
			vCalories += Number(burn);
			vDuration += Number(minutes);
		}
	
		vTblStr += '<tr><td></td><td class="cTotal">Totals</td><td align="center" class="cTotal">' + vDuration + '</td><td align="center" class="cTotal">' + vCalories + '</td><td width="50"></td></tr>';
		vTblStr += '</table>';
		vTblStr +='</td></tr></table></body></html>';


	
	
	var printWin = window.open('','','');
	printWin.document.open();
	printWin.document.write(vTblStr);
	printWin.document.close();
	printWin.print();
	
}
