/////////////////////////////////////////////////////////////////////////
//  Author      : Vishwanath Patel
//  Date        : 8-July-2005
//  Decription  : This JS is like a framework code in JavaScript.
//                Developer need not take care of Browser while writting the
//                Client-side code. All browser specific care has beeen tried 
//                to be taken in this file.
//
//  Copyright	: (c) Avani Cimcon, 2001
/////////////////////////////////////////////////////////////////////////


/////START////// Additional functions for Array
Array.prototype.addItem=function(item)
{
	var i=this.length;
	this[i]=item;
	return i;
};
Array.prototype.indexOf=function(value)
{
	for (var i=0;i<this.length;i++)
	{
		if (this[i]==value) return i;
	};
	return-1;
};
/////END////// Additional functions for Array

/////START////// Additional functions for String
String.prototype.startsWith=function(value)
{
	return (this.substr(0,value.length)==value);
};

String.prototype.endsWith=function(value)
{
	var L1=this.length;
	var L2=value.length;
	if (L2>L1) return false;
	return (L2==0||this.substr(L1-L2,L2)==value);
};

String.prototype.remove=function(start,length)
{
	var s="";
	if (start>0)
		s=this.substring(0,start);
	if (start+length<this.length)
		s+=this.substring(start+length,this.length);
	return s;
};

String.prototype.trim=function()
{
	return this.replace(/(^\s*)|(\s*$)/g,"");
};

String.prototype.ltrim=function()
{
	return this.replace(/^\s*/g,"");
};

String.prototype.rtrim=function()
{
	return this.replace(/\s*$/g,"");
};

String.prototype.replaceNewLineChars=function(replacement)
{
	return this.replace(/\n/g,replacement);
}
/////END////// Additional functions for String

/////START////// BROWSER DETECTION.
var __BrowserInfo = new Object();
var sAgent=navigator.userAgent.toLowerCase();
__BrowserInfo.IsIE=sAgent.indexOf("msie")!=-1;
__BrowserInfo.IsGecko=!__BrowserInfo.IsIE;
__BrowserInfo.IsMozila=(navigator.product == "Gecko");
__BrowserInfo.IsNetscape=sAgent.indexOf("netscape")!=-1;
if(__BrowserInfo.IsIE)
{
	__BrowserInfo.MajorVer=navigator.appVersion.match(/MSIE (.)/)[1];
	__BrowserInfo.MinorVer=navigator.appVersion.match(/MSIE .\.(.)/)[1];
}
else
{
	__BrowserInfo.MajorVer=0;
	__BrowserInfo.MinorVer=0;
};
__BrowserInfo.IsIE55OrMore = __BrowserInfo.IsIE && ( __BrowserInfo.MajorVer > 5 || __BrowserInfo.MinorVer>=5);
/////END////// BROWSER DETECTION.

/////START////// DOM DETECTION.
var __sDOMtype = '';
if (document.getElementById)
{
	__sDOMtype = "STD";
}
else if (document.all)
{
	__sDOMtype = "IE4";
}
else if (document.layers)
{
	__sDOMtype = "NS4";
}
/////END////// DOM DETECTION.

var __JS__CMenu ;
///START////// CONTEXT MENU ///////
function ObjContextMenu( sMenuClass )
{
	// Instantiate the class.
	__JS__CMenu = document.createElement( "TABLE" );
	document.body.appendChild( __JS__CMenu );
	__JS__CMenu.id = "__JS_Cntxmnu";
	__JS__CMenu.onmouseover = this.CMenuMouseOver;
	__JS__CMenu.onmouseout = this.CMenuMouseOut;
	__JS__CMenu.className = sMenuClass;
	__JS__CMenu.style.position = "absolute"
	__JS__CMenu.style.visibility = "hidden"
}
ObjContextMenu.prototype.show = __showCMenu;
ObjContextMenu.prototype.hide = __hideCMenu;

ObjContextMenu.prototype.OnMenuItemClick;

ObjContextMenu.prototype.ItemOverClass;
ObjContextMenu.prototype.ItemOutClass;
ObjContextMenu.prototype.className;
ObjContextMenu.prototype.length = 0 ;

ObjContextMenu.prototype.setCoordinates = __setCoordinates;
// Set the menu element which will be used as the context menu.
ObjContextMenu.prototype.setMenuElement = __setMenuElement;
// Alternatively, you may specify the HTML to be rendered as a context menu.
ObjContextMenu.prototype.setHTML = __setCMenuHTML;
// Return the object/element which is being used as the context menu.
ObjContextMenu.prototype.getMenuElement = __getMenuElement;
// Add a menu item to the context menu.
ObjContextMenu.prototype.addMenuItem = __addMenuItem;


ObjContextMenu.prototype.CMenuMouseOut = __CMenuMouseOut;
ObjContextMenu.prototype.CMenuMouseOver = __CMenuMouseOver;

ObjContextMenu.prototype.MenuItemMouseOver = __MenuItemMouseOver;
ObjContextMenu.prototype.MenuItemMouseOut  = __MenuItemMouseOut;

function __CMenuMouseOver()
{
	clearTimeout( __JS__CMenu.timeoutId );
}

function __CMenuMouseOut()
{
	__JS__CMenu.timeoutId = setTimeout( __hideContextMenu, 20);
}
function __hideContextMenu()
{
	__JS__CMenu.style.visibility = "hidden";
}

function __showCMenu()
{
	__JS__CMenu.style.visibility = "visible";
}
function __hideCMenu()
{
	__JS__CMenu.style.visibility = "hidden";
}
function __setCoordinates( oCoords )
{
	__JS__CMenu.style.left = oCoords.left;
	__JS__CMenu.style.top = oCoords.top;
	/*
	__JS__CMenu.style.pixelLeft = nContextLeft ;  
	__JS__CMenu.style.pixelTop = nContextTop;  
	__JS__CMenu.style.visibility = 'visible';  
	*/
}
function __setCMenuHTML( sHtml )
{
	__JS__CMenu.innerHTML = sHtml;
}

function __setMenuElement( oElement )
{
	__JS__CMenu = oElement;
}

function __getMenuElement()
{
	return __JS__CMenu;
}

function __addMenuItem( sMenuItemId, sMenuLabel, nInsertAt )
{
	var oTRCollection;
	var nTotalItems = 0;
	var oTr;

	if( __JS__CMenu.childNodes.length > 0 )
	{
		nTotalItems = parseInt(__JS__CMenu.childNodes[0].childNodes.length );
	}

	
	if( String( nInsertAt ) == "undefined" )
	{
		// Position is undefined then add menu item at end.
		oTr = __JS__CMenu.insertRow( nTotalItems );
	}
	else
	{
		// Position is specified then add menu item at that position.
		oTr = __JS__CMenu.insertRow( nInsertAt );
	}

	var oTd;
	oTd = oTr.insertCell(0);
	oTd.onmouseover = this.MenuItemMouseOver;
	oTd.onmouseout  = this.MenuItemMouseOut;
	oTd.className   = ContextMenu.ItemOutClass;
	oTd.onclick     = ContextMenu.OnMenuItemClick;
	
	if( String( sMenuItemId ) == "undefined" || String( sMenuItemId ).trim() == "" )
	{
		oTd.id = String( "CMenuItem" + this.length );
	}
	else
	{
		oTd.id = sMenuItemId;
	}

	oTd.innerHTML = "<SPAN style=\"width:100%\" >" + sMenuLabel + "</SPAN>";

	this.length += 1;

	return oTd;
}

function __MenuItemMouseOver( eventObj )
{
	this.className = ContextMenu.ItemOverClass;
}
function __MenuItemMouseOut( eventObj )
{
	this.className = ContextMenu.ItemOutClass;
}

// Event handler for the context menu. When the mouse is right-clicked.
function __On_ContextMenu( eventObj )
{
	eventObj = (eventObj) ? eventObj : ( (window.event) ? event : null );
	
	var __JS = new JavaScriptFramework(false);
	var oCoords = __JS.getPageEventCoords( eventObj );
	var obody = document.body;

	if(  parseInt( eventObj.clientX ) > parseInt( obody.clientWidth ) - parseInt( __JS__CMenu.offsetWidth ) )
	{
		oCoords.left  = oCoords.left  - parseInt( __JS__CMenu.offsetWidth ) ; 
	}
	if( parseInt( eventObj.clientY ) > parseInt( obody.clientHeight ) - parseInt( __JS__CMenu.offsetHeight ) )
	{
		oCoords.top  = oCoords.top  - parseInt( __JS__CMenu.offsetHeight ) ; 
	}
	ContextMenu.show();
	ContextMenu.setCoordinates( oCoords );

	__JS.cancelEvent( eventObj );	
	__JS.cancelBubble( eventObj );
}
///END////// CONTEXT MENU ///////


//// JavaScirptFramework class.
function JavaScriptFramework()
{
}

/////////////////////////////////////////// 
// Global variables for JavaScriptFramework.
// PROPERTIES for JavaScirptFramework class.

// to see Browser Info.
JavaScriptFramework.prototype.BrowserInfo = __BrowserInfo;
JavaScriptFramework.prototype.bDebug = false;

////////////////////////////////////
// METHODS JavaScirptFramework class.

// Debug.--> alert
JavaScriptFramework.prototype.Debug = __Debug;

// To get element by Id.
JavaScriptFramework.prototype.getElementById = __getElementById;
// To cancel the bubbling of the event.
JavaScriptFramework.prototype.cancelBubble = __cancelBubble;
// To cancel the default befaviour of the event.
JavaScriptFramework.prototype.cancelEvent = __cancelEvent;

//Added by Urvesh Vekariya.
// To get Key Code of Event.
JavaScriptFramework.prototype.getEventKeyCode = __getEventKeyCode;

// Returns mouse event location in the coordinate plane of the entire document.
JavaScriptFramework.prototype.getPageEventCoords = __getPageEventCoords;
// Handles the proper events to create & use a context sensitive menu.
JavaScriptFramework.prototype.createContextMenu = __createContextMenu;


/////START////// COMMON DOM FUNCTIONS.
// make an array to store cached locations of objects called by getElementById()
var __arrGetElemObjs = new Array();
// function to emulate document.getElementById
function __getElementById( idname, forcefetch )
{
	if( String( forcefetch ) == "undefined" )
	{
		forcefetch = false;
	}

	if (forcefetch || typeof(__arrGetElemObjs[idname]) == "undefined")
	{
		switch (__sDOMtype)
		{
			case "STD":
			{
				__arrGetElemObjs[idname] = document.getElementById(idname);
			}
			break;

			case "IE4":
			{
				__arrGetElemObjs[idname] = document.all[idname];
			}
			break;

			case "NS4":
			{
				__arrGetElemObjs[idname] = document.layers[idname];
			}
			break;
		}
	}
	return __arrGetElemObjs[idname];
}

function __cancelBubble( eventobj )
{
	if( __BrowserInfo.IsMozila )
	{
		eventobj.stopPropagation();
		this.Debug( "Event Bubble Cancelled." );
		return eventobj;
	}
	else if( __BrowserInfo.IsIE ) 
	{
		if( window.event != null )
		{
			//window.event.returnValue = false;
			window.event.cancelBubble = true;
			this.Debug( "Event Bubble Cancelled." );
			return window.event;
		}
		else
		{
			return null;
		}
	}
}

function __getEventKeyCode (eventobj)
{	
	var _sKeyCode;
	if( __BrowserInfo.IsMozila )
	{
		_sKeyCode = eventobj.which;
	}
	else if( __BrowserInfo.IsIE ) 
	{
		if( window.event != null )
		{
			_sKeyCode = eventobj.keyCode;
		}
		else
		{
			return null;
		}
	}
	return _sKeyCode;
}
function __cancelEvent( eventobj )
{
	if( __BrowserInfo.IsMozila )
	{
		eventobj.preventDefault();
		this.Debug( "Default Event Cancelled." );
		return eventobj;
	}
	else if( __BrowserInfo.IsIE ) 
	{
		if( window.event != null )
		{
			window.event.returnValue = false;
			this.Debug( "Default Event Cancelled." );
			return window.event;
		}
		else
		{
			return null;
		}
	}
}

function __getPageEventCoords( eventobj )
{
	eventobj = (eventobj) ? eventobj : ( (window.event) ? event : null );

	var coords = {left:0, top:0};

	if( eventobj.pageX )
	{
		coords.left = eventobj.pageX;
		coords.top = eventobj.pageY;
	}
	else
	{
		coords.left = eventobj.clientX + document.body.scrollLeft - document.body.clientLeft;
		coords.top  = eventobj.clientY + document.body.scrollTop - document.body.clientTop;
		// Include HTML element space if necessary.
		if( document.body.parentElement && document.body.parentElement.clientLeft )
		{
			var bodyParent = document.body.parentElement;
			coords.left += bodyParent.scrollLeft - bodyParent.clientLeft;
			coords.top  += bodyParent.scrollTop - bodyParent.clientTop;
		}
	}

	return coords;
}



function __createContextMenu( sClassName )
{
	// sClassName

	var bCanCreate = false;
	switch (__sDOMtype)
	{
		case "STD":
			{
				this.Debug( "Creating the Context Menu" )
				document.oncontextmenu = __On_ContextMenu;
				bCanCreate = true;
			}
			break;

		case "IE4":
			{
				this.Debug( "Creating the Context Menu" )
				document.oncontextmenu = __On_ContextMenu;
				bCanCreate = true;
			}
			break;

		case "NS4":
			{
				this.Debug( "Creating the Context Menu" )
				document.captureEvents(Event.MOUSEDOWN);  
				document.onmousedown = __On_ContextMenu;
				bCanCreate = true;
			}
			break;

		default :
			alert( "Could not create the Context Menu for your browser." )
			break;
	}
	if( bCanCreate )
	{
		ContextMenu =  new ObjContextMenu( sClassName );
		return ContextMenu;
	}
	return null;
}

function __getSrcElement( eventObj )
{
	eventObj = (eventObj) ? eventObj : ( (window.event) ? event : null );
	return ( eventObj.target ) ? eventObj.target : ( ( eventObj.srcElement ) ? eventObj.srcElement : null );
}

//////////////////////////////////
function __Debug( sStr )
{
	if( this.bDebug )
		alert( sStr );
}

/////END////// COMMON DOM FUNCTIONS.


