var questionnaireOptions = {
questionnaireCode: null,
dialog: null,
showQuestionnaireOptions: function(code)
{
questionnaireOptions.init(code);
},
init: function(code)
{
if (code){
this.questionnaireCode = code;
this.showOptionWindow();
}
return false;
},
showOptionWindow: function()
{
var obj = this;
var dialogParams = {
modal:true,
title:"Sharing Options",
overlay:{backgroundColor:'#000',opacity: 0.5},
buttons:{
CANCEL: function(){
obj.closeDialog();
},
'SHARE AND SEND NOTIFICATION': function() {
obj.shareQuestionnaireAndSend();
},
'SHARE ONLY': function(){
obj.shareQuestionnaire();
}
},
resizable: false,
width: 550
//height: 270
};
this.dialog = $('
').dialog(dialogParams)
.parent().find(".ui-dialog-titlebar-close").hide().end().end()
.append('This questionnaire will be available by visiting the following URL if sharing will be enabled:
'+BASE_URL+'/index.html?code='
+obj.questionnaireCode+'
E-mail for notification:');
},
closeDialog: function()
{
this.dialog.dialog('destroy').remove();
},
shareQuestionnaireAndSend: function()
{
if (!this.validateDialog()){
return false;
}
var obj = this;
var email = $(this.dialog).find('#email').val();
this.shareQuestionnaire(function() {
obj.getMailMessage(email);
})
return true;
//================
var options = {
url: BASE_URL + '/' + CURRENT_PAGE,
type: 'POST',
dataType: 'json',
data: {
action: 'shareAndSend',
email: $(this.dialog).children('#email').val()
},
success: function(){
obj.closeDialog();
}
}
$.ajax(options);
},
getMailMessage: function(email)
{
var obj = this;
var options = {
url: BASE_URL + '/' + CURRENT_PAGE,
type: 'POST',
dataType: 'json',
data: {
action: 'getSendMailWindow',
email: email
},
success: function(data){
if (data.form) {
obj.dialog.parent().find('.ui-dialog-content').empty();
obj.dialog.parent().find('.ui-dialog-content').append(data.form);
var editorOptions = {
mode: 'edit',
editorOptions: {
Config: {
AutoDetectLanguage: false,
EditorAreaCSS: BASE_URL + '/css/typographic.css'
},
BasePath: BASE_URL + '/js/fckeditor/',
ToolbarSet: 'Basic',
Height: 150
}
};
Qs_Form_Element_FckEditor.init('body', editorOptions);
obj.dialog.parent().find('.ui-dialog-buttonpane').empty();
var closeButton = $('').click(function() {
obj.closeDialog();
});
var sendButton = $('').click(function() {
obj.sendMail();
});
obj.dialog.parent().find('.ui-dialog-buttonpane').append(closeButton).append(sendButton);
}
}
}
$.ajax(options);
},
sendMail: function()
{
var obj = this;
if (!this.validateMail()) {
return false;
}
var bodyEditor = FCKeditorAPI.GetInstance('body');
this.dialog.find('textarea[name=body]').val(bodyEditor.GetData());
$.getJSON(BASE_URL + '/' + CURRENT_PAGE + '?' + this.dialog.find('form').serialize() + '&callback=?', {
action: 'sendMail'
}, function(data){
obj.closeDialog();
});
return true;
},
validateMail: function()
{
var bodyEditor = FCKeditorAPI.GetInstance('body');
var errorMessage = 'Value is required and can\'t be empty';
this.clearDialogError();
if ($.trim(bodyEditor.GetData()) == '') {
$('textarea[name=body]').after('' + errorMessage + '
');
return false;
}
if ($.trim($('input[name=subject]').val()) == '') {
$('input[name=subject]').after('' + errorMessage + '
');
return false;
}
return true;
},
shareQuestionnaire: function(callback)
{
var obj = this;
var options = {
url: BASE_URL + '/' + CURRENT_PAGE,
type: 'POST',
dataType: 'json',
data: {
action: 'shareQuestionnaire'
},
success: function(){
if (callback) {
callback();
} else {
obj.closeDialog();
}
}
}
$.ajax(options);
},
validateDialog: function()
{
this.clearDialogError();
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
var mail = $(this.dialog).children('#email').val();
var error = false;
if (!mail){
error = "E-mail is required";
}
if (!error && !emailPattern.test(mail)) {
error = "'" + mail + "' is no valid email address";
}
if (error){
this.showDialogError(error);
return false;
}
return true;
},
showDialogError: function(errorMessage)
{
$('' + errorMessage + '
').appendTo(this.dialog);
},
clearDialogError: function()
{
$(this.dialog).parent().find('.dialogError').remove();
}
}