/* Phone Input Mask*/
var phonePrefix = [];
	// NSW, ACT
	phonePrefix[2] = [];
	phonePrefix[2][3] = [3, 8];
	phonePrefix[2][4] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
	phonePrefix[2][5] = [0, 1, 2, 3, 5, 6, 7, 8, 9];
	phonePrefix[2][6] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
	phonePrefix[2][7] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // Proposed
	phonePrefix[2][8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // New
	phonePrefix[2][9] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
	// VIC, TAS
	phonePrefix[3] = [];
	phonePrefix[3][4] = [0, 1, 2, 3, 4, 5, 7, 8, 9];
	phonePrefix[3][5] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
	phonePrefix[3][6] = [1, 2, 3, 4, 5, 7];
	phonePrefix[3][7] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // Proposed
	phonePrefix[3][8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // New
	phonePrefix[3][9] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
	// Mobile
	phonePrefix[4] = [];
	phonePrefix[4][0] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
	phonePrefix[4][1] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
	phonePrefix[4][2] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
	phonePrefix[4][3] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
	phonePrefix[4][4] = [7, 8, 9];
	phonePrefix[4][5] = [0, 8];
	phonePrefix[4][6] = [6];
	phonePrefix[4][8] = [8];
	// QLD
	phonePrefix[7] = [];
	phonePrefix[7][2] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // Proposed
	phonePrefix[7][3] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
	phonePrefix[7][4] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
	phonePrefix[7][5] = [2, 3, 4, 5, 6, 7];
	phonePrefix[7][7] = [6];
	// SA, NT, WA
	phonePrefix[8] = [];
	phonePrefix[8][5] = [1, 2, 3, 4];
	phonePrefix[8][6] = [0, 1, 2, 3, 4, 5, 7, 8];
	phonePrefix[8][7] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
	phonePrefix[8][8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
	phonePrefix[8][9] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var phoneInput;
var phoneHiddenArea;
var phoneHiddenNumber;
var phoneMessageTimeout;
var maskMobile = "____ ___ ___";
var maskFixed = "(__) ____ ____";
var maskCurrent = maskFixed;

$(document).ready(function() {
	phoneInput = document.getElementById("phone_number");
	phoneHiddenArea = document.getElementById("phone_number_2");
	phoneHiddenNumber = document.getElementById("phone_number_3");
	if (phoneInput.value.length == 0) {
		phoneInput.value = maskCurrent;
	}
	phoneHiddenArea.value = "";
	phoneHiddenNumber.value = "";
});

var keyCodeToDigit = function (code) {
	if (code >= 48 && code <= 57) {
		return (code - 48);
	}
	if (code >= 96 && code <= 105) {
		return (code - 96);
	}
	return "";
}

var caretMoveLeft = function () {
	return caretMoveTo(getCaretPosition() - 1);
}

var caretMoveRight = function () {
	return caretMoveTo(getCaretPosition() + 1);
}

var caretMoveHome = function () {
	return caretMoveTo(0);
}

var caretMoveEnd = function () {
	return caretMoveTo(phoneInput.value.length);
}

var caretMove = function (delta) {
	return caretMoveTo(getCaretPosition() + delta);
}

var caretMoveTo = function (pos) {
	if (pos < 0) pos = 0;
	if (pos >= phoneInput.value.length) pos = phoneInput.value.length - 1;

	var cur = getCaretPosition();
	
	if (maskCurrent == maskFixed) {
		switch (pos) {
			case 0: pos = 1; break;
			case 3:
			case 4: pos = cur == 5 ? 2 : 5; break;
			case 9: pos = cur == 10 ? 8 : 10; break;
		}
	} else  {
		switch (pos) {
			case 4: pos = cur == 5 ? 3 : 5; break;
			case 8: pos = cur == 9 ? 7 : 9; break;
		}
	}

	if(phoneInput.setSelectionRange) {
		phoneInput.setSelectionRange(pos, pos + 1);
	} else if (phoneInput.createTextRange) {
		var range = phoneInput.createTextRange();
		range.collapse(true);
		range.moveEnd("character", pos + 1);
		range.moveStart("character", pos);
		range.select();
	}
	
	return false;
}

var getCaretPosition = function () {
	var pos = 0;
	if (document.selection) {
		var sel = document.selection.createRange();
		sel.moveStart("character", - phoneInput.value.length);
		pos = sel.text.length - 1;
	} else if (phoneInput.selectionStart || phoneInput.selectionStart == '0') {
		pos = phoneInput.selectionStart;
	}
	return (pos);
}

var phoneMessage = function (msg) {
	document.getElementById("phoneMessageBox").innerHTML = msg;
	clearTimeout(phoneMessageTimeout);
	phoneMessageTimeout = setTimeout(function () {
		document.getElementById("phoneMessageBox").innerHTML = "";
	}, 2500);
}

var maskChange = function (pos) {
	maskCurrent = maskCurrent == maskFixed ? maskMobile : maskFixed;

	var unmasked = phoneInput.value.replace(/[^0-9_]/g, "").replace(/[_]/g, "+");
	
	var val = maskCurrent;
	var n = 0;
	while (val.indexOf("_") >= 0) {
		val = stringChar(val, val.indexOf("_"), unmasked.charAt(n));
		n++;
	}
	phoneInput.value = val.replace(/[+]/g, "_");
	
	if (maskCurrent == maskFixed) {
		//phoneMessage("Land line number detected");
		caretMoveTo(pos + 1);
	} else {
		//phoneMessage("Mobile number detected");
		caretMoveTo(pos - 1);
	}
}

var checkArea = function (area, next1, next2) {
	if (area == null || area == "_") {
		return true;
	} else {
		if (!phonePrefix[area]) {
			return false;
		} else {
			if (next1 == null || next1 == "_") {
				return true;
			} else {
				if (!phonePrefix[area][next1]) {
					return false;
				} else {
					if (next2 == null || next2 == "_") {
						return true;
					} else {
						var flag = false;
						for (var i = 0; i < phonePrefix[area][next1].length; i++) {
							if (phonePrefix[area][next1][i] == next2) flag = true;
						}
						return flag;
					}
				}
			}
		}
	}
}

var filterDigit = function (digit, pos) {
	var num;
	var area;
	switch (maskCurrent) {
		case maskFixed:
			switch (pos) {
				case 1: num = 0; break;
				case 2: num = 1; break;
				case 5: num = 2; break;
				case 6: num = 3; break;
				case 7: num = 4; break;
				case 8: num = 5; break;
				case 10: num = 6; break;
				case 11: num = 7; break;
				case 12: num = 8; break;
				case 13: num = 9; break;
				default: num = null; break;
			}
			area = phoneInput.value.charAt(2);
			next1 = phoneInput.value.charAt(5);
			next2 = phoneInput.value.charAt(6);
			break;
		case maskMobile:
			switch (pos) {
				case 0: num = 0; break;
				case 1: num = 1; break;
				case 2: num = 2; break;
				case 3: num = 3; break;
				case 5: num = 4; break;
				case 6: num = 5; break;
				case 7: num = 6; break;
				case 9: num = 7; break;
				case 10: num = 8; break;
				case 11: num = 9; break;
				default: num = null; break;
			}
			area = phoneInput.value.charAt(1);
			next1 = phoneInput.value.charAt(2);
			next2 = phoneInput.value.charAt(3);
			break;
	}
	
	if (num == 0 && digit != 0) {
		phoneMessage("Must Start With \"0\"");
		return false;
	}
	if (num == 1) {
		if (!checkArea(digit, null, null)) {
			phoneMessage("Bad Area Code");
			return false;
		} else if ((maskCurrent == maskFixed && digit == 4) || (maskCurrent == maskMobile && digit != 4 && phonePrefix[digit])) {
			maskChange(pos);
		}
	}
	if (num == 2) {
		if (!checkArea(area, digit, null)) {
			phoneMessage("Bad Digit");
			return false;
		}
	}
	if (num == 3) {
		if (!checkArea(area, next1, digit)) {
			phoneMessage("Bad Digit");
			return false;
		}
	}
	return true;
}

var stringChar = function (string, pos, character) {
	return string.substring(0, pos) + character + string.substring(pos + 1, string.length);
}

var insertDigit = function (digit) {
	var pos = getCaretPosition();
	caretMoveTo(pos); // Make sure the caret is in a position where a digit can be inserted.
	pos = getCaretPosition(); // And get it again.
	if (filterDigit(digit, pos)) {
		pos = getCaretPosition(); // If mask changes, the caret position will change, so get it again.
		phoneInput.value = stringChar(phoneInput.value, pos, digit);
		caretMoveTo(pos + 1);
	}
	
	if (maskCurrent == maskFixed) {
		if (!checkArea(phoneInput.value.charAt(2), phoneInput.value.charAt(5), phoneInput.value.charAt(6))) {
			phoneInput.value = stringChar(phoneInput.value, 5, "_");
			phoneInput.value = stringChar(phoneInput.value, 6, "_");
			caretMoveTo(5);
		}
	} else {
		if (!checkArea(phoneInput.value.charAt(1), phoneInput.value.charAt(2), phoneInput.value.charAt(3))) {
			phoneInput.value = stringChar(phoneInput.value, 2, "_");
			phoneInput.value = stringChar(phoneInput.value, 3, "_");
			caretMoveTo(2);
		}
	}
	var unmasked = phoneInput.value.replace(/[^0-9]/g, "");
	if (unmasked.length == 10) {
		phoneHiddenArea.value = unmasked.substring(0, 2);
		phoneHiddenNumber.value = unmasked.substring(2, 6) + " " + unmasked.substring(6, 10);
	} else {
		phoneHiddenArea.value = "";
		phoneHiddenNumber.value = "";
	}

	return false;
};

var phoneInputUpdate = function (e) {
	var evt;
	if (window.event) evt = window.event;
	else if (e) evt = e;
	var code;
	if (window.event) code = window.event.keyCode;
	else if (e) code = e.which;

	// For debug purposes:
	// phoneMessage("* " + code + " > " + keyCodeToDigit(code) + " @ " + getCaretPosition());
	
	if (code == 9) return true;
	else if (code == 8) return caretMoveLeft();
	else if (code == 16) return false;
	else if (code == 17) return false;
	else if (code == 18) return false;
	else if (code == 46) return false;
	else if (code == 35) return caretMoveEnd();
	else if (code == 36) return caretMoveHome();
	else if (code == 37) return caretMoveLeft();
	else if (code == 39) return caretMoveRight();
	else if (code >= 48 && code <= 57) return insertDigit(keyCodeToDigit(code));
	else if (code >= 96 && code <= 105) return insertDigit(keyCodeToDigit(code));
	else phoneMessage("Digit Expected");
	
	return false;
};

var phoneInputFocused = function (e) {
	if (!phoneInput.value.match(/[0-9]/)) {
		caretMoveTo(0);
	} else {
		caretMoveTo(getCaretPosition());
	}

	return false;
};

var phoneInputBlurred = function (e) {
	if (phoneHiddenArea.value.length < 1 || phoneHiddenNumber.value.length < 1) {
		phoneMessage("Number not complete");
	}
};
