
/**
 */
function validateEmail(val) {
	var emailre;

	emailre = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

	return (val != "" && emailre.test(val));
}

/**
 * Funds in the database are stored as integers which are ten times the actual
 * stored value.  When displaying these values, we convert to a monetary display
 * by diving the value by 100 and padding appropriately.
 */
function fundsToMoney(val) {
	var money;
	var fval;

	money = val.toString();
	if (money.indexOf('.') == -1) {
		money += ".00";
	} else if (money.indexOf('.') == 0) {
		money += ".00";
	} else if (money.indexOf('.') == (money.length - 2)) {
		money += "0";
	}
	money = "$" + money;

	return (money);
}

/**
 */
function inspectObj(obj) {
	var res;

	if (obj == null) {
		alert("Cannot inspect null");
		return;
	}

	res = "";

	for (key in obj)
		res += key + ": " + obj[key] + "\n";

	alert("OBJECT: " + obj + "\nFields:\n" + res);
}

/**
 */
function sendAsyncPostRequest(dao) {
	var postData;
	var i;

	postData = "op=" + dao.operation;
	postData += "&ent=" + dao.entity;
	for (i = 0; i < dao.form.elements.length; i++) {
		if (dao.form.elements[i].name.length > 0) {
			postData += "&" + dao.form.elements[i].name + "=" +
			  dao.form.elements[i].value;
		}
	}

	var conn = YAHOO.util.Connect.asyncRequest("POST", dao.cgi, dao.callback,
	  postData);
}

/*
 */
function toggleBlock(id, buttonId) {
	var button;
	var e;

	e = document.getElementById(id);
	if (e == null)
		return;

	e.style.display = (e.style.display == "none") ? "block" : "none";

	if (buttonId != null) {
		button = document.getElementById(buttonId);
		if (button != null) {
			if (e.style.display == "none")
				button.className = "pushbutton";
			else
				button.className = "pushbutton-down";
		}
	}
}

/**
 */
function populateContactSelects(viewManager, form) {
	var stateProvSelect;
	var timeZoneSelect;
	var countrySelect;
	var roleSelect;
	var prefix;

	if (viewManager == null) {
		prefix = "";
	} else {
		prefix = viewManager.getInspectorPrefix();
		if (prefix != "")
			prefix += "_";
	}

	if (form == null) {
		roleSelect = document.getElementById(prefix + "role");
		timeZoneSelect = document.getElementById(prefix + "timezone");
		stateProvSelect = document.getElementById(prefix + "stateProv");
		countrySelect = document.getElementById(prefix + "country");
	} else {
		/*
		 * IE8 plugs a form member called "role" in to forms with no element
		 * named "role".  This mystery element is a string.
		 */
		roleSelect = form.role;
		if (typeof(roleSelect) == "string")
			roleSelect = null;
		timeZoneSelect = form.timezone;
		stateProvSelect = form.stateProv;
		countrySelect = form.country;
	}

	/*
	 * IE8 plugs a form member called "role" in to forms with no element named
	 * role.  This mystery element is a string.
	 */
	if (roleSelect != null)
		populateDBRoles(roleSelect);

	if (timeZoneSelect != null)
		populateTimeZones(timeZoneSelect);

	if (countrySelect != null && stateProvSelect != null) {
		populateCountries(countrySelect);
		updateStateProv(stateProvSelect, countrySelect);
	}
}

/**
 */
/*
YAHOO.msportal.dialog.wait = null;
YAHOO.util.Event.onDOMReady(function () {

	YAHOO.msportal.dialog.wait = 
	  new YAHOO.widget.Panel("wait",  
		{
			width: "240px", 
			fixedcenter: true, 
			close: false, 
			draggable: false, 
			zindex:4,
			modal: true,
			visible: false
		} 
	);
				
	YAHOO.msportal.dialog.wait.setHeader("Loading, please wait...");
	YAHOO.msportal.dialog.wait.setBody("<img src=\"images/rel_interstitial_loading.gif\"/>");
	YAHOO.msportal.dialog.wait.render(document.body);
});
*/

/**
 */
var errorLookup = {
MSRVP_DB_FAIL_NOERROR: "No error.",
REASON_INTERNAL_ERROR: "An error occurred processing your request.  Please notify us at support@elfresko.com.",
MSRVP_DB_FAIL_EVAL_EXCEEDED: "You have exceeded the maximum number of evaluation licenses allowed.",
MSRVP_DB_FAIL_INSUFFICIENT_FUNDS: "Your account has insufficient funds.", 
MSRVP_DB_FAIL_DUPLICATE_KEY: "An account with that email already exists.",
MSRVP_DB_FAIL_USER_NOT_FOUND: "User not found.",
MSRVP_DB_FAIL_INCORRECT_PASSWORD: "Incorrect password supplied.",
MSRVP_DB_FAIL_FSID_DUPE: "You have already created a license for this FSID.",
MSRVP_DB_FAIL_SEND_VALIDATION: "Failed sending validation to your email address.  Please notify us at support@elfresko.com.",
MSRVP_DB_FAIL_ALREADY_VALIDATED: "This account is already validated.",
MSRVP_DB_FAIL_VALIDATION_TOKEN_MISMATCH: "Validation failed.  Please try again.",
MSRVP_DB_FAIL_UNVALIDATED_ACCOUNT: "This account is not yet validated.\n\nPlease check your email or click 'Resend Validation' to try again."
};

/*
 */
var MD_VALIDATE_DEFAULT			= 100;
var MD_VALIDATE_STR_NOT_EMPTY	= 110;
var MD_VALIDATE_STR_NUMERIC		= 120;
var MD_VALIDATE_STR_EMAIL		= 130;

/**
 */
function ManagedForm(formName, validation) {
	this.validation = validation;
	this.initialized = false;
	this.formName = formName;
	this.form = null;

	/**
	 */
	this.init = function() {

		if (this.formName == null) {
			alert("ManagedForm requires a form name.");
			return;
		}

		if (this.validation == null) {
			alert("ManagedForm requires a validation structure.");
			return;
		}
	}

	/**
	 */
	this.getForm = function() { return (this.form); }

	/**
	 */
	this.validateDefault = function(e, label) {

		switch (e.nodeName) {
		case "INPUT":
			switch (e.type) {
			case "text":
				return (this.validateStrNotEmpty(e, label));
			case "password":
				/* passwords will have their own custom validation */
				return (true);
			default:
				alert("Unsupported INPUT type: " + e.type);
				return (false);
			}
			break;
		case "SELECT":
			if (e.selectedIndex <= 0) {
				alert("Please choose a " + label + ".");
				return (false);
			}
			return (true);
		default:
			alert("Unsupported node type: " + e.nodeName);
			return (false);
		}

		return (true);
	}

	/**
	 */
	this.validateStrNotEmpty = function(e, label) {

		if (e.value == "") {
			alert("Please enter a value for '" + label + "'.");
			return (false);
		}

		return (true);
	}

	/**
	 */
	this.validateStrNumeric = function(e, label) {

		if (isNaN(e.value)) {
			alert("Please enter a number for '" + label + "'.");
			return (false);
		}

		return (true);
	}

	/**
	 */
	this.validateStrEmail = function(e, label) {

		if (!validateEmail(e.value)) {
			alert("Please enter an email address for '" + label + "'.");
			return (false);
		}

		return (true);
	}

	/**
	 */
	this.validate = function() {
		var check;
		var label;
		var e;

		this.form = document[this.formName];

		for (var key in this.validation) {

			if (key == "custom")
				continue;

			e = this.form[key];
			if (e == null) {
				alert("Failed retrieving expected validation target '" +
				  key + "'");
				return (false);
			}

			check = this.validation[key].check;
			label = this.validation[key].label; 

			switch (this.validation[key].check) {
			case MD_VALIDATE_DEFAULT:
				if (!this.validateDefault(e, label))
					return (false);
				break;
			case MD_VALIDATE_STR_NOT_EMPTY:
				if (!this.validateStrNotEmpty(e, label))
					return (false);
				break;
			case MD_VALIDATE_STR_NUMERIC:
				if (!this.validateStrNumeric(e, label))
					return (false);
				break;
			case MD_VALIDATE_STR_EMAIL:
				if (!this.validateStrEmail(e, label))
					return (false);
				break;
			default:
				alert("Unsupported validation type: " + this.validation[key]);
				return (false);
			}
		}

		if (this.validation.custom != null)
			return (this.validation.custom());

		return (true);
	}
}

/**
 */
function ManagedDialog(id, managedForm) {
	this.managedForm = managedForm;
	this.initialized = false;
	this.specialFields = []; 
	this.onShow = null;
    this.id = id;

	/**
	 */
	this.init = function() {

		YAHOO.util.Dom.removeClass(this.id, "yui-pe-content");

		YAHOO.msportal.dialog[this.id] = new YAHOO.widget.Dialog(this.id, {
			width : "30em",
			fixedcenter : true,
			visible : false, 
			constraintoviewport : true,
			draggable:false,
			modal: true,
			close: true
		});

		YAHOO.msportal.dialog[this.id].render();

		this.initialized = true;
	}

	/**
	 */
	this.getManagedForm = function() { return (this.managedForm); }

	/**
	 */
	this.addSpecialField = function(name) {
		this.specialFields.push(name);
	}

	/**
	 */
	this.getSpecialFields = function() {
		return (this.specialFields);
	}

	/**
	 */
	this.setOnShow = function(onShow) {
		this.onShow = onShow;
	}

	/**
	 */
	this.show = function() {
		if (!this.initialized)
			this.init();
		if (this.onShow != null)
			this.onShow();
		YAHOO.msportal.dialog[this.id].show();
	}

	/**
	 */
	this.cancel = function() {
		if (!this.initialized)
			this.init();
		if (this.form != null)
			this.form.reset();
		YAHOO.msportal.dialog[this.id].hide();
	}

	/**
	 */
	this.dismiss = function() {
		if (!this.initialized)
			this.init();
		if (this.form != null)
			this.form.reset();
		YAHOO.msportal.dialog[this.id].hide();
	}
}

