/**
* merchant-offers.js
* Handles all JS actions relating to the merchant offers landing page including the
* submit button states and XML parsing for the country drop down menu
*
* @author Damian Galarza (dgalarza@mcdpartners.com)
*/

(function () {

	var http = mcd.http;
	
	var request;
	var queryString;
	var langFilter;
	var languageObj = {};
	
	var init = function () {		
		// queryString = http.readQueryString(window.location.search.substr(1));
		
		// By defualt we will be loading the english language
		langFilter = 'english';
		fetchXML();
		
		// Add our change handler on the country drop down to enable/disable go button
		var countrySelect = document.getElementById('select-country');

		// Reset the disabled status on the country drop down since we have javascript		
		countrySelect.disabled = '';
		
		mcd.event.add(countrySelect, 'change', function (e) {
			
			var target = mcd.event.getTarget(e);						
			var btn = document.getElementById('country-submit');
			if(btn){
				mcd.event.preventDefault(e);
				if(target.value !== '') {
					if(mcd.dom.hasClass(btn, 'privileges')) {
						btn.disabled = '';
						btn.src = '/images/buttons/btn-view-privileges.png';
					}
					else {
						btn.disabled = '';
						btn.src = '/images/buttons/btn-go.png';
					}
				}
				else {
					if(mcd.dom.hasClass(btn, 'privileges')) {
						btn.disabled = 'disabled';
						btn.src = '/images/buttons/btn-view-privileges-disabled.png';
					}
					else {			
						btn.disabled = 'disabled';
						btn.src = '/images/buttons/btn-go-disabled.png';
					}
				}
			}
			else {
			// else it is the promotions.html page, and each onchange alters the url	
				
				if(mcd.privileges){
					if(this.value == '') {
						return false;
					}
					mcd.privileges.util.setParam('country', this.value);
				}
			}
		});
	}
	
	/**
	* Set up our AJAX Request for the countries xml listing so
	* we can populate our select box
	*/
	var fetchXML = function () {
		request = http.request({
			'method' : 'GET',
			'uri' : '/merchant-offers/assets/xml/languages-countries.xml',
			'onreadystatechange' : ajaxResponse
		});
		
		request.send('');
	}
	
	/*
	* Handle the readystatechange for our request object
	*/
	var ajaxResponse = function () {
		if(request.readyState === 4) {
			if(request.status === 200) {
				parseCountryXML(request.responseXML);
			}
		}
	}
	
	/**
	* Take the country listing XML file and parse it
	*/
	var parseCountryXML = function (response) {				
		var languageNodes = response.getElementsByTagName('language');
		var languages = {};	
		
		// Find our language node
		var _thisNode, langNode;						
		for(var i=0; i<languageNodes.length; i++) {
			_thisNode = languageNodes[i];
			
			// Check if this is the language we're looking for
			if(_thisNode.getElementsByTagName('urlName')[0].childNodes[0].nodeValue == langFilter) {
				langNode = languageNodes[i];
				break;
			}
		}		
		// Once we've found our filter, parse the info into a JSON obj
		languageObj.urlName = langNode.getElementsByTagName('urlName')[0].childNodes[0].nodeValue;
		languageObj.realName = langNode.getElementsByTagName('realName')[0].childNodes[0].nodeValue;
		
		// Parse language categories
		var categories = langNode.getElementsByTagName('categories')[0].getElementsByTagName('category');		
		var _thisCat, tmpObj;
		var catArray = new Array();
		
		for(var i=0; i<categories.length; i++) {
			_thisCat = categories[i];
						
			tmpObj = {
				id: _thisCat.getAttribute('id'),
				name: _thisCat.childNodes[0].nodeValue
			};
			
			catArray.push(tmpObj);			
									
		}	
		
		// Push the category array to the language object
		languageObj.categories = catArray;
		
		// Loop through countries for this language
		var countries = langNode.getElementsByTagName('country');
		var countryArray = new Array();
		var _thisCountry;
		
		for(var i=0; i<countries.length; i++) {
			_thisCountry = countries[i];
			tmpObj = {
				urlName: _thisCountry.getElementsByTagName('urlName')[0].childNodes[0].nodeValue,
				realName: _thisCountry.getElementsByTagName('realName')[0].childNodes[0].nodeValue,
				location: _thisCountry.getElementsByTagName('location')[0].childNodes[0].nodeValue
			};
			
			countryArray.push(tmpObj);
		}				
		
		// Sort the country array based on urlName
		countryArray.sort(function(a, b){
			return (a.urlName < b.urlName) ? -1 : 1;
		});
		
		// Push the country array to the language object
		languageObj.countries = countryArray;
		
		// Default select option
		languageObj.countryDefault = langNode.getElementsByTagName('countryDefault')[0].childNodes[0].nodeValue;
		languageObj.cityDefault = langNode.getElementsByTagName('cityDefault')[0].childNodes[0].nodeValue;				
		
		publishDropDown();
	};
	
	/**
	* Publish the drop down menu after parsing the language properties
	*/
	var publishDropDown = function () {
		var selectMenu = document.getElementById('select-country');
		var frag = document.createDocumentFragment();		
		selectMenu.innerHTML = '';
		
		/*
		* Set the default select option then Loop through 
		* countries and push them to the select menu		 
		*/
		var _thisCountry, option;
		
		option = document.createElement('option');
		option.value = '';
		option.innerHTML = languageObj.countryDefault;
		
		frag.appendChild(option);
		
		mcd.util.each(languageObj.countries, function(_thisCountry) {
			option = document.createElement('option');			
			option.value = _thisCountry.urlName;
			option.innerHTML = _thisCountry.realName;
			
			frag.appendChild(option);					
		});
		
		selectMenu.appendChild(frag);		
		
	}	
	
	// Ready? Go!
	mcd.dom.ready(function () {
		init();	
	});
	
})();