//*****************************************************************************
// Alloy Navigator 5
//
// Copyright (c) 2002-2007 Alloy Software Inc.
// All Right Reserved
//
// The entire contents of this file is protected by U.S. and
// International Copyright Laws. Unauthorized reproduction, 
// reverse-engineering, and distribution of all or any portion of 
// the code contained in this file is strictly prohibited and may 
// result in severe civil and criminal penalties and will be 
// prosecuted to the maximum extent possible under the law. 
//
// RESTRICTIONS 
// 
// The source code contained within this file and all related 
// files or any portion of its contents shall at no time be 
// copied, transferred, sold, distributed, or otherwise made 
// available to other individuals without express written consent 
// and permission from Alloy Software Inc. 
//
var ERROR = 0;
var WARNING = 1;
var CONFIRM = 2;
var INFO = 3;
var PRM_IMAGES_PATH = 'pages/images/'; 
var LC_GLOBAL_TEXT_INFO ='Info';
var LC_GLOBAL_TEXT_ERROR ='Error';
var LC_GLOBAL_TEXT_CONFIRM = 'Confirm';
var LC_BUTTON_YES = 'Yes';
var LC_BUTTON_NO = 'No';
var LC_GLOBAL_TEXT_WARNING = '';
var LC_BUTTON_CANCEL = 'Cancel';

function isIE()
{
	return (navigator.userAgent.toUpperCase().indexOf('MSIE')!=-1); // && (navigator.userAgent.toUpperCase().indexOf('MSIE 7')==-1) ;
}

function isIE7()
{
	return (navigator.userAgent.toUpperCase().indexOf('MSIE 7')!=-1); // && (navigator.userAgent.toUpperCase().indexOf('MSIE 7')==-1) ;
}

MsgBox = function()
{
	this.visible = false;
  this.left = 0;
  this.right = 0;
  this.startX = 0;
  this.startY = 0;
  this.moving = false;
  this.caption = 'msg caption';
  this.msg = 'msg';
	this.buttons = null;
	this.reactions = null;
	this.canClose = false;
	this.msgType = 0;
	this.body = null;
	this.activeContent = null;
	this.evalActiveContent = false;
	
	this.getAbsolutePos = function(el) 
	{
		var SL = 0, ST = 0;
		var is_div = /^div$/i.test(el.tagName);
		if (is_div && el.scrollLeft)
			SL = el.scrollLeft;
		if (is_div && el.scrollTop)
			ST = el.scrollTop;
		var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
		if (el.offsetParent) {
			var tmp = this.getAbsolutePos(el.offsetParent);
			r.x += tmp.x;
			r.y += tmp.y;
		}
		return r;
	}
  
	this.mouseUp = function()
	{
		this.moving = false;
	}
		
	this.mouseDown = function(ev)
	{
	//	window.status = 'down';
		this.moving = true;
		var evnt = isIE() ? event : ev;
		{	this.startX = evnt.clientX;
			this.startY = evnt.clientY;
			if (document.getElementById('msg_main'))
			{	this.left = this.getAbsolutePos(document.getElementById('msg_main')).x;
				this.top = this.getAbsolutePos(document.getElementById('msg_main')).y;
			//	window.status = 'down coords:'+this.startX +';'+this.startY + 'msg: '+this.left+ ';'+this.top +' moving='+this.moving;
			}
		}
	}
	
	this.mouseMove = function(ev)
	{
		var evnt = isIE() ? event : ev;
		if (this.moving)
		{// window.status = 'moving';
		  var newX = this.left +evnt.clientX - this.startX;
			var newY = this.top + evnt.clientY - this.startY;
			this.startX = evnt.clientX;
			this.startY = evnt.clientY;
		//	window.status = 'move from:'+ this.left + ';' + this.top + ' to ' + newX +';' + newY;
			document.getElementById('msg_main').style.left = newX + 3 + 'px';
			document.getElementById('msg_main').style.top = newY + 3 + 'px';
			document.getElementById('msgbody').style.left = newX + 'px';
			document.getElementById('msgbody').style.top = newY + 'px';
			this.left = newX;
			this.top = newY;	
			//document.getElementById('bdysize').innerHTML='b:('+this.body.style.left+','+this.body.style.top+')('+this.body.clientWidth+','+this.body.clientHeight+')';
			//document.getElementById('shsize').innerHTML='s:('+this.shadow.style.left+','+this.shadow.style.top+')('+this.shadow.clientWidth+','+this.shadow.clientHeight+')';
		}
	//	else
	//		window.status = 'moving off';
	}
	
	this.close = function()
	{
		if (this.doc.getElementById('defence'))
			this.doc.body.removeChild(this.doc.getElementById('defence'));
		if (this.doc.getElementById('msg_main'))
			this.doc.body.removeChild(this.doc.getElementById('msg_main'));
		if (this.doc.getElementById('msgbody'))
			this.doc.body.removeChild(this.doc.getElementById('msgbody'));
	}
		
	this.createBody = function()
	{
		var shadow = this.doc.createElement('DIV');
		shadow.className='msgbox';
		shadow.id = 'msg_main';
		shadow.innerHTML = '<!--[if lte IE 6.5]><iframe></iframe><![endif]-->';
		var main = this.doc.createElement('DIV');
		main.className = 'msgbase';
		main.id ='msgbody';
		main.style.position='absolute';
		main.innerHTML = '<!--[if lte IE 6.5]><iframe></iframe><![endif]-->';
		main.setAttribute('unselectable','on');
						
		var caption = this.doc.createElement('DIV');
		caption.className = 'msg_caption';
		caption.id = 'msgcaption';
		this.addEvent(caption,'mousemove',msgMouseMove);
		this.addEvent(caption,'mousedown',msgMouseDown);
		this.addEvent(main,'mouseup',msgMouseUp);
		this.addEvent(main, 'mousemove',msgMouseMove);
		
		var tbl = this.doc.createElement('TABLE');
		tbl.className = 'msg_caption_table';
		var r = tbl.insertRow(0);
		cell = r.insertCell(0);
		cell.className='caption_text';
		cell.innerHTML = this.caption;// + '&nbsp;<span id=bdysize></span>&nbsp;<span id=shsize></span>';
		
		if (this.canClose)
		{
			cell = r.insertCell(1);
			cell.className = 'caption_close';
			var btn = this.doc.createElement('INPUT');
			btn.type='button';
			btn.value='X';
			btn.onclick = msgClose;
			cell.appendChild(btn);
		}	
		caption.appendChild(tbl); 
		main.appendChild(caption);
		
		var bdy = this.doc.createElement('DIV');
		bdy.className = 'msg_body';
		bdy.id='msgcontent';
		tbl = this.doc.createElement('TABLE');
		bdy.appendChild(tbl);
		tbl.className = 'msg_body_table';
		r = tbl.insertRow(0);
		cell = r.insertCell(0);
		cell.className = 'body_icon';
		switch (this.msgType)
		{	case ERROR: cell.innerHTML='<IMG SRC="'+PRM_IMAGES_PATH +'error.ico" />'; break;
			case WARNING: cell.innerHTML='<IMG SRC="'+PRM_IMAGES_PATH +'warning.ico" />'; break;
			case CONFIRM: cell.innerHTML='<IMG SRC="'+PRM_IMAGES_PATH +'confirm.ico" />'; break;
			default: cell.innerHTML='<IMG SRC="'+PRM_IMAGES_PATH +'info.ico" />';
		}	
		cell = r.insertCell(1);
		cell.className = 'body_text';
		cell.innerHTML = this.msg;
		if (this.activeContent!=null)
		{ r = tbl.insertRow(-1);
		  cell = r.insertCell(-1);
		  cell = r.insertCell(-1);
		  cell.id='msg_active_cell';
		  if (this.evalActiveContent!=true)
		    cell.innerHTML = this.activeContent;
		}
		bdy.scrollWifth = '10px';
		main.appendChild(bdy);
				
		var foot = this.doc.createElement('DIV');
		foot.className = 'msg_foot';
		foot.id='msgfoot';
		tbl = this.doc.createElement('TABLE');
		r = tbl.insertRow(0);
		tbl.setAttribute('celspasing','5');
		for (var i=0;i<this.buttons.length;i++)
		{
			var btn = this.doc.createElement('INPUT');
			btn.type='button';
			btn.value = this.buttons[i];
			btn.name= 'btn_'+i;
			btn.id='btn_' + i + '_id';
			btn.className='button';
			this.addEvent(btn,'click',msgReaction);
			cell = r.insertCell(i);
			cell.className= i==0 ? 'foot_first_cell' : 'foot_cell';
			cell.appendChild(btn);
			if (i==0)
			{ this.defaultButton = btn }
		}
		cell.className = this.buttons.length==1 ? 'foot_cell' : 'foot_last_cell';
		foot.appendChild(tbl);
	//	var tbl = document.createElement('DIV');
	//	tbl.innerHTML = navigator.userAgent;
	//	foot.appendChild(tbl);
		main.appendChild(foot);
	  this.body = main;
	  this.shadow = shadow;
	  return;
	}
	
	this.show = function(msgType, msg, caption,  buttons, reactions, canClose, doc)
	{
	  this.showEx(msgType, msg, caption,  buttons, reactions, canClose, doc, null,false);
	}
	
	this.showEx = function(msgType, msg, caption,  buttons, reactions, canClose, doc,aCont,needEvalCont)
	{
		if (!this.visible)
		{
			this.msg = msg;
			this.caption = caption;
			this.buttons = buttons;
			this.reactions = reactions;
			this.canClose = canClose;
			this.msgType = msgType;
			this.doc = doc;
			this.activeContent = aCont;
			this.evalActiveContent = needEvalCont;
			this.createBody();
			this.moving = false;
			this.startX = 0;
			this.startY = 0;
			this.left = 0;
			this.top = 0;
	    this.bg = doc.createElement('DIV');
			this.bg.className='back';
			this.bg.id='defence';
			this.addEvent(this.bg,'mouseup',msgMouseUp);
			this.addEvent(this.bg,'mousemove',msgMouseMove);
			
			if (doc.body)
			{	this.bg.style.height=doc.body.clientHeight +'px';
				doc.body.appendChild(this.bg);
				this.bg.appendChild(this.shadow);
				this.bg.appendChild(this.body);
				if ((this.activeContent!=null) && (this.evalActiveContent==true))
				  eval(this.activeContent);
		  	var h ;
				if (isIE())
				{	this.msg = this.msg + '(isIE)';
				  this.body.style.left = Math.floor((doc.body.clientWidth - 450) / 2)  + 'px';
					this.body.style.top = Math.floor((doc.body.clientHeight - 150) / 2) + 'px';
					this.shadow.style.left = Math.floor((doc.body.clientWidth - 450) / 2) + 3 + 'px';
					this.shadow.style.top = Math.floor((doc.body.clientHeight - 150) / 2) + 3 + 'px';
					if (isIE7())
					{ h = Math.max(this.body.scrollHeight,120); }
					else
					{ var r = this.body.getBoundingClientRect();
					  h = Math.max(r.bottom - r.top,120); }
					
				}
				else
				{	this.msg = this.msg + '(not isIE) ';
				  this.body.style.left = Math.floor((doc.documentElement.clientWidth - 450) / 2)  + 'px';
					this.body.style.top = Math.floor((doc.documentElement.clientHeight - 150) / 2) + 'px';
					this.shadow.style.left = Math.floor((doc.documentElement.clientWidth - 450) / 2) + 3 + 'px';
					this.shadow.style.top = Math.floor((doc.documentElement.clientHeight - 150) / 2) + 3 + 'px';
					h = Math.max( doc.getElementById('msgcaption').clientHeight + doc.getElementById('msgcontent').clientHeight + doc.getElementById('msgfoot').clientHeight,120);
				}
				this.body.style.height= h +'px';
				h +=3;
				this.shadow.style.height = h + 'px';
			}
			else
			{ this.shadow.style.height = this.body.clientHeight + 3 + 'px';}
		}
		this.body.focus();
	  
	  if (this.defaultButton)
	  { this.defaultButton.tabindex=0; 
	    this.defaultButton.focus();
	    this.body.scrollTop = 0;
	  }
	}

  this.disableButton = function(indx, bDisabled)
 	  {
	    var btn = this.doc.getElementById('btn_' + indx + '_id');
	    if (btn)
	      btn.disabled = bDisabled;    
	  }

	
	this.addEvent = function(el, evname, func) {
		if (el.attachEvent) { // IE
			el.attachEvent("on" + evname, func);
		} else if (el.addEventListener) { // Gecko / W3C
			el.addEventListener(evname, func, true);
		} else {
			el["on" + evname] = func;
		}
	}
	
	this.setActiveContent = function(ac)
	{
	  this.activeContent = ac;
	}
}


function addEvent(el, evname, func) 
{
  if (!el)
    return;
  if (el.attachEvent) { // IE
	 el.attachEvent("on" + evname, func);
	} 
	else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(evname, func, true);
	} 
	else {
	 el["on" + evname] = func;
	}
}

function removeEvent(el, evname, func) 
{
	if (el.detachEvent) { // IE
		el.detachEvent("on" + evname, func);
	} else if (el.removeEventListener) { // Gecko / W3C
		el.removeEventListener(evname, func, true);
	} else {
		el["on" + evname] = null;
	}
};

function stopEvent(ev) 
{
	ev || (ev = window.event);
	if (isIE()) 
	{
		ev.cancelBubble = true;
		ev.returnValue = false;
	} 
	else 
	{
		ev.preventDefault();
		ev.stopPropagation();
	}
	return false;
};

function msgMouseMove(ev)
{
	var msg = window.msgbox;
	if (msg)
	{msg.mouseMove(ev)}
}

function msgMouseDown(ev)
{
	var msg = window.msgbox;
	if (msg)
	{msg.mouseDown(ev)}
}

function msgMouseUp(ev)
{
	var msg = window.msgbox;
	if (msg)
	{msg.mouseUp(ev)}
}

function msgReaction(ev)
{
	var msg = window.msgbox;
	if (msg)
	{	var el = null;
	  if (isIE())
	  { if (event)
				el = event.srcElement; 
			else
				el = ev.srcElement;
		}
		else
			el = ev.target;
		var en = el.name;
		var i = parseInt(en.split('_',2)[1]);
	  eval(msg.reactions[i]);
		msg.close();
	}
}

function msgClose()
{
	var msg = window.msgbox;
	if (msg)
	{	msg.close();
	  window.msgbox=null;}
}

function showMessageBox(msgType, txt, caption, buttons, reactions, canClose, doc)
{
  showMessageBoxEx(msgType, txt, caption, buttons, reactions, canClose, doc, null,false)
}

function showMessageBoxEx(msgType, txt, caption, buttons, reactions, canClose, doc, aCont,needEvalCont)
{
	if (window.msgbox)
		msgClose();
	window.msgbox = new MsgBox();
	window.msgbox.showEx(msgType, txt, caption, buttons, reactions, canClose, doc, aCont,needEvalCont);
}

//  ---------- Different message types --------------------

function showInfo(txt)
{
	showMessageBox(INFO, txt, LC_GLOBAL_TEXT_INFO,  ['  OK  '], [""], false, document);
}

function showError(err)
{
	 showMessageBox(ERROR, err, LC_GLOBAL_TEXT_ERROR,  ['  OK  '], [""], false, document);
}

function showErrorEx(err,obj)
{
	if (obj.ownerDocument)
	{	showMessageBox(ERROR, err, LC_GLOBAL_TEXT_ERROR,  ['  OK  '], [""], false , obj.ownerDocument);}
	else
	{	showMessageBox(ERROR, err, LC_GLOBAL_TEXT_ERROR,  ['  OK  '], [""], false , document);}
}


function showConfirm(txt, btns, reactions)
{
	showMessageBox(CONFIRM, txt, LC_GLOBAL_TEXT_CONFIRM,  btns, reactions, false, document);
}

function showConfirmYesNo(txt, reactions)
{
	showMessageBox(CONFIRM, txt, LC_GLOBAL_TEXT_CONFIRM,  [LC_BUTTON_YES,LC_BUTTON_NO], reactions, false, document);
}

function showWarning(txt, btns, reactions)
{
	showMessageBox(WARNING, txt, LC_GLOBAL_TEXT_WARNING,  btns, reactions, false, document);
}




