/**
 * This javascript is used by the "CRM Login" dialog.
 * In provides functions the read the profile data from a cookie
 * when the dialog is loaded and to write updated profile data to 
 * the same cookie, when the dialog is saved.
 *
 * Use embGetCrmLogin() to get a reference to this object.
 */

// put everything into a "pseudo-function" to make it private:
(function() {

	var crmLogin = new CrmLogin();
	
	window.embGetCrmLogin = function() {
		//alert("Binding crmLogin to window...");
		return crmLogin;
	}
	
	function CrmLogin() {
	
		/**
		 * The number of dummy profiles that can be saved.
		 */
		var NUM_PROFILES = 3;
		
		/**
		 * The name of the cookie in which the profiledata gets stored.
		 */
		var COOKIE_NAME = "cma_profiles";
		
		/**
		 * The value of ten years in milliseconds. Used as validity for 
		 * the cookies. 
		 */
		var TEN_YEARS = 1000 * 60 * 60 * 24 * 356 * 10;
		
		/**
		 * An array that keeps all keys that are used to identify the profile data.
		 * Commented lines contain values which are not (yet?) supported by profilemanager.
		 */
		var KEYS = new Array();
		KEYS.push("sal"); // salutation
		KEYS.push("ti"); // title
		KEYS.push("fn"); // firstname
		KEYS.push("2fn"); // second first name
		KEYS.push("ln"); // lastname
		KEYS.push("2ln"); // second lastname
		KEYS.push("zip"); // post code
		KEYS.push("brh"); // birth date
		KEYS.push("cfg"); // bm-code of configured car
		KEYS.push("fav"); // bm-code of favorite car
		KEYS.push("favn"); // name of favorite car
		KEYS.push("favp"); // favorite price
		KEYS.push("favc"); // favorite currency
		KEYS.push("favr"); // favorite rate
		KEYS.push("favd"); // favorite date
		KEYS.push("dn1"); // dealer name 1
		KEYS.push("dn2"); // dealer name 2
		KEYS.push("ds"); // dealer street
		KEYS.push("dc"); // dealer city
		KEYS.push("dzip"); // dealer ZIP code
		
		
		/**
		 * A hashtable in which the profiledata gets stored 
		 * when the cookie is read and parsed.
		 */
		var profiledata;
		
		// *************************************
		// private methods:
		// *************************************
		
		/**
		 * Initializes the hashtable that holds the values 
		 * which will be read from the cookie. Sets all values
		 * to empty strings and the value for the current profile to
		 * "logout".
		 */
		function initCrmProfilesData() {
			profiledata = new Array();
			profiledata["pr"] = "logout"
			for (j = 0; j < KEYS.length; j++) {
				for (i = 0; i < NUM_PROFILES; i++) {
					profiledata[KEYS[j] + (i + 1)] = "";
				}
			}
		}
		
		/**
		 * Reads the profile data from the cookie and puts them into a hashtable.
		 */
		function readCrmProfilesJS() {
			
			initCrmProfilesData();
			
			var cookieData = get_cookie(COOKIE_NAME);
			if (cookieData) {
				cookieData = unescape(cookieData);
				var list = cookieData.split("|");
				for (i = 0; i < list.length; i++) {
					var help = list[i];
					var keyval = help.split("=");
					if (keyval.length == 2) {
						profiledata[keyval[0]] = unescape(keyval[1]);
					}
				}
			}
			
			// set the correct radio button for profile selection:
			var selectionValue = profiledata["pr"];
			var radios = document.getElementsByName("Profile_ITEM");
			for (i = 0; i < radios.length; i++) {
				if (radios[i].value == selectionValue) {
					radios[i].checked = true;
				} else {
					radios[i].checked = false;
				}
			} 
		}
		
		
		// *************************************
		// public methods:
		// *************************************
		
		/**
		 * Saves the values from the dialog into a cookie.
		 * This is called automatically when the dialog is closed via 
		 * the save button. 
		 */
		this.setCrmProfileJS = function () {
			
			// determine the currently selected profile:
			var selectedProfile;
			var profileRadios = document.getElementsByName("Profile_ITEM");
			for (i = 0; i < profileRadios.length; i++) {
				if (profileRadios[i].checked) {
					selectedProfile = profileRadios[i].value;
				}
			}
			
			var cookieData;

			if(selectedProfile == "logout"){
				cookieData = "prpr=" + selectedProfile + "|";
				cookieData += "pr=" + selectedProfile + "|";
			}
			cookieData = "pr=" + selectedProfile + "|";
			
			/*
			  Get the value from each input field in each profile and
			  append to a string like this: key1=val1|key2=val2|...
			*/
			for (i = 0; i < NUM_PROFILES; i++) {
			
				for (j = 0; j < KEYS.length; j++) {
					// overwrite the cookie with the profilemanager's value
					if((KEYS[j] == "cfg") && (String((i+1)) == selectedProfile)){
						set_cookie("lastCCBmCode", document.getElementsByName("crmlogin_" + KEYS[j] + "_p" + (i + 1))[0].value, TEN_YEARS, "/");
					}
					cookieData += KEYS[j] + (i + 1) + "=" + escape(document.getElementsByName("crmlogin_" + KEYS[j] + "_p" + (i + 1))[0].value) + "|";
				
				}
			}
			
			cookieData = escape(cookieData);
			set_cookie(COOKIE_NAME, cookieData, TEN_YEARS, "/");
			if(selectedProfile != "logout"){
				set_cookie("prevProf", selectedProfile, TEN_YEARS, "/");
			}
			return true;
		}
		
		/**
		 * Checks, whether the cookie with the CRM profile dummy data is present.
		 */ 
		this.isCrmProfileCookiePresent = function() {
			var test = get_cookie(COOKIE_NAME);
			if (test) {
				return true;
			}
			return false;
		}
		
		/**
		 * Returns a value from the hashtable which was filled from the cookie.
		 *
		 * @param key the key to the profile data. 
		 */
		this.getCrmProfileValueJS = function (/* String*/ key) {
			if (!profiledata) {
				readCrmProfilesJS();
			}
			return profiledata[key];
		} 
		
		/**
		 * Sets the value of an input-element in the current document.
		 *
		 * @param inputFieldName the name of the input field of which the value will be set
		 * @param profileDataKey the key of the profile-data which will be set as value
		 */
		this.fillInputFieldWithCrmProfileDataJS = function (/*String*/ inputFieldName, /*String*/ profileDataKey) {
			var data = this.getCrmProfileValueJS(profileDataKey);
			document.getElementsByName(inputFieldName)[0].value = data;
		}
	}

})();
