var oFocusElement = null;

function GA_ReloadLocation()
{
	var sUrl = window.location.toString();
	var iPos1 = sUrl.indexOf(GA_GetQueryString("code"));
	
	if (iPos1 != -1)
	{
		iPos1 = sUrl.indexOf("&code=" + GA_GetQueryString("code"));
		
		if (iPos1 != -1)
		{		
			sUrl = sUrl.replace("&code=" + GA_GetQueryString("code"), "");
		}
		else
		{
			sUrl = sUrl.replace("?code=" + GA_GetQueryString("code"), "");
		}
	}

    while (sUrl.indexOf("#") != -1)
    {
        sUrl = sUrl.replace("#","");
    }
	
	window.location = sUrl;
}

var GA_iPartnerCount = 0;

function GA_TrimParameter(sValue)
{
	if (sValue != null)
	{	
	    return sValue.replace(/(<([^>]+)>)/ig,"").replace(/^\s+/,'').replace(/\s+$/,'').replace('\xa0', '');
	}
	
	return "";
}

function GA_ShowAddRegInfo()
{
	var oAddReg = document.getElementById("GA_ExtraRegInfo");
	
	if (oAddReg.style.display == "none")
	{
		oAddReg.style.display = "block";
	}
	else
	{
		oAddReg.style.display = "none";
	}
}

function GA_CheckPartner(oElement)
{
	if (oElement.checked)
	{
		GA_iPartnerCount++;
	}
	else
	{
		if (GA_iPartnerCount > 0)
		{
		    GA_iPartnerCount--;
		}
	}
	
	if (GA_iPartnerCount == 0)
	{
		document.getElementById("GA_eventPartner").style.display = "none";
	}
	else
	{
		document.getElementById("GA_eventPartner").style.display = "block";
	}
}

function GA_ExpandPartner(sId)
{
	var oDiv = document.getElementById("Partner_" + sId);
	var oParBox = null;
	
	if (oDiv)
	{
		if (oDiv.style.display == "block")
		{
			oDiv.style.display = "none";
			
			oParBox = document.getElementById("PartnerBox_" + sId);
			
			if (oParBox)
			{
				oParBox.checked = false;
				GA_CheckPartner(oParBox);
			}
		}
		else
		{
			oDiv.style.display = "block";
		}
	}
}

function GA_CheckNumberRange(iMin, iMax, iValue, oEvent, oElement)
{
	if (!((iValue >= iMin) && (iValue <= iMax)))
	{
		oFocusElement = oElement;
		alert("The given value must be between " + iMin + " and " + iMax + ".");
		setTimeout("oFocusElement.focus();", 1);
	}
}

function GA_TextAreaLimit(oField)
{
	if (oField.value.length > 1024)
	{
		oField.value = oField.value.substring(0, 1024);
	}
}

function GA_UserLoggedIn()
{
	var oULIC = new vdf.ajax.SoapCall("UserLoggedIn", {}, "GA_Proxy.asp");
	oULIC.addParam("sSessionToken", GA_GetCookie("vdfSessionKey"));
	oULIC.addParam("sEmail", GA_GetCookie("gaEmail"));
	oULIC.addParam("sIp", GA_IP);
	oULIC.send(false);
	
	return (oULIC.getResponseValue() == 0);
}

function GA_TrimString (value)
{
    value = value.replace(/^\s+/,'');
    value = value.replace(/\s+$/,'');
    return value;
}

function GA_GetQueryString(sString)
{
	var i = 0;
	var hu = window.location.search.substring(1);
	var gy = hu.split("&");
	
	for (i = 0; i < gy.length; i++)
	{
		ft = gy[i].split("=");
		if (ft[0] == sString)
		{
			return ft[1];
		}
	}
	
	return "";
}

function GA_get_ElementsByName(sTag, sName)
{
	var oElements = document.getElementsByTagName(sTag);
	var aArr = new Array();
	var i = 0;
	var y = 0;
	var sAtt = "";

	if (GA_IsIE())
	{
		for (i = 0, y = 0; i < oElements.length; i++)
		{
			sAtt = oElements[i].getAttribute("name");

			if (sAtt == sName)
			{
				aArr[y] = oElements[i];
				y++;
			}
		}
	}
	else
	{
		aArr = document.getElementsByName(sName);
	}

	return aArr;
}

function GA_IsIE()
{
	return (navigator.appName.toString().indexOf("Internet Explorer") != -1);
}

function GA_LoginRedirect(sBaseUrl)
{
	if (!sBaseUrl)
	{
		sBaseUrl = window.location;
	}
	
	window.location = ("accountentry.asp?base=" + sBaseUrl);
}

function GA_CallFailed(oEvent)
{
	var oCall = oEvent.oSource, sError = null;
	
	if (oCall.aErrors != null && oCall.aErrors.length > 0)
	{
		sError = "" + oCall.aErrors[0].iNumber + " : " + oCall.aErrors[0].sErrorText;
		vdf.gui.alert(sError, 'Error');
		return true;
	}
	
	return false;
}

function GA_CallValue(oEvent, bIsArray)
{
	if (bIsArray)
	{
		return oEvent.oSource.getResponseValue("aArray");
	}
	
	return oEvent.oSource.getResponseValue();
}

function GA_ShowScreen(sDiv, sText)
{
    var oDivs = document.getElementsByTagName('div');
    var iCounter = 0;
	var bFound = false;
	
    for (iCounter = 0; iCounter < oDivs.length; iCounter++)
    {
        if (oDivs[iCounter].attributes["name"] != null && oDivs[iCounter].attributes["name"].value == "screen")
        {
            if (oDivs[iCounter].id == sDiv)
            {
                bFound = true;
				
				oDivs[iCounter].style.display = "";
				
				if (!GA_IsNothing(sText))
				{
					oDivs[iCounter].innerHTML = sText;
				}
            }
            else
            {
                oDivs[iCounter].style.display = "none";
            }
        }
    }
	
	if (!bFound)
	{
		document.getElementById(sDiv).innerHTML = sText;
	}
}

function GA_GenerateNumber()
{
	if (document.getElementById("GA_hiddenInput"))
	{
	    document.getElementById("GA_hiddenInput").value = Math.random();
    }
}

function GA_IsNothing(sValue)
{
	return (typeof(sValue) == "undefined" || sValue == null || sValue == "");
}

function GA_AccountLogout()
{
    var oALC = new vdf.ajax.SoapCall("AccountLogout", {}, "GA_Proxy.asp");
    oALC.addParam("sSessionToken", GA_GetCookie("vdfSessionKey"));
    oALC.onFinished.addListener(GA_handleAccountLogout);
    oALC.send(false);
}

function GA_handleAccountLogout(oEvent) 
{
	if (!GA_CallFailed(oEvent))
	{  
        if (GA_CallValue(oEvent) == 0)
        {
            document.cookie = 'gaEmail = ';
			document.cookie = 'vdfSessionKey = ';
			
			GA_ReloadLocation();
        }
        else
        {
            alert ("An error occured.");
        }
    }
}

function GA_SetCookie(sName, sValue, iExpires, sPath, sDomain, bSecure)
{
	var dToday = new Date();
	dToday.setTime(dToday.getTime());

	if (iExpires)
	{
		iExpires = iExpires * 1000 * 60 * 60 * 24;
	}
	else
	{
	    iExpires = 0;
	}
	
	var expires_date = new Date(dToday.getTime() + (iExpires));

	Delete_Cookie(sName, sPath, sDomain);
	
	document.cookie = sName + "=" +escape(sValue) +
	((iExpires) ? ";expires=" + expires_date.toGMTString() : "") +
	((sPath )? ";path=" + sPath : "") +
	((sDomain) ? ";domain=" + sDomain : "") +
	((bSecure) ? ";secure" : "");
}

function Delete_Cookie(sName, sPath, sDomain)
{
	if (GA_GetCookie(sName))
	{
		document.cookie = sName + "=" +
		((sPath) ? ";path=" + sPath : "") +
		((sDomain) ? ";domain=" + sDomain : "") +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}

function GA_GetCookieValue (sOffset)
{
    var sEndstr = document.cookie.indexOf (";", sOffset);
    var sResult = "";
	
    if (sEndstr == -1)
	{
		sEndstr = document.cookie.length;
	}
    
	sResult = unescape(document.cookie.substring(sOffset, sEndstr));
	
	return sResult;
}

function GA_GetCookie (name)
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    
    while (i < clen)
    {
        var j = i + alen;
        
        if (document.cookie.substring(i, j) == arg)
        {
            return GA_GetCookieValue (j);
        }
        
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    
    return "";
}

// structs
function tAccountInfo (sEmail, sFirstName, sMiddleName, sLastName, sCompanyName, sAddress1, sAddress2, sZipCode, sCity, sState, sCountryCode, sTelephone, sOldPassword, sPassword)
{
	this.sEmail        = sEmail;
	this.sFirstName    = sFirstName;
	this.sMiddleName   = sMiddleName;
	this.sLastName     = sLastName;
	this.sCompanyName  = sCompanyName;
	this.sAddress1     = sAddress1;
	this.sAddress2     = sAddress2;
	this.sZipCode      = sZipCode;
	this.sCity         = sCity;
	this.sState        = sState;
	this.sCountryCode  = sCountryCode;
	this.sTelephone    = sTelephone;
	this.sOldPassword  = sOldPassword;
	this.sPassword     = sPassword;
	//this.sEventNotify  = sEventNotify;
}

function tEventOpt (sId, sValue, sOptions, sPartner)
{
	this.sId      = sId;
	this.sDesc    = null;
	this.sDate    = null;
	this.sType    = null;
	this.sValue   = sValue;
	this.sOptions = sOptions;
	this.sPartner = sPartner;
}

function tEvent (sId, aEventOpt)
{
	this.sId       = sId;
	this.sDesc     = null;
	this.sType     = null;
	this.sDate     = null;
	this.aEventOpt = aEventOpt;
}
