var divMessage;


function PrintThisPage(divp) 
{ 
	var sOption="toolbar=no,location=no,directories=yes,menubar=yes,"; 
	sOption+="scrollbars=yes,width=850,height=600,left=100,top=25"; 
	
	var sWinHTML = document.getElementById('contentstart').innerHTML;
	

	if (arguments.length > 0)
	{
		var divPrint = document.getElementById(divp);
		if (divPrint)
			sWinHTML = divPrint.innerHTML; 
    }
    else
    {
		var withn = confirm("Do you want print number?\n(Click on OK to print with number, or Cancel to print withou number.)")
		if (!withn)
			sWinHTML = document.getElementById('CalendarContainer').innerHTML;
    }
    
	var winprint=window.open("","",sOption); 
	winprint.document.open(); 
	winprint.document.write('<html>'); 
	winprint.document.write('<LINK href="../Companies/Common/Themes/NewRPI/Styles/Default.css" rel="Stylesheet">');
	winprint.document.write('<body>'); 
	winprint.document.write(sWinHTML);          
	winprint.document.write('</body></html>'); 
	winprint.document.close(); 
	//var yourstate=winprint.confirm("Are you sure to print?")
	//if (yourstate) //Boolean variable. Sets to true if user pressed "OK" versus "Cancel."
	//{
	//	winprint.alert("Good!")
	//	winprint.print();
    //}
	winprint.focus(); 
}

function PrintJobPage() 
{ 
	var sOption="toolbar=no,location=no,menubar=yes,scrollbars=yes,resizable=yes"; 

	var sWinHTML = document.getElementById('hideforprint').innerHTML; 
   
	var winprint=window.open("","",sOption); 
	winprint.document.open(); 
	winprint.document.write('<html>');
	winprint.document.write('<LINK href="../Companies/Common/Themes/NewRPI/Styles/Default.css" rel="Stylesheet">');
	winprint.document.write('<body>'); 
	winprint.document.write(sWinHTML);          
	winprint.document.write('</body></html>'); 
	winprint.document.close(); 
	winprint.focus(); 
}


function PrintReport() 
{ 
	if (arguments.length < 2)
		return;
		
	var sOption="toolbar=no,location=no,menubar=yes,scrollbars=yes,resizable=yes"; 
	sOption+="scrollbars=yes,width=850,height=600,left=100,top=25"; 

	var winprint=window.open("","",sOption); 
	winprint.document.open(); 
	winprint.document.write('<html>');
	winprint.document.write('<LINK href="../Companies/Common/Themes/NewRPI/Styles/Default.css" rel="Stylesheet">');
	winprint.document.write('<body>'); 
	
	for (var j = 1; j < arguments.length; j++) {
		var sWinHTML = document.getElementById(arguments[j]).innerHTML; 
		winprint.document.write(sWinHTML);          
    }

	winprint.document.write('</body></html>'); 
	winprint.document.close(); 
	winprint.focus(); 
}


function GetReportAsy(d1, d2, div1, rType)
{
	// define divMessage for something wrong
	divMessage = div1;
	
	var fDate = document.getElementById(d1); 
	var tDate = document.getElementById(d2); 
	var divReport = document.getElementById(div1); 
	
	if (fDate.value.length == 10 && tDate.value.length == 10) {
		divReport.innerHTML = "<img src='../companies/common/images/waiting7.gif' />&#160;&#160;"
		divReport.innerHTML += "<span class='ContinueDay'>Getting Report...</span>";
		
		var f = '';
		if (arguments.length < 4)
			f = "AjaxMethod.GetDailyReport('"+fDate.value+"', '"+tDate.value+"', '"+div1+"', '0', GetReportAsy_CallBack);"; 
		else
			f = "AjaxMethod.GetDailyReport('"+fDate.value+"', '"+tDate.value+"', '"+div1+"', '"+rType+"', GetReportAsy_CallBack);"; 

		setTimeout(f, 300);
/*
		if (arguments.length < 4)
			AjaxMethod.GetDailyReport(fDate.value, tDate.value, div1, '0', GetReportAsy_CallBack); 
		else
			AjaxMethod.GetDailyReport(fDate.value, tDate.value, div1, rType, GetReportAsy_CallBack); 
*/		
	}
}

function GetReportAsy_CallBack(response) {
	if (response.value != null) 
	{
		var al = response; 
		var divReport = document.getElementById(al.value[0]); 
	    
	    if(al.value.length>2) 
		{
//			if (al.value[1] == "1")
//				document.write(al.value[2]);
//			else
				divReport.innerHTML = al.value[2];
		}
		else
			divReport.innerHTML = "<font color=red>get report Error...</font>";
	}
	else
	{
		var divOut = document.getElementById(divMessage); 
		divOut.innerHTML = "<font color=red>get report from database error...</font>";
	}
}

// function for trend
function GetTrendAsy(d1, d2, div1, duration, rType)
{
	var divReport = document.getElementById(div1); 
	
	divReport.innerHTML = "<img src='../companies/common/images/waiting7.gif' />&#160;&#160;"
	divReport.innerHTML += "<span class='ContinueDay'>Getting Report...</span>";
		
	var f = '';
	if (arguments.length < 5)
		f = "AjaxMethod.GetTrendTable('" +d1+"', '" +d2+"', '"+div1+"', '"+duration+"', '0', GetTrendAsy_CallBack);"; 
//		f = "AjaxMethod.GetTrendTable('"+fDate.value+"', '"+tDate.value+"', '"+div1+"', '0', GetTrendAsy_CallBack);"; 
	else
		f = "AjaxMethod.GetTrendTable('"+d1+"', '"+d2+"', '"+div1+"', '"+duration+"','"+rType+"', GetTrendAsy_CallBack);"; 

	setTimeout(f, 300);
		
	var floatdiv = document.getElementById(div1);
	
	//var divPos = getDivPosition(document.getElementById(thisanchor));

	//floatdiv.style.top = divPos.y - 100;
	//floatdiv.style.left = divPos.x;

		floatdiv.style.display = "block";

}

function GetTrendAsy_CallBack(response) {
	if (response.value != null) 
	{
		var al = response; 
		var divReport = document.getElementById(al.value[0]); 
	    
	    if(al.value.length>2) 
		{
			divReport.innerHTML = al.value[2];
		}
		else
			divReport.innerHTML = "<font color=red>get report Error...</font>";
	}
	else
		alert("no data...");
}


function GetContractAsy(d1, d2, email, sumBy)
{
	var divReport = document.getElementById('FloatingDiv'); 
	
		divReport.innerHTML = "<img src='../companies/common/images/waiting7.gif' />&#160;&#160;"
		divReport.innerHTML += "<span class='ContinueDay'>Getting Report...</span>";
		
		AjaxMethod.GetStaffContracts(d1, d2, email, sumBy, GetContractAsy_CallBack);

}

function GetContractAsy_CallBack(response) {
	var divReport = document.getElementById('FloatingDiv'); 
	if (response.value != null) 
	{
		var al = response; 
	    
	    if(al.value.length>0) 
		{
			var icnt = al.value[0];
			if (icnt>8)
				divReport.style.height = 200;
			else
				divReport.style.height = null;
				
			divReport.innerHTML = al.value[1];
		}
		else
			divReport.innerHTML = "<font color=red>get report Error...</font>";
	}
	else
		divReport.innerHTML = "<font color=red>get report from database error...</font>";
}


function getDetail(d1, d2, email, sumBy, thisanchor) {
	showFloat(thisanchor);
	GetContractAsy(d1, d2, email, sumBy);
}


// open the rate input
function showFloat(thisanchor)
{
	var floatdiv = document.getElementById('FloatingDiv');
	
	var divPos = getDivPosition(document.getElementById(thisanchor));

	floatdiv.style.top = divPos.y - 100;
	
	var botX = document.body.scrollLeft+document.body.offsetWidth/2;
	var botWidth = document.body.scrollLeft+document.body.offsetWidth;

	if (!document.all)
	{
		botX = document.body.scrollLeft+window.innerWidth/2;
		botWidth = document.body.scrollLeft+window.innerWidth
	}
	
	botX = botX - 200;
	botWidth = botWidth - botX - 100;

	floatdiv.style.left = (divPos.x > botX)?botX:divPos.x;

	floatdiv.style.width = botWidth;

	floatdiv.style.display = "block";
}

// print the table
function printFloat()
{
	var sWinHTML = document.getElementById('FloatingDiv').innerHTML; 
	
	// delete link button 
	var script = "<script language='javascript'> ";
	script += "	var arr = document.getElementsByTagName('a'); "
	script += "	for (i=arr.length-1; i>=0; i--) "; 
	script += "	arr[i].parentNode.removeChild(arr[i]); ";
	script += "</script>"
	
	var sOption="toolbar=no,location=no,menubar=yes,scrollbars=yes,resizable=yes"; 
   
	var winprint=window.open("","",sOption); 
	winprint.document.open(); 
	winprint.document.write('<html>');
	winprint.document.write('<LINK href="../Companies/Common/Themes/NewRPI/Styles/Default.css" rel="Stylesheet">');
	winprint.document.write('<body>'); 
	winprint.document.write(sWinHTML);
	winprint.document.write(script);
	winprint.document.write('</body></html>'); 
   
	var yourstate=winprint.confirm("Are you sure to print?")
	if (yourstate) //Boolean variable. Sets to true if user pressed "OK" versus "Cancel."
	{
		winprint.print();
    }

	winprint.document.close(); 
	winprint.focus(); 
}

// open the rate input
function closeFloat(fDiv)
{
	if (arguments.length == 0)
		var fDiv = 'FloatingDiv';
		
	var floatdiv = document.getElementById(fDiv);
	floatdiv.style.display = "none";
}

Number.prototype.NaN0=function(){return isNaN(this)?0:this;}
function getDivPosition(e){
	var left = 0;
	var top  = 0;
	while (e.offsetParent){
		left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
		top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
		e     = e.offsetParent;
	}


	left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
	top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);

	return {x:left, y:top};

}


