/* $Id: utils.js,v 1.1 2006/04/07 04:52:49 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);
	var len = frmList.length;
	for (i=0;i<len;i++) {
		var frmItem = frmList.item(i);

		var butElement = eval("frmItem."+buttonName);
			
		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);
		var len = frmList.length;
		for (j=0;j<len;j++) {
			var frmItem = frmList.item(j);
			var butElement = eval("frmItem."+buttonName);
			if (butElement.style.display == "none") {
				butElement.style.display = "inline";
			}else {
				butElement.style.display = "none";
			}
		}
	}
}

Utils.getElementList = function (frmName, eleName) {
	var eleArr = [];
	var frmList = eval("document."+frmName);
	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);
	var len = frmList.length;
	for (j=0;j<len;j++) {
		var frmItem = frmList.item(j);
		eleArr[j] = eval("frmItem."+eleName);
	}	

	return eleArr;
}

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

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

	return temp;
}

Utils.escapeAmpersands = function (str, escapeStr) {
	if (!escapeStr) {
		escapeStr = "&";	
	}
	var index = str.indexOf(escapeStr);
	var temp="";
	var i=0;
	
	while (index != -1) {
		temp += str.substring(i,index);
		i=index+escapeStr.length;
		temp += "$AMPSERAND$";
		index = str.indexOf(escapeStr, i);
	}
	temp += str.substring(i, str.length);

	return temp;
}

Utils.getCookieValue = function (name) {
	var len = name.length;        
	var index = (document.cookie).indexOf(name);
	if (index == -1) {
		return null;
	}
	var beginIndex = index+len;
	var endIndex = (document.cookie).indexOf(";",beginIndex);         
	return (document.cookie).substring(beginIndex+1,endIndex);
}

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";
			divNode.style.display="none";
		}
		else {
			var img = imgs[0];
			img.src = "/wo/images/toggleup.jpg";
			divNode.style.display="inline";
		}
	}
}

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

}

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

Utils.showMsg = function(msg) {
	/*
	var spanEle = document.getElementById("msgarea");
	spanEle.innerHTML = msg;
	spanEle.style.top="-2px";
	//spanEle.style.left=screen.width-spanEle.style.width;
	spanEle.style.display="inline";
	*/

	var processDiv=document.getElementById("processIcon");
	if(processDiv)
	{
	processDiv.style.display="inline";
	}
}

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


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

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


Utils.closeDiv = function(divId) {
	/*document.getElementById("menuactions").innerHTML = '';
	document.getElementById("docNameDiv").innerHTML = ''; */
	//document.getElementById(divId).style.display = "none";
}


Utils.createElement = function(tag, attributes, value) {
	var el = document.createElement(tag);
	
	if (attributes) {
		var attrArr = attributes.split(" ");
		for (var i in attrArr) {
			var attribute = attrArr[i];
			var nvPair = attribute.split("=");
			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(" ");
                for (var i in attrArr) {
                        var attribute = attrArr[i];
                        var nvPair = attribute.split("=");
                        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 = 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 = '';
	//bodyC.style.height="100%";	
	var innerbody = document.createElement("div");
	innerbody.id = "wo_innerbody";
	body.appendChild(innerbody);
	var ht=((Writer.leftPanelON)?Writer.getNormalEditorFrameHeight():Writer.getMaximizedEditorFrameHeight())+"px";
	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;
}

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

	if (docName.trim().length == 0) {
		alert("Your document name consists of only spaces. Please provide a valid name");
		return false;
	}

	
	/*
	var regExp = /^[a-z-a-z0-9_ ]{0,100}$/i;
	if (!regExp.test(docName)) {
		alert("Only \" , _ a to z and 0 to 9 are allowed.");		
		return false;
	}
	*/
	
	if(docName.indexOf('\'')>=0 || docName.indexOf('"')>=0 || docName.indexOf('\\') >=0)
	{
		alert('Special characters are not allowed in document name');
		return false;
  	}
	


	return true;
}

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

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

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='';
        divNode.className="redmsg";
        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);

		l.style.left="";
		l.style.right="";
		this.tot = document.body.offsetWidth;
		if(((this.tot)* 0.6) < x) {

			//this.xval = x ;
			this.xval = document.body.offsetWidth - x + 150;

			//alert(this.xval);

			//l.style.right = this.xval + "px";
			l.style.left = this.xval + "px";
		}
		else {
			l.style.left = x + "px";		
		}
		l.style.width="300px";

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

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

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

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


Utils.validateTags = function(tagName) {
	
	var regExp = /^[a-z-a-z0-9_," ]{0,100}$/i;
	if (!regExp.test(tagName	)) {
		/*
		document.getElementById('wo_tg_added').innerHTML = "Only \" , _ a to z and 0 to 9 are allowed.";
		document.getElementById('wo_tg_added').style.display = "inline";
		setTimeout("Writer.closeDivTag('wo_tg_added')" , 2000);		
		*/
		return false;
	}

	return true;
}

Utils.impDocConventionCheck = function(docname) {
	//alert("INSIDE");
	if(!docname || docname.length==0)
	{
		alert("Document name cannot be empty");
		return false;
	}
	/*
	var regExp = /^[a-z-a-z0-9_ ]{0,100}$/i;
	if (!regExp.test(docname)) {
		alert("Special characters are not allowed.");
		return false;
	}
	*/

	if(docname.indexOf('\'')>=0 || docname.indexOf('"')>=0 || docname.indexOf('\\') >=
0)
        {
                alert('Special characters are not allowed in document name');
                return false;
        }

	//Writer.closeUploadDiv();
	var el=document.getElementById("importprocess");

	if(el)
	{
	el.style.display="inline";
	}
	//Utils.showMsg("Importing Document ...");
}

Utils.showDocName = function(browsedoc) {

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

	if(fname.indexOf('.')!=-1)
	{
		fname = fname.substring(0,fname.indexOf('.'));
	}
	//alert(fname);
	document.importDoc.docname.value = fname;
	if(!browsedoc || browsedoc.length==0)
	{
		//alert("Browse document for import");
		//document.importDoc.docname.value == fname;
	}
}

Utils.selectTextBox=function(id)
{

var el=document.getElementById(id);

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

}

Utils.showInfo=function(htm)
{
//id=document.getElementById("createdTD");
shwid=document.getElementById('wordcnt');
contid=document.getElementById('_wordcnt');
contid.innerHTML=htm;
//alert(document.body.offsetWidth+"|"+id.offsetWidth)
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.top=id.offsetTop;
shwid.style.width="248px";
Writer.openSlider("wordcnt");
setTimeout(function() { Writer.closeSlider("wordcnt"); }  , 4000);

}


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";
       msgEle.style.left = x+"px";
       msgEle.style.top = y+"px";
   }
     if (width) {
       msgEle.style.width = width;
   }

   msgEle.style.display="block";   
}

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

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);
}

