﻿/**
 * Receive data and put it in the given DOM node
 * 
 * @param string data
 * @param DOM node
 */
function receive_ajax(data, node) {
	if(node != null){
		node.innerHTML = data; 
	
		var All = node.getElementsByTagName("*");
		for (var i=0; i<All.length; i++) {
			
			// avoid setting id and name when the attribute doesn't exist
			if(All[i].getAttribute("id") != '') {
				All[i].id = All[i].getAttribute("id");
			}
			if(All[i].getAttribute("name") != '') {
				All[i].name = All[i].getAttribute("name");
			}
		}
		
		var AllScripts = node.getElementsByTagName("script");
		for (var i=0; i<AllScripts.length; i++) {
			var s = AllScripts[i];
			
			if (s.src && s.src!="") {
				// Precedement asynchrone, mis en synchrone pour eviter des problemes de dependances de scripts
				AjaxGetData(s.src, eval) ;
			} else {
				// In some cases the cdata tags are in the js data, to ensure
				// there are no js errors we ensure these tags are removed wherever
				// they are found in the code. (the reason for their presence is 
				// not known, but should be found).
				eval(s.innerHTML.replace(/(<!\[CDATA\[)|(\]\]>)/g, ''));
				
			}
		}
	}

	hideTimer();
}

/**
 * Receive data and put it in the given DOM node
 * Postal codes version
 * 
 * @param string data
 * @param DOM node
 */
function receive_ajax_postal_codes(data, node) {
	if(node != null){
		node.innerHTML = data; 
	
		var All = node.getElementsByTagName("*");
		for (var i=0; i<All.length; i++) {
			
			// avoid setting id and name when the attribute doesn't exist
			if(All[i].getAttribute("id") != '') {
				All[i].id = All[i].getAttribute("id");
			}
			if(All[i].getAttribute("name") != '') {
				All[i].name = All[i].getAttribute("name");
			}
		}
		
		var AllScripts = node.getElementsByTagName("script");
		for (var i=0; i<AllScripts.length; i++) {
			var s = AllScripts[i];
			
			if (s.src && s.src!="") {
				// Precedement asynchrone, mis en synchrone pour eviter des problemes de dependances de scripts
				AjaxGetData(s.src, eval) ;
			} else {
				// In some cases the cdata tags are in the js data, to ensure
				// there are no js errors we ensure these tags are removed wherever
				// they are found in the code. (the reason for their presence is 
				// not known, but should be found).
				eval(s.innerHTML.replace(/(<!\[CDATA\[)|(\]\]>)/g, ''));
				
			}
		}
	}
	
	//Formats the result
	setRetailerFormat();

	hideTimer();
}

/**
 * Send an AJAX query using GET method
 * but abort any previous connection before
 * starting the new one.
 * 
 * @param string url
 * @param {Object} callback
 * @param DOM node 
 */
function AjaxGetDataAbortOld(url, callback, node) {
	if (typeof xhr != 'undefined' && xhr.readyState != 0)
		xhr.abort();
	
	AjaxGetData(url, callback, node);
}

/**
 * Send an AJAX query using GET method
 * 
 * @param string url
 * @param {Object} callback
 * @param DOM node 
 */
function AjaxGetData(url, callback, node) {
	xhr = false;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	  
	if (xhr) {
		try {
			showTimer();
			xhr.open("GET", url);

			xhr.onreadystatechange = function() {
				if (xhr.readyState == 4 && xhr.status == 200) {
					var data = xhr.responseText;
					
					if (null != callback) {

						if (null != node) {
							
							callback(data, node);
						} else {
							
							callback(data);
						}
					}
				}
			}
		
			xhr.send(null);
		} catch (e) {
			//TODO remove the alert() after the JS stuff has been correctly tested.
			alert('Erreur '+e);
		}
	}
	else {
		//TODO remove the alert() after the JS stuff has been correctly tested.
		alert('AJAX not available');
		window.location = url;
	}
}

/**
 * Send an AJAX query using GET method without a callback
 * 
 * @param string url
 * @param DOM node 
 */
function AjaxSendGetData(url) {
	 AjaxGetData(url, null, null);
}

/**
 * Send an AJAX query using POST method
 * 
 * @param string url
 * @param string parameters
 * @param {Object} callback
 * @param DOM node 
 */
function AjaxPostData(url, parameters, callback, node) {
	var xhr = false;
	
	if(window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else if(window.ActiveXObject) {
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if(xhr) {
		try {
			showTimer();
			xhr.open("POST", url, true);
			xhr.onreadystatechange = function() {
				if(xhr.readyState == 4 && xhr.status == 200) {
					var data = xhr.responseText;
					callback(data, node);
					hideTimer(url);
				}
			}
			xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xhr.setRequestHeader("Content-length", parameters.length);
			xhr.setRequestHeader("Connection", "close");	
			xhr.send(parameters);
		} catch (e) {}
	}
	else {
		window.location = url;
	}
}

function showTimer() {

	var id = 'loading-image';
	
	document.body.style.cursor = 'wait';
	document.getElementById(id).style.display = 'block';
}

function hideTimer() {

	var id = 'loading-image';
	
	document.body.style.cursor = '';
	document.getElementById(id).style.display = 'none';
}
