g_sms_wrong = "Du er jo poppet helt af!!"
g_sms_unknowncountry = "Unknown country code"

SmsValidation = function()
{
	var methods = {
		"dk" : denmark,
		"no" : norway,
		"se" : sweden,
		"fi" : finland
	}

	function doIt(countryCode, $elm)
	{
		Debug.trace('SmsValidation.doIt')
				
		if (methods[countryCode])
		{
			try
			{
				var clean = methods[countryCode](cleanIt($elm.val()))
				$elm.val(clean)
			}
			
			catch(err)
			{
				alert(g_sms_wrong)
			}
		}
		else
		{
			alert(g_sms_unknowncountry)
		}
	}
	
	function cleanIt(raw)
	{
		Debug.trace('SmsValidation.cleanIt')
		
		return raw.toLowerCase().replace("+", "00").replace(/[a-z\._/ -]/g, '')
	}
	
	function denmark(clean)
	{
		Debug.trace('SmsValidation.denmark')
		
		//
		// something like
		// 12 32 65 48
		//
		
		var firstFour = clean.substr(0, 4)
		
		var countryOk = (firstFour == "0045")
		
		if (countryOk && clean.length == 12)
		{
			// phonenumber is ok... make sure we input the clean number
			return clean
		}
		
		// phonenumber is ok but country is missing
		if (clean.length == 8)
		{
			return "0045" + clean
		}
		
		throw "Error"
	}
	
	function norway(clean)
	{
		Debug.trace('SmsValidation.norway')
		
		//
		// something like
		// 12 32 65 48
		//
		
		var firstFour = clean.substr(0, 4)
		
		var countryOk = (firstFour == "0047")
		
		if (countryOk && clean.length == 12)
		{
			// phonenumber is ok... make sure we input the clean number
			return clean
		}
		
		// phonenumber is ok but country is missing
		if (clean.length == 8)
		{
			return "0047" + clean
		}
		
		throw "Error"
	}
	
	function sweden(clean)
	{
		Debug.trace('SmsValidation.sweden')
		
		//
		// something like
		// 073-510 15 32  =>  0046 735 101 532
		//
		
		var firstFour = clean.substr(0, 4)
		
		var countryOk = (firstFour == "0046")
		
		if (countryOk && clean.length == 13)
		{
			// phonenumber is ok... make sure we input the clean number
			return clean
		}
		
		if (countryOk && clean.length == 14)
		{
			// remove leading zero (after the country code)
			// the leading zero should be the 5th character
			if (clean[5] == 0)
			{
				clean = clean.slice(0,4) + clean.slice(5)
				return clean
			}
			
			// we do not know why the length is wrong
			throw "Error"
		}
		
		if (!countryOk && clean.length == 10)
		{
			// country has not been entered
			
			// remove leading zero
			if (clean[0] == 0)
			{
				clean = clean.slice(1)
				return "0046" + clean
			}
			
			// we do not know why the length is wrong
			throw "Error"
		}
		
		if (!countryOk && clean.length == 9)
		{
			return "0046" + clean
		}
		
		throw "Error"
	}
	
	function finland(clean)
	{
		Debug.trace('SmsValidation.finland')
		
		//
		// something like
		// 014 348 9200  =>  00358 143489200
		//
		
		var firstFive = clean.substr(0, 5)
		
		var countryOk = (firstFive == "00358")
		
		if (countryOk && clean.length == 14)
		{
			// phonenumber is ok... make sure we input the clean number
			return clean
		}
		
		if (countryOk && clean.length == 15)
		{
			// remove leading zero (after the country code)
			// the leading zero should be the 5th character
			if (clean[5] == 0)
			{
				clean = clean.slice(0,5) + clean.slice(6)
				return clean
			}
			
			// we do not know why the length is wrong
			throw "Error"
		}
		
		if (!countryOk && clean.length == 10)
		{
			// country has not been entered
			
			// remove leading zero
			if (clean[0] == 0)
			{
				clean = clean.slice(1)
				return "00358" + clean
			}
			
			// we do not know why the length is wrong
			throw "Error"
		}
		
		if (!countryOk && clean.length == 9)
		{
			return "00358" + clean
		}
		
		throw "Error"
	}

	return {
		"doIt" : doIt
	}
}()
