+
+/*
+*********************************************************************************
+** AJAX HELPER CLASS ************************************************************
+*********************************************************************************
+*/
+var pzHttpRequest = function ( url, errorHandler ) {
+ this.request = null;
+ this.url = url;
+ this.errorHandler = errorHandler || null;
+ this.async = true;
+
+ if ( window.XMLHttpRequest ) {
+ this.request = new XMLHttpRequest();
+ } else if ( window.ActiveXObject ) {
+ try {
+ this.request = new ActiveXObject( 'Msxml2.XMLHTTP' );
+ } catch (err) {
+ this.request = new ActiveXObject( 'Microsoft.XMLHTTP' );
+ }
+ }
+};
+
+pzHttpRequest.prototype =
+{
+ get: function ( params, callback )
+ {
+ this._send( 'GET', params, '', callback );
+ },
+
+ post: function ( params, data, callback )
+ {
+ this._send( 'POST', params, data, callback );
+ },
+
+ load: function ()
+ {
+ this.async = false;
+ this.request.open( 'GET', this.url, this.async );
+ this.request.send('');
+ if ( this.request.status == 200 )
+ return this.request.responseXML;
+ },
+
+ _send: function ( type, params, data, callback )
+ {
+ this.callback = callback;
+ var context = this;
+ this.async = true;
+ this.request.open( type, this._urlAppendParams(params), this.async );
+ this.request.onreadystatechange = function () {
+ context._handleResponse();
+ }
+ this.request.send(data);
+ },
+
+ _urlAppendParams: function (params)
+ {
+ var getUrl = this.url;
+
+ var sep = '?';
+ var el = params;
+ for (var key in el) {
+ if (el[key] != null) {
+ getUrl += sep + key + '=' + encodeURI(el[key]);
+ sep = '&';
+ }
+ }
+ return getUrl;
+ },
+
+ _handleResponse: function ()
+ {
+ if ( this.request.readyState == 4 ) {
+ if ( this.request.status == 200 ) {
+ this.callback( this.request.responseXML );
+ }
+ // pz errors
+ else if ( this.request.status == 417 ) {
+ var errMsg = this.request.responseXML.getElementsByTagName("error")[0].childNodes[0].nodeValue;
+ var errCode = this.request.responseXML.getElementsByTagName("error")[0].getAttribute("code");
+
+ var err = new Error(errMsg);
+ err.code = errCode;
+
+ if (this.errorHandler) {
+ this.errorHandler(err);
+ }
+ else {
+ throw err;
+ }
+ }
+ else {
+ var err = new Error("XMLHttpRequest error. STATUS: "
+ + this.request.status + " STATUS TEXT: "
+ + this.request.statusText );
+ err.code = 'HTTP';
+
+ if (this.errorHandler) {
+ this.errorHandler(err);
+ }
+ else {
+ throw err;
+ }
+ }
+ }
+ }
+};
+
+/*
+*********************************************************************************
+** QUERY CLASS ******************************************************************
+*********************************************************************************
+*/
+var pzQuery = function()
+{
+ this.simpleQuery = '';
+ this.singleFilter = null;
+ this.advTerms = new Array();
+ this.filterHash = new Array();
+ this.numTerms = 0;
+ this.filterNums = 0;
+};
+pzQuery.prototype = {
+ reset: function()
+ {
+ this.simpleQuery = '';
+ this.advTerms = new Array();
+ this.simpleFilter = null;
+ this.numTerms = 0;
+ },
+ clearSimpleQuery: function()
+ {
+ this.simpleQuery = '';
+ },
+ addTerm: function(field, value)
+ {
+ var term = {"field": field, "value": value};
+ this.advTerms[this.numTerms] = term;
+ this.numTerms++;
+ },
+ getTermValueByIdx: function(index)
+ {
+ return this.advTerms[index].value;
+ },
+ getTermFieldByIdx: function(index)
+ {
+ return this.advTerms[index].field;
+ },
+ /* semicolon separated list of terms for given field*/
+ getTermsByField: function(field)
+ {
+ var terms = '';
+ for(var i = 0; i < this.advTerms.length; i++)
+ {
+ if( this.advTerms[i].field == field )
+ terms = terms + this.queryHas[i].value + ';';
+ }
+ return terms;
+ },
+ addTermsFromList: function(inputString, field)
+ {
+ var inputArr = inputString.split(';');
+ for(var i=0; i < inputArr.length; i++)
+ {
+ if(inputArr[i].length < 3) continue;
+ this.advTerms[this.numTerms] = {"field": field, "value": inputArr[i] };
+ this.numTerms++;
+ }
+ },
+ removeTermByIdx: function(index)
+ {
+ this.advTerms.splice(index, 1);
+ this.numTerms--;
+ },
+ toCCL: function()
+ {
+ var ccl = '';
+ if( this.simpleQuery != '')
+ ccl = this.simpleQuery;
+ for(var i = 0; i < this.advTerms.length; i++)
+ {
+ if (ccl != '') ccl = ccl + ' and ';
+ ccl = ccl + this.advTerms[i].field+'="'+this.advTerms[i].value+'"';
+ }
+ return ccl;
+ },
+ addFilter: function(name, value)
+ {
+ var filter = {"name": name, "id": value };
+ this.filterHash[this.filterHash.length] = filter;
+ this.filterNums++
+ return this.filterHash.length - 1;
+ },
+ setFilter: function(name, value)
+ {
+ this.filterHash = new Array();
+ this.filterNums = 0;
+ this.addFilter(name, value);
+ },
+ getFilter: function(index)
+ {
+ return this.filterHash[index].id;
+ },
+ getFilterName: function(index)
+ {
+ return this.filterHash[index].name;
+ },
+ removeFilter: function(index)
+ {
+ delete this.filterHash[index];
+ this.filterNums--;
+ },
+ clearFilter: function()
+ {
+ this.filterHash = new Array();
+ this.filterNums = 0;
+ },
+ getFilterString: function()
+ {
+ //temporary
+ if( this.singleFilter != null ) {
+ return 'pz:id='+this.singleFilter.id;
+ }
+ else if( this.filterNums <= 0 ) {
+ return undefined;
+ }
+
+ var filter = 'pz:id=';
+ for(var i = 0; i < this.filterHash.length; i++)
+ {
+ if (this.filterHash[i] == undefined) continue;
+ if (filter > 'pz:id=') filter = filter + '|';
+ filter += this.filterHash[i].id;
+ }
+ return filter;
+ },
+ totalLength: function()
+ {
+ var simpleLength = this.simpleQuery != '' ? 1 : 0;
+ return this.advTerms.length + simpleLength;
+ },
+ clearSingleFilter: function()
+ {
+ this.singleFilter = null;
+ },
+ setSingleFilter: function(name, value)
+ {
+ this.singleFilter = {"name": name, "id": value };
+ },
+ getSingleFilterName: function()
+ {
+ return this.singleFilter.name;
+ }
+}
+