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('Please fill in the required fields !');
		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 addOptions(_resp, _target) {
	if (!_target) {
		return false;
	}
	
	var _fopt = document.createElement('option');
	_fopt.value = '0';
	_fopt.label = 'Selectare';
	_fopt.text = 'Selectare';
	_target.appendChild(_fopt);
	
	for(i = 0; i < _resp.childNodes.length; ++i) {
		_xmlnode = _resp.childNodes[i];
		
		var _opt = document.createElement('option');
		_opt.value = _xmlnode.getAttribute('tid');
		_opt.label = _xmlnode.getAttribute('tval');
		_opt.text = _xmlnode.getAttribute('tval');
		_target.appendChild(_opt);
	}
	
}


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 'mfrmodels' :
						if (resp.childNodes.length > 0) {
							addOptions(resp, _target);
						}
					break;

					
					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 setLocation(_select) {
	document.location.href='/product/' + _select.options[_select.selectedIndex].value + '/' + _select.options[_select.selectedIndex].title;
}

function showtab(tval) {
	
	for(x=1; x < 4; x++ ) {
		getdom('tabbtn'+x).className = 'tab toff';
		getdom('tabcontent'+x).style.display = 'none';
	}
	getdom('tabbtn'+tval).className = 'tab ton';
	getdom('tabcontent'+tval).style.display = 'block';
}

/**
*/
function showModels() {
	var k = document.forms['frmsearch'].elements['qmake'];
	if (k.options[k.selectedIndex].value > 0) {
		getxdata('getmodels/mfr/' + k.options[k.selectedIndex].value, 'get', 'qmodel');
	} else {
		removeAllChildren(getdom('qmodel'));
	}
}

/**
*/
function showModelEquip(_tval) {
	getxdata('getmodelequip/model/' + _tval, 'get', 'productList');
}

/**
*/
function showAModels() {
	var k = document.forms['frmasearch'].elements['qb'];
	if (k.options[k.selectedIndex].value > 0) {
		getxdata('getmodels/mfr/' + k.options[k.selectedIndex].value, 'get', 'qamodel');
	} else {
		removeAllChildren(getdom('qamodel'));
	}
}

/**
*/
function changeList(_tparam, _tnode) {
	getdom('frmsort').submit();
}
