/* 
cws.js: library of javascript for animation / branding effects
    david.kelly@fanore.com (http://www.fanore.com/website_design.htm)
    
    
    
*/


//cws_region:validate that region dialogue has been populated properly
function cws_region(){
    var progress_bar_url=$F('progress_bar_url');

//region name
    if ($F('txt_title').blank()){
         show_error_message("please enter a name for this country / region!",'error_message_panel');
        $('txt_title').focus();
        return false;
    }
//tax_rate
    if ($F('tax_rate').blank()){
        $('tax_rate').value='0';
        return false;
    }
    var p=$F('tax_rate')
    
    if (isNaN(p)){
         show_error_message("please enter only numeric values for the tax rate (e.g. 0.215 = 21.5%)!",'error_message_panel');
        $('tax_rate').focus();
        return false;
    }
    $('url_payload').value=$F('url_params')
    //return false;
    $('save').target='keyhole'; 
// display a window indicating save progress 
    progress_window(progress_bar_url)         
    $('save').submit();
    
    
} 

//cws_tax_rate: retrieve the parent element tax rate as 'default'
function cws_tax_rate(){
    //alert($F('parentid'));
    if ($F('parentid')=='0'){
        return void[0];
    }
    if ($F('tax_rate')!='0'){
        return void[0];
    }
        var url="../asp/object_administration.asp"
        var params="objectid=1195&mode=6&tag=_tax_rate&recordid=" + $F('parentid')
        var d=new Date();
          var myAjax = new Ajax.Updater('ajax_tax_rate', 
                url, 
                {
                    method: 'get', 
                    parameters: params + '&d=' + d.toString(),
                    onComplete: function(obj){
                      $('tax_rate').value=$('ajax_tax_rate').innerText;
                      return void[0];
                    }
                });   
        var params="objectid=1195&mode=6&tag=_default_currency&recordid=" + $F('parentid')
        var d=new Date();
          var myAjax = new Ajax.Updater('ajax_default_currency', 
                url, 
                {
                    method: 'get', 
                    parameters: params + '&d=' + d.toString(),
                    onComplete: function(obj){
                      $('default_currency').value=$('ajax_default_currency').innerText;
                      return void[0];
                    }
                });   
        var params="objectid=1195&mode=6&tag=_require_zip&recordid=" + $F('parentid')
        var d=new Date();
          var myAjax = new Ajax.Updater('ajax_require_zip', 
                url, 
                {
                    method: 'get', 
                    parameters: params + '&d=' + d.toString(),
                    onComplete: function(obj){
                      $('require_zip').value=$('ajax_require_zip').innerText;
                      if (parseInt($('require_zip').value)==1){
                        $('select_zip').selectedIndex=0;
                      }
                      else{
                        $('select_zip').selectedIndex=1;
                      }
                      return void[0];
                    }
                });   
}

//cws_zip: used in commerce_settings/supported_countries editor
function cws_zip(){
    $F('require_zip').value=$F('select_zip');
    
    
}

//cws_currency:validate that currency dialogue has been populated properly
function cws_currency(){
    var progress_bar_url=$F('progress_bar_url');

//currency caption
    if ($F('caption').blank()){
         show_error_message("please enter a caption for this currency (e.g. Europe / Euro)!",'error_message_panel');
        $('caption').focus();
        return false;
    }
//currency name
    if ($F('txt_title').blank()){
         show_error_message("please enter the acronym for this currency (e.g. EUR or USD or GBP,etc.)!",'error_message_panel');
        $('txt_title').focus();
        return false;
    }
//exchange_rate
    if ($F('exchange_rate').blank()){
         show_error_message("please enter only numeric values for the exchange rate (e.g. 1 or 0.677!",'error_message_panel');
        $('exchange_rate').value='0';
        return false;
    }
    var p=$F('exchange_rate')
    
    if (isNaN(p)){
         show_error_message("please enter only numeric values for the tax rate (e.g. 0.215 = 21.5%)!",'error_message_panel');
        $('exchange_rate').focus();
        return false;
    }
    $('url_payload').value=$F('url_params')
    //return false;
    $('save').target='keyhole'; 
// display a window indicating save progress 
    progress_window(progress_bar_url)         
    $('save').submit();
    
    
} 


//cws_freight_service: validate freight service dialogue
function cws_freight_service(){
    var progress_bar_url=$F('progress_bar_url');

//caption
    if ($F('caption').blank()){
         show_error_message("please enter a name for this freight service!",'error_message_panel');
        $('caption').focus();
        return false;
    }




    $('url_payload').value=$F('url_params')
    //return false;
    $('save').target='keyhole'; 
// display a window indicating save progress 
    progress_window(progress_bar_url)         
    $('save').submit();
    

}


//cws_freight_services: when user selects a freight service, this triggers loading of list of costs assocaiated
function cws_freight_services(track){
    //exit if nothing selected
    if ($F('list_freight_services').blank()){return void[0];}
    //populate service_freight_costs panel
        var url="../asp/object_administration.asp"
        var params="objectid=1195&mode=6&tag=_list_freight_service_costs&freightserviceid=" + $F('list_freight_services')
        var d=new Date();
          var myAjax = new Ajax.Updater('service_freight_costs', 
                url, 
                {
                    method: 'get', 
                    parameters: params + '&d=' + d.toString(),
                    onComplete: function(obj){
                        //alert(track + "&freightserviceid=" + $F('list_freight_services') );
                      $('url_params').value=track + "&freightserviceid=" + $F('list_freight_services')  
                      return void[0];
                    }
                });   
    
    
    
}


//cws_freight_cost: validate the freight costs editing dialogue
function cws_freight_cost(){
    var progress_bar_url=$F('progress_bar_url');

//country_id
    if ($F('country_id')=='0'){
         show_error_message("please select the country (destination) to which this freight charge applies!",'error_message_panel');
        $('country_id').focus();
        return false;
    }
    
//range_start
    if ($F('range_start').blank()){
        show_error_message("please enter a value for the start of the range (e.g. 1 = 1 kg)!",'error_message_panel');
        return false;
    }
    
    var p=$F('range_start')
    if (isNaN(p)){
         show_error_message("please enter only numeric values for the start of the range (e.g. 1 = 1 kg)!",'error_message_panel');
        $('range_start').focus();
        return false;
    }
    
//range_end
    if ($F('range_end').blank()){
        show_error_message("please enter a value for the end of the range (e.g. 1 = 1 kg)!",'error_message_panel');
        return false;
    }
    
    var p=$F('range_end')
    if (isNaN(p)){
         show_error_message("please enter only numeric values for the end of the range (e.g. 1 = 1 kg)!",'error_message_panel');
        $('range_end').focus();
        return false;
    }
//range_end
    if ($F('price').blank()){
        show_error_message("please enter the price (e.g. 0 if free)!",'error_message_panel');
        return false;
    }
    
    var p=$F('price')
    if (isNaN(p)){
         show_error_message("please enter only numeric values for the price!",'error_message_panel');
        $('price').focus();
        return false;
    }
    
    $('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 check_xe_rate(param_acronym){
    var acronym='';
    if (arguments.length==0){
        if ($F('txt_title').blank()){
            show_error_message("please enter the acronym for this currency (e.g. EUR or USD or GBP,etc.) before checking rate!",'error_message_panel'); 
            $('txt_title').focus();
            return void[0];  
        }
        acronym=$F('txt_title');
    }
    else{
        acronym=param_acronym
    }
    
    var url="http://www.xe.com/ucc/convert.cgi?Amount=1&From=EUR&To=" + acronym;
    var x=window.open(url,'XE','width=1024,height=600', true);
    return void[0];
}

// pickup_site: pickups up the selected site before allowing generic selector to proceed
function pickup_site(){
    var ret=''
    ret='&site=' + $F('site_id');
    return ret;
}
