//Closes the current window.
function CloseWindow()
{
    //if( confirm('Do you want to close?') ){
	//    window.opener.close();
	//}else{
	//    return;
	//}
	window.close();
//	this.focus();
//	self.opener = this;
//	self.close();
}

//Enables a control and any validators that reference it.
//id - string - the control's id attribute. Must be the client id.
//enable - bool - true to enable, false to disable.
function EnableValidatedControl(id, enable)
{
	EnableControl(id, enable);
	EnableValidator(id, enable);
}

//Enables a control.
//id - string - the control's id attribute. Must be the client id.
//enable - bool - true to enable, false to disable.
function EnableControl(id, enable)
{
	var control = document.getElementById(id);
	
	if (enable)
	{
		control.removeAttribute('Disabled', null, null);
	}
	else
	{
		control.setAttribute('Disabled', 'Disabled', null);
		control.value = '';
	}
}

//Enables the validation for a control.
//id - string - the id attribute for the control being validated. Must be the client id.
//enable - bool - true to enable, false to disable.
function EnableValidator(id, enable)
{
	for (var i = 0; i < Page_Validators.length; i++)
	{
		if (Page_Validators[i].controltovalidate == id)
		{
			ValidatorEnable(Page_Validators[i], enable);
			Page_Validators[i].style.display = 'none';
		}
	}
}

//Shows or hides the element with the given id.
//id - string - the id of the element to show.
//show - bool - true to show the element, false to hide it.
function DisplayElement(id, show)
{
	var element = document.getElementById(id);
	
	if (show)
	{
		element.style.display = '';
	}
	else
	{
		element.style.display = 'none';
	}
}

//Opens the given url in a new window.
//url - string - the url to open in a new window.
function OpenNewWindow(url)
{
	window.open(url);
}

//Prints the current page.
function PrintPage()
{
	window.print();
}

//Removes currency formatting from a string and returns the number it represents.
//value - string - the currency formatted number to parse.
//return - number - the number value of the passed currency.
function ParseCurrency(value)
{
	var formattedValue = value;
	
	formattedValue = formattedValue.replace(/,/g, '');
	
	return (+(formattedValue.replace('$', '')));
}

//Adds currency formatting to the given number and returns the resultant string.
//value - number - the value to format as currency.
//return - string - the formatted currency representation of the value.
function FormatCurrency(value)
{
	var formattedValue = value.toString();
	
	if (formattedValue.indexOf('.') == -1)
	{
		for (var i = formattedValue.length - 3; i > 0; i -= 3)
		{
			formattedValue = formattedValue.substring(0, i) + ',' + formattedValue.substring(i);
		}
		formattedValue = formattedValue + '.00';
	}
	
	if (formattedValue.lastIndexOf('.') == formattedValue.length - 2)
	{
		formattedValue = formattedValue + '0';
	}
	
	if (formattedValue.indexOf('$') == -1)
	{
		formattedValue = '$' + formattedValue;
	}
	
	return formattedValue;
}

//Sums the number values of the controls whose id's are in the array and sets the total display
//control's value to the result.
//controlsToTotalArray - array of strings - the control id's to sum.
//totalDisplayControl - string - the id of the control to use to show the total.
function GetTotal(controlsToTotalArray, totalDisplayControl)
{
	var tempNumber;
	var total = 0;

	for (var i = 0; i < controlsToTotalArray.length; i++)
	{
		tempNumber = ParseCurrency(document.getElementById(controlsToTotalArray[i]).value);
		if (!isNaN(tempNumber))
		{
			total += tempNumber;
		}
	}
	
	document.getElementById(totalDisplayControl).innerHTML = FormatCurrency(total);
}

function SetDependentChildrenValidationMessage(parentName, defaultName, validatedControlID)
{
	if (typeof(parentName.value) != 'undefined' && parentName.value)
	{
		defaultName = parentName.value;
	}
	
	var message = 'Please enter the number of dependent children for ' + defaultName + ' as a positive number less than 15, for example: 2';
	
	for (var i = 0; i < Page_Validators.length; i++)
	{
		if (Page_Validators[i].controltovalidate == validatedControlID)
		{
			Page_Validators[i].errormessage = message;
		}
	}
}

function SetNightsOfCareValidators(nightsControlID, selectedFrequency)
{
	var controlName = 'NightsOfCare' + selectedFrequency + 'Validator';
	
	for (var i = 0; i < Page_Validators.length; i++)
	{
		if (Page_Validators[i].id.indexOf(selectedFrequency) !== -1 && Page_Validators[i].controltovalidate === nightsControlID)
		{
			Page_Validators[i].enabled = true;
	    	Page_Validators[i].style.display = 'none';
		}
		else if (Page_Validators[i].controltovalidate === nightsControlID)
		{
			Page_Validators[i].enabled = false;
		}
	}
}

function ExpandAllSteps(panelBarID)
{
    var panelBar = document.getElementById(panelBarID);
    
	for (var i = 0; i < panelBar.control.get_allItems().length; i++)
	{
		panelBar.control.get_allItems()[i].expand();
	}
}