/*
* The programming and software materials herein are copyright Cyberhomes LLC (CH).
* The programming and software materials are owned, held, or licensed by CH. Personal, educational,
* non-commercial, commercial or any other use of these materials, without the written permission of the
* CH, is strictly prohibited.
*/

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Filename: val_township.js
// Validation logic for Township search fields
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function validateTownshipFields()
	{
        // Before we start, let's make sure that the fields exist on the page. It's possible that this method is called without
        // the fields actually being present on the screen because of the way the JS was originally set up. (03/08/2005 [Vince Horst])
		var valid = true;
		if(document.ListingSearch.elements['Criteria/TownshipSearch/Township1'])
		{
		        valid = validateTownship(document.ListingSearch.elements['Criteria/TownshipSearch/Township1'].value) &&
			    validateTownshipRange(document.ListingSearch.elements['Criteria/TownshipSearch/TownshipRange1'].value) &&
			    validateTownshipSection(document.ListingSearch.elements['Criteria/TownshipSearch/TownshipSection1'].value) &&
			    validateTownship(document.ListingSearch.elements['Criteria/TownshipSearch/Township2'].value) &&
			    validateTownshipRange(document.ListingSearch.elements['Criteria/TownshipSearch/TownshipRange2'].value) &&
			    validateTownshipSection(document.ListingSearch.elements['Criteria/TownshipSearch/TownshipSection2'].value);
    			
		    if (valid)
		    {
			    document.ListingSearch.elements['Criteria/TownshipSearch/TownshipSection1'].value = ReformatTownshipSection(document.ListingSearch.elements['Criteria/TownshipSearch/TownshipSection1'].value);
			    document.ListingSearch.elements['Criteria/TownshipSearch/TownshipSection2'].value = ReformatTownshipSection(document.ListingSearch.elements['Criteria/TownshipSearch/TownshipSection2'].value);
		    }
		}
		return valid;
	}

	function validateTownship(township)
	{
		if (township.length>1) 
		{
			townshipDirection = township.charAt(township.length-1).toUpperCase();
			if (townshipDirection != "N" && townshipDirection != "S" ) 
			{
				alert("Invalid township specified.  Please specify N or S as direction.");
				return false;
			}
			
			townshipNumber = township.substr(0,township.length-1);
			if (isNaN(townshipNumber))
			{
				alert("Invalid township specified.  Please specify a valid number before the township direction.");
				return false;
			}
		}
		return true;
	}

	function validateTownshipRange(range)
	{
		if (range.length>1) 
		{
			rangeDirection = range.charAt(range.length-1).toUpperCase();
			if (rangeDirection != "E" && rangeDirection != "W" )
			{
				alert("Invalid range specified.  Please specify E or W as direction.");
				return false;
			}
			
			rangeNumber = range.substr(0,range.length-1);
			if (isNaN(rangeNumber))
			{
				alert("Invalid range specified.  Please specify a valid number before the range direction.");
				return false;
			} 
		}
		return true;
	}

	function validateTownshipSection(section)
	{
		if (section.length>1) 
		{
			var i, sections;
			var validSections = true;
			sections = section.split(",")
			for (i=0;i<sections.length;i++)
			{
				if (sections[i].length>0) 
				{
					validSections = validSections && validateTownshipSectionSingle(sections[i]);
				}
				if (!validSections)
				{
					return validSections;
				}
			}
			return validSections;
		}
		return true;
	}

	function validateTownshipSectionSingle(section)
	{
		if (section.indexOf("-")>0) 
		{
			return validateTownshipSectionSingle(section.substring(0,section.indexOf("-")))
				&& validateTownshipSectionSingle(section.substring(section.indexOf("-")+1));
		}
		else 
		{
			if (isNaN(section) || section<1 || section>36 )
			{
				alert("Invalid Section entered.  Please enter number(s) between 1 and 36 only.");
				return false;
			}
			return true;
		}
	}

	function ReformatTownshipSection(section)
	{
		var reformatedSection = '';
		if (section.length>0)
		{
			section = stripCharsInBag(section, " ");
			var i, sections;
			sections = section.split(",")
			for (i=0;i<sections.length;i++)
			{
				if (sections[i].length>0) 
				{
					var thisSection = '';
					thisSection = sections[i];
					if (thisSection.length<2) 
					{
						thisSection = "0" + thisSection;
					}
				
					if (thisSection.indexOf("-")>0)
					{
						thisSection = SplitRange(thisSection);
					}

					if (reformatedSection.length>0)
					{
						reformatedSection += ',' + thisSection;
					}
					else 
					{
						reformatedSection = thisSection;
					}
				}
			}
		}
		return reformatedSection;
	}

	function SplitRange(section)
	{
		if (section.indexOf("-")>0) 
		{
			var CommaDelimitedRange = '';
			var fromSection = parseInt(section.substring(0,section.indexOf("-")));
			var toSection = parseInt(section.substring(section.indexOf("-")+1));
			for (i=fromSection; i<=toSection; i++) 
			{
				if (CommaDelimitedRange.length > 0)
				{
					if (i<10) 
					{
						CommaDelimitedRange += ',0' + i;
					}
					else 
					{
						CommaDelimitedRange += ',' + i;
					}
				}
				else 
				{
					if (i<10) 
					{
						CommaDelimitedRange += '0' + i;
					}
					else 
					{
						CommaDelimitedRange += i;
					}
				}
			}
			return CommaDelimitedRange;
		}
		else 
		{
			return section;
		}
	}
