/*
 * Developed for the CRIF-HCI project by Thomson Consulting International.
 * TCI, 31st Floor, Centre Point, 103 New Oxford Street, London. WC1A 1PG.
 *
 * SOURCE FILE
 *	country.js
 *
 * DESCRIPTION
 *	Contains a set of functions related to form elements for choosing
 *	countries and US states.
 *
 * FUNCTIONS
 *	CountrySelected ( form )
 *	StateSelected ( form )
 *	TickerSelected ( form )
 *	CompanySelected ( form )
 */

function countrySelected ( form ) { return CountrySelected2(form); }
function stateSelected ( form ) { return StateSelected2(form); }

function countrySelectedList ( form ) { return CountrySelected3(form); }
function stateSelectedList ( form ) { return StateSelected3(form); }


function CountrySelected2 ( form ) {
	
	// Get the selected index value for the country
	var CountrySelectedIndex = form.country.selectedIndex;
	assert (CountrySelectedIndex >= 0, 'Country selected index was not found');

	// Set up a whole bunch of local variables and assert they are OK
	var CountryNS = getOptionIndex ( form.country , 'NS'); 
	assert ( CountryNS != null , 'Not selected value for the country drop down _NS_ does not exist' );
 
	var CountryNA = getOptionIndex ( form.country , 'NA');  
	assert ( CountryNA != null , 'Not selected value for the country drop down _NA_ does not exist' );

	var CountryUSA = getOptionIndex ( form.country , '840');
	assert ( CountryUSA != null , 'Not selected value for the country drop down _USA_ does not exist' );

	var StateNA = getOptionIndex ( form.state , 'NA');
	assert ( StateNA != null , 'Not selected value for the state drop down _NA_ does not exist' );
      
	var StateALL = getOptionIndex ( form.state , '');    
	assert ( StateALL != null , 'Not selected value for the state drop down _ALL_ does not exist' );

	// If the Country Option is set to "Not Applicable"
	if ((CountrySelectedIndex == CountryNA)) {
	
		// Then set the state to not applicable
		form.state.options[StateNA].selected = true;
	
		
		// Check that we are on a form that has the ticker element
		// could elaborate this to find by name the ticker thing
		// and set the search type to ticker
		if (form.radiobutton != null) {
			form.radiobutton[1].click();
		}
	} else	{
		// If the country option is set to USA
		if (form.country.options[CountrySelectedIndex].value == "840") {
			// Set the state to all
			form.state.options[StateALL].selected = true;
			// Set the search type to company
			if (form.radiobutton != null) form.radiobutton[0].click();
			}
		else {
			// Set the State to NA and Search Type to Company
			form.state.options[StateNA].selected = true;
			if (form.radiobutton != null) form.radiobutton[0].click()
			}

		}
}

function CountrySelected3 ( form ) {
	// Get the selected index value for the country
	var CountrySelectedIndex = form.country.selectedIndex;
	assert (CountrySelectedIndex >= 0, 'Country selected index was not found');

	// Set up a whole bunch of local variables and assert they are OK
	var CountryNS = getOptionIndex ( form.country , 'NS'); 
	assert ( CountryNS != null , 'Not selected value for the country drop down _NS_ does not exist' );
 
	var CountryNA = getOptionIndex ( form.country , 'NA');  
	assert ( CountryNA != null , 'Not selected value for the country drop down _NA_ does not exist' );

	var CountryUSA = getOptionIndex ( form.country , '840');
	assert ( CountryUSA != null , 'Not selected value for the country drop down _USA_ does not exist' );

	var StateNA = getOptionIndex ( form.state , 'NA');
	assert ( StateNA != null , 'Not selected value for the state drop down _NA_ does not exist' );
      
	var StateALL = getOptionIndex ( form.state , '');    
	assert ( StateALL != null , 'Not selected value for the state drop down _ALL_ does not exist' );

	// If the Country Option is set to "Not Applicable"
	if ((CountrySelectedIndex == CountryNA)) {
	
		// Then set the state to not applicable
		form.state.options[StateNA].selected = true;
	
		
		// Check that we are on a form that has the ticker element
		// could elaborate this to find by name the ticker thing
		// and set the search type to ticker
		if (form.radiobutton != null) {
			form.radiobutton[1].click();
		}
	} else	{
		// If the country option is set to USA
		if (form.country.options[CountrySelectedIndex].value == "840") {
			// Set the state to all
			form.state.options[StateALL].selected = true;
			// Set the search type to company
			if (form.radiobutton != null) form.radiobutton[0].click();
			}
		else {
			// Set the State to NA and Search Type to Company
			form.state.options[StateNA].selected = true;
			if (form.radiobutton != null) form.radiobutton[0].click()
			}

		}
}

function StateSelected2 ( form ) {
	// Get the state selected index value
	
	var StateSelectedIndex = form.state.selectedIndex;
	var CountrySelectedIndex = form.country.selectedIndex;
	

	// Set up a whole bunch of local variables and assert they are OK
	var CountryNS = getOptionIndex ( form.country , 'NS'); 
	assert ( CountryNS != null , 'Not selected value for the country drop down _NS_ does not exist' );



	var CountryNA = getOptionIndex ( form.country , 'NA');
	assert ( CountryNA != null , 'Not selected value for the country drop down _NA_ does not exist' );

	var CountryUSA = getOptionIndex ( form.country , '840');
	assert ( CountryUSA != null , 'Not selected value for the country drop down _USA_ does not exist' );

	var StateNA = getOptionIndex ( form.state , 'NA');
	assert ( StateNA != null , 'Not selected value for the state drop down _NA_ does not exist' );
      
	var StateALL = getOptionIndex ( form.state , 'ALL');    
	assert ( StateALL != null , 'Not selected value for the state drop down _ALL_ does not exist' );
	
	var State
	
 	// If state has been set to Not applicable and country is set to
 	// Not Applicable
 	if ((StateSelectedIndex == StateNA) && (CountrySelectedIndex == CountryNA)){
		// Set the search type to ticker
		if (form.radiobutton != null) {
			form.radiobutton[1].click();
		}
	} 	
	// Else if State is set to Not Applicable and country is set to USA
	else {
		if ((StateSelectedIndex == StateNA )&& (CountrySelectedIndex == CountryUSA)){
			// Then set country to Select a Country
			
			//form.state.options[StateNA].selected = true;
			form.country.options[CountryNS].selected = true;
			
			// Set the search type to Company
			if (form.radiobutton != null) {
				form.radiobutton[0].click();
			}
		}
		// Else set the country to USA
		else {
			form.country.options[CountryUSA].selected = true;
			// Set the search type to Company
			if (form.radiobutton != null) {
				form.radiobutton[0].click();
			}
		}
	}
}


function StateSelected3 ( form ) {
	// Get the state selected index value

	var StateSelectedIndex = form.state.selectedIndex;
	var CountrySelectedIndex = form.country.selectedIndex;


	// Set up a whole bunch of local variables and assert they are OK
	var CountryNS = getOptionIndex ( form.country , 'NS'); 
	assert ( CountryNS != null , 'Not selected value for the country drop down _NS_ does not exist' );

	var CountryNA = getOptionIndex ( form.country , 'NA');  
	assert ( CountryNA != null , 'Not selected value for the country drop down _NA_ does not exist' );

	var CountryUSA = getOptionIndex ( form.country , '840');
	assert ( CountryUSA != null , 'Not selected value for the country drop down _USA_ does not exist' );

	var StateNA = getOptionIndex ( form.state , 'NA');
	assert ( StateNA != null , 'Not selected value for the state drop down _NA_ does not exist' );
      
	var StateALL = getOptionIndex ( form.state , 'ALL');    
	assert ( StateALL != null , 'Not selected value for the state drop down _ALL_ does not exist' );

 	// If state has been set to Not applicable and country is set to
 	// Not Applicable
 	if ((StateSelectedIndex == StateNA) && (CountrySelectedIndex == CountryNA)){
	
		// Set the search type to ticker
		if (form.radiobutton != null) {
			form.radiobutton[1].click();
		}
	} 	
	// Else if State is set to Not Applicable and country is set to USA
	else {
		if ((StateSelectedIndex == StateNA )&& (CountrySelectedIndex == CountryUSA)){
			// Then set country to Select a Country
			form.country.options[CountryNS].selected = true;
			// Set the search type to Company
			if (form.radiobutton != null) {
				form.radiobutton[0].click();
			}
		}
		// Else set the country to USA
		else {
			form.country.options[CountryUSA].selected = true;
			// Set the search type to Company
			if (form.radiobutton != null) {
				form.radiobutton[0].click();
			}
		}
	}
}


