Factory = function(servletUrl, listenerUrl) {
  this.servletUrl = servletUrl;
  this.listenerUrl = listenerUrl;
  this.servletRequestBaseObj = null;
  this.listenerRequestBaseObj = null;
}

Factory.prototype.FactoryXMLHttpRequest = function() {
  if(window.XMLHttpRequest) {
    return new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {
    var msxmls = new Array(
    'Msxml2.XMLHTTP.5.0',
    'Msxml2.XMLHTTP.4.0',
    'Msxml2.XMLHTTP.3.0',
    'Msxml2.XMLHTTP',
    'Microsoft.XMLHTTP');
    for (var i = 0; i < msxmls.length; i++) {
      try {
        return new ActiveXObject(msxmls[i]);
      } catch (e) {
      }
    }
  }

  throw new Error("Could not instantiate XMLHttpRequest");
}

Factory.prototype.prepareGenericRequestData = function(request_type, extraParams) {
  var xml = "<userdata>";
  xml = xml + "<request_type>" + request_type + "<\/request_type>";

  // Add extra parameters if any...
  if (extraParams != null) {
    var extraParam = null;
    for(extraParam in extraParams) {
      if (extraParams[extraParam] != null)
        xml = xml + "<" + extraParam + ">" + extraParams[extraParam] + "<\/" + extraParam + ">";
      else
        xml = xml + "<" + extraParam + ">" + "" + "<\/" + extraParam + ">";
    }
  }
  xml = xml + "<\/userdata>";
  
  return xml;
}

Factory.prototype.prepareMapImageRequestData = function(request_type, request_xml, mapviewer_location) {
  var xml = "<userdata>";
  xml = xml + "<request_type>" + request_type + "<\/request_type>";
  xml = xml + "<xml_request>" + request_xml + "<\/xml_request>"
  xml = xml + "<mapviewer_location>" + mapviewer_location + "<\/mapviewer_location>"
  xml = xml + "<\/userdata>";
  
  return xml;
}

Factory.prototype.convertStringToXml = function(text) {
  var doc;
  if (window.ActiveXObject) {
    doc=new ActiveXObject("Microsoft.XMLDOM");
    doc.async="false";
    doc.loadXML(text);
  }
  // code for Mozilla, Firefox, Opera, etc.
  else {
    var parser=new DOMParser();
    doc=parser.parseFromString(text,"text/xml");
  }
  
  return doc;
}

Factory.prototype.formatNumber = function (pnumber, decimals) {
  if (isNaN(pnumber)) { return 0};
  if (pnumber=='') { return 0};
  
  var snum = new String(pnumber);
  var sec = snum.split('.');
  var whole = parseFloat(sec[0]);
  var result = '';
  
  if(sec.length > 1) {
    var dec = new String(sec[1]);
    dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
    dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
    var dot = dec.indexOf('.');
    if(dot == -1){
            dec += '.'; 
            dot = dec.indexOf('.');
    }
    while(dec.length <= dot + decimals) { dec += '0'; }
    result = dec;
  } else {
    var dot;
    var dec = new String(whole);
    dec += '.';
    dot = dec.indexOf('.');		
    while(dec.length <= dot + decimals) { dec += '0'; }
    result = dec;
  }	
  return result;
}

Factory.prototype.convertToSimpleArray = function(parameters) {
  var ordinaryParameters = new Array();
  var parameterIndex = 0;
  var parameterName;
  
  for (parameterName in parameters) {
    ordinaryParameters[parameterIndex] = parameters[parameterName];
    parameterIndex = parameterIndex + 1;
  }
  
  return ordinaryParameters;
}

Factory.prototype.getServletRequestBaseObj = function() {
  if (this.servletRequestBaseObj == null)
    this.servletRequestBaseObj = new ServletRequestBase(this.servletUrl);
    
  return this.servletRequestBaseObj;
}

Factory.prototype.getListenerRequestBaseObj = function() {
  if (this.listenerRequestBaseObj == null)
    this.listenerRequestBaseObj = new ListenerRequestBase(this.listenerUrl);
  
  return this.listenerRequestBaseObj;
}

// Date must be YYYYMMDDHHSS form year + month + day + hour + minute + second
// strDate d/M/yyyy H:mm:ss format
Factory.prototype.formatDateAsThemeParameter = function(strDate) {
  var dayEnd = strDate.indexOf("/", 0);
  var monthEnd = strDate.indexOf("/", dayEnd + 1);
  var yearEnd = strDate.indexOf(" ", monthEnd + 1);
  var hourEnd = strDate.indexOf(":", yearEnd + 1);
  var minuteEnd = strDate.length;
  
  var day = parseInt(strDate.slice(0, dayEnd));
  var month = parseInt(strDate.slice(dayEnd + 1, monthEnd));
  var year = parseInt(strDate.slice(monthEnd + 1, yearEnd ));
  var hour =  parseInt(strDate.slice(yearEnd + 1, hourEnd ));
  var minute =  parseInt(strDate.slice(hourEnd + 1, minuteEnd));
  var second =  0;
  
  var formattedDate = "";
  formattedDate += year;
  if (month < 10) formattedDate += "0" + month; else formattedDate += month; 
  if (day < 10) formattedDate += "0" + day; else formattedDate += day;
  if (hour < 10) formattedDate += "0" + hour; else formattedDate += hour;
  if (minute < 10) formattedDate += "0" + minute; else formattedDate += minute;
  if (second < 10) formattedDate += "0" + second; else formattedDate += second;
  return formattedDate;
}

Factory.prototype.encodeTurkishChars = function(str) {
  // return str;
  var result = str.replace(/Ý/g, "__I").replace(/ý/g, "__i").replace(/Þ/g, "__S").replace(/þ/g, "__s").replace(/Ð/g, "__G").replace(/ð/g, "__g").replace(/Ç/g, "__C").replace(/ç/g, "__c").replace(/Ü/g, "__U").replace(/ü/g, "__u").replace(/Ö/g, "__O").replace(/ö/g, "__o");
  return result;
}

Factory.prototype.decodeTurkishChars = function(str) {
  // return str;
  var result = str.replace(/__I/g, "Ý").replace(/__i/g, "ý").replace(/__S/g, "Þ").replace(/__s/g, "þ").replace(/__G/g, "Ð").replace(/__g/g, "ð").replace(/__C/g, "Ç").replace(/__c/g, "ç").replace(/__U/g, "Ü").replace(/__u/g, "ü").replace(/__O/g, "Ö").replace(/__o/g, "ö");
  return result;
}

Factory.prototype.getMaximumLabelCount = function(zoomLevel) {
  return __maxLabelCounts[zoomLevel];
}

Factory.prototype.getAutoLabelDisplayZoomLevel = function(resourceType) {
  return __autoLabelDisplayZoomLevel;
}
