var et_g_whitespace = " \t\n\r";
var et_g_reWhitespace = /^\s+$/
var whitespace = " \t\n\r";
//var wAjax = null;
if (typeof appContext == "undefined")
{
	appContext = "cm";
}
var siteContext = "/"+appContext+"/";
var unallowedChars = "\" \\ / ^ > < +";
function URLEncode(val)
{
	if(encodeURIComponent)
	{
		return encodeURIComponent(val);
	}
	else
	{
		return escape(val);
	}
}	

	function openResourceURL(url)
	{
        if(document.all)
		{
			openPopup(url, 750, 550, "resourceprev");
		}
		else
		{
			window.location.href=url;
		}
	}

	function openAttachment(url)
	{
             if(url.indexOf("?") != -1)
              {
                     url += "&att=true";
              }
              else
              {
                     url += "?att=true";
              }
			  openResourceURL(url);
	}


	function openPopup(url, width, height, title, wfeature){
		var windowX = (screen.width-width)/2;
		var windowY = (screen.height-height)/2;
		var strWindowFeatures = "";
		if(typeof wfeature != "undefined" && wfeature != null && !et_g_isWhitespace(wfeature))
		{
			strWindowFeatures	=	wfeature + ',width='+width+',height='+height+',left='+windowX+',top='+windowY+'screenX='+windowX+',screenY='+windowY;
		}
		else
		{
			strWindowFeatures += 'menubar=no,location=no,toolbar=no,directories=no,scrollbars=yes,status=no,resizable=yes,' +'width='+width+',height='+height+',left='+windowX+',top='+windowY+',screenX='+windowX+',screenY='+windowY ;
		}
		
		if(title && title.indexOf(' ') > -1)
		{
			title = title.replace(' ', '_');
		}
		
		winPopupWindow = window.open(url, title, strWindowFeatures);
		winPopupWindow.focus();
	}



function parseGetArchives(responseDOM, errorMessage)
{

	
		if(errorMessage == null)
		{

			var obj = parseResponse(responseDOM);
			if(obj == null)
			{
				alert("Response is not well formed.");
			}
			else
			{
				if(obj.statusCode == "200")
				{
					
					document.getElementById('archive').innerHTML = obj.output;
				
				}
				else
				{
					alert( "["+ obj.statusMessage + "] " + obj.output );
				}
				setSubColHeight();
			}
		}

}

function setSubColHeight()
{
  var f = document.getElementById("footer");
  var t = document.getElementById("subcol");
  if(t)
  {
	t.style.height=(f.offsetTop - t.offsetTop);
  }

}

function getArchives(blogName, userName) {

			
			//if( wAjax != null)
			//{
				//return;
			//}

			var wAjax = new WikiAjax();
			wAjax.setMethodTOPOST();
			wAjax.setCallback(parseGetArchives);
//String comments, String username, String email, String postassetid, String blogName, String userName
			
			var methodName = "getArchiveInformation";
			var params = new Array();
			var index = 0;
			params[index] = new Object();
			params[index].type = "String";
			//alert(document.forms[0].comment.value);

			params[index].value = blogName;
			index++;
			
			params[index] = new Object();
			params[index].type = "String";
			params[index].value = userName;
			index++;
			
		
			var req = createRequest(createManagerMethod(methodName, params));
			wAjax.addField("reqdata", req);
			wAjax.send();


}



function parsePostComment(responseDOM, errorMessage)
{

		if(errorMessage == null)
		{
			var obj = parseResponse(responseDOM);
			var loc = window.location.href;
			var a = loc.indexOf("?");
			if (a > 0)
			{
					loc = loc.substring(0, a)
			}

			var b = loc.indexOf("#");
			if (b > 0)
			{
					loc = loc.substring(0, b)
			}

	        window.location.href=loc + "?x=" + new Date().getSeconds() + "#comments";
		}

}


function postComment() {
	if(et_g_isWhitespace(document.forms[0].author.value))
			{
				alert("Please enter a name.");
				return;
			}
			/*if(et_g_isWhitespace(document.forms[0].email.value))
			{
				alert("Please enter an e-mail address.");
				return;
			}*/
			if(et_g_isWhitespace(document.forms[0].comment.value))
			{
				alert("Please enter some text in comment.");
				return;
			}

			document.body.style.cursor="wait";
		    
			var wAjax = new WikiAjax();
			wAjax.setMethodTOPOST();
			wAjax.setCallback(parsePostComment);
//String comments, String username, String email, String postassetid, String blogName, String userName

			var methodName = "createComment";
			var params = new Array();
			var index = 0;
			params[index] = new Object();
			params[index].type = "String";
			//alert(document.forms[0].comment.value);

			params[index].value = document.forms[0].comment.value;
			index++;
			
			params[index] = new Object();
			params[index].type = "String";
			params[index].value = document.forms[0].author.value;
			index++;

			params[index] = new Object();
			params[index].type = "String";
			params[index].value = document.forms[0].email.value;
			index++;
			
			params[index] = new Object();
			params[index].type = "String";
			params[index].value = document.forms[0].postassetid.value;
			index++;
			var siteName = document.forms[0].sitename.value;
			var blogName = document.forms[0].blogname.value;
					
			params[index] = new Object();
			params[index].type = "String";
			params[index].value = blogName;
			index++;
					
			params[index] = new Object();
			params[index].type = "String";
			params[index].value = document.forms[0].username.value;
			index++;

		
			var req = createRequest(createManagerMethod(methodName, params));
			wAjax.addField("reqdata", req);
			wAjax.send();
			showLoading();

}

function postCommentBlog() {
	
	if(et_g_isWhitespace(document.forms[1].author.value))
			{
				alert("Please enter a name.");
				return;
			}
			if(et_g_isWhitespace(document.forms[1].email.value))
			{
				alert("Please enter an e-mail address.");
				return;
			}
			if(et_g_isWhitespace(document.forms[1].comment.value))
			{
				alert("Please enter some text in comment.");
				return;
			}
		
			document.body.style.cursor="wait";
		    
			var wAjax = new WikiAjax();
			wAjax.setMethodTOPOST();
			wAjax.setCallback(parsePostComment);
			//String comments, String username, String email, String postassetid, String blogName, String userName
			var methodName = "createComment";
			var params = new Array();
			var index = 0;
			params[index] = new Object();
			params[index].type = "String";
			//alert(document.forms[0].comment.value);

			params[index].value = document.forms[1].comment.value;
			index++;
			
			params[index] = new Object();
			params[index].type = "String";
			params[index].value = document.forms[1].author.value;
			index++;

			params[index] = new Object();
			params[index].type = "String";
			params[index].value = document.forms[1].email.value;
			index++;

			params[index] = new Object();
			params[index].type = "String";
			params[index].value = document.forms[1].postassetid.value;
			index++;

			var siteName = document.forms[1].sitename.value;
			var blogName = document.forms[1].blogname.value;

			params[index] = new Object();
			params[index].type = "String";
			params[index].value = blogName;
			index++;
					
			params[index] = new Object();
			params[index].type = "String";
			params[index].value = document.forms[1].username.value;
			index++;
	
			var req = createRequest(createManagerMethod(methodName, params));

			wAjax.addField("reqdata", req);
			wAjax.send();
			showLoading();


}

	
function parseComments(responseDOM, errorMessage)
{

		if(errorMessage == null)
		{
			var obj = parseResponse(responseDOM);
			if(obj == null)
			{
				alert("Response is not well formed.");
			}
			else
			{
				if(obj.statusCode == "200")
				{
				   var response = obj.output;
				   var posts    =       response.split(";");
				   var counter = 0;

				   for(;counter < posts.length; counter++) {
						postdata = posts[counter];
						postid = postdata.split(":")[0];
						ncomments = postdata.split(":")[1];
						//alert(postid + " " + ncomments);


						document.getElementById(postid).innerHTML = ncomments;

					}

					
					//alert(obj.output);
				}
				else
				{
					alert( "["+ obj.statusMessage + "] " + obj.output );
				}
			}
		}

}


function getCommentCount(content)
{

			var wAjax = new WikiAjax();
			wAjax.setMethodTOPOST();
			wAjax.setCallback(parseComments);

			var methodName = "getCommentCountForPosts";
			var params = new Array();
			var index = 0;
			params[index] = new Object();
			params[index].type = "String";
			params[index].value = content;
			index++;
			
			params[index] = new Object();
			params[index].type = "String";
			params[index].value = "";
			index++;

			params[index] = new Object();
			params[index].type = "String";
			params[index].value = "system";
			index++;
			

			
			var req = createRequest(createManagerMethod(methodName, params));
			wAjax.addField("reqdata", req);
			wAjax.send();
}
	

	function createManagerMethod( methodName, params )
	{
		var request = new Array();
		request.push("<method name=\"");
		request.push(methodName);
		request.push("\">");
		if(params != null)
		{
			request.push("<input>");
			for(var i=0; i<params.length;i++)
			{
				request.push("<param type=\"");
				request.push(params[i].type);
				request.push("\">");
				request.push("<![CDATA[");
				request.push(params[i].value);
				request.push("]]>");
				request.push("</param>");
			}
			request.push("</input>");
		}
		request.push("</method>");
		return request.join(""); 
	}
	
	function parseResponse(responseDOM)
	{
	    

		var obj = new Object();
		if(responseDOM != null)
		{
			try
			{
				var root = responseDOM.documentElement;
			
				for( var i=0; i<root.childNodes.length;i++)
				{
					if(root.childNodes[i].nodeType == 1)
					{

						if(root.childNodes[i].nodeName == "status")
						{

							obj.statusCode = root.childNodes[i].getAttribute("code");
							obj.statusMessage = root.childNodes[i].firstChild.nodeValue;
						}
						else if(root.childNodes[i].nodeName == "output")
						{

							obj.output = root.childNodes[i].firstChild.nodeValue;
						}
					}
					
				}
			}
			catch(err){obj = null;}
		}
		return obj
	}

	function createRequest(data)
	{
		return "<?xml version=\"1.0\" encoding=\"utf-8\"?><request>"+data+"</request>";
	}


	function WikiAjax()
	{
		var requestObject 	= false;
		var responseObject 	= null;
		var method = "GET";
		var callBack = null;
	    if( window.XMLHttpRequest ) 
		{
	    	try 
			{
	    		requestObject = new XMLHttpRequest();
			} catch(e) 
			{
				requestObject = false;
			}
		}
		else if(window.ActiveXObject) 
		{
			try
			{
				requestObject = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) 
			{
				try 
				{
					requestObject = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) 
				{
					requestObject = false;
				}
			}
		}
		if(!requestObject)
		{
			alert("Browser does not support XMLHttpRequest.");
		}
		else
		{
			requestObject.onreadystatechange=function()
			{	
				if( requestObject.readyState == 4 ) 
				{
					requestObject.onreadystatechange=_nullFunction;
					if ( requestObject.status == 200 ) 
					{
						if( callBack != null )
						{
							try
							{
								callBack( requestObject.responseXML, null );
							}
							catch(err){}
						}
					}
					else
					{
						try
						{
							//alert("error");
							callBack( null,  "Error Occured. Status Code["+requestObject.status+"] Message[" + requestObject.statusText + "]" );
						}
						catch(err){}
					}
					//requestObject = null;
					//wAjax = null;
				}
			}
		}
		function _nullFunction(){}
		this.addField 	= addField;
		this.setMethodTOPOST 	= setMethodTOPOST;
		this.send		=	send;
		this.setCallback	=	setCallback;
		this.getRequest = getRequest;
		var arFields = new Array();
		function getRequest()
		{
			return requestObject;
		}
		function setCallback(c)
		{
			callBack = c;
		}
		function addField(name, value)
		{
			var fieldObj = new Object();
			fieldObj.name 	= name;
			fieldObj.value 	= value;
			arFields[arFields.length] = fieldObj;
		}
		function setMethodTOPOST()
		{
			method = "POST";
		}
		function send(asynchronous)
		{
			if(requestObject)
			{
				var asynchCall = true;
				if(asynchronous != null && asynchronous == false)
					asynchCall = false;
				requestObject.open(method, siteContext + "blogsiteajax/", asynchCall);
								
				if(method == "POST")
				{
					requestObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
				}
				requestObject.send(getRequestData());
			}
		}
		function getRequestData()
		{
			var str = null;
			if(arFields.length>0)
			{
				var arData = null;
				for (var i=0; i<arFields.length; i++)
				{
					if( arFields[i].name && !et_g_isWhitespace(arFields[i].name) )
					{
						if(arData == null)
						{
							arData = new Array();
						}
						else
						{
							arData.push("&");
						}
						arData.push(arFields[i].name);
						arData.push("=");
						arData.push(URLEncode(arFields[i].value));
					}
				}
				if( arData != null )
				{
					str = arData.join(""); 
				}
			}
			return str;
		}
	}	

	function et_g_isEmpty(s)
	{
		return ((s == null) || (s.length == 0));
	}
	
	/*
	 * checks for whitespace string
	 */
	function et_g_isWhitespace (s)
	{
		return (et_g_isEmpty(s) || et_g_reWhitespace.test(s));
	}

	function stripWhitespace (s)
	{
		return stripCharsInBag (s, whitespace)
	}

	function stripCharsInBag (s, bag)
	{
		var i;
		var returnString = "";
		// Search through string's characters one by one.
		// If character is not in bag, append to returnString.
		for (i = 0; i < s.length; i++)
		{
			// Check that current character isn't whitespace.
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) returnString += c;
		}
		return returnString;
	}

	function isValidEntityName (s)
	{
		s=stripWhitespace(s);
		var i;
		for (i = 0; i < s.length; i++)
		{
			// Check that current character is number or letter.
			var c = s.charAt(i);
			if ( c == "\"" || c == "\\" || c == "/" || c == "^" || c == ">" || c == "<" || c == "+"){
				return false;
			}
		}
		return true;
	}

	document.onkeydown=handleKey
	
	function handleKey(e)
	{
	   if(window.event) e=window.event||e;
	   var code;
	   if (e.keyCode) code = e.keyCode;
	   else if (e.which) code = e.which;
	   if (e.altKey && code==78)
	   {
			var postMenu = document.getElementById("post_menu");
			if (postMenu) 
			{
				postMenu.focus();
			}
	   }
	}


/**
function postComment() {
    var url = '/blog/jsp/ajax.jsp?operation=createcomment';
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.onreadystatechange = processRequest;
        req.open("POST", url, false);
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.send('&comments=' + escape(document.forms[0].comment.value)+
        '&username='+escape(document.forms[0].author.value) +
        '&email=' + escape(document.forms[0].email.value) +
        '&postassetid='+escape(document.forms[0].postassetid.value)+
        '&sitename='+escape(document.forms[0].sitename.value)+
        '&stagename='+escape(document.forms[0].stagename.value)+
        '&publishpath='+escape(document.forms[0].publishpath.value)+
        '&postid='+escape(document.forms[0].postid.value)+
        '&catid='+escape(document.forms[0].catid.value)+
        '&authtemplateid='+escape(document.forms[0].authtemplateid.value)
        );

        var loc = window.location.href;
        var a = loc.indexOf("?");
        if (a > 0)
        {
                loc = loc.substring(0, a)
        }

        var b = loc.indexOf("#");
        if (b > 0)
        {
                loc = loc.substring(0, b)
        }

        window.location.href=loc + "?x=" + new Date().getSeconds() + "#comments";
}

function processRequest() {
    if (req.readyState == 4) {
		
        if (req.status == 200) {
          parseMessages();
        } else {
          alert ( "Not able to retrieve description" );
				}
    }
}

function parseMessages() {
	var response  = req.responseText;

	//alert ( response );
}

function getCommentCount(content)
{

        var postids=content;
        //alert("postids..." + postids);
        if (window.XMLHttpRequest)
        {
                  req = new XMLHttpRequest();
          } else if (window.ActiveXObject) {
                  req = new ActiveXObject("Microsoft.XMLHTTP");
          }
                try
        {
                req.onreadystatechange = processCommentRequest;
                req.open("GET", "/blog/jsp/ajax.jsp?operation=commentcount&postids="+postids, true);
                req.send(null);
        }
        catch(err){alert(err);}

        //alert(req.responseText);
}

function processCommentRequest()
{
        try
        {
                if(!req)
                        alert("Not able to retrieve req");
                if (req.readyState == 4) {
                if (req.status == 200) {
                //alert("200")
                  parseComments();
                } else {
          alert ( "Not able to retrieve description" );
                }
        }
        }
        catch(err){alert(err);}
}


function parseComments()
{
   var response  = req.responseText;
   var posts    =       response.split(";");
   var counter = 0;

   for(;counter < posts.length; counter++) {
        postdata = posts[counter];
        postid = postdata.split(":")[0];
        ncomments = postdata.split(":")[1];
        //alert(postid + " " + ncomments);


        document.getElementById(postid).innerHTML = ncomments;

    }




}

**/




