var stateProductInfo = {};

stateProductInfo.offers = [{
  states:['NY','NJ'],
  startingAt:true,
  price:"$59.99",
  description:"<strong>Speeds up to 7.1Mbps for qualifying customers for 12 months with a 3-year contract</strong> (plus taxes and fees).",
  offer:"",
  disclaimer:"Offer for new HSI customers in select areas of NY & NJ, subj to service agmt & credit approval. Rate may change after 1st year. Early termination fees: up to $149 for HSI and up to $125 for voice. $88 set up fee. $99.95 Modem Fee.  Eligibility restrictions, equip fees, taxes & other charges may apply. Actual speeds and service availability may vary. HSI will be provisioned at 5.0 Mbps or up to 7.1 Mbps based on VZ line qualification requirements. Verizon Wi-Fi available in designated locations only. Software limitations &amp; other terms apply. Visit <a href='http://business.verizon.net/wifi'>business.verizon.net/wifi</a> for details. Offer expires 1/16/2010."
  },
  

  {
  states:['NY'],
  price:"$79.99",
  description:"<strong>Speeds up to 3Mbps for qualifying customers for 12 months with a 3-year contract</strong> (plus taxes and fees).",
  offer:"",
  disclaimer:"Subj to service agmt & credit approval. Rate may change after 1st year. Early termination fees: up to $149 for HSI and up to $125 for voice. $88 set up fee. $99.95 Modem Fee.  Eligibility restrictions, equip fees, taxes & other charges may apply. Actual speeds and service availability may vary. HSI will be provisioned at 3 Mbps or up to 1.5 Mbps based on VZ line qualification requirements.  Rebate: instructions mailed after service activation; online redemption reqd. Verizon Wi-Fi available in designated locations only. Software limitations &amp; other terms apply. Visit <a href='http://business.verizon.net/wifi'>business.verizon.net/wifi</a> for details. Offer expires 1/16/2010."
  },
  

  {
  states:['NY','NJ'],
  price:"$79.99",
  description:"<strong>Speeds up to 7.1Mbps for qualifying customers for 12 months with a 3-year contract</strong> (plus taxes and fees).",
  offer:"",
  disclaimer:"Subj to service agmt & credit approval. Rate may change after 1st year. Early termination fees: up to $149 for HSI and up to $125 for voice. $88 set up fee. $99.95 Modem Fee. Eligibility restrictions, equip fees, taxes & other charges may apply. Actual speeds and service availability may vary. HSI will be provisioned at 5.0 Mbps or up to 7.1 Mbps based on VZ line qualification requirements. Verizon Wi-Fi available in designated locations only. Software limitations &amp; other terms apply. Visit <a href='http://business.verizon.net/wifi'>business.verizon.net/wifi</a> for details. Offer expires 1/16/2010."
  },
  
 
  {
  states:['RI','NC','ID'],
  price:"$79.99",
  description:"<strong>Speeds up to 3Mbps for qualifying customers for 12 months with a 3-year contract</strong> (plus taxes and fees).",
  offer:"",
  disclaimer:"Subj to service agmt & credit approval. Rate may change after 1st year. Early termination fees: up to $149 for HSI and up to $125 for voice. $88 set up fee. $99.95 Modem Fee. Eligibility restrictions, equip fees, taxes & other charges may apply. Actual speeds and service availability may vary. HSI will be provisioned at 3 Mbps or up to 1.5 Mbps based on VZ line qualification requirements.  Verizon Wi-Fi available in designated locations only. Software limitations &amp; other terms apply. Visit <a href='http://business.verizon.net/wifi'>business.verizon.net/wifi</a> for details. Offer expires 1/16/2010. "
  },
  
  {
  states:['IL', 'MI', 'OH', 'TX', 'WI', 'SC', 'CA', 'MI', 'IN', 'FL', 'OR', 'WA','PA', 'DE', 'MA', 'MD', 'DC', 'VA', 'WV'],
  price:"$79.99",
  description:"<strong>Speeds up to 7.1Mbps for qualifying customers for 12 months with a 3-year contract</strong> (plus taxes and fees).",
  offer:"",
  disclaimer:"Subj to service agmt & credit approval. Rate may change after 1st year. Early termination fees: up to $149 for HSI and up to $125 for voice. $88 set up fee. $99.95 Modem Fee. Eligibility restrictions, equip fees, taxes & other charges may apply. Actual speeds and service availability may vary. HSI will be provisioned at 5.0 Mbps or up to 7.1 Mbps based on VZ line qualification requirements. Verizon Wi-Fi available in designated locations only. Software limitations &amp; other terms apply. Visit <a href='http://business.verizon.net/wifi'>business.verizon.net/wifi</a> for details. Offer expires 1/16/2010."
  },
  {
  states:['CT'],
  price:"",
  description:"Thank you for your interest in High Speed Internet and Phone Service from Verizon. Please call us and one of our Small Business Specialists will provide you with more information on our product offerings in your area.",
  offer:"",
  disclaimer:""
  },
  {
  states:[],
  price:"",
  description:"Thank you for your interest in High Speed Internet and Phone Service from Verizon. Unfortunately, Verizon does not provide local phone service in your area.  For wireless business services offered by Verizon Wireless, please visit <a href='http://www.verizonwireless.com/b2c/index.html?tab=business&lid=//global//Business'>Verizon Wireless for Small Business</a>.",
  offer:"",
  disclaimer:"",
  execute:function(){lpSendData("page", "ErrorName", "No service in this area.");}
  }
];

stateProductInfo.specialAreaCodes = {
  NY:{
    specialCodes:[[[914,845,516,631,718,347],0],[[716,585],1]],
    defaultIndex:2
  },
  NJ:{
    specialCodes:[[[201,732,973,908,609],0]],
    defaultIndex:2
  }
};

var PricingModule = new Class({
  currentState:null,
  initialize:function(wrapper) {
    this.wrapper = wrapper;
    this.header = this.wrapper.getElement('h2');
    this.makeRequest = this.wrapper.getElement('#make_request');
    this.areaCodeWrapper = this.wrapper.getElement('.pricing_area_code');
    this.areaCodeRequest = this.wrapper.getElement('#area_code_request');
    this.areaCode = this.wrapper.getElement('#area_code');
    this.newState = this.wrapper.getElement('#new_state');
    this.state_select = this.wrapper.getElement('#state');
    this.state_form = this.wrapper.getElement('#state_form');
    this.state_name = this.wrapper.getElement('#state_name');
    this.results = this.wrapper.getElement('#results');
    this.loader = this.wrapper.getElement('#loader');
    this.priceWrapper = this.wrapper.getElement('#price');
    this.price = this.wrapper.getElement('#price').getElement('span');
    this.disclaimer = $('disclaimer_extra');
    this.description = this.wrapper.getElement('#description');
    //this.offer = this.wrapper.getElement('#offer');
    this.startingAt = this.wrapper.getElement('#starting_at');
    window.pricing_redirect = null;
    
    this.cookied_state = CookieStuff.getCookieState('vzapps','STATE');
  	  	
  	this.state_form_fx = new Fx.Morph(this.state_form).set('opacity',0);		
  	this.result_fx = new Fx.Morph(this.results).set('opacity',0);
  	this.loader_fx = new Fx.Morph(this.loader).set('opacity',0);
  	
  	this.bindClickEvents();
  	
  	this.state_form_fx.set({'display':'none','opacity':'0'});
  	this.result_fx.set({'display':'none','opacity':'0'});
  	this.loader_fx.set({'display':'none','opacity':'0'});	
  	
  	
  	var state = CookieStuff.getCookieState('vzapps','STATE'),
  	    area_code = CookieStuff.getCookieState('vzapps','AREACODE');
  	    
  	this.state_select.setProperty('value',state);
  	this.areaCode.setProperty('value',area_code);

  	if (state == null || (window.pricing_redirect !== null && window.pricing_redirect !== '')) {
  		this.state_form_fx.start({'display':'block','opacity':'1'});
  	}	else {
		  this.currentState = state;
		  if (this.needsAreaCode(state) && $chk(area_code)) {
		    this.lookup(true,null);
		  } else {
		    this.lookup(null,null);  
		  }
  		
  	}
  },
  bindClickEvents:function() {
    var me = this;
    this.makeRequest.addEvent('click',function() {
      me.lookup(false,window.pricing_redirect);
    });
    this.areaCodeRequest.addEvent('click',function() {
      me.lookup(true,window.pricing_redirect);
    });
    
    this.newState.addEvent('click', function() {
  		me.renderForm();
  	});
  },
  lookup:function(useAreaCode,redirect) {
    var no_state = this.state_select.getElement('#choose_state'),
        rdr = redirect;
		if (no_state.selected){
			alert("Please select a state.");
			no_state.focus();
		}	else {
			var state = this.state_select.getProperty('value'),
			    ac = this.areaCode.getProperty('value');
			    
			this.currentState = state;
			
			CookieStuff.setVZAppsCookie(state,ac);
			getLocInfo();
			
			if (this.needsAreaCode(state) && this.areaCodeWrapper.hasClass('hidden') && !useAreaCode) {
			  this.renderForm(); 
			  this.showAreaCode();
			  return this;
			} else if (rdr !== null && rdr !== '') {
			  document.location = rdr;
			} else if (useAreaCode) {
			 this.renderOfferInfo(this.getOfferInfoAreaCode(this.currentState,ac)); 
			} else {
			 this.renderOfferInfo(this.getOfferInfo(state)); 
			}
		}
  },
  renderSearching:function() {
    this.state_form_fx.set({'display':'none','opacity':'0'});
		this.loader_fx.start({'display':'block','opacity':'1'});
		this.header.removeClass('check-large');
		this.header.setStyle('width','auto');
		this.header.set('text','Searching for pricing information...');
  },
  renderForm:function(keepAreaCode) {
    this.removeSearching();
    this.removeOfferInfo();
  	if (!keepAreaCode) {
  		this.hideAreaCode();
  	}
    
    this.state_form_fx.set({'display':'block','opacity':'1'});
    this.header.removeClass('check-large');
  	this.header.setStyle('width','auto');
  	this.header.set('text','Pricing Information');
  },
  removeSearching:function() {
    var me = this;
    setTimeout(function() {
  		me.loader_fx.start({'display':'none','opacity':'0'});
    },1000);
  },
  renderOfferInfo:function(obj) {
     var me = this; 
     if ($chk(obj)) {
       
      if ($chk(obj.price)) {
        this.showPrice();
        var prices = obj.price.split('.');
        var priceHtml = prices[0]+"."+prices[1]; 
        this.price.set('html',priceHtml);  
      } else {
        this.hidePrice();
      }
    
      var re = /(#{order_tele})/;
      
      var desc = obj.description.replace(re,window.orderTeleNumber);
      this.description.set('html',desc);
      
      if ($chk(obj.offer)) {
        this.showOffer();
        //this.offer.set('html',obj.offer);  
      } else {
        this.hideOffer();
      }
            
      this.setStateName(this.currentState);
      this.removeSearching();
      if ($chk(obj.startingAt)) {
       this.renderStartingAt(); 
      } else {
       this.removeStartingAt();
      } 
      setTimeout(function() {
        if (me.disclaimer) {
          if ($chk(obj.disclaimer)) {
            me.disclaimer.set('html',obj.disclaimer);  
          } else {
            me.disclaimer.set('html','');
          }  
        }
        
        me.header.set('text','Pricing Information');
        me.result_fx.start({'display':'block','opacity':'1'});
      },1000); 
     }
	 if($chk(obj.execute)) {
		obj.execute(); 
	 }
  },
  removeOfferInfo:function() {
    this.result_fx.set({'display':'none','opacity':'0'});
  },
  hidePrice:function() {
    this.priceWrapper.setStyle('display','none');
  },
  showPrice:function() {
    this.priceWrapper.setStyle('display','block');
  },
  hideOffer:function() {
    //this.offer.setStyle('display','none');
  },
  showOffer:function() {
    //this.offer.setStyle('display','block');
  },
  showAreaCode:function() {
	$('area_code').set('value', '');
    this.areaCodeWrapper.removeClass('hidden');
  },
  hideAreaCode:function() {
    this.areaCodeWrapper.addClass('hidden');
  },
  renderStartingAt:function() {
    this.startingAt.setStyle('visibility','visible');
  },
  removeStartingAt:function() {
    this.startingAt.setStyle('visibility','hidden');
  },
  needsAreaCode:function(state) {
    var spi = stateProductInfo;
    
    for (var s in spi.specialAreaCodes) {
      if (s === state) {
        return true;
      }
    }
    return false;
  },
  getOfferInfo:function(state,index) {
    var spi = stateProductInfo;

    if ($chk(index)) {
      
     this.renderSearching();
     return spi.offers[index];   
    } else {
      for (var s in spi.specialAreaCodes) {
        if (s === state) {
	        this.renderForm(); 
          this.showAreaCode(); 
          return;
        }
      }
      
      this.renderSearching();
      
      for (var i=0,len=spi.offers.length;i<len;i++) {
        if ($chk(spi.offers[i].states) && spi.offers[i].states.contains(state)) {
         return spi.offers[i]; 
        }
      }
    }
    //the default(last one)
    return spi.offers[spi.offers.length - 1];  
    
  },
  getOfferInfoAreaCode:function(state,area_code) {
    var obj = stateProductInfo.specialAreaCodes;
    area_code = area_code.toInt();
    
    for (var ac in obj) {
      if (ac === state) {
        var codes = obj[ac].specialCodes;
        
        for (var i=0,len=codes.length;i<len;i++) {
          if (codes[i][0].contains(area_code)) {
            return this.getOfferInfo(state,codes[i][1]);
          } 
        }
        return this.getOfferInfo(state,obj[ac].defaultIndex);
      }
    }
    return false;
  },
  getFullState:function(abbr) {
    for (var i=0;i<arrStateAbbrev.length;i++) {
			if (abbr == arrStateAbbrev[i]) {
				return arrStateFull[i];
			}		
		}	
  },
  setStateName:function(state) {
    this.state_name.set('text',this.getFullState(state));
  }
  
});


window.addEvent('domready', function() {
	var module = $('pricing-module');
	if (module) {
		var pm = new PricingModule($('pricing-module'));
	}

	var pg_head = $$('.pg_head')[0];
	var pg_head_h1 = pg_head.getElement('h1');
	
	if (pg_head_h1) {
		var loc = location.href;
		var email_name = pg_head_h1.innerHTML;
		var email_alt = $('email-alt');
		if (email_alt) {
			email_alt.href='/email.aspx?title='+email_name+'&path='+ loc;  
		}
	}
});