// File with functions to work with ajax

// Function to show the progress of ajax load
function initProgress() {
  Element.show('progressMsg');
}

// Function to hide the progress of ajax load
function resetProgress() {
  Element.hide('progressMsg');
}

// Function to send a request to server in url parameter
// Returns an XMLHttpRequest object
function ajaxRequest(url) {
   var text = '';
   var xml;
   if (document.all) { // IE version
       // older versions (IE4 and some IE5.0) might be using MSXML2.XMLHTTP.4.0
       xml = new ActiveXObject("Microsoft.XMLHTTP");
       xml.Open("GET", url, false);
       xml.Send()
   } else { // Mozilla/Netscrap 6+ version     
       xml = new XMLHttpRequest();
       xml.open("GET", url, false);
       xml.send(null);
   }
   return xml;
}

// Function to send a request to server in url parameter
// Returns a text string
function ajax(url) {
   var text = '';
   var xml;	
   xml=ajaxRequest(url);
   text = xml.responseText;	
   return text;
}


// Function to parser a text with format like TextResponse from AjaxTags:
// Sorts lines alphabetically
// Drops empty lines from the response
// Inserts an empty line at the beginning.
//<![CDATA[

CustomTextParser = Class.create();
CustomTextParser.prototype = Object.extend(new AbstractResponseParser(), {
  initialize: function() {
    this.type = "text";
  },

  load: function(request) {
    this.content = request.responseText;
    this.split();
  },

  split: function() {
    this.itemList = new Array();
    this.itemList.push(new Array(' ',' '));	
    var lines = this.content.split('\n');
    lines = lines.sort();
    for (var i=0; i<lines.length; i++) {
        if (lines[i] != '') {
      	    this.itemList.push(lines[i].split(','));
	}
    }
  }
});
//]]>


// Function to parser a specific case of list
//with format like TextResponse from AjaxTags:
// It works only with numeric lists where the values are 
//always equal to the labels.
// Drops empty lines from the response.
// Inserts an empty line at the beginning.
//<![CDATA[

function subtract(a, b) {
	return a - b;
}

CustomNumericTextParser = Class.create();
CustomNumericTextParser.prototype = Object.extend(new AbstractResponseParser(), {
  initialize: function() {
    this.type = "text";
  },

  load: function(request) {
    this.content = request.responseText;
    this.split();
  },

  split: function() {
    this.itemList = new Array();
    this.itemList.push(new Array(' ',' '));	
    var lines = this.content.split('\n');
    var linesAux= new Array ();

    for (var i=0; i<lines.length; i++) {
        if (lines[i] != '') {
	    tmp = lines[i].split(',');
            linesAux[i] = tmp[0];
	}
    }

    linesAux = linesAux.sort(subtract);

    for (var i=0; i<linesAux.length; i++) {
        if (linesAux[i] != '') {
      	    this.itemList.push(new Array (linesAux[i],linesAux[i]));
	}
    }
  }
});
//]]>


// Function to fill a select element
//<![CDATA[
function fillSelect(targ, items, selectedValue) {
 
 	var target = document.getElementById(targ);
    target.options.length = 0;
    target.disabled = false;
    for (var i=0; i<items.length; i++) {
      var newOption = new Option(items[i][0], items[i][1]);
      if (selectedValue == items[i][1]) {
        newOption.selected = true;
      }
      target.options[i] = newOption;
    }
}
//]]>

// Function that disable the select in case of error
function checkMapServerResponse(id, msg) {
	var select = document.getElementById(id);
	// Check if mapserver return an error
	if (select.options[0].text == 'error' || select.options[0].text == 'empty' ||  
			select.options[1].text == 'error' || select.options[1].text == 'empty') {
		select.length = 1;
		select.options[0].text = msg;
		select.options[0].value = '';
		select.disabled = false;
	}
}

// Fill the specific select with the content of response to the query
function ajaxFillSelect(query, select, value, msg) {
	var xml = ajaxRequest(query);
	var parser = new CustomTextParser();
	parser.load(xml);
	var response = parser.itemList;
	fillSelect(select, response, value);
	$(select).options[0] = new Option(msg, "");
	if (value == '') $(select).selectedIndex = 0;

	checkMapServerResponse(select, msg);
	
	resetProgress();
}

// Check if the query was successful and insert 
// the element 0 in the specific element
function postFill(element, msg) {

	resetProgress();
	checkMapServerResponse(element, msg);
	
	$(element).options[0]= new Option(msg, "");
	$(element).selectedIndex=0;
}


function back(){
	history.back(1);
}
