var _error = '_Error';
var yes = 'yes';
var _yes = '_Yes';
var _no = '_No';

//Important field:keeps track of the current state of validation,
//Can be checked anytime durig validation
var validationSuccess = true;
var errorFieldId = null;
 
// Regular expressions used to check valid format
var expEmail = '^[A-Za-z0-9\\.\\_\\-]+@[A-Za-z0-9\\-\\_]+(\\.[A-Za-z0-9\\-\\_]{2,}){1,}(\\.[A-Za-z]{2,4})?$';
var expPassword = '^[\\S]{6,}$';
var expTel = '^((\\+33)|[0-9]{1})[0-9\\.\\-\\/ ]{9,13}$';
var expTextOnly = '^[a-zA-Z]{2,}$';
var expTextFree = "^[0-9a-zA-Z\\-\\,\\'\\. ]{2,}$";
var expAddress = '^[0-9a-zA-Z ]{1,}';
var expInteger = '^[0-9]{1,}$';
var expAddress = '^[0-9a-zA-Z ]{1,}';
var expZipcode = '^[0-9a-zA-Z ]{5,}';
var expUsername = '^[\\S]{6,}$'
var expNoregexp = '';
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };

//reset the validationSuccess and errorFieldId
//when CreateAccount button is selected
function resetValidationSuccess() {
	validationSuccess = true;
	errorFieldId = null;
}

//returns errorFieldId toOrganizationRegistrationAddForm.jsp
function getErrorFieldId() {
	return errorFieldId;
}

//sets the errorFieldId
function setErrorFieldId(id) {
	if(errorFieldId == null) {
		errorFieldId = id;
	}
}

//sets error message and makes textfield class 'text error'
function alertError(id, message) {
	if (dojo.byId(id + _error)) {
		if(dojo.byId(id + _error).innerHTML == '') {
			dojo.byId(id + _error).innerHTML = message;
		}
	} else {
		var errorBox = document.createElement('div');
		errorBox.id = id + _error;
		errorBox.style.fontWeight='bold';
		errorBox.style.cssFloat='left';
		errorBox.style.height='auto';
		errorBox.style.color = '#FF0000';
		errorBox.innerHTML = message;
		var container = dojo.byId(id);
		container.parentNode.appendChild(errorBox);
		if (dojo.byId(id).type == 'radio') {
			var form = dojo.byId(id).form;
			var nb_rad = form.elements[id].length;
			for (var i=0; i<nb_rad; i++) {
				form.elements[id][i].style.marginBottom = '5px';
			}
		} else {
			container.style.marginBottom = '5px';
		}
	}
	if ((dojo.byId(id).type == 'text') || (dojo.byId(id).type == 'password')) {
		dojo.byId(id).className = 'text error';
	}
}

function deleteErrorMessage(id){
	if (dojo.byId(id)) {
		if (dojo.byId(id + _error)) {
			dojo.byId(id).parentNode.removeChild( dojo.byId(id + _error) );
		}
		if (dojo.byId(id).type == 'radio') {
			var form = dojo.byId(id).form;
			var nb_rad = form.elements[id].length;
			for (var i=0; i<nb_rad; i++) {
				form.elements[id][i].style.marginBottom = '20px';
			}
		} else if (dojo.byId(id).type != 'checkbox') {
			dojo.byId(id).style.marginBottom = '20px';
		}
		if (dojo.byId(id).className == 'text error') {
			dojo.byId(id).className = 'text';
		}
	}
}

//1.checks the format of value according to regExp using match() or
//2.for dropdown list check if any item is selected
    function hasValidValue(value,regExp) {
	//This is for listSelection. 'selected' is from properties
		if(regExp == 'selected') {
			if(value != '')
				return true;
			    return false;
		}
		 rexExpToApply = new RegExp(regExp,"i");	
		//console.debug("regExp="+regExp);
		if(value.match(rexExpToApply)){
			return true;
		}
		return false;
}					

	
//checks if any value is entered
function hasValue(value) {
	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
	return (value.trim().length != 0) 
}

function checkInputRadio(id) {
	var form = dojo.byId(id).form;
	var nb_rad = form.elements[id].length;
	for (var i=0; i<nb_rad; i++) {
		if( form.elements[id][i].checked ) {
			return true;
		}
	}
	return false;
}

function getInputRadioValue(id) {
	var form = dojo.byId(id).form;
	var nb_rad = form.elements[id].length;
	for (var i=0; i<nb_rad; i++) {
		if( form.elements[id][i].checked ) {
			return form.elements[id][i].value;
		}
	}
	return '';
}

function checkInputCheckbox(id) {
	if (dojo.byId(id).checked) {
		return true;
	} else {
		return false;
	}
}

//main function to do validation for any field with <code>id</code> 
//according to <code>validation</code> and check if <code>required</code>(mandatory)
function checkForValidFormat(id,validation,required,message) {
	var result = true;
	if(dojo.byId(id) != null) {
		switch (dojo.byId(id).type) {
			case 'radio':
				if (required == yes) {
					result = checkInputRadio(id);
				}
				break;
			case 'checkbox':
				if (required == yes) {
					result = checkInputCheckbox(id);
				}
				break;
			case 'select-one':
				if (required == yes) {
					if (!hasValue(dojo.byId(id).value)) {
						result = false;
					}
				}
				break;
			default:
				// Text, Password
				var bHasValue = hasValue(dojo.byId(id).value);
				var bHasValidValue;
				if (typeof validation == "string" && validation != 'checkUKPostCode') {
					bHasValidValue = hasValidValue(dojo.byId(id).value, validation);
				} else {
					if(validation == 'checkUKPostCode') {
						validation = checkUKPostCode;
					}
					//bHasValidValue = eval(validation.name+'(\''+id+'\')');
					if (navigator.appName == 'Microsoft Internet Explorer') {
							bHasValidValue = eval(validation(id));
					} else {
							bHasValidValue = eval(validation.name+'(\''+id+'\')');
					}
				}
				if (required == yes) {
					if (!bHasValue || !bHasValidValue) {
						result = false;
					}
				} else {
					if (bHasValue && !bHasValidValue) {
						result = false;
					}
				}
				break;
		}
		if (result == true) {
			deleteErrorMessage(id);
			validationSuccess = validationSuccess && true;
		} else {
			alertError(id,message);
			if (getErrorFieldId() == null) {
				setErrorFieldId(id);
			}
			validationSuccess = false;
		}
		return result;
	}
}

function checkForMatchingPassword(id1, id2, message) {
	if (dojo.byId(id1).value != dojo.byId(id2).value) {
		if (!dojo.byId(id1 + _error) && !dojo.byId(id2 + _error)) {
			alertError(id2, message);
			validationSuccess = false;
			if (getErrorFieldId() == null) {
				setErrorFieldId(id2);
			}
		}
	}
}

function focusOnErrorField() {
	dojo.byId(getErrorFieldId()).focus();
}

