
/**
 * Error Handling File, Fontshop.com
 *
 * This file handles all of the newer in-line error messages by validating
 * them and displaying the appropriate confirmation or error messages inline.
 *
 * Author: Nick Fehr <nick@fontshop.com>
 */


var LOGIN_FORM;

/**
* This function simply gets an element by ID. I used it to consolidate my code.
* Return: HTMLelement
*/
function getById(id) {
	return document.getElementById(id);
}	

// Instantiates Dataset. Redundant, since each field is reset in each function
// but I'm trying to work within the class created in addresses.js. Fehr 6/23/08
if (getById("create_account")) {
	LOGIN_FORM = new Dataset(getById("create_account"));
} 

/*
	This function will make a red background flash and then fade
	It's used to hightlight information in the form that hasn'y been filled out yet. 
*/		
function redFade(el) {
	var b = 100;
	
	function f() {
		el.style.background = 'rgb(255,' + (b+=4) + ',' + (b+=4) + ')';
		if (b < 255) {
			setTimeout(f, 40);
		} else {
			el.style.background = ''; 
		}	
	};
	
	f();
}

/**
* This function validates email using validateEmailAddress in addresses.js 
* and displays appropriate messages
* Parameters: input element
* Return: boolean
*/
function checkEmail(input, markHolder) {
	LOGIN_FORM.email_address = input.value; //necessary in case value is changed
	markHolder = getById(markHolder);
	
	if (LOGIN_FORM.validateEmailAddress()) {
		markHolder.innerHTML = '<img src="images/errors/bg-fieldset-welldone.gif" />';
		return true;
	} else {
		markHolder.innerHTML = '&#9664; must be valid';
		return false;
	}
}

/**
* This function checks to see if the password has enough characters with validateLength() from addresses.js
* and displays appropriate messages
* Parameters: input element
* Return: boolean
*/
function passwordValid(input) {
	LOGIN_FORM.password = input.value; //necessary in case value is changed
	var markHolder = getById("passVSpan");
	if (!LOGIN_FORM.validateLength()) {
		markHolder.innerHTML = '&#9664; min 5 characters';	
		return false;
	} else {
		markHolder.innerHTML = '<img src="images/errors/bg-fieldset-welldone.gif" />';
		return true;
	}
}

/**
* This function validates the password using validatePassword() in addresses.js and displays appropriate 
* messages. It checks to see if the two passwords match.
* Parameters: input element
* Return: boolean
*/
function checkPassword(input) {
	LOGIN_FORM.confirmation = input.value; //necessary in case value is changed
	var markHolder = getById("passSpan");
	if (LOGIN_FORM.validatePassword()) {
		markHolder.innerHTML = '<img src="images/errors/bg-fieldset-welldone.gif" />';
		return true;
	} else {
		markHolder.innerHTML = '&#9664; must match';
		return false;
	}
}


/**
* Error message function. Displays red error message with unicode arrow pointing left.
* Takes the ID of the empty element to place the message in as a parameter.
* Parameters: elementId, messsage
*/
function redErrorMessage(elementId, message){
	var markHolder = getById(elementId);
	markHolder.style.color="red";
	markHolder.innerHTML = '&#9664; ' + message;
}

//Validates the entire registration form. Returns true if valid. 
if (document.getElementById("submitRegister")) {
	IS_VALID_REG = false;
}

/**
* This function validates the entire registration form and displays messages.
* Return: boolean
*/
function checkRegistrationForm() {
	IS_VALID_REG = true;

	var submitButton = getById("submitRegister");

	if (!checkEmail(getById("email_address_registration"), "emailRegistrationSpan")){
			redErrorMessage("emailRegistrationSpan", "must be valid");
			IS_VALID_REG = false;
	}
	
	LOGIN_FORM.password = getById("password_registration"); //necessary in case value is changed
	
	if (!passwordValid(getById("password_registration"))) {
		redErrorMessage("passVSpan", "min 5 characters");
		IS_VALID_REG = false;
	}
	
	if (!checkPassword(getById("confirmation"))) {
		redErrorMessage("passSpan", "must match");
		IS_VALID_REG = false;
	}	
	
	return IS_VALID_REG;
}

/** 
* This function validates the Login process. There are multiple 
* <span>s that are used to hold error messages and unicode arrows
* which get set depending on the field's validity.
* Return: boolean
*/
function checkLogin() {
	IS_VALID_LOGIN = true; //global
	
	var loginButton = getById("loginSubmitButton");

	if (!checkEmail(getById("email_address_login"), "emailLoginSpan")){
		getById("incorrectPasswordSpan").innerHTML="";
		
		redErrorMessage("emailLoginSpan", "must be valid");
		
		IS_VALID_LOGIN = false;
	} else {
		getById("incorrectPasswordSpan").innerHTML="";
		getById("emailLoginSpanMessage").innerHTML="";
		getById("emailLoginSpan").innerHTML="";
	}
	
	var ie_version = 0;
	if (/msie (\d+\.\d+);/i.test(navigator.userAgent)){ //test for MSIE x.x;
		 var ie_version = Math.round(RegExp.$1)
		 var is_ie = true;
	}
	
	if (ie_version != 7) {
		if (!getById("password").value) {
			getById("passwordLoginSpan").innerHTML="";
			redErrorMessage("passwordLoginSpan", "required");

			IS_VALID_LOGIN = false;
		} else {
			getById("passwordLoginSpanMessage").innerHTML="";
			getById("passwordLoginSpan").innerHTML="";
		}
	}
	
	return IS_VALID_LOGIN;
}

/**
* Validates the EULA agreement page. Returns true if all EULA boxes are checked	
* Return: boolean
*/
function checkEULA() {
	IS_VALID_EULA = true;	
	
	var errorButton = getById("eulaSubmitButton");
		errorButton.src=  "includes/languages/english/images/buttons/button_submit_off.gif";
		
	var eulaArray = new Array(); //array for checkbox objects
	var eulaCounter = 0;
	
	while(getById("eula_agreed" + eulaCounter)) {
		eulaArray[eulaCounter] = getById("eula_agreed" + eulaCounter);
		eulaCounter++;
	}
	var numChecked = 0;	
	
	for (var i=0; i<eulaArray.length; i++) {
		if (eulaArray[i].checked == false) {
			redErrorMessage("eulaErrorSpan" + i, "This item must be checked to continue");
			getById("eulaErrorBottom").innerHTML = "You must agree to each foundry's user license to continue";
			errorButton.src= "includes/languages/english/images/buttons/button_submit_grey.gif";
			errorButton.style.cursor="default";	
		} else {
			getById("eulaErrorSpan" + i).innerHTML = "";
			numChecked++;
		}
	}
	
	if (numChecked == eulaArray.length && numChecked > 0) {
		getById("eulaErrorBottom").innerHTML = "";
		errorButton.src= "includes/languages/english/images/buttons/button_submit_off.gif";
		errorButton.style.cursor="pointer";
		IS_VALID_EULA = true;
	} else {
		IS_VALID_EULA = false;
	}
	return IS_VALID_EULA;
}

/*
Research page
*/

function checkResearch(){
	IS_VALID_RESEARCH = false;
	
	if (getById("appearance").value != ""){
		IS_VALID_RESEARCH = true;
	}
	
	if (!IS_VALID_RESEARCH){
		getById("appearance_span_error").innerHTML = "must enter description &#9650;";
	} else {
		getById("appearance_span_error").innerHTML = "";
	}
	
	return IS_VALID_RESEARCH;
}

