var Calculator = Class.create(
{
  initialize: function() 
  {
      this.selected_usage = "";
    this.selected_media = "";
    this.selected_quantity = "";
    this.selected_insertion = "";
    this.selected_copies = "";
    this.selected_duration = "";
    this.selected_size = "";
    this.selected_region = "";
    this.selected_position = "";
    
    this.selected_usage_value = "";
    this.selected_media_value = "";
    this.selected_quantity_value = "";
    this.selected_insertion_value = "";
    this.selected_copies_value = "";
    this.selected_duration_value = "";
    this.selected_size_value = "";
    this.selected_region_value = "";
    this.selected_position_value = "";
    
    this.selected_usage_section_caption = "";
    this.selected_media_section_caption = "";
    this.selected_quantity_section_caption = "";
    this.selected_insertion_section_caption = "";
    this.selected_copies_section_caption = "";
    this.selected_duration_section_caption = "";
    this.selected_size_section_caption = "";
    this.selected_region_section_caption = "";
    this.selected_position_section_caption = "";
    
    //this.basePrice = basePrice;
    
    //this.basePrice = 75;
    //this.currency = "USD";
    
    this.usage_ads_mul = 1;
    this.usage_pubs_mul = 1;
    this.usage_int_mul = 1.5;
    this.usage_per_mul = 1.25;
    
    //this.usage_ads_caption = ;
    //this.usage_pubs_mul = 1;
    
    
    
    this.media_ads_email_mul = 0.2; //Email-promotional
    this.media_ads_web_mul = 0.4; //Website
    this.media_ads_cd_mul = 0.6; //CD/DVD
    this.media_ads_dir_mul = 0.8; //Directory
    this.media_ads_bro_mul = 1; //Brochures/Direct Mailing
    this.media_ads_mag_mul = 1.2; //Magazines
    this.media_ads_news_mul = 1.2; //newspapers
    this.media_ads_tab_mul = 1.4; //Table tent/counter card
    this.media_ads_tra_mul = 1.6; //Trade show panel/presentation
    this.media_ads_bra_mul = 1.8; //Branding/Packaging
    this.media_ads_post_mul = 2; //Indoor Display
    this.media_ads_trans_mul = 2.2; //Transit Ads
    this.media_ads_out_mul = 2.4; //Outdoor Display
    this.media_ads_bil_mul = 2.6; //Billboards
    this.media_ads_tel_mul = 2.8; //Television/Film
    
    /*
    this.media_ads_email_mul = 1.5; //Email-promotional
    this.media_ads_web_mul = 3.0; //Website
    this.media_ads_cd_mul = 4.5; //CD/DVD
    this.media_ads_dir_mul = 6; //Directory
    this.media_ads_bro_mul = 7.5; //Brochures/Direct Mailing
    this.media_ads_mag_mul = 9; //Magazines
    this.media_ads_news_mul = 9; //newspapers
    this.media_ads_tab_mul = 10.5; //Table tent/counter card
    this.media_ads_tra_mul = 12; //Trade show panel/presentation
    this.media_ads_bra_mul = 13.5; //Branding/Packaging
    this.media_ads_post_mul = 15; //Indoor Display
    this.media_ads_trans_mul = 16.5; //Transit Ads
    this.media_ads_out_mul = 18; //Outdoor Display
    this.media_ads_bil_mul = 19.5; //Billboards
    this.media_ads_tel_mul = 21; //Television/Film
    */
    
    this.media_pubs_web_mul = 0.3; //Website
    this.media_pubs_rep_mul = 0.2; //Annual Financial Report
    this.media_pubs_bok_mul = 0.6; //Book(printed)
    this.media_pubs_bro_mul = 0.3; //Booklet Brochure
    this.media_pubs_mag_mul = 0.7; //Magazines
    this.media_pubs_news_mul = 0.4; //Newspapers
    
    /*
    this.media_pubs_web_mul = 0.1; //Website
    this.media_pubs_rep_mul = 0.2; //Annual Financial Report
    this.media_pubs_bok_mul = 0.3; //Book(printed)
    this.media_pubs_bro_mul = 0.4; //Booklet Brochure
    this.media_pubs_mag_mul = 0.5; //Magazines
    this.media_pubs_news_mul = 0.6; //Newspapers
    */
    
    
    
    this.insertion_ads_1_mul = 1;
    this.insertion_ads_3_mul = 2.25;
    this.insertion_ads_5_mul = 3;
    this.insertion_ads_10_mul = 4.15;
    this.insertion_ads_x_mul = 5.75;
    
    
    this.insertion_pubs_1_mul = this.insertion_ads_1_mul;
    this.insertion_pubs_3_mul = this.insertion_ads_3_mul;
    this.insertion_pubs_5_mul = this.insertion_ads_5_mul;
    this.insertion_pubs_10_mul = this.insertion_ads_10_mul;
    this.insertion_pubs_x_mul = this.insertion_ads_x_mul;
    
    this.position_pos_ins_mul = 2;
    this.position_pos_cov_mul = 2;
    
    this.duration_ads_1_mul = 1;
    this.duration_ads_3_mul = 1.25;
    this.duration_ads_6_mul = 1.5;
    this.duration_ads_12_mul = 1.75;
    this.duration_ads_36_mul = 2;
    this.duration_ads_60_mul = 2.25;
    this.duration_ads_120_mul = 2.5;
    
    this.duration_pubs_1_mul = this.duration_ads_1_mul;
    this.duration_pubs_3_mul = this.duration_ads_3_mul;
    this.duration_pubs_6_mul = this.duration_ads_6_mul;
    this.duration_pubs_12_mul = this.duration_ads_12_mul;
    this.duration_pubs_36_mul = this.duration_ads_36_mul;
    this.duration_pubs_60_mul = this.duration_ads_60_mul;
    this.duration_pubs_120_mul = this.duration_ads_120_mul;
    
    this.duration_web_1_mul = this.duration_ads_1_mul;
    this.duration_web_3_mul = this.duration_ads_3_mul;
    this.duration_web_6_mul = this.duration_ads_6_mul;
    this.duration_web_12_mul = this.duration_ads_12_mul;
    this.duration_web_36_mul = this.duration_ads_36_mul;
    this.duration_web_60_mul = this.duration_ads_60_mul;
    this.duration_web_120_mul = this.duration_ads_120_mul;
    
    this.size_ads_1_4_mul = 1.5;
    this.size_ads_1_2_mul = 1.75;
    this.size_ads_3_4_mul = 2;
    this.size_ads_1_mul = 2.25;
    
    this.size_pos_1_4_mul = this.size_ads_1_4_mul;
    this.size_pos_1_2_mul = this.size_ads_1_2_mul;
    this.size_pos_3_4_mul = this.size_ads_3_4_mul;
    this.size_pos_1_mul = this.size_ads_1_mul;
    
    
    this.size_pubs_1_4_mul = this.size_ads_1_4_mul;
    this.size_pubs_1_2_mul = this.size_ads_1_2_mul;
    this.size_pubs_3_4_mul = this.size_ads_3_4_mul;
    this.size_pubs_1_mul = this.size_ads_1_mul;
    
    this.quantity_1_mul = 1;
    this.quantity_5_mul = 1.05;
    this.quantity_50_mul = 1.10;
    this.quantity_100_mul = 1.15;
    this.quantity_500_mul = 1.25;
    this.quantity_1000_mul = 1.5;
    this.quantity_5000_mul = 1.75;
    this.quantity_10000_mul = 2.0;
    this.quantity_50000_mul = 2.5;
    this.quantity_100000_mul = 2.75;
    this.quantity_x_mul = 3;
    
    this.quantity_2_1_mul = this.quantity_1_mul;
    this.quantity_2_100_mul = this.quantity_100_mul;
    this.quantity_2_500_mul = this.quantity_500_mul;
    this.quantity_2_1000_mul = this.quantity_1000_mul;
    this.quantity_2_5000_mul = this.quantity_5000_mul;
    this.quantity_2_10000_mul = this.quantity_10000_mul;
    this.quantity_2_50000_mul = this.quantity_50000_mul;
    this.quantity_2_100000_mul = this.quantity_100000_mul;
    this.quantity_2_x_mul = this.quantity_x_mul;
    
    this.quantity_pos_100_mul = this.quantity_100_mul;
    this.quantity_pos_500_mul = this.quantity_500_mul;
    this.quantity_pos_1000_mul = this.quantity_1000_mul;
    this.quantity_pos_5000_mul = this.quantity_5000_mul;
    this.quantity_pos_10000_mul = this.quantity_10000_mul;
    this.quantity_pos_50000_mul = this.quantity_50000_mul;
    this.quantity_pos_100000_mul = this.quantity_100000_mul;
    this.quantity_pos_x_mul = this.quantity_x_mul;
    
    this.quantity_web_1_mul = this.quantity_1_mul;
    this.quantity_web_100_mul = this.quantity_100_mul;
    this.quantity_web_500_mul = this.quantity_500_mul;
    this.quantity_web_1000_mul = this.quantity_1000_mul;
    this.quantity_web_5000_mul = this.quantity_5000_mul;
    this.quantity_web_10000_mul = this.quantity_10000_mul;
    this.quantity_web_50000_mul = this.quantity_50000_mul;
    this.quantity_web_100000_mul = this.quantity_100000_mul;
    this.quantity_web_x_mul = this.quantity_x_mul;
    
    this.position_web_sub_mul = 1;
    this.position_web_home_mul = 4;
    this.position_web_home_sub_mul = 5;
    this.position_web_ban_mul = 1.75;
    this.position_web_int_mul = 1.25;
    
    this.position_web_2_sub_mul = this.position_web_sub_mul;
    this.position_web_2_home_mul = this.position_web_home_mul;
    this.position_web_2_home_sub_mul = this.position_web_home_sub_mul;
    this.position_web_2_ban_mul = this.position_web_ban_mul;
    this.position_web_2_int_mul = this.position_web_int_mul;
    
    this.region_1_mul = 1;
    this.region_5_mul = 1.25;
    this.region_20_mul = 2;
    this.region_x_mul = 3;
    
    
    this.copies_ads_5000_mul = 1.25;
    this.copies_ads_10000_mul = 1.50;
    this.copies_ads_25000_mul = 1.75;
    this.copies_ads_50000_mul = 2;
    this.copies_ads_100000_mul = 2.25;
    this.copies_ads_250000_mul = 2.50;
    this.copies_ads_500000_mul = 2.75;
    this.copies_ads_1000000_mul = 3;
    this.copies_ads_2000000_mul = 3.25;
    this.copies_ads_5000000_mul = 3.50;
    this.copies_ads_10000000_mul = 3.75;
    this.copies_ads_20000000_mul = 4;
    this.copies_ads_30000000_mul = 4.25;
    this.copies_ads_x_mul = 4.75;
    
    this.copies_pubs_5000_mul = this.copies_ads_5000_mul;
    this.copies_pubs_10000_mul = this.copies_ads_10000_mul;
    this.copies_pubs_25000_mul = this.copies_ads_25000_mul;
    this.copies_pubs_50000_mul = this.copies_ads_50000_mul;
    this.copies_pubs_100000_mul = this.copies_ads_100000_mul;
    this.copies_pubs_250000_mul = this.copies_ads_250000_mul;
    this.copies_pubs_500000_mul = this.copies_ads_500000_mul;
    this.copies_pubs_1000000_mul = this.copies_ads_1000000_mul;
    this.copies_pubs_2000000_mul = this.copies_ads_2000000_mul;
    this.copies_pubs_5000000_mul = this.copies_ads_5000000_mul;
    this.copies_pubs_10000000_mul = this.copies_ads_10000000_mul;
    this.copies_pubs_20000000_mul = this.copies_ads_20000000_mul;
    this.copies_pubs_30000000_mul = this.copies_ads_30000000_mul;
    this.copies_pubs_x_mul = this.copies_ads_x_mul;
    
    this.region_1_count = 1;
    this.region_5_count = 5;
    this.region_20_count = 20;
    this.region_x_count = 'x';
    
    this.country_select_count = 0;
    
    },
    init: function()
  {
    
    
    this.usage();
    /*
    this.media_ads();
    this.media_pubs();
    this.insertion_ads();
    this.copies_ads();
    this.copies_pubs();
    this.duration_ads();
    this.size_ads();
    this.quantity();
    this.quantity_2();
    this.quantity_web();
    this.position_web();
    this.position_web_2();
    this.duration_web();
    this.quantity_pos();
    this.size_pos();
    this.position_pos();
    this.region();
    this.insertion_pubs();
    this.size_pubs();
    this.duration_pubs();
    */
    
    
    
    //this.insertion_duration_ads();
    //this.insertion_duration_size_ads();
    //this.observeSectionRow();
    
    this.setCountryObserver();
    
    $('calc-rev-usage').observe('click',function(event)
    {
      $('calc-main').show();
      $('calc-desc').show();
      $('calc-desc-header').show();
      $('calc-result').hide();
      Event.stop(event);
    });
    
    
    
    /*
    $('country-button').observe('click',function()
    {
      $('country-panel').toggle();
      if($F('country-button') == "Open country selection panel")
      {
        $('country-button').value = "Close country selection panel";
      }
      else
      {
        $('country-button').value = "Open country selection panel";
      }
    });
    */
    //console.log(Event);
    
  },
  usage: function()
  {
	if($('usage_ads'))
	{			    
	    $('usage_ads').observe('click',function()
	    {
	      $('usage').insert({'after':$('media_ads')});
	      
	      this.removeNextSiblings($('usage'));
	      $('media_ads').show();
	      this.media_ads();
	      
	      var current_price = this.basePrice * this.usage_ads_mul;
	      this.media_ads_price = current_price;
	      
	      this.selected_usage = this.getCaption($('usage_ads'));
	      this.selected_usage_value = this.getValue($('usage_ads'));
	      this.selected_usage_section_caption = this.getSectionCaption($('usage'));
	      
	      $('usage_ads').siblings().each(function(elm)
	      {
	        $(elm).removeClassName('section-row-selected');
	      });
	      $('usage_ads').addClassName('section-row-selected');
	      
	      
	    }.bind(this));
	    
	    $('usage_ads').observe('mouseover', function()
	    {
	    	$('usage_ads').addClassName('section-row-hover');
	      	$('calc-desc').update($('usage_ads').title);
	    });
	    
	    $('usage_ads').observe('mouseout', function()
	    {
	      	$('usage_ads').removeClassName('section-row-hover');
	    });
    }
    
    if($('usage_pubs'))
	{
	    $('usage_pubs').observe('click',function()
	    {
	      $('usage').insert({'after':$('media_pubs')});
	      this.removeNextSiblings($('usage'));
	      $('media_pubs').show();
	      this.media_pubs();
	      
	      var current_price = this.basePrice * this.usage_pubs_mul;
	      this.media_pubs_price = current_price;
	      
	      this.selected_usage = this.getCaption($('usage_pubs'));
	      this.selected_usage_value = this.getValue($('usage_pubs'));
	      this.selected_usage_section_caption = this.getSectionCaption($('usage'));
	      
	      $('usage_pubs').siblings().each(function(elm)
	      {
	        $(elm).removeClassName('section-row-selected');
	      });
	      $('usage_pubs').addClassName('section-row-selected');
	    }.bind(this));
	    
	    $('usage_pubs').observe('mouseover', function()
	    {
	      	$('usage_pubs').addClassName('section-row-hover');
	      	$('calc-desc').update($('usage_pubs').title);
	    });
	    
	    $('usage_pubs').observe('mouseout', function(){
	      	$('usage_pubs').removeClassName('section-row-hover');
	    });
    }
    
    if($('usage_int'))
    {
	    $('usage_int').observe('click',function()
	    {
	      $('usage').insert({'after':$('duration_ads')});
	      this.removeNextSiblings($('usage'));
	      $('duration_ads').show();
	      this.duration_ads();
	      var current_price = this.basePrice * this.usage_int_mul;
	      this.duration_ads_price = current_price;
	      
	      this.selected_usage = this.getCaption($('usage_int'));
	      this.selected_usage_value = this.getValue($('usage_int'));
	      this.selected_usage_section_caption = this.getSectionCaption($('usage'));
	      
	      $('usage_int').siblings().each(function(elm)
	      {
	        $(elm).removeClassName('section-row-selected');
	      });
	      $('usage_int').addClassName('section-row-selected');
	    }.bind(this));
	    
	    $('usage_int').observe('mouseover', function()
	    {
	      	$('usage_int').addClassName('section-row-hover');
	      	$('calc-desc').update($('usage_int').title);
	    });
	    
	    $('usage_int').observe('mouseout', function()
	    {
	      	$('usage_int').removeClassName('section-row-hover');
	    });
    }
    
    if($('usage_per'))
    {
	    $('usage_per').observe('click',function()
	    {
	      $('usage').insert({'after':$('duration_ads')});
	      this.removeNextSiblings($('usage'));
	      $('duration_ads').show();
	      this.duration_ads();
	      
	      var current_price = this.basePrice * this.usage_per_mul;
	      this.duration_ads_price = current_price;
	      
	      this.selected_usage = this.getCaption($('usage_per'));
	      this.selected_usage_value = this.getValue($('usage_per'));
	      this.selected_usage_section_caption = this.getSectionCaption($('usage'));
	      
	      $('usage_per').siblings().each(function(elm)
	      {
	        $(elm).removeClassName('section-row-selected');
	      });
	      $('usage_per').addClassName('section-row-selected');
	    }.bind(this));
	    
	    $('usage_per').observe('mouseover', function()
	    {
	      	$('usage_per').addClassName('section-row-hover');
	      	$('calc-desc').update($('usage_per').title);
	    });
	    
	    $('usage_per').observe('mouseout', function()
	    {
	      	$('usage_per').removeClassName('section-row-hover');
	    });
	}
     
  },
  media_ads: function()
  {
    
    this.observe_spec_row($('media_ads_news'),'media_ads','copies_ads','size_ads','media');
    this.observe_spec_row($('media_ads_mag'),'media_ads','copies_ads','size_ads','media');
    
    this.observe_spec_row($('media_ads_bro'),'media_ads','quantity_2','size_ads','media');
    this.observe_spec_row($('media_ads_bra'),'media_ads','quantity_2','size_ads','media');
    this.observe_spec_row($('media_ads_post'),'media_ads','quantity_2','size_ads','media');
    //this.observe_spec_row($('media_ads_dis'),'media_ads','quantity','size_ads','media');
    this.observe_spec_row($('media_ads_bil'),'media_ads','quantity','size_ads','media');  
    this.observe_spec_row($('media_ads_tel'),'media_ads','duration_ads','region','media');
    this.observe_spec_row($('media_ads_web'),'media_ads','position_web','duration_web','media');
    //this.observe_spec_row($('media_ads_ele'),'media_ads','quantity_web','duration_web','media');
    //this.observe_spec_row($('media_ads_pos'),'media_ads','quantity_pos','size_pos','media');  
    
    this.observe_spec_row($('media_ads_dir'),'media_ads','quantity','size_ads','media');
    this.observe_spec_row($('media_ads_out'),'media_ads','quantity','size_ads','media');
    this.observe_spec_row($('media_ads_tab'),'media_ads','quantity','size_ads','media');
    this.observe_spec_row($('media_ads_tra'),'media_ads','quantity','size_ads','media');
    
    this.observe_spec_row($('media_ads_trans'),'media_ads','quantity','size_ads','media');
    
    this.observe_spec_row($('media_ads_email'),'media_ads','quantity_web','duration_web','media');
    this.observe_spec_row($('media_ads_cd'),'media_ads','quantity_web','duration_web','media');
    
    
  },
  media_pubs: function()
  {
    this.observe_spec_row($('media_pubs_news'),'media_pubs','copies_pubs','size_pubs','media');
    this.observe_spec_row($('media_pubs_mag'),'media_pubs','copies_pubs','size_pubs','media');
    this.observe_spec_row($('media_pubs_rep'),'media_pubs','copies_pubs','size_pubs','media');
    this.observe_spec_row($('media_pubs_bro'),'media_pubs','copies_pubs','size_pubs','media');
    this.observe_spec_row($('media_pubs_bok'),'media_pubs','copies_pubs','size_pubs','media');
    //this.observe_spec_row($('media_pubs_web'),'media_pubs','insertion_pubs','size_pubs','media');
    //this.observe_spec_row($('media_pubs_web'),'media_pubs','position_web_2','duration_web','media');
    this.observe_spec_row($('media_pubs_web'),'media_pubs','duration_web','','media');
    
  },
  insertion_ads: function()
  {
    var section_id = "insertion_ads";
    var next_section_id = "size_ads";
    var next_next_section_id = "duration_ads";
    
    var section = "insertion";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
    
        
  },
  copies_ads: function()
  {
    var section_id = "copies_ads";
    var next_section_id = "size_ads";
    var next_next_section_id = "duration_ads";
    
    var section = "copies";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
    
        
  },
  copies_pubs: function()
  {
    var section_id = "copies_pubs";
    var next_section_id = "size_pubs";
    var next_next_section_id = "duration_ads";
    
    var section = "copies";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
    
        
  },
  insertion_pubs: function()
  {
    var section_id = "insertion_pubs";
    var next_section_id = "size_pubs";
    var next_next_section_id = "duration_pubs";
    var section = "insertion";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
    
        
  },
  
  duration_ads: function()
  {
    var section_id = "duration_ads";
    var next_section_id = "region";
    var next_next_section_id = "";
    var section = "duration";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
  },
  duration_pubs: function()
  {
    var section_id = "duration_pubs";
    var next_section_id = "region";
    var next_next_section_id = "";
    var section = "duration";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
  },
  duration_web: function()
  {
    var section_id = "duration_web";
    var next_section_id = "";
    var next_next_section_id = "";
    var section = "duration";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
  },
  size_ads: function()
  {
    var section_id = "size_ads";
    var next_section_id = "duration_ads";
    var next_next_section_id = "region";
    var section = "size";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
  },
  size_pubs: function()
  {
    var section_id = "size_pubs";
    var next_section_id = "duration_pubs";
    var next_next_section_id = "region";
    var section = "size";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
  },
  size_pos: function()
  {
    var section_id = "size_pos";
    var next_section_id = "position_pos";
    var next_next_section_id = "duration_ads";
    
    var section = "size";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
  },
  
  quantity: function()
  {
    var section_id = "quantity";
    var next_section_id = "size_ads";
    var next_next_section_id = "duration_ads";
    var section = "quantity";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
  },
  quantity_2: function()
  {
    var section_id = "quantity_2";
    var next_section_id = "size_ads";
    var next_next_section_id = "duration_ads";
    var section = "quantity";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
  },
  quantity_web: function()
  {
    var section_id = "quantity_web";
    var next_section_id = "duration_web";
    var next_next_section_id = "";
    var section = "quantity";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
  },
  quantity_pos: function()
  {
    var section_id = "quantity_pos";
    var next_section_id = "size_pos";
    var next_next_section_id = "position_pos";
    var section = "quantity";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
  },
  position_pos: function()
  {
    var section_id = "position_pos";
    var next_section_id = "duration_ads";
    var next_next_section_id = "region";
    var section = "position";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
  },
  position_web: function()
  {
    var section_id = "position_web";
    var next_section_id = "duration_web";
    var next_next_section_id = "";
    var section = "position";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
  },
  position_web_2: function()
  {
    var section_id = "position_web_2";
    var next_section_id = "duration_web";
    var next_next_section_id = "";
    var section = "position";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
  },
  region: function()
  {
    var section_id = "region";
    var next_section_id = "";
    var next_next_section_id = "";
    var section = "region";
    
    this.observe_seq_section(section_id,next_section_id,next_next_section_id,section);
  },
  getCaption: function(element)
  {
    return $(element).cleanWhitespace().select('.section-caption').first().innerHTML;
  },
  getSectionCaption: function(element)
  {
    return $(element).cleanWhitespace().select('.section-row-header').first().innerHTML;
    //return "test";
  },
  getValue: function(element)
  {
    return $(element).cleanWhitespace().select('.section-value').first().innerHTML;
  },
  formatCurrency: function(num)
  {
    num = num.toString().replace(/\$|\,/g,'');
    
    if(isNaN(num))
      num = "0";
    
    num = Math.round(num);
    
    sign = (num == (num = Math.abs(num)));
    
    num = Math.floor(num*100+0.50000000001);
    
    cents = num%100;
    
    num = Math.floor(num/100).toString();
    
    if(cents<10)
    
    cents = "0" + cents;
    
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    
    num = num.substring(0,num.length-(4*i+3))+','+
    
    num.substring(num.length-(4*i+3));
    
    return (num + '.' + cents);
    
    //return num;
  },
  formatCurrency_nosep: function(num)
  {
    num = num.toString().replace(/\$|\,/g,'');
    
    if(isNaN(num))
      num = "0";
    
    num = Math.round(num);
    
    sign = (num == (num = Math.abs(num)));
    
    num = Math.floor(num*100+0.50000000001);
    
    cents = num%100;
    
    num = Math.floor(num/100).toString();
    
    if(cents<10)
    
    cents = "0" + cents;
    
    return (num + '.' + cents);
    
    //return num;
  },
  getSelected: function()
  {
    var selected = "";
    
    
    
    if(this.selected_usage != "")
    {
      selected += this.getSelectedTemplate(this.selected_usage_section_caption,this.selected_usage);
    }
    
    if(this.selected_media != "")
    {
      selected += this.getSelectedTemplate(this.selected_media_section_caption,this.selected_media);
    }
    
        
    if(this.selected_quantity != "")
    {
      selected += this.getSelectedTemplate(this.selected_quantity_section_caption,this.selected_quantity);
    }
    
    if(this.selected_insertion != "")
    {
      selected += this.getSelectedTemplate(this.selected_insertion_section_caption,this.selected_insertion);
    }
    
    if(this.selected_copies != "")
    {
      selected += this.getSelectedTemplate(this.selected_copies_section_caption,this.selected_copies);
    }
    /*
    if(this.selected_circulation != "")
    {
      selected += this.getSelectedTemplate(this.selected_circulation_section_caption,this.selected_circulation);
    }
    */
    if(this.selected_duration != "")
    {
      selected += this.getSelectedTemplate(this.selected_duration_section_caption,this.selected_duration);
    }
    
    if(this.selected_size != "")
    {
      selected += this.getSelectedTemplate(this.selected_size_section_caption,this.selected_size);
    }
    
    if(this.selected_position != "")
    {
      selected += this.getSelectedTemplate(this.selected_position_section_caption,this.selected_position);
    }
    
    if(this.selected_region != "")
    {
      selected += this.getSelectedTemplate(this.selected_region_section_caption,this.selected_region);
    }
    
    selected += this.getSelectedTemplate("Exclusivity","Non-exclusive (Please <a target=\"_blank\" href=\"http://www.inmagine.com/contact.php\">contact us</a> for exclusivity of this image.)");
    
    return selected;
  },
  setValueIntoTextbox: function()
  {
    
    $('usage_value').value = this.selected_usage_value;
    $('media_value').value = this.selected_media_value;
    $('quantity_value').value = this.selected_quantity_value;
    $('copies_value').value = this.selected_copies_value;
    $('insertion_value').value = this.selected_insertion_value;
    $('duration_value').value = this.selected_duration_value;
    $('size_value').value = this.selected_size_value;
    $('position_value').value = this.selected_position_value;
    $('region_value').value = this.selected_region_value;
    
    
  },
  getSelectedTemplate: function(section,caption)
  {
    return "<div><b>"+section+":</b> "+caption+"</div>";
  },
  observe_seq_section: function(section_id,next_section_id,next_next_section_id,section)
  {
    var self = this;
    
    $(section_id).childElements().each(function(element)
    {
      if($(element).hasClassName('section-row')) 
      {
        self.observe_spec_row(element, section_id, next_section_id,next_next_section_id,section);
        
      }
    });
  },
  observe_spec_row: function(element,section_id,next_section_id,next_next_section_id,section)
  {
    var self = this;
    
    
    
    $(element).observe('click',function()
    {
      
      var multiplier = eval('self.'+element.id+'_mul');
      var price = eval('self.'+section_id+'_price');
        
      var current_price = self.calculate_price(price,multiplier);
      
      eval('self.selected_'+section+' = self.getCaption($(element))');
      eval('self.selected_'+section+'_value = self.getValue($(element))');
      eval('self.selected_'+section+'_section_caption = self.getSectionCaption($(section_id))');
      //selected_usage_section_caption
      
      if (next_section_id != "") 
      {
        $(section_id).insert(
        {
          'after': $(next_section_id)
        });
        self.removeNextSiblings($(section_id));
        $(next_section_id).show();
        eval('self.'+next_section_id+'()');
        
        eval('self.'+next_section_id+'_price  = current_price');
        
        if (next_next_section_id == "")
        {
          	
          
          $(next_section_id).childElements().each(function(elm)
          {
            if($(elm).hasClassName('section-row')) 
            {
              $(elm).setStyle(
              {
                width: '230px'
                
              });
              
              var end_section_price = self.calculate_price(eval('self.'+next_section_id+'_price'),eval('self.'+elm.id+'_mul'));
              var old_section_price = end_section_price;
              
              	if(end_section_price < self.min_total_price)
				{
					end_section_price = self.min_total_price;
				}
              
              $(elm.id + '_pointer').update("<span>"+self.currency + ' ' + self.formatCurrency(end_section_price)+"</span><img src='/calc/arrow.gif' style='border: 0px; vertical-align: middle;'>");
              //self.currency + ' ' + self.formatCurrency(old_section_price)+' '+
              //var price_end = "<span>"+self.currency + ' ' + self.formatCurrency(self.calculate_price(eval('self.'+next_section_id+'_price'),eval('self.'+elm.id+'_mul')))+"</span>";
              //$(elm.id + '_pointer').insert({'top':price_end});
            }
          });
        }
        
      }
      else
      {
        
        
        if(section_id == "region")
        {
          var country_count = eval('self.'+element.id+'_count');
          
          self.country_select_count = country_count;
          
          if(self.country_select_count == 'x')
          {
            $('calc-country-selection').hide();
          }
          else
          {
            $('calc-country-selection').show();
          }
          
        }
        else
        {
          $('calc-country-selection').hide();
        }
        
        if(current_price < self.min_total_price)
		{
			current_price = self.min_total_price;
		}
        
        
        $('calc-main').hide();
        $('calc-desc').hide();
        $('calc-desc-header').hide();
        $('calc-result').show();
        $('price').update(self.currency+' '+self.formatCurrency(current_price));
        $('price_value').value = self.formatCurrency_nosep(current_price);
        $('country-count').update(self.country_select_count);
        $$('.country_strict_option').each(function(element)
        {
          $(element).remove();
        });
        
        $('calc-selected-section').update(self.getSelected());
        self.setValueIntoTextbox();
		
      }
      
      $(element).siblings().each(function(elm)
      {
        $(elm).removeClassName('section-row-selected');
      });
      $(element).addClassName('section-row-selected');
      
      if($(section_id).id == "media_ads" || $(section_id).id == "media_ads")
      {
        $('calc-desc').update($(element).title);
      }
    });
    
    //$(element).addClassName('section-row-selected');
    
    $(element).observe('mouseover', function()
    {
        $(element).addClassName('section-row-hover');
      
    if($(section_id).id == "media_ads" || $(section_id).id == "media_pubs")
    {
      $('calc-desc').update($(element).title);
      
    }
    else
    {
      if($('media_ads').select('.section-row-selected').first())
      {
        $('calc-desc').update($('media_ads').select('.section-row-selected').first().title);
      }
      else if($('media_pubs').select('.section-row-selected').first())
      {
        $('calc-desc').update($('media_pubs').select('.section-row-selected').first().title);
      }
      
    }
    });
    
    $(element).observe('mouseout', function()
    {
      $(element).removeClassName('section-row-hover');
      if($(section_id).id == "media_ads" || $(section_id).id == "media_pubs")
    {
      $('calc-desc').update($(element).title);
    }
    else
    {
      if($('media_ads').select('.section-row-selected').first())
      {
        $('calc-desc').update($('media_ads').select('.section-row-selected').first().title);
      }
      else if($('media_pubs').select('.section-row-selected').first())
      {
        $('calc-desc').update($('media_pubs').select('.section-row-selected').first().title);
      }
      
    }
    });
    


  },
  removeNextSiblings: function(element)
  {
    var self = this;
    
    $(element).nextSiblings().each(function(elm)
    {
      if ($(elm).visible()) 
      {
        $(elm).childElements().each(function(elm2)
        {
          $(elm2).removeClassName('section-row-selected');
          Event.stopObserving($(elm2));
        });
        $(elm).hide();
        
        var tmp = $(elm).id;
        var tmp2 = tmp.split("_");
        var section = tmp2[0];
        
        eval('self.selected_' + section + ' = ""');
        eval('self.selected_' + section + '_value = ""');
        
        
      }
      
    });
  },
  calculate_price: function(price,multiply)
  {
    var current_price = price *  multiply;
    //console.log(price + 'x' + multiply + '=' + current_price);
    return current_price;
    
  },
  setCountryObserver: function()
  {
    
    var self = this;
    
    $("strict").observe("click",function()
    {
      self.toStrict();
    });
    $("allow").observe("click",function()
    {
      self.toAllow();
    });
    
    //$("save").observe("click",function()
    //{
      //saveRestrictedCountry();
    //});
  },
  toStrict: function()
  {
    //alert(this.country_select_count);
    
    var self = this;
    
    var current_country_count = $$('.country_strict_option').length;
    
    var html_option = "";
    
    $$('.country_allow_option').each(function(element)
    {
      if($(element).selected && current_country_count < self.country_select_count)
      {
        html_option = html_option + "<option class='country_strict_option' value="+$(element).value+">"+$(element).innerHTML+"</option>";
        $(element).remove();
        current_country_count++;
      }
    });
    
    //if (this.country_select_count != 'x') 
    //{
      if (html_option != "") {
        $("country_strict").insert({
          top: html_option
        });
      }
    //}
  },

  toAllow: function()
  {
    var html_option = "";
    $$('.country_strict_option').each(function(element)
    {
      if($(element).selected)
      {
        html_option = html_option + "<option class='country_allow_option' value="+$(element).value+">"+$(element).innerHTML+"</option>";
        $(element).remove();
      }
    });
    
    if(html_option != "")
    {
      $("country_allow").insert({ top: html_option });
    }
  }
  
    
});
