function Validate(form){

  if (form.TYPE_OF_DWELLING.value == "" || form.TYPE_OF_DWELLING.value == "Pick One"){
    alert("You must specify a type of dwelling.");
    form.TYPE_OF_DWELLING.focus();
    return false;
  }
  if (form.STREET_NUMBER.value.length < 1){
    alert("You must specify a street number.");
    form.STREET_NUMBER.focus();
    return false;
  }
  if (form.STREET_ADDRESS.value.length < 1){
    alert("You must specify a street name.");
    form.STREET_ADDRESS.focus();
    return false;
  }
  if (form.LOCATION_CODE.value == "" || form.LOCATION_CODE.value == "Pick One"){
    alert("You must specify a location code.");
    form.LOCATION_CODE.focus();
    return false;
  }
  
  if (form.BEDROOMS.value == "" || form.BEDROOMS.value == "-" || form.BEDROOMS.value == "Pick One"){
    alert("You must specify # of bedrooms.");
    form.BEDROOMS.focus();
    return false;
  }
  if (form.BATHROOMS.value == "" || form.BATHROOMS.value == "-" || form.BATHROOMS.value == "Pick One"){
    alert("You must specify # of bathrooms.");
    form.BATHROOMS.focus();
    return false;
  }
  if (form.AS_IS_SELLING_PRICE.value.length < 1){
    alert("You must specify an As Is price.");
    form.AS_IS_SELLING_PRICE.focus();
    return false;
  }
  if (form.SELLING_PRICE_C.value.length < 1){
    alert("You must specify a selling price.");
    form.SELLING_PRICE_C.focus();
    return false;
  }
  if (form.PRICE_NOFEE.value.length < 1){
    alert("You must specify a Sunflower's price.");
    form.PRICE_NOFEE.focus();
    return false;
  }

  return true;
}
function ValidateYear(form, item, next_field_id)
{
	// Makes sure that this is a valid 4 digit year
	if (item.value != '')
	{
		new_value = ConvertToInt(item, '');
		if (new_value < 1000)
		{
			alert('Please enter a valid 4-digit year');
			form.elements[next_field_id].focus();
			item.focus();
			return false;
		}
	}

}
function ConvertToInt(item, null_value)
{
	if (!null_value)
	{
		null_value = '';
	}
	// clean value of non-numeric characters
	new_value = RemoveNonNumeric(item.value);

	// convert to int
	if (new_value != '')
	{
		new_value = parseInt(new_value);
	}
	else
	{
		new_value = null_value;
	}


	return new_value;
}
function AddFee(form) {
	total = 0;
	// clean input fields
	form.AS_IS_SELLING_PRICE.value = RemoveNonNumeric(form.AS_IS_SELLING_PRICE.value);
	form.SELLING_PRICE_C.value = RemoveNonNumeric(form.SELLING_PRICE_C.value);

	if (form.AS_IS_SELLING_PRICE.value != '')
	{
		form.AS_IS_SELLING_PRICE.value = parseInt(form.AS_IS_SELLING_PRICE.value);
	}
    if (form.SELLING_PRICE_C.value != '')
	{
		form.SELLING_PRICE_C.value = parseInt(form.SELLING_PRICE_C.value);
	}

	if (form.AS_IS_SELLING_PRICE.value != "") {
		total = parseInt(1.06 * parseInt(form.AS_IS_SELLING_PRICE.value));
		form.AS_IS_SELLING_PRICE_wfee.value = total;
	}
	else
	{
		// set As Is Selling Price With Fee also to blank
		form.AS_IS_SELLING_PRICE_wfee.value = '';
	}
	if (form.SELLING_PRICE_C.value != "") {
		total = parseInt(1.06 * parseInt(form.SELLING_PRICE_C.value));
		form.SELLING_PRICE_C_wfee.value = total;
	}
    else
	{
		// set Selling Price With Fee also to blank
		form.SELLING_PRICE_C_wfee.value = '';
	}
}
function CheckPropertyID(form){
	if (form.PROPERTY_ID.value.length < 1){
    alert("You must specify a property ID#.");
    form.PROPERTY_ID.focus();
    return false;
  }
  form.PROPERTY_ID.focus();
  return true;
}

function CheckStreetName(form){
	if (form.STREET_NAME.value.length < 1){
    alert("You must specify a Street Name.");
    form.STREET_NAME.focus();
    return false;
  }
  return true;
}

function CheckPersonalInfo(form){
	if (form.user_id.value.length < 1){
    alert("You must specify a ID #.");
    form.user_id.focus();
    return false;
  }
  if (form.contact_fname.value.length < 1){
    alert("You must specify a first name.");
    form.contact_fname.focus();
    return false;
  }
  if (form.contact_lname.value.length < 1){
    alert("You must specify a last name.");
    form.contact_lname.focus();
    return false;
  }
  if (form.user_username.value.length < 1){
    alert("You must specify a login ID.");
    form.user_username.focus();
    return false;
  }
  if (form.user_type.value.length < 1){
    alert("You must specify a user type.");
    form.user_type.focus();
    return false;
  }
  if (form.user_password.value.length < 7 || form.user_password.value.length > 14){
    alert("Password must be 7-14 digits.");
    form.user_password.focus();
    return false;
  }
  return true;
}
function OpenNewWindow( url, width, height, options, name ) {
	if (! width ) width = 570;
	if (! height ) height = 570;
	if (! options ) options = "scrollbars=no,menubar=no,toolbar=no,location=no,status=no,resizable=yes";
	if (! name ) name = "otherWindows";
	var newWin = window.open( url, name, "width=" + width + ",height=" + height + "," + options );
}

function RemoveNonNumeric( strString )
{
	// Variables
	var strValidCharacters = "1234567890.";
	var strReturn = "";
	var strBuffer = "";
	var intIndex = 0;


	// Loop through the string
	for( intIndex = 0; intIndex < strString.length; intIndex++ )
	{
		// Get this character
		strBuffer = strString.substr( intIndex, 1 );

		// Is this a number
		if( strValidCharacters.indexOf( strBuffer ) > -1 )
		{
			// Yes
			strReturn += strBuffer;
		}
	}

	// Return the value
	return strReturn;
}

function CleanData( strValidCharacters, strString )
{
	// Works like RemoveNonNumeric above, but allows programmer to sepcify the
	// valid cahracter.  Here's an example:
	// newString = CleanData('1234567890.,$', someString);

	// Variables
	var strReturn = "";
	var strBuffer = "";
	var intIndex = 0;


	// Loop through the string
	for( intIndex = 0; intIndex < strString.length; intIndex++ )
	{
		// Get this character
		strBuffer = strString.substr( intIndex, 1 );

		// Is this a number
		if( strValidCharacters.indexOf( strBuffer ) > -1 )
		{
			// Yes
			strReturn += strBuffer;
		}
	}

	// Return the value
	return strReturn;
}

