

// Toggle an elmeent to be displayed or blocked
function ToggleDisplay(name)
{
	if (document.getElementById) // DOM3 = IE5+, NS6+, Mozilla
	{	
		if (document.getElementById(name).style.display == 'none')
			document.getElementById(name).style.display = 'block';		
		else
			document.getElementById(name).style.display = 'none';		
	}			
	else // Only makes visible if hidden (I have no way to test these browsers)
	{	if (document.layers) // Netscape 4
			document.name.visibility = 'visible';
		else // IE 4
			document.all.name.style.visibility = 'visible';		
	}	
}

// Toggle an element between two values
function ToggleValue(name, v0, v1)
{	if (document.getElementById) // DOM3 = IE5+, NS6+, Mozilla
	{	if (document.getElementById(name).value == v0)
			document.getElementById(name).value = v1;		
		else
			document.getElementById(name).value = v0;  	
	}	
}

function ValidateLength(id, min, max)
{	return (document.getElementById(id).value.length >= min) && 
			(document.getElementById(id).value.length <= max);
}

function SetText(id, msg)
{	if (document.getElementById)
		document.getElementById(id).innerHTML = msg;
}

//=============================
// REGISTRATION FORM VALIDATION
//=============================
function CheckUsername()
{	if (!ValidateLength('username', 4, 20))
	{	SetText('usernamem', 'Username must be between 6 and 20 characters long.');
		return false;	}
	if (user.username.value.match(/[^a-z^A-Z^0-9^_]/))
	{	SetText('usernamem', 'Username must contain only A-Z, 0-9, and underscores.');
		return false;	}
	SetText('usernamem', '');
	return true;
}

function CheckPassword()
{	if (!ValidateLength('password', 6, 20))
	{	SetText('passwordm', 'Password must be between 6 and 20 characters long.');
		return false;	}
	if (user.password.value.match(/[^a-z^A-Z^0-9^_]/))
	{	SetText('passwordm', 'Password must contain only A-Z, 0-9, and underscores.');
		return false;	}
	SetText('passwordm', '');
	return true;
}

function CheckFirst()
{	if (!ValidateLength('first', 2, 20)) 
	{	SetText('firstm', 'First name must be between at least 2 characters long.');
		return false;	}
	if (user.first.value.match(/[^a-z^A-Z^.]/)) 
	{	SetText('firstm', 'First name must contain only A-Z.');
		return false;	}
	SetText('firstm', '');
	return true;
}

function CheckLast()
{	if (!ValidateLength('last', 2, 20)) 
	{	SetText('lastm', 'Last name must be between at least 2 characters long.');
		return false;	}
	if (user.last.value.match(/[^a-z^A-Z^.]/)) 
	{	SetText('lastm', 'Last name must contain only A-Z.');
		return false;	}
	SetText('lastm', '');
	return true;
}

function CheckEmail()
{	text= document.getElementById('email');
	a   = text.value.indexOf("@");
	dot = text.value.lastIndexOf(".");
	len = text.value.length-1;
	spc = text.value.indexOf(" ");
	if (a<1 || dot-a<2 || len-dot>6 || len-dot<2 || (spc<len && spc!=-1))
	{	SetText('emailm', 'Please enter a valid email address.');
		return false;	}
	SetText('emailm', '');
	return true;
}

function CheckType()
{	if (!user.type[0].checked && !user.type[1].checked)
	{	SetText('typem', 'Please choose an account type.');
		return false;	}
	SetText('typem', '');
	return true;	
}

function CheckAgree()
{	if (!user.agree.checked)
	{	SetText('agreem', 'You must agree to the terms below to proceed.<br>');
		return false;	}
	SetText('agreem', '');
	return true;
}

// Validate Registration Form
function ValidateUser()
{	
	a = CheckUsername();
	b = CheckPassword();
	c = CheckFirst();
	d = CheckLast();
	e = CheckEmail();
	f = CheckType(); 
	g = CheckAgree();
	if (a && b && c && d && e && f && g)
		return true;
	alert('Please recheck the form.  At least one of the fields doesn\'t meet the requirements.');
	return false;
}


//=============================
// AD FORM VALIDATION
//=============================
function CheckName()
{	if (!ValidateLength('name', 4, 40))
	{	SetText('namem', 'Ad Title must be at least 4 characters long.');
		return false;	}
	SetText('namem', '');
	return true;
}

function CheckCategory()
{	if (!ValidateLength('category', 4, 40))
	{	SetText('categorym', 'You must select a category.');
		return false;	}
	SetText('categorym', '');
	return true;
}

function CheckContact()
{	if (!ValidateLength('contact', 6, 40))
	{	SetText('contactm', 'Contact must be at least 6 characters long.');
		return false;	}
	SetText('contactm', '');
	return true;
}

function CheckPrice()
{	if (!ValidateLength('price', 2, 40))
	{	SetText('pricem', 'Price must be at least 2 characters long.');
		return false;	}
	SetText('pricem', '');
	return true;
}

function CheckCounty()
{	if (!ValidateLength('county', 3, 40))
	{	SetText('countym', 'You must select a county.');
		return false;	}
	SetText('countym', '');
	return true;
}

function CheckDescription()
{	if (!ValidateLength('description', 6, 1000000))
	{	SetText('descriptionm', 'Description must be at least 6 characters long.');
		return false;	}
	SetText('descriptionm', '');
	return true;
}

function CheckAgree2()
{	if (!ad.agree.checked)
	{	SetText('agreem', 'You must agree to the terms below to proceed.<br>');
		return false;	}
	SetText('agreem', '');
	return true;
}

function ValidateAd()
{	a = CheckName();
	b = CheckCategory();
	c = CheckContact();
	d = CheckPrice();
	e = CheckCounty();
	f = CheckDescription();
	g = CheckAgree2();
	if (a && b && c && d && e && f && g)
		return true;
	alert('Please recheck the form.  At least one of the fields doesn\'t meet the requirements.');
	return false;
}

//=============================
// FEEDBACK FORM VALIDATION
//=============================
function CheckFeedback()
{	
	if (!ValidateLength('id', 1, 8)) 
	{	SetText('idm', 'You must enter an ad id to leave feedback.');
		return false;	}
	if (feedback.id.value.match(/[^1-9]/)) 
	{	SetText('idm', 'The ad id must be a number.');
		return false;	}
	SetText('idm', '');
	return true;
}



