// ---------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------
// Shows Step 2 and Step 3
// Browser Detect code from: http://www.quirksmode.org/js/detect.html
// Returns:
// Browser name:		BrowserDetect.browser 
// Browser version:	BrowserDetect.version 
// OS name:				BrowserDetect.OS
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


// ---------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------
// Fade For Dialog
var pageWidth;
var pageHeight;
function GetPageDim() {
	var BrowserName = BrowserDetect.browser;
	var BrowserVersion = BrowserDetect.version;
	var XScrollWidth;
	var YScrollHeight;
	
	if (BrowserName == "Explorer") {
		XScrollWidth = 0;
		YScrollHeight = 0;
		if (BrowserVersion < 7) {
			XScrollWidth = 20;
		}
		if (BrowserVersion < 7) {
			YScrollHeight = 20;
		}
		
		if (document.documentElement.offsetWidth < document.documentElement.scrollWidth) {
			pageWidth = document.documentElement.scrollWidth + "px";
		} else {
			pageWidth = (document.documentElement.offsetWidth - XScrollWidth) + "px";
		}
		if (document.documentElement.clientHeight < document.documentElement.scrollHeight) {
			pageHeight = document.documentElement.scrollHeight + "px";
		} else {
			pageHeight = document.documentElement.clientHeight + "px";
		}
		
	} else if (BrowserName == "Firefox") {
		if (window.scrollMaxY > 0) {
			pageWidth = window.innerWidth + window.scrollMaxX - 17 + "px";
		} else {
			pageWidth = window.innerWidth + window.scrollMaxX + "px";
		}
		if (window.scrollMaxX > 0) {
			pageHeight = window.innerHeight + window.scrollMaxY - 17 + "px";
		} else {
			pageHeight = window.innerHeight + window.scrollMaxY + "px";
		}
		
		
	} else if (BrowserName == "Safari") {
		if (document.documentElement.offsetWidth < document.documentElement.scrollWidth) {
			pageWidth = document.documentElement.scrollWidth + "px";
		} else {
			pageWidth = document.documentElement.offsetWidth + "px";
		}
		if (document.documentElement.offsetHeight < document.documentElement.scrollHeight) {
			pageHeight = document.documentElement.scrollHeight + "px";
		} else {
			pageHeight = document.documentElement.offsetHeight + "px";
		}
		
	}
}

// ---------------------------------------------------------------------------------------
// Fade For Dialog
var MaskingIsOn = false;
function ShowMaskingBG() {
	GetPageDim();
	document.getElementById('MaskingBG').style.display = "block";
	document.getElementById('MaskingBG').style.width = pageWidth;
	document.getElementById('MaskingBG').style.height = pageHeight;
	document.getElementById('MaskingBG').style.visibility = "inherit";
	MaskingIsOn = true;
}
function HideMaskingBG() {
	document.getElementById('MaskingBG').style.visibility = "hidden";
	document.getElementById('MaskingBG').style.display = "none";
	MaskingIsOn = false;
}
function ResizeMaskingBG() {
	if (MaskingIsOn) {
		GetPageDim();
		document.getElementById('MaskingBG').style.width = pageWidth;
		document.getElementById('MaskingBG').style.height = pageHeight;
	}
}


// ---------------------------------------------------------------------------------------
// Shows Step 2 and Step 3
function FindRates() {
	document.getElementById("Step2Box").style.display = "block";
	document.getElementById("Step3Icon").style.display = "block";
	document.getElementById("Step3_Desc").style.display = "block";
}
// ---------------------------------------------------------------------------------------
// adv_search mouseover, mouseout, onfocus, onblur
var FieldIsInFocus = false;
function AdvFocusOnField(thisObj) {
	thisObj.className = "adv_txtfield_over";
	FieldIsInFocus = true;
}
function AdvBlurOnField(thisObj) {
	thisObj.className = "adv_txtfield";
	FieldIsInFocus = false;
}

function AdvFieldOver(thisObj) {
	if (FieldIsInFocus == false) {
		thisObj.className = "adv_txtfield_over";
	}
}
function AdvFieldOut(thisObj) {
	if (FieldIsInFocus == false) {
		thisObj.className = "adv_txtfield";
	}
}
// ---------------------------------------------------------------------------------------
// tb_data | mouseover | mouseout
function TableOver(thisObj) {
	if (thisObj.className == "gray") {
		thisObj.className = "gray over";
	} else if (thisObj.className == "") {
		thisObj.className = "over";
	}
}
function TableOut(thisObj) {
	if (thisObj.className == "gray over") {
		thisObj.className = "gray";
	} else if (thisObj.className == "over") {
		thisObj.className = "";
	}
}
function TableSel(thisObj, ProviderDetail) {
	if (thisObj.className == "") {
		thisObj.className = "sel";
	} else {
		thisObj.className = "gray sel";
	}
	// Show Provider Details
	document.getElementById(ProviderDetail).style.display = "block";
}
// ---------------------------------------------------------------------------------------
// Slide Invoice Out/In
var InvoiceHidden = 45;
var InvoiceVisible = 472;
function InvoiceVisible_Done() {
	document.getElementById('BtnShowDetails_' + TabSelected).style.display = "none";
	document.getElementById('BtnHideDetails_' + TabSelected).style.display = "block";
}
function ShowInvoice() {
	panel = document.getElementById('InvoiceSlider_' + TabSelected);
	var Height = panel.offsetHeight;

	if (Height<InvoiceVisible) {
		if (InvoiceVisible-Height>10) {
			panel.style.height = Height + 10 + "px";
			setTimeout("ShowInvoice()", 15);
		} else {
			panel.style.height = InvoiceVisible + "px";
			InvoiceVisible_Done();
		}
	}
}
function InvoiceHidden_Done() {
	document.getElementById('BtnShowDetails_' + TabSelected).style.display = "block";
	document.getElementById('BtnHideDetails_' + TabSelected).style.display = "none";
}
function HideInvoice() {
	panel = document.getElementById('InvoiceSlider_' + TabSelected);
	var Height = panel.offsetHeight;

	if (Height>InvoiceHidden) {
		if (Height-InvoiceHidden>10) {
			panel.style.height = Height - 10 + "px";
			setTimeout("HideInvoice()", 15);
		} else {
			panel.style.height = InvoiceHidden + "px";
			InvoiceHidden_Done();
		}
	}
}

var TabSelected;
var InvoiceIsClosed_Fix = true;
var InvoiceIsClosed_Var = true;
var InvoiceIsClosed_Heat = true;
function CheckInvoice(tabname, toggle) {
	TabSelected = tabname;
	var toggling = (toggle == undefined) ? true : toggle;
	var opening;
	
	if (tabname == "Fixed") {
		opening = toggling ? InvoiceIsClosed_Fix : true;
		InvoiceIsClosed_Fix = !opening;
	} else if (tabname == "Var") {
		opening = toggling ? InvoiceIsClosed_Var : true;
		InvoiceIsClosed_Var = !opening;
	} else {
		opening = toggling ? InvoiceIsClosed_Heat : true;
		InvoiceIsClosed_Heat = !opening;
	}
	
	if (opening) {
		InvoiceVisible = document.getElementById('InvoiceHeight_' + TabSelected).offsetHeight;
		ShowInvoice();
	} else {
		HideInvoice();
	}
}
// ---------------------------------------------------------------------------------------
// Select Tabs: "Switch Provider for an Existing Business" | "Find Provider for a New Business "
function SelBizTab(tabName) {
	// Reset all
	document.getElementById("Tab_SwitchProvider").className = "tab";
	document.getElementById("Content_SwitchProvider").style.display = "none";
	document.getElementById("Tab_FindProvider").className = "tab";
	document.getElementById("Content_FindProvider").style.display = "none";
	
	// Select
	document.getElementById("Tab_" + tabName).className = "tab sel";
	document.getElementById("Content_" + tabName).style.display = "block";
	
	// Show Slider
	//ShowOrHideSlider();
}
// ---------------------------------------------------------------------------------------
// ShowPopup_RelativeTo | Position of Dropdown relative to inline item
function ShowPopup_RelativeTo(Position, PopName, addtop, addleft) {
	var InlinePosition = document.getElementById(Position);	// relative position inline
	var PopupPosition = document.getElementById(PopName);		// actual popup position
	
	PopupPosition.style.display = "block";
	PopupPosition.style.top = InlinePosition.offsetTop + addtop + "px";
	PopupPosition.style.left = InlinePosition.offsetLeft + addleft + "px";
}
/* By Marcelo: Former function didn't work for me, but the name was ok. So just rename it a bit until we refactor and stick with one implementation */
function ShowPopup_RelativeTo2(PopName, Target, addtop, addleft) {
  if (navigator.appVersion.indexOf('MSIE 8')>0){
    ShowPopup_RelativeTo(Target, PopName, addtop, addleft);
  } else {
    Element.clonePosition(PopName, Target, { setTop: true, offsetLeft: addleft, offsetTop: addtop });
  }
  $(PopName).style.display = 'block';
}

// ShowPopup_RelativeTo | Position of Dropdown relative to inline item
function ShowPopup_NewRelativeTo(Base, PopName, addtop, addleft) {
	var PopupPosition = $(PopName);		// actual popup position
	PopupPosition.style.display = "block";
	Position.clone($(Base), PopupPosition, { setLeft: true, setTop: true, offsetLeft: addleft, offsetTop: addtop })
}
function ShowPopup_RelativeToTopOnly(Position, PopName, addtop) {
	var InlinePosition = document.getElementById(Position);	// relative position inline
	var PopupPosition = document.getElementById(PopName);		// actual popup position
	
	PopupPosition.style.display = "block";
	PopupPosition.style.top = InlinePosition.offsetTop + addtop + "px";
}


function ShowPopup(PopName) {
	var PopupPosition = document.getElementById(PopName);
	if(PopupPosition){
		PopupPosition.style.display = "block";
	}
}
function ShowPopupLoc(PopName, Top, Left) {
	var PopupPosition = document.getElementById(PopName);
	PopupPosition.style.display = "block";
	PopupPosition.style.top = Top + "px";
	PopupPosition.style.left = Left + "px";
}
function HidePopup(PopName) {
	var PopupPosition = document.getElementById(PopName);
	if(PopupPosition){
		PopupPosition.style.display = "none";
	}
}

// ------------------------------------------------------
// function ShowSendingSpinner(submit_btn_id) {
//   // assumes the div name and button names, very limited for now
//   $(submit_btn_id).fade();
//   $('preview_button').fade();
//   $('cancel_send').fade();
//   $('commconsole_send_ajax_loader').show();
// }

// ---------------------------------------------------------------------------------------
// Select Tabs: "Fixed Rates" | "Variable Rates (MCPE)" | "Heat Rates"
function SelRatesTab(tabName) {
	// Reset all
	document.getElementById("Tab_FixedRates").className = "tab";
	//document.getElementById("Content_FixedRates").style.display = "none";
	document.getElementById("Tab_VariableRates").className = "tab";
	//document.getElementById("Content_VariableRates").style.display = "none";
	// document.getElementById("Tab_HeatRates").className = "tab";
	//document.getElementById("Content_HeatRates").style.display = "none";
	
	// Select
	document.getElementById("Tab_" + tabName).className = "tab sel";
	//document.getElementById("Content_" + tabName).style.display = "block";
	
	// Show Slider
	//ShowOrHideSlider();
}

function SelTermsTab(tabName) {
	// Reset all
	document.getElementById("Tab_36mo").className = "tab";
	document.getElementById("Tab_24mo").className = "tab";
	document.getElementById("Tab_12mo").className = "tab";
	document.getElementById("Tab_6mo").className = "tab";
	document.getElementById("Tab_90day").className = "tab";
	document.getElementById("Tab_30day").className = "tab";
	document.getElementById("Tab_other").className = "tab";
	// Select
	document.getElementById("Tab_" + tabName).className = "tab sel";
}
// ---------------------------------------------------------------------------------------
// Select BOX "Today's Average"
function TA_Sel(boxNum) {
	// Reset all
	document.getElementById("TA1").className = "box1";
	document.getElementById("TA2").className = "box2";
	document.getElementById("TA3").className = "box3";
	
	// Select
	document.getElementById("TA" + boxNum).className = "box" + boxNum + " sel";
}
// ---------------------------------------------------------------------------------------
// Select Home Tabs
function SelHomeTab(tabName,tabNum) {
          var variable_rates_current_src = document.getElementById("HomeTab_VariableRates") ? document.getElementById("HomeTab_VariableRates").src : null;
          var tx = !variable_rates_current_src || !/_index/.match(variable_rates_current_src);
	// Reset all
	if (document.getElementById("HomeTab_FixedRates")) { document.getElementById("HomeTab_FixedRates").src = "/images/home_tab1.gif"; }
	if (document.getElementById("Content_FixedRates")) { document.getElementById("Content_FixedRates").style.display = "none"; }
	if (document.getElementById("HomeTab_VariableRates")) {
          if (tx) { //Image for tx
            document.getElementById("HomeTab_VariableRates").src = "/images/home_tab2.gif";
          } else { // Image for non-tx
            document.getElementById("HomeTab_VariableRates").src = "/images/home_tab2_index.gif";
          }
        }
	if (document.getElementById("Content_VariableRates")) { document.getElementById("Content_VariableRates").style.display = "none"; }
	if (document.getElementById("HomeTab_HeatRates")) { document.getElementById("HomeTab_HeatRates").src = "/images/home_tab3.gif"; }
	if (document.getElementById("Content_HeatRates")) { document.getElementById("Content_HeatRates").style.display = "none"; }
	if (document.getElementById("HomeTab_RealTimeQuote")) { document.getElementById("HomeTab_RealTimeQuote").src = "/images/home_tab4.gif"; }
	if (document.getElementById("Content_RealTimeQuote")) { document.getElementById("Content_RealTimeQuote").style.display = "none"; }
	
	// Select
        if (tabName != 'VariableRates' || tx) {
	  document.getElementById("HomeTab_" + tabName).src = "/images/home_tab" + tabNum + "_sel.gif";
        } else {
	  document.getElementById("HomeTab_VariableRates").src = "/images/home_tab2_sel_index.gif";
        }
	document.getElementById("Content_" + tabName).style.display = "block";

	// Focus Zip Code field
	if (tabName == 'RealTimeQuote')
		document.getElementById("email_realTime").focus();
	 else
		document.getElementById("ZipCode_" + tabName).focus();
}

function GetHomeTabSelected() {
  var possible_tabs = { 'FixedRates': 1, 'VariableRates': 2, 'HeatRates': 3, 'RealTimeQuote': 4 };
  for (var tab_id in possible_tabs) {
    if (document.getElementById("Content_" + tab_id) && document.getElementById("Content_" + tab_id).style.display == 'block') {
      return possible_tabs[tab_id];
    };
  }
  // should never reach this point
  return 0;
}

// Copy addresses
function CopyBusinessAddress(checked) {
  var c, lockin
  if($('user_deal_serviceaddress')){
    c = $('user_deal_serviceaddress').checked
    lockin = true
  }
  if($('user_deals_attributes_0_serviceaddress')){
    c = $('user_deals_attributes_0_serviceaddress').checked
    lockin = false
  }
	if (c) {
          $$('input[service_address_address_field]').each(function (el) {
              el.value = $('user_address').value;
          });
          $$('input[service_address_city_field]').each(function (el) {
            el.value = $('user_city').value;
          });
          $$('select[service_address_state_field]').each(function(select) {
            $(select).childElements()[$('user_state').selectedIndex].selected = true;
          });
          $$('input[service_address_zip_field]').each(function (el) {
            el.value = $('user_zip').value;
          });
	}
}

// ---------------------------------------------------------------------------------------
// RefreshNow functions

// ChangeColor - id, color in hex
function ChangeColor(idName, color) {
	document.getElementById(idName).style.backgroundColor = color;
}
function ResetColor(idName) {
	document.getElementById(idName).style.backgroundColor = "";
}
// RefreshCell - id | pos or neg sign | include arrow? | onlybg
function RefreshCell(idName, sign, arrow, onlybg) {
	if (sign >=0) {
		// Add Arrow & Green or Red font color
		if (onlybg == false) {
			if (arrow == true) {
				document.getElementById(idName).className = "pos arrowup";
			} else {
				document.getElementById(idName).className = "pos";
			}
		}
		// Update Background Color
		ChangeColor(idName, "#c5e8c5");
		timeoutFuncName = "ResetColor('"+idName+"');";
		setTimeout (timeoutFuncName, 1000);
	} else {
		// Add Arrow & Green or Red font color
		if (onlybg == false) {
			if (arrow == true) {
				document.getElementById(idName).className = "neg arrowdn";
			} else {
				document.getElementById(idName).className = "neg";
			}
		}
		// Update Background Color
		ChangeColor(idName, "#fbc9cb")
		timeoutFuncName = "ResetColor('"+idName+"');";
		setTimeout (timeoutFuncName, 1000);
	}
}
// Custom Call for DEMO only
function RefreshNow() {
	for (i=1; i<=4; i++) {
		RefreshCell("data_row" + i + "_col2", 1, false, true);
		RefreshCell("data_row" + i + "_col3", 1, false, false);
		RefreshCell("data_row" + i + "_col4", 1, true, false);
	}
	
	RefreshCell("data_row5_col2", -1, false, true);
	RefreshCell("data_row5_col3", -1, false, false);
	RefreshCell("data_row5_col4", -1, true, false);
	
	for (j=6; j<=12; j++) {
		RefreshCell("data_row" + j + "_col2", 1, false, true);
		RefreshCell("data_row" + j + "_col3", 1, false, false);
		RefreshCell("data_row" + j + "_col4", 1, true, false);
	}
}
// ---------------------------------------------------------------------------------------
// CheckFields_HomeRegBox | home.html
function CheckFields_HomeRegBox() {
	var field1 = document.getElementById("RegField1").value;
	var field2 = document.getElementById("RegField2").value;
	var field3 = document.getElementById("RegField3").value;
	
	if ( (field1!="") && (field2!="") && (field3!="") ) {
		document.getElementById("RegisterDiv").className = "submit_btn_corner submit_large";
		document.getElementById("RegisterBtn").disabled = false;
	} else {
		document.getElementById("RegisterDiv").className = "submit_btn_corner submit_large submit_off";
		document.getElementById("RegisterBtn").disabled = true;
	}
}
// ---------------------------------------------------------------------------------------
// CheckFields_RegisterHome | home.html
function CheckFields_RegisterHome() {
	var answer1 = document.getElementById("Answer1").checked;
	var answer2 = document.getElementById("Answer2").checked;
	var answer3 = document.getElementById("Answer3").checked;
	
	if (answer1 || answer2 || answer3) {
		document.getElementById("RegisterOKDiv").className = "submit_btn_corner btn_ok";
		document.getElementById("RegisterOKBtn").disabled = false;
	} else {
		document.getElementById("RegisterOKDiv").className = "submit_btn_corner btn_ok submit_off";
		document.getElementById("RegisterOKBtn").disabled = true;
	}
}
function CheckFields_RegisterHome2() {
	var answer1 = document.getElementById("Answer1").checked;
	var answer2 = document.getElementById("Answer2").checked;
	var answer3 = document.getElementById("Answer3").checked;
	
	if (answer1 || answer2 || answer3) {
		document.getElementById("RegisterOKDiv").className = "submit_btn_corner submit_large";
		document.getElementById("RegisterOKBtn").disabled = false;
	} else {
		document.getElementById("RegisterOKDiv").className = "submit_btn_corner btn_ok submit_off";
		document.getElementById("RegisterOKBtn").disabled = true;
	}
}
function CheckReceiveReport() {
	var answer1 = document.getElementById("Answer1").checked;
	var answer2 = document.getElementById("Answer2").checked;
	var answer3 = document.getElementById("Answer3").checked;
	
	if (answer1) {
		location.href = "free_analysis_report.html";
	} else if (answer2) {
		ShowPopup('HomeRegisterPop2');
	} else {
		HideMaskingBG();
		HomeShowDD();
	}
}
// ---------------------------------------------------------------------------------------
// CheckFields_Register | register.html
function CheckFields_Register() {
	if(document.getElementById('RegField1')!= null){
		var field1 = document.getElementById("RegField1").value;
		var field2 = document.getElementById("RegField2").value;
		var field3 = document.getElementById("RegField3").value;
	}else{
		var field1 = document.getElementById("user_company").value;
		var field2 = document.getElementById("user_company").value;
		var field3 = document.getElementById("user_company").value;

	}
	var answer1 = document.getElementById("Answer1").checked;
	var answer2 = document.getElementById("Answer2").checked;
	var answer3 = document.getElementById("Answer3").checked;
	
	if ( (field1!="") && (field2!="") && (field3!="") && (answer1 || answer2 || answer3) ) {
		document.getElementById("RegisterDiv").className = "submit_btn_corner submit_large";
		document.getElementById("RegisterBtn").disabled = false;
	} else {
		document.getElementById("RegisterDiv").className = "submit_btn_corner submit_large submit_off";
		document.getElementById("RegisterBtn").disabled = true;
	}
}
// ---------------------------------------------------------------------------------------
// Select All | Clear All
function SelectAll(idName, from, to) {
	for (i=from; i<=to; i++) {
		document.getElementById(idName+i).checked = true;
	}
}
function ClearAll(idName, from, to) {
	for (i=from; i<=to; i++) {
		document.getElementById(idName+i).checked = false;
	}
}
// ---------------------------------------------------------------------------------------
// Custom Dropdown functions
function DD_HideAll() {
  $$('.dropdown_custom').each(Element.hide);
}
function DD_Hide(id) {
  Element.hide(id);
}
function DD_Over(thisObj) {
	DD_Out(thisObj);
	document.getElementById(thisObj).className = "sel";
}
function DD_Out(thisObj) {
	document.getElementById(thisObj).className = "";
}
// Each dropdown needs a GLOBAL variable to store the default selected value
// arrayDD_Vars
// This is where you get "DD_Vars_Index" values
// ------------------------------
var arrayDD_Vars = [];
// CitiesDropdown: DD_Vars_Index = 0
arrayDD_Vars[0] = "North";
// PastDateDropdown: DD_Vars_Index = 1
arrayDD_Vars[1] = "PastDate12";
// MonthDropdown: DD_Vars_Index = 2
arrayDD_Vars[2] = "MmYyJun08";
// MonthDropdown: DD_Vars_Index = 3
arrayDD_Vars[3] = "MmYyVarJun08";
// MonthDropdown: DD_Vars_Index = 4
arrayDD_Vars[4] = "MmYyHeatJun08";
// CitiesDropdown_MCPE: DD_Vars_Index = 5
arrayDD_Vars[5] = "North";
// CitiesDropdown_Heat: DD_Vars_Index = 6
arrayDD_Vars[6] = "North";
// ------------------------------
var SelDefaultItem = false;
function DD_SelDefault(DD_Vars_Index) {
	DD_Over(arrayDD_Vars[DD_Vars_Index]);
	SelDefaultItem = true;
}
function DD_UnSelDefault(DD_Vars_Index) {
	if (SelDefaultItem) {
		DD_Out(arrayDD_Vars[DD_Vars_Index]);
		SelDefaultItem = false;
	}
}
// FieldId: is the ID name of div or input to update selection
// UpdateInput: 1 = INPUT.value, 0 = innerHTML
function DD_SelItem(thisObj, FieldId, UpdateInput, DD_Vars_Index) {
	SelId = thisObj + "";
	if (UpdateInput == 1) {
		document.getElementById(FieldId).value = document.getElementById(SelId).innerHTML;
		//document.getElementById(ItemFieldId).value = Item_Id;
	} else {
		document.getElementById(FieldId).innerHTML = document.getElementById(SelId).innerHTML;
		//document.getElementById(ItemFieldId).value = Item_Id;
	}
	arrayDD_Vars[DD_Vars_Index] = SelId;
}
// ---------------------------------------------------------------------------------------
// SetTableScrollHeight - for fixed rate | rates.html
function SetTableScrollHeight(numRows) {
	if (numRows>9) {
		document.getElementById("FixedRateScrollDiv").className = "tb_scroll";
	} else {
		document.getElementById("FixedRateScrollDiv").className = "tb_scroll_noheight";
	}
}
// ---------------------------------------------------------------------------------------
// Past Date dropdown | home.html
// PastDateVal is the DEFAULT item selected in the custom dropdown
var PastDateVal = "PastDate12";
function UnSelPastDate() {
	document.getElementById(PastDateVal).className = "";
}
function SelDefaultPastDate() {
	document.getElementById(PastDateVal).className = "sel";
}
function SelPastDate(thisObj) {
	PastDateVal = thisObj.id + "";
	document.getElementById("PastDateHTML").innerHTML = document.getElementById(PastDateVal).innerHTML;
}
// ---------------------------------------------------------------------------------------
// City dropdown | home.html
// CityVal is the DEFAULT item selected in the custom dropdown
var CityVal = "CityDallas";
function UnSelCity() {
	document.getElementById(CityVal).className = "";
}
function SelDefaultCity() {
	document.getElementById(CityVal).className = "sel";
}
function SelCity(thisObj) {
	CityVal = thisObj.id;
	document.getElementById("CityHTML").value = document.getElementById(CityVal).innerHTML;
}
// ---------------------------------------------------------------------------------------
// MmYy dropdown | home.html
// MmYyVal is the DEFAULT item selected in the custom dropdown
var MmYyVal = "MmYyJun08";
function UnSelMmYy() {
	document.getElementById(MmYyVal).className = "";
}
function SelDefaultMmYy() {
	document.getElementById(MmYyVal).className = "sel";
}
function SelMmYy(thisObj) {
	MmYyVal = thisObj.id;
	document.getElementById("MmYyHTML").value = document.getElementById(MmYyVal).innerHTML;
}

// ---------------------------------------------------------------------------------------
// SliderInit | SliderSel | SlideOver | SlideOut
// Select Slider Item
function SliderInit(sliderName,num) {
	// move slider
	var SliderLeft = (num*45);
	document.getElementById("Slider_" + sliderName).style.left = SliderLeft + "px";
	
	// Select correct scale_txt
	for (i=0; i<7; i++) {
		document.getElementById("ScaleTxt" + i + "_" + sliderName).className = "";
	}
	document.getElementById("ScaleTxt" + num + "_" + sliderName).className = "sel";
}
function SliderSel(sliderName,num) {
	// Select slider item
	SliderInit(sliderName,num);
	
	// Call Corresponding functions:
	if (sliderName == "MCPE") {
//		SliderSel_MCPE(num);
	} else if (sliderName == "Fixed") {
//		SliderSel_Fixed(num);
	}
}
function SlideOver(sliderName,num) {
	var sliderItem = document.getElementById("ScaleTxt" + num + "_" + sliderName);
	if (sliderItem.className != "sel") {
		sliderItem.className	= "over";
	}
}
function SlideOut(sliderName,num) {
	var sliderItem = document.getElementById("ScaleTxt" + num + "_" + sliderName);
	if (sliderItem.className != "sel") {
		sliderItem.className	= "";
	}
}
// ---------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------
// BODY functions
/*~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// home.html
function Home_Onclick() {
	HidePopup("HomeHelpBox");
	HidePopup("HomeHelpBox2");
	HidePopup("HomeHelpBox3");
	HidePopup("HomeHelpBox4");
}
function Home_Onmousedown() {
	HidePopup("CitiesDropdown");
	HidePopup("CitiesDropdown_MCPE");
	HidePopup("CitiesDropdown_Heat");
	HidePopup("CitiesDropdown_Real");
	HidePopup("PastDateDropdown");
	HidePopup("PastDateDropdownReal");
}
/* onresize */
function Home_Onresize() {
	ResizeMaskingBG();
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// rates.html
function OnMouseDown_Rates() {
	HidePopup("HelpBoxStep2");
	HidePopup("Fixed_HelpBox");
	HidePopup("Variable_HelpBox");
	HidePopup("Heat_HelpBox");
	HidePopup('Call_HelpBox');
	
	// IE6 Bug. Show hidden menus fields
	ShowPopup("FixedTermsMenu");
	ShowPopup("FixedRefreshMenu");
}
function Rates_Onmousedown() {
	HidePopup("MonthDropdown");
}
function RatesHideMenus() {
	HidePopup("FixedTermsMenu");
	HidePopup("FixedRefreshMenu");
}
/* onresize */
function Rates_Onresize() {
	ResizeMaskingBG();
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// rates_registered.html
function OnMouseDown_RatesRegUser() {
	HidePopup("HelpBoxStep2");
}
function get_current_state_and_dist_to_param() {
  return '&state=' + escape($('StateINPUT').value) + '&distributor=' + escape($('DistributorINPUT').value);
}

function MakeMeterNumberContigous() {
  var number = 1;
  $$('.meter:visible').each(function (element) {
    $(element).select('.num_meter_text').each(function (span) {
      $(span).update(number);
    });
    $(element).setAttribute('id', 'Meter'+number);
    number++;
  });
}

function NextServiceAddressId() {
  var length = 0;
  $$('.meter').each(function () { length++; });
  return length;
}
