// Global Declarations
var isNetScape = isBrowser("Netscape", 0);
var validationFormNumber = 0;
var validationSuccess;
var usernameArray = new Array();
var passwordArray = new Array();

//address1Array = GetElement('RTAVaddress1', 'text');
usernameArray = xClass$('PERKSUserName');
//address2Array = GetElement('RTAVaddress2', 'text');
passwordArray = xClass$('PERKSPassword');



//***********************************************************************************************************************
//this is the initial call that occurs when the button is pressed
function PerksLoginValidation()
{  
	//DisableSubmitButton();
	SendLoginRequest();
}
//***********************************************************************************************************************

//***********************************************************************************************************************
function SendLoginRequest()
{ 
 //var form = x$("realTime");
usernameArray = xClass$('PERKSUserName');
passwordArray = xClass$('PERKSPassword');
var _username = usernameArray[validationFormNumber].value;
var _password = passwordArray[validationFormNumber].value

 var ajax = new Ajax();
 var oAjxRespHandler = new AjaxResponseHandler1(ajax, LoginValidationCallback);  
 var currentURL = window.location.href;
  _postURL = currentURL.substring(0,currentURL.toLowerCase().indexOf('/about-gevalia/'));//https://www.gevalia.com/
 _postURL = _postURL + '/controls/dtc/common/PerksController.aspx';
 if (window.location.href.indexOf('https:')==0 && _postURL.indexOf('http:')==0)
   _postURL = _postURL.replace('http:','https:');   
 var sFormData="UserName=" + _username + "&Password=" + _password;
  
 ajax.uiWaitMethod = oAjxRespHandler.showWaitStatus;
 ajax.uiClearWaitMethod = oAjxRespHandler.clearWaitStatus;
 ajax.makeRequest('POST', _postURL, oAjxRespHandler.onResponseReceived,sFormData);
 if(typeof PERKSTimeout != 'undefined')
	ajax.TimeoutID = setTimeout(RequestTimedOut, PERKSTimeout);
 else
	ajax.TimeoutID = setTimeout(RequestTimedOut, 15000);
}
//***********************************************************************************************************************

//***********************************************************************************************************************
function AjaxResponseHandler1(_ajaxObj, _callBack, _waitParams)
{	
var me = this;
	me.toString = function() {return "AjaxResponseHandler1"; }
	me.shouldParseResponseXML = true;
	me.shouldDisplayDefaultError = true;
	me.formLayer = document;
	me.readyState = "wait";
	me.ajaxObj = _ajaxObj;
	me.args = [];
	me.callBackFunction = _callBack;
	me.waitParams = _waitParams;
	if (arguments.length > 2){
     for (var argsi=2; argsi < arguments.length; argsi++){
       me.args.push(arguments[argsi]);
     }
	}
	
	me.parseResponseXML = function(_responseXML){   
    var xmlResp = xLoadXML(_responseXML);

		var xmlRespElem;
		if (!xmlResp){
			return true;
			}
		else
		{		
			if(isNetScape == true)
			{	var popupLayer = xmlResp.firstChild.childNodes[1].attributes[0].nodeValue;
				xmlRespElem = xmlResp.firstChild.childNodes[1];
			}
			else 
			{	var popupLayer = xmlResp.firstChild.firstChild.attributes(0).nodeValue;
				xmlRespElem = xmlResp.firstChild.firstChild;
			}
			if(popupLayer == "SUCCESS") // && popupLayer != "NoAltAddrLayer" && popupLayer != "InvalidAddrLayer")
				return true;
			else
			{	if (!xmlRespElem) //layer
					return true;

				DisplayErrorMessage(popupLayer);
			}
		}
	}
	
	me.onResponseReceived = function()
	{	
	try
		{	me.readyState = me.ajaxObj.checkReadyState()
			if (me.readyState == "ok")
			{	if (me.shouldParseResponseXML)
				{	var response = me.ajaxObj.request.responseText;
					//call the method to parse the responses
					validationSuccess = me.parseResponseXML(response);	
				}
				if (me.callBackFunction)  
					me.callBackFunction();		
			}
			else if (me.readyState == "error")
			{	if (me.shouldDisplayDefaultError){
					//display default error message	
				}
				if (me.callBackFunction)  me.callBackFunction();
			}
		}
	catch(err)
		{ 
			//display nothing
		}
	}
	me.showWaitStatus = function()
  {  window.status = "Sent the request. Awaiting response..."  }
  me.clearWaitStatus = function()
  {   window.status = "Done."  }
}
//***********************************************************************************************************************

//***********************************************************************************************************************
function LoginValidationCallback()
{	
	if(validationSuccess)
	{
		openPerksWindow();
	}	
}
//***********************************************************************************************************************

//***********************************************************************************************************************
function RequestTimedOut()
{ 
	DisplayErrorMessage('We could not log you in at this time.  Please try again.');
}
//***********************************************************************************************************************

//***********************************************************************************************************************
function DisplayErrorMessage(msg)
{ 
	var perksErrorDiv = document.getElementById('PerksError');
	
	if (perksErrorDiv != null)
	{
		perksErrorDiv.style.display = "block";
		perksErrorDiv.innerHTML = msg;
	}
}
//***********************************************************************************************************************