function trim(str)
{	
	var lead = 0, trail = str.length - 1;

	while(str.charAt(lead) == ' ') lead++;
	while(str.charAt(trail) == ' ') trail--;

	return lead > trail ? '' : str.substring(lead, trail + 1);
}

function checkEmail(strEmail)
{
	 if (strEmail.indexOf('@',0) < 0 || strEmail.indexOf('@',0) == 0 || strEmail.indexOf('.',0) < 0) 
	{
		alert('Please type a valid e-mail address!');
		return false;
	}
	return true;
}

function checkPhone(strPhone) 
{
	var goodChars = "0123456789()-+ ";
	var i = 0;	

	for (i =0; i < strPhone.length; i++)
	{
		if (goodChars.indexOf(strPhone.charAt(i)) == -1)
		{
			alert('Please type a valid phone number!');
			return false;
		} 
	}	
	if (strPhone.length < 6)
	{
		alert('Please type a full phone number!');
		return false;
	}
	return true;
}

function showAlert()
{
	strAlert = 'Please provide all required information';
	alert(strAlert);
}

function checkForm(frmObj)
{
	var found;
	var i, ii;
	var curField, curName, curType;
	var strReq = '_req';
	var isFilled, isRequired;	
	var passCounter = 0;
	var passField1;

	with(frmObj)
	{
		for (i = 0; i < elements.length; i++)
		{
			curField = elements[i];	
			
			curField.value = trim(curField.value);

			curName = curField.name;
			curType = curField.type;
			isFilled = curField.value != '' ? true : false;
			isRequired = curName.indexOf(strReq) != -1 ? true : false;

			if(curType == 'text' && isFilled && curName.indexOf('Email') != -1)
			{
				if (!checkEmail(curField.value))
				{
					curField.focus();
					curField.select();
					return false;
					break;
				}
			}
			if(curType == 'text' && isFilled && (curName.indexOf('Phone') != -1 || curName.indexOf('Fax') != -1 || curName.indexOf('Cell') != -1))
			{
				if (!checkPhone(curField.value))
				{
					curField.focus();
					curField.select();
					return false;
					break;
				}
			}
			else if (elements[curName].length && isRequired &&  (curType == 'radio' || curType == 'checkbox'))
			{	
				found = 0;

				for (ii = 0; ii < elements[curName].length; ii++)
				{
					if (elements[curName][ii].checked)
					{
						found = 1;
						break;
					}
				}
				if (found == 0)
				{	
					showAlert();
					elements[curName][0].focus();	
					elements[curName][0].click();
					return false;
					break;
				}
			}
			else if (isRequired && (curType == 'select-one' || curType == 'select-multiple'))
			{
				if (curField.selectedIndex == 0)
				{
					showAlert();
					curField.focus();
					return false;
					break;
				}
			}
			else if (isRequired && (curType == 'text' || curType == 'textarea' || curType == 'password'))
			{
				if (!isFilled)
				{
					showAlert();
					curField.focus();
					return false;
					break;
				}
			}
			if (curType == 'password' && passCounter == 0)
			{
				passCounter++;
				passField1 = curField;
			}
			else if (curType == 'password' && passCounter == 1)
			{
				if (passField1.value != curField.value)
				{
					alert('The entered passwords do not match.\nPlease re enter them.');
					curField.value = "";
					passField1.focus();
					passField1.select();
					return false;
					break;
				}				
			}
		}		
	}	
	return true;
}								

function manualSubmit(frmObj)
{	
	if(checkForm(frmObj))
	{
		frmObj.submit();
	}
}

function manualReset(frmObj)
{		
	frmObj.reset();	
}
