var currentUrl = document.location.href;
var errorMessages = new Array();

//Browser detect.  Fix top nav
$(document).ready(function() {
	if(navigator.userAgent.indexOf('Mac') > 0) {
		if(navigator.userAgent.indexOf("Safari") > -1) {
			$("#topNav").removeClass("topNav").addClass("topNavSafari");
		}else if(navigator.userAgent.indexOf('Firefox') > -1) {
			$("#topNav").removeClass("topNav").addClass("topNavMozilla");
		}
	}
});

//Draw backgrounds for boxes
$(document).ready(function()
{
	$(".box").backgroundCanvas();
	$(".boxClear").backgroundCanvas();
	$(".boxClearNoFloat").backgroundCanvas();
	$(".boxClearNoPad").backgroundCanvas();
	$(".boxNoPad").backgroundCanvas();
});

function DrawBackground() {
	$(".box").backgroundCanvasPaint(TestBackgroundPaintFkt);
	$(".boxClear").backgroundCanvasPaint(clearCanvas);
	$(".boxClearNoFloat").backgroundCanvasPaint(clearCanvas);
	$(".boxClearNoPad").backgroundCanvasPaint(clearCanvas);
	$(".boxNoPad").backgroundCanvasPaint(TestBackgroundPaintFkt);
}
// Draw the background on load and resize
var drawBackground = false;
$(window).load(function() {
	drawBackground = true;
	window.setTimeout(function() {
		if (drawBackground)
		{
		drawBackground = false;
		DrawBackground();
		}
	}, 50);
});

$(window).resize(function() {
	drawBackground = true;
	window.setTimeout(function() {
		if (drawBackground)
		{
			drawBackground = false;
			DrawBackground();
		}
	}, 50);
});

function TestBackgroundPaintFkt(context, width, height, elementInfo)
{
var options = {x:0, height: height, width: width,
radius:5, border: 0 };
// Draw the red border rectangle
context.fillStyle = "#b3b3b3";
$.canvasPaint.roundedRect(context,options);
// Draw the gradient filled inner rectangle
var backgroundGradient = context.createLinearGradient(0, 0, 0, height - 10);
backgroundGradient.addColorStop(0 ,'#fff');
backgroundGradient.addColorStop(1, '#ebebeb');
options.border = 1;
context.fillStyle = backgroundGradient;
$.canvasPaint.roundedRect(context,options);
}

function clearCanvas(context, width, height, elementInfo)
{
var options = {x:0, height: height - 1, width: width,
radius:5, border: 0 };
// Draw the red border rectangle
context.fillStyle = "#b3b3b3";
$.canvasPaint.roundedRect(context,options);
// Draw the gradient filled inner rectangle
var backgroundGradient = context.createLinearGradient(0, 0, 0, 0);
backgroundGradient.addColorStop(0 ,'#fff');
backgroundGradient.addColorStop(0, '#fff');
options.border = 1;
context.fillStyle = backgroundGradient;
$.canvasPaint.roundedRect(context,options);
}

//prevent double submits
$(document).ready(function(){
   $(".submitButton").attr("disabled", false);
 
   $("form").submit(function(){
     $(".submitButton").attr("disabled", true);
     return true;
   })
 });

//automatic tabs
$(document).ready(function(){
	$('#phoneAreaCode').autotab({ target: 'phonePrefix', format: 'number' });
	$('#phonePrefix').autotab({ target: 'phoneSuffix', format: 'number' });
	$('#phoneSuffix').autotab({ target: 'zipCode', format: 'number' });
	$('#accountNumberCorp').autotab_magic().autotab({ target: 'accountNumberHouse', format: 'number' });
	$('#accountNumberHouse').autotab_magic().autotab({ target: 'accountNumberCust', format: 'number' });
	$('#accountNumberCust').autotab({ target: 'firstName', format: 'number' });
	$('.phoneEmail,#phoneExt,#phoneSuffix').autotab({ target: 'email', format: 'number' });
	$('.phoneExt').autotab({ target: 'firstName', format: 'number' });
	$('#zipCode').autotab({ format: 'number'});
	 //$('#username').autotab({ format: 'alphanumeric', target: 'password' });
	 //$('#password').autotab({ previous: 'username', target: 'confirm' });
 });


//Show/hide fields on first registration page
$(document).ready(function()
{
	if ($('#createOptimumId1:checked').val() === "false") {
		$("#createId").hide();
		$("#existingId").show();
	}

	if ($('#createOptimumId2:checked').val() === "true") {
		$("#createId").show();
		$("#existingId").hide();
	}

	$("#createOptimumId1").click(function() {
		$("#createId").hide();
		$("#existingId").show();
	});

	$("#createOptimumId2").click(function() {
		$("#createId").show();
		$("#existingId").hide();
	});

	$(".myovLink").click(function() {
		fullWin(this.href);
		parent.Shadowbox.close();
		return false
	});
});

//Top Nav rules



$(document).ready(function()
{

	$('#topNavInternet,#topNavTelevision,#topNavPhone').mouseover(function() {
		$(this).css("background-image", "url(/images/header/nav_on.gif)"); 
		$(this).css("cursor","pointer");
		});

	$('#topNavBenefits').mouseover(function() {
		$(this).css("background-image", "url(/images/header/nav_end_on.gif)"); 
		$(this).css("cursor","pointer");
		});
	$('#topNavInternet,#topNavTelevision,#topNavPhone')
		.mouseout(function() {
		$(this).css("background-image", "url(/images/header/nav_off.gif)");
		});
	$('#topNavBenefits')
		.mouseout(function() {
		$(this).css("background-image", "url(/images/header/nav_end_off.gif)");
		});

	$('#topNavInternet').mouseover(function() {
	  $('#snOOL').show();
	});	

	$('#topNavInternet').mouseout(function() {
	  $('#snOOL').hide();
	});

	$('#topNavInternet').click(function() {
		s.linkTrackVars = 'prop2';
		s.prop2 = 'Internet';
		s.tl();
		document.location.href = '/high-speed-internet';
	});

	$('#topNavTelevision').mouseover(function() {
	  $('#sniOTV').show();
	});

	$('#topNavTelevision').mouseout(function() {
	  $('#sniOTV').hide();
	});

	$('#topNavTelevision').click(function() {
		s.linkTrackVars = 'prop2';
		s.prop2 = 'Television';
		s.tl();
	  document.location.href = '/digital-cable-tv';
	});

	$('#topNavPhone').mouseover(function() {
	  $('#snOV').show();
	});

	$('#topNavPhone').mouseout(function() {
	  $('#snOV').hide();
	});

	$('#topNavPhone').click(function() {
		s.linkTrackVars = 'prop2';
		s.prop2 = 'Phone';
		s.tl();
	  document.location.href = '/business-phone';
	});

	$('#topNavBenefits').mouseover(function() {
	  $('#snBenefits').show();
	});

	$('#topNavBenefits').mouseout(function() {
	  $('#snBenefits').hide();
	});

	$('#topNavBenefits').click(function() {
		s.linkTrackVars = 'prop2';
		s.prop2 = 'Benefits';
		s.tl();
	  document.location.href = '/small-business-benefits';
	});

	if (currentUrl.indexOf("/digital-cable-tv") != -1) {
		$("#topNavTelevision").className = "mainOn";
	} else {
		$("#topNavTelevision").className = "main";
	}

	if (currentUrl.indexOf("/small-business-benefits") != -1) {
		$("#topNavBenefits").className = "mainLastOn";
	} else {
		$("#topNavBenefits").className = "mainLast";
	}

	if (currentUrl.indexOf("/business-phone") != -1) {
		$("#topNavPhone").className = "mainOn";
	} else {
		$("#topNavPhone").className = "main";
	}

	if (currentUrl.indexOf("/high-speed-internet") != -1) {
		$("#topNavInternet").className = "mainOn";
	} else {
		$("#topNavInternet").className = "main";
	}


});

function fullWin(url) {
	if (url == "fedex")
	{
	window.open("http://www.optimumbusiness.com/small-business-benefits/fedex.jsp","new_window","width=790,height=450,scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes,status=yes");
	return false;
	} else {
	window.open(url,"new_window","width=790,height=450,scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes,status=yes");
	return false;
	}
}

function openWin(url,name,width,height,scrollbars,resizable,menubar,toolbar,currentWindow) {	
	if (!resizable)	resizable = "no";
	if (!menubar) menubar = "no";
	if (!toolbar) toolbar = "no";
	winFeatures = "width=" + width + ",height=" + height + ",scrollbars=" + scrollbars + ",resizable=" + resizable + ",menubar=" + menubar + ",toolbar=" + toolbar + ",top=100,left=100,screenX=100,screenY=100";
	window.open(url,name,winFeatures);
	if (currentWindow === "close") self.close();
}

var urlParams = {};
(function () {
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

    while (e = r.exec(q))
       urlParams[d(e[1])] = d(e[2]);
})();


if ("bsp" in urlParams === true) {
	var expireDate, expireLength;
	expireLength = 30 * 1000 * 60 * 60 * 24;
	var expireDate = new Date( today.getTime() + (expireLength) );
	document.cookie="bsp=" + urlParams["bsp"] + ";expires=" + expireDate;
}


//Omniture tracking
 $(document).ready(function() {
	$("#benefitsSubmit").click(function(){
		s.linkTrackVars = 'event15';
		s.events = 'event15';
		s.tl();
	});
	$(".solution_finder_link").click(function() {
		s.linkTrackVars = 'evar34';
		s.eVar34 = 'Start Solution Finder';
		s.tl();
	});
	$("#reviseButton").click(function() {
		s.linkTrackVars = 'evar34';
		s.eVar34 = 'Revise Solution Finder Results';
		s.tl();
	});
	$(".submitQuestions").click(function() {
		s.linkTrackVars = 'evar34';
		s.eVar34 = 'Submit Questions';
		s.tl();
	});
	$("#partnerSubmit").click(function(){
		s.linkTrackVars = 'prop25';
		s.prop25 = 'partner submit';
		s.tl();
	});
	$(".infoLink").click(function() {
		s.linkTrackVars = 'prop32';
		s.prop32 = $(this).text();
		s.tl();
	});
	$(".sipGuideLink").click(function() {
		s.linkTrackVars = 'prop17';
		s.prop17 = $(this).text();
		s.tl();
	});
	$(".boxLinkFeature").click(function() {
		s.linkTrackVars = 'prop22';
		s.prop22 = $(this).text();
		s.tl();
	});

 });

//search

 $(document).ready(function() {
	 $("#search-box").val("Search");
	$("#search-box")
		.focus(function(){
			if($(this).val()==$(this).attr('title')) 
				$(this).val('');
				$(this).css("color","#656565");
		})
		.blur(function(){
			if($(this).val().trim().length==0) 
				$(this).val($(this).attr('title'));
		});
	/*if($("#search-box").val().trim().length==0) 
	 {
		$("#search-box").val($("#search-box").attr('title'));
	 }*/

$('#search-button').click(function() {
  if ($("#search-box").val() === "Search" || $("#search-box").val() === "") {
	  alert("Please enter a search term.");
	  return false;
  }
  s.linkTrackVars = 'eVar2';
  s.eVar2 = $("#search-box").val();
  s.tl();
});
  
});

$("a").click(function() {
	$(this).css("outline", "none"); 
});



//TransMotion espanol links
var MP = {
	<!-- mp_trans_disable_start -->     
	Version: '1.0.15',    
	Domains: {'es':'espanol.optimumbusiness.com'},				
	SrcLang: 'en',
	<!-- mp_trans_disable_end -->	
	UrlLang: 'mp_js_current_lang',	
	SrcUrl: unescape('mp_js_orgin_url'),
	<!-- mp_trans_disable_start --> 		
	init: function() {	   
		if (MP.SrcUrl.indexOf('p_js_')==1) {	       
			MP.SrcUrl = location.href;           
			MP.UrlLang = MP.SrcLang;	   
		}	
	},    
	switchLanguage: function(lang) {		
		if (lang!=MP.SrcLang) {            
			var script = document.createElement('SCRIPT');            
			script.src = location.protocol+'//'+MP.Domains[lang]+'/en'+lang+'/?1023749632;'+escape(MP.SrcUrl);            
			document.body.appendChild(script);        
					
		} else if (lang==MP.SrcLang) {		    
			MP.switchToLang(MP.SrcUrl);				
		}		
		return false;    
	},	
	switchToLang: function(url) {        
		var mplink=document.createElement('A');		
		if (mplink.click) { // using location.href will cause IE6 to not report referrer		    
			mplink.href=url;            
			document.body.appendChild(mplink);            
			mplink.click();
		} else {		    
			location.href=url; 		
		}	
	}
<!-- mp_trans_disable_end -->	
};


Shadowbox.init();

