/*
	Copyright 2008 Lee S. Barney
	
	
    This file is part of QuickConnectiPhoneHybrid.

    QuickConnectiPhoneHybrid 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.

    QuickConnectiPhoneHybrid 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 QuickConnectiPhoneHybrid.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
 * 
 *  Validation Functions
 *
 *
 *
 * Custom validation functions will only be called if the request passed all of the default
 * validation functions.
 * 
 * It should be remembered that there is no automated data passing from one validation function to the next.
 * It is intended that validation functions should be independent of each other to make them easier 
 * to design and maintain.
 * 
 * 
 */
    

//There are no default validation functions for this application example.

/*
 * This validation function ensures that the name field is not empty
 */

 
 
 /*
 *
 *
 *  Business Control Functions
 *
 *
 */

 
function loadBCF(){
	//load what the current semester is and those dates
	setSemesterDates(); //in semesterdates.js
	
	//if a local XML file is passed in, use it, else don't
	if(session.getAttribute('curAreaSettings').localXMLFileURL){
		var accessObject = new ServerAccessObject(session.getAttribute('curAreaSettings').localXMLFileURL); //curAreaSettings is is set in the body tag of each of the area pages
		accessObject.getData(ServerAccessObject.XML, true);
	}
	
	var accessObject = new ServerAccessObject('http://activities.byui.edu/EventsSvc/');
	//window.alert(accessObject);
	return accessObject.getData(ServerAccessObject.Text, true, session.getAttribute('curAreaSettings').calAreaParameters);
	
	//use their weird function instead, and see if it works for IE7? -- test only
	//fetchCalXML(); //test only
	//alert(session.getAttribute('resultSet')); //test only
	//return accessObject.getData(ServerAccessObject.XML, true, "Category=" + session.getAttribute('actType') + "&ForDays=" + '120');//getDays());
	//alert('ie7-1'); //test only
}


function getDetailBCF(){
	//document.getElementById('displayName').innerHTML = database.getData;	
	//document.getElementById('nameDisplay').innerHTML = 'getting Name '+window['openDatabase'];
	database.getData('SELECT name FROM names WHERE id=1');
	//return nothing since database query results are handled asynchronously
}



/*
*
*
*   View Control Functions
*
*
*/

/*
* This function displays a name if one was found in the database or
* if a name was just inserted into the database.
*/
function storeXMLVCF(result){ // all of the functions call in here are stored in VCFFuncs1.js
	if (!result.childNodes){ //if result is text, parse it to XML
		//sendConsole("functions.js:91 " + result);
		result = CreateXMLStringParser(result);
	}

	if(result.getElementsByTagName('Semester')[0]){
		//localFile
		session.addAttribute('customXML', result);	
	}
	else{
		//main
		session.addAttribute('mainXML', result);
	}
	//if(session.hasAttribute('customXML') && session.hasAttribute('mainXML')){
		//alert(session.getAttribute('customXML').childNodes[3].childNodes[1].childNodes[0].nodeValue); //test only
		//var testSession = session.getAttribute('mainXML');//test only
		//var testSession2 = session.getAttribute('mainXML').getElementsByTagName('Event')[0].childNodes[1].childNodes[0].nodeValue; // test only
		//inspectObject("functions.js:107", session.getAttribute('mainXML'));//test only -- throws error WHY DOESN't THIS WORK?!?!?!?!?!
		//alert('ie7-2'); //test only
		//sendConsole("functions.js:109 " + testSession2); // test only
		
	if(session.hasAttribute('customXML') && session.hasAttribute('mainXML')){	
		session.addAttribute('XMLToEvtSetterMap', createXMLTagMap());
		
		var eventMap = new Object();
		
		if(session.hasAttribute('mainXML')){ //these are conditional, so if one fails, the other will still load
			generateEvents(eventMap,session.getAttribute('mainXML')); 
		}
		
		if(session.hasAttribute('customXML')){
			generateEvents(eventMap,session.getAttribute('customXML'));
		}
		
		session.removeAttribute('customXML');//is this a good idea to unload it?
		session.removeAttribute('mainXML');
	}
	
}
/*
* This function displays to the user that they need to enter a name for display since no
* stored value could be found.
*/ //this function will hold the functions to output the data to the calApp
function showCalAppVCF(data){ // all of the functions call in here are stored in VCFFuncs2.js
   
	activateEventTabs(); //show the event content
	//if outdoor, switch the semeseter tab?
	
	switchGlobalTab(session.getAttribute('semester').curSemester);
   
	displayEventList(session.getAttribute('eventMap'));
	//window.alert(data);

   
	//call the functions that activate the proper tab 
		
}


function showHomeArea(){
	activateHomeTab(); //in VCSFuncs2.js
	
}




/*
*
*
*   Security Control Functions
*
*
*/

//Since no there is no accessing of data from servers there are no security control functions in this example


/*
*
*
*   Error Control Functions
*
*/

function dbError(){
    displayPromptVCF();
}




