// Date: 20110501
// Description: ViewSonic TCO Calculator Script for LED LCD Monitors and LED TVs.
// Version: 1.0
// Author: Jared Ornstead
// Title: Web and Social Media Manager
// Company: ViewSonic Corporation
// Address: 381 Brea Canyon Road, Walnut Ca. 91789
// Phone: 909-444-8970
// Email: jared.ornstead@viewsonic.com
// Copyright: Copyright(c) 2011 ViewSonic Corporation, All Rights Reserved.

// JavaScript Document
function showMyResult() 
	{	
		//Initialize Variables
		var oldEnergy = 0;
		var newEnergy = 0;
		var annualSavings = 0;
		var costPerKiloWattHr = 0.0000962; // Cost per Kilo Watt Hour, 9.62 cents per kilo watt hour
		var weeksPerYear = 50; // Number of weeks per year to calculate

		//Retrieve Variable values and store
		var numOfMonitors = document.getElementById("numOfMonitors").value;
		var avgPwrOldModel = document.getElementById("avgPwrOldModel").value;
		var numOfOprHrsPerDay = document.getElementById("numOfOprHrsPerDay").value;
		var numDaysPerWeek = document.getElementById("numDaysPerWeek").value;
		var modelSelect = document.getElementById("modelSelect").value;
		
		numOfMonitors = convertToInt(numOfMonitors);
		avgPwrOldModel = convertToInt(avgPwrOldModel);
		numOfOprHrsPerDay = convertToInt(numOfOprHrsPerDay);
		numDaysPerWeek = convertToInt(numDaysPerWeek);
		modelSelect = convertToInt(modelSelect);

		oldEnergy = (numOfMonitors * avgPwrOldModel * numOfOprHrsPerDay * numDaysPerWeek * weeksPerYear * costPerKiloWattHr); //watts per hour
		newEnergy = (numOfMonitors * modelSelect * numOfOprHrsPerDay * numDaysPerWeek * weeksPerYear * costPerKiloWattHr); //watts per hour
		annualSavings = oldEnergy - newEnergy;
		
		var roundedNumber = roundNumber(annualSavings,2)
		
		document.getElementById("result").innerHTML = "Your annual savings is: $ " + roundedNumber; // HTML Output
	}
	
		
function convertToInt(inNum) 
	{
  		if (isNaN(inNum)) return inNum;
		return parseInt(inNum);
  	}


function LCD_validateForm() { 
		   if (document.getElementById)
		   {
				var i,p,q,nm,test,num,min,max,errors='',args=LCD_validateForm.arguments;
				var errorExists = false;
				for (i=0; i<(args.length-2); i+=3) 
				{ 
					test=args[i+2]; val=document.getElementById(args[i]);
			  		if (val) 
					{ 
						nm=val.name; 
						if ((val=val.value)!="") 
						{
							if (test.indexOf('isEmail')!=-1) 
							{ 
								p=val.indexOf('@');
				  				if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
							} else if (test!='R') 
								{ 
									num = parseFloat(val);
				  
				  					if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
				  					if (test.indexOf('inRange') != -1) 
									{ 
										p=test.indexOf(':');
										min=test.substring(8,p); 
										max=test.substring(p+1);
					
										if (num<min || max<num) {
										errorExists = true;
										errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
										}
			  						} 
								}	 
							} else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; 
						}
					} 
					
					if (errorExists) 
						{
							alert('The following error(s) occurred:\n'+errors);
							document.LCD_returnValue = (errors == '');
							return 0;
						}
						
					else {
					showMyResult();
					}
		   		} 
		   }


function roundNumber(num, dec) 
	{
		var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
		return result;
	}	
