//Added a variable for UTC Upgrade Phase 2 to get vgnextoid after appling furl
//Start
var vgnextparam;

//Alternative to getParamValue() method	
		function getVgnextValue(vgnextid)
		{
			vgnextparam = vgnextid;
			return vgnextparam;
		}

//End
function getSelectedItem( countryOptionId, stateOptionId  )
		{
			var elem  = document.getElementById( countryOptionId );
	 		
	 		if( elem.options[ elem.selectedIndex ].text == "USA- North America" )
	 			 document.getElementById( stateOptionId ).disabled="";
	 		else
	 		{
	 			var stateOption = document.getElementById( stateOptionId );
	 			stateOption.disabled="disabled";
	 			stateOption.selectedIndex = 0;
	 		}
		}
		
		function getParamValue( paramName )
		{
			var elem = null; 
			 mainURL = window.location.search;
  			 //alert("mainURL = "+mainURL);
  			 
  			 URLparts = mainURL.split('?');
			 //alert("Arguments = "+URLparts[1]);
			 
			  Arguments = URLparts[1].split('&');
  			  //for (i in Arguments) 
      		 //alert("Argument "+i+" = "+Arguments[i]+"<p>");
 
			  for (i in Arguments) {
				pair = Arguments[i].split('=');
				
				if( pair[0] == paramName )
			  	{
			  		return pair[1];
			  	}
			}
		}
		
		function URLDecode(psEncodeString)
		{
		  // Create a regular expression to search all +s in the string
		  var lsRegExp = /\+/g;
		  // Return the decoded string
		  return unescape(String(psEncodeString).replace(lsRegExp, " "));
		}
		
		function prepareForm(countryOptionId, stateOptionId, baOptionId)
		{	
			 var elem = null; 
			 mainURL = window.location.search;
  			 //alert("mainURL = "+mainURL);
  			 
  			 URLparts = mainURL.split('?');
			 //alert("Arguments = "+URLparts[1]);
			 
			  Arguments = URLparts[1].split('&');
  			  //for (i in Arguments) 
      		 //alert("Argument "+i+" = "+Arguments[i]+"<p>");
 
			  for (i in Arguments) {
				pair = Arguments[i].split('=');
			  	
			  	if( pair[0] == 'country' )
			  	{
			  		elem  = document.getElementById( countryOptionId );
			  		setSelectedItem( elem, URLDecode(pair[1]) );
			  		getSelectedItem( countryOptionId, stateOptionId  );
			  	}
			  	else if( pair[0] == 'state' )
			  	{
			  		elem  = document.getElementById( stateOptionId );
			  		setSelectedItem( elem, URLDecode(pair[1]) );
			  	}
			  	else if( pair[0] == 'ba' )
			  	{
			  		elem  = document.getElementById( baOptionId );
			  		setSelectedItem( elem, URLDecode(pair[1]) );
			  	}
			  	//document.write("Parameter: "+pair[0]+"<br>");
			  	//document.write("Value    : "+pair[1]+"<p>");
  			  }
  			  
		}
		
		function configureCountryList(country)
		{
			elem  = document.getElementById( 'country' );
			setSelectedItem( elem, country );
			
			if( country != "USA- North America" )
				getSelectedItem('country', 'state');				
		}
		
		function configureStatesList(state)
		{
			elem  = document.getElementById( 'state' );
			setSelectedItem( elem, state );	
		}
		
		function setSelectedItem( elem, itemVal )
		{
			  	if( elem != null)
			  	{
				  	for( var i = 0 ; i < elem.options.length; i++)
					{
						if( itemVal == elem.options[i].text )
						{
							elem.selectedIndex = i;
							break;
						}
					}
				}
		}
		
		function doSubmit(location_form, countryOptionId, stateOptionId, baOptionId )
		{
			
			
			var countryElem  = document.getElementById( countryOptionId );
	 		var stateElem  = document.getElementById( stateOptionId );
	 		var baElem  = document.getElementById( baOptionId );
	 		
	 		if( baElem.options[ baElem.selectedIndex ].text == "Select")
	 		{
	 			alert("Please select a Business Area");
	 			baElem.focus();
	 			return;
	 		}
	 		
	 		if( countryElem.options[ countryElem.selectedIndex ].text == "Select")
	 		{
	 			alert("Please select a Country");
	 			countryElem.focus();
	 			return;
	 		}
	 		
	 		var country = countryElem.options[ countryElem.selectedIndex ].text;
	 		country = country.split("-", 1);
	 		
	 		if( stateElem.options[ stateElem.selectedIndex ].text == "Select" &&
	 				country == "USA")
	 		{
	 			alert("Please select a State");
	 			stateElem.focus();
	 			return;
	 		}
	 		
	 		//Added for upgrade phase 2 alternative for getParamValue method
	 		//document.getElementById(location_form).vgnextoid.value = getParamValue( 'vgnextoid' );
			document.getElementById(location_form).vgnextoid.value = vgnextparam;
			//End upgrade phase 2
			
			document.getElementById(location_form).baIndex.value=baElem.options.selectedIndex;
			document.getElementById(location_form).countryIndex.value=countryElem.options.selectedIndex ;
			document.getElementById(location_form).stateIndex.value=stateElem.options.selectedIndex ;
	 		
			document.getElementById(location_form).submit();
			
		}
		
		
		function checkLocationEnter(e)
		{ 
			var characterCode;
			
			if(e && e.which){ 
				e = e;
				characterCode = e.which;
			}
			else{
				e = event;
				characterCode = e.keyCode;
			}
			
			if(characterCode == 13){ 
				doSubmit('location_form','country','state','ba' );
				return false;
			}
			else{
				return true;
			}

		}
		
		function resetForm()
		{
			document.location_form.ba.options.selectedIndex = "0";
			document.location_form.country.options.selectedIndex = "0";
			document.location_form.state.options.selectedIndex = "0";
			document.location_form.state.disabled="disabled";
		}
