
var isOldStyleErrorBox = false;

function configureErrorBox(arguments) {
	arguments = $.extend({
		style: "old",
		bgColor1: "#000",
		fontColor1: "#fff",
		timeout1: 0,
		bgColor2: "#000",
		fontColor2: "#fff",
		timeout2: 0
	}, arguments);
	
	if (!$("div#ErrorBox").exists()) {
		return false;
	}
	
	isOldStyleErrorBox = (arguments.style == "old")?true:false;
	
	// Preload background images
	if (!isOldStyleErrorBox) {
		$.preloadImages(
			template_path + "/images/progress/error_bottom.png", 
			template_path + "/images/progress/error_top.png", 
			template_path + "/images/progress/circle_exclamation.png"
		);
	}
	
	// Display ErrorBox immediately if errors exist
	displayErrorFromAPI();
	
	// Configure error handlers
	if ($("input[name='pin']").exists()) {
		registerAlertPasswordErrors();
	}
	else {
		registerAlertUsernameErrors();
	}
	
	if (isOldStyleErrorBox) {
		$("div#ErrorBox").startBackgroundColorAnimation(arguments);
	}
	
}

function displayErrorFromAPI() {
	
	var error = $("div#ErrorBoxText").html();
	if (error != null && error.length > 0) {
		showErrorBox();
	}
	
}

function fixErrorBoxPNGs() {
	
	if (isIE6()) {
		$("#ErrorBox").fixPNGs();
	}
	
}

function formatPhoneNumber(phoneNumber) {
	
	return phoneNumberStripCountryCode(phoneNumberStripNonNumericals(phoneNumber));
	
}

function inputIsShortCode(password) {
	
	var shortCode = getShortCode();
	return (shortCode.length > 0 && password == shortCode)?true:false;
	
}

function inputIsValidPassword(password) {
	
	var isValid = false;
	
	if (getPassword().length > 0) {
		if (password == getPassword()) {
			isValid = true;
		}
	}
	else if (!inputIsShortCode(password) && !inputIsValidPhoneNumber(password)) {
		isValid = true;
	}
	
	return isValid;
	
}

function inputIsValidPhoneNumber(username) {
	
	var isValid = false;
	var phoneMin = 2011000000;
	var phoneMax = 9899999999;
	
	phoneNumber = formatPhoneNumber(username);
	
	// Check that phone number is within valid range
	if (phoneNumber.length == 10 && phoneNumber >= phoneMin && phoneNumber <= phoneMax && phoneNumber.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/) ) {
		isValid = true;
	}
	
	return isValid;
}

function phoneNumberStripCountryCode(phoneNumber){
	
	if (typeof country_code == "undefined" || country_code == null || country_code.length == 0) {
		// var country_code = "1";
	}
	
	if (phoneNumber.substring(0, country_code.length) == country_code) {
		phoneNumber = phoneNumber.substring(country_code.length);
	}
	
	return phoneNumber;
	
}

function phoneNumberStripNonNumericals(phoneNumber) {
	 
	return phoneNumber.replace(/[^0-9]/g, ""); 
	
}

function phoneNumberValidDigits(phoneNumber) {
	
	return formatPhoneNumber(phoneNumber).length;
	
}

function registerAlertPasswordErrors(){
	
	var shortCode = getShortCode();
	
	var ErrorEmptyInput = 	"Oops! It looks like you forgot to enter your Password before submitting the form. " +
							"Please check your Cell Phone for your Password and try again.";
	var ErrorIncorrectPassword = 	"You have entered an incorrect password. Please check your text messages on " + 
									"cell phone number " + formated_username + " for the secret password!";
	
	var ErrorPhoneNumber = 	"It looks like you entered a Cell Phone Number into the Password box. " +
							"Please check your Cell Phone for a text message with your Password. If";
	if (isOldStyleErrorBox) {
		ErrorPhoneNumber += " ";
	}
	else {
		ErrorPhoneNumber += "<br/>";
	}
	ErrorPhoneNumber += "you submit the form a password will be sent to that Cell Phone Number.<br/>";
	
	var ErrorShortCode = "";
	
	if (shortCode.length > 0) {
		ErrorShortCode =	shortCode + " is our contact number.<br/>" +
							"Please open the text from " + shortCode + " to get your password.<br/>";
	}
	else {
		ErrorShortCode =	"Oops! You have entered our contact number as your Password. Please check your " +
							"Cell Phone for a text message containing your password.<br/>";
	}						
	
	$("form#form input[name='pin']").blur(function() {
		
		var error = "";
		var password = $("form#form input[name='pin']").val();
		
		if (password.length > 0) {
			if (inputIsShortCode(password)) {
				error = ErrorShortCode;
			}
			else if (inputIsValidPhoneNumber(password)) {
				error = ErrorPhoneNumber;
			}
		}
		else {
			// error = ErrorEmptyInput;
		}
		
		if (error.length > 0) {
			$("div#ErrorBoxText").html(error);
			showErrorBox();
		}
		/*
		else if (inputIsValidPassword(password)) {
			$("div#ErrorBox").hide();
		}
		//*/
		
	});
	
	$("form#form input[name='pin']").keyup(function(event) {
		
		var code = getKeyCodeFromEvent(event);
		var error = "";
		var password = $("form#form input[name='pin']").val();
		
		if (password.length > 0) {
			if (inputIsShortCode(password)) {
				error = ErrorShortCode;
			}
			else if (inputIsValidPhoneNumber(password)) {
				error = ErrorPhoneNumber;
			}
			else if (code == 9 || code == 13) {
				if (!inputIsValidPassword(password)) {
					error = ErrorIncorrectPassword;
				}
			}
		}
		else {
			if (code == 9 || code == 13) {
				error = ErrorEmptyInput;
			}
		}
		
		if (error.length > 0) {
			$("div#ErrorBoxText").html(error);
			showErrorBox();
		}
		else if (inputIsValidPassword(password)) {
			$("div#ErrorBox").hide();
		}
		
	});
	
	$("form#form input#SubmitBtn").click(function() {
		
		var error = "";
		var password = $("form#form input[name='pin']").val();
		
		if (password.length > 0) {
			if (inputIsShortCode(password)) {
				error = ErrorShortCode;
			}
			else if (inputIsValidPhoneNumber(password)) {
				error = ErrorPhoneNumber;
			}
			else if (!inputIsValidPassword(password)) {
				error = ErrorIncorrectPassword;
			}
		}
		else {
			error = ErrorEmptyInput;
		}
		
		if (error.length > 0) {
			$("div#ErrorBoxText").html(error);
			showErrorBox();
		}
		else if (inputIsValidPassword(password)) {
			$("div#ErrorBox").hide();
		}
		
	});
	
}

function registerAlertUsernameErrors() {
	
	var ErrorInvalidPhoneNumber = 	"Please enter your Cell Phone Number in the following format: ###-###-####<br/>" +
									"It should be ten digits long beginning with your area code.<br/>"; 
	var ErrorEmptyInput = 			"Oops! It looks like you forgot to enter your Cell Phone Number before " +
									"submitting the form. Please enter your Cell Phone Number and try again.<br/>";
	
	var ErrorNotEnoughDigits = 		ErrorInvalidPhoneNumber;
	var ErrorTooManyDigits = 		ErrorInvalidPhoneNumber;
	var ErrorInvalidCharacters = 	ErrorInvalidPhoneNumber;
	
	$("form#form input[name='username']").blur(function() {
		
		var error = "";
		var username = $("form#form input[name='username']").val();
		
		if (username.length == 0) {
			// error = ErrorInvalidPhoneNumber;
		}
		else if (username.length < 10) {
			error = ErrorNotEnoughDigits;
		}
		else if (!inputIsValidPhoneNumber(username)) {
			error = ErrorInvalidPhoneNumber;
		}
		
		if (error.length > 0) {
			$("div#ErrorBoxText").html(error);
			showErrorBox();
		}
		else if (inputIsValidPhoneNumber(username)) {
			$("div#ErrorBox").hide();
		}
		
	});
	
	$("form#form input[name='username']").keyup(function(event) {
		
		var code = getKeyCodeFromEvent(event);
		var error = "";
		var username = $("form#form input[name='username']").val();
		
		if (!inputIsValidPhoneNumber(username)) {
			if (phoneNumberValidDigits(username) <= 10) {
				if (code == 13) {
					if (username.length == 0) {
						error = ErrorEmptyInput;
					}
					else {
						error = ErrorInvalidPhoneNumber;
					}
				}
				else if (code == 9 && username.length > 0) {
					error = ErrorInvalidPhoneNumber;
				}
			}
			else {
				error = ErrorInvalidPhoneNumber;
			}
		}
		
		if (error.length > 0) {
			$("div#ErrorBoxText").html(error);
			showErrorBox();
		}
		else if (inputIsValidPhoneNumber(username)) {
			$("div#ErrorBox").hide();
		}
		
	});
	
	$("form#form input#SubmitBtn").click(function() {
		
		var error = "";
		var username = $("form#form input[name='username']").val();
		
		if (!inputIsValidPhoneNumber(username)) {
			error = ErrorInvalidPhoneNumber;
			if (username.length == 0) {
				error = ErrorEmptyInput;
			}
		}
		
		if (error.length > 0) {
			$("div#ErrorBoxText").html(error);
			showErrorBox();
		}
		else if (inputIsValidPhoneNumber(username)) {
			$("div#ErrorBox").hide();
		}
		
	});
	
}

function showErrorBox() {
	
	if (isOldStyleErrorBox) {
		$("div#ErrorBox").show();
	}
	else {
	
		stopErrorBoxImageAnimation();
		
		$("div#ErrorBox").show();
		fixErrorBoxPNGs();
		
		startErrorBoxImageAnimation();
		
	}
	
}

var ErrorBoxImageAnimationInterval;
var ErrorBoxNotificationBackground = "";

function startErrorBoxImageAnimation() {
	
	if (ErrorBoxNotificationBackground.length == 0) {
		ErrorBoxNotificationBackground = $("div#ErrorBox div.ErrorBoxNotification").css("background-image");
	}
	
	// Toggle display on timer
	ErrorBoxImageAnimationInterval = window.setInterval(function() {
		if ($("div#ErrorBox div.ErrorBoxNotification").css("background-image") == ErrorBoxNotificationBackground) {
			$("div#ErrorBox div.ErrorBoxNotification").css("background-image","none");
		}
		else {
			$("div#ErrorBox div.ErrorBoxNotification").css("background-image", ErrorBoxNotificationBackground);
			
			if (isIE6()) {
				$("div#ErrorBox").fixPNGs();
			}
		}
	}, 0.6 * 1000);
	
}

function stopErrorBoxImageAnimation() {
	
	clearInterval(ErrorBoxImageAnimationInterval);
	
}
