/* 
product_catalogue.js: library of javascript for product catalogue management
    david.kelly@fanore.com (http://www.fanore.com/website_design.htm)
    
    
    
*/

//apply_catalogue_filters: captures the selected filters and passes them as ajax params to target
function apply_catalogue_filters(){
    var url="../asp/object_administration.asp"
    var params="objectid=1180&mode=6&tag=products&category=" + $F('category') + "&brand=" + $F('brand') + "&distillery=" + $F('distillery') + "&site=" + $F('site') + "&report=filters"
    //alert(params)
    ajax_get_url(url ,params, 'ajax_results', '');
    return void[0];
}


//edit_product: enable editing of a product record
function edit_product(param_product,param_category,param_brand,param_distillery,param_site){
    var url="../asp/object_administration.asp"
    var params="objectid=1180&mode=6&tag=edit&recordid=" + param_product + "&category=" + param_category + "&brand=" + param_brand + "&distillery=" + param_distillery + "&site=" + param_site
    
    //alert(params)
    $('url_params').value=params
    //window.location.href=url + '?' + params
    //return void[0];
    //ajax_get_url(url ,params, 'ajax_results', 'catalogue_dialogue');
    var d=new Date();
      var myAjax = new Ajax.Updater('ajax_results', 
            url, 
            {
                method: 'get', 
                parameters: params + '&d=' + d.toString(),
                onComplete: function(obj){
                    eval('setup_editor();');
                    eval('adjust_product_editor_dialogue();');
                    setup_tab_tracking('database_editor');
                    eval('tickle_session();')
                }
            });   


}
//adjust_product_editor_dialogue: initialise the product editor dialogue (via ajax or called directly)
function adjust_product_editor_dialogue(){
//gift box option                  
  Event.observe('gift_box_option','click',function(event){
    var chk=$('gift_box_option').checked;
    if(chk==false){
        $('gift_box_details').hide();
        $('gift_box').value='0';
    }
    else{
        $('gift_box_details').show();
        $('gift_box').value='1';
    }
  });
//case price option                  
  Event.observe('case_price_option','click',function(event){
    var chk=$('case_price_option').checked;
    if(chk==false){
        $('case_price_details').hide();
        $('case_price_enabled').value='0';
    }
    else{
        $('case_price_details').show();
        $('case_price_enabled').value='1';
    }
  });
                  
                  


}


//toggle_auto_seo: turns on/off the auto fill function for the seo description
function toggle_auto_seo(){
    //get current value to decide how to toggle
    var i_current=parseInt($F('stop_auto_seo'))
    var i_new=1; //default == 'stop'
    var s_caption="enable autofill" //default == enable
    if (i_current==i_new){
        i_new=0;
        s_caption="stop autofill"
    }
    $('stop_auto_seo').value=i_new;
    $('auto_seo_button').value=s_caption
    
}

//toggle_auto_abstract: turns on/off the auto fill function for the product abstract
function toggle_auto_abstract(){
    //get current value to decide how to toggle
    var i_current=parseInt($F('stop_auto_abstract'))
    var i_new=1; //default == 'stop'
    var s_caption="enable autofill" //default == enable
    if (i_current==i_new){
        i_new=0;
        s_caption="stop autofill"
    }
    $('stop_auto_abstract').value=i_new;
    $('auto_abstract_button').value=s_caption
}


// clones text (up to a limit) into two spaces, seo_description & summary narrative
function cws_narrative(){
        var alreadyRun = false;
        var cwseditor = FCKeditorAPI.GetInstance('txtHTML') ;
    	var oDOM = cwseditor.EditorDocument ;
    	var iLength ;
        iLength = oDOM.body.innerText.length ;
        var buffer=oDOM.body.innerText;
        //alert(buffer)
        //get value of seo auto fill 
        var i_seo=parseInt($F('stop_auto_seo'))
        var i_abstract=parseInt($F('stop_auto_abstract'))
        iLength=buffer.length
        if (iLength > 100){
            iLength=100;
        }
        if ((i_seo==0)){
        
            //$('auto_seo_description').innerText=buffer.substring(0,iLength);
            $('txt_seo_description').value=buffer.substring(0,iLength);
            }
        
        iLength=buffer.length
        if (iLength > 500){
            iLength=500;
        }
        if ((i_abstract==0)){
            //$('auto_abstract').innerText=buffer.substring(0,iLength);
            $('txt_abstract').value=buffer.substring(0,iLength);
            var el=get_el('characters')
            if (el){
                 $('characters').innerText=$('txt_abstract').value.length 
            }
            }
    return void[0];        
}




// check_catalogue_item: checks that required product information has been entered
function check_catalogue_item(){

    //var main_image=$F('header_image');
    var progress_bar_url=$F('progress_bar_url');
    var product_type=parseInt($F('product_type'))
//product name
    if ($F('txt_title').blank()){
         show_error_message("please enter a name for this product!",'error_message_panel');
        $('txt_title').focus();
        return false;
    }
//product price
    if ($F('txt_price').blank()){
         show_error_message("please enter a price for this product!",'error_message_panel');
        $('txt_price').focus();
        return false;
    }
    var p=$F('txt_price')
    
    if (isNaN(p)){
         show_error_message("please enter only numberic values for the price!",'error_message_panel');
        $('txt_price').focus();
        return false;
    }
    if (p==0){
         show_error_message("please enter a value greater than zero for the price!",'error_message_panel');
        $('txt_price').focus();
        return false;
    
    }
//category
    if (parseInt($F('product_category'))==0){
         show_error_message("please select a product category!",'error_message_panel');
        $('product_category').focus();
        return false;
    }

//brand
    if (parseInt($F('product_brand'))==0){
         show_error_message("please select a brand name!",'error_message_panel');
        $('product_brand').focus();
        return false;
    }

//distillery
    if (parseInt($F('product_distillery'))==0){
         show_error_message("please select a distillery!",'error_message_panel');
        $('product_distillery').focus();
        return false;
    }

//country
    if (parseInt($F('product_country'))==0){
         show_error_message("please select a country / region!",'error_message_panel');
        $('product_country').focus();
        return false;
    }

//is this a bundle?
    if (product_type=1){
    
        //check if any items selected for the bundle
        var m=$F('members');
        if (m.length > 1){
            var a_members=m.split(',');
            var quantities='0'
            for (var i=0; i<a_members.length;i++){
                var x=parseInt(a_members[i])
                if (x!=0){
                    // validate quantity of member entered
                    var q='quantity' + x.toString()
                    if ($F(q).blank()){
                        show_error_message("please enter a quantity for each bundled item!",'error_message_panel');
                        $(q).focus();
                        return false;
                    }
                    var p=$F(q)
                    
                    if (isNaN(p)){
                         show_error_message("please enter only a numeric quantity for each bundled item!",'error_message_panel');
                        $(q).focus();
                        return false;
                    }
                    if (p==0){
                        show_error_message("please enter a quantity greater than zero for each bundled item !",'error_message_panel');
                        $(q).focus();
                        return false;
                    }
                    quantities+=',' + p
                    
                }
            }
        }
        $('member_quantities').value=quantities;
    }    
    
//  deal with image properties
    var has_picture=parseInt($F('has_picture')) //does this product already have an image ?
    var n_file=$F('product_image') // has user selected an image
    
    //default value for replacing an image is NO
    $('replace_picture').value=0 
    //if file not blank then reset replace image
    if (!n_file.blank()){
        //check that file is a JPG
        var m=n_file.toString().toLowerCase();
        var c=m.lastIndexOf(".")
        m=m.substr((c+1),4)
        if (m !='jpg'){
           show_error_message("please select a JPG for the picture !",'error_message_panel'); 
           $('product_image').focus()
           return false;
        }
        $('replace_picture').value=1
        //double check that user wants to perform this action
        if (has_picture==1){
            if (!confirm("Please confirm that you wish to replace the picture used for this product!")){
                $('replace_picture').value=0
            }
        }
    }
    

    var el=get_el('FCKDATASOURCE');
    if (el){    
        var cEditor = FCKeditorAPI.GetInstance('txtHTML');
        var datasource_name = 'fd_description';
        //alert(datasource_name);
       $(datasource_name).value= cEditor.GetXHTML(true);
         
    }       
    $('url_payload').value=$F('url_params')
    //return false;
    $('save').target='keyhole'; 
// display a window indicating save progress 
    progress_window(progress_bar_url)         
    $('save').submit();
}

function master_categories(){
    var url="../asp/object_administration.asp"
    var params="objectid=1179&mode=6&tag=_list" 
    ajax_get_url(url ,params, 'ajax_results', '');

}

 //edit_master_category: enable editing of a master category record
function edit_master_category(param_category){
    var url="../asp/object_administration.asp"
    var params="objectid=1179&mode=6&tag=edit&recordid=" + param_category
    
    //window.location.href=url + '?' + params
    //return void[0];
    $('url_params').value="master_categories();"
    var d=new Date();
      var myAjax = new Ajax.Updater('ajax_results', 
            url, 
            {
                method: 'get', 
                parameters: params + '&d=' + d.toString(),
                onComplete: function(obj){
                  Event.observe('txt_abstract','keyup',function(event){
                        if ($('txt_abstract').value.length > 499){
                            $('txt_abstract').value=$('txt_abstract').value.truncate(500,'')
                        }
                        $('characters').innerText=$('txt_abstract').value.length 
                        
                    
                                      });
                  eval('setup_editor();')
                  
                  setup_tab_tracking('database_editor');
                  eval('tickle_session();')
                }
            });   


}

// check_master_category: checks that required product information has been entered
function check_master_category(){

    //var main_image=$F('header_image');
    var progress_bar_url=$F('progress_bar_url');
//product name
    if ($F('txt_title').blank()){
         show_error_message("please enter a name for this product!",'error_message_panel');
        $('txt_title').focus();
        return false;
    }
    var el=get_el('FCKDATASOURCE');
    if (el){    
        var oEditor = FCKeditorAPI.GetInstance('txtHTML');
        var datasource_name = 'fd_description';
        //alert(datasource_name);
       $(datasource_name).value= oEditor.GetXHTML(true);
         
    }       
    $('url_payload').value=$F('url_params')
    //return false;
    $('save').target='keyhole'; 
// display a window indicating save progress 
    progress_window(progress_bar_url)         
    $('save').submit();
 
 }

//toggle_cws_bundle: toggles the bundle contents dialogue for CWS product
function toggle_cws_bundle(param_type){
    return void[0];
    if (parseInt(param_type)==0){
      $('bundle_contents_workspace').hide();
    }
    else
    {   
      $('bundle_contents_workspace').show();
    }
    
}


