﻿var g_LocalizationWindowMgr;
var g_ClientCountryMgr;

// Handles overlay creation, toogle views
function LocalizationWindowMgr(sCountryCode, sCurrencyCode, fIsDNSDomestic)
{
	
	this.CountryCode = sCountryCode;
	this.CurrencyCode = sCurrencyCode;
	this.IsDNSDomestic = (fIsDNSDomestic.toLowerCase() == "true") ? true : false;

	this.ToggleCountry = function(srcDOMElement, nCatId) {
	    
	    var elName = "SelectACountryCntr";
	    var element = gE(elName);
	    var fMakeVisible = true;
	    var fFrmIsValid = true;

	    if (element) {
	        fMakeVisible = !(element.style.visibility == "visible");
	        if (!fMakeVisible) {
	            if (g_LocalizationWindowMgr.CountryCode == "US" &&
					g_LocalizationWindowMgr.CurrencyCode == "USD" &&
					g_LocalizationWindowMgr.IsDNSDomestic
					) {
	                // do nothing
	            }
	            else {
	                var countryClientMgr = new CountryClientMgr();
	                fFrmIsValid = countryClientMgr.Validate();
	            }
	        }
	        if (fFrmIsValid) {
	            showOverlay(srcDOMElement, elName, fMakeVisible, false, true);
	            this.makeModal(fMakeVisible);
	        }
	    }
	    else {

	        var countryFormHelper = new CountryFormHelper(this.CountryCode, this.CurrencyCode);
	        var sURI = countryFormHelper.GetURI();
	        var arQSArgs = countryFormHelper.GetQSArgs();
	        var countryCode = countryFormHelper.GetCountryCode();
	        var currencyCode = countryFormHelper.GetCurrencyCode();
	        var iFrameSrc = countryFormHelper.GetHost();
	        iFrameSrc += "/templates/localization/choose_country.asp";
	        if (nCatId) {
	            iFrameSrc += "?catid=" + nCatId;
	        }


	        var html = "";
	        html += "<div id=\"SelectACountryFrameCntr\">";
	        html += "<iframe title=\"select country\" name=\"SelectACountryFrame\" id=\"SelectACountryFrame\" onload = \"return initCountryChooser();\" src=\"" + iFrameSrc + "\" hspace=\"0\" vspace=\"0\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\" scrolling=\"no\"></iframe>";
	        html += "<form id=\"frmCountry\" name=\"frmCountry\" method=\"GET\" action=\"" + sURI + "\">";
	        html += "<input type=\"hidden\" name=\"countrycode\" id=\"countrycode\" value=\"" + countryCode + "\"/>";
	        html += "<input type=\"hidden\" name=\"currencycode\" id=\"currencycode\" value=\"" + currencyCode + "\"/>";


	        var arQSItem;
	        var sId;
	        var sValue;

	        for (var i = 0; i <= arQSArgs.length - 1; i++) {
	            arQSItem = arQSArgs[i].split("=");
	            sId = arQSItem[0];
	            if (countryFormHelper.IsValidArgument(sId)) {
	                sValue = arQSItem[1];
	                html += "<input type=\"hidden\" name=\"" + sId + "\" id=\"" + sId + "\" value=\"" + sValue + "\"/>"
	            }
	        }

	        html += "</form>";
	        html += "</div>";

	        var elOverlay = new Overlay(elName, "", 600);
	        elOverlay.footerCloseBtn = false;
	        elOverlay.bodyContent = html;
	        elOverlay.render();
            
	        showOverlay(srcDOMElement, elName, fMakeVisible, false, true);

	        // overlay has been created and displayed, now wire events and make it modal
	        this.makeModal(fMakeVisible);
	        this.wireEvents();
	    }

		this.handleIeIframeHack(fMakeVisible);

	    if (fMakeVisible) {
	        this.positionOverlay(elName);
	    }

	}
	this.wireEvents = function()
	{
		try
		{
			var elIframe = document.getElementById("SelectACountryFrame");
			
			if (elIframe.contentWindow.addEventListener)
			{
				elIframe.contentWindow.addEventListener("load",initCountryChooser,false);
			}
			else if(elIframe.contentWindow.attachEvent)
			{
				elIframe.contentWindow.attachEvent("onload", initCountryChooser);
			}
		}
		catch(e)
		{}
	}
	this.makeModal = function(fMakeVisible)
	{
		try
		{
			var winMgr = GetWinMgr();
			if(winMgr)
			{
				if(fMakeVisible)
				{
					winMgr.SetModal(true);
				}
				else
				{
					winMgr.SetModal(false);
				}
			}
		}
		catch(e){}
	}
	this.positionOverlay = function(elName)
	{
	    var element = document.getElementById(elName);
	    if(!element)
	    {
	        return;
	    }
	    
	    // Window Width
		var clientWidth = f_clientWidth();
		var clientWidthOff = parseInt(clientWidth/2)
		
		// Window Height
		var clientHeight = f_clientHeight();
		var clientHeightOff = (clientHeight/2);
		
		// Window Scroll Left/Top
		var scrollLeft = f_scrollLeft();
		var scrollTop = f_scrollTop();
		
		// Element width/offset (element we're moving)
		var elementWidth = element.clientWidth;
		var elementWidthOff = parseInt(elementWidth/2);
		
		// Element height/offset (element we're moving)
		var elementHeight = element.clientHeight;
		var elementHeightOff = (elementHeight/2);
		
		element.style.left = ((clientWidthOff - elementWidthOff) + scrollLeft);
		element.style.top = ((clientHeightOff + scrollTop) - elementHeightOff);
	}
	this.handleIeIframeHack = function(fMakeVisible)
	{
		var iframe = document.getElementById("iFrmIE6Hack");
		if(iframe)
		{
			if (fMakeVisible)
			{
				iframe.style.visibility = "hidden";
			}
			else
			{
				iframe.style.visibility = "visible";
			}
		}
	}
}

// Form helper
function CountryFormHelper(sCountryCode, sCurrencyCode)
{
	this.GetCountryCode = function()
	{
		if(sCountryCode)
		{
			return sCountryCode;
		}
		else
		{
			return "";
		}
	}
	
	this.GetCurrencyCode = function()
	{
		if(sCurrencyCode)
		{
			return sCurrencyCode;
		}
		else
		{
			return "";
		}
	}
	
	this.GetHost = function()
	{
		var sHost = document.location.protocol + "//" + document.location.host;
		//if(document.location.port)
		//{
		//	sHost += ":" + document.location.port;
		//}
		return sHost;
	}
	this.GetURI = function()
	{
		var sURI = this.GetHost();
		sURI += document.location.pathname;
		return sURI;
	}
	this.GetQSArgs = function()
	{
		var sSearch = document.location.search;
		var arArgs = "";
		
		if(sSearch)
		{
			if(sSearch.charAt(0) == "?")
			{
				sSearch = sSearch.substr(1);
			}
			arArgs = sSearch.split("&")
		}
		
		return arArgs;
	}
	this.IsValidArgument = function(argName)
	{
		switch(argName)
		{
			case "countrycode":
				return false;
			case "currencycode":
				return false;
			case "clearcountry":
				return false;
			default:
				return true;
		}
	}
}

// global event handlers

function initCountryChooser()
{
	g_ClientCountryMgr = new CountryClientMgr();
	g_ClientCountryMgr.WireEvents();
}

function onCountryChange()
{
	if(g_ClientCountryMgr)
	{
		g_ClientCountryMgr.HandleCountryOnChange();
	}
}

function handleCountryChange(srcDOMElement, nCatId)
{
	if(g_LocalizationWindowMgr)
	{
		g_LocalizationWindowMgr.ToggleCountry(srcDOMElement, nCatId);
	}
}

function handleDomesticOnclick()
{
	var elForm = document.getElementById("frmCountry");
	if(elForm)
	{
		
		if(g_LocalizationWindowMgr)
		{
			if( 
				g_LocalizationWindowMgr.CountryCode == "US" && 
				g_LocalizationWindowMgr.CurrencyCode == "USD" && 
				g_LocalizationWindowMgr.IsDNSDomestic
				)
			{
				// only close the overlay, the current country choice is fine.
				handleCountryChange();
				return;
			}
		}
		
		g_ClientCountryMgr.SyncFormToCountry(elForm, "US", "USD");
		elForm.submit();
	}
}

function handleIntlOnclick()
{
	var isValid = g_ClientCountryMgr.Validate();
	if(isValid)
	{
		var elForm = document.getElementById("frmCountry");
		if(elForm)
		{
			
			if(g_LocalizationWindowMgr)
			{
				
				var selectedCountryCode = g_ClientCountryMgr.GetCountryCode();
				var selectedCurrencyCode = g_ClientCountryMgr.GetCurrencyCode();
				
				if(
					g_LocalizationWindowMgr.CountryCode == selectedCountryCode &&
					g_LocalizationWindowMgr.CurrencyCode == selectedCurrencyCode &&
					g_LocalizationWindowMgr.IsDNSDomestic == false
					)
				{
					// only close the overlay, the current country choice is fine.
					handleCountryChange();
					return;
				}
			}
			
			
			var isValidSync = g_ClientCountryMgr.SyncToForm(elForm);
			elForm.submit();
		}
	}
}

// Main biz object for client interactions

function CountryClientMgr()
{
	
	try
	{
		
		var elIFrame;
		var elDocument;
		
		// Set this document instance to that of the iframe document
		elIFrame = document.getElementById("SelectACountryFrame");
		elDocument = this.GetIFrameDocument(elIFrame);
		
		this.documentElement = elDocument;
		
	}
	catch(e)
	{
		return;
	}
	
	
	this.btnContinueIntlId = 'btnContinueIntl';
	this.btnContinueDomesticId = 'btnContinueDomestic';
	this.selCountryId = 'selCountry';
	this.selCurrencyId = 'selCurrency';
	this.persistCurrency = true;
	
	var _instance = this;
	
	var _elCountry = null;
	
	var _elCurrency = null;
	
	var _elIntlButton = null;
	var _elDomesticButton = null;
	
	
	
	this.GetCountryElement = function()
	{
		if(!_elCountry)
		{
			_elCountry = _instance.documentElement.getElementById(this.selCountryId);
		}
		return _elCountry;
	}
	this.GetCountryCode = function()
	{
		var elCountry = this.GetCountryElement();
		var countryCode = this.GetSelectValue(elCountry);
		return countryCode;
	}
	
	this.GetCurrencyElement = function()
	{
		if(!_elCurrency)
		{
			_elCurrency = _instance.documentElement.getElementById(this.selCurrencyId);
		}
		return _elCurrency;
	}
	this.GetCurrencyCode = function()
	{
		var elCurrency = this.GetCurrencyElement();
		var currencyCode = this.GetSelectValue(elCurrency);
		return currencyCode;
	}
	
	this.GetIntlButton = function()
	{
		if(!_elIntlButton)
		{
			_elIntlButton = _instance.documentElement.getElementById(this.btnContinueIntlId);
		}
		return _elIntlButton;
	}
	this.GetDomesticButton = function()
	{
		if(!_elDomesticButton)
		{
			_elDomesticButton = _instance.documentElement.getElementById(this.btnContinueDomesticId);
		}
		return _elDomesticButton;
	}
	
	this.GetSelectValue = function(elSel)
	{
		return elSel.options[elSel.selectedIndex].value;
	}
	
	// Sync the current selected values to form field elements.
	this.SyncToForm = function(frm)
	{
		try
		{
			var countryCode = this.GetCountryCode();
			var currencyCode = this.GetCurrencyCode();
			return this.SyncFormToCountry(frm, countryCode, currencyCode);
		}
		catch(e)
		{
			return false;
		}
	}
	// Sync passed in values to frm field elements.
	this.SyncFormToCountry = function(frm, countryCode, currencyCode)
	{
		try
		{
			frm.countrycode.value = countryCode;
			frm.currencycode.value = currencyCode;
		}
		catch(e)
		{
			return false;
		}
		return true;
	}
}
CountryClientMgr.prototype.IsValidCountry = function()
{
	try
	{
		var elCountry = this.GetCountryElement();
		return (elCountry.selectedIndex > 0);
	}
	catch(e)
	{
		return false;
	}
}
CountryClientMgr.prototype.IsValidCurrency = function()
{
	try
	{
		var elCurrency = this.GetCurrencyElement();
		return (elCurrency.selectedIndex > 0);
	}
	catch(e)
	{
		return false;
	}
}
CountryClientMgr.prototype.Validate = function()
{
	try
	{
		var isValidCountry = this.IsValidCountry();
		var isValidCurrency = this.IsValidCurrency();
		
		if(!isValidCountry)
		{
			alert('please select a country');
			return false;
		}
		else if(!isValidCurrency)
		{
			alert('please select a currency');
			return false;
		}
	}
	catch(e)
	{
		return false;
	}
	return true;
	
}
CountryClientMgr.prototype.WireEvents = function(elFrame)
{
	try
	{
		var elIntlButton = this.GetIntlButton();
		if(elIntlButton)
		{
			elIntlButton.onclick = handleIntlOnclick;
		}
		
		var elDomesticButton = this.GetDomesticButton();
		if(elDomesticButton)
		{
			elDomesticButton.onclick = handleDomesticOnclick;
		}
		
		var elCountry = this.GetCountryElement();
		elCountry.onchange = onCountryChange;
	
	}
	catch(e){}
}
CountryClientMgr.prototype.HandleCountryOnChange = function()
{
	try
	{
		var elCountry = this.GetCountryElement();
		var defaultCurrencyCode = elCountry.options[elCountry.selectedIndex].getAttribute('cur');
		var elCurrency = this.GetCurrencyElement();
		if( (elCurrency.selectedIndex == 0) || (this.persistCurrency == false) )
		{
			this.persistCurrency = false;
			this.SetSelectedOption(elCurrency, defaultCurrencyCode);
		}
	}
	catch(e){}
}
CountryClientMgr.prototype.SetSelectedOption = function(elSelect, sValue)
{
	try
	{
		for(var i=0; i<= elSelect.options.length-1; i++)
		{
			var elOption = elSelect.options[i];
			if(elOption.value == sValue)
			{
				elOption.selected = true;
				break;
			}
		}
	}
	catch(e){}
}
CountryClientMgr.prototype.GetIFrameDocument = function(elIFrame)
{
	var elDocument;
	if (elIFrame.contentWindow)
	{
		elDocument = elIFrame.contentWindow.document;
	}
	else if (elIframe.contentDocument)
	{
		elDocument = elIFrame.contentDocument;
	}
	else if (elIframe.document)
	{
		elDocument = elIFrame.document;
	}
	return elDocument;
}
