// set which form element to focus on, usually the first field of the form
function setFocus(the_form_name, the_field_name)
{
	var temp_form_name_string = String(the_form_name);
	var temp_field_name_string = String(the_field_name);
	
	eval("document." + temp_form_name_string + "." + temp_field_name_string + ".focus()");
}

// disables 2 buttons, use this to disable a submit/save button to prevent the user from submitting the form multiple times
function DisableButtons(jsActionButtonName, jsCancelButtonName)
{
	for(var x=0; x < document.forms.length; x++)
	{
		for(var y=0; y < document.forms[x].elements.length; y++)
		{
			if (document.forms[x].elements[y].name.indexOf(jsActionButtonName) == 0)
			{
				document.forms[x].elements[y].value = 'Processing...';
				document.forms[x].elements[y].disabled = true;
			}
			
			if (document.forms[x].elements[y].name.indexOf(jsCancelButtonName) == 0)
			{
				document.forms[x].elements[y].disabled = true;
			}
		}
	}
}

// check for an empty string (treat a string with spaces as empty)
function isEmpty(the_string)
{
	var temp_string = String(the_string), x = 0;
	
	for(x=0; x < temp_string.length; x++)
	{
		if(temp_string.charAt(x) != ' ')
			return false;
	}
	
	return true;
}

// determine whether a checkbox is checked
function isChecked(the_element)
{
	// the element should be document.formname.elementname
	var x=0;
	if( eval(the_element + ".length") )
	{
		for(x=0; x< eval(the_element + ".length"); x++)
		{
			if(eval(the_element + "[x].checked")) 
				return true;
		}
		return false;
	}
	else
	{
		if( eval(the_element + ".checked") ) 
			return true;
		else 
			return false;
	}
}

// check if a string has space(s)
function hasSpace(the_value)
{
	var temp_string = String(the_value), x = 0;

	for(x=0; x < temp_string.length; x++)
	{
		if(temp_string.charAt(x) == ' ') 
			return true;
	}
	
	return false;
}

// check if the value is an integer
function isInteger(the_value)
{
    var lnValue = the_value + "";	
    
    if (isEmpty(lnValue == ""))
		return false;

    for (var i = 0; i < lnValue.length; i++)
	{
    	if (lnValue.charAt(i) < "0" || lnValue.charAt(i) > "9")
    		return false;
	}
	
    return true;
}

// check if the value is a number (whole numbers, negative numbers, decimals)
function isNumber(the_value)
{
    var lnValue = the_value + "";	
    
    if (isEmpty(lnValue == ""))
		return false;
	
    for (var i = 0; i < lnValue.length; i++)
	{
    	if ((lnValue.charAt(i) < "0" || lnValue.charAt(i) > "9") &&
    		(lnValue.charAt(i) != "." && lnValue.charAt(i) != "-"))
    			return false;
    }
    
	return true;
}

// check if a string is conforms to a valid email format (i.e. xxxxxx@xxx.xxx)
function isEmail(the_string)
{
	var temp_string = String(the_string), x = 0, number_of_at = 0;
	var found_at = false, found_period = false;
	for(x=0; x < temp_string.length; x++)
	{
		// check for a space or a semicolon or a command which are invalid
		if(temp_string.charAt(x) == ' ' || temp_string.charAt(x) == ';' || temp_string.charAt(x) == ',' || temp_string.charAt(x) == '\'')
			return false;
		
		// check for the at-sign @ and count how many
		if(temp_string.charAt(x) == '@' && temp_string.charAt(x+1) != '')
		{
			number_of_at = number_of_at +1;
			found_at = true;
		}
		
		// check for the period
		if(temp_string.charAt(x) == '.'  && temp_string.charAt(x+1) != '')
			found_period = true;
	}
	
	if(number_of_at != 1)
		return false;
	else if(found_at && found_period)
		return true;
	else
		return false;
}

//
function openRegWin(urlRegWin)
{
	var lcWinSpec="width=800,height=700,toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes";
	window.open(urlRegWin, "regWin", lcWinSpec);
}
