
function MyAjax(){
	
	var option = null;
	var param = null;
	var item = new Array();
	var xhr = null;
	
	this.load = function() {
		
		// Loading xhr method
		if (window.XMLHttpRequest) { 
			xhr = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		} else 
			return;
		
		// We call ajax.php file who permits to load xml
		file = "index.php?action=MyAjax"+this.option;
		
		// opening file
		xhr.open("GET", file, true);
		
		// We defined call to server 
		xhr.onreadystatechange = function () {
			// if CpAjax is defined
			// CpAjax is for Company Ajax : treatments for the rubric list
			if(typeof(CpAjax) != 'undefined')
				CpAjax.treatment(xhr, param);
				
			// if RbAjax is defined	
			// RbAjax is for Country Ajax : treatments for the select field in moreoption fieldset
			if(typeof(RbAjax) != 'undefined')
				RbAjax.treatment(xhr, param);
		}
		
		// Closing xhr connection
		xhr.send(null);
		
		// fill empty items table
		this.items = new Array();
		
    };
    
// treatment function
    this.treatment = function (xhr, param){
    	// if xhr is ready
		if (xhr.readyState == 4) {
			// if response from http server is ok
			if(xhr.status == 200){
				
				// Content of the XML document
				var docXML= xhr.responseXML;
				
				// choose function with param var
				switch(this.param) {
					// show department list with country code
					case "country" :
						this.country(docXML);
						break;
					// show rubric tree
					case "rubric" :
						this.rubric(docXML);
						break;
					
					default :
						break;
				} // end switch
				
			} // end xhr.status test
			
		} // end xhr.readyState test
	};
	
/**

*/

/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
/* % this.country = function (docXML)                                 % */
/* % this function compiles department list to show it behold         % */
/* %                                                                  % */
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */	

	this.country = function (docXML) {
		
		// List of the donnee tag in the XML document
		items = docXML.getElementsByTagName("dep");
		nbDep = items.length;
		
		// if the country gots departments
		if(nbDep > 0 ) {
			new Effect.Appear($('listDepartment'));
			$('listDepartment').style.display  = 'block';
			// empty select field tagged department
			//  this field is in search_field tpl
			$('xmlDepartment').options.length = 0;
			
			// 
			for (i=0; i<items.length; i++) {
				
				// collection of attributes 
				var ville        = items.item(i).attributes;
				idVille          = ville[0].nodeValue;
				nameVille        = ville[1].nodeValue;
				
				// fill select input 
				var dep = document.createElement('option');
				dep.value = idVille;
				dep.innerHTML = nameVille;
				$('xmlDepartment').appendChild(dep);
				
			}
		} else{
			// if there are no result, we fill empty our department select input
			$('xmlDepartment').options.length = 0;
			$('listDepartment').style.display  = 'block';
		}
		
	};

	// while we got rubric in our xml node we do...
	this.doRubric = function (node) {
	
		var b = new MyBrowser;
		var rubricFather = 0;
		// getting id attribute
		if(node.getAttribute('id_rubric') == 0)
		{
			rubricFather = node.getAttribute('id_profile');
		}
		else
		{
			rubricFather = node.getAttribute('id_rubric');
		}
		
		// getting first child of the xml node
		var node     = node.firstChild;
		var i = 0;
		// while our xml tree is not empty
		while (node != null) {
			
			// getting rubric tags
			if (node.nodeName == 'rubric') {
				// creating a new blockquote element in the dom
				var newBlockQ  = document.createElement('div');
				// with this new BQ we do ...
				with ( newBlockQ ) {
					// Giving it an id
					(node.getAttribute('id_rubric') == 0)?
						id_used = node.getAttribute('id_profile')
						: id_used = node.getAttribute('id_rubric') ;
					id = 'rub_'+id_used;
					
				}
				
				// If it gots child (rubric or companies)
					(node.getAttribute('open') == 1)?
						newBlockQ.className = 'clicked'
						: newBlockQ.className = 'unclicked' ; // if it gots childs
					
				// creating a new link attach to the current BQ
				var a            = document.createElement('a');
				
				// Add attribute onclick
				var onclick           = document.createAttribute('onclick');
				// giving it a value to its onclick property
				onclick.nodeValue     = 'son('+node.getAttribute('id_rubric')
					+', '+node.getAttribute('id_profile')+'); return false;';
				
				// with this new link we do ...
				with ( a ) {
					// giving it an id
					id         = 'a_'+newBlockQ.id;
					// giving it a name
					className       = 'rubricLink';
					// initiate its href property
					href       = './#';
					// initiate its content
					innerHTML  = node.getAttribute('lib');
					// attaching onclick property	
				}
				
				// this method resolve an ie bug.
				// b.ie come from MyBrowser class
				// ie return true if we are on version of Internet Explorer (5.5 to 7)
				if(!b.ie)
					a.setAttributeNode(onclick) // this method is for all browsers, but IE
				else {
					// that functions are for IE (and only for it)
					a.nodeId = node.getAttribute('id_profile');
					a.nodeIdR = node.getAttribute('id_rubric');
					a.onclick = function () { son(this.nodeIdR , this.nodeId); return false ; };
				}
				
				// attaching current link to current BQ
				newBlockQ.appendChild(a);
				
				// If the current rubric are the roots ofthe tree
				//alert('rub_'+rubricFather);
				( rubricFather == 0 )?
					$('liste').appendChild(newBlockQ)
					: $('rub_'+rubricFather).appendChild(newBlockQ) ;				
				
				// reload the procedure
				this.doRubric(node);
				
			// if current xml node is a company
			} else if(node.nodeName == 'company') {
				
				// creating new BQ element
				var newBlockQ = document.createElement('div');
				
				// Creating new a element
				var newLink   = document.createElement('a');
				
				
				
				// attaching id node to current BQ
				with ( newBlockQ ) {
					id = 'rub_'+node.getAttribute('id_rubric');
					
				}
				
				// init current link
				with ( newLink ) {
					id = 'a_'+node.getAttribute('id_rubric');
					className = 'company';
					innerHTML = node.getAttribute('lib');
					href = 'index.php?action=company&company='+node.getAttribute('id');
				}
				
				switch (node.getAttribute('bold')) {
					
					case '0':
						newLink.className = 'company';
						break;
						
					case '1':
						newLink.className = 'boldCompany';
						break;
						
					case '2':
						newLink.className = 'topCompany';
						break;
						
					default :
						break;
						
				}
				
				
				
				newBlockQ.appendChild(newLink);
				
				$('rub_'+rubricFather).appendChild(newBlockQ);
				
			}
			
			
			node = node.nextSibling;
		}
		
	};
	
	
// this function constructs the rubric tree from xml datas 
	this.rubric = function (docXML) {
		
		this.doRubric(docXML.getElementsByTagName('root')[0]);
		
	};
	
};

function son(id_rubric, id_profile)
{
	(id_rubric == 0)?
		id = id_profile
		: id = id_rubric ;
		
	if($('rub_'+id).className == 'unclicked'){
		
		$('rub_'+id).className = "clicked";
		
		RbAjax.option = "&method=rubric&id_rubric="+id_rubric+"&id_profile="+id_profile;
		
		if($('rub_'+id).childNodes.length <= 1)
			RbAjax.load();
		
	}else{
		$('rub_'+id).className = "unclicked";
	}
	
};





