var global_originalDomain = "johndeere.com";
var global_currentDomain = "deere.com";

function formatURLForSpaces(input)
{
    return escape(input).replace(/\+/g,"%2b");
}

function clearAllOptions(selectObject)
{
	while(selectObject.options.length > 0)
		selectObject.options[0] = null;
}

function setOptions()
{
	var configForm = document.getElementById("configForm");
	configurationOptions = configForm.displayConfigurationOptions.value;
	if(configurationOptions=="true")
	{
		userActions = setOptions.arguments;
		sel = document.configForm.userActionSelect;
		
			
		pos = 0;
		textArray = new Array();
		valueArray = new Array();
		
		for(; pos < userActions.length; pos++)
		{
			if (userActions[pos]=="configureProduct")
			{
				textArray[pos]= document.configForm.configureProductTranslation.value;
				valueArray[pos]="configureProduct";
			}
			else if (userActions[pos]=="accessorize")
			{
				textArray[pos]= document.configForm.accessorizeTranslation.value;
				valueArray[pos]="accessorize";
			}
			else if (userActions[pos]=="needsAnalysis")
			{
				textArray[pos]= document.configForm.needsAnalysisTranslation.value;
				valueArray[pos]="needsAnalysis";
			}
		}			
			//Delete all existing options
		clearAllOptions(sel)
		//Add the new options
		for (i = 0; i < pos; i++)
		{
			sel.options[i] = new Option(textArray[i], valueArray[i]);
		}
	}
		
}

function setDefaultOption(defaultOption)
{
	var configForm = document.getElementById("configForm");
	configurationOptions = configForm.displayConfigurationOptions.value;
	if(configurationOptions=="true")
	{
		sel = document.configForm.userActionSelect;
		sel.options[0].selected = true;
		for (k=1; k<sel.options.length; k++)
		{
			if(sel.options[k].value==defaultOption)
			{	
				sel.options[k].selected = true;
			}
		}
	}
	
}


function getCategory()
{
	if (document.configForm.category.length==null)
	{
		return formatURLForSpaces(document.configForm.category.value);
	}
	else
	{
		for(i=0; i<document.configForm.category.length; i++)
		{
			if (document.configForm.category[i].checked==true)
			{
				return formatURLForSpaces(document.configForm.category[i].value);
			}
		}
	}
	return "";
}

function getUserAction()
{
	return formatURLForSpaces(document.configForm.userActionSelect.options[document.configForm.userActionSelect.selectedIndex].value);
}

function getCountry()
{
	if(document.configForm.countrySelect != null)
		return document.configForm.countrySelect.options[document.configForm.countrySelect.selectedIndex].value;
	else
		return null;
}

function browserRedirect(redirectURL)
{
	if(navigator.appVersion.indexOf("MSIE 4") == -1)
	{
		document.location.replace(redirectURL);
	}
	else
	{
		document.location.href=redirectURL;
	}
}

function global_getDomain(aUrl)
{
	domain = aUrl;
	
	// Remove Beginning.
   if (domain.length>7 && domain.toUpperCase().indexOf("HTTP://")==0)
   {
   	domain = domain.substring(7,domain.length);
   }

	// Remove ending.
   if (domain.length>0 && domain.indexOf("?")>0)
   {
   	domain = domain.substring(0,domain.indexOf("?"));
   }
   if (domain.length>0 && domain.lastIndexOf("/")>6)
   {
   	domain = domain.substring(0,domain.lastIndexOf("/"));
   }
   if (domain.length>0 && domain.lastIndexOf(":")>4)
   {
   	domain = domain.substring(0,domain.lastIndexOf(":"));
   }

	// Take only last two sections.   
   domainParts = domain.split('.');
   if (domainParts.length>2)
   {
   	domain = domainParts[domainParts.length-2]+"."+domainParts[domainParts.length-1]
   }

	return domain;
}

function global_switchDomain(aUrl, domainSwitchMessage)
{
	// The following cannot currently be used to switch port numbers!
	originalDomain = global_originalDomain.toUpperCase();
	currentDomain = global_currentDomain.toUpperCase();

	//For local testing...originalDomain = "localhost".toUpperCase();
	//For local testing...currentDomain = "127.0.0.1";

	if (aUrl.toUpperCase().indexOf(originalDomain)>-1)
	{
		domain = global_getDomain(aUrl);
		start = aUrl.toUpperCase().indexOf(domain.toUpperCase());
		end = start+domain.length;

		if (domain.toUpperCase().indexOf(originalDomain)>-1)
		{
			auditImage = new Image(1,1);
			referrerPage = document.referrer;
			referrerPage = formatReferrerForLogging(referrerPage);
			referrerPage = referrerPage + "_" + document.title;
			auditImage.src = "/servlet/com.deere.u90947.eproducts.view.servlets.AuditTrailServlet?webPageName=" + referrerPage + "&rnd=" + Math.round(Math.random()*900);
			resultUrl = aUrl.substring(0,start) + currentDomain.toLowerCase() + aUrl.substring(end,aUrl.length);
//BEGIN: WARN USER - Only do this after all references to the Configurator have been changed to 'deere.com'.
//alert(domainSwitchMessage);
//END: WARN USER
			document.location.replace(resultUrl);
			return false;
		}
	}
	return true;
}

function global_launchPageInitialization(aUrl, domainSwitchMessage)
{
	if (global_switchDomain(aUrl, domainSwitchMessage)==false)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function formatReferrerForLogging(strIn)
{
	if (strIn==null || strIn.length<1 || strIn=="UNDEFINED")
		return "";
		
	var charAtIndex =0;
	var strOut="";

	for(index=0; index<strIn.length; index++)
	{
		charAtIndex = strIn.charAt(index);		
		if (charAtIndex == '/')
			strOut = strOut + "-";
		else if (charAtIndex == '\/')
			strOut = strOut + "*";
		else if (charAtIndex == ':')
			strOut = strOut + "~";
		else
			strOut = strOut + charAtIndex;
	}	
	if (strOut.indexOf("?")>-1)
		strOut = strOut.substring(0,strOut.indexOf("?"));
	if (strOut.length>200)	
		strOut = strOut.substring(0,200);
	return strOut;
}
