function getTownship(tile) { return(tile.substring(0,2)) }
function getTownshipDir(tile) { return(tile.substring(2,3)) }
function getRange(tile) { return(tile.substring(3,5)) }
function getRangeDir(tile) { return(tile.substring(5,6)) }
function getSection(tile) { return(tile.substring(6,8)) }
function getSectionQuarter(tile) { return(tile.substring(8,10)) }
function getTownshipQuarter(tile) { 
	section = getSection(tile)
	if ((section>=1 && section<=3) || (section>=10 && section<=15))
		return("NE")
	if ((section>=4 && section<=9) || (section>=16 && section<=18))
		return("NW")
	if ((section>=19 && section<=21) || (section>=28 && section<=33))
		return("SW")
	if ((section>=22 && section<=27) || (section>=34 && section<=36))
		return("SE")
	return("") 
}
function getSectionString(strSection) 
{ 
	if(+strSection<10 && strSection.substring(0,1)!='0')
		strSection = '0' + +strSection

	return(strSection) 
}
function getTownshipRange(tile) { return(getTownship(tile)+getTownshipDir(tile)+getRange(tile)+getRangeDir(tile)) }

function getTile(dir,tile) {
	township = getTownship(tile)
	range = getRange(tile)
	section = getSection(tile)
	rangedir = getRangeDir(tile)
	quarter = getSectionQuarter(tile).toLowerCase()
	changeNS=false
	changeEW=false
	
	switch (dir.substring(0,1)) {
		case 'n':
			if (quarter.substring(0,1)=='n')
			{
				quarter = 'S'+quarter.substring(1,2).toUpperCase()
				changeNS=true
			}
			else
				quarter = 'N'+quarter.substring(1,2).toUpperCase()	
		break
		case 's':
			if (quarter.substring(0,1)=='n')
				quarter = 'S'+quarter.substring(1,2).toUpperCase()
			else
			{
				quarter = 'N'+quarter.substring(1,2).toUpperCase()	
				changeNS=true
			}
		break
		case 'e':
			if (quarter.substring(1,2)=='e')
			{
				quarter = quarter.substring(0,1).toUpperCase()+'W'
				changeEW=true
			}
			else
				quarter = quarter.substring(0,1).toUpperCase()+'E'
		break
		case 'w':
			if (quarter.substring(1,2)=='w')
			{
				quarter = quarter.substring(0,1).toUpperCase()+'E'
				changeEW=true
			}
			else
				quarter = quarter.substring(0,1).toUpperCase()+'W'
		break
	}
	switch (dir.substring(1,2)) {
		case 'e':
			if (quarter.substring(1,2)=='e' || quarter.substring(1,2)=='E')
			{
				quarter = quarter.substring(0,1).toUpperCase()+'W'
				changeEW=true
			}
			else
				quarter = quarter.substring(0,1).toUpperCase()+'E'
		break
		case 'w':
			if (quarter.substring(1,2)=='w' || quarter.substring(1,2)=='W')
			{
				quarter = quarter.substring(0,1).toUpperCase()+'E'
				changeEW=true
			}
			else
				quarter = quarter.substring(0,1).toUpperCase()+'W'
		break
	}


	if (changeNS)
	{
		switch (dir.substring(0,1)) {
			case 'n':
				if (section<=6 && section >=1)
				{
					section = (+section+36)
					township = +township + 1
				}
				section = +section - (((((+section-1) % 6))+1)*2) + 1
			break
			case 's':
				if (section<=36 && section >=31)
				{
					section = (+section-36)
					township = +township - 1
				}
				base = (Math.floor((+section-1)/6)+1)*6
				section = (base - +section) + base + 1
			break
		}
	}
	
	if (changeEW)
	{
		switch (dir.substring(0,1)) {
			case 'e':
				if (((Math.floor((+section-1)/6))%2)==0)
				{
					if ((+section - 1)%6==0)
					{
						section = +section + 5
						if (range==1 && rangedir=='W')
							rangedir='E'
						else
						{
							if (rangedir=='E')
								range = +range + 1
							else
								range = +range - 1
						}
					}
					else
						section = +section - 1
				}
				else
				{
					if ((+section)%6==0)
					{
						section = +section - 5
						if (range==1 && rangedir=='W')
							rangedir='E'
						else
						{
							if (rangedir=='E')
								range = +range + 1
							else
								range = +range - 1
						}
					}
					else
						section = +section + 1
				}
			break
			case 'w':
				if (((Math.floor((+section-1)/6))%2)==0)
				{
					if (+section%6==0)
					{
						section = +section - 5
						if (range==1 && rangedir=='E')
							rangedir='W'
						else
						{
							if (rangedir=='W')
								range = +range + 1
							else
								range = +range - 1
						}
					}
					else
						section = +section + 1
				}
				else
				{
					if ((+section-1)%6==0)
					{
						section = +section + 5
						if (range==1 && rangedir=='E')
							rangedir='W'
						else
						{
							if (rangedir=='W')
								range = +range + 1
							else
								range = +range - 1
						}
					}
					else
						section = +section - 1
				}
			break
		}
		switch (dir.substring(1,2)) {
			case 'e':
				if (((Math.floor((+section-1)/6))%2)==0)
				{
					if ((+section - 1)%6==0)
					{
						section = +section + 5
						if (range==1 && rangedir=='W')
							rangedir='E'
						else
						{
							if (rangedir=='E')
								range = +range + 1
							else
								range = +range - 1
						}
					}
					else
						section = +section - 1
				}
				else
				{
					if ((+section)%6==0)
					{
						section = +section - 5
						if (range==1 && rangedir=='W')
							rangedir='E'
						else
						{
							if (rangedir=='E')
								range = +range + 1
							else
								range = +range - 1
						}
					}
					else
						section = +section + 1
				}
			break
			case 'w':
				if (((Math.floor((+section-1)/6))%2)==0)
				{
					if (+section%6==0)
					{
						section = +section - 5
						if (range==1 && rangedir=='E')
							rangedir='W'
						else
						{
							if (rangedir=='W')
								range = +range + 1
							else
								range = +range - 1
						}
					}
					else
						section = +section + 1
				}
				else
				{
					if ((+section-1)%6==0)
					{
						section = +section + 5
						if (range==1 && rangedir=='E')
							rangedir='W'
						else
						{
							if (rangedir=='W')
								range = +range + 1
							else
								range = +range - 1
						}
					}
					else
						section = +section - 1
				}
			break
		}
	}
	if(+range<10)
		range = '0' + +range
	if(+township<10)
		township = '0' + +township
	if(+section<10)
		section = '0' + +section
	return(township+"N"+range+rangedir+section+quarter)
}

function sectionExists(strSection)
{
	for (i=0; i<arSections.length; i++)
	{
		if (strSection==(getTownshipRange(arSections[i][0])+getSectionString(arSections[i][6])+arSections[i][5]))
			return (true)
	}
	return (false)
}
