/* $Id: utils.js,v 1.5 2009/03/16 16:24:06 shanmugampl Exp $ */

/**
 * utils.js 
 *
 *
 * Created: Mon 21 Mar 2005 16:10
 *
 * @author <a href="mailto: vijayr">vijayr</a>
 * @version
 **/

function Utils() {
}

Utils.toggleButtonValue = function (frmName, buttonName, value1, value2, action1,action2) {
	var frmList = eval("document." + frmName); // No I18N
	var len = frmList.length;
	for (i=0;i<len;i++) {
		var frmItem = frmList.item(i);

		var butElement = eval("frmItem."+buttonName); // No I18N

		var butValue = butElement.value;
		if (butValue == value1) {
			butElement.value = value2;
			butElement.onclick = action2;
		}
		else {
			butElement.value = value1;
			butElement.onclick = action1;
		}
	}
}

Utils.hideOrShowButtons = function (frmName, buttonArr) {
	for (i in buttonArr) {
		var buttonName = buttonArr[i];
		var frmList = eval("document."+frmName); // No I18N
		var len = frmList.length;
		for (j=0;j<len;j++) {
			var frmItem = frmList.item(j);
			var butElement = eval("frmItem."+buttonName); // No I18N
			if (butElement.style.display == "none") {
				butElement.style.display = "inline"; // No I18N
			}else {
				butElement.style.display = "none"; // No I18N
			}
		}
	}
}

Utils.getElementList = function (frmName, eleName) {
	var eleArr = [];
	var frmList = eval("document."+frmName); // No I18N
	var len = frmList.length;
	for (j=0;j<len;j++) {
		var frmItem = frmList.item(j);
		var spanElements = frmItem.getElementsByTagName("span");
		var spanElement = spanElements[0];
		if (spanElement.id == eleName) {
			eleArr[j] = spanElement;
		}
	}
	return eleArr;
}

Utils.getFormElementList = function(frmName, eleName) {
	var eleArr = [];
	var frmList = eval("document."+frmName); // No I18N
	var len = frmList.length;
	for (j=0;j<len;j++) {
		var frmItem = frmList.item(j);
		eleArr[j] = eval("frmItem."+eleName); // No I18N
	}	

	return eleArr;
}

Utils.removeCarriageReturns = function (str, searchStr) {
	if (!searchStr) {
		searchStr = "\n"; // No I18N
	}

	var index = str.indexOf(searchStr);
	var temp=""; // No I18N
	var i=0;
	while (index != -1) {
		temp += str.substring(i, index);
		i=index+searchStr.length;
		temp += " " ; // No I18N
		index = str.indexOf(searchStr, i);
	}
	temp += str.substring(i,str.length);

	return temp;
}

Utils.escapeAmpersands = function (str, escapeStr) {
	if (!escapeStr) {
		escapeStr = "&"; // No I18N	
	}
	var index = str.indexOf(escapeStr);
	var temp="";
	var i=0;

	while (index != -1) {
		temp += str.substring(i,index);
		i=index+escapeStr.length;
		temp += "$AMPSERAND$"; // No I18N
		index = str.indexOf(escapeStr, i);
	}
	temp += str.substring(i, str.length);

	return temp;
}



Utils.showOrHideBox = function(tdNode, divId) {
	var node = document.getElementById(divId);
	var divNodes = node.getElementsByTagName("div");
	var imgs = tdNode.getElementsByTagName("img");
	for (var i=0;i<divNodes.length;i++) {
		var divNode = divNodes[i];
		if (divNode.style.display != "none") {
			var img = imgs[0];
			img.src = "/wo/images/toggledown.jpg"; // No I18N
			divNode.style.display="none"; // No I18N
		}
		else {
			var img = imgs[0];
			img.src = "/wo/images/toggleup.jpg"; // No I18N
			divNode.style.display="inline"; // No I18N
		}
	}
}

Utils.showProgressIndicator=function()
{
	var processDiv=document.getElementById("processIcon");
	if(processDiv)
	{
		processDiv.style.display="inline"; // No I18N
	}

}

Utils.hideProgressIndicator = function() {
	var processDiv=document.getElementById("processIcon");
	if(processDiv)
	{
		processDiv.style.display="none"; // No I18N	
	}
}

Utils.showMsg = function(msg) {
	var processDiv=document.getElementById("processIcon");
	if(processDiv)
	{
		processDiv.style.display="inline"; // No I18N
	}
}

Utils.hideMsg = function() {
	var processDiv=document.getElementById("processIcon");
	if(processDiv)
	{
		processDiv.style.display="none"; // No I18N	
	}
}

Utils.openDiv = function(divId) {
	document.getElementById(divId).style.display = "block"; // No I18N
}

Utils.hideDiv = function(divId) {
	document.getElementById(divId).style.display = "none"; // No I18N
}


Utils.closeDiv = function(divId) {
}

Utils.createElement = function(tag, attributes, value) {
	var el = document.createElement(tag);

	if (attributes) {
		var attrArr = attributes.split(" "); // No I18N
		for (var i in attrArr) {
			var attribute = attrArr[i];
			var nvPair = attribute.split("="); // No I18N
			var name = nvPair[0];
			var attrValue = nvPair[1];
			if (name && attrValue) {
				el.setAttribute(name,attrValue);
				if (name == "class") {
					el.className=attrValue;
				}
			}
			else {
				el.setAttribute(name,"");
			}
		}
	}

	if (value) {
		var textNode = null;
		if (typeof value == "string") {
			textNode = document.createTextNode(value);
		}
		else {
			textNode = value;
		}
		el.appendChild(textNode);
	}
	return el;
}


Utils.createElementFromDoc = function(doc,tag, attributes, value) {
	var el = doc.createElement(tag);
	if (attributes) {
		var attrArr = attributes.split(" "); // No I18N
		for (var i in attrArr) {
			var attribute = attrArr[i];
			var nvPair = attribute.split("="); // No I18N
			var name = nvPair[0];
			var attrValue = nvPair[1];
			if (name && attrValue) {
				el.setAttribute(name,attrValue);
				if (name == "class") {
					el.className=attrValue;
				}
			}
			else {
				el.setAttribute(name,""); // No I18N
			}
		}
	}

	if (value) {
		var textNode = null;
		if (typeof value == "string") {
			textNode = doc.createTextNode(value);
		}
		else {
			textNode = value;
		}
		el.appendChild(textNode);
	}

	return el;
}

Utils.clearDiv = function(div) {
	var divNode = document.getElementById(div);
	divNode.innerHTML = '';
}

Utils.loadInBody = function(result) {
	var body = document.getElementById("body");
	var bodyC = document.getElementById("bodyC");
	body.innerHTML = '';
	var innerbody = document.createElement("div"); // No I18N
	innerbody.id = "wo_innerbody"; // No I18N
	body.appendChild(innerbody);
	var ht=((Writer.leftPanelON)?Writer.getNormalEditorFrameHeight():Writer.getMaximizedEditorFrameHeight())+"px"; // No I18N
	bodyC.style.height=ht;	
	body.style.padding=10;
	innerbody.innerHTML = result;
}

Utils.getPos = function(el) {
	var r = { offsetLeft: el.offsetLeft, offsetTop: el.offsetTop };
	if (el.offsetParent) {
		var tmp = Utils.getPos(el.offsetParent);
		r.offsetLeft += tmp.offsetLeft;
		r.offsetTop  += tmp.offsetTop;
		r.offsetRight += tmp.offsetRight;
	}
	return r;
}

/**
 * Unused functionality
 */
Utils.validateDoc =  function validate(docName) {
	if (!docName || docName.length == 0 ) {
		alert("Provide a name for the document"); // No I18N
		return false;
	}

	if (docName.trim().length == 0) {
		alert("Your document name consists of only spaces. Please provide a valid name"); // No I18N
		return false;
	}
	if(docName.indexOf('\'')>=0 || docName.indexOf('"')>=0 || docName.indexOf('\\') >=0)
	{
		alert('Special characters are not allowed in document name'); // No I18N
		return false;
	}
	return true;
}

Utils.showMsgDisplay = function(msg, displaymillis) {
	var divNode = document.getElementById("msgarea");
	divNode.innerHTML = msg;
	divNode.style.display = "inline"; // No I18N
	setTimeout(function() { Utils.hideMsgDisplay() }  , displaymillis);
}

Utils.showBlogMsg = function(msg) {
	var divNode = document.getElementById("infoMessageContent");
	if(divNode)
	{
		divNode.innerHTML=''; // No I18N
		var textNode = document.createTextNode(msg);
		divNode.appendChild(textNode);
		Writer.openSlider("infoMessage"); // No I18N
		setTimeout(function() { Writer.closeSlider("infoMessage"); }  , 4000); // No I18N
	}
}

Utils.changeClass=function(arr,cname)
{
	for(i=0;i<arr.length;i++)
	{
		var id=arr[i];
		var el=document.getElementById(id);
		if(el)
		{
			el.className=cname;
		}
	}//for
}

Utils.showMsgInId = function(msg,id) {
	var divNode = document.getElementById(id);
	if(divNode)
	{
		divNode.innerHTML=''; // No I18N
		divNode.className="redmsg"; // No I18N
		var textNode = document.createTextNode(msg);
		divNode.appendChild(textNode);
	}
}


Utils.showLockMsg= function(user,x,y)
{
	var l=document.getElementById("lockdiv");

	if(l)
	{
		var s=document.getElementById("lockdivname");
		if(s && user)
		{
			s.innerHTML=user;
		}
		MenuActions._addEvent(document, "mousedown", MenuActions.lockClick); // No I18N

		l.style.left=""; // No I18N
		l.style.right=""; // No I18N
		this.tot = document.body.offsetWidth;
		if(((this.tot)* 0.6) < x) {
			this.xval = document.body.offsetWidth - x + 150;
			l.style.left = this.xval + "px"; // No I18N
		}
		else {
			l.style.left = x + "px"; // No I18N		
		}
		l.style.width="300px"; // No I18N

		l.style.top = y + 4 + "px"; // No I18N
		l.style.position = "absolute"; // No I18N
		Writer.openSlider("lockdiv"); // No I18N
	}
}

Utils.hideLockMsg= function()
{
	var l=document.getElementById("lockdiv");

	if(l)
	{
		l.style.display='none'; // No I18N
	}
}

Utils.hideMsgDisplay = function() {
	var divNode = document.getElementById("msgarea");
	divNode.innerHTML='';
	divNode.style.display = "none"; // No I18N
}


Utils.validateTags = function(tagName) {
	var regExp = /^[a-z-a-z0-9_," ]{0,100}$/i; // No I18N
	if (!regExp.test(tagName	)) {
		return false;
	}

	return true;
}

	Utils.impDocConventionCheck = function(docname) {
		if(!docname || docname.length==0)
		{
			alert("Document name cannot be empty"); // No I18N
			return false;
		}
		if(docname.indexOf('\'')>=0 || docname.indexOf('"')>=0 || docname.indexOf('\\') >= 0)
		{
			alert('Special characters are not allowed in document name'); // No I18N
			return false;
		}

		var el=document.getElementById("importprocess");

		if(el)
		{
			el.style.display="inline"; // No I18N
		}
	}

Utils.showDocName = function(browsedoc) {

	var fname;
	if (browsedoc.lastIndexOf("/") != -1) {
		fname = browsedoc.substring(browsedoc.lastIndexOf('/')+1,browsedoc.length); // No I18N
	}
	else {
		fname = browsedoc.substring(browsedoc.lastIndexOf('\\')+1,browsedoc.length); // No I18N
	}

	if(fname.indexOf('.')!=-1)
	{
		fname = fname.substring(0,fname.indexOf('.')); // No I18N
	}
	document.importDoc.docname.value = fname;
	if(!browsedoc || browsedoc.length==0)
	{
	}
}

Utils.selectTextBox=function(id)
{

	var el=document.getElementById(id);

	if(el)
	{
		el.focus();
		el.select();
	}

}

Utils.showInfo=function(htm)
{
	shwid=document.getElementById('wordcnt');
	contid=document.getElementById('_wordcnt');
	contid.innerHTML=htm;
	if(HTMLArea.is_ie){
		shwid.style.left=document.body.offsetWidth-280;
		shwid.style.top=document.body.offsetHeight-98;
	}
	else 
	{
		shwid.style.left=document.body.offsetWidth-258;
		shwid.style.top=document.body.offsetHeight-76;
	}
	shwid.style.width="248px"; // No I18N
	Writer.openSlider("wordcnt"); // No I18N
	setTimeout(function() { Writer.closeSlider("wordcnt"); }  , 4000); // No I18N

}


Utils.findDocDimension = function() {
	if (isIE) {
		return {
width : document.body.offsetWidth,
height : document.body.offsetHeight
		}
	}
	else {
		return {
width : window.screen.width - document.body.scrollLeft,
height : window.screen.height - document.body.scrollTop
		}
	}
} 


Utils.showCenterMsg = function(selectId, message, x,y,width) {
	var msgEle = document.getElementById(selectId);
	if (!msgEle) {
		return;
	}

	msgEle.innerHTML = message;
	if (x && y) {
		msgEle.style.position = "absolute"; // No I18N
		msgEle.style.left = x+"px"; // No I18N
		msgEle.style.top = y+"px"; // No I18N
	}
	if (width) {
		msgEle.style.width = width;
	}

	msgEle.style.display="block"; // No I18N   
}

Utils.hideLoadingMsg=function()
{
	var msgEle=document.getElementById("loading_msg");
	if(msgEle)
	{
		msgEle.style.display="none"; // No I18N
	}
}

Utils.showLoadingMsg=function()
{

	var doc = Utils.findDocDimension();
	var left = (doc.width / 2 ) - document.body.scrollLeft ;
	var top;
	if (isIE) {
		top = doc.height/2 - 100;
	}
	else {
		top = (doc.height / 2) - 100;
	}
	Utils.showCenterMsg("loading_msg","Loading ...",left,top,100); // No I18N
}
