function PopUpWindow(url)
{
	window.open(url, 'newWin', 'alwaysRaised,resizable,menubar,scrollbars,width=900,height=800');
}

function DisplayReport(path, format, parameters)
{
	url = '/CertificationPortal/Pages/ReportViewer.aspx?Path=' + path + '&Format=' + format + parameters;
	PopUpWindow(url);
}

function DisplayControl(controlName)
{
	if(document.getElementById(controlName) != null)
	{
		document.getElementById(controlName).style.display='';
	}
}

function HideControl(controlName)
{
	if(document.getElementById(controlName) != null)
	{
		document.getElementById(controlName).style.display='none';
	}
}

function EnableControlIfControlChecked(controlToEnableOrDisable, checkableControl)
{
	if(document.getElementById(controlToEnableOrDisable) != null)
	{
		EnableDisableCheckBox(controlToEnableOrDisable, (document.getElementById(checkableControl) != null && document.getElementById(checkableControl).checked));
	}
}

function EnableControl(controlName)
{
	if(document.getElementById(controlName) != null)
	{
		document.getElementById(controlName).disabled='';
	}
}

function DisableControl(controlName)
{
	if(document.getElementById(controlName) != null)
	{
		document.getElementById(controlName).disabled='disabled';
	}
}

function EnableDisableControl(controlName, enable)
{
	if(document.getElementById(controlName) != null)
	{
		if(enable)
		{
			document.getElementById(controlName).disabled='';
		}
		else
		{
			document.getElementById(controlName).disabled='disabled';
			document.getElementById(controlName).value='';
		}
	}
}

function EnableDisableCheckBox(controlName, enable)
{
	if(document.getElementById(controlName) != null)
	{
		if(enable)
		{
			document.getElementById(controlName).disabled='';
		}
		else
		{
			document.getElementById(controlName).disabled='disabled';
			document.getElementById(controlName).checked=false;
		}
	}
}
//
//This function loads the appropriate states and provinces based on the currently selected country
//
function LoadStateProvinces(sCountryControlId, sStateProvinceControlId, sSelectedValue)
{
	var oCountry = document.getElementById(sCountryControlId);
	var oStateProvince = document.getElementById(sStateProvinceControlId);
	
	if (!IsNull(oCountry) && !IsNull(oStateProvince))
	{	
		var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		oXmlDoc.async = false; 
		oXmlDoc.load("/CertificationPortal/Pages/AJAX/GetGenericValue.aspx?sCommand=LoadStates&sCountryId=" + oCountry.value);
		
		if (!DidErrorOccur(oXmlDoc, "An unknown error occured while retrieving the States for the selected Country."))
		{
			// Clear the list
			// Changing the length to 0 doesn't actually remove the optons		
			while (oStateProvince.options.length > 0)
			{
				oStateProvince.remove(0);
			}
			
			loadListBoxFromXml(oStateProvince, oXmlDoc.documentElement, sSelectedValue)
		}	
	}
}

function loadListBoxFromXml(listBox, documentElement, selectedValue)
{
	if(documentElement.childNodes.length > 0)
	{
		listBox.disabled = false;
		for(var i = 0; i < documentElement.childNodes.length; i++)
		{
			var displayValue = documentElement.childNodes[i].getAttribute("displayvalue");
			var id = documentElement.childNodes[i].getAttribute("id");
			
			if(displayValue != null && displayValue != "" && id != null && id != "")
			{
				listBox.options[i] = new Option(displayValue, id);
				if(id == selectedValue)
				{
					listBox.selectedIndex = i;
				}
			}
		}
	}
	else
	{
		listBox.disabled = true;
	}
}

function ValidateQuantityTextBox(textBoxId, minValue, maxValue)
{
	var textBox = document.getElementById(textBoxId);

	if(textBox.disabled == '')
	{
		if(isNaN(textBox.value) || parseInt(textBox.value) < minValue || trim(textBox.value) == '')
		{
			textBox.value = minValue;
		}
		else if(parseInt(textBox.value) > maxValue)
		{
			textBox.value = maxValue;
		}
		else 
		{
			textBox.value = parseInt(textBox.value);
		} 
	}
}

function ValidateInteger(value, defaultValue)
{
	if(value == null || isNaN(value) || trim(value) == '')
	{
		value = defaultValue;
	}
	else 
	{
		value = parseInt(value);
	} 
	return value;
}

//Credit: http://www.redantigua.com/js-trim.html
function trim(s) 
{
	while (s.substring(0,1) == ' ') 
	{
		s = s.substring(1,s.length);
	}
	while (s.substring(s.length-1,s.length) == ' ') 
	{
		s = s.substring(0,s.length-1);
	}
	return s;
}

function OpenChild(address, args, winsettings) 
{
    var args = window.showModalDialog(address, args, winsettings);
    return args
}

// Notifies the user if an error occured
function DidErrorOccur(oXmlDoc, sMessage)
{
	// If an error occured the document element will be error
	var oError = oXmlDoc.selectSingleNode("error");
	
	// Display message if error exists
	if (oError != null)
	{			
		alert(oError.text);
		return true;
	}
	
	// If xml is blank the page probably didn't load correctly 
	if (oXmlDoc.xml == "")
	{		
		alert(sMessage);
		return true;
	}
	
	return false;
}  

function IsNull(o)
{
	return ("undefined" == typeof(o) || "unknown" == typeof(o) || null == o)
}
