// ============================================================================
// Asks for confirmation from the user, before setting up the item to be deleted.
// The specified hidden field will be set to the item's id, and the form will be posted back.
// 
// string hiddenFieldName : The name of the hidden field to store the deletable item's id in.
// int itemId : The id of the item to delete.
// string itemName : The name of the item to delete.
// ============================================================================
function DeleteItem(hiddenFieldName, itemId, itemName)
{
	var confirmDelete = false;
	confirmDelete = window.confirm("Are you sure you want to delete item '" + itemName + "'?");
	if (confirmDelete)
	{
		var hidField = FindField(hiddenFieldName);
		hidField.value = itemId;
		window.document.forms[0].submit();
	}
}



// =============================================================================
// Validates the supplied password.
// string password = the password to validate
// int minLength = the minimum length of a valid password
// int maxLength = the maximum length of a valid password
// int complexity = How complex does a password have to be
//		1 : any characters
//		2 : one or more upper and one or more lowercase letters
//		3 : one or more upper and one or more lowercase letters plus one or more non letter
//		4 : one or more upper and one or more lowercase letters plus one or more digit 
//          plus one or more punctuation|symbol.
// =============================================================================
function ValidatePassword(password, minLength, maxLength, complexity)
{
	var patternLowerCaseLetters = /[a-z]/;
	var patternUpperCaseLetters = /[A-Z]/;
	var patternNonLetter = /[^A-Za-z]/;
	var patternDigits = /[0-9]/;
	var patternPunctuation = /[\W_]/;
	var isValid;
	
	// First test length
	if (password.length < minLength || password.length > maxLength)
	{
		isValid = false;
	}
	else
	{
		isValid = true;

		// Test in order of complexity.
		if (complexity >= 2)
		{
			isValid = patternLowerCaseLetters.test(password);
			if (isValid) isValid = patternUpperCaseLetters.test(password);
			if (complexity >= 3 && isValid)
			{
				isValid = patternNonLetter.test(password);
				if (complexity == 4 && isValid)
				{	
					isValid = patternDigits.test(password);
					if (isValid) isValid = patternPunctuation.test(password);
				}
			}
		}
	}
		
	return isValid;
} // ValidatePassword()


// ================================================================================
// Finds the specified field in the form.
// Checks to see if there is an element in the first form on the page that
// contains the 'findName' value in its name.
// Returns the last matching element if one exists, null if there are no matches.
// ================================================================================
function FindField(findName)
{
	var elementName;
	var element = null;
	findName = ":" + findName; 
	
	// Only bother if there is at least one form on the page.
	if (document.forms.length > 0)
	{
		// Loop through all the elements of the first form
		for (i = 0; i < document.forms[0].elements.length; i++)
		{
			// Grab the element name
			elementName = document.forms[0].elements[i].name;
			// Check to see if the name contains the specified text.
			//1. The elementName ends with findName. ex: _ctl2_txtName
			//2. The elementName containts  findName: (CheckBox List). ex: _ctl2:chkSubstrateSuitability:0
			if (elementName.indexOf(findName)>=0
				&&(
					elementName.indexOf(findName) == (elementName.length - findName.length)
					||elementName.indexOf(findName+":")>=0
					)		
				)
			{
				//alert(findName + "\n" + elementName + "\n" + elementName.indexOf(findName) + "\n" + (elementName.length - findName.length));
				
				// Found a match. Set the element.
				element = document.forms[0].elements[i];
				//There are multiple form element for radio and check box list
				//For checkbox list return the first checked element			
				if (element.type == "radio" || element.type == "checkbox")
				{
					if (element.checked)
						break;
					else
						element= null;
				}
			}//Found the element
		}//Loop through all elements
	}
	return element;
} // FindField()

// =========================================================
// Returns the first option object that has a 'value'
// property that matches strValue. If no match, returns null.
// - objSelect is the Select object that contains the 
//   options list.
// - strValue is the value to find in the options list.
// =========================================================
function FindOption(objSelect, strValue)
{
	var i;
	var objOption = null;
	for (i = 0; i < objSelect.length; i++)
	{
		if (objSelect.options[i].value == strValue)
		{
			objOption = objSelect.options[i];
			break;
		}
	}
	return objOption;
} // FindOption()

// =========================================================
// Returns the index of the first option object that has a 'value'
// property that matches strValue. If no match, returns null.
// - objSelect is the Select object that contains the 
//   options list.
// - strValue is the value to find in the options list.
// =========================================================
function FindOptionIndex(objSelect, strValue)
{
	var i;
	var intIndex = null;
	for (i = 0; i < objSelect.length; i++)
	{
		if (objSelect.options[i].value == strValue)
		{
			intIndex = i;
			break;
		}
	}
	return intIndex;
} // FindOption()


// User to display the Remind Me popup
function popRemindMe(sid) 
{
	window.open('?page=remindme&sid=' + sid,'RemindMe','width=390,height=200');
}