// These three functions must be included in your page.

// validatePublisherForm() is called before processing offers form.
// This is where you validate data from your own form, and if necessary, returns error.
// If this function returns false, offers won't be processed.
function validatePublisherForm() {
	if(document.getElementById("CoRegForm")) {
		return true;
	} else {
		if(validatorUserAdd.form()) {
			var params = "_method=POST&"+escape("data[User][email]")+"="+escape(jQ("#UserEmail1").val());
			var data = jQ.ajax({
				type: 'POST',
				url: "users/register.json",
				data: params,
				dataType: "json",
				async: false
			}).responseText;

			if(data) {
				data = eval("["+data+"]");
				if(data[0]) {
					if(data[0].error) {
						jQ("#UserEmail").val(jQ("#UserEmail1").val());
						showLogin();
					}
					else
						return true;
				}
			}
		}
	}

	return false;
}

// submitPublisherForm() is called by the LSF scripts after processing the coreg offers sending data to the LSF server.
// This function must be the only way to submit the form.
function submitPublisherForm() {
	if(document.getElementById("CoRegForm")) {
		jQ("#CoRegForm").submit();
	} else {
		jQ("#UserAddForm").submit();
	}
}

// This is a security wrapper, used to call the two previous functions.
function LSFaction(action) {
	switch (action) {
		case 0: if (window.display_LSF_offers) display_LSF_offers(); break;
		case 1: if (window.LSF_submit) LSF_submit(); else if (validatePublisherForm()) submitPublisherForm(); break;
	}
}
