
function getHTTPObject() { 
  if (typeof XMLHttpRequest != 'undefined') { 
    return new XMLHttpRequest(); 
  } 
  try { 
    return new ActiveXObject("Msxml2.XMLHTTP"); 
  } catch (e) { 
    try { 
      return new ActiveXObject("Microsoft.XMLHTTP"); 
    } catch (e) {} 
  } 
  return false; 
}
//--------------------------


function isEmptyString(str) {
  return str.replace(" ", "") == "";
}

function trimString(str) {
  if (str == '') return '';
  for(var i = 0; i < str.length; i++) {
    if(str.charAt(i) != " ") {
      str = str.substring(i, 9999);
      break;
    }
  }

  for(var i = str.length - 1; i >=0; i--) {
    if(str.charAt(i) != " ") {
      str = str.substring(0, i+1);
      break;
    }
  }
  return str;
}

function isNull(value) {
   return (!value) || (typeof value == 'undefined') || (value === 'null');
}

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function toRad(deg) {
  return deg * Math.PI/180;
}

function parseBoolean(b) {
	if ((b === "false") || (b === false) || (b === 0)) {
		return false;
	} else {
		return new Boolean(b);
	}
}

function removeSpecialChars(s)
{
	var r=s.toLowerCase();
	r = r.replace(new RegExp(/[àáâãäå]/g),"a");
	r = r.replace(new RegExp(/æ/g),"ae");
	r = r.replace(new RegExp(/ç/g),"c");
	r = r.replace(new RegExp(/[èéêë]/g),"e");
	r = r.replace(new RegExp(/[ìíîï]/g),"i");
	r = r.replace(new RegExp(/ñ/g),"n");                
	r = r.replace(new RegExp(/[òóôõö]/g),"o");
	r = r.replace(new RegExp(//g),"oe");
	r = r.replace(new RegExp(/[ùúûü]/g),"u");
	r = r.replace(new RegExp(/[ýÿ]/g),"y");
	return r;
};

function isNullOrEmpty(value) {
  return (value === false) || (value == null) || (typeof value == 'undefined') || ((typeof value == 'string') && (value == '')); 
}

function getURLParam(name) {
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var tmpURL = window.location.href;
	var results = regex.exec(tmpURL);
	if((results == null) || (results[1] == 'null')) {
		return "";
	} else {
		return unescape(results[1]);
	}
}

function getURLParam2(name) {
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var tmpURL = window.location.href;
	var results = regex.exec(tmpURL);
	if(results == null) {
		return "";
	} else {
		return decodeURIComponent(results[1]);
	}
}

function URLEncode(plaintext)
{
	var SAFECHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.!~*'()";
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
          var ch = plaintext.charAt(i);
          if (ch == " ") {
            encoded += "+";				// x-www-urlencoded, rather than %20
          } else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};

function callFlow2(flowName, params) {
	flowName = encodeURIComponent(flowName);
	params = encodeURIComponent(JSON.stringify(params));
	var code = 'var ifrm = document.createElement("iframe");';
		code+= 'ifrm.setAttribute("src", "/mk/l2s/commons/helper.jsp?flowName=' + flowName + '&params=' + params + '");';
		code+= 'ifrm.style.width = "1px";';
		code+= 'ifrm.style.height = "1px";';
		code+= 'ifrm.style.left = "-1px";';
		code+= 'ifrm.style.top = "-1px";';
		code+= 'document.body.appendChild(ifrm);';
	return window.parent.eval.apply(window.parent, [code]);
}

function callFlow(flowName, params) {  
	var code = "var p = new Array();\n";
	var isJava = false;
	try {
		var reducedName = 'window.' + window.parent.reduceVariable(flowName);
		var ruleFunction =  window.parent.eval.apply(window.parent, [reducedName]);
		isJava = (ruleFunction == undefined);
	} catch (ex) {
		isJava = true;
	}

	for(i = 0; i < params.length; i++) {
		var p = params[i];
		if(p instanceof Boolean) {
		  //
		} else if(p instanceof Date) {
			p = ' new Date(' + p.getTime() + ')';
        } else if(p instanceof String) {
			p = JSON.stringify(p);
		} else if(isNumber(p)) {
			//
		} else if(!isJava && (p instanceof Object)) {
			if(typeof p == 'array' || typeof p == 'Array' || Array.isArray(p)) {
				var pArray = '[';
				for(var j in p) {
					var item = p[j];
					if(j > 0) {
						pArray += ',';
					}
					pArray += '(new Map()).getInstance(' + JSON.stringify(item) + ')';
				}
				pArray += ']';
				p = pArray;
			} else {
				p = '(new Map()).getInstance(' + JSON.stringify(p) + ')';
			}
		} else {
			p = JSON.stringify(p);
		}
		code += 'p[' + i + '] = ' + p + ';\n';
	}

	if(isJava) {
		code += 'executeRuleFromJS("' + flowName + '", p)';
    } else {
		code += "var sysCode = d.WFRForm.sys.value;\n";
		code += "var formCode = d.WFRForm.formID.value;\n";
		code += 'executeJSRuleNoField(sysCode, formCode,"' + flowName + '", p, true);\n';
	}
	return window.parent.eval.apply(window.parent, [code]);
}

function L2SCore() {
}

L2SCore.prototype.processQueue = function (buffer) {
  	for(i = 0; i < buffer.queue.length; i++) {
		var queueItem = buffer.queue[i];
		var fParams = new Array();
		for(j = 0; j < queueItem.params.length; j++) {
			var p = queueItem.params[j];
			if(p instanceof Boolean) {
				//
			} else if(typeof p == 'string') {
				p = p.replace(/\n/g, '\\n');
				if(p.indexOf('"') > 0) {
					p = "'" + p + "'";
				} else {
					p = '"' + p + '"';
				}
			} else if(p instanceof Date) {
				p = ' new Date(' + p.getTime() + ')';
			} else {
				p = '"' + p + '"';
			}
			fParams.push(p);
		}
		var code = 'document.controller.' + queueItem.name + '(' + fParams.join(', ') + ');';
		eval(code);
	}
}