

var UIRegistry = {}


function IsIE(version){
        var rc = false;
        if (navigator.userAgent.indexOf('MSIE') >  - 1){
                if (navigator.userAgent.indexOf("Opera") == ( - 1)){
                        if (null != version){
                                if (navigator.userAgent.indexOf(version) >  - 1){
                                        rc = true;
                                }
                        }
                        else{
                                rc = true;
                        }
                }
        }
        return rc;
}


function IsVisible(obj){
        ;
        var result = false;
        if (obj != null && obj.style != null){
                var d = obj.style.display;
                result = (d != null && d != "none");
        }
        return result;
}

function GetAbsoluteTop(pDiv) {
		var vtop = pDiv.offsetTop;
		var vParent = pDiv.offsetParent;
        while (vParent.tagName.toUpperCase() != "BODY")
        {
            vtop += vParent.offsetTop;
            vParent = vParent.offsetParent;
        }
		return vtop;
}            
function GetAbsoluteLeft(pDiv) {
		var vleft = pDiv.offsetLeft;
		var vParent = pDiv.offsetParent;
        while (vParent.tagName.toUpperCase() != "BODY")
        {
            vleft += vParent.offsetLeft;
            vParent = vParent.offsetParent;
        }
		return vleft;
}

function ShowHide(obj, show){
        ;
        ;
        if (obj != null){
                var visible = IsVisible(obj);
                if (visible != show){
                        obj.style.display = show ? "block" : "none";
                }
        }
}

function SubmitMessage() {
	var message = UIRegistry.inputBox.value;
	if  (message.length > 0) {
		if (message.substring(message.length-1) == "\n" ) {
			message = message.substring(0,message.length-1);
		}
		if (message.substring(message.length-1) == "\r" ) {
			message = message.substring(0,message.length-1);
		}
		if (message.length > 0)
			UIRegistry.conversation.SendMessage(message);
	}
	UIRegistry.inputBox.value="";
	if (false == UIRegistry.sendButton.disabled){
			EnableSendButton(false);
    }
}


function HidePopups(){
	ShowColorPopup(false);
	ShowEmoticonPopup(false);
};

function ShowEmoticonPopup(showFlag){
		if (UIRegistry.transcriptWindow == null)return;


		var emoticonDiv = UIRegistry.emoticonDiv;

		if (showFlag){
				var emoticonImage = UIRegistry.emoticonImage;

				emoticonDiv.style.left = GetAbsoluteLeft(emoticonImage) + 12;

				emoticonDiv.style.top = GetAbsoluteTop(emoticonImage) - 158;

		}
		ShowHide(emoticonDiv, showFlag);

};
function ShowColorPopup(showFlag){
		if (UIRegistry.transcriptWindow == null)return;

		var colorDiv = UIRegistry.colorDiv;

		if (showFlag){
				var colorImage = UIRegistry.colorImage;
				colorDiv.style.left = GetAbsoluteLeft(colorImage) + 12;
				colorDiv.style.top = GetAbsoluteTop(colorImage) - 70;


		}
		ShowHide(colorDiv, showFlag);

};

function ApplyFormat() {
	if (UIRegistry.conversation != null)
		UIRegistry.conversation.GetFormat().ApplyStyle(UIRegistry.inputBox);
}



function EnableSendButton(b) {
	if (b) {
		sendButton.className = "sendButtonEnabled";
		sendButton.disabled = false;
	}
	else {
        sendButton.className = "sendButtonDisabled";
    	sendButton.disabled = true;    
	}
}



function OnComposeKeyUp(e){
        ;
        ;
		var textControl = UIRegistry.inputBox;
		var sendButton = UIRegistry.sendButton;
        try{
                if (textControl != null && sendButton != null){

                        var msg = textControl.value;             
                        if (msg.length > 0){
                                if (true == sendButton.disabled){                                        
                                        EnableSendButton(true);
                                }
                                if (msg.length > 400){
                                        textControl.value = msg.substring(0, 400);
                                }
                        }
                        else{
                                if (false == sendButton.disabled){
                                        EnableSendButton(false);
                                }
                        }
                }
        }
        catch (exception){
                alert(exception.description);
        }
}

function OnComposePaste(e){
		var textControl = UIRegistry.inputBox;
		var sendButton = UIRegistry.sendButton;
        try{
                if (textControl != null && sendButton != null){
                        if (true == sendButton.disabled){
                                EnableSendButton(true);
                        }
                }
        }
        catch (exception){
                ;
        }
}



function OnComposeKeyPressed(e){
	try{
           if (IsIE()){
                   if (13 == event.keyCode){
                           if (event.shiftKey){}
                           else{
                               window.setTimeout('SubmitMessage();',10);
                               return false;
                           }
                   }
           }
           else if ((null != e) && (null != e.which)){
                   if (13 == e.which){
                           if (e.shiftKey){}
                           else{
                               window.setTimeout('SubmitMessage();',10);
                               return false;
                           }
                   }
           }
   }
   catch (exception){
           ;
   }

   return true;
}

function OnDocumentKeyDown(e) {

	if (IsIE()) {
		var p = window;
		var p1 = null;
		while (p != p1) {
			try {
				if (p.event.altKey) {
					if ( 67 == p.event.keyCode) {        	
			       		globalConversation.AcceptActivity();
			        }
			        else if (68 == p.event.keyCode) {
			       		globalConversation.RejectActivity();
			        }
			    }
			    else {
			    	if ( 27 == p.event.keyCode) {
			    		return false;
			    	}
			    }
			} catch (e) {}
		    p1 = p;
		    p = p.parent;
		}    
    }
    else {
    	if (e.altKey) {
        	if (67 == e.which) {
        		globalConversation.AcceptActivity();
        	}
        	else if (68 == e.which) {
        		globalConversation.RejectActivity();
        	}
        }
        else {
	    	if ( 27 == e.which) {
	    		return false;
	    	}
	    }
    }
}

function OnSendButtonClicked() {
	SubmitMessage();
    UIRegistry.inputBox.focus();
}

function OnChangeNameButtonClicked() {
	var name = UIRegistry.nameBox.value;
	if (name != null && name != "")
		UIRegistry.conversation.SetDisplayName(name);
	UIRegistry.inputBox.focus();
}

function OnChangeFont(value) {
	UIRegistry.conversation.SetFont(value);
	ApplyFormat();
}

function OnChangeSize(value) {
	UIRegistry.conversation.SetSize(value);
	ApplyFormat();
}

function OnClickColor(){

	var colorDiv = UIRegistry.colorDiv;
	ShowEmoticonPopup(false);
	ShowColorPopup(!IsVisible(colorDiv));
};
function OnClickEmoticon(){

	var emoticonDiv = UIRegistry.emoticonDiv;
	ShowColorPopup(false);
	ShowEmoticonPopup(!IsVisible(emoticonDiv));
};


function OnClickEmoticonPopup(cell){
		
		var title = cell.getAttribute("title");
		;
		;
		UIRegistry.inputBox.value += title;
		if (true == UIRegistry.sendButton.disabled){
			EnableSendButton(true);
        }
		
		ShowEmoticonPopup(false);
		UIRegistry.inputBox.focus();
};
function OnClickColorPopup(cell){
		
		var color = cell.getAttribute("bgcolor");
		;
		;
		UIRegistry.conversation.SetColor(color);
		ApplyFormat();
		ShowColorPopup(false);
		UIRegistry.inputBox.focus();
};

function OnToggleStyle(styleCode, cell){
		var conversation = UIRegistry.conversation;
		if (conversation != null){
				var styleToggle = conversation.ToggleStyle(styleCode);
				if (cell != null){
						cell.style.background = styleToggle ? "#2053C8" : "#EBF1FA";
				}
				ApplyFormat();

		}
};

function OnClickNudge(){

	UIRegistry.conversation.SendNudge();
	UIRegistry.inputBox.focus();
};


function givedec(hex)
{
   if(hex == "A") val = 10;
   else if(hex == "B") val = 11;
   else if(hex == "C") val = 12; 
   else if(hex == "D") val = 13;
   else if(hex == "E") val = 14;
   else if(hex == "F") val = 15;
   else val = eval(hex);
   return val;
}

function givehex(dec)
{
   if(dec == 10) val = "A";
   else if(dec == 11) val = "B";
   else if(dec == 12) val = "C";
   else if(dec == 13) val = "D";
   else if(dec == 14) val = "E";
   else if(dec == 15) val = "F";
   else val = "" + dec;
   return val;
}

function hex2rgb(hex)
{
   if(hex.indexOf("#") == 0)hex = hex.substring(1);
   hex = hex.toUpperCase();

   a = givedec(hex.substring(0, 1));
   b = givedec(hex.substring(1, 2));
   c = givedec(hex.substring(2, 3));
   d = givedec(hex.substring(3, 4));
   e = givedec(hex.substring(4, 5));
   f = givedec(hex.substring(5, 6));

   x = (a * 16) + b;
   y = (c * 16) + d;
   z = (e * 16) + f;
   
   return {"r":x,"g":y,"b":z};
}

function rgb2hex(r,g,b)
{

   h1 = givehex(Math.floor(r / 16));
   h2 = givehex(r % 16);
   h3 = givehex(Math.floor(g / 16));
   h4 = givehex(g % 16);
   h5 = givehex(Math.floor(b / 16));
   h6 = givehex(b % 16);
   hex = h1 + h2 + h3 + h4 + h5 + h6;
   return hex;
}


function colorbar(shex,ehex,h) {
	
	var srgb = hex2rgb(shex);
	var ergb = hex2rgb(ehex);
	var roff = (ergb.r-srgb.r)/h;
	var goff = (ergb.g-srgb.g)/h;
	var boff = (ergb.b-srgb.b)/h;

	for (var i=0;i<h;i++)
	{
		var hex = rgb2hex(srgb.r+Math.floor(roff*i),srgb.g+Math.floor(goff*i),srgb.b+Math.floor(boff*i));
		document.write("<table width=100% height=1px border=0 cellspacing=0 cellpadding=0 bgcolor=#" + hex + "><tr><td></td></tr></table>");
	}

}


var lastP4Width = -1;
var lastScreenX = -1;

function OnDragbarMouseDown(e) {
	if(!e)e=window.event;
	lastScreenX = e.screenX;
	lastP4Width = $('p4Window').offsetWidth;
	if($('dragBar').setCapture)
		$('dragBar').setCapture();
	else {
		window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
	}
	var flowDiv = $('flowDiv');
	if (!flowDiv) {
		flowDiv = document.createElement('div');
		flowDiv.id = 'flowDiv';
		flowDiv.style.cursor = 'w-resize';
		//flowDiv.style.backgroundColor = "red";
		flowDiv.style.position = 'absolute';
		document.body.appendChild(flowDiv);
		
	}
	else {
		flowDiv.style.display = "block";
	}

	document.onmousemove = function(e1) {
		if(!e1)e1=window.event;
		if (lastScreenX > 0) {
			flowDiv.style.left = 0;
			flowDiv.style.top = 0;
			flowDiv.style.height = document.body.offsetHeight;
			flowDiv.style.width = document.body.offsetWidth;
			
			var dx = lastP4Width -(e1.screenX-lastScreenX);

			if (dx > 0)
			{
				$('p4Window').style.width = dx;
				$('p4Panel').style.width = dx + 14;
			}
			
		}
	};
	document.onmouseup = function() {
		lastScreenX = -1;
		if($('dragBar').releaseCapture)
			$('dragBar').releaseCapture();
		else {
			window.releaseEvents(Event.MOUSEMOVE | Event.MOUSEUP);
		}
		document.onmousemove = null;
		document.onmouseup = null;
		
		$('flowDiv').style.display = "none";
		
		UIRegistry.conversation.ScrollToBottom();
		UIRegistry.inputBox.focus();
	};
}


