var intUI = 0;
function intUniqueIndex() {return ++intUI;}
function strUniqueIndex() {return 'ui_uind_' + intUniqueIndex();}

// **************************************************
// Right click Menus
// **************************************************
var RCMenu_all = new Array();
var RCMenu_delayedhidetimer = -1;
var RCMenuIsFor = null;

// when item is clicked call appropriate handler, specified in custAction attribute for menu item
function RCMenu_ItemClick()
{
	try
	{	
		callbackItemAction(event.srcElement.custAction, RCMenuIsFor);
	}
	catch(e)
	{
		alert('Please add callback handler which looks like callbackItemAction(strAction, Node)');
	}
	RCMenu_Hideall();
	return false;
}

// call this funciton to create menus'
// for example if I want to create menu that has EDIT and DELETE...
// var o = new Menu('menu1');
// o.addItem('edit', 'Edit this file');
// o.addItem('delete', 'Delete this file');
// then I need to add handlres for edit and delete
// oMenuCommands.edit = funciton()
// {
//    alert('this is edit for ' + oMenuIsFor.outerHTML);
// }
// same for delete

function RCMenu()
{
	var divMenu = document.createElement('DIV');
	divMenu.className = 'clsContextMenu';
	divMenu.onmouseleave = RCMenu_Delayedhide;
	divMenu.onmouseover = RCMenu_CancelDelayedhide;
	
	document.body.appendChild(divMenu);
	
	this.div = divMenu;
	
	var oHeader = document.createElement('DIV');
	oHeader.className = 'clsHead';
	oHeader.innerHTML = '-';
	this.div.appendChild(oHeader);
	this.oHeader = oHeader;
	
	var oBody = document.createElement('DIV');
	oBody.className = 'clsBody';
	oBody.innerHTML = '';
	this.div.appendChild(oBody);
	this.oBody = oBody;
	
	
	
	this.addItem = function(action, caption)
	{
		var oA = document.createElement('A');
		oA.href = '#';
		oA.custAction = action;
		oA.onclick = RCMenu_ItemClick;
		oA.innerHTML = caption;
		this.oBody.appendChild(oA);
	}
	
	this.addSeperator = function()
	{
		var oHR = document.createElement('HR');
		this.oBody.appendChild(oHR);
	}

	this.intIndex = RCMenu_all.push(this) - 1;
}

function RCMenu_Show(ind)
{
	RCMenu_Hideall();
	
	var oMenu = RCMenu_all[ind]
	
	var divMenu;
	divMenu = oMenu.div;
	
	divMenu.style.display = 'block';
	divMenu.style.left = event.clientX + document.body.scrollLeft - 10;
	divMenu.style.top = event.clientY + document.body.scrollTop - 10;
	oMenuIsFor = event.srcElement;

	oMenu.oHeader.innerText = oMenuIsFor.innerText;
	
	RCMenuIsFor = event.srcElement;
}

function RCMenu_Delayedhide()
{
	RCMenu_delayedhidetimer = setTimeout('RCMenu_Hideall();', 500);
}

function RCMenu_CancelDelayedhide()
{

	if (RCMenu_delayedhidetimer<0) return;
	clearTimeout(RCMenu_delayedhidetimer);
	RCMenu_delayedhidetimer = -1;
}

function RCMenu_Hideall()
{
	for (var i=0; i<RCMenu_all.length; i++)
		RCMenu_all[i].div.style.display = 'none';
}

// addes context menu to item, item MUST have custMenu specified
function RCMenu_AddContextMenu(o, oMenu)
{
	eval('o.oncontextmenu = function() {RCMenu_Show(' + oMenu.intIndex + '); return false;}');
}



// **************************************************
// Drag and drop function
// **************************************************

var oDnD_Data = null;

// call this function for SPANs or DIVs you want to make draggable
function DnD_MakeDraggable(o)
{
	o.onselectstart = function() {this.dragDrop(); return false;}
	o.ondragstart = function() 
		{
			event.dataTransfer.effectAllowed = 'move';
			event.dataTransfer.dropEffect = 'move'; 
			//event.dataTransfer.setData("Text", this.innerHTML); // uncoment this if you want this droppable to textboxes
			oDnD_Data = this;
			//fnMouseTip(this.innerHTML);
		}
		
	o.ondragend = function() 
		{
			oDnD_Data = null;
			//fnMouseTip('');
		}
}


// call this funciton for the SPAN object you want to accept drops
// strDropFunciton is the name of the method of the oDropCommands that will be called
// for example if I assign Move function (ie Drag_MakeDragtarget(myspan, 'Move'))
// I must create a functio for Move
// oDragCommands.Move = function()
// {
//    this.oSrc is the source object, and this.oDest is this object (one accepting drops) 
// }
function DnD_MakeAcceptDrop(o, strAction)
{
	 o.ondragenter = function() 
		{
			if (oDnD_Data == null) return true; // need only valid data
			if (this == oDnD_Data) return true; // can't drag to itself
			DnD_Hover(this); return false;
		}
		
	 
	 o.ondragleave  = function() 
		{
			if (oDnD_Data == null) return true; // need only valid data
			if (this == oDnD_Data) return true; // can't drag to itself
			DnD_Unhover(this); return false;
		}
		
	o.ondragover  = function() 
		{ 
			if (oDnD_Data == null) return true; // need only valid data
			return false;
		}

	o.ondrop = function() 
		{
			DnD_Unhover(this);
			try
			{
				callback_DnDDrop(oDnD_Data, strAction);
			}
			catch(e)
			{
				alert('specify callback_DnDDrop(oSource, strAction) not found');
			}
		};
}


// change this for behavior... do not uses classes, since using classes might mess up appearance. (different drop objects have different apperance)
function DnD_Hover(o)
{
	if (isNaN(o.original_backgroundColor)) o.original_backgroundColor = o.style.backgroundColor; // save the original on 1st hover
	if (isNaN(o.original_color)) o.original_color=o.style.color; // save the original on 1st hover
	o.style.backgroundColor = 'black';
	o.style.color = 'white';
}

function DnD_Unhover(o)
{
	o.style.backgroundColor = o.original_backgroundColor;
	o.style.color = o.original_color;
}

// **************************************************
// End - Drag and drop function
// **************************************************


// Adds var to form

function subtext_addvar(name, value, bSubmit)
{
	var oForm = document.forms["Form1"];
	var oHidden = oForm.elements[name];
	if (oHidden==undefined)
	{
		oHidden = document.createElement('INPUT');
		oHidden.type = 'hidden';
		oHidden.name = name;
		oHidden.id = name;
		oForm.appendChild(oHidden);
	}
	oHidden.value = value;
	
	if (bSubmit) oForm.submit();
	
}

// **************************************************
// Date picker
// **************************************************

var PD_div = null;
var PD_bodyclick = null;
var PD_dhtid = -1;
var PD_hiddenelements = new Array();


function PD_Delayhide()
{
	PD_dhtid = setTimeout('PD_Hide();', 500);
	
}

function PD_CancelDelayhide()
{
	if (PD_dhtid<0) return;
	clearTimeout(PD_dhtid);PD_dhtid=-1;
}

function PD_Hide()
{
	try
	{
		PD_div.innerHTML = '';
		PD_div.style.display = 'none';
		while(PD_hiddenelements.length>0)
		{
			PD_hiddenelements.pop().style.visibility = '';
			
		}
	}
	catch(e){}
}

function PD_Show(inpid, mc)
{
	window.event.cancelBubble = true;
	
	var tr, td, a, tpart, tbl, s, dt, thisMonth, thisYear, arr;
	var d;
	d = eval(inpid);
	if (mc==undefined)
	{
		PD_Hide();
		if (PD_div==null)
		{
			PD_div = document.createElement('DIV');
			document.body.insertBefore(PD_div, document.body.children[0]);
			PD_div.className = "clsDatePick";
			PD_div.id = 'PD_divPick';
		}
		
		PD_div.style.left = event.clientX-10;
		if (event.clientX-10+PD_div.clientWidth>document.body.clientWidth) 
			PD_div.style.left = document.body.clientWidth - PD_div.clientWidth - 10;
		
		
		PD_div.style.top = event.clientY-10;
		PD_div.style.display = 'block';
		PD_div.style.position = 'absolute';
		PD_div.onmouseleave = PD_Delayhide;
		PD_div.onmouseover = PD_CancelDelayhide;
		
	}
	else
	{
		PD_div = PD_div;
	}
	
	PD_div.innerHTML = '';

	var imc = mc;
	if (imc==undefined) imc = 0;
	
	var dateSel;
	dateSel = new Date(d);
	
	if (isNaN(dateSel)) dateSel = new Date();
	
	var dateFrom = new Date(dateSel);
	dateFrom.setDate(1);
	dateFrom.setMonth(dateFrom.getMonth() + imc);
	thisMonth = dateFrom.getMonth();
	thisYear = dateFrom.getFullYear();
	
	//alert(d + ' | ' + dateSel + ' | ' + dateFrom + ' | ' + thisYear);
	dateFrom.setDate(dateFrom.getDate() - dateFrom.getDay());
		
	// prev, next, month name
	tbl =  document.createElement('TABLE');
	tbl.cellspapcing = tbl.cellpadding = tbl.boder = 0;
	tbl.className = "clsMonthPicker";
	PD_div.appendChild(tbl);
	
	tr = document.createElement('tr');
	tbl.appendChild(tr);
	
	td = document.createElement('td');
	tr.appendChild(td);
	td.align='left';
	a = document.createElement('A');
	td.appendChild(a);
	a.href='#';
	a.onclick='JavaScript: PD_Show("' + inpid + '", ' + (imc-1) + '); return false;';
	a.oncontextmenu='JavaScript: PD_Show("' + inpid + '", ' + (imc-12) + '); return false;';
	a.title = 'Right-click for year, Left for month';
	a.innerText = '<<';;

	td = document.createElement('td');
	tr.appendChild(td);
	td.width = '99%';
	td.align = 'center';
	arr = ('Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec').split(',');
	td.innerText = arr[thisMonth] + ', ' + thisYear;

	td = document.createElement('td');
	tr.appendChild(td);
	td.align='right';
	a = document.createElement('A');
	td.appendChild(a);
	a.href='#';
	a.onclick='JavaScript: PD_Show("' + inpid + '", ' + (imc+1) + '); return false;';
	a.oncontextmenu='JavaScript: PD_Show("' + inpid + '", ' + (imc+12) + '); return false;';
	a.title = 'Right-click for year, Left for month';
	a.innerText = '>>';;
	
	tbl =  document.createElement('TABLE');
	tbl.className = 'clsCalendar';
	tbl.cellspapcing = tbl.cellpadding = tbl.boder =0;
	PD_div.appendChild(tbl);

	tpart = document.createElement('THEAD');
	tbl.appendChild(tpart);
	
	// weekday names
	tr = document.createElement('TR');
	tpart.appendChild(tr);
	
	arr = ('Sun,Mon,Tue,Wed,Thu,Fri,Sat').split(',');
	for(i=0; i<arr.length; i++)
	{
		td = document.createElement('TD');
		tr.appendChild(td);
		td.innerText = arr[i];
	}
	
	tpart = document.createElement('TBODY');
	tbl.appendChild(tpart);		
	
	
	for(i=0;i<42; i++)
	{
		if ((i%7) == 0) 
		{
		
			tr = document.createElement('TR');
			tpart.appendChild(tr);
		}
		
		td = document.createElement('TD');
		tr.appendChild(td);
		
		if (dateFrom.getMonth() != thisMonth)
			td.className = 'clsOtherMonth';
				
		a = document.createElement('A');
		td.appendChild(a);
		a.href='#';
		s = PD_Prefix(dateFrom.getMonth()+1) + '/' + PD_Prefix(dateFrom.getDate()) + '/' + (dateFrom.getFullYear()>100 ? dateFrom.getFullYear() : dateFrom.getFullYear() + 1900).toString();
		dt = new Date(d);

		if (!isNaN(dt))
		{
			s+= ' ' + PD_Prefix((dt.getHours() % 12 ==0) ?  '12' : dt.getHours() % 12) + ':' + PD_Prefix(dt.getMinutes()) + ':' + PD_Prefix(dt.getSeconds()) + ' ' + ((dt.getHours()>=12) ? 'PM' : 'AM');
		}
		
		a.onclick='JavaScript: '+inpid+' ="' + s + '"; PD_Hide(); return false;';
		a.innerText = dateFrom.getDate();
		if (dateFrom.toString() == dateSel.toString())
			td.className = 'clsSelected';
		
		dateFrom.setDate(dateFrom.getDate()+1);
	}

	PD_div.innerHTML += '';
	
	for(i=0;i<document.all.length;i++)
	{
		var el = document.all[i];
		if (el.tagName.toUpperCase()=='SELECT')
		{
			if(el.style.visibility!='hidden')
			{
				{
					el.style.visibility = 'hidden';
					PD_hiddenelements.push(el);
				}
			}
		}
	}
}

function PD_Prefix(i)
{
	if (i>=10) return i.toString();
	else
	return '0'+i;
}
		
// **************************************************
// END - Date picker
// **************************************************


function subtext_Toggle(strID)
{
	var s = '';
	s = (document.all[strID].style.display!='block')?'block':'none';
	document.all[strID].style.display = s;
	var d = new Date();
	d.setFullYear(d.getFullYear()+10);
	document.cookie = strID + '_toggle='+s+'; expires=' + d.toUTCString();
}


function subtext_showHTMLhelp(s)
{
	var wnd = window.open('subtext_help' , '', 'width=400, height=300, scrollbars=yes');
	wnd.document.open();
	wnd.document.write('<html><head><LINK href="subtext.css" type="text/css" rel="stylesheet"></head><body>'+s+'<br><p align=center><input type=button value=Close onclick=self.close() /></p></body></html>');
	wnd.document.close();
}
				