var xmlDoc;
var XMLDocument;
function loadXML(file)
{
	// Determine whether we are using Mozilla or IE based browser, then create xmlDoc item
	// MOZILLA
	if ((typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined'))
	{
		xmlDoc = document.implementation.createDocument("", "", null)
		xmlDoc.async = false; // Allows localhost XML files to be read by JavaScript
	}
	// IE
	else if (typeof window.ActiveXObject != 'undefined')
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false; // Allows localhost XML files to be read by JavaScript
		while(xmlDoc.readyState != 4) {}; 
	}
	// Load all data from XML file
	xmlDoc.load(file);
	// Retrieve root node of XML document
	XMLDocument = xmlDoc.documentElement;
}

function isValidEmail(email, required)
{
	if (required==undefined)
	{   
		// if not specified, assume it's required
		required=true;
	}
	if (email==null)
	{
		if (required)
		{
			return false;
		}
		return true;
	}
	if (email.length==0)
	{  
		if (required)
		{
			return false;
		}
		return true;
	}
	if (!allValidChars(email))
	{  
		// check to make sure all characters are valid
		return false;
	}
	if (email.indexOf("@") == -1)
	{ 
		//  must contain @, and it must not be the first character
		return false;
	}
	else if (email.lastIndexOf(".") <= email.indexOf("@"))
	{  
		// last dot must be after the @
		return false;
	}
	else if (email.indexOf("@") == email.length) 
	{
		// @ must not be the last character
		return false;
	}
	else if (email.indexOf("..") > -1)
	{ 
		// two periods in a row is not valid
		return false;
	}
	else if (email.indexOf(".") == email.length)
	{  
		// must not be the last character
		return false;
	}
	else if (emailDomainSuffixInvalid(email))
	{
		// domain suffix must be in allowed list from XML document
		return false;
	}
	return true;
}

function emailDomainSuffixInvalid(email)
{
	var pos = email.lastIndexOf(".");
	var domainSuffix = email.substring(pos+1, email.length);
	var suffixes = XMLDocument.getElementsByTagName('suffix');
	for (var i = 0; i < suffixes.length; i++)
	{
		var suffix = suffixes[i].firstChild.nodeValue;
		if (!IsNullOrEmpty(suffix))
		{
			if (suffix.toUpperCase() == domainSuffix.toUpperCase())
			{
				return false;
			}
		}
	}
	return true;
}

function CheckIfFieldIsBlank(obj)
{
	if (obj.value != "")
	{
		obj.style.backgroundColor = null;
	}
}

function IsNullOrEmpty(value)
{
	if (value == null)
	{
		return true;
	}
	else if (value == "")
	{
		return true;
	}
	else
	{
		return false;
	}
}

function createOption(obj,newValue,newText)
{
	var objSelect = obj;
	var objOption = document.createElement("option");
	objOption.text = newText
	objOption.value = newValue
	
	if(document.all && !window.opera)
		{objSelect.add(objOption);}
	 else
		{objSelect.add(objOption, null);};
}

function allValidChars(email)
{
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
