var ISCPasswordRollup = Class.create( {
tlog: function() {},
initialize: function(config) {
this.pwdrequestcnt = 0;
this.search_constraints = config.search_constraints;
this.search_constraints.password_rollup = this;
this.in_process = {};
this._password_unlocks_tt = {};
this.password_unlocks_tt_count = {};
this._active_password_check = 0;
this._last_password_used = "";
var has_pw_ticket_types = this.search_constraints.isc.edp.event.get_enabled_ticket_types().any(function(s){ return s.password; });
if (!has_pw_ticket_types) {
$('isc-password-section').hide();
}
},
setup: function() {
this.iscpasswordlb = new ISCPasswordNotifyDialog(this);
this.iscpasswordpolling = new ISCPasswordPollingDialog(this);
this.iscpasswordaccepted = new ISCPasswordAcceptedDialog(this);
Event.observe('isc-password-submit', 'click', function() {
this.search_constraints.isc.seat_map.on_interaction();
this.check({
password: $('isc-password-input').value,
event_id: this.search_constraints.isc.edp.event.event_id
}); }.bind(this)
);
Event.observe('isc-password-form', 'submit', function() {
this.search_constraints.isc.seat_map.on_interaction();
this.check({
password: $('isc-password-input').value,
event_id: this.search_constraints.isc.edp.event.event_id
}); }.bind(this)
);
this.password_ticket_type_info = new ISCPasswordRollupTicketTypeInfo();
this.pwPopup = new Popup( 'isc-password-info-box', {
close_id: 'passPopupClose',
drag_id: [$('isc-password-info-box-content').down(), $('isc-password-info-box-title')],
zIndex: 105,
links: [{
group: 'edp',
link_id: 'QMark1',
toggle: true,
anchor_align:  { x:  1, y: -1 },
popup_align: { x: -1, y: -1 },
offset: { x: 5, y: 0},
onShow: function() { this.password_ticket_type_info.closeAll(); }.bind(this)
}]
});
this.in_process = {};
if (!this.search_constraints.isc.edp.event.get_enabled_ticket_types().select(function(s) {return s.is_locked;}).length) {
return;
}
this.search_constraints.isc.seat_map.prevent_auto_refresh();
var saved_passwords = this.getSavedPasswords();
this.setup_validation = {
valid: [],
invalid: [],
count: saved_passwords.length
};
false;
for( var i = 0; i < saved_passwords.length; i++ ) {
var spw = saved_passwords[i];
this.check({
password: spw,
event_id: this.search_constraints.isc.edp.event.event_id,
suppress_notifications: true,
during_setup: true
});
}
this.search_constraints.isc.seat_map.allow_auto_refresh();
this.reset_fields();
},
savePassword: function( pw ) {
var eid = this.search_constraints.isc.edp.event.event_id;
var cookie = new CookieTree( '_E' );
var pobj = cookie.get('pw') || {};
var savedpw = pobj[eid] || [];
if (savedpw.indexOf(pw) == -1) {
savedpw.push(pw);
pobj[eid] = savedpw;
cookie.set('pw',pobj);
}
},
getSavedPasswords: function() {
var cookie = new CookieTree( '_E' );
var pobj = cookie.get('pw') || {};
return pobj[this.search_constraints.isc.edp.event.event_id] || [];
},
removeSavedPassword: function(pw) {
var eid = this.search_constraints.isc.edp.event.event_id;
var cookie = new CookieTree( '_E' );
var pobj = cookie.get('pw') || {};
var savedpw = pobj[eid] || [];
var pos = savedpw.indexOf(pw);
if (pos != -1) {
savedpw.splice(pos,1);
pobj[eid] = savedpw;
cookie.set('pw',pobj);
}
},
purgeSavedPasswords: function() {
var eid = this.search_constraints.isc.edp.event.event_id;
var cookie = new CookieTree( '_E' );
var pobj = cookie.get('pw') || {};
var savedpw = pobj[eid] || [];
for (var i in this.password_unlocks_tt_count) {
if (this.password_unlocks_tt_count[i] <= 0) {
delete this.password_unlocks_tt_count[i];
this.removeSavedPassword(i);
}
}
},
add_pw_to_tt: function(pw, ett) {
if (!this._password_unlocks_tt[pw]) {
this._password_unlocks_tt[pw] = {};
}
this._password_unlocks_tt[pw][ett] = true;
},
check: function(data) {
false;
if (data.password.blank())
return;
var trackerid = "cb"+this.pwdrequestcnt;
this.pwdrequestcnt++;
var url = ISCPasswordRollup.pwd_url;
var params = {
command          : "event_authenticate",
event_id         : data.event_id,
primary_password : data.password,
tko              : new Date().getTime()
};
this.search_constraints.isc.seat_map.prevent_auto_refresh();
this._active_password_check++;
if (!data.suppress_notifications)
{
this.notify_polling();
this.disable_fields();
}
url = url + '?' + MooHash.toQueryString(params);
this.in_process[trackerid] = {
sajax: new Sajax( url, this.handler.bind( this, trackerid, data) ),
timeout: window.setTimeout(this.handler.bind( this, trackerid, data),10000)
};
},
register_on_unlock_success_handler: function(handler) {
Event.observe( $('isc-password-section'), 'isc:unlockpassword', handler );
},
reset_fields: function() {
if (this._active_password_check < 1)
{
$('isc-password-input').value = '';
$('isc-password-input').disabled = false;
$('isc-password-submit').disabled = false;
}
},
disable_fields: function() {
$('isc-password-input').disabled = "disabled";
$('isc-password-submit').disabled = "disabled";
},
notify_polling: function() {
this.iscpasswordpolling.show();
},
notify_failed: function(data) {
this.notify_lb(data);
},
notify_lb: function(data) {
this.iscpasswordlb.setMessage(data);
this.iscpasswordlb.show();
},
increment_pw_tt_cnt: function(pw) {
if (this.password_unlocks_tt_count[pw]) {
this.password_unlocks_tt_count[pw]++;
} else {
this.password_unlocks_tt_count[pw] = 1;
}
},
decrement_pw_tt_cnt: function(pw) {
if (this.password_unlocks_tt_count[pw]) {
this.password_unlocks_tt_count[pw]--;
} else {
this.password_unlocks_tt_count[pw] = 0;
}
},
handler: function(token, data, response) {
false;
var suppress_notifications = data.suppress_notifications;
var cur_process = this.in_process[token];
if (!cur_process || !cur_process.timeout)
{
false;
delete this.in_process[token];
return;
}
window.clearTimeout(cur_process.timeout);
cur_process.timeout = null;
var pw_response = response || {};
var new_match = [];
var tttext = [];
var validated = {};
var show_intermediate_msg = false;
if (Object.isArray(pw_response.match) && pw_response.match.size()) {
for(var x=0; x < pw_response.match.length; x++) {
var m = pw_response.match[x];
var tt = this.search_constraints.ticket_types.tt_modules[m.extended_ticket_type];
if ( tt && tt.tt.is_enabled ) {
if ( tt.tt.is_locked == false && tt.tt.password_used != pw_response.primary_password ) {
show_intermediate_msg = true;
this.decrement_pw_tt_cnt(tt.tt.password_used);
this.increment_pw_tt_cnt(pw_response.primary_password);
} else if (!tt.tt.password_used) {
this.increment_pw_tt_cnt(pw_response.primary_password);
}
tt.tt.password_used = pw_response.primary_password;
pw_response.match[x].primary_password = pw_response.primary_password;
new_match.push ( pw_response.match[x] );
this.search_constraints.isc.on_unlock_ticket_type( pw_response.match[x] );
if ( tt.tt.parent ) {
if ( validated[pw_response.primary_password] )
continue;
else
validated[pw_response.primary_password] = true;
}
var tmptttext = "";
if (pw_response.match[x].description) {
tmptttext = pw_response.match[x].description;
} else {
tmptttext = tt.tt.description;
}
if (pw_response.match[x].ticket_limit > 0) {
tmptttext = tmptttext + " (" + ISCPasswordRollup.tu_good_for.replace(/_n_/g, pw_response.match[x].ticket_limit) + ")";
}
tttext.push(tmptttext);
}
}
false;
}
pw_response.match = new_match;
this.setCurrentPassword( pw_response.primary_password );
if (pw_response.match && pw_response.match.size()) {
if (!suppress_notifications) {
if (show_intermediate_msg) {
var dg = new DialogChain({
begin_function  : function(){},
end_function    : function(){},
cancel_function : function(){}
});
this.iscpasswordlb.is_dialog = true;
this.iscpasswordlb.setMessage({
title: ISCPasswordRollup.duplicate_password_title,
content: ISCPasswordRollup.duplicate_password_content
});
dg.add_dialog(this.iscpasswordlb);
this.iscpasswordaccepted.updateMessage(tttext);
dg.add_dialog(this.iscpasswordaccepted);
dg.start({});
} else {
this.iscpasswordaccepted.updateMessage( tttext );
this.iscpasswordaccepted.show();
}
}
if (this.password_unlocks_tt_count[pw_response.primary_password] > 0) {
this.savePassword(pw_response.primary_password);
}
this.purgeSavedPasswords();
$('isc-password-section').fire('isc:unlockpassword', pw_response.match);
if ((this.setup_validation.valid.length + this.setup_validation.invalid.length) < this.setup_validation.count) {
this.setup_validation.valid.push(data.password);
}
}
else {
if (!suppress_notifications) {
this.notify_lb({
content: data ? pw_response.error_message : ISCPasswordRollup.default_server_error_message
});
}
if ((this.setup_validation.valid.length + this.setup_validation.invalid.length) < this.setup_validation.count) {
this.setup_validation.invalid.push(data.password);
}
}
this.search_constraints.isc.seat_map.allow_auto_refresh();
this._active_password_check--;
this.reset_fields();
delete this.in_process[token];
if (this.setup_validation.valid.length + this.setup_validation.invalid.length == this.setup_validation.count) {
document.fire('isc:checked_saved_passwords');
}
return;
},
setCurrentPassword: function(pw) {
this._last_password_used = pw;
}
});
var ISCPasswordRollupTicketTypeInfo = Class.create({
initialize: function() {
this.list = [];
var first = true;
var tip = $('isc-password-info-box-content').down();
edp.event.get_enabled_ticket_types().each(function(tt) {
if ( tt.password && tt.ticket_info_popup ) {
if(!first) tip.insert('<br/>');
var ta = new Element('a',{href:'javascript:void(0)','class':"expandosLink"});
ta.appendChild( new Element('span').update( tt.description ) );
tip.insert(ta);
this.add(ta);
ta.observe('click',function(ev) {
var t = Event.findElement(ev,'a');
this.closeAll(t);
this.toggle(t);
}.bind(this));
var tmp = tt.ticket_info_popup.module.$('content').cloneNode( true );
tmp.select('div').each(function(el) {
if (el.id.match(/_isc_/)) {
el.id = "";
el.show();
}
else if (el.id.match(/_classic_/))
el.remove();
});
tmp.id = "";
tmp.style.height = "";
tmp.style.lineHeight = "1.3em";
var tc = new Element('div',{'class':"expandos-open",style:'display:none'});
tc.appendChild(tmp);
tip.insert( tc);
first = false;
}
}.bind(this));
tip.insert(new Element('br'));
},
add: function(el) {
this.list.push($(el));
},
closeAll: function(elem) {
this.list.each(function(el) {
if (typeof elem == 'undefined' || el != elem) {
el.className = 'expandosLink';
el.next().hide();
}
});
},
toggle: function(el) {
if (el.className == 'expandosLink') {
el.className = 'expandosLink-open';
el.next().show();
} else {
el.className = 'expandosLink';
el.next().hide();
}
}
});
var ISCPasswordNotifyDialog = Class.create( Popup, {
tlog: function(){},
initialize: function( $super, password_rollup ) {
var seat_map = password_rollup.search_constraints.isc.seat_map;
$super( 'isc-password-lb', {
zIndex: 110,
lightbox: { opacity: 0.6 },
close_id: [ "closeLB", "cancelLB", "closeLB2", "cancelLB2" ],
onHide: seat_map.allow_auto_refresh.bind( seat_map ),
onShow: seat_map.prevent_auto_refresh.bind( seat_map )
});
this.password_rollup = password_rollup;
this.is_dialog = false;
$('closeLB2').observe('click', this.next.bind(this) );
$('cancelLB2').observe('click', this.next.bind(this) );
},
next: function() {
this.dialog_chain.next();
},
setMessage: function(data) {
$('isc-password-lb-title').update(data.title ? data.title : ISCPasswordRollup.default_error_title);
$('isc-password-lb-title').className = data.title_class;
$('isc-password-lb-text').update(data.content ? data.content : ISCPasswordRollup.default_error_message);
},
show: function($super) {
Popup.hide_group('passwordpolling');
var focusme = null;
if (this.is_dialog) {
$('cancelLB').up().hide();
$('closeLB').hide();
$('closeLB2').show();
focusme = $('cancelLB2');
focusme.up().show();
} else {
focusme = $('cancelLB');
focusme.up().show();
$('closeLB').show();
$('closeLB2').hide();
$('cancelLB2').up().hide();
}
this.is_dialog = false;
$super({
group: 'edp',
anchor_id: document.body,
popup_align: {
x: "center",
y: "center"
}
});
focusme.focus();
},
check: function() {
return true;
}
});
ISCPasswordAcceptedDialog = Class.create( Popup, {
tlog: function(){},
initialize: function( $super, password_rollup ) {
var seat_map = password_rollup.search_constraints.isc.seat_map;
$super( 'isc-password-accepted', {
zIndex: 110,
lightbox: { opacity: 0.6 },
close_id: [ 'isc-password-accepted-ok-button'],
onHide: seat_map.allow_auto_refresh.bind( seat_map ),
onShow: seat_map.prevent_auto_refresh.bind( seat_map )
});
this.password_rollup = password_rollup;
$('isc-password-accepted-ok-button').observe('click', this.show_map_handler.bind(this));
},
show: function( $super ) {
Popup.hide_group('edp');
$super({
group: 'passwordpolling',
anchor_id: document.body,
popup_align: {
x: "center",
y: "center"
}
});
click_track.log_datapoint({ password_success:'' });
$('isc-password-accepted-ok-button').focus();
},
show_map_handler: function() {
this.password_rollup.search_constraints.isc.seat_map.call_iscapp('resetMap');
this.password_rollup.search_constraints.price_display.reset();
this.password_rollup.search_constraints.ticket_types.uncheck_all();
if (this.password_rollup._password_unlocks_tt[this.password_rollup._last_password_used]) {
for( var i in this.password_rollup._password_unlocks_tt[this.password_rollup._last_password_used] ) {
this.password_rollup.search_constraints.ticket_types.check_ticket_type(i);
}
}
},
next: function() { return; },
check: function() { return true; },
updateMessage: function(tttext) {
if (!Object.isArray(tttext)) return;
var el = $('isc-password-accepted-text');
var ul = new Element('ul', { 'class': "list-left highlight", style: "margin-top:1em" } );
for (var i = 0; i < tttext.length; i++) {
ul.insert( new Element('li').update(tttext[i]) );
}
el.update(ul);
}
});
ISCPasswordPollingDialog = Class.create( Popup, {
tlog: function(){},
initialize: function($super, password_rollup) {
$super( 'isc-password-statusbox-polling' );
this.password_rollup = password_rollup;
},
show: function($super) {
$super({
anchor_id   : 'map-area',
group       : 'passwordpolling',
popup_align : { x: -1, y: -1 },
anchor_align: { x: -1, y: -1 },
offset      : { x: 5, y: 5 }
});
},
check: function() { return true; }
});
ISCPasswordRollup.status_display_time = 10;

