﻿/**
 * 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();
}

function receive_ajax_order(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();
	
	if(data == 'ok'){
		var url = document.location.href.split('Order-Form');
		
		if(url.length == 2){
			document.location = 'Thank-you';
		}else{
			document.location = 'Creative-Quick-Order/Thank-you';
		}
	}
}

function receive_ajax_countries(data, node) {
	if(node != null){
		node.innerHTML += data; 
	}
	
	if(null != selected_country){
		var options = document.getElementById('country_id').options;
		
		for(var i = 0 ; i < options.length ; i++){
			if(options[i].value == selected_country){
				options[i].selected = true;
			}
		}
		populateProvinces(selected_country);
	}

	hideTimer();
}

/**
 * Receive data and put it in the given DOM node
 * 
 * @param string data
 * @param DOM node
 */
function receive_ajax_send(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, ''));
				
			}
		}
	}
	document.getElementById('name').value ='';
	document.getElementById('company').value ='';
	document.getElementById('email').value ='';
	document.getElementById('content').value ='';
	document.getElementById('countdown').innerHTML ='500';
	
	document.getElementById('sentMessage').style.display = 'block';

	hideTimer();
}

/**
 * Receive data and put it in the given DOM node
 * 
 * @param string data
 * @param DOM node
 */
function receive_ajax_user_account(data, node) {
	receive_ajax(data, node);
	
	// Populates the fields
	document.getElementById('first_name').value = document.getElementById('ajax_first_name').value;
	document.getElementById('last_name').value = document.getElementById('ajax_last_name').value;
	document.getElementById('company').value = document.getElementById('ajax_company').value;
	document.getElementById('street').value = document.getElementById('ajax_street').value;
	document.getElementById('zip').value = document.getElementById('ajax_zip').value;
	document.getElementById('city').value = document.getElementById('ajax_city').value;
	
	var countries = document.getElementById('country').options;
	var country = document.getElementById('ajax_country').value;
	
	for(var i = 0 ; i < countries.length ; i++){
		if(countries[i].value == country){
			countries[i].selected = 'selected';
		}
	}
	
	selectStates();
	
	if(country == 'canada' || country == 'Canada'){
		var states = document.getElementById('state_ca').options;
	}else if(country == 'united states' || country == 'United States of America' || country == 'united states of america'){
		var states = document.getElementById('state_us').options;
	}else{
		var states = Array();
	}
	
	for(var i = 0 ; i < states.length ; i++){
		if(states[i].value == document.getElementById('ajax_state').value){
			states[i].selected = 'selected';
		}
	}
}

/**
 * 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';
	
	if (null != document.getElementById(id)) {
		document.body.style.cursor = 'wait';
		document.getElementById(id).style.display = 'block';
	}
}

function hideTimer() {
	var id = 'loading-image';
	
	if (null != document.getElementById(id)) {
		document.body.style.cursor = '';
		document.getElementById(id).style.display = 'none';
	}
}

