//====================================================================================================
//	File Name		:	functions.js
//----------------------------------------------------------------------------------------------------
//	Purpose			:	Javascript Utility functions
//	Creation Date	:	05-May-2003
//	Copyright		:	Copyrights  2003 Dot Infosys
//====================================================================================================

//function truebody(){
//	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
//}

//====================================================================================================
//	Function Name	:	popupWindowURL
//	Purpose			:	Whenever you wanna open a link into a new window just call this function
//								you need to pass some arguemnts as described below.
//	Creation Date	:	05-May-2003
//----------------------------------------------------------------------------------------------------
function popupWindowURL(url, winname,  w, h, menu, resize, scroll) {

    var x = (screen.width-w)/2;
    var y = (screen.height-h)/3;

	if (winname == null) winname = "newWindow";
	if (w == null) w = 800;
	if (h == null) h = 600;
	if (resize == null) resize = 1;

	menutype   = "nomenubar";
	resizetype = "noresizable";
	scrolltype = "noscrollbars";
	if (menu) menutype = "menubar";
	if (resize) resizetype = "resizable";
	if (scroll) scrolltype = "scrollbars";
	//alert(url+","+x+","+winname);
    cwin=window.open(url,winname,"top=" + y + ",left=" + x + ",screenX=" + x + ",screenY=" + y + "," + "status," + menutype + "," + scrolltype + "," + resizetype + ",width=" + w + ",height=" + h);

	if (!cwin.opener) cwin.opener=self;
	cwin.focus();

	return true;
}

function ToggleLoader(flg, msg)
{
	if(flg)
	{
		DisableBodyArea(true);

		var x = (truebody().clientWidth - 200)/2;
		var y = (truebody().clientHeight - 100)/2;
		//var y = 475;
//		var y = 475+120;
		if(msg)
			document.getElementById('stdLoaderMsg').innerHTML = msg;
		else
			document.getElementById('stdLoaderMsg').innerHTML = LM_LOADING;

		document.getElementById('stdLoader').style.left = x+"px";
		document.getElementById('stdLoader').style.top = y+"px";
		document.getElementById('stdLoader').style.display = '';
	}
	else
	{
		document.getElementById('stdLoader').style.display = 'none';
		DisableBodyArea(false)
	}
}

function DisableBodyArea(flg)
{
	if(flg)
	{
		document.getElementById('stdDisableArea').style.width	= truebody().scrollWidth + 'px';
		document.getElementById('stdDisableArea').style.height	= truebody().scrollHeight + 'px';
		document.getElementById('stdDisableArea').style.display = '';
	}
	else
	{
		document.getElementById('stdDisableArea').style.display = 'none';
	}
}

var t_id = setInterval(animate, 20);
var pos=0;
var dir=2;
var len=0;

function animate()
{
	return;
	var elem = document.getElementById('progress');

	if(elem != null) {
//		alert('here');
		if (pos==0) len += dir;
		if (len>32 || pos>79) pos += dir;
		if (pos>79) len -= dir;
		if (pos>79 && len==0) pos=0;
		elem.style.left = pos;
		elem.style.width = len;
	}
}

function remove_loading() {
	this.clearInterval(t_id);
	var targelem = document.getElementById('loader_container');
	targelem.style.display='none';
	targelem.style.visibility='hidden';
	var t_id = setInterval(animate,60);
}

function ShowHide(block, img, imgUp, imgDown, lblText, txtShow, txtHide)
{
	if(block)
	{
		if(block.style.display == 'none')
		{
			block.style.visibility	= 'visible';
			block.style.display		= '';
			if(img)
				img.src = imgUp;
			if(lblText)
				lblText.innerHTML = txtHide;
		}
		else
		{
			block.style.visibility 	= 'hidden';
			block.style.display 	= 'none';
			if(img)
				img.src = imgDown;
			if(lblText)
				lblText.innerHTML = txtShow;
		}
	}
}

function ToggleView(viewList, imgId, imgList)
{
	for(i=0; i<viewList.length; i++)
	{
		if(document.getElementById(viewList[i]).style.display == '')
			lastVisible=i;
		document.getElementById(viewList[i]).style.display		= 'none';
	}

	lastVisible++;
	if(viewList.length == lastVisible) lastVisible=0;

	document.getElementById(viewList[lastVisible]).style.display	= '';
	if(imgId && imgList[lastVisible])
	{
		imgId.src = Templates_Image + imgList[lastVisible];
	}
}

var arrTabActive 		= new Array(Templates_Image + 'tab_active_left.gif', Templates_Image + 'tab_active_spacer.gif', Templates_Image + 'tab_active_right.gif');
var arrTabInactive 		= new Array(Templates_Image + 'tab_deactive_left.gif', Templates_Image + 'tab_deactive_spacer.gif', Templates_Image + 'tab_deactive_right.gif');

var arrSubTabActive 	= new Array(Templates_Image + 'corner_sub01.gif', Templates_Image + 'corner_sub02.gif', Templates_Image + 'corner_sub03.gif');
var arrSubTabInactive 	= new Array(Templates_Image + 'corner_sub04.gif', Templates_Image + 'corner_sub05.gif', Templates_Image + 'corner_sub06.gif');

function changeTabTo(masId, chId, totalTab, clsActive, clsInactive, fnCall, imgActive, imgInactive)
{
	for(i=1; i<=totalTab; i++)
	{
		if(i==chId)
		{
			if(clsActive)
				document.getElementById('tab'+masId+'_'+i).className 				= clsActive;
//				document.getElementById('tabContent'+masId+'_'+i).style.visibility 		= 'visible';
			document.getElementById('tabContent'+masId+'_'+i).style.display 		= '';

			document.getElementById('tab'+masId+'_'+i+'_img1').src 				= imgActive[0];
			document.getElementById('tab'+masId+'_'+i+'_img2').style.backgroundImage		= 'url(\'' + imgActive[1] + '\')';
			document.getElementById('tab'+masId+'_'+i+'_img2').className 		= clsActive;
			document.getElementById('tab'+masId+'_'+i+'_img3').src 				= imgActive[2];
			
			if(fnCall)
				eval('setTimeout("' + fnCall + '", 5000);');
		}
		else
		{
			if(clsInactive)
				document.getElementById('tab'+masId+'_'+i).className 				= clsInactive;
//				document.getElementById('tabContent'+masId+'_'+i).style.visibility 		= 'hidden';
			document.getElementById('tabContent'+masId+'_'+i).style.display 		= 'none';
			
			document.getElementById('tab'+masId+'_'+i+'_img1').src 				= imgInactive[0];
			document.getElementById('tab'+masId+'_'+i+'_img2').style.backgroundImage		= 'url(\'' + imgInactive[1] + '\')';
			document.getElementById('tab'+masId+'_'+i+'_img2').className 		= clsInactive;
			document.getElementById('tab'+masId+'_'+i+'_img3').src 				= imgInactive[2];
		}
	}
}

function attachEventListener(target, eventType, functionRef, capture) 
{ 
	if (typeof target.addEventListener != "undefined") 
	{ 
		target.addEventListener(eventType, functionRef, capture); 
	} 
	else if (typeof target.attachEvent != "undefined") 
	{ 
		var functionString = eventType + functionRef; 
		target["e" + functionString] = functionRef; 
		
		target[functionString] = function(event) 
		{ 
			if (typeof event == "undefined") 
			{ 
				event = window.event; 
			} 
			target["e" + functionString](event); 
		}; 
		
		target.attachEvent("on" + eventType, target[functionString]); 
	} 
	else 
	{ 
		eventType = "on" + eventType; 
		
		if (typeof target[eventType] == "function") 
		{ 
			var oldListener = target[eventType]; 
			
			target[eventType] = function() 
			{ 
				oldListener(); 
				
				return functionRef(); 
			} 
		} 
		else 
		{ 
			target[eventType] = functionRef; 
		} 
	} 
} 

function detachEventListener(target, eventType, functionRef, capture) 
{ 
	if (typeof target.removeEventListener != "undefined") 
	{ 
		target.removeEventListener(eventType, functionRef, capture) 
	} 
	else if (typeof target.detachEvent != "undefined") 
	{ 
		var functionString = eventType + functionRef; 
		
		target.detachEvent("on" + eventType, target[functionString]); 
		
		target["e" + functionString] = null; 
		target[functionString] = null; 
	} 
	else 
	{ 
		target["on" + eventType] = null; 
	} 
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + getExpiryDate(expires) : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function getExpiryDate(nodays) {
	var UTCstring;

	Today = new Date();
	nomilli=Date.parse(Today);
	Today.setTime(nomilli+nodays*24*60*60*1000);
	UTCstring = Today.toUTCString();

	return UTCstring;
}

function resizeImage(img, width, height)
{
	var newImage = new Image();
	newImage.src = img.src;
	
	thumbWidth 	= width;
	thumbHeight	= Math.round((thumbWidth/newImage.width)*newImage.height);

	if(thumbHeight > height)
	{
		thumbHeight	= height;
		thumbWidth 	= Math.round((thumbHeight/newImage.height)*newImage.width);
	}
	img.width 	= thumbWidth?thumbWidth:width;
	img.height	= thumbHeight?thumbHeight:height;
}

function PrintMe_Click(title, element)
{ 
  var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
      disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25"; 
  var content_vlue = element.innerHTML; 
  
  var docprint=window.open("","",disp_setting); 
   docprint.document.open(); 
   docprint.document.write('<html><head><title>' + title + '</title>'); 
   docprint.document.write('</head><body onLoad="self.print()"><center>');          
   docprint.document.write(content_vlue);          
   docprint.document.write('</center></body></html>'); 
   docprint.document.close(); 
   docprint.focus();
}

function UploadImage_Change(obj, pic, defaultVal, defaultWidth)
{
	imgTag = document.getElementById(pic);
	imgTag.width=120;

	if(obj.value == '')
		imgTag.src = defaultVal;
	else
	{
		imgTag.src = obj.value;
		if(defaultWidth != '')
			imgTag.width=defaultWidth;
	}
}

