function OnRequestReseller(objCheckbox, strPrefix)
{
	var bDisabled = !objCheckbox.checked ;
	var objForm = objCheckbox.form ;
	
	for(var i=0; i < objForm.elements.length; i++) 
	{
		var element = objForm.elements[i];
		if( element.name.indexOf(strPrefix) == 0 )
		{
			element.disabled = bDisabled ;
		}
	}
}

function OnChangeOptionEnable( objSelect, objInput, strCompare )
{
	var iIndex = objSelect.selectedIndex ;
	if( iIndex == -1 )
		return ;

	var bDisabled = true ;
	if( objSelect.options[iIndex].value == strCompare )
	{
		bDisabled = false ;
	}

	objInput.disabled = bDisabled ;
}

function OnChangeOptionEnableCountry( objSelect, objInput, strCompare )
{
	var iIndex = objSelect.selectedIndex ;
	if( iIndex == -1 )
		return ;

	var bDisabled = true ;
	if( objSelect.options[iIndex].value[3] == strCompare )
	{
		bDisabled = false ;
	}

	objInput.disabled = bDisabled ;
	
	var objRow = document.getElementById('nonMilitaryRow') ;
	if( objRow != null )
	{
		if( bDisabled == true )
			objRow.style.display = "none" ;
		else
			objRow.style.display = "table-row" ;
	}
}

function FindValueInSelection( objSelect, strValue )
{
	var options = objSelect.options ;
	var iLength = options.length ;

	for(var i=0;i<iLength;i++)
	{
		if( options[i].value == strValue )
			return i ;
	}
	
	return -1 ;
}

function SelectAllOptions( objSelect )
{
	var options = objSelect.options ;
	var iLength = options.length ;

	for(var i=0;i<iLength;i++)
	{
		options[i].selected = true ;
	}
}

function OnRequestAddCountry( objSelect, objDestination )
{
	var iIndex = objSelect.selectedIndex ;
	if( iIndex == -1 )
		return ;
		
	var objSelection = objSelect.options[iIndex]
		
	strCountry = objSelection.value ;
	if( FindValueInSelection(objDestination, strCountry) != -1 )
		return ;
		
	var objOption = document.createElement('option');
	objOption.value = objSelection.value ;
	objOption.text = objSelection.text ;
	
	try
	{
		objDestination.add(objOption, null) ;
	}
	catch(ex)
	{
		objDestination.add(objOption) ;
	}
	
	SelectAllOptions(objDestination) ;
}

function OnRequestRemoveCountry( objSelect, objDestination )
{
	var iIndex = objSelect.selectedIndex ;
	if( iIndex == -1 )
		return ;
		
	strCountry = objSelect.options[iIndex].value ;
	
	iIndex = FindValueInSelection(objDestination, strCountry) ;
	if( iIndex == -1 )
		return ;
		
	objDestination.remove(iIndex) ;
	
	SelectAllOptions(objDestination) ;
}

function OnCheckCountry( objSelect )
{
	var iIndex = objSelect.selectedIndex ;
	if( iIndex == -1 )
		return ;
		
	var strCountry = objSelect.options[iIndex].value ;

	var c = strCountry[3] ;
	if( c == 'S' )
	{
		alert("Export license required for this country apart from the Fiber Modeler range, please contact us directly at licenses@simulayt.com") ;
	}
	else if( c == 'O' )
	{
		alert("Please complete the \"Undertaking of Non-military Use\" unless you are requesting a license from the Fiber Modeler range") ;
	}
}

function AddOptionsToSelect( objSelect, options, selectedIndex )
{
	objSelect.length = 0 ;
	
	for(var i=0;i<options.length;i++)
	{
		var objOption = document.createElement('option');
		objOption.value = options[i] ;
		objOption.text = options[i] ;
		
		if( i == selectedIndex )
		{
			objOption.selected = true ;
		}
		
		try
		{
			objSelect.add(objOption, null) ;
		}
		catch(ex)
		{
			objSelect.add(objOption) ;
		}
	}
}

function OnCheckLicensePurpose( objSelect )
{
	var iIndex = objSelect.selectedIndex ;
	if( iIndex == -1 )
		return ;
		
	var strPurpose = objSelect.options[iIndex].value ;

	if( strPurpose == "Trial" )
	{
		alert("Please complete the \"Trial License Form\"") ;
	}
	else if( strPurpose == "Transfer" )
	{
		alert("Please complete the \"Transfer Request Form\"") ;
	}
	else if( strPurpose == "Other" )
	{
		alert("Please enter the purpose in the box on the right") ;
	}
	
	objTimes = document.getElementById("licenseduration") ;
	if( objTimes != null )
	{
		if( strPurpose == "Trial" )
		{
			AddOptionsToSelect(objTimes, new Array("30 days", "Other"), 0) ;
		}
		else if( objTimes.length == 2 )
		{
			AddOptionsToSelect(objTimes, new Array("7 days", "30 days", "Year End", "Other"), 1) ;
		}
	}
	
	// Enable rows
	objRow = document.getElementById("trialRow") ;
	if( objRow != null )
	{
		if( strPurpose == "Trial" )
			objRow.style.display = "table-row" ;
		else
			objRow.style.display = "none" ;
	}
	
	objRow = document.getElementById("transferRow") ;
	if( objRow != null )
	{
		if( strPurpose == "Transfer" )
			objRow.style.display = "table-row" ;
		else
			objRow.style.display = "none" ;
	}
	
	objOther = document.getElementById("licensepurposeother") ;
	if( objOther != null )
	{
		if( strPurpose == "Other" )
			objOther.style.display = "inline" ;
		else
			objOther.style.display = "none" ;	
	}
}

