//##########################################################################################

//place our event hook
window.onload = function() {
	//find the username field and restrict input
	var Username = PageTemplate.GetNodesByAttribute("data-field","username").GetNode();
	if(Username) {
		Username.onkeypress = function(Event) {
			Event = BrowserWindow.GetEvent(Event);
			return Page.RestrictInput(Event);
		};
	}
	
	//find the country drop down
	var CountryDD = PageTemplate.GetNodesByAttribute("data-field","country").GetNode();
	if(CountryDD) {
		CountryDD.onchange = function(Event) {
			Event = BrowserWindow.GetEvent(Event);
			Page.ShowHidePostalCode(CountryDD);
		};
	}
	
	//call on first load
	Page.ShowHidePostalCode(CountryDD);

	//find the edit buttons
	var EmailField = PageTemplate.GetNodesByAttribute("data-field","email").GetNode();
	if(EmailField) {
		EmailField.focus();
	}
};

//##########################################################################################

//--> Setup Object :: Page
	Page = {};
//<-- End Setup Object :: Page

//##########################################################################################

//--> Begin Function :: ShowHidePostalCode
	Page.ShowHidePostalCode = function(CountryDD) {
		var PostalCode = PageTemplate.GetNodesByAttribute("data-label","postal_code").GetNode();
		if(CountryDD[CountryDD.selectedIndex].getAttribute("data-field") == "has_postal_codes") {
			PostalCode.className = "postal_code";
		}
		else {
			if(CountryDD[CountryDD.selectedIndex].getAttribute("data-field") == "divider") {
				for(var i = 0 ; i < CountryDD.options.length ; i++) {
					if(CountryDD.options[i].value == "us") {
						CountryDD.selectedIndex = i;
						PostalCode.className = "postal_code";
						break;
					}
				}
			}
			else {
				PostalCode.className = "postal_code_hidden";
			}
		}
	}
//<-- End Function :: ShowHidePostalCode

//##########################################################################################

//--> Begin Function :: RestrictInput
	Page.RestrictInput = function(Event) {
		//if they pressed esc, remove focus from field
		if(Event.Keyboard.CharacterCode == 27) {
			Event.Target.blur();
			return false;
		}
		
		//ignore if they are press other keys
		//remember that code: 39 is the down key AND ‘ key and DEL also equals .
		if(!Event.Keyboard.CtrlKey && (Event.Keyboard.CharacterCode != 9 && Event.Keyboard.CharacterCode != 8 && Event.Keyboard.CharacterCode != 36 && Event.Keyboard.CharacterCode != 37 && Event.Keyboard.CharacterCode != 38 && (Event.Keyboard.CharacterCode != 39 || (Event.Keyboard.CharacterCode == 39 && Event.Keyboard.Character == "`")) && Event.Keyboard.CharacterCode != 40)) {
			if(Event.Keyboard.Character.match(/[A-Za-z0-9]/g)) {
				return true;
			}
			else {
				return false;
			}
		}
	}
//<-- End Function :: RestrictInput

//##########################################################################################