﻿/* ************************************************************************************ */
/* ---> Requires jQuery library                                                         */
/*                                                                                      */
/* Change Log                                                                           */
/*                                                                                      */
/* 10-06-2010  - Richard Chapman - Deltasoft - Added                                    */
/* ************************************************************************************ */

function clibtnadd_Click(s,e)
{
// Process all custom text options
var r = jQuery('.optmemo'); 
var idx = 0;   
if (r.length > 0)
    {
    idx = 0;
    var memo;
    for (idx = 0; idx < r.length; idx++)
       {
       memo = r.get(idx);
       clistorage.Set(memo.id,memo.value);
       }
    }
    
// Custom image options are handled as they go
    
// Process all available option dropdowns and validate them and their option type
r = jQuery('.optdropdown');
if (r.length > 0)
    {
    var dropdown;
    for (idx = 0; idx < r.length; idx++)
       {
       dropdown = r.get(idx);
       
       // Validate the entries as we go
       var o = dropdown.options[dropdown.selectedIndex];
       var optid = dropdown.id.slice('dropdown_'.length);
       
       // Fetch the full category name in case it needs to be reported
       var optcategory = jQuery('#header_' + optid).text();
              
       // If the value (id) is zero then nothing has been selected so bail out
       if (o.value == 0)
          {
          e.processOnServer = false;
          dropdown.focus();
          msgbox('Select a ' + optcategory + ' option');
          return;
          }

       var v = true;
       var m = '';
       var t = jQuery(dropdown).attr('custominfo'); 
       switch (t)
          {
          case '1':
             // Ensure that if required an image has been uploaded
             if (jQuery(o).attr('noimage') !== 'true')
                {
                v = false;
                if (clistorage.Contains('img_' + optid)) {
                   var img = clistorage.Get('img_' + optid);
                   if (img !== '') {
                      var blankimg = 'bongo/assets/gif/picture32x32.gif';
                      var imgptr = img.toLowerCase().lastIndexOf(blankimg);
                      v = (imgptr !== (img.length - blankimg.length));
                      }
                   }
                   
                if (!v)
                   m = 'Please upload image for ' + optcategory + ' option';
                }
             break;
          case '2':
             // Ensure that if required text has been entered
             if (jQuery(o).attr('maxtext') > 0)
                {
                v = false;
                if (clistorage.Contains('memo_' + optid))
                   v = (clistorage.Get('memo_' + optid).length > 0);
                   
                if (!v)
                   m = 'Please enter text for ' + optcategory + ' option';
                }
             break;                       
          default:
             // Nothing further to validate
             break;             
          }
          
       if (v)
          clistorage.Set(dropdown.id,o.value);
       else
          {
          e.processOnServer = false;
          dropdown.focus();
          msgbox(m);
          break;
          }
       }
    }
}

function msgbox(m)
{
var e = clipupmessage.GetWindowByName('errorwin');
clilblmessage.SetText(m);
clipupmessage.ShowWindow(e);
}

function cliimg_Click(s)
{
    clistorage.Set('activeoptionID',s);
    var img = jQuery('#'+s+'_ctl').attr("src");
    cliimgcustom.SetImageUrl(img);
    clipupimg.Show();
}

function cliuplcustomimg_OnUploadComplete(s,e)
{
    if (e.isValid)
       {
       cliimgcustom.SetImageUrl(e.callbackData);
       }      
}

function clibtnok_Click(s,e)
{
    var optobj = clistorage.Get('activeoptionID');
    var imgurl = cliimgcustom.GetImageUrl();
    clistorage.Set(optobj,imgurl);
    jQuery('#'+optobj+'_ctl').attr("src",imgurl);
    clipupimg.Hide();
}

function clibtnmsgok_Click(s,e)
{
    var errorwin = clipupmessage.GetWindowByName('errorwin');
    clipupmessage.HideWindow(errorwin);
}

function clihtmeditor_CustomCommand(s,e)
{
    if (e.commandName == 'openfile')
        {
        clipupopenfile.Show();
        }
}
