var ENTER_KEY_CODE = "13" ;
var ERR_FUTURE_DATE="Reports can not be generated for a date in future, please select a past/current date.";
var ERR_COMPARE_DATE="Start Date can not be later than End Date, Please select a date before End Date." ;

// Validates the input on submit
function Validate()
{	
	if (CheckFutureDate(Form1.txtStartDate)== false || CheckFutureDate(Form1.txtEndDate) == false || CompareDates() == false)
	{
		var s = Form1.txtPassedStartDate.value;	
		if (s != "0") 
		{
			var dataGrid = document.getElementById("reportDataGrid");
			if (dataGrid != null)
			{
				dataGrid.style.visibility = "hidden" ;
				lblLegend.style.display = "none" ;
			}
			var labelCheck = document.getElementById("lblCheck");
			if (labelCheck != null)
			{
				labelCheck.style.display = "none" ;
			}
		}
		return false;
	}
	Form1.txtPassedStartDate.value = Form1.txtStartDate.value;
	Form1.txtPassedEndDate.value = Form1.txtEndDate.value;	
	return true ;		
}

// handles the keypressed event for enter key
function keypressed()
{
	if (event.keyCode==ENTER_KEY_CODE)
	{
		Form1.hidEnterkey.value = "y" ; 
	}
}	

function ChangeGraph(chartType)
{
	//alert(chartType);
	if(chartType == "COM")
	{
		//alert(document.getElementById("MarketSharePanel"));
		//alert(document.getElementById("ChangesPanel"));
		//document.getElementById("MarketSharePanel").style.visibility = "hidden";
		//document.getElementById("ChangesPanel").style.visibility = "visible";
		
		document.getElementById("OthersPanel").style.display = "none";
		document.getElementById("COMPanel").style.display = "block";
	}
	else
	{
		//document.getElementById("ChangesPanel").style.visibility = "hidden";
		//document.getElementById("MarketSharePanel").style.visibility = "visible";
		
		document.getElementById("OthersPanel").style.display = "block";
		document.getElementById("COMPanel").style.display = "none";
	}
}

// Displays the help
function TLDDomainCountHelp()
{
	var sFeatures = "height=500px, width=600px, scrollbars=1, help=0;"
	window.open("../help/TLDDomainCountsHelp.htm","",sFeatures);
}

function DisplayStartCalendar()
{
	var s = Form1.txtPassedStartDate.value;	
	if (s == "0") 
	{
		DateInput('txtStartDate', true, 'MM/DD/YYYY',null,true);
	}
	else
	{
		DateInput('txtStartDate', true, 'MM/DD/YYYY', s,true);
	}
	
}
// Displays the calendar
function DisplayEndCalendar()
{
	var s = Form1.txtPassedEndDate.value;	
	if (s == "0") 
	{
		DateInput('txtEndDate', true, 'MM/DD/YYYY');
	}
	else
	{
		DateInput('txtEndDate', true, 'MM/DD/YYYY', s);
	}
	
}
// validates for future date
function CheckFutureDate(dateControl)
{
	var currentDate = new Date() ;
	var selectedDate = new Date(dateControl.value) ;
	if (selectedDate > currentDate)
	{
		alert(ERR_FUTURE_DATE);
		dateControl.focus();
		dateControl.select();
		return false;
	}
	return true;	
}
// compares the start and end date
function CompareDates()
{
	var startDate = new Date(Form1.txtStartDate.value) ;
	var endDate = new Date(Form1.txtEndDate.value) ;
	if (startDate > endDate)
	{
		alert(ERR_COMPARE_DATE);
		Form1.txtStartDate.focus();
		Form1.txtStartDate.select();
		return false;
	}
	return true;	
}
// displays the total in bold
function DisplayBold(check,value)
{
	if (check == "Total")
	{
		document.writeln("<b>");
		DisplayinThousands(value);
		document.writeln("</b>");
	}
	else
	{
		DisplayinThousands(value);
	}
}

// displays the numbers with commas
function DisplayinThousands(value)
{
	var stringValue = "" ;
	var remainder ; 
	var remainderString = "" ;
	var i ; 
	var count ;
	
	var numericValue = value;
	if (isNaN(numericValue))
	{
		document.writeln(value) ;
		return;
	}
	
	if (numericValue < 0) 
	{
		numericValue = (-1) * numericValue ;
	}
	
	while (numericValue >= 1000)
	{
		remainder = numericValue % 1000 ;
		numericValue = Math.floor(numericValue / 1000) ;
		remainderString = "" + remainder;
		count = 3-remainderString.length ;
		for (i = 0 ; i < count ; i++)
		{
			remainderString = "0" + remainderString ;
		}		
		stringValue =  "," + remainderString + stringValue; 	
	}
	stringValue = numericValue + stringValue  ;
	document.writeln(stringValue) ;
}
