var baseurl = '/';
var isIE = 0;
if(navigator.appName.indexOf('Microsoft') !=-1 ) {
	isIE = 1;
}

function expandbox (elemid) {
	if (getdom(elemid)) {
		var k = getdom(elemid);
		if (k.style.display == 'none' || k.style.display == '') {
			k.style.display = 'block';
		} else {
			k.style.display = 'none';
		}
	}
}
function getdom(objid) {
	return (document.getElementById(objid));
}

function removeAllChildren(tobj) {
	if(tobj.childNodes.length ) {
		while(tobj.childNodes.length) {
			tobj.removeChild(tobj.firstChild);
		}
	}
}

function addfields(frmName) {
	var o = document.forms[frmName].elements;
	for(i=0;i<o.length;i++){
		o[i].oblig=false;
	}
}

function addoblig(frmname, elems) {
	var k=document.forms[frmname].elements;
	for(i=0;i<elems.length;i++) {
		k[elems[i]].oblig=true;
	}
}

function xsubmit(frmName) {
	var o = document.forms[frmName].elements;
	var t=0;
	for(i=0;i<o.length;i++){
		if(o[i].oblig==true && (o[i].type=='checkbox' || o[i].type=='radio') ) {
			if(!o[i].checked) {
				t++;
			}
		} else if(o[i].oblig==true && ( ( (o[i].type=='text' || o[i].type=='password' || o[i].type=='file' || o[i].type=='textarea' || o[i].type=='file' || o[i].type=='hidden') && o[i].value.length==0) || (o[i].type=='select-one' && o[i].selectedIndex<=0 ) ) ) {
			t++;
			o[i].style.backgroundColor = '#ffcccc';
		} else if (o[i].type != 'submit' && o[i].type != 'button') {
		}
	}
	if(t!=0) {
		alert('Veuillez remplir les champs indiques!');
		return false;
	}
	return true;
}

function xcheck(frmName) {
	var o = document.forms[frmName].elements;
	var t=0;
	for(i=0;i<o.length;i++){
		if(o[i].oblig==true && (o[i].type=='checkbox' || o[i].type=='radio') ) {
			if(! o[i].checked) {
				t++;
			}
		}
		if(o[i].oblig==true && ( ( (o[i].type=='text' || o[i].type=='password' || o[i].type=='textarea' || o[i].type=='file' ) && o[i].value.length==0) || (o[i].type=='select-one' && o[i].selectedIndex<=0) || (o[i].type=='select-multiple' && o[i].selectedIndex<=0) ) ) {
			t++;
		}
	}
	if(t!=0) {
		return false;
	}
	return true;
}





function remoting() {
	this.aRequests = new Array();
	this.aRequests[0] = null;
	this.targets = new Array();
	
	this.request = function(url, params, reqmethod, targetobj) {
		method = reqmethod;
		requestxml= params;
		var req = null;
		var openIndex = this.aRequests.length;
		this.targets[openIndex]=targetobj;
		
		if (window.XMLHttpRequest) {
			var self = this;
			req = new XMLHttpRequest();
			req.onreadystatechange = function() {
				self.handle(openIndex);
			};
			this.aRequests[openIndex] = req;
			req.open(method, url, true);
			req.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
			req.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
			if(method == 'POST') {
				req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
			}
			req.send(requestxml);
		} else if (window.ActiveXObject) {
            req = new ActiveXObject("Microsoft.XMLHTTP");
			if (req) {
				var self = this;
				req.onreadystatechange = function() {
					self.handle(openIndex);
				};
				this.aRequests[openIndex] = req;
				req.open(method, url, true);
				req.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
				req.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
				if(method == 'POST') {
					req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
				}
				req.send(requestxml);
			} else {
				return false;
            }
		} else {
			return false;
		}
		return true;
	}
      
      
	this.handle = function(arrindex) {
		var found=false;
		if (this.aRequests[arrindex] != null && this.aRequests[arrindex].readyState == 4) {
			if (this.aRequests[arrindex].status == 200) {
            //k = this.targets[arrindex];
				if(this.targets[arrindex]!=null) {
					var _target = getdom(this.targets[arrindex]);
					if(!_target) {alert('target not found' + this.targets[arrindex]);return;};
				} else {
					return;
				}
            
				var resp = this.aRequests[arrindex].responseXML.documentElement;
				removeAllChildren(_target);
				
				switch(resp.nodeName) {
					case 'optionvalues' :
						if (resp.childNodes.length > 0) {
							for(i = 0; i < resp.childNodes.length; ++i) {
								_node = resp.childNodes[i];
								_zopt = document.createElement('option');
								_target.appendChild(_zopt);
								_zopt.value = _node.getAttribute('tid');
								_zopt.text = _node.getAttribute('tval');
							}
						}
					break;
					
					case 'categories' :
						if (resp.childNodes.length > 0) {
							for(i = 0; i < resp.childNodes.length; ++i) {
								_node = resp.childNodes[i];
								_zopt = document.createElement('option');
								_target.appendChild(_zopt);
								_zopt.value = _node.getAttribute('tid');
								_zopt.text = _node.getAttribute('tval');
							}
						}
					break;
					
					default :
					break;
				}
				
				this.aRequests[arrindex]=null;
				this.targets[arrindex]=null;
			}
		}
	} // end handler
	
}
//end class remoting
 
var rconn = new remoting(); // global scope

function getxdata(_url, _method, _targetObj) {
	var qstr = baseurl+'pipe/' + _url;
	rconn.request(qstr,'','GET', _targetObj);
}

function gMfr(_tsel, _target) {
	if (_tsel.options[_tsel.selectedIndex].value != 0) {
		getxdata('getmfrs/qcat/' + _tsel.options[_tsel.selectedIndex].value, 'GET', _target);
	} else {
		removeAllChildren(getdom(_target));
		_selector = getdom(_target);
		_zopt = document.createElement('option');
		_selector.appendChild(_zopt);
		_zopt.value = '0';
		_zopt.text = 'selectionner';
	}
}
function gCat(_tsel, _target) {
	if (_tsel.options[_tsel.selectedIndex].value != 0) {
		getxdata('getcs/qcat/' + _tsel.options[_tsel.selectedIndex].value, 'GET', _target);
	} else {
		removeAllChildren(getdom(_target));
		_selector = getdom(_target);
		_zopt = document.createElement('option');
		_selector.appendChild(_zopt);
		_zopt.value = '0';
		_zopt.text = 'selectionner';
	}
}
