var ENTER_KEY_CODE = "13" ;
var ERR_SPECIAL_CHARS = "contains a special character";
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." ;
var regexp = new RegExp(/ /);
var ERR_NO_KEYWORDS_REMOVE = "There are no keywords to remove.";
var ERR_SEL_ITEM_REMOVE = "Please select a keyword to remove.";
var ERR_ENTER_KEYWORD_SEARCH="Please enter the keywords for performing the search.";
var ERR_ENTER_KEYWORD ="Please enter a NameServer.";
var ERR_SPECIAL = ",please enter a text without any special characters.";
var DUPLICATE_VALUE = "is Entered Already";
		

function createOption(value, text)
{
	var oOption = document.createElement("OPTION");
	oOption.value = value;
	oOption.text = text;
	return oOption;
}

function onLoad()
{
	
	Form1.selKeyword1.focus();
}
// Adds the keyword to keyword 1 select box
function AddKeyword1()
{
	AddKeyword(Form1.selKeyword1,Form1.selKeyword1,1);
}


function AddKeyword(TextBox, ListBox, KeyWordIndex)
{
		TextBox.value =  RTrim(TextBox.value);	
    	var duplicate=true;
    	if(ListBox.length >= 0)
		{	
				count = ListBox.length - 1;
				while(count >=0)
			{			
				if(TextBox.value.toLowerCase()==ListBox.options[count].value.toLowerCase())
				{
					alert(TextBox.value+" "+DUPLICATE_VALUE);
					TextBox.value="";
					duplicate=false;										
				}
				count--;				
			}		
		}
		if(duplicate)				
		{
			var iChars = "`~!@#$%^&*()+=[]\\\';,/{}|\":<>?_";	
			var myregexp = new RegExp(/^\s*|\s*$/);	 
			TextBox.value = TextBox.value.replace(myregexp,"");				
			
		 if(TextBox.value.match(regexp))
		{
			alert(SPACE_BETWEEN_STRINGS);
			TextBox.focus();			
			return false;
		}					
			
			
			// Check for keyword length. Max 64 characters
			if(TextBox.value.length >64)
			{
				alert(ERR_TEXT_EXCEEDS);
				return false;
			}
	
			// Check for special characters in the keyword
			for(i = 0;i < TextBox.value.length; i++)
			{
				if (iChars.indexOf(TextBox.value.charAt(i)) != -1)
				{
					alert("Name Server Name" + " " + ERR_SPECIAL_CHARS +" "+"'" + TextBox.value.charAt(i)+"'"+ ERR_SPECIAL);
					return false;
				}
			}
			// If the keyword is not empty, add to list
			if (TextBox.value != "")
			{			
				ListBox.options.add(createOption(TextBox.value,TextBox.value)) ;
				TextBox.value = "";
				TextBox.focus();
			}
			else
			{
				alert(ERR_ENTER_KEYWORD);
				TextBox.focus() ;
			}	
		}
		
}

function keypressed()
{
	
	var flag = true ;
	if (event.keyCode==ENTER_KEY_CODE)
	{
		
		flag = false;					
		switch(event.srcElement.name) 
		{
			
			case "selKeyword1" :	AddKeyword1();									
									break;
			case "txtDelimiters" :	AddDelimiters();
									break ;
			case "txtKeyword2":		AddKeyword2();
									break;
			case "txtKeyword3":		AddKeyword3();
									break;
			default :	flag = true;
						break ;			
		}
	}	
	return flag; 
}	

	
function RemoveKeyword1()
{
	RemoveKeyword(Form1.selKeyword1,Form1.hidselKeyword1) ;
}

// removes the selected keyword from the keyword select box 
function RemoveKeyword(selectBox,hiddenBox)
{
	
	if (selectBox.selectedIndex >= 0) 
	{
		count = selectBox.length - 1;
		while (count >= 0 )
		{
			if (selectBox.options[count].selected)
			{
			
				selectBox.options.remove(count)
			}
			count--;
		}
		hiddenBox.value = "";					
		
	}	
	else
	{
	if(selectBox.length ==0)
		{
		alert(ERR_NO_KEYWORDS_REMOVE);
		}
		else
		{				  
		alert(ERR_SEL_ITEM_REMOVE);
		}
	}

}



// 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;
	}
	if ( Form1.selKeyword1.length == 0)
		{
			alert(ERR_ENTER_KEYWORD_SEARCH) ;
			return false;
		}	
	Form1.txtPassedStartDate.value = Form1.txtStartDate.value;
	Form1.txtPassedEndDate.value = Form1.txtEndDate.value;	
	Form1.txtSortBy.value = Form1.selSortBy.value;
	populate();
	return true ;		
}

function populate()
{
	Form1.hidselKeyword1.value = "";	
	
	for(i = 0; i < Form1.selKeyword1.length; i++)
	{
		if(Form1.hidselKeyword1.value != "")
		{
			Form1.hidselKeyword1.value +=  "," + Form1.selKeyword1.options[i].value;
		}
		else
		{
			Form1.hidselKeyword1.value = Form1.selKeyword1.options[i].value;
		}
	}
}



// Displays the help
function MovementsByNameServerHelp()
{
	var sFeatures = "height=500px, width=600px, scrollbars=1, help=0;"
	window.open("/GddReports/help/MovementsByNameServerHelp.htm","",sFeatures);
}

function DisplayStartCalendar()
{
	var s = Form1.txtPassedStartDate.value;	
	if (s == "0") 
	{
		DateInput('txtStartDate', true, 'MM/DD/YYYY');
	}
	else
	{
		DateInput('txtStartDate', true, 'MM/DD/YYYY', s);
	}
	
}
// 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 gain in different styles
function DisplayGain(gain)
{
	if (gain != null && gain != "")
	{
		var strGain = new String(gain);
		if (strGain.indexOf("-") == 0)
		{
			document.writeln('<span style="color:red">');
			document.writeln("-") ;
		}
		else
		{
			document.writeln('<span style="color: #008000">');
		}
		DisplayinThousands(strGain) ;
		document.writeln('</span>');		
	}
}


// 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) ;
}

// opens the drill down to different pages
function openDrillDown(type,nameServer,date)
{	
	var sDate = date;	
	var pageToOpen = "" ; 
	var lbltitle = "2" 
	switch(type)
	{
		case 1:	
				pageToOpen = "NewDomains.aspx" ;
				break ;
				
		case 2:	
				pageToOpen = "InDomains.aspx" ;
				break ;
				
		case 3:	
				pageToOpen = "OutDomains.aspx" ;
				break ;
				
		default:	
				pageToOpen = "RemovedDomains.aspx" ;
				break ;
				
	}
	window.location = pageToOpen+"?Date="+sDate+"&NameServer="+nameServer+"&Title="+lbltitle;
}

function DisplayLink(type,nameServer,date)
{
	var nameServerString = "'" + nameServer + "'" ;
	var link = '<a href="javascript:openDrillDown(' +  type + ',' + nameServerString + ',\'' + date + '\');" title ="' ;
	var title = "" ;
	
	switch(type)
	{
		case 1:	
				title = 'Click to view the list of new domains.">' ;
				break ;
				
		case 2:	
				title = 'Click to view the list of domains moved in">' ;
				break ;
				
		case 3:	
				title = 'Click to view the list of domains moved out.">' ;
				break ;
				
		default:	
				title = 'Click to view the list of domains removed.">' ;
				break ;
				
	}
	link = link + title ;
	document.writeln(link) ;
}

function DisplayNumeric(value,type,nameServer,date)
{
	if (value != "0" ) 
	{
		DisplayLink(type,nameServer,date) ;
		DisplayinThousands(value) ;	
		document.writeln("</a>") ;	
	}	
	else
	{
		document.writeln(value) ;	
	}	
}

// displays the out as negative
function DisplayNegative(value,type,nameServer,date)
{
	if (value != "0")
	{
		DisplayLink(type,nameServer,date) ;
		document.writeln("-") ;	
		DisplayinThousands(value) ;	
		document.writeln("</a>") ;			
	}	
	else
	{
		document.writeln(value) ;	
	}	
}

// function for triming the spaces in the right
function RTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;	
	var strTemp = "";
	if(v_length < 0)
		{
			return"";
		}
	var iTemp = v_length -1;
	while(iTemp > -1)
		{
			if(VALUE.charAt(iTemp) == w_space)
			{
			}
			else
			{
				strTemp = VALUE.substring(0,iTemp +1);
				break;
			}
	iTemp = iTemp-1;

		} 
return strTemp;

}
