function SetInitialFocus()
{
	var _element = null;
	if (document.theForm.vCompany.value.length == 0 && document.theForm.vCompany.type != 'hidden' && document.theForm.vCompany.disabled != true) {
		_element = document.theForm.vCompany;
	} else if (document.theForm.vLoginname.value.length == 0 && document.theForm.vLoginname.type != 'hidden' && document.theForm.vLoginname.disabled != true) {
		_element = document.theForm.vLoginname;
	} else if (document.theForm.vPassword.type != 'hidden' && document.theForm.vPassword.disabled != true) {
		_element = document.theForm.vPassword;
	}
	if (_element != null) {
		_element.focus();
		if (_element.type == 'text') {
			_element.select();
		}
		return true;
	}
	return false;
}

function onError(form_object, input_object, object_value, error_message)
{
	alert(error_message);
	if (input_object.type != 'hidden' && input_object.disabled != true)
		input_object.focus();
	return false;
}

function hasValue(obj, obj_type)
{
	if (obj_type == "TEXT" || obj_type == "PASSWORD") 
	{
    	if (obj.value.length == 0) 
      		return false;
    	else 
      		return true;
	}
    else if (obj_type == "SELECT")
	{
        for (i=0; i < obj.length; i++)
	   	{
			if (obj.options[i].selected)
				return true;
		}
       	return false;	
	}
    else if (obj_type == "SINGLE_VALUE_RADIO" || obj_type == "SINGLE_VALUE_CHECKBOX")
	{
		if (obj.checked)
			return true;
		else
       		return false;	
	}
    else if (obj_type == "RADIO" || obj_type == "CHECKBOX")
	{
        for (i=0; i < obj.length; i++)
    	{
			if (obj[i].checked)
				return true;
		}
       	return false;	
	}
}

function checkForm(_form, checkpwd)
{
	if (!hasValue(_form.vCompany, "TEXT")) 
	{
		onError(_form, _form.vCompany, _form.vCompany.value, "Company is required.");
		return false; 
	}
	if (!hasValue(_form.vLoginname, "TEXT")) 
	{
		onError(_form, _form.vLoginname, _form.vLoginname.value, "Username is required.");
		return false; 
	}
	if (checkpwd && !hasValue(_form.vPassword, "PASSWORD")) 
	{
		onError(_form, _form.vPassword, _form.vPassword.value, "Password is required.");
		return false; 
	}
    return true;
}

function RequestPassword()
{
	// Validate the form without password
	if (!checkForm(document.theForm, false))
		return;
	var message = "Your password will be sent to your email address.\n\nAre you sure?";
	if (!confirm(message))
		return;
	// Submit
	document.theForm.vRequestPassword.value = "1";
	document.theForm.submit();
}
