LoginMirra.prototype = new Object();
LoginMirra.prototype.constructor = LoginMirra;
LoginMirra.superclass = null;

function LoginMirra(moduleNamespace) {	
	this.moduleNamespace = moduleNamespace;
	this.windowId = null;
	this.windowObj = null;
};
LoginMirra.prototype.showRegistrationForm = function(windowId, url) {
	var bundle = new LoginMirraBundle();
	var wsgWindow = new WSGWindow(windowId, bundle.REGISTRATION, 700, 200, 100, null);
	wsgWindow.create();
	this.windowObj = wsgWindow;
	this.windowId = windowId;
	document.getElementById(wsgWindow.bodyId).innerHTML = this.buildRegistrationForm(wsgWindow.id, url, this.moduleNamespace);
	document.getElementById(wsgWindow.bodyId).innerHTML += this.getButtons(wsgWindow.id, url, this.moduleNamespace);
    wsgWindow.show(); 	
    document.getElementById('firstName_' + this.moduleNamespace).focus();
};

LoginMirra.prototype.sendRegistrationData = function(windowId, url) {
	var tmpFormValidator = eval('formValidator_' + this.moduleNamespace);
	if (!tmpFormValidator.validateRegistration()) return;
	var bundle = new LoginMirraBundle();		
	var wsgXMLHttpRequestObject = new WSGXMLHttpRequestObject();	
	var xmlHttpRequestObject = wsgXMLHttpRequestObject.getXMLHttpRequestObject();	
	var _this = this;
	var oldContent = document.getElementById(this.windowObj.bodyId).innerHTML;
	if (xmlHttpRequestObject) {
		var requestTail = new StringBuffer();
		var firstName = document.getElementById('firstName_' + this.moduleNamespace).value;		
		var lastName = document.getElementById('lastName_' + this.moduleNamespace).value;
		var phone = document.getElementById('phone_' + this.moduleNamespace).value;
		var email = document.getElementById('email_' + this.moduleNamespace).value;
		var username = document.getElementById('userName_' + this.moduleNamespace).value;				
		var password = document.getElementById('password_' + this.moduleNamespace).value;
		
		requestTail.append('firstName=');
		requestTail.append(firstName);
		requestTail.append('&lastName=');
		requestTail.append(lastName);
		requestTail.append('&phone=');
		requestTail.append(phone);
		requestTail.append('&email=');
		requestTail.append(email);						
		requestTail.append('&username=');
		requestTail.append(username);
		requestTail.append('&hash=');
//		requestTail.append(Base64.encode(password));
		requestTail.append(password);		
		
		var _url = encodeURI(url + '?ns=' + this.moduleNamespace + '&' + requestTail.toString() + '&sId=' + sessionId + '&sync=' + Math.random());
		xmlHttpRequestObject.open("GET", _url);
		xmlHttpRequestObject.onreadystatechange = function() {
			if (xmlHttpRequestObject.readyState == 4 && xmlHttpRequestObject.status == 200) {
				var response = null;					
                if (_ie) xmlHttpRequestObject.responseXML.loadXML(xmlHttpRequestObject.responseText);                
                response = xmlHttpRequestObject.responseXML.documentElement;    
                try {
                	var results = null;                	
    				var errorFound = false;
    				var errorMsg = null;
    				var wsgWindow = null;
                	if (response != null && response.childNodes.length > 0) {
	                	if (response.getElementsByTagName("error") != undefined && response.getElementsByTagName("error")[0] != undefined) {
	                		if (response.getElementsByTagName("errorURL") != undefined && response.getElementsByTagName("errorURL")[0] != undefined) {
	                			document.location.href = response.getElementsByTagName("errorURL")[0].childNodes[0].nodeValue;
	                			return;
	                		}
	                		errorFound = true;
	                		errorMsg = response.getElementsByTagName("error")[0].childNodes[0].nodeValue;	                			                		
	                	} else if (response.childNodes[0].data == 1) {
	                		document.getElementById(_this.windowObj.bodyId).innerHTML = _this.getMessage(WSG_MESSAGE_TYPE_INFO, bundle.REGISTRATION_OK);	                			                	
	                	} else if (response.childNodes[0].data == -1) {
	                		document.getElementById(_this.windowObj.bodyId).innerHTML = _this.getMessage(WSG_MESSAGE_TYPE_ERROR, bundle.SYSTEM_ERROR);	                			                	
	                	} else if (response.childNodes[0].data == -2) {	                		
	                		document.getElementById(_this.windowObj.bodyId).innerHTML = oldContent;
	                		_wsgUtils.hideSystemMessage(_this.moduleNamespace + '_window');
	                		_wsgUtils.showSystemMessage(_this.moduleNamespace + '_window', WSG_MESSAGE_TYPE_ERROR, bundle.USERNAME_ALREADY_USED);
	                		_this.restore(_this.moduleNamespace, firstName, lastName, phone, email, username);
	                		wsgXMLHttpRequestObject.killXML(xmlHttpRequestObject);
	                		return;
	                	} else if (response.childNodes[0].data == -3) {	                		
	                		document.getElementById(_this.windowObj.bodyId).innerHTML = oldContent;
	                		_wsgUtils.hideSystemMessage(_this.moduleNamespace + '_window');
	                		_wsgUtils.showSystemMessage(_this.moduleNamespace + '_window', WSG_MESSAGE_TYPE_ERROR, bundle.EMAIL_ALREADY_USED);
	                		_this.restore(_this.moduleNamespace, firstName, lastName, phone, email, username);
	                		wsgXMLHttpRequestObject.killXML(xmlHttpRequestObject);
	                		return;
	                	} else {
	                		document.getElementById(_this.windowObj.bodyId).innerHTML = _this.getMessage(WSG_MESSAGE_TYPE_ERROR, response.childNodes[0].data);
	                	}	
	                	document.getElementById(_this.windowObj.bodyId).innerHTML += _this.getCloseButton(_this.windowObj.id);
	                } 
	                wsgXMLHttpRequestObject.killXML(xmlHttpRequestObject);	               
                } catch(e) {
                	alert(e);
                }
			} else if (xmlHttpRequestObject.readyState != 0) {
				document.getElementById(_this.windowObj.bodyId).innerHTML = _wsgUtils.getWindowAjaxLoaderWithHeight("Запазване на данните...", "300");
			} else {
				document.getElementById(_this.windowObj.bodyId).innerHTML = oldContent;
        		_wsgUtils.hideSystemMessage(_this.moduleNamespace + '_window');
        		_wsgUtils.showSystemMessage(_this.moduleNamespace + '_window', WSG_MESSAGE_TYPE_ERROR, bundle.USERNAME_OR_EMAIL_ALREADY_USED);
        		_this.restore(_this.moduleNamespace, firstName, lastName, phone, email, username);
				alert("There was a problem retrieving the XML data!");
			}
		};
		xmlHttpRequestObject.send(null);
	}
};

LoginMirra.prototype.restore = function (moduleNamespace, firstName, lastName, phone, email, username) {
	document.getElementById('firstName_' + moduleNamespace).value = firstName; 
	document.getElementById('lastName_' + moduleNamespace).value = lastName; 
	document.getElementById('phone_' + moduleNamespace).value = phone; 
	document.getElementById('email_' + moduleNamespace).value = email; 
	document.getElementById('userName_' + moduleNamespace).value = username; 
};
LoginMirra.prototype.buildRegistrationForm = function (windowId, url, moduleNamespace) {
	var bundle = new LoginMirraBundle();
	var str = new StringBuffer();
	str.append('<table cellpadding="0" cellspacing="0" border="0" height="100%" width="100%">');
	str.append('<tr>');
	str.append('<td width="100%" align="center" height="20">');
	str.append(this.getWindowMessage(this.moduleNamespace));
	str.append('</td>');
	str.append('</tr>');
	str.append('<tr>');
	str.append('<td width="100%" align="center">');
	str.append('<table cellpadding="0" cellspacing="0" border="0">');
	str.append('<tr>');
	str.append('<td align="right" class="textb-padding-left" nowrap="nowrap">' + bundle.FISTNAME + '<span class="required-asterics">*</span>:&nbsp;</td>');
	str.append('<td width="4"></td>');
	str.append('<td align="left" class="text"><input type="text" id="firstName_' + this.moduleNamespace + '" class="inputbox-200" maxlength="50" onkeyup="if (_wsgUtils.checkEnter(event)) {loginMirra_' + moduleNamespace + '.sendRegistrationData(\'' + windowId + '\',\'' + url + '\');}"/></td>');
	str.append('</tr>');
	str.append('<tr valign="middle">');
	str.append('<td height="10" colspan="3"></td>');
	str.append('</tr>');
	str.append('<tr valign="middle">');
	str.append('<td align="right" class="textb-padding-left" nowrap="nowrap">' + bundle.LASTNAME + '<span class="required-asterics">*</span>:&nbsp;</td>');
	str.append('<td width="4"></td>');
	str.append('<td align="left" class="text"><input type="text" id="lastName_' + this.moduleNamespace + '" class="inputbox-200" maxlength="50" onkeyup="if (_wsgUtils.checkEnter(event)) {loginMirra_' + moduleNamespace + '.sendRegistrationData(\'' + windowId + '\',\'' + url + '\');}"/></td>');											
	str.append('</tr>');		
	str.append('<tr valign="middle">');
	str.append('<td height="10" colspan="3"></td>');
	str.append('</tr>');
	str.append('<tr valign="middle">');
	str.append('<td align="right" class="textb-padding-left" nowrap="nowrap">' + bundle.PHONE + '<span class="required-asterics">*</span>:&nbsp;</td>');
	str.append('<td width="4"></td>');
	str.append('<td align="left" class="text"><input type="text" id="phone_' + this.moduleNamespace + '" class="inputbox-200" maxlength="50" onkeyup="if (_wsgUtils.checkEnter(event)) {loginMirra_' + moduleNamespace + '.sendRegistrationData(\'' + windowId + '\',\'' + url + '\');}"/></td>');											
	str.append('</tr>');		
	str.append('<tr valign="middle">');
	str.append('<td height="10" colspan="3"></td>');
	str.append('</tr>');
	str.append('<tr valign="middle">');
	str.append('<td align="right" class="textb-padding-left" nowrap="nowrap">' + bundle.EMAIL + '<span class="required-asterics">*</span>:&nbsp;</td>');
	str.append('<td width="4"></td>');
	str.append('<td align="left" class="text"><input type="text" id="email_' + this.moduleNamespace + '" class="inputbox-200" maxlength="100" onkeyup="if (_wsgUtils.checkEnter(event)) {loginMirra_' + moduleNamespace + '.sendRegistrationData(\'' + windowId + '\',\'' + url + '\');}"/></td>');											
	str.append('</tr>');		
	str.append('<tr valign="middle">');
	str.append('<td height="10" colspan="3"></td>');
	str.append('</tr>');
	str.append('<tr valign="middle">');
	str.append('<td align="right" class="textb-padding-left" nowrap="nowrap">' + bundle.USERNAME + '<span class="required-asterics">*</span>:&nbsp;</td>');
	str.append('<td width="4"></td>');
	str.append('<td align="left" class="text"><input type="text" id="userName_' + this.moduleNamespace + '" class="inputbox-200" maxlength="100" onkeyup="if (_wsgUtils.checkEnter(event)) {loginMirra_' + moduleNamespace + '.sendRegistrationData(\'' + windowId + '\',\'' + url + '\');}"/></td>');											
	str.append('</tr>');		
	str.append('<tr valign="middle">');
	str.append('<td height="10" colspan="3"></td>');
	str.append('</tr>');
	str.append('<tr valign="middle">');
	str.append('<td align="right" class="textb-padding-left" nowrap="nowrap">' + bundle.PASSWORD + '<span class="required-asterics">*</span>:&nbsp;</td>');
	str.append('<td width="4"></td>');
	str.append('<td align="left" class="text"><input type="password" id="password_' + this.moduleNamespace + '" class="inputbox-200" maxlength="100" onkeyup="if (_wsgUtils.checkEnter(event)) {loginMirra_' + moduleNamespace + '.sendRegistrationData(\'' + windowId + '\',\'' + url + '\');}"/></td>');											
	str.append('</tr>');		
	str.append('<tr valign="middle">');
	str.append('<td height="10" colspan="3"></td>');
	str.append('</tr>');
	str.append('<tr valign="middle">');
	str.append('<td align="right" class="textb-padding-left" nowrap="nowrap">' + bundle.RETYPE_PASSWORD + '<span class="required-asterics">*</span>:&nbsp;</td>');
	str.append('<td width="4"></td>');
	str.append('<td align="left" class="text"><input type="password" id="retypePassword_' + this.moduleNamespace + '" class="inputbox-200" maxlength="100" onkeyup="if (_wsgUtils.checkEnter(event)) {loginMirra_' + moduleNamespace + '.sendRegistrationData(\'' + windowId + '\',\'' + url + '\');}"/></td>');											
	str.append('</tr>');
	str.append('<tr valign="middle">');
	str.append('<td height="10" colspan="3"></td>');
	str.append('</tr>');
	str.append('<tr valign="middle">');
	str.append('<td align="left" colspan="3" nowrap="nowrap" class="text">(<span class="required-asterics">*</span>)<b>&nbsp;' + bundle.REQUIRE_FIELD + '</b></td>');
	str.append('</tr>');
	str.append('</table>');
	str.append('</td>');
	str.append('</tr>');
	str.append('</table>');
	return str.toString(); 
};

LoginMirra.prototype.getMessage = function (type, text) {
	var str = new StringBuffer();
	str.append('<table cellpadding="0" cellspacing="0" border="0" height="100%" width="100%">');
	str.append('<tr>');
	str.append('<td width="100%" align="center">');
	str.append('<table cellpadding="0" cellspacing="0" border="0" class="wsg-message-box" width="100%">');
	str.append('<tr height="60">');
	str.append('<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>');
	str.append('<td width="35%" align="right">');
	if (WSG_MESSAGE_TYPE_INFO == type) {
		str.append('<img src="' + _wsgUtils.getInfoIcon() +'" border="0" />');
	} else if (WSG_MESSAGE_TYPE_WARNING == type) { 
		str.append('<img src="' + _wsgUtils.getWarningIcon() +'" border="0" />');
	} else {
		str.append('<img src="' + _wsgUtils.getErrorIcon() +'" border="0" />');
	}
	str.append('</td>');
	str.append('<td>&nbsp;&nbsp;&nbsp;</td>');
	str.append('<td align="left" nowrap="nowrap" class="textb" width="65%">');
	str.append(text);
	str.append('</td>');
	str.append('</tr>');
	str.append('</table>');
	str.append('</td>');
	str.append('</tr>');
	str.append('</table>');
	return str.toString(); 
};

LoginMirra.prototype.getButtons = function (windowId, url, moduleNamespace) {
	var bundle = new LoginMirraBundle();
	var sb = new StringBuffer();	 
	sb.append('<table width="100%" border="0" cellpadding="0" cellspacing="0" height="20">');
	sb.append('<tr><td height="20"></td></tr><tr>');						
	sb.append('<td width="100%" align="center">');	
	sb.append('<table border="0" cellpadding="0" cellspacing="0" height="20">');
	sb.append('<tr valign="middle">');
	sb.append('<td title="' + bundle.REGISTRATION+ '" class="button" onclick="loginMirra_' + moduleNamespace + '.sendRegistrationData(\'' + windowId + '\',\'' + url + '\');" align="center">&nbsp;&nbsp;' + bundle.REGISTRATION + '&nbsp;&nbsp;</td>');																																								
	sb.append('<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>');																																								
	sb.append('<td title="' + bundle.CLOSE+ '" class="button" onclick="new WSGWindow().close(\'' + windowId + '\');" align="center">&nbsp;&nbsp;' + bundle.CLOSE + '&nbsp;&nbsp;</td>');																																								
	sb.append('</tr>');
	sb.append('</table>');	
	sb.append('</td>');				
	sb.append('</tr>');		
	sb.append('<tr><td height="5"></td></tr>');
	sb.append('</table>');
	return sb.toString();
};
LoginMirra.prototype.getCloseButton = function (windowId) {
	var bundle = new LoginMirraBundle();
	var sb = new StringBuffer();	 
	sb.append('<table width="100%" border="0" cellpadding="0" cellspacing="0" height="20">');
	sb.append('<tr><td height="20"></td></tr><tr>');						
	sb.append('<td width="100%" align="center">');	
	sb.append('<table border="0" cellpadding="0" cellspacing="0" height="20">');
	sb.append('<tr valign="middle">');
	sb.append('<td title="' + bundle.CLOSE+ '" class="button" onclick="new WSGWindow().close(\'' + windowId + '\');" align="center">&nbsp;&nbsp;' + bundle.CLOSE + '&nbsp;&nbsp;</td>');																																								
	sb.append('</tr>');
	sb.append('</table>');	
	sb.append('</td>');				
	sb.append('</tr>');		
	sb.append('<tr><td height="5"></td></tr>');
	sb.append('</table>');
	return sb.toString();
};
LoginMirra.prototype.getWindowMessage = function (moduleNamespace) {
	var sb = new StringBuffer();
	sb.append('<div id="' + moduleNamespace + '_window_msg" class="hide-message">');
	sb.append('<table cellpadding="0" cellspacing="0" border="0" align="center">');
	sb.append('<tr>');
	sb.append('<td width="50%"></td>');
	sb.append('<td>');
	sb.append('<table cellpadding="0" cellspacing="0" border="0" align="center" class="wsg-message-box">');
	sb.append('<tr>');
	sb.append('<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>');
	sb.append('<td align="left">');
	sb.append('<table cellpadding="0" cellspacing="0" border="0" align="center">');
	sb.append('<tr>');
	sb.append('<td colspan="3" height="10"></td>');
	sb.append('</tr>');
	sb.append('<tr>');
	sb.append('<td id="' + moduleNamespace + '_window_msgIcon" align="right"></td>');
	sb.append('<td>&nbsp;&nbsp;&nbsp;</td>');
	sb.append('<td id="' + moduleNamespace + '_window_MsgTxt" class="msg-text" align="left"></td>');
	sb.append('</tr>');
	sb.append('<tr>');
	sb.append('<td colspan="3" height="10"></td>');
	sb.append('</tr>');
	sb.append('</table>');
	sb.append('</td>');
	sb.append('<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>');
	sb.append('</tr>');
	sb.append('</table>');
	sb.append('<table>');
	sb.append('<tr>');
	sb.append('<td height="10"></td>');
	sb.append('</tr>');
	sb.append('</table>');
	sb.append('</td>');
	sb.append('<td width="50%"></td>');
	sb.append('</tr>');
	sb.append('</table>');
	sb.append('</div>');
	return sb.toString();
};
