/**
 * Custom alert windows for TrustWorks
 * @version $Id: alertboxes.js 41 2008-06-11 00:32:32Z maros $
 * @author Martin Adamec
 */

var alertBox = function(alertMessage) {
	var box = '<div id="twBoxContainer"><div id="twAlertBox"><p>'+alertMessage+'</p><input name="alert_submit" id="twBoxSubmit" type="submit" value="ok" /></div></div>';
	// hide all sIFR elements for proper modal window display
	// but only on the splash page
	if ($('twRightWrapper') && $$('.sIFR-flash')) {
		$$('.sIFR-flash').each(function(e) {
			e.removeClassName('sIFR-flash').toggleClassName('sIFR-flash-hidden');
		});
	}
	$$('body')[0].insert({top:box});
	document.observe('click', new Wipper().processClick);
}

var infoBox = function(infoMessageArr) {
	//<p class="twInfoFirst">Here is an important message from TrustWorks.</p>

    //<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla enim leo, iaculis vel venenatis id, vehicula ac leo. Etiam sollicitudin malesuada tortor, non imperdiet dolor commodo quis. Suspendisse malesuada tellus quis mauris pulvinar cursus. Nulla gravida, nulla quis vehicula ullamcorper, mi diam volutpat ligula, nec tempus erat diam ac tortor. Morbi turpis eros, consectetur at adipiscing tempus, sodales at nisl. Vestibulum placerat, mauris sit amet adipiscing feugiat, tortor dolor commodo risus, elementum laoreet erat quam pretium mauris. Nam eget nulla sollicitudin risus lobortis varius. Nullam ac convallis sem. Nullam ullamcorper sodales auctor. Nullam pharetra magna ac orci facilisis scelerisque. Donec sed dui ac lacus scelerisque iaculis ac a ligula. Aenean aliquam mauris eu mauris fringilla volutpat nec id orci.</p>
	var infoMessage = '';
    infoMessageArr.each(function(parag) {
    	var classFirst = (infoMessageArr.indexOf(parag) == 0)? ' class="twInfoFirst"' : '';
    	infoMessage += '<p'+classFirst+'>'+parag+'</p>';
    });

	var box = '<div id="twBoxContainer"><div id="twInfoBoxWrapper"><div id="twInfoHead"></div><div id="twInfoBox"><div id="twInfoBoxContent">' +
				infoMessage +
			  '<input name="alert_submit" id="twBoxSubmit" type="submit" value="ok" /></div></div><div id="twInfoBottom"></div></div></div>';
	// hide all sIFR elements for proper modal window display
	// but only on the splash page
	if ($('twRightWrapper') && $$('.sIFR-flash')) {
		$$('.sIFR-flash').each(function(e) {
			e.removeClassName('sIFR-flash').toggleClassName('sIFR-flash-hidden');
		});
	}

	$$('body')[0].insert({top:box});
	document.observe('click',  new Wipper().processClick);
}

function openForgotPwdBox(e) {
	e.element().stopObserving('click');
	//$('twForgotPwdBoxContainer').show();
	var box = '	<div id="twForgotPwdBoxContainer"><div id="twForgotPasswordBox" class="popupBox"><div id="twForgotPasswordInnerBox"><p id="twBitchingBox"></p><label for="twForgotPassword">Enter your email (user ID):</label><input name="twForgotPassword" type="text" id="twForgotPassword" value="" /><input name="twRequestPassword" type="button" id="twRequestPassword" value="Submit"/></div></div></div>';
		// hide all sIFR elements for proper modal window display
	// but only on the splash page
	if ($('twRightWrapper') && $$('.sIFR-flash')) {
		$$('.sIFR-flash').each(function(e) {
			e.removeClassName('sIFR-flash').toggleClassName('sIFR-flash-hidden');
		});
	}

	$$('body')[0].insert({top:box});
	$('twRequestPassword').observe('click', processPwdRequest);
}

function processPwdRequest(e) {
	if(!$('twBitchingBox').empty()) {
		// display any previoously hidden sIFR elements
		if ($$('.sIFR-flash-hidden')) {
			$$('.sIFR-flash-hidden').each(function(e) {
				e.removeClassName('sIFR-flash-hidden').toggleClassName('sIFR-flash');
			});
		}
		if($('twForgotPwdBoxContainer')) {
			$('twForgotPwdBoxContainer').remove();
		}
		$('twForgotPwdTrigger').observe('click', openForgotPwdBox);
	}
	else {
		if(!$('twForgotPassword').value) {
			$('twBitchingBox').update('Please insert something before you try to submit.');
		}
		else {
			// display any previoously hidden sIFR elements
			if ($$('.sIFR-flash-hidden')) {
				$$('.sIFR-flash-hidden').each(function(e) {
					e.removeClassName('sIFR-flash-hidden').toggleClassName('sIFR-flash');
				});
			}
			var emailValue = $('twForgotPassword').value;
			if($('twForgotPwdBoxContainer')) {
				$('twForgotPwdBoxContainer').remove();
			}
			$('twForgotPwdTrigger').observe('click', openForgotPwdBox);
			new Ajax.Request('ajax.php', {
				method: 'get',
				parameters: {script: 'forgotPassword', emailValue: emailValue},
				onSuccess: function(transport) {
					var response = transport.responseText;
					if (response == 'OK') {
						infoBox(['Your password was successfully reset. Please check your email to retrieve it and do not forget to change it after you log in again.']);
					}
					else if(response == 'neverConfirmed') {
						infoBox(['Your account has not yet been authenticated.',
						'A new notification has just been sent to the address you provided. Please verify that a spam filter is not blocking mail from TrustWorks.',
						'The next step is to open the registration email and click the link inside to complete your registration.  Welcome to the community!']);
					}
					else if(response == 'neverConfirmedMailProblem') {
						alertBox('Account was found. We also found that you never finished the registration process by following instruction in your registration email from TrustWorks. Unfortunately, we were not able to send you another registration email this time due to some technical problems on our server. Please contact your system administrator or try again later.');
					}
					else if(response == 'accountNotFound') {
						alertBox('Account was not found. Please contact your system administrator or register your new account with TrustWorks.');
					}
					else {
						alertBox('Technical problems with functionality on the server, request was not processed. Wait a little while and try again, please.');
					}
				},
				onFailure: function(transport) {alertBox('Technical problems whlile connecting with server, selection was not recorded. Wait a little while and try again, please.')}

			});
		}
	}
}

var Wipper = Class.create ({

	initialize: function() {
		this.processClick = this._processClick.bind(this);
	},
	_processClick: function(e) {
		if (e.element().identify() == 'twBoxSubmit') {
			document.stopObserving('click', this.processClick);
			// display any previoously hidden sIFR elements
			if ($$('.sIFR-flash-hidden')) {
				$$('.sIFR-flash-hidden').each(function(e) {
					e.removeClassName('sIFR-flash-hidden').toggleClassName('sIFR-flash');
				});
			}
			if($('twBoxContainer')) {
				$('twBoxContainer').remove();
			}
		}
	}
});