/*
The SoftAd Group
Copyright (c) 2000-2004 The SoftAd Group, Inc.  All rights reserved
*/
// To use this object the following files must be also included.
// CNFormHandler.js
// CNForm.js  - see [CNFormHandler.prototype = new CNForm;] at bottom.
// CNCommon.js
// HTMLDom.js
// CNObjectDOM.js
// CNString.js
// [en-us.js] -or- [de-de.js] -or- [es-es.js] -or- [fr-fr.js] for message constants

// -------------------------------------------------------------------------------
// File name        : CNFormHandler.js [class file]
// -------------------------------------------------------------------------------
// Author           : Ken Wimberley
// Created on       : July 2, 2003
// 
// This file contains the the CNFormHandler object that holds all the specific methods
// for form submittal that are used in ChannelNet.  CNFormHandler specifically deals
// with the dynamic posting of forms.
// -------------------------------------------------------------------------------
// Last Updated     : November 11, 2003
// Updated by       : Ken Wimberley
// -------------------------------------------------------------------------------
// Copyright (c) 2003 The SoftAd Group, Inc.


// -------------------------------------------------------------------------------
// Function         : CNFormHandler()
// -------------------------------------------------------------------------------
// Author           : Ken Wimberley
// Created on       : July 2, 2003
// 
// This function creates the CNFormHandler

/*Begin Class*/
function CNFormHandler() {
	//	********************** Public properties *******************
	//	************************************************************
	this.sCharIgnore = "_";
	//	********************** Public methods **********************
	//	************************************************************
	this.create = create;
	this.getGrammar = getGrammar;
	this.getEnvironment = getEnvironment;
	//	********************** Private methods **********************
	//	************************************************************	
	this._isValidCNForm = _isValidCNForm;
	
	//	************************** Globals *************************
	//	************************************************************
	var bReturn;
	var iReturn;
	var sReturn = "";
	var sErrors = "";
	var oReturn = null;
	
	// -------------------------------------------------------------------------------
	// Function         : create()
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : Wednesday, July 2, 2003
	// 
	// This function returns a new CNFormHandler object.
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME		REQUIRED	TYPE        DESCRIPTION
	// -------------------------------------------------------------------------------	
	// 
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------	
	function create(){
		var oNewCNFormHandler = new CNFormHandler();
		return oNewCNFormHandler;		
	}
	
	function isValidCNForm(oForm){
		sReturn = "";
		if (!oForm.cn) {
			sReturn += "2" + ",";
		}
		if (oForm.cn.value == '') {
			sReturn += "3" + ",";			
		}
		if (!oForm.act) {
			sReturn += "4" + ",";			
		}
		if (oForm.act.value == "") {
			sReturn += "5" + ",";
		}
		if (!oForm.debug) {
			sReturn += "6" + ",";			
		}
		if (oForm.debug.value != '0' && oForm.debug.value != '1' && oForm.debug.value != '2' && oForm.debug.value != '3') {
			sReturn += "7" + ",";			
		}
		if (!oForm.crt) {
			sReturn += "8" + ",";			
		}
		if (!oForm.pageID) {
			sReturn += "9" + ",";			
		}
		if (oForm.pageID.value == '') {
			sReturn += "10" + ",";			
		}
		if(sReturn != ""){
			sReturn = sReturn.substring(0,sReturn.length-1);
		}
		return sReturn;
	}
	
	// -------------------------------------------------------------------------------
	// Function         : _isValidCNForm(oForm)
	// -------------------------------------------------------------------------------
	// Checks oForm for ChannelNet required input elements.  
	// -------------------------------------------------------------------------------
	// Returns a boolean
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME			REQUIRED	TYPE	DESCRIPTION
	// oForm		Required	form	Reference to the form which is being submitted.
	// -------------------------------------------------------------------------------
	// History
	// -------------------------------------------------------------------------------
	// DATE			NAME			DESCRIPTION
	// 08/01/2003   Ken Wimberley	Created	
	// 11/12/2003	Ken Wimberley	Updated
	function _isValidCNForm(oForm){
		bReturn = true;
		var sMessage = "The form does not adhere to ChannelNet grammar rules.";
		if (oForm.cn==null) {
			alert(sMessage + '\nMust have a context(cn)');
			bReturn = false;
		} else if (oForm.cn.value=='') {
			alert(sMessage + '\nMust have a context value');
			bReturn = false;
		} else if (oForm.act==null) {
			alert(sMessage + '\nMust have an action(act)');
			bReturn = false;
		} else if (oForm.act.value=='') {
			alert(sMessage + '\nMust have an action value');
			bReturn = false;
		} else if (oForm.debug==null) {
			alert(sMessage + '\nMust have a debug value');
			bReturn = false;
		} else if (oForm.debug.value!='0' && oForm.debug.value!='1' && oForm.debug.value!='2'&& oForm.debug.value!='3') {
			alert(sMessage + '\nMust have a debug value of 0,1, 2, or 3');
			bReturn = false;
		} else if (oForm.crt==null) {
			alert(sMessage + '\nMust have a criteria(crt)');
			bReturn = false;
		}
		return bReturn;
	}
			
	// -------------------------------------------------------------------------------
	// Function         : getGrammar(sFormID, bValidate)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 31, 2003
	// 
	// This function creates and returns a new instance of a CNGrammar object
	// or null if an error exists in the form.
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME			TYPE        DESCRIPTION
	// sFormID		string		id of form
	// bValidate	boolean		switch whether to validate or not
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function getGrammar(sFormID, bValidate){
		sErrors = "";
		oReturn = null;
		var oForm = document.getElementById(sFormID);
		if(oForm){		
			if(bValidate){
			
				if(this._isValidCNForm(oForm)){
					var oCNGrammar = new CNGrammar();
					oReturn = oCNGrammar.create(oForm);
				}
								
			} else {
				var oCNGrammar = new CNGrammar();
				oReturn = oCNGrammar.create(oForm);				
			}
		}
		return oReturn;
	}
	
	// -------------------------------------------------------------------------------
	// Function         : getEnvironment()
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : August 6, 2003
	// 
	// This function creates and returns a new instance of a CNEnvironment object
	// or null if an error exists in the form.
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// This function expects one to many ID string values of forms
	//
	// IMPORTANT!	When an submitting an environment, Channelnet may assign theme
	//				based on the LAST request.  For multi-action calls using SiteBuilder runtime,
	//				the SiteBuilder form used for assigning theme must have an id = 'form_default'
	//				In order for ChannelNet to properly assign theme for SiteBuilder runtime,
	//				the LAST form ID passed MUST be 'form_default'.
	//				See CN JavaScript Class Library Specification.doc for more info.
	//	
	// example:		getEnvironment('form1', 'form2', 'form_default')
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function getEnvironment(){
		var args = getEnvironment.arguments;
		var argLen = getEnvironment.arguments.length;
		var oCNGrammar = null;
		var aryCNGrammars = new Array();
		var oDefaultForm = null;
		var oCNE = null;
		var bCNEExists = false;
		var oReq = null;
		var bMetricslog = false;
		var oParams, paramLen, sParam;
		
		try {
		
			for(var i=0; i<argLen; i++){
				oCNGrammar = this.getGrammar(args[i],false);
				if(oCNGrammar){
					if (!bCNEExists && oCNGrammar.label.toLowerCase() == "default"){
						oCNE = oCNEnvironment.create('pageid',oCNGrammar.pageID);
						bCNEExists = true;												
					}
					aryCNGrammars[i] = oCNGrammar;					
				}
			}
			CNGrammarsLen = aryCNGrammars.length;
			for(i=0; i<CNGrammarsLen; i++){			
				
				if(bCNEExists && aryCNGrammars[i].label.toLowerCase() != "default"){
					if(aryCNGrammars[i].form._metricslog){
						bMetricslog = aryCNGrammars[i].form._metricslog.value;
					}
					oReq = oCNE.addRequest(aryCNGrammars[i].label, bMetricslog, aryCNGrammars[i].cn, aryCNGrammars[i].act, aryCNGrammars[i].lang);
					oParams = aryCNGrammars[i].form._param;
					if(oParams.length && oParams.length > 1){					
						paramLen = oParams.length;
						for(j=0; j<paramLen; j++){
						
							if(oParams[j].value != ""){
								sParam = getParam(oParams[j].id, aryCNGrammars[i].label);
								if(sParam != ""){
									oReq.addParameter(sParam, oParams[j].value);
								}
							}					
						}
					} else {
						sParam = getParam(oParams.id, aryCNGrammars[i].label);
						if(sParam != ""){
							oReq.addParameter(sParam, oParams.value);
						}
					}
				}
				
				if(bCNEExists && aryCNGrammars[i].label.toLowerCase() == "default"){
					if(aryCNGrammars[i].form._metricslog){
						bMetricslog = aryCNGrammars[i].form._metricslog.value;
					}
					oReq = oCNE.addRequest('default', bMetricslog, aryCNGrammars[i].cn, aryCNGrammars[i].act, aryCNGrammars[i].lang);

					oParams = aryCNGrammars[i].form._param;
					paramLen = oParams.length;
					for(j=0; j<paramLen; j++){
						if(oParams[j].value != ""){
							sParam = getParam(oParams[j].id, aryCNGrammars[i].label);								
							if(sParam != ""){
								oReq.addParameter(sParam, oParams[j].value);
							}
						}							
					}
				}
				
			}
		}
		finally {
			return oCNE;
		}
	}
	
	function getParam(sEltID, sFormLabel){
		sReturn = "";
		//sEltID should look like "default_channelkey" where "default" is sFormLabel
		var iStart = sEltID.indexOf(sFormLabel,0);
		if(iStart >= 0){
			sReturn = sEltID.substring((iStart + sFormLabel.length + 1), sEltID.length);
		}
		return sReturn;	
	}
	
}
/*End Class*/
//CNFormHandler.prototype = new CNForm;
var oCNFormHandler = new CNFormHandler();

/*Begin Sub Class*/
// -------------------------------------------------------------------------------
// Class	        : CNGrammar()
// -------------------------------------------------------------------------------
// Author           : Ken Wimberley
// Created on       : June 28, 2003
// 
// This object represents a "bag" of ChannelNet specific form properties that when
// passed to the oCNForm object via the submitGrammar() method will dynamically create
// a ChannetlNet single-action form post.
// -------------------------------------------------------------------------------
// Last Updated     : 
// Updated by       : 
// -------------------------------------------------------------------------------

function CNGrammar(){
	//	********************** Public properties *******************
	//	************************************************************
	this.form = null;
	this.cn = "";
	this.act = "";
	this.crt = "";
	this.lang = "";
	this.style = "";
	this.pageID = "";
	this.debug = "";
	this.label = "";
	this.xml = "";
	//	********************** Public methods **********************
	//	************************************************************
	this.create = create;	
	//	********************** Private methods **********************
	//	************************************************************
	this._setXML = null;	//To be implemented
	//	************************** Globals *************************
	//	************************************************************
	var bReturn;
	var iReturn;
	var sReturn = "";
	
	function create(oForm){
		var oNewCNGrammar = new CNGrammar();
		oNewCNGrammar.form = oForm;
		oNewCNGrammar.label = oForm.name;
		
		for(var i=0; i<oForm.elements.length; i++){
			switch(oForm.elements[i].name.toLowerCase()){
				case "cn":
					oNewCNGrammar.cn = oForm.elements[i].value;
					break;
				
				case "act":
					oNewCNGrammar.act = oForm.elements[i].value;
					break;
				
				case "crt":
					oNewCNGrammar.crt = oForm.elements[i].value;
					break;
				
				case "lang":
					oNewCNGrammar.lang = oForm.elements[i].value;
					break;
					
				case "style":
					oNewCNGrammar.style = oForm.elements[i].value;
					break;
				
				case "pageid":
					oNewCNGrammar.pageID = oForm.elements[i].value;
					break;
				
				case "debug":
					oNewCNGrammar.debug = oForm.elements[i].value;
					break;
			}
		}
		return oNewCNGrammar;
	}
}
/*End Sub Class*/

/*Begin Sub Class*/
// -------------------------------------------------------------------------------
// Class			: CNEnvironment()
// -------------------------------------------------------------------------------
// Author           : Ken Wimberley
// Created on       : June 28, 2003
// 
// This object exposes methods to build an XML string allowing multiple grammar calls.
// This function creates the CNEnvironment.  The CNObjectDOM is required.
// -------------------------------------------------------------------------------
// Last Updated     : 
// Updated by       : 
// -------------------------------------------------------------------------------

function CNEnvironment() {
	//	********************** Public properties *******************
	//	************************************************************
	this.root = null;
	this.environment = null;
	this.systemvars = null;
	//	********************** Public methods **********************
	//	************************************************************
	this.create = create;
	this.addRequest = addRequest;
	this.getXML = getXML;
	//	********************** Private methods **********************
	//	************************************************************
	this.addSystemVar = addSystemVar;
	this.createCNRequest = createCNRequest;	
	//	************************** Globals *************************
	//	************************************************************
	var bReturn;
	var iReturn;
	var sReturn = "";
	// -------------------------------------------------------------------------------
	// Function         : create()
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 28, 2003
	// 
	// This function returns a new CNEnvironment object.
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME				REQUIRED	TYPE        DESCRIPTION
	// -------------------------------------------------------------------------------	
	// sSysVarKey		Required	string	
	// sSysVarValue		Required	string	
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------		
	function create(sSystemVarKey, sSystemVarValue) {
		var oEnv = new CNEnvironment();		
		oEnv.root = oCNObjectDOM.createNode('root');
		oEnv.environment = oCNObjectDOM.createNode('cnenvironment');
		oEnv.root.appendChild(oEnv.environment);
		oEnv.systemvars = oCNObjectDOM.createNode('systemvars');
		oEnv.environment.appendChild(oEnv.systemvars);
		oEnv.addSystemVar(sSystemVarKey, sSystemVarValue);

		return oEnv;
	}

	// -------------------------------------------------------------------------------
	// Function         : addSystemVar(sKey, sValue)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 31, 2003
	// 
	// This function creates and adds a <var> child node to the <systemvars> node
	// of the ChannelNet environment.
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME			TYPE        DESCRIPTION
	// sKey			string		the "name" attribute of the <var> node
	// sValue		string		the text value of the <var> node
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function addSystemVar(sKey, sValue) {
		var oParent = this.systemvars;
		var oNode = oCNObjectDOM.createNode('var');
		oNode.setAttribute('name',sKey);
		oNode.setValue(sValue);
		oParent.appendChild(oNode);
	}
	
	// -------------------------------------------------------------------------------
	// Function         : addRequest(sLabel, sMetricslog, sBO, sAction, sLang)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 31, 2003
	// 
	// This function creates, adds and returns a <cnrequest> child node to the ChannelNet environment.
	// Uses helper function:  createCNRequest()
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME			TYPE        DESCRIPTION
	// sLabel		string		the "label" attribute of the <cnrequest> node
	// sMetricslog	string		the value of the metricslog attribute of the <cnrequest> node
	// sBO			string		the text value of the <businessobject> child node
	// sAction		string		the text value of the <action> child node
	// sLang		string		the text value of the <language> child node
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function addRequest(sLabel, sMetricslog, sBO, sAction, sLang) {
		var oEnv = this.environment;
		var oRequest = this.createCNRequest(sLabel, sMetricslog, sBO, sAction, sLang);
		var oNode = oRequest.request;
		oEnv.appendChild(oNode);
		oRequest.request = oNode;
		return oRequest;
	}
		
	// -------------------------------------------------------------------------------
	// Function         : createCNRequest(sLabel, sMetricslog, sBO, sAction, sLang)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 31, 2003
	// 
	// This helper function creates and returns a <cnrequest> child node to the ChannelNet environment.
	// -------------------------------------------------------------------------------
	// Parameters : see addRequest()
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	// SAMPLE CNRequest fragment
	// -------------------------------------------------------------------------------
	//	<cnrequest label="products" metricslog="false">
	// 		<businessobject>catalog</businessobject>
	// 		<action>view</action>
	// 		<language>145042</language>
	// 		<parameters>
	// 			<parameter name="page">categorynavigate</parameter>
	// 			<parameter name="startnode">6</parameter>
	// 		</parameters>
	//	</cnrequest>
	// -------------------------------------------------------------------------------
	
	function createCNRequest(sLabel, sMetricslog, sBO, sAction, sLang) {
		var oRequest = new CNRequest();
		oRequest.request = oCNObjectDOM.createNode('cnrequest');
		oRequest.request.setAttribute('label',sLabel);
		oRequest.request.setAttribute('metricslog',sMetricslog);
		var oNode = oCNObjectDOM.createNode('businessobject');
		oNode.setValue(sBO);
		oRequest.request.appendChild(oNode);
		oNode = oCNObjectDOM.createNode('action');
		oNode.setValue(sAction);
		oRequest.request.appendChild(oNode);
		oNode = oCNObjectDOM.createNode('language');
		if (sLang=='')sLang='145042';
		oNode.setValue(sLang);
		oRequest.request.appendChild(oNode);
		oNode = oCNObjectDOM.createNode('parameters');		
		oRequest.request.appendChild(oNode);
		oRequest.parameters = oNode;
		return oRequest;
	}

	// -------------------------------------------------------------------------------
	// Function         : getXML()
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 31, 2003
	// 
	// This function returns the xml string of the ChannelNet environment.
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------	
	function getXML() {
		sReturn = this.root.getInnerHTML();
		var re = /VAR/g;
		sReturn = sReturn.replace(re,'var');
		return sReturn;
	}
}
/*End Sub Class*/
var oCNEnvironment = new CNEnvironment();


/*Begin Sub Class*/
// -------------------------------------------------------------------------------
// Sub Class        : CNRequest()
// -------------------------------------------------------------------------------
// Author           : Ken Wimberley
// Created on       : July 31, 2003
// -------------------------------------------------------------------------------
// Last Updated     : 
// Updated by       : 
// -------------------------------------------------------------------------------
function CNRequest(){

	//	********************** Public properties *******************
	//	************************************************************
	this.request = null;
	this.parameters = null;
	//	********************** Public methods **********************
	//	************************************************************
	this.addParameter = addParameter;

	// -------------------------------------------------------------------------------
	// Function         : addParameter(sKey, sValue)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 31, 2003
	// 
	// This function creates and adds a <paramater> child node to the <parameters> node
	// of the ChannelNet environment.
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME			TYPE        DESCRIPTION
	// sKey			string		the "name" attribute of the <parameter> node
	// sValue		string		the text value of the <parameter> node
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function addParameter(sKey, sValue) {
		var oNode = oCNObjectDOM.createNode('parameter');
		oNode.setAttribute('name',sKey);
		oNode.setValue(sValue);
		oParent = this.parameters;
		oParent.appendChild(oNode);
	}
}
/*End Sub Class*/
var oCNRequest = new CNRequest();


