includes/clientside/static/ajax.js
changeset 29 e5484a9e0818
parent 28 dd2edcdc6c03
child 30 7e8fd44b36b0
equal deleted inserted replaced
28:dd2edcdc6c03 29:e5484a9e0818
   471 // Change the user's preferred style/theme
   471 // Change the user's preferred style/theme
   472 
   472 
   473 function ajaxChangeStyle()
   473 function ajaxChangeStyle()
   474 {
   474 {
   475   var inner_html = '';
   475   var inner_html = '';
   476   inner_html += '';
   476   inner_html += '<p><label>Theme: ';
       
   477   inner_html += '  <select id="chtheme_sel_theme" onchange="ajaxGetStyles(this.value);">';
       
   478   inner_html += '    <option value="_blank" selected="selected">[Select]</option>';
       
   479   inner_html +=      ENANO_THEME_LIST;
       
   480   inner_html += '  </select>';
       
   481   inner_html += '</label></p>';
       
   482   var chtheme_mb = new messagebox(MB_OKCANCEL|MB_ICONQUESTION, 'Change your theme', inner_html);
       
   483   chtheme_mb.onbeforeclick['OK'] = ajaxChangeStyleComplete;
       
   484 }
       
   485 
       
   486 function ajaxGetStyles(id)
       
   487 {
       
   488   var thediv = document.getElementById('chtheme_sel_style_parent');
       
   489   if ( thediv )
       
   490   {
       
   491     thediv.parentNode.removeChild(thediv);
       
   492   }
       
   493   if ( id == '_blank' )
       
   494   {
       
   495     return null;
       
   496   }
       
   497   ajaxGet(stdAjaxPrefix + '&_mode=getstyles&id=' + id, function() {
       
   498       if ( ajax.readyState == 4 )
       
   499       {
       
   500         // IE doesn't like substr() on ajax.responseText
       
   501         var response = String(ajax.responseText + ' ');
       
   502         response = response.substr(0, response.length - 1);
       
   503         if ( response.substr(0,1) != '[' )
       
   504         {
       
   505           alert('Invalid or unexpected JSON response from server:\n' + response);
       
   506           return null;
       
   507         }
       
   508         
       
   509         // Build a selector and matching label
       
   510         var data = parseJSON(response);
       
   511         var options = new Array();
       
   512         for( var i in data )
       
   513         {
       
   514           var item = data[i];
       
   515           var title = themeid_to_title(item);
       
   516           var option = document.createElement('option');
       
   517           option.value = item;
       
   518           option.appendChild(document.createTextNode(title));
       
   519           options.push(option);
       
   520         }
       
   521         var p_parent = document.createElement('p');
       
   522         var label  = document.createElement('label');
       
   523         p_parent.id = 'chtheme_sel_style_parent';
       
   524         label.appendChild(document.createTextNode('Style: '));
       
   525         var select = document.createElement('select');
       
   526         select.id = 'chtheme_sel_style';
       
   527         for ( var i in options )
       
   528         {
       
   529           select.appendChild(options[i]);
       
   530         }
       
   531         label.appendChild(select);
       
   532         p_parent.appendChild(label);
       
   533         
       
   534         // Stick it onto the messagebox
       
   535         var div = document.getElementById('messageBox');
       
   536         var kid = div.firstChild.nextSibling;
       
   537         
       
   538         kid.appendChild(p_parent);
       
   539         
       
   540       }
       
   541     });
       
   542 }
       
   543 
       
   544 function ajaxChangeStyleComplete()
       
   545 {
       
   546   var theme = $('chtheme_sel_theme');
       
   547   var style = $('chtheme_sel_style');
       
   548   if ( !theme.object || !style.object )
       
   549   {
       
   550     alert('Please select a theme from the list.');
       
   551     return true;
       
   552   }
       
   553   var theme_id = theme.object.value;
       
   554   var style_id = style.object.value;
       
   555   
       
   556   if ( typeof(theme_id) != 'string' || typeof(style_id) != 'string' )
       
   557   {
       
   558     alert('Couldn\'t get theme or style ID');
       
   559     return true;
       
   560   }
       
   561   
       
   562   if ( theme_id.length < 1 || style_id.length < 1 )
       
   563   {
       
   564     alert('Theme or style ID is zero length');
       
   565     return true;
       
   566   }
       
   567   
       
   568   ajaxPost(stdAjaxPrefix + '&_mode=change_theme', 'theme_id=' + escape(theme_id) + '&style_id=' + escape(style_id), function()
       
   569     {
       
   570       if ( ajax.readyState == 4 )
       
   571       {
       
   572         if ( ajax.responseText == 'GOOD' )
       
   573         {
       
   574           var c = confirm('Your theme preference has been changed.\nWould you like to reload the page now to see the changes?');
       
   575           if ( c )
       
   576             window.location.reload();
       
   577         }
       
   578         else
       
   579         {
       
   580           alert('Error occurred during attempt to change theme:\n' + ajax.responseText);
       
   581         }
       
   582       }
       
   583     });
       
   584   
       
   585   return true;
       
   586   
       
   587 }
       
   588 
       
   589 function themeid_to_title(id)
       
   590 {
       
   591   if ( typeof(id) != 'string' )
       
   592     return false;
       
   593   id = id.substr(0, 1).toUpperCase() + id.substr(1);
       
   594   id = id.replace(/_/g, ' ');
       
   595   id = id.replace(/-/g, ' ');
       
   596   return id;
   477 }
   597 }
   478 
   598 
   479 /*
   599 /*
   480 function ajaxChangeStyle()
   600 function ajaxChangeStyle()
   481 {
   601 {