	/*
	 * This file houses ajax specific and related functions
	*/	
	
	//use this variable when you need some cleanup done post request or have chained requests
	//this will be reset on each use
	var cleanUpFunction = emptyCleanUp;
	
	//should be set by inc_ajax.jsp
	//some sites have html output by header files, this takes care of that
	var ajaxJSONStartString = '//JSON START';
	
	var userId;
	var userHint;
	var userEmail;
	var sendEmail;
	
	function callAjaxRequest(fullPath, successFunction, failureFunction, createFunction) {		
		createFunction();
	
		dojo.xhrGet({
			url: fullPath,
			handleAs: "text",

			load: function(data, args){
				successFunction(data, args);										
				cleanUp();
			},
			// if any error occurs, it goes here:			
			error: function(error, args){
				failureFunction(error, args);
				cleanUp();
			}
		});
	}	
	
	var emptyCleanUp = function() {
	}
		
	function cleanUp() {
		var cleanUpFunc = cleanUpFunction;
					
		//we clean up first in case they have a third link in the chain
		cleanUpFunction = emptyCleanUp;
		
		if( cleanUpFunc != null && cleanUpFunc != '')
			cleanUpFunc();	
	}
	
	/***********  Get User Info / Email Password for password and hint retrieval      ****************************************************************/
	var info;
	
	function retrieveUser(path, dataElement) {
		var index = path.indexOf('?');
		if (index > -1) {
			path += "&";
		} else {
			path += "?";
		}
		
		var params = 'dataElement=' + dataElement;
		callAjaxRequest(path + params, onSuccess, onFailure, onCreate);
	}
	
	var onSuccess = function(data, args) {	
		//removes any code that might exist from header jsp includes
		var jsonStr = strAfter(ajaxJSONStartString, data);				
		info = dojo.fromJson(jsonStr);
		
		userId = info.user[0].userid;
		userHint = info.user[1].password_hint;
		userEmail = info.user[2].email;
		
		dojo.byId('hint').innerHTML = userHint;
	}
	
	var onFailure = function(error, args) {
	        if (sessionLocale == 'fr_CA') {
			dojo.byId('hint').innerHTML = 'Incapable de rapporter l\'allusion ...';
		} else {
			dojo.byId('hint').innerHTML = 'Unable to retrieve hint ...';
		}
	}

	var onCreate = function() {
		info = null;
		if (sessionLocale == 'fr_CA') {
			dojo.byId('hint').innerHTML = 'Rapporter ...';
		} else {
			dojo.byId('hint').innerHTML = 'Retrieving ...';
		}
	}
	
	function emailPassword(path, dataElement) {
		var index = path.indexOf('?');
		if (index > -1) {
			path += "&";
		} else {
			path += "?";
		}
				
		var params = 'dataElement=' + dataElement;
		callAjaxRequest(path + params, onEmailSuccess, onEmailFailure, onEmailCreate);
	}
	
	var onEmailSuccess = function(data, args) {		
		//removes any code that might exist from header jsp includes		
		var jsonStr = strAfter(ajaxJSONStartString, data);				
		info = dojo.fromJson(jsonStr);
		
		sendEmail = info.success[0].sendEmail;
		if (sendEmail == 'SENT') {
		        if (sessionLocale == 'fr_CA') {
				dojo.byId('emailSent').innerHTML = 'Votre nouveau mot de passe a été envoyé';
			} else {
			  	dojo.byId('emailSent').innerHTML = 'Your new password has been sent';
			}
		} else {
		        if (sendEmail == 'CROSS_SITE_LOGIN') {
		                dojo.byId('emailSent').innerHTML = info.success[1].message;
		        } else {
		                if (sessionLocale == 'fr_CA') {
		        	        dojo.byId('emailSent').innerHTML = 'L\'E-mail d\'envoi A Echoué. S\'il vous plaît contacter le Service clients à 1-888-664-6636.';
		                } else if (sessionLocale == 'en_CA') {
			                dojo.byId('emailSent').innerHTML = 'Sending Email Failed. Please contact Customer Service at 1-888-664-6636.';
						} else {
			                dojo.byId('emailSent').innerHTML = 'Sending Email Failed. Please contact Customer Service at 1-866-411-1501.';
			        }
			}
		}	
	}
	
	var onEmailFailure = function(error, args) {
	        if (sessionLocale == 'fr_CA') {
			dojo.byId('emailSent').innerHTML = 'L\'E-mail d\'envoi A Echoué. S\'il vous plaît contacter le Service clients à 1-888-664-6636.';
		} else if (sessionLocale == 'en_CA') {
			dojo.byId('emailSent').innerHTML = 'Sending Email Failed. Please contact Customer Service at 1-888-664-6636.';
		} else {
			dojo.byId('emailSent').innerHTML = 'Sending Email Failed. Please contact Customer Service at 1-866-411-1501.';
		}
	}

	var onEmailCreate = function() {
		info = null;
		if (sessionLocale == 'fr_CA') {
			dojo.byId('emailSent').innerHTML = 'E-mail d\'envoi ...';
		} else {
			dojo.byId('emailSent').innerHTML = 'Sending Email ...';	
		}
	}
	
	/***********  Get Address Info for Edit Billing/Shipping      ****************************************************************/
	function retrieveAddress(path, dataElement) {		
		var params = '?dataElement=' + dataElement;
		
		callAjaxRequest(path+params, onAddressSuccess, onAddressFailure, onAddressCreate);
	}

	function retrieveAddressCA(path, dataElement) {		
		var params = '&dataElement=' + dataElement;
		
		callAjaxRequest(path+params, onAddressSuccessCA, onAddressFailure, onAddressCreate);
	}

	
	var onAddressSuccess = function(data, args) {	
		//removes any code that might exist from header jsp includes
		var jsonStr = strAfter(ajaxJSONStartString, data);				
		info = dojo.fromJson(jsonStr);
		
		var type = info.address[0].type;

		if (type == "SHP") {
			dojo.byId("first_name_shipping").value = info.address[1].firstName;
			dojo.byId("last_name_shipping").value = info.address[2].lastName;
			dojo.byId("address_1_shipping").value = info.address[3].address1;
			dojo.byId("address_2_shipping").value = info.address[4].address2;
			dojo.byId("city_shipping").value = info.address[5].city;
			dojo.byId("state_shipping").value = info.address[6].state;
			dojo.byId("zip_shipping").value = info.address[7].postal;
			dojo.byId("telephone_shipping").value = info.address[8].phone;
			dojo.byId("military_shipping").value = info.address[10].apo;			
		} else {
			dojo.byId("first_name").value = info.address[1].firstName;
			dojo.byId("last_name").value = info.address[2].lastName;
			dojo.byId("address_1").value = info.address[3].address1;
			dojo.byId("address_2").value = info.address[4].address2;
			dojo.byId("city").value = info.address[5].city;
			dojo.byId("state").value = info.address[6].state;
			dojo.byId("zip").value = info.address[7].postal;
			dojo.byId("telephone").value = info.address[8].phone;
			dojo.byId("military").value = info.address[10].apo;
		}
	}

	var onAddressSuccessCA = function(data, args) {	
		//removes any code that might exist from header jsp includes
		var jsonStr = strAfter(ajaxJSONStartString, data);				
		info = dojo.fromJson(jsonStr);
		
		var type = info.address[0].type;

		if (type == "SHP") {
			dojo.byId("first_name_shipping").value = info.address[1].firstName;
			dojo.byId("last_name_shipping").value = info.address[2].lastName;
			dojo.byId("address_1_shipping").value = info.address[3].address1;
			dojo.byId("address_2_shipping").value = info.address[4].address2;
			dojo.byId("city_shipping").value = info.address[5].city;
			dojo.byId("state_shipping").value = info.address[6].state;
			dojo.byId("zip_shipping").value = info.address[7].postal;
			dojo.byId("telephone_shipping").value = info.address[8].phone;
			//dojo.byId("military_shipping").value = info.address[10].apo;			
		} else {
			dojo.byId("first_name").value = info.address[1].firstName;
			dojo.byId("last_name").value = info.address[2].lastName;
			dojo.byId("address_1").value = info.address[3].address1;
			dojo.byId("address_2").value = info.address[4].address2;
			dojo.byId("city").value = info.address[5].city;
			dojo.byId("state").value = info.address[6].state;
			dojo.byId("zip").value = info.address[7].postal;
			dojo.byId("telephone").value = info.address[8].phone;
			//dojo.byId("military").value = info.address[10].apo;
		}
	}
	
	var onAddressFailure = function(error, args) {
	        if (sessionLocale == 'fr_CA') {
			alert("N\'adresse pas trouvé. S\'il vous plaît choisit un autre ou bénéfices avec précédent adresse.");
		} else {
			alert("Address not found. Please select another or proceed with previous address.");
		}
	}

	var onAddressCreate = function() {
		info = null;
	}
	
	
	/***********So these may be more utilitarian than  ajax specific functions...but they mostly get used with ajax so...yeah****************/
	//returns every after needle in the haystack
	//returns null on error
	function strAfter(needle, haystack)
	{
		try{
			var index = haystack.indexOf(needle);
			haystack = haystack.substr(index+needle.length, haystack.length);
			return haystack;
		}catch(err){
			return null;
		}
	}
	
	
	/***********end util functions****************/
