$(function(){
	
	// You'll need to fix individual PNGs as they are shown
	fixUsernamePNGs();
	
	// Fix var issues before registering event handlers
	if (!confirmInitializedVariables()) {
		
	}
	
	// Set up dialogs
	configureDialogs();
	
	// Validate form before submit
	registerFormValidation();

	// Give focus to input box
	$("input[name='username']").focus();
	
});

function configureDialogs() {
	
	var centerBase = $("div#Container");
	
	if ($("div#UsernameBoxPosition").exists()) {
		$("div#UsernameBoxPosition").centerTo({
			horizontal: centerBase,
			left: "0px",
			right: "0px"
		});
		$("div#UsernameBoxPosition").show();
	}
	if ($("div#Overlay").exists()) {
		configureOverlay({
			opacity: 0.5
		});
	}
	if ($("div#ErrorBox").exists()) {
		$("div#ErrorBox").centerTo({
			horizontal: centerBase,
			left: "0px",
			right: "0px"
		});
		configureErrorBox({
			style: "old",
			bgColor1: "#c40000",
			fontColor1: "#f5fe80",
			timeout1: 1.4,
			bgColor2: "#000",
			fontColor2: "#f5fe80",
			timeout2: 1
		});
	}
	
}

function fixUsernamePNGs() {
	
	if (isIE6()) {
		$("div#UsernameBox").fixPNGs();
	}
	
}

function registerFormValidation() {
	
	// Apply modified jquery.numeric plugin (found at the bottom of this page.
	// Numeric has been modified to allow special phone number characters.
	$("form input[name='username']").numeric();
	
	// 'Click' SubmitBtn if the enter key is pressed from username input field
	$("form#form input[name='username']").keypress(function(event) {
		var code = getKeyCodeFromEvent(event);
		if (code == 13) {
			event.preventDefault();
			$("form#form input#SubmitBtn").click();
		}
	}) 
	
	$("form#form input#SubmitBtn").click(function(event) {
		
		event.preventDefault();
		$("form#form input[name='username']").blur();
		
		var submitForm = true;
		var username = $("form#form input[name='username']").val();
		
		if ($("form#form input#Agree").exists()) {
			if (inputIsValidPhoneNumber(username)) {
				if (!$("form#form input#Agree").is(":checked")) {
					if (window.confirm("Click OK to Agree to the Terms & Conditions")) {
						$("form#form input#Agree").attr("checked", true);
					}
					else {
						submitForm = false;
					}
				}
			}
			else {
				submitForm = false;
			}
		}
		
		if (submitForm) {
			// $("form#form input[name='username']").val(formatPhoneNumber(username));
			$("form#form").submit();
		}
		else {
			$("form#form input[name='username']").focus();
		}
		
    });
	
	$("form#form").submit(function(event) {
		
		// Prevent submission unless phone number is valid
		var username = $("form#form input[name='username']").val();
		if (!inputIsValidPhoneNumber(username)) {
			event.preventDefault();
			$("form#form input[name='username']").focus();
		}
		
	});
	
}

/* 
 * jquery.numeric.js
 */
/*
 *
 * Copyright (c) 2006/2007 Sam Collett (http://www.texotela.co.uk)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * Version 1.0
 * Demo: http://www.texotela.co.uk/code/jquery/numeric/
 *
 * $LastChangedDate$
 * $Rev$
 */
 
/*
 * Allows only valid characters to be entered into input boxes.
 * Note: does not validate that the final text is a valid number
 * (that could be done by another script, or server-side)
 *
 * @name     numeric
 * @param    decimal      Decimal separator (e.g. '.' or ',' - default is '.')
 * @param    callback     A function that runs if the number is not valid (fires onblur)
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @example  $(".numeric").numeric();
 * @example  $(".numeric").numeric(",");
 * @example  $(".numeric").numeric(null, callback);
 *
 */
jQuery.fn.numeric = function(decimal, callback)
{
	decimal = decimal || ".";
	callback = typeof callback == "function" ? callback : function(){};
	this.keypress(
		function(e)
		{
			var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
			// allow enter/return key (only when in an input box)
			if(key == 13 && this.nodeName.toLowerCase() == "input")
			{
				return true;
			}
			else if(key == 13)
			{
				return false;
			}
			var allow = false;
			// allow Ctrl+A
			if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) return true;
			// allow Ctrl+X (cut)
			if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) return true;
			// allow Ctrl+C (copy)
			if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) return true;
			// allow Ctrl+Z (undo)
			if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) return true;
			// allow or deny Ctrl+V (paste), Shift+Ins
			if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */
			|| (e.shiftKey && key == 45)) return true;
			// if a number was not pressed
			if(key < 48 || key > 57)
			{	
				specialPhoneCharacters = /[\(\)+-\. ]/g;
				var character = String.fromCharCode(key);
				if (character.match(specialPhoneCharacters)) {
					return true;
				}
				// check for other keys that have special purposes
				if(
					key != 8 /* backspace */ &&
					key != 9 /* tab */ &&
					key != 13 /* enter */ &&
					key != 35 /* end */ &&
					key != 36 /* home */ &&
					key != 37 /* left */ &&
					key != 39 /* right */ &&
					key != 46 /* del */
				)
				{
					allow = false;
				}
				else
				{
					// for detecting special keys (listed above)
					// IE does not support 'charCode' and ignores them in keypress anyway
					if(typeof e.charCode != "undefined")
					{
						// special keys have 'keyCode' and 'which' the same (e.g. backspace)
						if(e.keyCode == e.which && e.which != 0)
						{
							allow = true;
						}
						// or keyCode != 0 and 'charCode'/'which' = 0
						else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
						{
							allow = true;
						}
					}
				}
			}
			else
			{
				allow = true;
			}
			return allow;
		}
	)
	.blur(
		function()
		{
			var val = jQuery(this).val();
			if(val != "")
			{
				var re = new RegExp("^\\d+$|\\d*" + decimal + "\\d+");
				if(!re.exec(val))
				{
					callback.apply(this);
				}
			}
		}
	);
	return this;
}
