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 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;
					
					case 'czrows':
						addOptions(resp, _target);
					break;
					
					case 'zcrows':
						addOptions(resp, _target);
					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';
}


_pWidth = 500;
_pHeight = 500;

function liveSupport(_href) {
	var _left = parseInt((document.body.clientWidth - _pWidth) / 2);
	var _top = parseInt((document.body.clientHeight - _pHeight) / 2);
	_win = window.open(_href, 'gallery', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=' + _pWidth + ',height=' + _pHeight + ',screenX=' + _left + ',screenY=' + _top);
	_win.focus();

}
function addOptions(_resp, _target) {
	if (!_target) {
		return false;
	}
	
	var _fopt = document.createElement('option');
	_fopt.value = '0';
	_fopt.label = 'Please select';
	_fopt.text = 'Please select';
	_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 changeList(_tparam, _tnode) {
	k = getdom('frmfilters');
	k[_tparam].value = _tnode.options[_tnode.selectedIndex].value;
	k.submit();
}

function setSearchPrice(_from, _to) {
	k = document.forms['frmfilters'];
	k.elements['qpfrom'].value = _from;
	k.elements['qpto'].value = _to;
	k.submit();
}
function setQQ(_tnode) {
	k = document.forms['frmfilters'];
	k.elements['qq'].value = _tnode.checked + 0;
	k.submit();
}
function vsrc() {
	document.forms['frmfilters'].submit();
}

function changeSA() {
	var k = document.forms['frmnewuser'].elements;
	var fields = new Array('saddress_firstname', 'saddress_lastname', 'saddress_company', 'saddress_street_address', 'saddress_street_address_c', 'saddress_city', 'saddress_state', 'saddress_country', 'saddress_county', 'saddress_postcode', 'saddress_phone');
	var setTo = null;
	if(k['user_saddress'].checked) {
		setTo = true;
	} else {
		setTo = false;
	}
	for(var x = 0; x < fields.length; ++x) {
		k[fields[x]].disabled = setTo;
	}
}
function changeStateList(_from, _to) {
	k = getdom(_from);
	if(k.value != '' && parseInt(k.value) != 0) {
		getxdata('getzones/cid/' + parseInt(k.value), 'get', _to);
	}
}
function changeCountiesList(_from, _to) {
	k = getdom(_from);
	if(k.value != '' && parseInt(k.value) != 0) {
		getxdata('getcounties/cid/' + parseInt(k.value), 'get', _to);
	}
}
