﻿//	**************************************************************************
//	Jena - Javascript Library (Controls)
//	requires	jena.Services
//				jena.Form
//				jena.Dialog
//				jena.Core
//
//	Written by Robert Swiston
//
//	Copyright 2010, RomiDesigns - All Rights Reserver
//
//	No part of this script may be reproducted or altered without the
//	expressed consent of RomiDesigns or its representative(s)
//	**************************************************************************

function jenaControls(pWarning)
{
	var self = this;
	
	pWarning = pWarning || "";
	
	this.messageControl = (pWarning != "" ? document.getElementById(pWarning) : null);
	
	return self;
};

jenaControls.prototype.application = function()
{
	var self = this;
	
	//show the appliction (chitose) login control
	this.login = function()
	{
		var SoapResponse = jServices().ApplicationService().call("LoginDialog", {OnCancel:"jDialog.close();", OnLogin:"jControls('this.id.Warning').login(jForm().value('this.id.Email'), jForm().value('this.id.Password')).authenticate();", OnForgot:"", OnCreate:""});
		var DivControl = jCore().create("div").text(SoapResponse.result.value).member;
		
		alert(DivControl, "Login");
	};
	
	return self;
};

jenaControls.prototype.controls = function()
{
	var self = this;
	
	this.callService = function(pReference, pObject)
	{
		return jServices().ControlService().call(pReference, pObject);
	};
	//change the state values of the state control to the return values from the country selected
	this.changeCountry = function(pCountry, pStateId)
	{
		var State = jCore("#" + pStateId).member;
		
		if (State != null)
		{
			jForm().clearControl(State);
			var StateNodes = this.getStates(pCountry).nodes("//cState");
			for (var iState = 0; iState < StateNodes.length; iState++)
			{
				var NewOption = document.createElement("option");
					
				NewOption.text = StateNodes[iState].node("Name").value;
				NewOption.value = StateNodes[iState].node("Abbreviation").value;
				
				if (document.all && !window.opera) State.add(NewOption);
				else State.add(NewOption, null);
			}
		}
	};
	//get the list of states from the submitted country
	this.getStates = function(pCountry)
	{
		var Soap = this.callService("GetStates", {Country:pCountry});
		
		return Soap.parent;
	};
	return self;
};

jenaControls.prototype.login = function(pName, pCredentail, pFirst, pLast, pPhone)
{
	var self = this;
	
	pName = pName || "";
	pCredentail = pCredentail || "";
	pFirst = pFirst || "";
	pLast = pLast || "";
	pPhone = pPhone || "";
	
	var LoginControlId = null;
	var AccountControlId = null;
	
	this.callService = function(pReference, pObject)
	{
		return jServices().UserService().call(pReference, pObject);
	};
	//authenticate the user's credential and either show the error or refresh the page
	this.authenticate = function(pUrl)
	{
		var Soap = this.callService("Login", {UserName:pName, PassWord:pCredentail});
		
		if (Soap.error !== "") jCore(this.messageControl).showError(Soap.error);
		else jPage.navigate(pUrl || "");
	};
	//send an email to the user with their password
	this.forgotPassword = function()
	{
		var Soap = this.callService("ForgotPassword", {UserName:pName});
		
		if (Soap.error !== "") jCore(this.messageControl).showError(Soap.error);
	};
	//log out the curren tuser
	this.logout = function()
	{
		var Soap = this.callService("LogOut");
		jPage.navigate();
	};
	this.registerComplete = function(pUrl)
	{
		var Soap = this.callService("Register", {UserName:pName, FirstName:pFirst, LastName:pLast, PhoneNumber:pPhone});
		
		if (Soap.error !== "") jCore(this.messageControl).showError(Soap.error);
		else jPage.navigate(pUrl || "");
	};
	//register the new user account with the database
	this.registerValidate = function(pAccountId, pLoginId, pUrl)
	{
		var MemberId = this.messageControl.id;
		
		var Soap = this.callService("UserExists", {UserName:pName});
		var OnClose = function(pResult)
		{
			if (pResult === 2)
			{
				jForm().clearForm(AccountControlId);
				jForm().value(LoginControlId + ".Email_Address", pName);
			}
			else
			{
				var Control = new jControls(MemberId).login(pName, pCredentail, pFirst, pLast, pPhone).registerComplete(pUrl);
			}
		};
		
		if (Soap.result.value.toLowerCase().indexOf("user already exists") > 0)
		{
			if (jCore("#" + pLoginId).isNull) jCore(this.messageControl).showError(Soap.error);
			else
			{
				AccountControlId = pAccountId;
				LoginControlId = pLoginId
				alert("This email already has an account here<br/>Would you like to login?", "Account Registration", null, "YesNo", null, OnClose);
			}
		}
		else
		{
			this.registerComplete(pUrl);
		}
	};
	return self;
};

var jControls = function(pWarning)
{
	return new jenaControls(pWarning);
};

