var agent = navigator.userAgent.toLowerCase();

$(document).ready(function(){
	attachLeadValidation();
	attachBubblePopups();
	attachPopups();
	attachInputWatermarks();
	if($('.topAd').length > 0){
		$('.topAd').click(function(){
			var url = $(this).attr("rel");
            var windowName = "popUp";
            var windowSize = "width=1050,height=700,scrollbars=yes";
 			window.open(url, windowName, windowSize);
	 		event.preventDefault();
		});
	}
	
});

attachLeadValidation = function(){
	// First Page Validation
	$("#leadPageForm").validate({
	    invalidHandler: function(form, validator) {
	      var errors = validator.numberOfInvalids();
	      if (errors) {
	        var message = errors == 1
	          ? 'You missed 1 field. It has been highlighted'
	          : 'You missed ' + errors + ' fields. They have been highlighted';
	        $("div.error span").html(message);
	        $("div.error").show();
	      } else {
	        $("div.error").hide();
	      }
	    }
	 });
	 $("input#expDate").mask("99/99");
	 if(!isMobile()){
		$("input#phone").mask("(999) 999-9999");
	 }
	
	 //$("input#CVV").mask("999");
	 $("input[type=submit]").click(function(){
	 	warning = false;
		deleteWaterMarks();
	 });
};

window.onbeforeunload = function() { 
  if (warning) {
    return message;
  }
}

deleteWaterMarks = function(){
	if ($('.InputWrapper').length > 0) {
		$('.InputWrapper input[type=text]').each(function() {
			if($(this).val() == $(this).attr('rel')){
				$(this).val('');
			}
		});
	}
}

attachInputWatermarks = function() {
	if($('.InputWrapper').length > 0){
		
		$('.InputWrapper input[type=text]').focus(function(){
			if($(this).val() == $(this).attr('rel')){
				$(this).val('');
			}
		});
		
		$('.InputWrapper input[type=text]').blur(function(){
			if($(this).val() == ''){
				$(this).val($(this).attr('rel'));
			}
		});
		
		$('.InputWrapper input[type=text]').each(function() {
			if($(this).val() == ''){
				$(this).val($(this).attr('rel'));
			}
		});
		
	}
}

attachBubblePopups = function(){
	/*
if($('#cvvImage').length != 0){
		$('#cvvImage').SetBubblePopup({
			innerHtml: '<div class="center popupMessage">The last 3 digits on the back of your card.<br /><img src="https://sites.rocketleads.net/images/homebizdatabase/cvv.gif" /></div>', 
			bubbleAlign: 'left',
			tailAlign: 'left',
			color: 'green',
			contentStyle: 'font-size:16px;',
			imageFolder: 'https://sites.rocketleads.net/images/homebizdatabase/bp_images',
		});
	}
*/
	
	if($('#cvvImage2').length != 0){
		

		$('#cvvImage2').SetBubblePopupOptions({
			
			position : 'left',
			align	 : 'top',
			tail	 : {align: 'top'},
			innerHtml: '<div class="center popupMessage">The last 3 digits on the back of your card.<br /><img src="https://sites.rocketleads.net/images/homebizdatabase/cvv.gif" /></div>',
			innerHtmlStyle: {
								color:'#FFFFFF', 
								'text-align':'center'
							},
			themeName: 	'all-black',
			themePath: 	'https://sites.rocketleads.net/images/jquerybubblepopup-theme',
			closingDelay: 200
		});
		
		
		var options =	{ 
							position : 'right',
							align	 : 'top',
							tail	 : {align: 'top'},
							
							innerHtml: '<div class="center popupMessage">The last 3 digits on the back of your card.<br /><img width="100" height="63" src="https://sites.rocketleads.net/images/homebizdatabase/cvv.gif" /></div>',
				
							innerHtmlStyle: {
												color:'#FFFFFF', 
												'text-align':'center'
											},
							
							themeName: 	'all-azure',
							themePath: 	'/images/jquerybubblepopup-theme',
							
							closingDelay: 200
		};
		
		$('#cvvImage2').CreateBubblePopup( options );
	}
}

attachPopups = function(){
	$('.popup').popify({
		height: 620,
		width: 340,
		createnew: 0
	});
}

function isMobile(){
    return (
        (agent.indexOf("iphone") != -1) ||
        (agent.indexOf("ipod") != -1) ||
        (agent.indexOf("android") != -1)
    );
}

