var test_corpemail = false; // hack
var slideform;
jQuery(document).ready(function($){

if(location.pathname == '')
{
	
}

$('a[href*=#]').click(function() {
	if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
		var $target = $(this.hash);
		$target = $target.length && $target	|| $('[name=' + this.hash.slice(1) +']');
		if ($target.length) {
			var targetOffset = $target.offset().top - 40;
			$('html,body').animate({scrollTop: targetOffset}, 1000);
			return false;
		}
	}
});

slideform = $('.slide_out_form').tabSlideOut({
    tabHandle: '.handle',                     //class of the element that will become your tab
    tabLocation: 'right',                      //side of screen where tab lives, top, right, bottom, or left
    speed: 300,                               //speed of animation
    action: 'click',                          //options: 'click' or 'hover', action to trigger animation
    topPos: '130px',                          //position from the top/ use if tabLocation is left or right
    leftPos: '20px',                          //position from left/ use if tabLocation is bottom or top
    fixedPosition: true                      //options: true makes it stick(fixed position) on scroll
});

// added to overcome the page reset of the tabSlideOut
if(location.hash)
{
	var $target = $(location.hash);
	$target = $target.length && $target	|| $('[name=' + location.hash.slice(1) +']');
	if ($target.length) {
		var targetOffset = $target.offset().top - 40;
		$('html,body').animate({scrollTop: targetOffset}, 1000);
		return false;
	}
}


jQuery.validator.addMethod("corpemail", function(value, element) {
	if (this.optional(element) || /^(?!(.*hotmail.com|.*msn.com|.*gmail.com|.*yahoo.com|.*aol.com))/.test(value))
	{
		test_corpemail = false;
		return true;
	}
	else
	{
		test_corpemail = true;
		return false;
	}
}, "Please specify a corporate email address");


$("#registration_form").validate({
	onfocusout: false,

	onkeyup: false,

	showErrors: function(errorMap, errorList) {
		for ( var i = 0; this.errorList[i]; i++ ) {
			var error = this.errorList[i];
			this.settings.highlight && this.settings.highlight.call( this, error.element, this.settings.errorClass, this.settings.validClass );
		}
		if( this.errorList.length ) {
			this.toShow = this.toShow.add( this.containers );
		}
		if (this.settings.success) {
			for ( var i = 0; this.successList[i]; i++ ) {
				this.showLabel( this.successList[i] );
			}
		}
		if (this.settings.unhighlight) {
			for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) {
				this.settings.unhighlight.call( this, elements[i], this.settings.errorClass, this.settings.validClass );
			}
		}
		this.toHide = this.toHide.not( this.toShow );
		this.hideErrors();
		this.addWrapper( this.toShow ).show();

	},
	
	invalidHandler: function(form, validator) {
		$(document).unbind('click');
		if (test_corpemail == true)
			$('#corpinvalid').dialog({ dialogClass: 'ui-widget-error', modal: true, buttons: { "Ok": function(event) { $(this).dialog("close");event.stopPropagation(); } }, close: function(event, ui) { slideform.clickScreenToClose(); } });
		else
			$('#invalid').dialog({ dialogClass: 'ui-widget-error', modal: true, buttons: { "Ok": function(event) { $(this).dialog("close");event.stopPropagation(); } }, close: function(event, ui) { slideform.clickScreenToClose(); } });
	},

	submitHandler: function(form) {
		$.ajax({   
			url: "/b2b-sales-lead-generation-campaigns/index.php?ajax=yes",    

			type: "POST",   

			data: $(form).serialize(),        

			cache: false,   

			success: function (html) {                 
				if (html==1) {
					$('#success').dialog({ modal: true,  buttons: { "Ok": function(event) { $(this).dialog("close");event.stopPropagation(); } }, close: function(event, ui) { slideform.clickScreenToClose(); } });
					$('#registration_form').fadeOut(1000, function() {
						$('#registration_form').remove();
						$('<h4>Thank You</h4><p>One of our representatives will email you the free guide shortly. Please whitelist @identitystudios.biz to make sure you receive it.</p><p>Feel free to browse our website and connect with us if you\'d like to learn more about our campaigns.</p><p>Contact us by phone at (877) 675-8303 or by email at <a href="mailto:hello@identitystudios.biz">hello@identitystudios.biz</a>.</p>').hide().addClass('success').appendTo('#regform').fadeIn(1000);
					});
				} else {
					$('#error').dialog({ dialogClass: 'ui-widget-error', modal: true, buttons: { "Ok": function(event) { $(this).dialog("close");event.stopPropagation(); } }, close: function(event, ui) { slideform.clickScreenToClose(); } });
				}          
			}          
		}); 

	}
});






});


