


//if (typeof(CM_WEBCOMPONENT_REGISTER) == 'undefined') (function() { // #ifndef
	
CM_WEBCOMPONENT_REGISTER = function (strName) {

	this.strName = strName;
	this.objLogin = null
	this.objBackground = null;
	this.objErrorContainer = null;
	this.objErrorText = null;
	this.strName = strName;
	this.arrField = new Array();
	this.blnSending = false;
	this.objSendDataHttpRequest = new DHL.NET.HTTPREQUEST();
	this.arrOnShowCallback = new Array();
	this.arrOnHideCallback = new Array();
	this.arrOnConfirmRegistrationCallback = new Array();
	this.blnDisplayBackground = true;
	this.strSelectedForm = CM_WEBCOMPONENT_REGISTER.FORM_LOGIN;
	this.strLastPath = "";
	this.blnLockDisplay = false;
	this.strEmail = "";
	this.objCurrentUrl = null;
	
}

/*
var PUBLIC = CM_WEBCOMPONENT_REGISTER.prototype;
var PRIVATE = CM_WEBCOMPONENT_REGISTER.prototype;
var STATIC = CM_WEBCOMPONENT_REGISTER; 
var SELF = CM_WEBCOMPONENT_REGISTER;
*/ 

// Constant Definition {{{

CM_WEBCOMPONENT_REGISTER.FORM_LOGIN = 'divLogin';
CM_WEBCOMPONENT_REGISTER.FORM_REGISTER = 'divRegister';
CM_WEBCOMPONENT_REGISTER.FORM_CONFIRM = 'divConfirm';
CM_WEBCOMPONENT_REGISTER.FORM_FORGOT = 'divForgot';
CM_WEBCOMPONENT_REGISTER.FORM_FORGOTRESPONSE = 'divForgotAnswer';
CM_WEBCOMPONENT_REGISTER.REGISTER_MESSAGE = 'divRegisterMessage';

// }}}
 
 
CM_WEBCOMPONENT_REGISTER.prototype.lockDisplay = function(blnLockDisplay) {
	this.blnLockDisplay = blnLockDisplay;
}

CM_WEBCOMPONENT_REGISTER.prototype.setEmail = function(strEmail) {
	this.strEmail = strEmail;
}

CM_WEBCOMPONENT_REGISTER.prototype.setCurrentLink = function setCurrentLink(strLink) {
	this.objCurrentUrl = new CM_URL(strLink);
}

CM_WEBCOMPONENT_REGISTER.prototype.addOnShowEvent = function(ptCallback) {
	if(ptCallback) {
		this.arrOnShowCallback.push(ptCallback);
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.addOnHideEvent = function(ptCallback) {
	if(ptCallback) {
		this.arrOnHideCallback.push(ptCallback);
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.selectForm = function(strFormName) {
	switch(strFormName) {
		case CM_WEBCOMPONENT_REGISTER.FORM_LOGIN :
			this.strSelectedForm = CM_WEBCOMPONENT_REGISTER.FORM_LOGIN;
		break;
		case CM_WEBCOMPONENT_REGISTER.FORM_REGISTER :
			this.strSelectedForm = CM_WEBCOMPONENT_REGISTER.FORM_REGISTER;
		break;
		case CM_WEBCOMPONENT_REGISTER.FORM_CONFIRM :
			this.strSelectedForm = CM_WEBCOMPONENT_REGISTER.FORM_CONFIRM;
		break;
		case CM_WEBCOMPONENT_REGISTER.FORM_FORGOT :
			this.strSelectedForm = CM_WEBCOMPONENT_REGISTER.FORM_FORGOT;
		break;
		case CM_WEBCOMPONENT_REGISTER.FORM_FORGOTRESPONSE :
			this.strSelectedForm = CM_WEBCOMPONENT_REGISTER.FORM_FORGOTRESPONSE;
		break;
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.displayForm = function(strForm) {
	var selectedForm = (strForm? strForm: this.strSelectedForm);

	if(selectedForm) {
		this.resetDisplay();
		document.getElementById(CM_WEBCOMPONENT_REGISTER.FORM_LOGIN + this.strName).style.display = "none";
		document.getElementById(CM_WEBCOMPONENT_REGISTER.FORM_REGISTER + this.strName).style.display = "none";
		document.getElementById(CM_WEBCOMPONENT_REGISTER.FORM_CONFIRM + this.strName).style.display = "none";
		document.getElementById(CM_WEBCOMPONENT_REGISTER.FORM_FORGOT + this.strName).style.display = "none";
		document.getElementById(CM_WEBCOMPONENT_REGISTER.FORM_FORGOTRESPONSE + this.strName).style.display = "none";
		document.getElementById(CM_WEBCOMPONENT_REGISTER.REGISTER_MESSAGE + this.strName).style.display = "none";
		document.getElementById(selectedForm + this.strName).style.display = "block";
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.isDisplayBackground = function(blnDisplay) {
	this.blnDisplayBackground = blnDisplay;
}

CM_WEBCOMPONENT_REGISTER.prototype.addOnConfirmRegistrationEvent = function(ptCallback) {
	if(ptCallback) {
		this.arrOnConfirmRegistrationCallback.push(ptCallback);
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.onShowEvent = function() {
	for(var intI = 0; intI < this.arrOnShowCallback.length; intI++) {
		if(this.arrOnShowCallback[intI]) {
			this.arrOnShowCallback[intI](this);
		}
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.onHideEvent = function() {
	for(var intI = 0; intI < this.arrOnHideCallback.length; intI++) {
		if(this.arrOnHideCallback[intI]) {
			this.arrOnHideCallback[intI](this);
		}
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.onConfirmRegistrationEvent = function() {
	for(var intI = 0; intI < this.arrOnConfirmRegistrationCallback.length; intI++) {
		if(this.arrOnConfirmRegistrationCallback[intI]) {
			this.arrOnConfirmRegistrationCallback[intI](this);
		}
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.sendRegisterRequest = function(objForm) {
	if(!this.blnSending) {
		this.blnSending = true;
		var objUrlToSend = this.objCurrentUrl;

		strAttributes = "wcName=" + this.strName + "&" +
			"login" + '=' + objForm["login"].value + "&" +
			"password" + '=' + objForm["password"].value + "&" +
			"email" + '=' + objForm["email"].value; 

		this.objSendDataHttpRequest.open('POST', objUrlToSend, true);
		this.objSendDataHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.objSendDataHttpRequest.setRequestHeader('Content-length', strAttributes.length);
		this.objSendDataHttpRequest.setRequestHeader('Connection', 'close');

		var me = this;
		this.objSendDataHttpRequest.onreadystatechange(function() { me.onReceiveData(); });
		this.objSendDataHttpRequest.send(strAttributes);
		//display register message
		document.getElementById("divRegisterMessage" + this.strName).style.display = "block";
		document.getElementById("divRegister" + this.strName).style.display = "none";
		
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.resendEmail = function() {
	if(!this.blnSending) {
		this.blnSending = true;
		var objUrlToSend = this.objCurrentUrl;
		var strAttributes = 'wcName=' + this.strName + '&resend=1';

		this.objSendDataHttpRequest.open('POST', objUrlToSend, true);

		this.objSendDataHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.objSendDataHttpRequest.setRequestHeader('Content-length', strAttributes.length);
		this.objSendDataHttpRequest.setRequestHeader('Connection', 'close');

		//this.objSendDataHttpRequest.onreadystatechange(this.hide(me));
		this.objSendDataHttpRequest.send(strAttributes);
		
		//remove resend link
		var arrResend = dom_getElementsByTagName(document.getElementById(CM_WEBCOMPONENT_REGISTER.FORM_CONFIRM + this.strName), '*');
		arrResend["reconfirmEmail"].innerHTML = "<br /><b>Confirmation email resent to " + this.strEmail + "</b>";
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.onReceiveData = function() {
	if(this.objSendDataHttpRequest.getReadyState() == 4) {
		//response has been received
		this.blnSending = false;
		if(this.objSendDataHttpRequest.getStatus() == 200) {
			if(this.objSendDataHttpRequest.getResponseText() == 'ok') {
				//proceed to next page
				objUrl = DHL.NET.URL.getCurrent();
				objUrl.setPath("/confirm_signup.htm");
				document.location.replace(objUrl.toString());
			}
			else {
				//display Error
				this.objErrorContainer.style.display = "block";
				this.objErrorText.innerHTML = this.objSendDataHttpRequest.getResponseText();
				if(document.getElementById('flash-displaybox')) {
					parent.updateText(2, this.objSendDataHttpRequest.getResponseText());
				}

				//display register message
				document.getElementById("divRegisterMessage" + this.strName).style.display = "none";
				document.getElementById("divRegister" + this.strName).style.display = "block";
			}
		}
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.validate = function(objForm) {
	var blnError = false;
	var arrField = new Object(); 
	
	objVerification = DHL.VALIDATION.VERIFICATION.getInstance();
	arrField['login'] = objVerification.auth(
		objForm['login'].value
	);
	arrField['password'] = objVerification.auth(
		objForm['password'].value
	);
	
	if (objForm['login'].value && objForm['password'].value) {
		arrField['auth'] = objVerification.authMatch(objForm['login'].value,objForm['password'].value);
	}

	arrField['email'] = objVerification.email(
		objForm['email'].value
	);

	// Create Error Msg {{{ 
	var strError = '';
	for(strKey in arrField){
		if(arrField[strKey] && typeof arrField[strKey] == 'object' ) {
			strError += objVerification.formatStringError(arrField[strKey], strKey); 
			blnError = true;
		}
	}

	if(objForm["terms"].checked == false) {
		strError += "You need to accept the Terms and Conditions.";
		blnError = true;
	}

	if(blnError){
		/*if(document.getElementById('flash-displaybox')) {
			parent.updateText(2, strError);
		} else {*/
			alert(strError);
		//}
		return false;
	} else {
		this.sendRegisterRequest(objForm);
		return true;
	}
	//}}}
}

CM_WEBCOMPONENT_REGISTER.prototype.setLoginTemplate = function(objTemplate) {
	this.objLogin = objTemplate;
}

CM_WEBCOMPONENT_REGISTER.prototype.setBackgroundTemplate = function(objBackgroundTemplate) {
	this.objBackground = objBackgroundTemplate;
}

CM_WEBCOMPONENT_REGISTER.prototype.setErrorTemplate = function(objTemplate) {
	this.objErrorContainer = objTemplate;
}

CM_WEBCOMPONENT_REGISTER.prototype.setErrorTextTemplate = function(objTemplate) {
	this.objErrorText = objTemplate;
}

CM_WEBCOMPONENT_REGISTER.prototype.displayFixed = function(strFormName) {
	var arrFullPageSize = getPageSizeWithScroll();
	if(this.objLogin) {
		this.displayForm(strFormName);
		//center to current window position
		this.objLogin.style.position = "relative";
		this.objLogin.style.margin = "0px";
		this.objLogin.style.display = "block";
		this.objLogin.style.top = "0px";
		this.objLogin.style.left = "0px";
		document.getElementById("divClose" + this.strName).style.display="none";
		
	}
	
	if(this.objBackground && this.blnDisplayBackground) {
		//stretch to full window size
		this.objBackground.style.height = (arrFullPageSize['height']) + "px";
		this.objBackground.style.display = "block";
		var me = this;
		this.objBackground.onclick = function() {
			me.hide();
		}
	}
	this.onShowEvent();
}

CM_WEBCOMPONENT_REGISTER.prototype.onDisplayLogin = function(strFormName, strUrl, strAccess) {
	var arrViewPort = getViewportSize();
	var arrFullPageSize = getPageSizeWithScroll();
	var arrScroll = getScrollXY();
	if(strUrl) {
		document.getElementById("lastKnownPath" + this.strName).value = strUrl;
	}
	if(strAccess) {
		document.getElementById("redirectAccess" + this.strName).value = strAccess;
	}

	if(this.objLogin) {
		this.displayForm(strFormName);
		this.objLogin.style.display = "block";
	}

	if(this.blnLockDisplay) {
		document.getElementById("divClose" + this.strName).style.display = "none";
	}

	if(this.objBackground && this.blnDisplayBackground) {
		//stretch to full window size
		this.objBackground.style.height = (arrViewPort['height']) + "px";
		this.objBackground.style.display = "block";
		if(!this.blnLockDisplay) {
			var me = this;
			this.objBackground.onclick = function() {
				me.hide();
			}
		}
	}
	this.onShowEvent();
}

CM_WEBCOMPONENT_REGISTER.prototype.onConfirmRegistration = function() {
	var arrViewPort = getViewportSize();
	var arrFullPageSize = getPageSizeWithScroll();
	var arrScroll = getScrollXY();
	
	if(this.objLogin) {
		//center to current window position
		//this.objLogin.style.top = (arrViewPort['height'] / 2 + arrScroll['offY'] - 100) + "px";
		//this.objLogin.style.left = (arrViewPort['width'] / 2 + arrScroll['offX'] - 250) + "px";
		this.objLogin.style.display = "block";
		this.displayForm(CM_WEBCOMPONENT_REGISTER.FORM_CONFIRM);
	}
	if(this.objBackground && this.blnDisplayBackground) {
		//stretch to full window size
		this.objBackground.style.height = (arrFullPageSize['height']) + "px";
		this.objBackground.style.display = "block";
		var me = this;
		this.objBackground.onclick = function() {
			me.hide();
		}
	}
	this.onShowEvent();
}

CM_WEBCOMPONENT_REGISTER.prototype.hide = function(objThis) {
	this.blnSending = false;
	if (objThis) {
		if(objThis.objLogin) {
			objThis.objLogin.style.display = "none";
		}
		if(objThis.objBackground) {
			objThis.objBackground.style.display = "none";
		}
		objThis.onHideEvent();
	} else {
		if(this.objLogin) {
			this.objLogin.style.display = "none";
		}
		if(this.objBackground) {
			this.objBackground.style.display = "none";
		}
		this.onHideEvent();
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.forgotPassword = function (strEmail) {
	objForm = document.getElementById('forgot' + this.strName);
	objForm["email"].value = strEmail;
	this.onForgotPassword(objForm);
}

CM_WEBCOMPONENT_REGISTER.prototype.onForgotPassword = function(objForm) {
	if(!this.blnSending) {
		this.blnSending = true;

		var blnError = false;
		var objUrlAuth = new DHL.NET.URL.getCurrent();
		var objErrorContainer = document.getElementById(this.strName + "ErrorContainer");
		objErrorContainer.style.display="none";
		objForm["btnSend"].disabled = true;

		//validate email address
		objForm["email"].value = objForm["email"].value.replace(/\r\n|\n|\t/gi, ",");
		var arrEmail = DHL.NET.EMAIL.parseMultiEmail(objForm["email"].value);
		if(
			arrEmail &&
			arrEmail.length
		) {
			strAttributes = "frmName=" + this.strName + "&" +
				"action=forgotpassword" + "&" +
				"email=" + objForm["email"].value;

			this.objSendDataHttpRequest.open('POST', objUrlAuth, true);
			
			this.objSendDataHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			this.objSendDataHttpRequest.setRequestHeader('Content-length', strAttributes.length);
			this.objSendDataHttpRequest.setRequestHeader('Connection', 'close');
			
			var me = this;
			this.objSendDataHttpRequest.onreadystatechange(function() {
				me.onForgotPasswordResponse();
			});
			this.objSendDataHttpRequest.send(strAttributes);
		} else {
			this.blnSending = false;
			objForm["btnSend"].disabled = false;
			objErrorContainer.innerHTML = "&nbsp;Please enter a valid email address";
			objErrorContainer.style.color="red";
			objErrorContainer.style.display="block";
			if(document.getElementById('flash-displaybox')) {
				parent.updateText(4, "&nbsp;Please enter a valid email address");
			}
		}
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.login = function (strUserName,strPassword) {
	if(this.objLogin) {
		document.getElementById('auth' + this.strName)['login'].value = strUserName;
		document.getElementById('auth' + this.strName)['password'].value = strPassword;
		document.getElementById('auth' + this.strName).submit();
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.createNewAccount = function (strUserName, strPassword, strEmail) {
	document.getElementById('registration' + this.strName)['login'].value = strUserName;
	document.getElementById('registration' + this.strName)['password'].value = strPassword;
	document.getElementById('registration' + this.strName)['email'].value = strEmail;
	document.getElementById('registration' + this.strName)["terms"].checked = true;
	this.validate(document.getElementById('registration' + this.strName));
}

CM_WEBCOMPONENT_REGISTER.prototype.onForgotPasswordResponse = function() {
	if(this.objSendDataHttpRequest.getReadyState() == 4 && this.objSendDataHttpRequest.getStatus() == 200) {
		if(this.objSendDataHttpRequest.getResponseText()){
			this.blnSending = false;
			document.getElementById("divForgot" + this.strName).style.display = "none";
			document.getElementById("divForgotAnswer" + this.strName).style.display = "block";
			if(document.getElementById('flash-displaybox')) {
				parent.updateText(4);
			}
		}
	}
}

CM_WEBCOMPONENT_REGISTER.prototype.resetDisplay = function resetDisplay(){
	if(document.getElementById('hotmail')) {
		document.getElementById('hotmail').style.display = 'none';
	}
	if(document.getElementById('yahoo')) {
		document.getElementById('yahoo').style.display = 'none';
	}
	if(document.getElementById('gmail')) {
		document.getElementById('gmail').style.display = 'none';
	}
	if(document.getElementById('aol')) {
		document.getElementById('aol').style.display = 'none';
	}
	if(document.getElementById('solution')) {
		document.getElementById('solution').style.display = 'none';
	}
	if(document.getElementById('confirmContainer')) {
		document.getElementById('confirmContainer').style.display = 'block';
	}
}

//})();
DHL.JS.require('dhl.validation.verification', DHL.JS.typeXML);