
var isdrag=false;
var x,y;
var dobj;
function $( s ) { return document.getElementById( s ); }
function ce( t ) { return document.createElement( t ); }
function v(){}; // void function
function insertAfter(node, referenceNode)
{
  referenceNode.parentNode.insertBefore(node, referenceNode.nextSibling);
}
function isdefined( variable)
{
    return (typeof(variable) == "undefined")?  false: true;
}
function isnumber( variable)
{
    return (typeof(variable) == "number")?  false: true;
}

function val( o )
{
	var vo = o*1;
	return vo+'' == 'NaN' ? 0 : vo;
}

function redirect(pmUrl)
{
	window.location.href=pmUrl;
}
function redirectPage(pmUrl,pmTabSelected)
{
	if(pmTabSelected)
	{
		setCookie('tabSelected', pmTabSelected);
	}
	else
		delCookie('tabSelected');
		
	if(pmUrl.match(/cmp_schools\/web\/index.php/g))
	{
		pmUrl += "?hnd="+Math.random();
	}
	window.location.href=siteName+pmUrl;
}
function inspect( dobj )
{
	win = window.open( '', 'inspect' );
	for (var i in dobj)	
	{ 
		var j = eval( 'dobj.' + i );
		win.document.write( i + ', ' + typeof( j )+ ', ' +  j + '<br>' );
//		debug( i, typeof( j ), j );
	}
}
function addOption(selectbox,text,value )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}
var debugdisplay = false;
function debugclose()
{
	var _log = $('log');
	_log.parentNode.removeChild( _log );
	_log = null;
}

function debug()
{
	var i;
	var list = [];

	if( !debugdisplay )
	{ 
		alert( 'debug wait' );
	}
	if( $('log') == null )
	{
		log = ce( 'div' );
		log.id = 'log';
		log.className = 'debug';
		log.innerHTML = '<nobr><b>Debug Window</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:debugclose()">close</a></nobr>';
		document.body.appendChild( log );
	}
	for( i = 0 ; i < arguments.length ; i++ )
	{
		list[i] = arguments[i];
	}
	$('log').innerHTML += '<br>' + list.join( ', ' );
	$('log').style.zIndex = 1000000;
	$('log').style.top = document.body.scrollTop + 10;
}
function out( s )
{
	outstr += s;
}

function outflush( o )
{
	o.innerHTML += outstr;
	outstr = '';
}

var outstr = '';


function movemouse(e)
{
	var x, y, ox, oy, scol, sitem, item;
	var i, target, c;
	if (isdrag)
	{
		scol = -1;
		x = nn6 ? e.clientX : event.clientX;
		y = nn6 ? e.clientY : event.clientY;
//		y += $('header').clientHeight;
		dobj.style.left = ox = dobj.tx + x - dobj.x;
		dobj.style.top  = oy = dobj.ty + y - dobj.y;
		ox += dobj.clientWidth/2;
		oy += dobj.clientHeight/2 - $('header').clientHeight;;
		target = $('tmp');
		//get the closest position
		for( i = 0 ; i < ncols ; i++ )
		{
			c = $('col'+i);
			if( ox >= c.left && ox <= (c.left+c.w) )
			{
				scol = i;
				break;
			}
		}
		if( scol == -1 ) return;
		if( dobj.dragable == c_dragsamecol && scol != dobj.col )
		{
			$('_h_' + dobj.id).style.cursor = 'not-allowed';
			return;
		}
		$('_h_' + dobj.id).style.cursor = 'move';
		c = $('col'+scol);
		sitem = -1;
		if( c.ntops && oy < c.tops[0] ) sitem = 0;
		else if( oy > c.tops[c.ntops] )
		{
			sitem = -1;
		}
		else
		{
			for( i = 0 ; i < c.ntops ; i++ )
			{
				if( oy >= c.tops[i] && oy <= c.tops[i+1] )
				{
					sitem = i;
					break;
				}
			}
		}
		// debug( 'col', scol, c.ntops, 'item', sitem );
		if( sitem == -1 )
		{
			target.style.width = c.w;
			target.parentNode.removeChild(target);
			c.appendChild( target );
			getslotpos();
		}
		else
		{
			item = c.childNodes[sitem];
			if( item != target && item.dragable != c_nodrag )
			{
				target.style.width = c.w;
				target.parentNode.removeChild( target );
				c.insertBefore( target, item );
				getslotpos();
			}
		}
		return false;
	}
}

function selectmouse(e) 
{
  /*
  var fobj       = nn6 ? e.target : event.srcElement;
  var topelement = nn6 ? "HTML" : "BODY";
  var parent;

  if( fobj.tagName == 'A' ) return;
 
 while (fobj.tagName != topelement && fobj.className != "winhead")
  {
    fobj = nn6 ? fobj.parentNode : fobj.parentElement;
  }


  if (fobj.className=="winhead" && fobj.parentNode.dragable != c_nodrag )
  {
    getslotpos();
	dobj = fobj.parentNode;
  	dobj.oldposition = dobj.style.position;
  	dobj.oldz = dobj.style.zIndex;

	if( dobj.oldposition != 'absolute' ) dobj.style.position = 'relative';
	dobj.style.position = 'absolute';
    dobj.tx = dobj.left;
    dobj.ty = dobj.top;
    dobj.x = nn6 ? e.clientX : event.clientX;
    dobj.y = nn6 ? e.clientY : event.clientY;
	dobj.style.zIndex = 1000;
	parent = dobj.parentNode;
    document.onmousemove=movemouse;

	var tmp = ce( 'div' );
	tmp.id = 'tmp';
	tmp.className = 'tmp';
	tmp.zIndex = 1;
	tmp.style.position = 'relative';
	tmp.style.top = dobj.style.top;
	tmp.style.left = dobj.style.left;
	tmp.style.width = dobj.clientWidth;
	tmp.style.height = dobj.clientHeight;
	parent.replaceChild( tmp, dobj );
	document.body.appendChild( dobj );

	dobj.style.left = dobj.left;
	dobj.style.top = dobj.top;

    isdrag = true;
    return false;
  }
  */
}

function stopdrag()
{
	var target;
	if( isdrag )
	{
		isdrag=false;
		dobj.style.position = dobj.oldposition;
		dobj.style.zIndex = dobj.oldz;
		document.onmousemove = '';
		if( dobj.oldposition != 'absolute' )
		{
//			dobj.style.left = 5;
//			dobj.style.top = 5;
			dobj.style.left = 0;
			dobj.style.top = 0;
		}
		target = $('tmp');
		dobj.style.width = target.style.width;
		if( target != null ) target.parentNode.replaceChild( dobj, target );
	}
}
function shadow( w, h )
{
	var w2 = w-90;
	var h2 = h-90;
	out( '<div style="position:relative;width:'+w+'px;height:'+h+'px">' + 
			png( 'tool/' + ctable + '/sdw1.png', 45, 45, 'position:absolute;left:0px;top:0px' ) +
			png( 'tool/' + ctable + '/sdw2.png', w2, 45, 'position:absolute;left:45px;top:0px' ) +
			png( 'tool/' + ctable + '/sdw3.png', 45, 45, 'position:absolute;right:0px;top:0px' ) +
			png( 'tool/' + ctable + '/sdw4.png', 45, h2, 'position:absolute;right:0px;top:45px' ) +
			png( 'tool/' + ctable + '/sdw5.png', 45, 45, 'position:absolute;right:0px;bottom:0px' ) +
			png( 'tool/' + ctable + '/sdw6.png', w2, 45, 'position:absolute;left:45px;bottom:0px' ) +
			png( 'tool/' + ctable + '/sdw7.png', 45, 45, 'position:absolute;left:0px;bottom:0px' ) +
			png( 'tool/' + ctable + '/sdw8.png', 45, h2, 'position:absolute;left:0px;top:45px' ) +
    '</div>' );
}

function whead( t, v )
{
	if( t.parentNode.dragable == c_nodrag ) return;
	if( v )
		t.style.cursor = 'move';
	else
		t.style.cursor = 'auto';
}

function png( src, w, h, s )
{
	if( nn6 )
		return '<img src="' + src + '" width=' + w + ' height=' + h + ' border=0 alt="" style="' + s + '">';
	else
		return '<div style="' + s + ';width:' + w + ';height:' + h + ";filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')" + '"></div>';
}

function drawwin(wid, id, title, url, funk, meth)
{
	outstr = '';
	out( '<div id="_h_'+ id + '" class=winhead onmouseover="whead(this,1)" onmouseout="whead(this,0)"><table border=0 width=100% cellpadding=0 cellspacing=0><tr><td width=10>' + png( 'tool/' + ctable + '/w1.png', 10, 17, 'position:relative;left:0px;top:0px' ) + '</td><td background=tool/' + ctable + '/w2.png class=wintit>'+ title + '</td><td width=10>' + png( 'tool/' + ctable + '/w3.png', 10, 17, '' ) + '</table></div>' );
	out( '<div id="_b_'+ id + '" class="winclass"></div>' );
//	out( '<img src="tool/' + ctable + '/esp.gif" width=5 height=5 border=0>' );
	wid.innerHTML = outstr;
	if( !nn6 ) $('_b_'+id).style.top = -2;
}

function slotwin()
{
	var w2, h2;
	var offs = nn6 ? 6 : 4;
	var wid = ce( 'div' );
	var parent;
	var id, title, column, dragflag, url, func, meth, i;

	// default values
	dragflag = c_nodrag;
	func = 'resulttxt';
	meth = 0;

	i = 0;
	id = arguments[i++];
	title = arguments[i++];
	column = arguments[i++];
	url = arguments[i++];
	if( arguments.length > i )	dragflag = arguments[i]; i++;
	if( arguments.length > i )	func = arguments[i]; i++;
	if( arguments.length > i )	meth = arguments[i]; i++;

	parent = $('col'+column);
	wid.id = id;
	wid.className = '';
	wid.style.display = 'block';
	wid.style.position = 'relative';
	wid.dragable = dragflag;
	wid.col = column;
	w2 = parent.clientWidth;
	
	wid.style.width = w2;
	parent.appendChild( wid );

	url = nocache( url ); // no caching
	if( title != '' )
	{
		wid.className = 'wincontent';
		drawwin( wid, id, title, url, func, meth );
		$('_b_'+id).innerHTML = '<p>Loading...</p>';
		
		ajaxrequest( url, func, $('_b_'+id), meth);
	}
	else
	{
		ajaxrequest( url, func, wid, meth);
	}
	getslotpos();
}

var ncols = 4;

function canvas()
{
	var c = $('divcanvas');
	if( c == null ) c = document.body;
	return c;
}

function createcanvas()
{	
	document.body.innerHTML = '<div id="divcanvas"><div id="header" class="canvas"></div><div id="main" class="canvas"></div><div id="footer" class="footer"></div></div>';
	ajaxrequest( 'footer.html', 'resulttxt', $('footer'), 0);
}

function gettopleft( o )
{
	var oleft = 0; otop = 0;
	var pos = new Object;

	while( o != document.body )
	{
//		debug( o.id, o.tagName, o.className, o.offsetTop, o.offsetLeft, 'otop', otop, 'oleft', oleft );
		if( isdefined( o.offsetTop ) && isdefined( o.tagName ) && o.tagName != 'TR' && o.tagName != 'TD' ) otop += o.offsetTop;
		if( isdefined( o.offsetLeft ) && isdefined( o.tagName ) && o.tagName != 'TR' && o.tagName != 'TD' ) oleft += o.offsetLeft;
		o = o.parentNode;
	}
	pos.top = otop;
	pos.left = oleft;
	return pos;
}

var popupwin = null;

function popupresult( txt, obj )
{
	var pop = popupwin;
	var st, sb;
	var i;

	if ( pop == null ) return;
	resultform( txt, obj );
	popupresize();
	var imglist = obj.getElementsByTagName("IMG");
	for( i = 0 ; i < imglist.length ; i++ )
	{
		if( isdefined( imglist[i].onload ) && imglist[i].onload != null )
		{
			imglist[i].onload2 = imglist[i].onload;
			imglist[i].onload = function() { this.onload2(); this.style.display = 'block'; popupresize(); };
		}
		else
			imglist[i].onload = function() { this.style.display = 'block'; popupresize(); };
	}
	st = pop.offsetTop - document.body.scrollTop;
	sb =  (document.body.scrollTop+document.body.clientHeight) - (pop.offsetTop + pop.clientHeight);
	if( st < 0 ) scrollBy( 0, st-10 );
	else if( sb < 0 ) scrollBy( 0, -sb + 10 );
}

function maxwid()
{
//	return document.body.clientWidth - 20;
	return 968;
	return 862;
}

var myloc;
//# - This function used to change proper case - added by jeyaseelan on 06/06/07
String.prototype.toProperCase = function(){
     return this.toLowerCase().replace(/\w+/g,function(s){
          return s.charAt(0).toUpperCase() + s.substr(1);
     })
};

function erasescripts( o )
{
	var i, bc;
	if( !isdefined(o.tagName) || !isdefined(o.childNodes)) return;

	var cells = o.getElementsByTagName("script");
	for( i = cells.length-1 ; i >= 0 ; i-- )
	{
		cells[i].parentNode.removeChild( cells[i] );
	}
}

var loginname = '';
var navbarurl = '';
function initpage( url, login , pageName, pmValue)
{
	var i, rb, b;
	pageName = pageName + "[search]" + pmValue;
//	alert(pageName)
	//strip scripts from body to copy
	erasescripts( document.body );
	var bodyhtml = document.body.innerHTML;
	createcanvas();
	nb = ce('div');
	nb.id = 'navbar';
	nb.className = 'navbar';
	loginname = login;
	$('header').appendChild( nb );
	addmainhtml( bodyhtml );
	var tmp = 'rpar=' + escape(login);
	navbarurl = url + '?rpar=' + escape(login);

	navtxt = getCookie( navbarurl );
	if( navtxt == '' )
		ajaxrequest( navbarurl, 'resultnavbar', pageName, 1 );
	else
	{
		var navxml = parsexml( navtxt );
		resultnavbar( navxml, pageName, navtxt );
	}
	document.body.style.display = 'block';
	buttonizeall( document.body );
}

function initpagenew( url, login , pageName, pmValue)
{
	var i, rb, b;
	document.write( '</div>' );
	document.write( '<div id="main" class="canvas">' );

	pageName = pageName + "[search]" + pmValue;
	loginname = login;
	var tmp = 'rpar=' + escape(login);
	navbarurl = url + '?rpar=' + escape(login);

	navtxt = getCookie( navbarurl );
	if( navtxt == '' )
		ajaxrequest( navbarurl, 'resultnavbar', pageName, 1 );
	else
	{
		var navxml = parsexml( navtxt );
		resultnavbar( navxml, pageName, navtxt );
	}
	document.body.style.display = 'block';
	$('divcanvas').style.position = 'relative';
	var left = (document.body.clientWidth - maxwid() - 20 )/2;
	if( left > 0 ) $('divcanvas').style.left = left;
	debugdisplay = true;
}



function createslots( args )
{
	var cw, mw = maxwid();
	var i, s, c, l, j;
	var wlist = [];
	var slottop;
	var out = '';
	ncols = 0;

	slottop = 0;
	for( i = 0, s = 0 ; i < args.length ; i++, ncols++ )
		if( args[i] >= 0 ) s += args[i];
	for( i = 0, j = 0 ; i < args.length ; i++, j++ )
	{
		if( args[i] < 0 ) wlist[j] = (mw-s)/(-args[i]);
		else wlist[j] = args[i];
	}
	l = 0;

	out = '<table width=' + mw + ' cellpadding=0 cellspacing=0 border=0><tr>';
	img = ce( 'img' );
	img.src = 'tool/' + ctable + '/esp.gif';
	img.id = 'mainesp';
	for( i = 0 ; i < ncols ; i++ )
	{
		out += '<td class="col' + i + '" id ="col' + i + '" width="' + wlist[i] + '" valign=top></td>';
	}
	out += '</tr></table>';
	$('main').innerHTML = out;
	for( i = 0 ; i < ncols ; i++ )
		$('col'+i).w = wlist[i];
}

function getslotpos()
{
	var i, j, c, d, t, l;
	var offh = $('header').clientHeight;
	var offw = ((document.body.clientWidth - 20) - maxwid())/2;
	var maxh = 0;
//	l = parseInt($('col0').style.left);
	l = 0;

	for( i = 0 ; i < ncols ; i++ )
	{	
//		t = parseInt($('col0').style.top);
		t = 0;

		c = $('col'+i);
		c.left = l;
		c.tops = [];
		for( j = 0 ; j < c.childNodes.length ; j++ )
		{
			if( c.clientHeight > maxh ) maxh = c.clientHeight ;
			d = c.childNodes[j];
			d.top = t + parseInt(d.style.top+0) + offh;
			c.tops[j] = t;
			t += d.clientHeight;
			d.left = l + parseInt(d.style.left+0) + offw;
		}
		c.tops[j] = t;
		c.ntops = j;
		l += c.w;
//		debug( c.id, c.left, 'tops', c.tops, 'tots', c.ntops );
	}
}

function rpar()
{
	return '?' + Math.random();
}

function preload()
{
	var i;
	for( i = 0 ; i < arguments.length ; i++ )
	{
		imglist[i] = new Image;
		imglist[i].src = arguments[i];
	}
}

function mywin( node )
{
	while( (node.tagName != 'DIV' && !isdefined( node.formdiv )) && node != document.body )
	{
		node = node.parentNode;
	}
	return node;
}

function rbox( id )
{
	var i, rb, b;
	rb = ce('div');
	rb.className = 'rbox';
	rb.id = '_rb_' + id;
	for( i = 1 ; i <= 4 ; i++ )
	{
		b = ce('div');
		b.className = 'rboxc' + i;
		rb.appendChild( b );
	}
	rb.onresize = oddsize;
	return rb;
}

function oddsize( e )
{
	var o = nn6 ? e.target : event.srcElement;
	if( o.clientWidth & 1 ) o.style.width = o.clientWidth+1;
	if( o.clientHeight & 1 ) o.style.height = o.clientHeight+1;

}

function resulttagcloud( xml, obj )
{}

function tagcloud( id, title, url, script )
{}

function resulttagcloudr( xml, obj )
{}

function tagcloudr( title, url, seeallurl,type )
{}

function tagcloudmain( title, script )
{}


var profilecollapsed = getCookie( 'profilecollapsed' ) == 'true' ? true : false;
var profilephotomax = profilecollapsed == 1 ? 40 : 100;
//alert( 'pc ' + profilecollapsed );

function profilesize( v )
{}

function resultprofile( xml, obj, xmltxt )
{}

var profileurl = '';
function profile( url )
{}


function profilenew( url )
{}


//# - added for display group display in the header -  By Jeyaseelan
function groups( url )
{}


//# - added for display group display in the header -  By Jeyaseelan
function groupsnew( url )
{}


function resultgroup( xml, obj )
{}

var maindivs = 0;
function addmain( url )
{
	var d = ce('div');
	d.id = 'main_' + maindivs;
	d.className = 'maindiv';
	$('main').appendChild( d );
	ajaxrequest( nocache( url ), 'resulttxt', d, 0 );
	maindivs++;
}

function addmainhtml( s )
{
	var d = ce('div');
	d.id = 'main_' + maindivs;
	d.className = 'maindiv';
	d.innerHTML = s;
	$('main').appendChild( d );
	maindivs++;
}

function addmainchilds( cl )
{
	var d = ce('div');
	d.id = 'main_' + maindivs;
	d.className = 'maindiv';

	$('main').appendChild( d );
	for( i = 0 ; i < cl.length ; i++ )
	{
		if( cl[i].tagName != 'SCRIPT' )
		{
			d.appendChild( cl[i] );
		}
	}
	maindivs++;
}


var hascols = false;
function leftcol()
{
	hascols = true;
	document.write( '<table cellpadding=0 cellspacing=0 border=0 width=100%><tr><td class="lcol">' );
}

function rightcol()
{
	document.write( '</td><td class=midcol>&nbsp;</td><td class="rcol">' );
}

function endcol()
{

	if( !hascols ) return;
	document.write( '</td></tr></table>' );
	hascols = false;
}


function splittext( classname, maxwid, stext )
{
	var tmp = ce( 'div' );
	var i, j;
	tmp.className = classname;
	tmp.style.position = 'absolute';
	tmp.style.left = -10000;
	tmp.style.width = '';
	tmp.style.display = 'block';

	var txtsplit = stext.split( / |\n/ );

	document.body.appendChild( tmp );
//	tmp.style.width = '10px';
	tmp.style.width = 'auto';
	var k = 40;
	for( i = 0 ; i < txtsplit.length ; i++ )
	{
		tmp.innerHTML = txtsplit[i];
		if( tmp.clientWidth > maxwid )
		{
			var out = txtsplit[i];
			for( j = 1 ; j < out.length && j < 1000; j++ )
			{
				tmp.innerHTML = out.substring( 0, j );
				if( tmp.clientWidth > maxwid )
				{
					out = out.substring( 0, j - 5) + '<br>' + out.substring( j-5, 10000 );
				}
			}
			txtsplit[i] = out;
			tmp.innerHTML = txtsplit[i];
		}
	}
	document.body.removeChild( tmp );
	return txtsplit.join( ' ' );

	
	dim = 5;
	while( 1 && tmp.clientHeight > maxheight )
	{
		stext = stext.substring( 0, stext.length - dim );
		while( stext.charAt(stext.length-1).match( /[a-zA-Z<]/ ) )
			stext = stext.substring( 0, stext.length - 1 );
		stext += more;
		if( dim < more.length ) dim += more.length;
		tmp.innerHTML = stext;
	}
	return stext;
}


function shrinktext( classname, maxheight, stext, maximizescript, moreflag, width )
{
//	return stext;
	var tmp = ce( 'div' );
	var more = moreflag ? ' ... <a href="javascript:'+ maximizescript + '">(more)</a>' : '...';
	tmp.className = classname;
	tmp.style.position = 'absolute';
	tmp.style.left = -10000;

	//#-- If more length string will be wrap into multi line.
//	stext = stext.wordWrap(38, "\n", true);
	//#--
	if( isdefined( width ) ) tmp.style.width = width;
	
	document.body.appendChild( tmp );
	tmp.innerHTML = stext;
	dim = 5;
	while( 1 && tmp.clientHeight > maxheight )
	{
		stext = stext.substring( 0, stext.length - dim );
		while( stext.charAt(stext.length-1).match( /[a-zA-Z<]/ ) )
			stext = stext.substring( 0, stext.length - 1 );
		stext += more;
		if( dim < more.length ) dim += more.length;
		tmp.innerHTML = stext;
	}
	document.body.removeChild( tmp );
	return stext;
}

function imgmaxdimmiddle( img, max )
{
	if( !isdefined( img.tim ) ) img.tim = 0;
	if( img.tim < 10 && img.width == 0 )
	{
		img.tim++;
		setTimeout( function() { imgmaxdimmiddle( img, max ); }, 1000 );
		return;
	}
	imgmaxdim( img, max );
	img.style.position = 'relative';
	img.style.left = Math.floor( (max-img.clientWidth)/2 );
}

function imgmaxdimcenter( img, max )
{
	if( !isdefined( img.tim ) ) img.tim = 0;
	if( img.tim < 10 && img.width == 0 )
	{
		img.tim++;
		setTimeout( function() { imgmaxdimcenter( img, max ); }, 1000 );
		return;
	}
	imgmaxdim( img, max );
	img.style.position = 'relative';
	img.style.left = Math.floor( (max-img.clientWidth)/2 );
	img.style.top = Math.floor( (max-img.clientHeight)/2 );
}

function errorlog( component, str )
{
	alert( component + ': ' + str );
}

function imgmaxdim(img, max)
{
	if( !isdefined( img.tim ) ) img.tim = 0;
	if( img.tim < 10 && img.width == 0 )
	{
		img.tim++;
		setTimeout( function() { imgmaxdim( img, max ); }, 1000 );
		return;
	}
	var r = 1;
	var left = parseInt( img.offsetLeft + 0 );
	var top = parseInt( img.offsetTop + 0 );
	if( !isdefined( img.nheight ) )
	{
		img.nheight = img.height;
		img.nwidth = img.width;
	}
	var w = img.nwidth;
	var h = img.nheight;
	if( w == -1 )
	{
		debug( 'reload', img.width );
		setTimeout( function() { imgmaxdim( img, max ) }, 1000 );
		return;
	}
	var m = w > h ? w : h;
	if( m > max ) r = max/m;

	img.width = Math.floor( w*r );
	img.height = Math.floor( h*r );
	var w2 = img.width;
	var h2 = img.height;
	var m2 = w2 > h2 ? w2 : h2;
	if( m2 > max )
	{
//		debug( 'force' );
		img.style.width = Math.floor( w*r );
		img.style.height = Math.floor( h*r );
	}
//	debug( img.src, w, h, r, 'x', img.width, img.height );
	if( left <= 0 ) 
	{
		img.style.left = Math.floor((max-img.height)/2);
		img.style.left = 0;
	}
	if( top < 0 )
	{
		img.style.position = 'relative';
		img.style.top = 0;
	}
	var o = img.parentNode;
	while( o != document.body && o.parentNode != null && o.onmouseover == null ) o = o.parentNode;
	if( isdefined( o.onmouseover ) ) o.title = img.title;
	img.isloaded = true;
}
function loadhtml( div, url ){	div.innerHTML = 'Loading...'; ajaxrequest( nocache( url ), 'resultform', div, 0 ); }

//#- Function Added By Rajendran - its for Redirect to correspondiing page
function pageRedirect( pageName )
{
	window.location.href = pageName;
}//#--


function getnext( o )
{
	var childs = o.parentNode.childNodes;
	for( var i = 0 ; i < childs.length - 1 ; i++ )
		if( childs[i] == o ) return childs[i+1];
	return null;
}

function getprev( o )
{
	var childs = o.parentNode.childNodes;
	for( var i = 1 ; i < childs.length ; i++ )
		if( childs[i] == o ) return childs[i-1];
	return null;
}

var imglist = new Array;

preload( 'tool/' + ctable + '/esp.gif' );

document.onmousedown=selectmouse;
document.onmouseup=stopdrag;
//# - check for special extended characters
function checkspecialchars(e)
{
	var isIE = navigator.userAgent.indexOf("MSIE") != -1; 
	
	//# - Get keycode
	if(!isIE) 
	{
		var pNumKeyCode = e.which
	}
	else
		var pNumKeyCode = (window.event) ? event.keyCode : e.keyCode;
	//#- check for special chars
	//if(pNumKeyCode >= 33 && pNumKeyCode<48 || pNumKeyCode>=58 && pNumKeyCode<65 || pNumKeyCode >= 91 && pNumKeyCode<97 || pNumKeyCode >= 123 && pNumKeyCode<127)//
	if(pNumKeyCode >= 160 && pNumKeyCode<256)
	{
		return false;
	}
	else
			window.event.returnValue = "NULL";
	return true;
}
//# - remember me option - added by jeyaseelan on 12/01/2006
function checkrememberme(ckbox,form)
{
	var obj = eval("document."+form+".ckRemember");
	
	if(ckbox.checked == true)
		obj.value = 1;
	else
		obj.value = 0;
	
}
//#-- Redirect to Global Users page.
// tree component starts here
var stopev = false;
function treerecover( n )
{
	var dc = document.cookie.split( '; ' );
	for( var i = 0 ; i < dc.length ; i++ )
	{
		var tmp = dc[i].split( '=' );
		if( tmp[0] == n ) return tmp[1]*1;
	}
	return 0;
}
function treesave( n, v )
{
	document.cookie = n + '=' + v;
}

function treeclick( event, o )
{
	var myclass, otherclass;


	if( stopev ) return;
	stopev = true;
	setTimeout( 'stopev = false', 100 );

	var to = o;
	while( to != document.body && to.className != 'tree' ) to = to.parentNode;

	if( o.className == 'treeplus' )
	{
		myclass = 'treeminus';
		o.className = myclass;
		var ch = o.clientHeight;
//		to.scrollTop += ch;
		treesave( o.getAttribute('ident'), 0 );
	}
	else if( o.className == 'treeminus' )
	{
		myclass = 'treeplus';
		o.className = myclass;
		treesave( o.getAttribute('ident'), 1 );
	}
	else if( o.className == 'treenode' )
	{
		var sel = $(o.getAttribute('select'));
		var idx = o.getAttribute('index')*1;
		if( !sel.multiple )
		{
			var dlist = to.getElementsByTagName("DIV");
			for( var i = 0 ; i < dlist.length ; i++ )
				if( dlist[i].className == 'treenodesel' )
					dlist[i].className = 'treenode';
		}

		sel.options[idx].selected = true;
		myclass = sel.multiple ? 'treenodeck' : 'treenodesel';
		o.className = myclass;
	}
	else if( o.className == 'treenodeck' )
	{
		var sel = $(o.getAttribute('select'));
		var idx = o.getAttribute('index')*1;
		sel.options[idx].selected = false;
		myclass = 'treenode';
		o.className = myclass;
	}
}

function tree( selectsource, id, height,splitby )
{
	var tree = ce( 'DIV' );
	var sel = selectsource;
	var i;
	tree.id = id;
	tree.className = 'tree';
	hang = sel.parentNode;
	hang.insertBefore( tree, sel );
	tree.onscroll = function() { treesave( tree.id + '_scroll', tree.scrollTop ); };
	
	sel.style.display = 'none';
	tree.style.height = height;
	var treein = '';
	var llist = new Array;
	var levels = 0;
	for( var i = 0 ; i < sel.options.length ; i++ )
	{
		var txt = sel.options[i].text;
		var nlist = splitby?txt.split( splitby ):txt.split( "|" );
		for( var j = 0 ; j < nlist.length - 1 ; j++ )
		{
			if( nlist[j] != llist[j] )
			{
				while( levels > j )
				{
					treein += '</div></div>\n';
					levels--;
				}
				var ident = 'tree_' + escape( nlist[j] );
				var tclass =  treerecover( ident ) ? 'treeplus' : 'treeminus';
				treein += '<div class=' + tclass + ' ident="' + ident + '" onclick="treeclick( event, this )"><nobr>' + nlist[j] + '</nobr><div class=treesub>\n';
				levels++;
			}
		}
		var tclass =  sel.options[i].selected ? (sel.multiple ? 'treenodeck':'treenodesel') : 'treenode';
		treein += '<div class=' + tclass + ' index="' + i + '" select="' + sel.id + '" onclick="treeclick( event, this )"><nobr>' + nlist[j] + '</nobr></div>\n';
		llist = nlist;
	}
	tree.innerHTML = treein;
	tree.scrollTop = treerecover( tree.id + '_scroll' );
}

function editcontent(id)
{
	var ptop = event.clientY + document.body.scrollTop;
	var pleft = event.clientX + document.body.scrollLeft;
	ptop -= ie ? event.y: event.layerY-5;
	pleft -= ie ? event.x : event.layerX-10;
	popupwidth = 500;
	pageName = "editcontent.php?hidid="+id;
	popup( pageName, ptop+5, pleft+45, popupwidth);
}
function deleteresourcetags()
{
	
	if(!document.frmtags.addselect.value)
	{
		alert("Please select atleast one tag to delete!")
		return false;
		//document.frmtags.hidClose.value = 1;
	}
	else
	{
		if(!confirm('Are you sure to delete this tag?')) 
		{
			document.frmtags.hidClose.value = 1;
		}
	}
	return true;
}

function insertad()
{
	document.write( '<iframe src="ad.html" scrolling="No" width="310" height="263" marginwidth="0" marginheight="0" frameborder="No" style="border: 1px solid #ccc"></iframe><br>&nbsp;' );
}

function forcercolwid()
{
	return !ie ? ' style="width: 300px"' : ' style="width: 312px"';
}

function nocache( url )
{
	var ret;
	if( url.indexOf('?') > 0 )
		ret = url + '&rpar=' + Math.random();
	else
		ret = url + '?rpar=' + Math.random();
	return ret;
}

function sessioncache( url )
{
	var ret;
	if( url.indexOf('?') > 0 )
		ret = url + '&rpar=' + scache;
	else
		ret = url + '?rpar=' + scache;
	return ret;
}
//# - Get Parent window filename and set to cookie added by kamalakannan on 28-Nov-06 at 4.30pm >
function filename_set_cookie(winlocation, pmCookieName)
{
	//# - replace & inplace of [amp] which set in for media video redirection
	winlocation = winlocation.replace("[amp]","&");
	var curpagename = parent.window.location;
	//# - get File name
	var currentpagename = getbasename(curpagename);
	
	//if(!in_array( currentpagename, aCur_filenames))
	if(currentpagename)
	{
		pmCookieName = pmCookieName ? pmCookieName : "backfilename";
		setCookie (pmCookieName , currentpagename);
	}
	window.location.href = winlocation;
}


function bmclick()
{ 
	if( ie )
		alert( 'Right-click on this button and select "Add to Favorites" to add the BookMarklet' );
	else
		alert( 'Drag this BookMarklet to your "Favorites" menu entry' );
	return false;
}
//# - To store the session value before it kills
var sessTimeUp,sessTransaction;
var sessTime	 = parseInt(sesLifeTime) * 1000;		//# - Time Limit to commit session
//var sessTime	 = 60 * 1000;
var transactiontime = 60 * 1000; //# - 1 mins
function sessTimeIt()
{
	sessTimeUp = window.setTimeout(warn, sessTime);
	sessTransaction = window.setTimeout(tracetransaction, transactiontime);
}

function warn()
{
	clearTimeout( sessTimeUp);
	ajaxrequest('setlogouttime.php' , 'resultsession', null, 0 );
}
function tracetransaction()
{
	ajaxrequest('setlogouttime.php?update=1' , '', null, 0 );
}
function sessResetTime()
{
	clearTimeout( sessTimeUp);
	sessTimeUp = setTimeout(warn, sessTime);
	sessTransaction = window.setTimeout(tracetransaction, transactiontime);
	
}
function resultsession(txt,obj)
{
	if(txt)
	{
	/*	//# - redirect to the previous page when thge session has been terminated
		var curpagename = parent.window.location;
		//# - get File name
		curpagename = curpagename.toString().split("/");
		curpagename = curpagename[curpagename.length-1];
		setCookie ('LoginRedirectUrl' , curpagename);
		window.location.href = "index.php";*/
		
		filename_set_cookie("index.php", 'LoginRedirectUrl')
	}
	
	
}
function PopupCalendar(val)
{	
	var pleft = (val==1)?360:570;
	var ptop = 240;
	var calwidth = 300;
	popup('php_calendar.php?check='+val,ptop,pleft,calwidth);
}
function calendar(year,month,date,type)
{	
	if(type == 1)
		document.frmDate.txtFromDate.value = month + '-' + date + '-' + year;
	else
		document.frmDate.txtToDate.value = month + '-' + date + '-' + year;
	nopopup();
}
function calendarmonth(url,val)
{
	nopopup();
	var pleft = (val==1)?360:570;
	var ptop = 240;
	var calwidth = 300;
	popup(url,ptop,pleft,calwidth);
}
//#---this is used to admin add to featured product
function featuredprod(event, obj, pid)
{
	update = 0;
	if(obj.checked == true)
		update = 1;
			
	if(ie)
	{
		ptop = 490;
		pleft = 580;
	}
	else if(nn6off)
	{
		ptop = 530;
		pleft = 590;
	}
	popupwidth = 350
	popup( 'addfeature_prod.php?pid='+pid+'&update='+update, ptop, pleft, popupwidth );	
	//popup("addfeature_prod.php?pid="+pid+"&update="+update,500,590,350);
	
}
function chartpopup()
{
	var ptop = 42;
	var pleft = 550;
	popup('charts.php?',ptop,pleft,280);
}
//#-- changes by Rajendran
function prodhandle( event, o ,pid , func)
{
	//#!-- added by Jeyaseelan to handle the product in Admin reports page.
	var ptop = event.clientY + document.body.scrollTop;
	var pleft = event.clientX + document.body.scrollLeft;
	ptop -= ie ? event.y: event.layerY;
	pleft -= ie ? event.x : event.layerX;
	pleft += Math.floor(o.clientWidth/2);
	ptop += Math.floor(o.clientHeight/2);

	popupwidth = 350;
	pleft = (func == "editurl")?pleft+35:pleft+55;
	pageName = "prodhandle.php?pid="+pid+"&func="+func;
	popup( pageName, ptop+10, pleft, popupwidth );
	
}