// requires layers.js
// requires detect.js

////////////////////////////////////////////////////////////////////////////////

var BROWSER_IE = 1, BROWSER_NETSCAPE6 = 2, BROWSER_MOZILLA = 0, BROWSER_UNKNOWN = 99;
var PLATFORM_MAC = 1, PLATFORM_DEFAULT = 0;
function BrowserDetector() {
	var agentString = navigator.userAgent.toLowerCase();
	var noMatch = true;
	this.platform = PLATFORM_DEFAULT;
	if(agentString.indexOf('mac') >= 0) { this.platform = PLATFORM_MAC; }
	if(noMatch) {
		var temp = agentString.match(/msie (\d+)\.(\d+)/);
		if(temp) {
			this.browser = BROWSER_IE;
			this.majVer = temp[1];
			this.minVer = temp[2];
			noMatch = false;
		}
	}
	if(noMatch) {
		temp = agentString.match(/netscape6\/(\d+)\.(\d+)/);
		if(temp) {
			this.browser = BROWSER_NETSCAPE6;
			this.majVer = temp[1];
			this.minVer = temp[2];
			noMatch = false;
		}
	}
	if(noMatch) {
		temp = agentString.match(/mozilla\/(\d+)\.(\d+)/);
		if(temp) {
			this.browser = BROWSER_MOZILLA;
			this.majVer = temp[1];
			this.minVer = temp[2];
			noMatch = false;
		}
	}
	if(noMatch) {
		this.browser = BROWSER_UNKNOWN;
		this.majVer = 4;
		this.minVer = 0;
	}
	this.isNS4 = BrowserDetector_isNS4;
	this.isIE = BrowserDetector_isIE;
	this.isGecko = BrowserDetector_isGecko;
	this.isMac = BrowserDetector_isMac;
}
function BrowserDetector_isNS4() { return (this.browser == BROWSER_MOZILLA && this.majVer == 4); }
function BrowserDetector_isIE() { return (this.browser == BROWSER_IE); }
function BrowserDetector_isGecko() { return ((this.browser == BROWSER_MOZILLA && this.majVer >= 5)
	|| this.browser == BROWSER_NETSCAPE6); }
function BrowserDetector_isMac() { return (this.platform == PLATFORM_MAC); }
var g_detector = new BrowserDetector();

////////////////////////////////////////////////////////////////////////////////

function HideElement(id) {
	if(g_detector.isGecko() || g_detector.isIE()) {
		var element = document.getElementById(id);
		if(element == null) { return false; }
		element.style.display = "none";
		return true;
	} else { return false; }
}
function ShowElement(id) {
	if(g_detector.isGecko() || g_detector.isIE()) {
		var element = document.getElementById(id);
		if(element == null) { return false; }
		element.style.display = "";
		return true;
	} else { return false; }
}

////////////////////////////////////////////////////////////////////////////////
// Global variables

var MV_OK = 0, MV_EMPTY = 1, MV_USER =  2, MV_ERROR = 4;
var g_netscape6Kludge;
if(g_detector.isGecko()) { g_netscape6Kludge = 0; }
else { g_netscape6Kludge = null; }
var g_gasMeterSet, g_elecMeterSet;
var g_imageCacheLeft, g_imageCacheRight;
var g_preImagesLeft, g_preImagesRight;
var g_numImages = 0;
var g_rightPlain, g_leftPlain, g_rightArrow, g_leftArrow, g_noArrow;
var g_imageFolder;
var g_formName;
var g_defaultError = "Error in JavaScript code.";

////////////////////////////////////////////////////////////////////////////////
// Global functions

function CheckInt(i, min, max) {
	if(i == null) return false;
	if(isNaN(i)) return false;
	if(i < min || i > max) return false;
	return true;
}
function NetscapeKludgeHelper(value, orientation, imageObjName, token) {
	var evalString, imageCache, imageFileName;
	imageFileName = MakeImageFileName(value, orientation);
	if(orientation) imageCache = g_imageCacheLeft;
	else imageCache = g_imageCacheRight;
	if(g_netscape6Kludge != token) {
		imageCache[value] = null;
		return;
	}
	document.images[imageObjName].src = imageFileName;
	evalString = "NetscapeKludgeHelper(" + value + "," + orientation + ",\"" + imageObjName + "\"," + token + ")";
	setTimeout(evalString, 50);
}
function ImageCacheHelper(value, orientation, imageObjName) {
	var imageFileName, imageCache, evalString;
	imageFileName = MakeImageFileName(value, orientation);
	if(orientation == null || imageObjName == null) return;
	if(value == null) {
		// Blank dials are preloaded
		if(g_netscape6Kludge != null) {
			g_netscape6Kludge++;
		}
		document.images[imageObjName].src = imageFileName;
		return;
	}
	if(orientation) imageCache = g_imageCacheLeft;
	else imageCache = g_imageCacheRight;
	if(imageCache == null) {
		alert(g_defaultError);
		return;
	}
	// Note: value ranges from 0...g_numImages
	if(imageCache[value] == null) {
		imageCache[value] = new Image();
		imageCache[value].src = imageFileName;
	}
	if(g_netscape6Kludge != null) {
		g_netscape6Kludge++;
		NetscapeKludgeHelper(value, orientation, imageObjName, g_netscape6Kludge);
		return;
	}
	if(imageCache[value].complete == true) {
		document.images[imageObjName].src = imageFileName;
		imageCache[value] = null;
	} else {
		evalString = "ImageCacheHelper(" + value + "," + orientation + ",\"" + imageObjName + "\")";
		setTimeout(evalString, 50);
	}
}
function PreloadImages() {
	g_preImagesLeft = new Array();
	g_preImagesRight = new Array();
	g_imageCacheLeft = new Array();
	g_imageCacheRight = new Array();
	// Only preload exact integer values
	for(i = 0; i < 10; i++) {
		g_preImagesLeft[i] = new Image();
		g_preImagesLeft[i].src = MakeImageFileName(Math.floor(i * (g_numImages / 10)), true);
		g_preImagesRight[i] = new Image();
		g_preImagesRight[i].src = MakeImageFileName(Math.floor(i * (g_numImages / 10)), false);
	}
	// Preload misc. other images
	g_leftPlain = new Image();
	g_leftPlain.src = g_imageFolder + "rmL.gif";
	g_rightPlain = new Image();
	g_rightPlain.src = g_imageFolder + "rmR.gif";
	g_leftArrow = new Image();
	g_leftArrow.src = g_imageFolder + "arrow_right.gif";
	g_rightArrow = new Image();
	g_rightArrow.src = g_imageFolder + "arrow_left.gif";
	g_noArrow = new Image();
	g_noArrow.src = g_imageFolder + "empty.gif";
}
function GetSetByTag(tag) {
	if(tag == 'Gas') {
		return g_gasMeterSet;
	} else if(tag == 'Elec') {
		return g_elecMeterSet;
	} else {
		return null;
	}
}
function MakeImageFileName(imageNum, left) {
	// left should be true if you want the image of a left-oriented dial,
	// or false if you want the image of a right-oriented dial
	// number should be from 0...g_numImages-1
	if(CheckInt(imageNum, 0, g_numImages - 1) == false && imageNum != null) return null;
	var imageName;
	imageName = g_imageFolder + "rm";
	if(left) imageName += "L";
	else imageName += "R";
	if(imageNum != null) imageName += imageNum;
	imageName += ".gif";
	return imageName;
}
function GetNamedObject(objName) {
	return eval("document.forms['" + g_formName + "']." + objName);
}
function InitImageCache() {
	if(g_numImages) {
		if(g_numImages <= 0) return;
		g_imageCacheLeft = new Array(g_numImages);
		g_imageCacheRight = new Array(g_numImages);
	}
}
function InitAllDials() {
	var meterSet;
	meterSet = GetSetByTag('Elec');
	if(meterSet) {
		meterSet.clear();
	}
	meterSet = GetSetByTag('Gas');
	if(meterSet) {
		meterSet.clear();
	}
}

////////////////////////////////////////////////////////////////////////////////
// Meter class

function Meter(dialsPerMeter, meterNumber, set) {
	// Meter constructor
	var i;
	// Member variables
	if(dialsPerMeter < 0) return null;
	this.dialCount = dialsPerMeter;
	this.exactValue = new Array();
	for(i = 0; i < dialsPerMeter; i++) {
		this.exactValue[i] = null;
	}
	this.parentSet = set;
	this.meterNumber = meterNumber;
	// Member methods
	this.clear = Meter_clear;
	this.makeDigitObjName = Meter_makeDigitObjName;
	this.makeImageObjName = Meter_makeImageObjName;
	this.setDialValue = Meter_setDialValue;
	this.dialClick = Meter_dialClick;
	this.focus = Meter_focus;
	this.change = Meter_change;
	this.validate = Meter_validate;
	this.showDialValue = Meter_showDialValue;
	this.show = Meter_show;
	this.showArrows = Meter_showArrows;
	this.hideArrows = Meter_hideArrows;
	// Done
	return this;
}
function Meter_clear() {
	var readingDayObj, readingMonthObj, readingYearObj;
	var year;
	for(i = 1; i <= this.dialCount; i++) {
		this.setDialValue(i, null);	
	}
	if(this.dialCount >= 5) {
		this.setDialValue(1, 0);
	}
	// Create the name prefix string for all form fields related to this meter
	var objNamePrefix = this.parentSet.tag + "Meter" + this.meterNumber + "_";
	// Get references to text box objects
	readingDayObj = GetNamedObject(objNamePrefix + "Reading_Day");
	readingMonthObj = GetNamedObject(objNamePrefix + "Reading_Month");
	readingYearObj = GetNamedObject(objNamePrefix + "Reading_Year");
	// Make sure all objects were actually found
	// If they aren't found, there is an error in the HTML
	if(readingDayObj && readingMonthObj && readingYearObj) {
		readingDayObj.value = '';
		readingMonthObj.value = '';
		year = (new Date()).getYear();
		// Some browsers actually still have a Y2K bug
		if(year < 2000) {
			year += 1900;
		}
		readingYearObj.value = year;
	}
}
function Meter_makeDigitObjName(dial) {
	if(CheckInt(dial, 1, this.dialCount) == false) return null;
	return this.parentSet.tag + "Meter" + this.meterNumber + "_Dial" + dial;
}
function Meter_makeImageObjName(dial) {
	if(CheckInt(dial, 1, this.dialCount) == false) return null;
	return this.parentSet.tag + "Dial" + dial;
}
function Meter_showDialValue(dial) {
	this.setDialValue(dial, this.exactValue[dial - 1]);
}
function Meter_setDialValue(dial, exactValue) {
	var imageObjName;
	var digitObj, digitObjName, digitValue;
	var valueArray, imageCache;
	if(CheckInt(dial, 1, this.dialCount) == false) return;
	imageObjName = this.makeImageObjName(dial);
	digitObjName = this.makeDigitObjName(dial);
	digitObj = GetNamedObject(digitObjName);
	if(digitObj == null || imageObjName == null) return;
	if(CheckInt(exactValue, 0, g_numImages - 1) == false) {
		// Tried to set number to something not valid
		digitValue = '';
		exactValue = null;
	} else digitValue = Math.floor(exactValue / g_numImages * 10);
	this.exactValue[dial - 1] = exactValue;
	digitObj.value = digitValue;
	var dialOrientation;
	if(this.parentSet.orientation)
		dialOrientation = (dial % 2 == 0)? true: false;
	else dialOrientation = (dial % 2 != 0)? true: false;
	ImageCacheHelper(exactValue, dialOrientation, imageObjName);
}
function Meter_dialClick(dial, x, y) {
	var exactValue, currentMeter;
	// Because image is 85x85, ...
	x = 42 - x; // x = ((g_imageSize - 1) / 2) - x;
	y = 42 - y;
	// Calculate the new dial value
	if(((this.parentSet.orientation == false) && (dial % 2 == 0))
	   || ((this.parentSet.orientation == true) && (dial % 2 == 1))) {
		   x = -x;
	}
	exactValue = (Math.PI + Math.atan2(x, -y)) / (2 * Math.PI);
	exactValue = Math.floor(exactValue * g_numImages);
	// Update the image and the textbox
	this.setDialValue(dial, exactValue);
}
function Meter_focus() {
	this.parentSet.setCurrentMeter(this.meterNumber);
}
function Meter_change(dial) {
	var digitObjName = this.makeDigitObjName(dial);
	var digitObj = GetNamedObject(digitObjName);
	if(isNaN(digitObj.value)) {
		digitObj.focus();
		digitObj.select();
		alert("Number required.");
		return;
	}
	this.parentSet.setCurrentMeter(this.meterNumber);
	var newExactValue = Math.floor(parseInt(digitObj.value, 10) * (g_numImages / 10));
	this.setDialValue(dial, newExactValue);
}
function Meter_validate() {
	// Check for MV_OK, MV_USER, MV_ERROR, MV_EMPTY
	var i;
	var digitObjName;
	var numValid, firstInvalid;
	var meterNumberObj, digitObj, readingDayObj, readingMonthObj, readingYearObj;
	var meterNumVal;
	// Create the name prefix string for all form fields related to this meter
	var objNamePrefix = this.parentSet.tag + "Meter" + this.meterNumber + "_";
	// Get references to text box objects
	meterNumberObj = GetNamedObject(objNamePrefix + "Number");
	readingDayObj = GetNamedObject(objNamePrefix + "Reading_Day");
	readingMonthObj = GetNamedObject(objNamePrefix + "Reading_Month");
	readingYearObj = GetNamedObject(objNamePrefix + "Reading_Year");
	// Make sure all objects were actually found
	// If they aren't found, there is an error in the HTML
	if(meterNumberObj == null || readingDayObj == null || readingMonthObj == null || readingYearObj == null)
		return MV_ERROR;
	firstInvalid = 0; numValid = 0;
	// Validate the number corresponding to each dial on the meter
	// Don't show messages yet
	for(i = 1; i <= this.dialCount; i++) {
		digitObjName = this.makeDigitObjName(i);
		digitObj = GetNamedObject(digitObjName);
		if(digitObj == null)
			return MV_ERROR;
		if(ValidateSingleDigit(digitObj) == false) {
			if(firstInvalid == 0)
				firstInvalid = i;
		} else numValid++;
	}
	// Validate the meter number
	if(meterNumberObj.value != '') {
		// The meter number is non-null
		meterNumVal = new String(meterNumberObj.value);
		if(!(meterNumVal.match(/^\s*\d+\s*$/) || meterNumVal.match(/^\s*[mM][hH]\d+\s*$/))) {
			meterNumberObj.focus();
			alert("The meter number you have provided was not recognized.");
			return MV_USER;
		}
		// The meter number is valid
		if(numValid != this.dialCount) {
			// If the meter number is valid but not all digits are valid, show a message
			digitObjName = this.makeDigitObjName(firstInvalid);
			digitObj = GetNamedObject(digitObjName);
			digitObj.focus();
			alert("You have not entered a valid value for dial no. " + firstInvalid + " of your meter reading.");
			return MV_USER;
		}
		// If the meter number is valid but the date isn't valid, show a message
		if(ValidateDate(readingDayObj, readingMonthObj, readingYearObj, "Date of " + this.parentSet.userFriendlyName + " Reading") == false)
			return MV_USER;
		return MV_OK;
	} else {
		if((numValid == 1) && (this.dialCount >= 5)) {
			digitObj = GetNamedObject(this.makeDigitObjName(1));
			if(digitObj.value == '0') {
				this.clear();
				return MV_EMPTY;
			}
		}
		if(numValid != 0) {
			if(confirm("You have started to enter a meter reading. Continue? (Click Cancel to erase the reading.)")) {
				meterNumberObj.focus();
				return MV_USER;
			} else {
				this.clear();
				return MV_EMPTY;
			}
		} else {
			this.clear();
			return MV_EMPTY;
		}
	}
	return MV_ERROR;
}
function Meter_show() {
	var i;
	for(i = 1; i <= this.dialCount; i++) {
		this.showDialValue(i);	
	}
}
function Meter_showArrows() {
	var obj;
	obj = eval("document.images['" + this.parentSet.tag + "Meter" + this.meterNumber + "_LeftArrow']");
	if(obj) { obj.src = g_leftArrow.src; }
	obj = eval("document.images['" + this.parentSet.tag + "Meter" + this.meterNumber + "_RightArrow']");
	if(obj) { obj.src = g_rightArrow.src; }
}
function Meter_hideArrows() {
	var obj;
	obj = eval("document.images['" + this.parentSet.tag + "Meter" + this.meterNumber + "_LeftArrow']");
	if(obj) { obj.src = g_noArrow.src; }
	obj = eval("document.images['" + this.parentSet.tag + "Meter" + this.meterNumber + "_RightArrow']");
	if(obj) { obj.src = g_noArrow.src; }
}

////////////////////////////////////////////////////////////////////////////////
// MeterSet class

function MeterSet(meterCount, dialsPerMeter, leftOriented, userFriendlyName, accountNumDigits, tag) {
	// MeterSet constructor
	var i;
	// Member variables
	if(meterCount < 0) return null;
	this.orientation = leftOriented;
	this.userFriendlyName = userFriendlyName;
	this.accountNumDigits = accountNumDigits;
	this.tag = tag;
	this.currentMeter = 0;
	// Methods
	this.clear = MeterSet_clear;
	this.getValidMeterCount = MeterSet_getValidMeterCount;
	this.getMeter = MeterSet_getMeter;
	this.validateAccountNumber = MeterSet_validateAccountNumber;
	this.setCurrentMeter = MeterSet_setCurrentMeter;
	this.getCurrentMeter = MeterSet_getCurrentMeter;
	// Initialization
	this.meters = new Array();	
	for(i = 0; i < meterCount; i++) {
		this.meters[i] = new Meter(dialsPerMeter, i + 1, this);
	}
	this.setCurrentMeter(1);
	// Done
	return this;
}
function MeterSet_clear() {
	var i;
	for(i = 0; i < this.meters.length; i++) {
		this.meters[i].clear();
	}
}
function MeterSet_getValidMeterCount() {
	var i,	result, valid;
	valid = 0;
	for(i = 0; i < this.meters.length; i++) {
		result = this.meters[i].validate();
		if(result == MV_OK) {
			valid++;
		}
		if(result == MV_ERROR || result == MV_USER) {
			return -1;
		}
	}
	return valid;
}
function MeterSet_getMeter(meter) {
	var result = null;
	if(meter > 0 && meter <= this.meters.length) {
		result = this.meters[meter - 1];
		if(result.meterNumber != meter) {
			alert(g_defaultError);
			result = null;
		}
	}
	return result;
}
function MeterSet_getCurrentMeter() {
	return this.getMeter(this.currentMeter);
}
function MeterSet_setCurrentMeter(meter) {
	if(CheckInt(meter, 1, this.meters.length) == false) return;
	if(CheckInt(this.currentMeter, 1, this.meters.length)) {
		if(this.currentMeter == meter) return;
		this.meters[this.currentMeter - 1].hideArrows();
	}
	this.currentMeter = meter;
	this.meters[this.currentMeter - 1].show();
	this.meters[this.currentMeter - 1].showArrows();
}
function MeterSet_validateAccountNumber(textBoxObj, messageStr, enableNA) {
	// Changed on 5 Jan 2006: No more validation for account numbers
	/*
	if(textBoxObj == null) return false;
	if(ValidateText(textBoxObj, messageStr) == false)
		return false;
	if(enableNA) {
		if(CheckForNA(textBoxObj, messageStr) == true)
			return true;
	}
	var accountNumber = textBoxObj.value;
	if(isNaN(accountNumber) || accountNumber.length != this.accountNumDigits) {
		textBoxObj.focus();
		alert("You have entered an invalid " + this.userFriendlyName + " account number.\nPlease ensure you have entered " + this.accountNumDigits + " digits as required.");
		return false;
	}
	*/
	return true;
}

////////////////////////////////////////////////////////////////////////////////
// Initialization and validation
////////////////////////////////////////////////////////////////////////////////

function LoadStartService() { LoadForm('Start_Service'); }
function LoadStopService() { LoadForm('Stop_Service'); }
function LoadMeterReading() { LoadForm('Meter_Reading'); }
function LoadEPPApplication() { 
	// Initialize global variables
	g_imageFolder = "gif/";
	g_numImages = 100;
	g_formName = 'EPP_Application';
	InitImageCache();
	PreloadImages();
	// Initialize meter sets
	g_elecMeterSet = new MeterSet(1, 5, false, "Electricity Meter", 9, "Elec");
	g_gasMeterSet = new MeterSet(1, 5, false, "Natural Gas Meter", 14, "Gas");
	InitAllDials();
	ResetMeterVisibility();
}
function LoadForm(formNameStr) {
	// Initialize global variables
	g_imageFolder = "gif/";
	g_numImages = 100;
	g_formName = formNameStr;
	InitImageCache();
	PreloadImages();
	// Initialize meter sets
	g_elecMeterSet = new MeterSet(6, 5, false, "Electricity Meter", 9, "Elec");
	g_gasMeterSet = new MeterSet(1, 5, false, "Natural Gas Meter", 14, "Gas");
	// Initialize electrical meter exact values
	InitAllDials();
	ResetMeterVisibility();
}

////////////////////////////////////////////////////////////////////////////////

function edcNS(dial, x, y, left, top) {
	if(x != null && y != null && left != null && top != null)
		g_elecMeterSet.getCurrentMeter().dialClick(dial, x - left, y - top);
}
function gdcNS(dial, x, y, left, top) {
	if(x != null && y != null && left != null && top != null)
		g_gasMeterSet.getCurrentMeter().dialClick(dial, x - left, y - top);
}
function edcMSIE(dial, x, y) { dcMSIE(g_elecMeterSet, dial, x, y); }
function gdcMSIE(dial, x, y) { dcMSIE(g_gasMeterSet, dial, x, y); }
function dcMSIE(meterSet, dial, x, y) {
	if(x != null && y != null) {
		var meter = meterSet.getCurrentMeter();
		if(meter == null) return;
		if(g_detector.isIE()) {
			if(g_detector.isMac()) { meter.dialClick(dial, x + document.body.scrollLeft, y + document.body.scrollTop); }
			else { meter.dialClick(dial, x, y); }
		}
	}
}
function edcNS6(dial, x, y) { dcNS6(g_elecMeterSet, dial, x, y); }
function gdcNS6(dial, x, y) { dcNS6(g_gasMeterSet, dial, x, y); }
function dcNS6(meterSet, dial, x, y) {
	if(g_detector.isGecko() && (x != null) && (y != null)) {
		var meter = meterSet.getCurrentMeter();
		if(meter == null) return;
		var imageObj = GetNamedObject(meter.makeImageObjName(dial));
		if(imageObj == null) return;
		var imageObjX = imageObj.x;
		var imageObjY = imageObj.y;
		if (imageObjX ==null) imageObjX = imageObj.offsetLeft;
		if (imageObjY ==null) imageObjY = imageObj.offsetTop;
		meter.dialClick(dial, x - imageObjX, y - imageObjY );
	}
}
function gmFocus(meter) { SetFocus(g_gasMeterSet, meter); }
function emFocus(meter) { SetFocus(g_elecMeterSet, meter); }
function SetFocus(meterSet, meterNumber) {
	if(meterSet == null) {
		alert(g_defaultError);
		return;
	}
	var meter = meterSet.getMeter(meterNumber);
	if(meter == null) {
		alert(g_defaultError);
		return;
	}
	meter.focus();
}
function gmChange(meter, dial) { SyncDial(g_gasMeterSet, meter, dial); }
function emChange(meter, dial) { SyncDial(g_elecMeterSet, meter, dial); }
function SyncDial(meterSet, meterNumber, dial) {
	if(meterSet == null) {
		alert(g_defaultError);
		return;
	}
	var meter = meterSet.getMeter(meterNumber);
	if(meter == null) {
		alert(g_defaultError);
		return;
	}
	meter.change(dial);
}

////////////////////////////////////////////////////////////////////////////////

function ValidateElecMeters(f) {
	var validCount;
	var doCheck = false;
	if(f.Electricity_Service.checked) doCheck = true;
	if(doCheck && f.Electricity_Meter_Reading) {
		if(ValidateRadioButtons(f.Electricity_Meter_Reading, "Do you have an electricity meter reading?") == false) return false;	
		if(GetRadioButtonValue(f.Electricity_Meter_Reading) == 1) doCheck = false;
	}
	if(doCheck) {
		validCount = g_elecMeterSet.getValidMeterCount();
		if(validCount < 0) return false;
		if(validCount == 0) {
			f.ElecMeter1_Number.focus();
			alert("You have indicated that you have an electricity reading.\nPlease enter your meter number.");
			return false;
		}
	}
	return true;
}
function ValidateGasMeters(f) {
	var validCount;
	var doCheck = false;
	if(f.Natural_Gas_Service.checked) doCheck = true;
	if(doCheck && f.Natural_Gas_Meter_Reading) {
		if(ValidateRadioButtons(f.Natural_Gas_Meter_Reading, "Do you have a natural gas meter reading?") == false) return false;	
		if(GetRadioButtonValue(f.Natural_Gas_Meter_Reading) == 1) doCheck = false;
	}
	if(doCheck) {
		validCount = g_gasMeterSet.getValidMeterCount();
		if(validCount < 0) return false;
		if(validCount == 0) {
			f.GasMeter1_Number.focus();
			alert("You have indicated that you have a natural gas meter reading.\nPlease enter your meter number.");
			return false;
		}
	}
	return true;
}

////////////////////////////////////////////////////////////////////////////////
// Validation (General)

function ValidateNumber(textBoxObj) {
	// Returns true if the text box contains a number
	// Returns false if it does not
	if(textBoxObj == null) {
		alert(g_defaultError);
		return false;
	}
	if(isNaN(textBoxObj.value)) {
		textBoxObj.focus();
		textBoxObj.select();
		alert("This field requires a number.");
		return false;
	} else return true;
}
var REQUIRE_AREA_CODE = 1, PREVENT_AREA_CODE = 2, ALLOW_AREA_CODE = 0;
function ValidatePhoneNumber(phoneBoxObj, option, messageStr) {
	var valid = false;
	var numberString = new String(phoneBoxObj.value);
	// Allow the user to enter NA
	if(CheckForNA(phoneBoxObj) == true)
		return true;
	if(option == REQUIRE_AREA_CODE || option == ALLOW_AREA_CODE) {
		if(numberString.match(/^\s*1?\s*((\(\d{3}\))|(\d{3}))\s*\d{3}\s*\-?\s*\d{4}\s*$/)) {
			return true;
		}
	}
	if(option == PREVENT_AREA_CODE || option == ALLOW_AREA_CODE) {
		if(numberString.match(/^\s*\d{3}\s*\-?\s*\d{4}\s*$/)) {
			return true;
		}
	}
	phoneBoxObj.focus();
	phoneBoxObj.select();
	if(messageStr == null) messageStr = "Please enter a valid phone number, or n/a if not applicable.";
	alert(messageStr);
	return false;
}
function ValidateText(textBoxObj, messageStr) {
	// Returns true if the text box contains text
	// Returns false if it does not
	if(textBoxObj == null) {
		alert(g_defaultError);
		return false;
	}
	if(textBoxObj.value == '') {
		textBoxObj.focus();
		textBoxObj.select();
		alert(messageStr);
		return false;
	} else return true;
}
function ValidateSingleDigit(textBoxObj) {
	// Returns true if the text box contains one base-10 digit
	// Returns false if not
	// Deliberately doesn't show a message
	var value;
	if(textBoxObj == null) return false;
	value = parseInt(textBoxObj.value, 10);
	if(isNaN(value)) return false;
	if(value < 0 || value > 9) return false;
	return true;
}
function ValidateDate(dayObj, monthObj, yearObj, dateStr) {
	// Returns true if the date is valid
 	// Returns false if it is not
	var day, month, year;
	var maxDay;
	dateStr += ": ";
	if(ValidateText(dayObj, dateStr + "Please enter a day number.") == false) return false;
	day = dayObj.value;
	day = parseInt(day, 10);
	if(isNaN(day)) {
		dayObj.focus();
		alert(dateStr + "The day field requires a numeric value.");
		return false;
	}
	if(ValidateText(monthObj, dateStr + "Please enter a month number.") == false) return false;
	month = monthObj.value;
	month = parseInt(month, 10);
	if(isNaN(month)) {
		monthObj.focus();
		alert(dateStr + "The month field requires a numeric value.");
		return false;
	}
	if(ValidateText(yearObj, dateStr + "Please enter a year.") == false) return false;
	year = yearObj.value;
	year = parseInt(year, 10);
	if(isNaN(year)) {
		yearObj.focus();
		alert(dateStr + "The year field requires a numeric value.");
		return false;
	}
	switch(month) {
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		maxDay = 31;
		break;
	case 2:
		if(year % 4 == 0 && (!((year % 100 == 0) && (year % 400 != 0))))
			maxDay = 29;
		else maxDay = 28;
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		maxDay = 30;
		break;
	default:
		monthObj.focus();
		alert(month);
		alert(dateStr + "Please enter a number from 1-12 in the month field.");
		return false;
	}
	if(day <= maxDay && day > 0)
		return true;
	dayObj.focus();
	alert(dateStr + "Please enter a number from 1-" + maxDay + " in the day field.");
	return false;
}
function ValidateEMailAddress(emailStr) {
	// Return true if the e-mail address appears to be valid
	// Return false and display a message if the e-mail address is not valid
	var i;
	var temp = emailStr.match(/^(.*)@(.*)$/);
	if(temp == null || emailStr.match(/\.\./)) {
		alert("E-Mail address seems incorrect. (Check @ and .s)");
		return false;
	}
	var userName = temp[1];
	if(!userName.match(/^[a-zA-Z0-9_\.\-]+$/)) {
		alert("User name seems incorrect.");
		return false;
	}
	var domain = temp[2];
	temp = domain.match(/^([0-9]{3})\.([0-9]{3})\.([0-9]{3})\.([0-9]{3})$/)
	if(temp != null) {
		for(i = 0; i < 4; i++) {
			if(temp[i] >= 256) {
				alert("Invalid IP address.");
				return false;
			}
		}
	} else if(!domain.match(/^([a-zA-Z\-_0-9]+)(\.[a-zA-Z\-_0-9]+)+$/)) {
		alert("Invalid domain name.");
		return false;
	}
	if(!domain.match(/^(.*)([a-zA-Z]{2,3})$/)) {
		alert("Invalid domain name. (Did you forget the top-level domain?)");
		return false;
	}
	return true;
}
function GetRadioButtonValue(listObj) {
	var i;
	for(i = 0; i < listObj.length; i++) {
		if(listObj[i].checked) return i;
	}
	return -1;
}
function ValidateRadioButtons(listObj, messageStr) {
	if(GetRadioButtonValue(listObj) == -1) {
		listObj[0].focus();
		alert(messageStr);
		return false;
	} else return true;
}
function ValidatePostalCode(textBoxObj) {
	var str = new String(textBoxObj.value);
	if(str.match(/([a-zA-Z]\d[a-zA-Z]\s*\-?\s*\d[a-zA-Z]\d)|(\d{5})/)) return true;
	textBoxObj.focus();
	textBoxObj.select();
	alert("Please enter a valid postal code.");
	return false;
}
function CheckForNA(textBoxObj) {
	var temp;
	if(textBoxObj == null) {
		alert(g_defaultError);
		return false;
	}
	temp = textBoxObj.value;
	temp = temp.toLowerCase();
	if(temp == "n/a" || temp == "not applicable" || temp == "na" || temp == "n\a") {
		return true;
	} else {
		return false;
	}
}

////////////////////////////////////////////////////////////////////////////////
// Form validation

function ValidateStartService() {
	var f = document.forms['Start_Service'];
	var naStr = "\nIf this field is not applicable, please enter n/a.";
	if(f == null) {
		alert(g_defaultError);
		return false;
	}
	// Check service types
	if(f.Electricity_Service.checked == false && f.Natural_Gas_Service.checked == false) {
		f.Electricity_Service.focus();
		alert("Please indicate the service(s) you are applying for.");
		return false;
	}
	// Check new address
	if(ValidateText(f.New_Street, "Please enter your new street address." + naStr) == false) return false;
	if(ValidateText(f.New_City_or_Town, "Please enter the name of your new city or town.") == false) return false;
	if(ValidateText(f.New_Postal_Code, "Please enter your new postal code.") == false) return false;
	if(ValidatePostalCode(f.New_Postal_Code) == false) return false;
	if(ValidateDate(f.Possession_Day, f.Possession_Month, f.Possession_Year,"Possesion Date") == false) return false;
	// Check personal information
	if(ValidateText(f.First_Name, "Please enter your first name.") == false) return false;
	if(ValidateText(f.Last_Name, "Please enter your last name.") == false) return false;
	if(ValidateText(f.Registered_Owner, "Please enter the name of the registered owner.") == false) return false;	
	if(ValidateText(f.Home_Phone_Number, "Please enter your home phone number." + naStr) == false) return false;
	if(ValidatePhoneNumber(f.Home_Phone_Number, ALLOW_AREA_CODE, "Please enter a valid home phone number." + naStr) == false) return false;
	if(ValidateText(f.Home_Phone_Number_Cell, "Please enter your cell phone number." + naStr) == false) return false;
	if(ValidatePhoneNumber(f.Home_Phone_Number_Cell, ALLOW_AREA_CODE, "Please enter a valid cell phone number." + naStr) == false) return false;
	// Personal information (continued)
// 	if(ValidateText(f.Driver_License, "Please enter your Manitoba driver license number." + naStr) == false) return false;
	if(ValidateText(f.Employer, "Please enter the name of your employer." + naStr) == false) return false;
	if(ValidateText(f.Business_Phone_Number, "Please enter your business phone number." + naStr) == false) return false;
	if(ValidatePhoneNumber(f.Business_Phone_Number, ALLOW_AREA_CODE, "Please enter a valid business phone number." + naStr) == false) return false;
	if(ValidateText(f.Name_To_Appear_On_Bill, "Please enter how you would like your name to appear on your bill." + naStr) == false) return false;
/*	// Check landlord information (if renting)
	if(ValidateRadioButtons(f.Own_or_Rent, "Do you own or rent?") == false) return false;
	if(f.Own_or_Rent[1].checked) {
		if(ValidateText(f.Landlord_Name, "Please enter the name of your landlord or building manager.") == false) return false;
		if(ValidateText(f.Landlord_Address, "Please enter the address of your landlord or building manager.") == false) return false;
		if(ValidateText(f.Landlord_Phone_Number, "Please enter the phone number of your landlord or building manager.") == false) return false;
		if(ValidatePhoneNumber(f.Landlord_Phone_Number, ALLOW_AREA_CODE, null) == false) return false;
	}*/
	// Validate own or rent
	if(ValidateRadioButtons(f.Own_or_Rent, "Did you own or rent?") == false) return false;
	if(GetRadioButtonValue(f.Own_or_Rent) == 0) { // They owned
		if(ValidateText(f.Lawyer_Name, "Please enter the name of your lawyer." + naStr) == false) return false;
		if(ValidateText(f.Lawyer_Phone_Number, "Please enter the phone number of your lawyer." + naStr) == false) return false;
		if(ValidatePhoneNumber(f.Lawyer_Phone_Number, ALLOW_AREA_CODE, null) == false) return false;
	} else {
		if(ValidateText(f.Landlord_Name, "Please enter the name of the landlord at the residence where service is to be stopped.") == false) return false;
		if(ValidateText(f.Landlord_Address, "Please enter the address of the landlord at the residence where service is to be stopped.") == false) return false;
		if(ValidateText(f.Landlord_Phone_Number, "Please enter the phone number of the landlord at the residence where service is to be stopped.") == false) return false;
		if(ValidatePhoneNumber(f.Landlord_Phone_Number, ALLOW_AREA_CODE, null) == false) return false;
	}
	// Check electricity account number
	if(f.Electricity_Service.checked) {
		if(g_elecMeterSet.validateAccountNumber(f.Previous_Account_Number_Electricity, "Please enter your previous electricity account number." + naStr, true) == false) return false;
	}
	// Check natural gas account number
	if(f.Natural_Gas_Service.checked) {
		if(g_gasMeterSet.validateAccountNumber(f.Previous_Account_Number_Gas, "Please enter your previous natural gas account number." + naStr, true) == false) return false;
	}
	// Check previous address
	if(ValidateText(f.Previous_Street, "Please enter your previous street address.") == false) return false;

if(CheckForNA(f.Previous_Street) == true) {
		f.Previous_Street.focus();
		f.Previous_Street.select();
		alert("Please enter your previous street address even if you were not responsible for utilities.");
		return false;
	}


	if(ValidateText(f.Previous_City_or_Town, "Please enter the name of the city or town in which you last lived.") == false) return false;
	
	if(CheckForNA(f.Previous_City_or_Town) == true) {
		f.Previous_City_or_Town.focus();
		f.Previous_City_or_Town.select();
		alert("Please enter the name of the city or town in which you last lived even if you were not responsible for utilities.");
		return false;
	}


	if(ValidateText(f.Previous_Province, "Please enter the name of the province in which you last lived.") == false) return false;
	
	if(CheckForNA(f.Previous_Province) == true) {
		f.Previous_Province.focus();
		f.Previous_Province.select();
		alert("Please enter the name of the province in which you last lived even if you were not responsible for utilities.");
		return false;
	}


	if(ValidateText(f.Previous_Postal_Code, "Please enter your previous postal code.") == false) return false;

if(ValidatePostalCode(f.Previous_Postal_Code) == false) return false;
	
	
	
	// Check emergency contact and lawyer
	if(f.Natural_Gas_Service.checked) {
		if(f.Own_or_Rent[0].checked) {
			if(ValidateText(f.Lawyer_Name, "Please enter the name of your lawyer." + naStr) == false) return false;
			if(ValidateText(f.Lawyer_Phone_Number, "Please enter the phone number of your lawyer." + naStr) == false) return false;
			if(ValidatePhoneNumber(f.Lawyer_Phone_Number, ALLOW_AREA_CODE, null) == false) return false;
		}
	}
	// Check information provider fields
	if(ValidateText(f.Info_Provider_First_Name, "Information provided by - please enter your first name.") == false) return false;
	if(ValidateText(f.Info_Provider_Last_Name, "Information provided by - please enter your last name.") == false) return false;
	if(ValidateText(f.Info_Provider_Phone_Number, "Information provided by - please enter your phone number.") == false) return false;
	if(ValidatePhoneNumber(f.Info_Provider_Phone_Number, ALLOW_AREA_CODE, null) == false) return false;
	// Validate electricity meter readings
	if(ValidateElecMeters(f) == false) return false;
	// Validate natural gas meter readings
	if(ValidateGasMeters(f) == false) return false;
	// Validate for PAPP
	if(ValidateRadioButtons(f.Same_PAPP_Bank_Info, "If your current account is on the PAPP, do you wish to continue with the same bank information? Otherwise select I am currently not on the PAPP.") == false) return false;
	// Validate e-mail address
	if(ValidateText(f.EMail_Address, "Please enter your EMail Address.") == false) return false;
	if(f.EMail_Address.value != '') {
		if(ValidateEMailAddress(f.EMail_Address.value) == false) {
			f.EMail_Address.focus();
			return false;
		} else {
			//f.mail_bcc.value = f.EMail_Address.value;
			f.mail_user.value = f.EMail_Address.value;
		}
	}
	SetDestinationEMailAddress(f);
	return true;
}

////////////////////////////////////////////////////////////////////////////////

function ValidateStopService() {
	var f = document.forms['Stop_Service'];
	var naStr = "\nIf this field is not applicable, please enter n/a.";
	if(f == null) return false;
	if(f.Electricity_Service.checked == false && f.Natural_Gas_Service.checked == false) {
		f.Electricity_Service.focus();
		alert("Please indicate the service(s) you are stopping.");
		return false;
	}
	if(f.Electricity_Service.checked) {
		if(g_elecMeterSet.validateAccountNumber(f.Electricity_Account_Number, "You have chosen to stop electricity service. Please enter your electricity account number." + naStr, true) == false) return false;
	}
	if(f.Natural_Gas_Service.checked) {
		if(g_gasMeterSet.validateAccountNumber(f.Natural_Gas_Account_Number, "You have chosen to stop natural gas service. Please enter your natural gas account number." + naStr, true) == false) return false;
	}
	if(ValidateText(f.Service_Street, "Please enter your service street.") == false) return false;
	if(ValidateText(f.Service_City_or_Town, "Please enter your service city or town.") == false) return false;
	if(ValidateText(f.Service_Postal_Code, "Please enter your service postal code.") == false) return false;
	if(ValidatePostalCode(f.Service_Postal_Code) == false) return false;
	if(ValidateText(f.First_Name, "Please enter your first name.") == false) return false;
	if(ValidateText(f.Last_Name, "Please enter your last name.") == false) return false;
	if(ValidateText(f.Registered_Owner, "Please enter the name of the registered owner.") == false) return false;
	if(ValidateRadioButtons(f.Own_or_Rent, "Did you own or rent?") == false) return false;
	if(GetRadioButtonValue(f.Own_or_Rent) == 0) { // They owned
		if(ValidateDate(f.Legal_Possession_Day, f.Legal_Possession_Month, f.Legal_Possession_Year,"Legal Possession Date") == false) return false;
		if(ValidateText(f.Lawyer_Name, "Please enter the name of your lawyer." + naStr) == false) return false;
		if(ValidateText(f.Lawyer_Phone_Number, "Please enter the phone number of your lawyer." + naStr) == false) return false;
		if(ValidatePhoneNumber(f.Lawyer_Phone_Number, ALLOW_AREA_CODE, null) == false) return false;
	} else {
		if(ValidateDate(f.Ownership_Change_Day, f.Ownership_Change_Month, f.Ownership_Change_Year,"Date Key Handed In") == false) return false;
		if(ValidateText(f.Landlord_Name, "Please enter the name of the landlord at the residence where service is to be stopped.") == false) return false;
		if(ValidateText(f.Landlord_Address, "Please enter the address of the landlord at the residence where service is to be stopped.") == false) return false;
	if(ValidateText(f.Landlord_Postal_Code, "Please enter the postal cose of the landlord at the residence where service is to be stopped.") == false) return false;
		if(ValidatePostalCode(f.Landlord_Postal_Code) == false) return false;
	if(ValidateText(f.Landlord_Phone_Number, "Please enter the phone number of the landlord at the residence where service is to be stopped.") == false) return false;
		if(ValidatePhoneNumber(f.Landlord_Phone_Number, ALLOW_AREA_CODE, null) == false) return false;
	}
	if(ValidateText(f.Forwarding_Street, "Please enter your forwarding street.") == false) return false;
	if(ValidateText(f.Forwarding_City_or_Town, "Please enter your forwarding city or town.") == false) return false;
	if(ValidateText(f.Forwarding_Province, "Please enter your forwarding province.") == false) return false;
	if(ValidateText(f.Forwarding_Postal_Code, "Please enter your forwarding postal code.") == false) return false;
	if(ValidatePostalCode(f.Forwarding_Postal_Code) == false) return false;
	if(ValidateText(f.Forwarding_Area_Code, "Please enter your forwarding area code." + naStr) == false) return false;
/*	if(!f.Forwarding_Area_Code.value.match(/^\s*\d{3}\s*$/)) {
		f.Forwarding_Area_Code.focus();
		alert("Please enter a valid area code.");
		return false;
	}*/
	if(ValidateText(f.Forwarding_Phone_Number, "Please enter your forwarding phone number." + naStr) == false) return false;
	if(ValidatePhoneNumber(f.Forwarding_Phone_Number, PREVENT_AREA_CODE, "Please enter a valid forwarding phone number." + naStr) == false) return false;
	// Validate provider information
	if(ValidateText(f.Info_Provider_First_Name, "Information provided by - please enter your first name.") == false) return false;
	if(ValidateText(f.Info_Provider_Last_Name, "Information provided by - please enter your last name.") == false) return false;
	if(ValidateText(f.Info_Provider_Phone_Number, "Information provided by - please enter your phone number.") == false) return false;
	if(ValidatePhoneNumber(f.Info_Provider_Phone_Number, ALLOW_AREA_CODE, null) == false) return false;
	// Validate electricity meter readings
	if(ValidateElecMeters(f) == false) return false;
	// Validate natural gas meter readings
	if(ValidateGasMeters(f) == false) return false;
	if(ValidateText(f.EMail_Address, "Please enter your EMail Address.") == false) return false;
	if(f.EMail_Address.value != '') {
		if(ValidateEMailAddress(f.EMail_Address.value) == false) {
			f.EMail_Address.focus();
			return false;
		} else {
			//f.mail_bcc.value = f.EMail_Address.value;
			f.mail_user.value = f.EMail_Address.value;
		}
	}
	SetDestinationEMailAddress(f);
	return true;
}

////////////////////////////////////////////////////////////////////////////////

function ValidateEPPApplication() {
	var f = document.forms['EPP_Application'];
	var temp;
	var naStr = "\nIf this field is not applicable, please enter n/a.";
	if(f == null) return false;
	if(f.Electricity_Service.checked == false && f.Natural_Gas_Service.checked == false) {
		f.Electricity_Service.focus();
		alert("Please indicate the service(s) for being applied for under the EPP.");
		return false;
	}
	if(f.Electricity_Service.checked) {
		if(g_elecMeterSet.validateAccountNumber(f.Electricity_Account_Number, "You have indicated you would like to join the Equal Payment Plan for electricity.\nPlease enter your 9-digit electricity account number." + naStr, true) == false) return false;	
	}
	if(f.Natural_Gas_Service.checked) {
		if(g_gasMeterSet.validateAccountNumber(f.Natural_Gas_Account_Number, "You have indicated you would like to join the Equal Payment Plan for natural gas.\nPlease enter your 14-digit natural gas account number." + naStr, true) == false) return false;
	}
	if(ValidateText(f.Service_Street, "Please enter your street.") == false) return false;
	if(ValidateText(f.Service_City_or_Town, "Please enter your city or town.") == false) return false;
	if(ValidateText(f.Service_Postal_Code, "Please enter your postal code.") == false) return false;
	if(ValidatePostalCode(f.Service_Postal_Code) == false) return false;
	if(ValidateText(f.First_Name, "Please enter your first name.") == false) return false;
	if(ValidateText(f.Last_Name, "Please enter your last name.") == false) return false;
	if(f.Electricity_Service.checked) {
		if(ValidateRadioButtons(f.Air_Conditioning, "Does the service address have air conditioning?") == false) return false;
		if(ValidateRadioButtons(f.Electric_Heat, "Does the service address have electric heat?") == false) return false;
		if(ValidateText(f.Area, "What is the floor area of the service address, in square ft.?") == false) return false;
		if(ValidateText(f.Total_kWs, "Please enter the total kWs of electric heat.") == false) return false;
	}
// 	if(ValidateGasMeters(f) == false) return false;
// Validate e-mail address
	if(ValidateText(f.EMail_Address, "Please enter your EMail Address.") == false) return false;
	if(f.EMail_Address.value != '') {
		if(ValidateEMailAddress(f.EMail_Address.value) == false) {
			f.EMail_Address.focus();
			return false;
		} else {
			//f.mail_bcc.value = f.EMail_Address.value;
			f.mail_user.value = f.EMail_Address.value;
		}
	}
	SetDestinationEMailAddress(f);
	return true;
}


////////////////////////////////////////////////////////////////////////////////

function ValidateMeterReading() {
	var f = document.forms['Meter_Reading'];
	var temp;
	var validMeters, i;
	var naStr = "\nIf this field is not applicable, please enter n/a.";
	if(f == null) return false;
	if(ValidateRadioButtons(f.Reading, "Please indicate the type of reading you are submitting.") == false) return false;
	if(f.Electricity_Service.checked == false && f.Natural_Gas_Service.checked == false) {
		f.Electricity_Service.focus();
		alert("Please indicate the service(s) you are reading a meter for.");
		return false;
	}

/*	if(f.Electricity_Service.checked) {
		if(g_elecMeterSet.validateAccountNumber(f.Electricity_Account_Number, "You have indicated that you have an electricity meter reading.\nPlease enter your 9-digit electricity account number.", true) == false) return false;
	}
	if(f.Natural_Gas_Service.checked) {
		if(g_gasMeterSet.validateAccountNumber(f.Natural_Gas_Account_Number, "You have indicated that you have a natural gas meter reading.\nPlease enter your 14-digit natural gas account number.", false) == false) return false;
	}
	if(ValidateText(f.First_Name, "Please enter your first name.") == false) return false;
	if(ValidateText(f.Last_Name, "Please enter your last name.") == false) return false;
	if(ValidateText(f.Telephone_Number, "Please enter your telephone number." + naStr) == false) return false;
	if(ValidatePhoneNumber(f.Telephone_Number, ALLOW_AREA_CODE, null) == false) return false;
	if(ValidateText(f.Street, "Please enter your street.") == false) return false;
	if(ValidateText(f.City_or_Town, "Please enter the name of your city or town.") == false) return false;
	if(ValidateText(f.Postal_Code, "Please enter your postal code.") == false) return false;
	if(ValidatePostalCode(f.Postal_Code) == false) return false; */
	if(ValidateElecMeters(f) == false) return false;
	if(ValidateGasMeters(f) == false) return false;
if(ValidateText(f.EMail_Address, "Please enter your EMail Address.") == false) return false;
	if(f.EMail_Address.value != ''){
		if(ValidateEMailAddress(f.EMail_Address.value) == false) {
			f.EMail_Address.focus();
			return false;
		} else {
			//f.mail_bcc.value=f.EMail_Address.value;
			f.mail_user.value = f.EMail_Address.value;
		}
	}
	SetDestinationEMailAddress(f);
	saveCookie();
	return true;
}

////////////////////////////////////////////////////////////////////////////////

function ResetMeterVisibility() {
	var f = document.forms[0];
	if(f.Electricity_Service) {
		if(f.Electricity_Service.checked == true) { ShowElement("Elec_Dial_Area"); }
		else { HideElement("Elec_Dial_Area"); }
	}
	if(f.Natural_Gas_Service) {
		if(f.Natural_Gas_Service.checked == true) { ShowElement("Gas_Dial_Area"); }
		else { HideElement("Gas_Dial_Area"); }
	}
}

////////////////////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////////////////////

function ValidateQuestionsConcerns() {
	var f = document.forms['Questions'];
	var temp;
	var naStr = "\nIf this field is not applicable, please enter n/a.";
	if(f == null) return false;
	
	if(ValidateText(f.Comment, "Please enter your question") == false) return false;
	
	if(ValidateText(f.EMail_Address, "Please enter your EMail Address.") == false) return false;
	if(f.EMail_Address.value != '') {
		if(ValidateEMailAddress(f.EMail_Address.value) == false) {
			f.EMail_Address.focus();
			return false;
		} else {
			//f.mail_bcc.value = f.EMail_Address.value;
			f.mail_user.value = f.EMail_Address.value;
		}
	}
	SetDestinationEMailAddress(f);
	return true;
}

////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////

function ValidateBuilding() {
	var f = document.forms['BuildingMove'];
	var temp;
	var naStr = "\nIf this field is not applicable, please enter n/a.";
	if(f == null) return false;
	
		if(ValidateRadioButtons(f.Move_On_Provincial_Highway_or_Road, "Is the structure move taking place on a Provincial Highway, Provincial Trunk Highway, or Provincial Road?") == false) return false;


if(GetRadioButtonValue(f.Move_On_Provincial_Highway_or_Road) == 0) { // Yes
		if(ValidateText(f.MIT_Number,"Move on Provincial Highway is checked, please enter MIT Number.") == false) return false;
}

if((f.MIT_Number.value != "") && (isNaN(f.MIT_Number.value)))
{alert ("Please enter only numbers for MIT Number.")
f.MIT_Number.focus();
return false;
}

	if(ValidateText(f.Contact_Person, "Please enter the name of your Contact Person") == false) return false;
	
if(f.Business_Phone_Number.value == '' && f.Cell_Phone_Number.value == '') {
		f.Cell_Phone_Number.focus();
		alert("Please enter either your Business or Cell Phone Number");
		return false;
	}
	
	if(ValidateText(f.Proposed_Route, "Please enter your Proposed Route") == false) return false;

	

if(ValidateText(f.EMail_Address, "Please enter your EMail Address.") == false) return false;
	if(f.EMail_Address.value != '') {
		if(ValidateEMailAddress(f.EMail_Address.value) == false) {
			f.EMail_Address.focus();
			return false;
		} else {
			//f.mail_bcc.value = f.EMail_Address.value;
			f.mail_user.value = f.EMail_Address.value;
		}
	}
	SetDestinationEMailAddress(f);
	return true;
}

////////////////////////////////////////////////////////////////////////////////























function SetDestinationEMailAddress(f) {
	var destEMail = new String();
	
	if((document.forms['Stop_Service']) || (document.forms['Start_Service'])) {
		destEMail = "customerservice@hydro.mb.ca";
		//destEMail = "dwiebe@hydro.mb.ca"; // DEBUG
	} else {
		if(f.Electricity_Service.checked) {
			destEMail = destEMail + "customerservice@hydro.mb.ca,";
		}
		if(f.Natural_Gas_Service.checked) {
			destEMail = destEMail + "customerservice@hydro.mb.ca,";
		}
		destEMail = destEMail.substring(0, destEMail.length - 1);
		//destEMail = "dwiebe@hydro.mb.ca"; // DEBUG
	}
	f.mail_to.value = destEMail;
}


/*function SetDestinationEMailAddress(f) {
	var destEMail = new String();
	
	if((document.forms['Stop_Service']) || (document.forms['Start_Service'])) {
		destEMail = "pnixdorf@hydro.mb.ca";
		//destEMail = "dwiebe@hydro.mb.ca"; // DEBUG
	} else {
		if(f.Electricity_Service.checked) {
			destEMail = destEMail + "pnixdorf@hydro.mb.ca";
		}
		if(f.Natural_Gas_Service.checked) {
			destEMail = destEMail + "pnixdorf@hydro.mb.ca";
		}
		destEMail = destEMail.substring(0, destEMail.length - 1);
		//destEMail = "dwiebe@hydro.mb.ca"; // DEBUG
	}
	f.mail_to.value = destEMail;
}
*/
