/*
	Copyright 2008 Lee S. Barney
	
	
    This file is part of QuickConnectiPhoneHybrid.

    QuickConnectiPhoneH is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QuickConnectiPhoneH is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with QuickConnectiPhoneH.  If not, see <http://www.gnu.org/licenses/>.


 */
 
 
 /*
 *  This section of the file contains the front controller through which all requests for application activity are made.
 */
 
 function handleRequest(aCmd){
	//document.getElementById('nameDisplay').innerHTML = aCmd;
	if(aCmd != null){
		session.addAttribute('prevCmd', session.getAttribute('curCmd'));
		session.addAttribute('curCmd', aCmd);
		if(checkValidation(aCmd)){
			var data = dispatchToBCF(aCmd);
			/*
			* If the business rule does not need to contact a server
            * or the phone data access object for data
			* then it can return data itself and the call to the VCF can 
			* be done synchronously.  Any business rule returning null or having 
			* no return will require the VCF dispatching to be done asynchronously. 
			* This is the responsibility of the business rule or some delegate of it such as
            * a call to the server access or phone data access object.
			*/
			if(data != null){
				dispatchToVCF(aCmd, data);
			}
		}
		else{
			dispatchToError(aCmd, 'validation failed');
		}
	}
}

/*
*   This section of the file contains the validation, security, business, view, and error controllers.
*
*/
var validationMap = new Array();
var businessMap = new Array();
var viewMap = new Array();
var errorMap = new Array();
var securityMap = new Array();

var BUSINESS_CALL = 0;
var VCF_CALL = 1;
var ERROR_CALL = 2;
var SECURITY_CALL = 3;
var VALIDATION_CALL = 4;
/*
* the dispatch method is used to handle all events from the GUI, 
* all events when the server sends data back as a response to a request, 
* and all error events.
*/
function dispatchToBCF(cmd, event){
    if(event == null){
        event = window.event;
    }
    if(event != null){
        stopDefault(event);
    }

    var businessBean = businessMap[cmd];
    return businessBean['bcf']();
}

function dispatchToVCF(aCmd, data){
    /*
    * the command will be null if dispatch is 
    * being called due to a successful return 
    * from the server or the database.
    */
    if(aCmd == null){
        aCmd = session.getAttribute('curCmd');
    }
	
    var veiwBean = viewMap[aCmd];
    if(veiwBean != null){
        var aList = veiwBean['viewFunctionArray'];
        var numFuncs = aList.length;
        for(var i = 0; i < numFuncs; i++){
            retVal = aList[i](data);
			//window.alert(retVal);
            /*
            * a view control function can return 'stop' or nothing.
            * if 'stop' is returned then no more vcfs will be called
            * from the vcf stack
            */
            if(retVal && retVal == 'stop'){
                break;
            }
        }
    }
}

function dispatchToError(errorCommand, errorMessage){
    var errorMappingBean = errorMap[errorCommand];
    return errorMappingBean['ecf'](errorMessage);
}
	
/*  
 * The checkSecurity is responsible for executing all of the default and 
 * any custom security functions associated with a specific command.
 */
function checkSecurity(securityCommand, data){
	return check(securityCommand, 'security', data);
}

/*  
 * The checkSecurity is responsible for executing all of the default and 
 * any custom security functions associated with a specific command.
 */
function checkValidation(validationCommand){
	return check(validationCommand, 'validation', null);
}

/*
 * This function is not intended to be called directly by the programmer.  Do not use it.
 */
function check(command, type, data){
	var retVal = true;
	/*
	 * execute all of the default functions that apply to all commands if there are any default functions defined.
	 */
	var map = securityMap;
	if(type == 'validation'){
		map = validationMap;
	}
	//var map = type + 'Map';
	var aBean = map['default'];
	if(aBean != null){
		var aList = aBean[type+'FunctionArray'];
		var numFuncs = aList.length;
		for(var i = 0; i < numFuncs; i++){
			retVal = window[aList[i]](data);
			if(retVal == false){
				break;
			}
		}
	}
	/*
	 * if the default functions have passed then do those specifically for the command
	 */
	if(retVal == true){
		aBean = map[command];
        
    var found = (aBean != null);
    
		if(found){
			var aList = aBean[type+'FunctionArray'];
			var numFuncs = aList.length;
			for(var i = 0; i < numFuncs; i++){
				retVal = aList[i](data);
				if(retVal == false){
					break;
				}
			}
		}
	}
	return retVal;
}
