// FormCheck.js

// whitespace characters
var whitespace = " \t\n\r";

// Check whether string s is empty.
function isEmpty(s)
{
	return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or has whitespace characters only.
function isWhitespace (s)
{
	if (isEmpty (s)) return true;
	
	// Search through string's characters one by one
	// until we find a non-whitespace character.
	// When we do, return false; if we don't, return true.
	for (var i = 0; i < s.length; i++)
	{
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		
		if (whitespace.indexOf(c) == -1) return false;
	}
	
	return true;
}

// Returns true if form element is empty or has whitespace characters only.
function isFormFieldEmpty (form_field, type)
{
	//alert (form_field.name);
	//alert (form_field.type);
	//alert (form_field);
	//alert (form_field.value);
	//alert (form_field.selectedIndex);
	//alert (form_field.length);
	//alert (form_field.checked);
	
	if (type == 'htmlarea' && miupdate_editor == 'tinymce')
	{
		form_field.value = tinyMCE.getContent (form_field.name);
	}
	
	if (type == 'multiselect')
	{
		//alert ('Multiselect Empty: '+form_field.name);
		//alert (form_field.selectedIndex);
		if (form_field.selectedIndex == -1) return true;
	}
	else if (type == 'checkbox')
	{
		//alert (form_field.checked);
		if (!form_field.checked) return true;
	}
	else if (type == 'radiobutton')
	{
		var checked = false;
		for (var i = 0; i < form_field.length; i++)
		{
			if (form_field[i].checked)
			{
				checked = true;
				break;
			}
		}
		
		if (!checked) return true;
	}
	else if (isWhitespace (form_field.value))
	{
		//alert ('isWhitespace: '+form_field.name);
		return true;
	}
	
	// Form field is not empty
	return false;
}

// Strips string s of HTML and returns it
function stripHTML (s)
{
	return s.replace(/<[^>]*>/g, '');
}

// Strips string s of HTML and returns it
function stripHTML2 (s)
{
	var strippedString = '';
	var inTag = false;
	for (var i = 0; i < s.length; i++)
	{
		if (s.charAt(i) == '<') inTag = true;
		if (s.charAt(i) == '>')
		{
			inTag = false;
			i++;
		}
		
		if (!inTag) strippedString += s.charAt(i);
	}
	
	return strippedString;
}

