$jQ = jQuery.noConflict();
	// Use jQuery via $jQ(...)
	$jQ(document).ready(function()
	{
		$jQ('#cal-form').click(function(){
		$jQ(".delivery-badge").hide();
		$jQ("#calculator").hide();
		var email_amount = deleteComma($jQ("#email_send").val());
		planType(email_amount);
		});
	//exapnd and collapse the SEO footer
	//JS code for SEO at the bottom of layout template to expand and collapse
	$jQ('#link-details').click(function(){
		$jQ('#seo-extra-text').toggle();
        if ($('#link-details').html() == 'collapse &gt;&gt;')
        {
          $('#link-details').html('read more >>');
        }else{
        	$jQ('#link-details').html('collapse >>');  
        }
      });
	$jQ('#sign-in').click(function(){
        var myopts = {
          title: 'User Authentication',
          bgiframe: true,
          bgiframe: true,
          modal: true,
          width: 410,
          position: 'center'
        };
        
        // get the ajax login form:
        $jQ.get('/login', function(data){            
          dialog_window = document.createElement("div");            
          dialog_window.id = '_login_ajax';
          $jQ(document.body).append(dialog_window);
          $jQ('#_login_ajax').html(data);
          $jQ('#_login_ajax').dialog(myopts);    
        });          
        
        return false;
      });
	//this is where we set up the contact sales modal window via AJAX
	$jQ('.contact-sales-link').click(function(){
        var myopts = {
          title: 'Contact Sales',
          bgiframe: true,
          modal: true,
          width: 610,
          position: 'center'
        };
        
        // get the iframe form:          
        $jQ.get('contact-sales.html', function(data){            
          dialog_window = document.createElement("div");            
          dialog_window.id = '_contact_sales';
          $jQ(document.body).append(dialog_window);
          $jQ('#_contact_sales').html(data);
          $jQ('#_contact_sales').dialog(myopts);            
        });          
      });
	//this function sets the plan type based on page visit and the value of the pricing calculator		
	function planType(email_amount)
	{
		var dedip = $jQ("#dedicated_ip").val();
		//this is where define the plan vars
		var planObj = {"plans":[
		{"plan":"bronze","credits":"40000", "overage":"0.001", "dip":"0", "price":"9.95", "colum":"col4", "width_th":"186px", "plan_id":"78"},
		{"plan":"silver","credits":"100000", "overage":"0.00085", "dip":"1", "price":"79.95", "colum":"col3", "width_th":"190px", "plan_id":"11"},
		{"plan":"gold","credits":"300000", "overage":"0.00050", "dip":"1", "price":"199.95", "colum":"col2", "width_th":"190px", "plan_id":"12"},
		{"plan":"platinum","credits":"700000", "overage":"0.00045", "dip":"1", "price":"399.95", "colum":"col1", "width_th":"186px", "plan_id":"44"},
		{"plan":"hv","credits":"1700000", "overage":"0.00045", "dip":"1", "price":"399.95", "colum":"col1", "width_th":"182px", "plan_id":"1"}
		]};
		var setPlan = 2;
		var ded_ip = $('#dedicated_ip').attr('checked');
		var subs = $('#subuser').attr('checked');
		if (email_amount <= 99999 && (ded_ip) || (subs)) 
		{ 
			setPlan = 1;
		} else if (email_amount <= 99999 && !(ded_ip) || !(subs))
		{
			setPlan = 0;
		}
		if (email_amount >= 99999 && email_amount <= 241000) 
		{ 
			setPlan = 1;
		}
		if (email_amount >= 242000 && email_amount <= 699000) 
		{ 
			setPlan = 2;
		}
		if (email_amount >= 651000 && email_amount <= 1600000) 
		{ 
			setPlan = 3;
		}
		if (email_amount >= 1600001) 
		{ 
			setPlan = 4;
		}
		if (setPlan == 4)
		{
			$jQ("#high-volume").show();
		}else
		{
			$jQ("#high-volume").hide();
		}
		if ($jQ.cookie("extole_details"))
		{
			$jQ("#notification-windows").html('<div class="extole-notice"><img src="/images/pricing/check.png" /> You are entitled to a 25% discount on ANY package you signup with! New customer only.</div><div class="extole-notice"><img src="/images/pricing/x.png" /> Discounted pricing applies only to first 3 months of service. regular pricing will apply thereafter.</div>');
		}
		var x = setPlan;
		var elements = new Array();
			elements[1] = '0';
			elements[2] = '1';
			elements[3] = '2';
			elements[4] = '3';
			elements[5] = '4';
		if ((email_amount - planObj.plans[setPlan].credits) <= 0 || setPlan == 4 ) 
		{
			var over_amount = '';
			var total_amount = '';
			var total_format = '';
			var amount_format = '';
		} else if(email_amount >= planObj.plans[setPlan].credits && email_amount <= 999999)
		{ 
			over_amount = (Number(email_amount) - Number(planObj.plans[setPlan].credits)) * Number(planObj.plans[setPlan].overage);
			total_amount = Number(over_amount)+Number(planObj.plans[setPlan].price);
			total_format = total_amount.toFixed(2);
			
			if ($jQ.cookie("extole_details"))
			{
				discount = Number(planObj.plans[setPlan].price) * Number('.25');
				discount_price = Number(planObj.plans[setPlan].price) - Number(discount);
				total_amount_discount = Number(over_amount) + discount_price;
				amount_format = '<span class="price-overage">Overage Charge: $'+over_amount.toFixed(2)+'<br />Monthly Charge: $'+discount_price.toFixed(2)+'<br /> Total Amount: $'+total_amount_discount.toFixed(2)+'</span><br />';
			}
			else
			{
				amount_format = '<span class="price-overage">Overage Charge: $'+over_amount.toFixed(2)+'<br />Monthly Charge: $'+planObj.plans[setPlan].price+'<br /> Total Amount: $'+total_amount.toFixed(2)+'</span><br />';
			}
			
			//amount_format = '<span class="price-overage">Overage Charge: $'+over_amount.toFixed(2)+'<br />Monthly Charge: $'+planObj.plans[setPlan].price+'<br /> Total Amount: $'+total_amount.toFixed(2)+'</span><br />';
			
		} else if (email_amount > 999999) 
		{
			over_amount = (Number(email_amount) - Number(planObj.plans[setPlan].credits)) * Number(planObj.plans[setPlan].overage);
			total_amount = Number(over_amount)+Number(planObj.plans[setPlan].price);
			total_format = total_amount.toFixed(2);
			total_amount_discount = Number(over_amount)+Number(planObj.plans[setPlan].price) * Number('.25');
			
			if ($jQ.cookie("extole_details"))
			{
				discount = Number(planObj.plans[setPlan].price) * Number('.25');
				discount_price = Number(planObj.plans[setPlan].price) - Number(discount);
				total_amount_discount = Number(over_amount) + discount_price;
				amount_format = '<span class="price-overage">Overage Charge: $'+over_amount.toFixed(2)+'<br />Monthly Charge: $'+discount_price.toFixed(2)+'<br /> Total Amount: $'+total_amount_discount.toFixed(2)+'</span><br />';
			}
			else
			{
				amount_format = '<span class="price-overage">Overage Charge: $'+over_amount.toFixed(2)+'<br />Monthly Charge: $'+planObj.plans[setPlan].price+'<br /> Total Amount: $'+total_amount.toFixed(2)+'</span><br />';
			}
			
		}
		
		for (x in elements) 
		{
			$jQ(".section-bg." + planObj.plans[setPlan].colum).addClass("recommendedEven");
			$jQ(".section-bg .align-center." + planObj.plans[setPlan].colum).addClass("recommendedEven");
			$jQ(".align-center." + planObj.plans[setPlan].colum).addClass("recommendedOdd");
			$jQ("." + planObj.plans[setPlan].colum + ".signUpTd").addClass("recommendedOdd recommendedBtm");
			$jQ("#" + planObj.plans[setPlan].plan + "-th").addClass("recommended");
			$jQ("#" + planObj.plans[setPlan].plan + "-th").css("width", planObj.plans[setPlan].width_th);
			$jQ("#" + planObj.plans[setPlan].plan + "-th").css("height", "162px");
			$jQ("#" + planObj.plans[setPlan].plan + "-th").html('<span class="recylw" style="margin-top:2px;">Recommended</span>' + amount_format + '<a title="' + planObj.plans[setPlan].plan + '" http://sendgrid.com/user/signup/package/'+planObj.plans[setPlan].plan_id+'">' + planObj.plans[setPlan].plan + '</a><div class="signUpR" style="height:30px;margin:-5px;"><a title="Sign Up" href="http://sendgrid.com/user/signup/package/'+planObj.plans[setPlan].plan_id+'">Sign Up</a></div>');
			$jQ("#" + planObj.plans[setPlan].plan + "-bottom").addClass("signUpR");
			if ($jQ.cookie("extole_details"))
			{
				discount = Number(planObj.plans[setPlan].price) * Number('.25');
				discount_price = Number(planObj.plans[setPlan].price) - Number(discount);
				$jQ("#"+planObj.plans[setPlan].plan).html('<span class="price-red"><strike>$'+planObj.plans[setPlan].price+'</strike></span><br /><span class="price-discount">$'+discount_price.toFixed(2)+'</span>');
			}
			else
			{
				$jQ("#"+planObj.plans[setPlan].plan).html('<span class="price">$'+planObj.plans[setPlan].price+'</span>');
			}
		}
	
		var i=0;
		for (i=1;i<=4;i++)
		{
			if (elements[i] != setPlan)
			{
				$jQ("#" + planObj.plans[elements[i]].plan + "-bottom").removeClass("signUpR");
				$jQ(".section-bg."+planObj.plans[elements[i]].colum).removeClass("recommendedEven");
				$jQ(".section-bg."+planObj.plans[elements[i]].colum).removeClass("recommendedOdd");
				$jQ("."+planObj.plans[elements[i]].colum+".signUpTd").removeClass("recommendedOdd recommendedBtm");
				$jQ(".align-center."+planObj.plans[elements[i]].colum).removeClass("recommendedOdd");
				$jQ("#"+planObj.plans[elements[i]].plan+"-th").removeClass("recommended");
				$jQ("#"+planObj.plans[elements[i]].plan+"-th").css("width", "140px");
				
				if ($jQ.cookie("extole_details"))
				{
					discount = Number(planObj.plans[elements[i]].price) * Number('.25');
					discount_price = Number(planObj.plans[elements[i]].price) - Number(discount);
					$jQ("#"+planObj.plans[elements[i]].plan).html('<span class="price-red"><strike>$'+planObj.plans[elements[i]].price+'</strike></span><br /><span class="price-discount">$'+discount_price.toFixed(2)+'</span>');
				}
				else
				{
					$jQ("#"+planObj.plans[elements[i]].plan).html('<span class="price">$'+planObj.plans[elements[i]].price+'</span>');
				}
				
				$jQ("#"+planObj.plans[elements[i]].plan+"-th").html(planObj.plans[elements[i]].plan+'<div class="signUp"><span class="signup-text-rs"><a title="Sign Up" href="http://sendgrid.com/user/signup/package/'+planObj.plans[elements[i]].plan_id+'">Sign Up</a></span></div>');
			}
		}
			  
		if (setPlan == 4)
		{
			$jQ("#bronze-th").css("width", "145px");
			$jQ("#lite-th").css("width", "142px");
		}
	}
	//set default plan to silver on inital visit
	planType('100000');
	//this function replaces Commas from numbers in calculator
	function deleteComma(MyString)
	{
		return MyString.replace(/\,/g,'');
	}
	//this is for the video on the features page
	$jQ('#video-link').click(function()
	{
		$jQ('#sendgrid-video').html('<iframe src="http://player.vimeo.com/video/23283604?&api=1&id=vimeoplayer&autoplay=1" width="500" height="281" frameborder="0"></iframe>');
		$jQ('#sendgrid-video').show();
		var myopts = {
			title: 'SendGrid Tour Video',
			bgiframe: true,
			modal: true,
			width: 540,
			position: 'center',
			close: function(){$('#sendgrid-video').html('close video');}
		};
	$('#features-video').dialog(myopts);
		return false;
	});
	//this is the ending tag for the document.ready function
	});
	
	//toggel the plus and minus buttons
	 function tooglePoints(a,b){
		 $jQ(a).toggleClass('minus-pos-new');
		 $jQ(b).toggle();
	}
	
   //hide the play image and show the iframe video
     function toggleDivs() 
     {
       $jQ('#sendgrid-box').hide();
       $jQ('#sendgrid-video').show();
     } 
     //check for cookie. If not present set if siteID is present in string.
     function siteIDCookie()
     {
    	var affString = window.location.href;    	 		
		var text = affString;
		var regex = /siteID=([A-Za-z0-9-]+)/;
		match = regex.exec(text);
		//check that there is a publisher ID and that a cookie has already been set. If a pmgPublisher cookie is not found set one with the value of the publisher ID from the query string
    	if (match != null && !$jQ.cookie("pmgSiteID")) 
    	{
    		$jQ.cookie("pmgSiteID", match[1], { expires: 45 });
    		//$jQ('#affForm').html('<iframe src="http://go.sendgrid.com/rs/sendgrid/images/newSignup.html?publisher="'+$jQ.cookie("pmgPublisher");+'width="530" height="1450" frameborder="0" scrolling="no"></iframe>');
    	}
    	
     }
     //this is where we set the join form iframe. If a cookie is already present or if a publisher ID is present in the query string the value will be appended to the publisher value.
     function setFormUrl()
     {
    	 siteIDCookie();
    	 $jQ('#affForm').html('<iframe src="http://sendgrid.com/user/signup/aff/1/siteID/'+$jQ.cookie("pmgSiteID")+'" width="530" height="500" frameborder="0" scrolling="no"></iframe>');
   
     }
     //this function returns the current year
     function getCurrentYear(){
         var currentTime = new Date()
         var year = currentTime.getFullYear()
         document.write(year)
       }
     
  // code for the tabs on the one page legal document
		$jQ(function() {
			$jQ( "#tabs" ).tabs();
		  });
