var applicationHeight;
var applicationWidth;
var browserVersion;
var applicationWindow;
var runPath;
function getObj(id)
{
	return document.getElementById(id);
} 
function getVersion()
{
	browserVersion=navigator.appName;
}

function initComponants()
{
	runPath='';
	if (document.body && document.body.offsetWidth)
	{
	applicationWidth = document.body.offsetWidth;
	applicationHeight = document.body.offsetHeight;
	}
	if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth )
	{
	applicationWidth = document.documentElement.offsetWidth;
	applicationHeight = document.documentElement.offsetHeight;
	}
	if (window.innerWidth && window.innerHeight)
	{
	applicationWidth = window.innerWidth;
	applicationHeight = window.innerHeight;
	}
	getVersion();
	applicationWindow=getObj('applicationWindow');
	//applicationWindow.style.height=applicationHeight+"px";
	//applicationWindow.style.width=applicationWidth+"px"; 
}
function delay(timeToDelay)
{
	var date = new Date();
	var curDate = null;
	while(curDate-date < timeToDelay)
	{
		curDate = new Date();
	}
}
function createTextbox(id,className,type)
{
	obj = document.createElement('input');
	obj.setAttribute("type",type);
	obj.setAttribute("className", className);
	obj.className=className;
	obj.setAttribute("id",id);
	return obj;
}
function createTextArea(id,className,row,col)
{
	tArea = document.createElement('textarea');
	tArea.setAttribute("id",id);
	tArea.setAttribute("className",className);
	tArea.className=className;
	tArea.setAttribute("rows",row);
	tArea.setAttribute("cols",col);
	return tArea;
}
function createTable(id,className,row,col)
{
	tb = document.createElement('table');
	tb.setAttribute("className",className);
	tb.className=className;
	myTbody = document.createElement("tbody");
	for(i=0;i<row;i++)
	{
		
		tr = document.createElement('tr');
		for(j=0;j<col;j++)
		{
			td = document.createElement('td');
			td.setAttribute("id",id+'_td_'+i+'_'+j);
			//td.style="vartical-align:top";
			//td.setAttribute("valign",valign);
			td.innerHTML="&nbsp;";
			tr.appendChild(td);
		}
		myTbody.appendChild(tr);
		
			
		
		
		
	}
	tb.appendChild(myTbody);
	return tb;
}
function createSpan(id,className,html)
{
	sp = document.createElement('span');
	sp.setAttribute("id",id);
	sp.setAttribute("className",className);
	sp.className=className;
	sp.innerHTML=html;
	return sp;
}
function createComboBox(id,className)
{
	cb = document.createElement('select');
	cb.setAttribute("id",id);
	cb.setAttribute("className",className);
	cb.className=className;
	return cb;
}
function createOption(val,text,selected)
{
	ops = document.createElement('option');
	ops.setAttribute("value",val);
	ops.value=val;
	ops.innerHTML=text;
	ops.setAttribute("selected",selected);
	ops.selected=selected;
	return ops;
}
function createButton(id,className,val,fun)
{
	bt = document.createElement('input');
	bt.setAttribute("id",id);
	bt.setAttribute("type",'button');
	bt.setAttribute("className",className);
	bt.className=className;
	bt.setAttribute("value",val);
	bt.setAttribute("onclick",fun);
	bt.onclick=function(){setTimeout(fun,10);};
	return bt;
}
function createDiv(id,className,html)
{
	dv = document.createElement('div');
	dv.setAttribute('id',id);
	dv.setAttribute("className",className);
	dv.className=className;
	dv.innerHTML=html;
	return dv;
}
var zIndexLavel;
function setUpZindexLavel()
{
	zIndexLavel=zIndexLavel+1;
}
function setDownZindexLavel()
{
	zIndexLavel=zIndexLavel-1;
}
function setOpacity(obj,value) 
{
	obj.style.opacity = 1-(value/100);
	obj.style.filter = 'alpha(opacity=' + (100-value) + ')';
}

function applicationBusyCanvase(id)
{
	
	dx = createDiv(id+"_divX","","");
	dx.style.width=document.getElementById("applicationWindow").offsetWidth+"px";
	dx.style.height=document.getElementById("applicationWindow").offsetHeight+"px";
	dx.style.left="0px";
	dx.style.top="0px";
	dx.style.position="absolute";
	d = createDiv(id,"","");
	d.style.width=document.getElementById("applicationWindow").offsetWidth+"px";
	d.style.height=document.getElementById("applicationWindow").offsetHeight+"px";
	d.style.left="0px";
	document.getElementById("applicationWindow").scrollTop="10px";
	//delay(2000);
	//alert(document.body.scrollHeight);
	d.style.top="0px";
	d.style.position="absolute";
	d.style.cursor="wait";
	setOpacity(d,70);
	setUpZindexLavel();
//	d.style.zIndex = zIndexLavel+" ";
	d.style.backgroundColor  = "#cccccc";
	dx.appendChild(d);
	return dx;
}
function busy(id)
{

	
	var scrolledX, scrolledY;
	if( self.pageYOffset ) 
	{
		scrolledX = self.pageXOffset;
		scrolledY = self.pageYOffset;
		
	} 
	else if( document.documentElement && document.documentElement.scrollTop ) 
	{
		scrolledX = document.documentElement.scrollLeft;
		scrolledY = document.documentElement.scrollTop;
		
	} 
	else if( document.body ) 
	{
		scrolledX = document.body.scrollLeft;
		scrolledY = document.body.scrollTop;
		
	}
	// Next, determine the coordinates of the center of browser's window

	if(applicationHeight>scrolledY)
	{
		scrolledY = applicationHeight;
	}
	
	if(applicationWidth>scrolledX)
	{
		scrolledX = applicationWidth;
	}
	var centerX, centerY;
	if( self.innerHeight ) {
	centerX = self.innerWidth;
	centerY = self.innerHeight;
	} else if( document.documentElement && document.documentElement.clientHeight ) {
	centerX = document.documentElement.clientWidth;
	centerY = document.documentElement.clientHeight;
	} else if( document.body ) {
	centerX = document.body.clientWidth;
	centerY = document.body.clientHeight;
	} 
	//var leftoffset = scrolledX + (centerX - width) / 2;
	//var topOffset = scrolledY + (centerY - height) / 2;
	
	var leftoffset = scrolledX+(centerX - 128) / 2;
	var topOffset = scrolledY+(centerY - 15) / 2;
	db = applicationBusyCanvase(id+"_ajax_busy");	
	d = createDiv("waitingImg","","");
	d.innerHTML='<img src="'+runPath+'application/image/ajax-loader.gif" width="128" height="15" />';
	d.style.top = topOffset+"px";
	d.style.left = leftoffset+"px";
	d.style.position="absolute";
	d.style.display = "block"; 	
	
	db.appendChild(d);
	document.getElementById('applicationWindow').appendChild(db);
}
function free(id)
{
	document.getElementById("applicationWindow").removeChild(document.getElementById(id+"_ajax_busy_divX"));
	
}
function makeBlankCanvas(id,className,left,top,width,height,html,title,closeFun)
{
	height = parseInt(height)+21;
	var objId = id;
	id = id+"_canvas";
	var scrolledX, scrolledY;
	if( self.pageYOffset ) 
	{
		scrolledX = self.pageXOffset;
		scrolledY = self.pageYOffset;
		
	} 
	else if( document.documentElement && document.documentElement.scrollTop ) 
	{
		scrolledX = document.documentElement.scrollLeft;
		scrolledY = document.documentElement.scrollTop;
		
	} 
	else if( document.body ) 
	{
		scrolledX = document.body.scrollLeft;
		scrolledY = document.body.scrollTop;
		
	}
	// Next, determine the coordinates of the center of browser's window

	var centerX, centerY;
	if( self.innerHeight ) {
	centerX = self.innerWidth;
	centerY = self.innerHeight;
	} else if( document.documentElement && document.documentElement.clientHeight ) {
	centerX = document.documentElement.clientWidth;
	centerY = document.documentElement.clientHeight;
	} else if( document.body ) {
	centerX = document.body.clientWidth;
	centerY = document.body.clientHeight;
	} 
	//var leftoffset = scrolledX + (centerX - width) / 2;
	//var topOffset = scrolledY + (centerY - height) / 2;
	
	//
	//
	var leftoffset = left;
	var topOffset = top;
	if(left=="0")
	{
		var leftoffset = scrolledX+(centerX - width) / 2;
	}
	if(top=="0")
	{
		var topOffset = scrolledY+(centerY - height) / 2;
	}
	
	if(closeFun==null)
	{
		closeFun='closeCanvas('+"'"+objId+"'"+')';
	}
	db = applicationBusyCanvase(id+"_busy");
	setUpZindexLavel();
	d = createDiv(id);
	d.style.cursor="wait";
	d.style.width=(width)+"px";
	d.style.height=(height)+"px";
	//d.style.filter="Blur(Direction=45, Strength=8)";
	d.style.left=leftoffset+"px";
	d.style.top=topOffset+"px";
	d.style.position="absolute";
	d.style.display = "block"; 	
//	d.style.zIndex = zIndexLavel+" ";
	d.innerHTML='<table width="'+(width)+'px" height="'+(height)+'px"  border="0" cellpadding="0" cellspacing="0">  <tr>    <td><img src="'+runPath+'application/image/c1.png" width="32" height="32" /></td>    <td background="'+runPath+'application/image/i1.png" width="'+(width)+'px"></td>    <td><img src="'+runPath+'application/image/c2.png" width="32" height="32" /></td>  </tr>  <tr>    <td background="'+runPath+'application/image/i2.png" height="'+(height)+'px"></td>    <td id="'+id+'_viewPoint"></td>    <td background="'+runPath+'application/image/i3.png"></td>  </tr>  <tr>    <td><img src="'+runPath+'application/image/c3.png" width="32" height="32" /></td>    <td background="'+runPath+'application/image/i4.png"></td>    <td><img src="'+runPath+'application/image/c4.png" width="32" height="32" /></td>  </tr></table>';
	
	dv = createDiv(id+"_view");
	dv.style.width=width+"px";
	dv.style.height=height+"px";
	dv.style.cursor="auto";

	dv.className = className;

	
	dv.innerHTML='<div id="'+id+'_title" style="height:20px"><table width="100%"><tr><td><b>'+title+'</b></td><td align="right" ><a  id="'+id+'_closeBtn'+'" onclick="'+closeFun+'" href="javascript:">Close</a></td></tr></table></div><div id="'+id+'_content"></div>';	
	db.appendChild(d);	
	document.getElementById('applicationWindow').appendChild(db);
	
	document.getElementById(id+'_viewPoint').appendChild(dv);
	//document.getElementById(id+'_title').innerHTML="<b>"+title+'</b><a  id="_closeBtn">Close</a>';
	//document.getElementById(id+'_closeBtn').onclick=closeFun;
	document.getElementById(id+'_title').className=className+"_title";
	document.getElementById(id+'_content').style.height=height-20;
	document.getElementById(id+'_content').style.overflow="auto";
	document.getElementById(id+'_content').innerHTML=html;
	return db;
}

function createTabPanel(id,className,tabCnt)
{
	ns=createDiv(id,"","");
	ns.setAttribute("className",id+className);
	ns.className = id+className;
	
	tbcnt = createTextbox(id+"_tabCnt","","hidden")
	tbcnt.value=tabCnt;
	ns.appendChild(createTextbox(id+"_selectedTab","","hidden"));
	ns.appendChild(tbcnt);
	title=createDiv(id+"_bar","","");
	title.setAttribute("className",id+className+"_bar");
	title.className = id+className+"_bar";
	for(i=0;i<tabCnt;i++)
	{
		tb=createDiv(id+"_tab"+i,"","Tab "+i);
		tb.setAttribute("className",id+className+"_title");
		tb.className = id+className+"_title";
		tbId=createTextbox(id+"_tab"+i+"_ID","","hidden");
		tbId.value=i;
		title.appendChild(tbId);
		tb.onclick=function (e)
		{
			var browser=navigator.appName;
			var b_version=navigator.appVersion;
			var version=parseFloat(b_version);
			var evt=window.event || e;
			//alert(evt.srcElement.id);
			//setTabContent('eventTab',0,browser);
			if(browser!="Microsoft Internet Explorer")
			{
				setSelectedtab(id,getObj(e.target.id+"_ID").value);
			}
			else
			{
				setSelectedtab(id,getObj(window.event.srcElement.id+"_ID").value);
			}
			
		}
		//setSelectedtab(id,getObj(e.target.id+"_ID").value);
		title.appendChild(tb);
		dt = createTextbox(id+"_value"+i,"","hidden");
		dt.value="Tab Content "+i;
		title.appendChild(dt);
	}
	cnt=createDiv(id+"_content","","");
	cnt.setAttribute("className",id+className+"_content");
	cnt.className = id+className+"_content";	
	ns.appendChild(title);
	ns.appendChild(cnt);
	
	return ns;/**/
}
function initALlTab(id)
{
	tabCnt=getObj(id+"_tabCnt").value;
	className=getObj(id).className;
	//alert(tabCnt);
	for(i=0;i<tabCnt;i++)
	{
		//alert(id+"_tab"+i);
		getObj(id+"_tab"+i).className= className+"_title";
		getObj(id+"_tab"+i).setAttribute("className",className+"_title");
	}
}
function setSelectedtab(id,tabIndex)
{
	initALlTab(id);
	tabClass=getObj(id).className;
	getObj(id+"_tab"+tabIndex).className=tabClass+"_selected";
	getObj(id+"_content").innerHTML = getObj(id+"_value"+tabIndex).value;
	tabClicked(id+"_tab"+tabIndex);
}
function setTabTitle(id,i,title)
{
	getObj(id+"_tab"+i).innerHTML = title;
}
function setTabContent(id,i,html)
{
	getObj(id+"_value"+i).value = html;
	
	if(getObj(id+"_selectedTab").value==i)
	{
		getObj(id+"_content").innerHTML=html;
	}
	
}
