/*
Licensed Materials - Property of IBM
 
5724S31

Copyright IBM Corp.  2007    All Rights Reserved.

US Government Users Restricted Rights - Use, duplication or 
disclosure restricted by GSA ADP Schedule Contract with IBM 
Corp.
*/
dojo.require("dojo.string.*");

var RoninUtils = {
	debug: false,
	version: "1.0",
	baseUrl: "<%=com.ibm.ronin.framework.RoninUtil.getBaseUrl(request,true)%>",
	
	//NAME: rewriteUrl (targetUrl, proxyUrl) returns url 
	//DESC: Takes the target URL as well as the proxy URL and returns the rewritten URL for the resource
	//NOTE: This specifics of this function (such as rewriting rules) would eventually be returned 
	//		by the server as soon as the proxy implementation is chosen
	rewriteURL: function (/*String*/ targetUrl, /*String*/ proxyUrl, /*boolean*/ credentials) {
    var newURL = proxyUrl;
	
	var host = window.location.host;
	var protocol = window.location.protocol;
	
	// check if this is a proxy request:
	if ( (targetUrl.indexOf(protocol) == 0 && targetUrl.indexOf(host) == protocol.length+2) && (!credentials)) {
		return targetUrl;
	}
    
	// else go on with proxy stuff
    if (targetUrl.indexOf("https") == 0)
      newURL += "https/";
    else
      newURL += "http/";
      
    newURL += targetUrl.substr(targetUrl.indexOf("://") + 3);
    
    return newURL;
	},
	
	generateThreadUID: function (size) {
  		function getRandomNumber(range) {
			return Math.floor(Math.random() * range);
        }

		function getRandomChar() {
			var chars = "0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ";
			return chars.substr( getRandomNumber(chars.length), 1 );
		}

		var buf = new dojo.string.Builder();
		for(var i = 0; i < size; i++)
			buf.append(getRandomChar());
		return buf.toString();
	},	
	
	getDomainFromURL: function (str) { 
		var result = ""; 
		if (str == null || str.length == 0) return ""; 
		var i = str.indexOf("/",7); 
		if (i > -1) 
			result = str.substring(7, i); 
		else 
			result = str.substring(7, str.length); 
		return result; 
	},
	
	loadScript: function(script) {
		if (!SemTagSvc.scripts[script]) {
			SemTagSvc.scripts[script] = true;
			var scriptElem = document.createElement("script");
			scriptElem.src = SemTagSvc.baseUrl + script;
			document.body.insertBefore(scriptElem, document.body.firstChild);
		}
	}
	
	//init: function() {
	//	var head = document.getElementsByTagName('head');
	//	if (head[0]) {
	//		var css = document.createElement('link');
	//		css.rel = "stylesheet";
	//		css.href = SemTagSvc.baseUrl + '/ui/styles.css';
	//		css.type = "text/css";
	//		head[0].appendChild(css);
	//	}	
	
};

/*
RoninUtils.StringBuffer.prototype.append = function append(string) { 
	this.buffer.push(string); 
	return this; 
}; 
	
RoninUtils.StringBuffer.prototype.toString = function toString() { 
   return this.buffer.join(""); 
}; 

RoninUtils.StringBuffer.prototype.clear = function clear() { 
   this.buffer.length = 1;
};
*/ 
