
Phone = function()
{
	/**
	 * Removes non-int characters
	 * and replaces the first encountered
	 * '+' with '00'
	 */
	function Clean( haps )
	{
		haps = haps.replace('+', '00')
		haps = haps.replace('-', '')
		haps = haps.replace('/', '')
		haps = haps.replace(/ /g,'') // remove spaces
		var out = ''
		var i
		
		for (i=0; i<haps.length; i++)
		{
			if (isFinite(haps[i]))
				out = out + haps[i]
		}

		return out
	}
	
	
	
	
	/**
	 * Try to determine to which country the
	 * the entered phonenumber belong.
	 *
	 * @param int The phonenumber to check
	 * @return object An object with.. lang=A language code of the "sensed" country (da,no,se,fi) or '?'
	 * when unable to determine for sure... set=true if the language code was explicit, false otherwise
	 */
	function GuessLang( haps )
	{
		var daLength = 8
		var noLength = 8
		var seLength = 9
		var fiLength = 10
		
		var daCode = '45'
		var noCode = '47'
		var seCode = '46'
		var fiCode = '358'
		
		// read input
		var thenum = haps
		
		// get first two pairs for language code identification
		var firstTwo = thenum.substr(0, 2)
		var nextTwo = thenum.substr(2, 2)
		
		// check for language code
		if (firstTwo == '00')
		{
			switch (nextTwo)
			{
				case daCode:
					return {lang:'da', set:true}
				case noCode:
					return {lang:'no', set:true}
				case seCode:
					return {lang:'se', set:true}
				default:
					if (thenum.substr(2, 3) == fiCode)
						return {lang:'fi', set:true}
			}
		}
		
		// check length
		switch (thenum.length)
		{
			case daLength:
			case noLength:
				return {lang:'?', set:false}
			case seLength:
				return {lang:'se', set:false}
			case fiLength:
				return {lang:'fi', set:false}
			default:
				1+1 // do nothing
		}
		
		return {lang:'?', set:false}
	}
	
	return {
		Clean:Clean,
		GuessLang:GuessLang
	}
}();
