/* Author: Stephan Taylor
*/

jQuery(document).ready(function(){
/* 	jQuery('#advisor-contact').attr({'action':''}); */
	jQuery("a[rel=#advisor-contact]").overlay();
	jQuery("a[rel=#advisor-contact]").click(function(){
		$scrolltop = jQuery(window).scrollTop();
		$scrolladj = $scrolltop + 50;
		$scrolladj = $scrolladj + 'px';
		jQuery('#advisor-contact').css({ 'top' : $scrolladj });
	});
	
	jQuery('li.required input').blur(function(){
		if(!jQuery(this).val() == '') {
			jQuery(this).parent('li').removeClass('highlight');
		}
	});

	/* Ajax Form to PHP */
	jQuery("#advisor-contact form input[type='submit']").click(function(){

		$oid 					= jQuery("input[name='oid']").val();
/*
		$debug 				= jQuery("input[name='debug']").val();
		$debugEmail		= jQuery("input[name='debugEmail']").val();
*/
		$first_name 	= jQuery("input#first_name").val();
		$last_name 		= jQuery("input#last_name").val();
		$email 				= jQuery("input#email").val();
		$phone 				= jQuery("input#phone").val();
		$comments 		= jQuery("textarea#00NA00000084vJD").val();
		$subscribe 		= jQuery("input#00NA00000084vDU").val();
		$submit				= jQuery('input[type="submit"]').val();
		$errmsg 			= '';
		$i						= 0;
		$opt					= [];
		$value				= [];
		
		jQuery('#advisor-contact input[type="checkbox"]').each(function(){
			$opt[$i] = jQuery(this).attr('id');
			if(jQuery(this).is(':checked')) {
				$value[$i] = jQuery(this).val();
			}
			else {
				$value[$i] = '0';
			}
			$i++;
		});


		var $dataString = 'oid=' + $oid + '&first_name=' + $first_name + '&last_name=' + $last_name + '&email=' + $email + '&phone=' + $phone + '&'+$opt[0]+'=' + $value[0] + '&'+$opt[1]+'=' + $value[1] + '&'+$opt[2]+'=' + $value[2] + '&'+$opt[3]+'=' + $value[3] + '&'+$opt[4]+'=' + $value[4] + '&'+$opt[5]+'=' + $value[5] + '&00NA00000084vJD=' + $comments + '&'+$opt[6]+'=' + $value[6] + '&submit=' + $submit;
		

	// We're only checking for empty required fields. Salesforce will filter the rest.
		jQuery('li.required').each(function(index){
			$inputval = jQuery(this).find('input').val();
			if($inputval == '' || $inputval == null) { 
				jQuery(this).addClass('highlight');
				jQuery('li.highlight:first').focus();
				return $errmsg = 'ERROR: Please review required fields.';
			}
		});
		
		if($errmsg == '') {
			jQuery.ajax({
				type: "POST",
				url: "/new/wp-content/themes/nlc/salesforce.php",
				data : $dataString,
				beforeSend: function(){
					jQuery("#advisor-contact form input[type='submit']").attr({'value':'Sending...'}).css({'opacity':'.5','cursor':'default'});
				},
				success: function($dataString){
					jQuery('<div></div>').attr({'id':'success-note'}).appendTo('#advisor-contact');
					jQuery('<h2></h2>').html('Thank You for your interest, '+$first_name+' '+$last_name).appendTo('#advisor-contact #success-note');
					jQuery('<p></p>').html('An advisor will be in contact with you shortly.').appendTo('#advisor-contact #success-note');
				},
				complete: function() {
					jQuery('#advisor-contact form').css({'visibility':'hidden'});
					jQuery('#success-note').fadeIn(100);
				},
				error: function(){
					alert('Error: Data could not be sent.');
				}
			});	// END ajax request.
		} 
		else {
			jQuery('#advisor-contact #error-msg').remove();
			jQuery('<p></p>').attr({'id':'error-msg'}).html($errmsg).appendTo('#advisor-contact form');
			return false;
		}
	
		return false;
	}); // END submit click on #advisor-contact


	jQuery('#contact-form input[type="submit"]').click(function(){
	
		$oid 					= jQuery("#contact-form input[name='oid']").val();
/*
		$debug 				= jQuery("input[name='debug']").val();
		$debugEmail		= jQuery("input[name='debugEmail']").val();
*/
		$first_name 	= jQuery("#contact-form input#first_name").val();
		$last_name 		= jQuery("#contact-form input#last_name").val();
		$email 				= jQuery("#contact-form input#email").val();
		$phone 				= jQuery("#contact-form input#phone").val();
		$comments 		= jQuery("#contact-form textarea#00NA00000084vJD").val();
		$subscribe 		= jQuery("#contact-form input#00NA00000084vDU").val();
		$submit				= jQuery('#contact-form input[type="submit"]').val();
		$errmsg 			= '';
		$i						= 0;
		$opt					= [];
		$value				= [];
		
		jQuery('#contact-form input[type="checkbox"]').each(function(){
			$opt[$i] = jQuery(this).attr('id');
			if(jQuery(this).is(':checked')) {
				$value[$i] = jQuery(this).val();
			}
			else {
				$value[$i] = '0';
			}
			$i++;
		});


		var $dataString = 'oid=' + $oid + '&first_name=' + $first_name + '&last_name=' + $last_name + '&email=' + $email + '&phone=' + $phone + '&'+$opt[0]+'=' + $value[0] + '&'+$opt[1]+'=' + $value[1] + '&'+$opt[2]+'=' + $value[2] + '&'+$opt[3]+'=' + $value[3] + '&'+$opt[4]+'=' + $value[4] + '&'+$opt[5]+'=' + $value[5] + '&00NA00000084vJD=' + $comments + '&'+$opt[6]+'=' + $value[6] + '&submit=' + $submit;
		

	// We're only checking for empty required fields. Salesforce will filter the rest.
		jQuery('#contact-form li.required').each(function(index){
			$inputval = jQuery(this).find('input').val();
			if($inputval == '' || $inputval == null) { 
				jQuery(this).addClass('highlight');
				jQuery('li.highlight:first').focus();
				return $errmsg = 'ERROR: Please review required fields.';
			}
		});
		
		if($errmsg == '') {
			jQuery.ajax({
				type: "POST",
				url: "/new/wp-content/themes/nlc/salesforce.php",
				data : $dataString,
				beforeSend: function(){
					jQuery("#contact-form input[type='submit']").attr({'value':'Sending...'}).css({'opacity':'.5','cursor':'default'});
				},
				success: function($dataString){
					jQuery('<div></div>').attr({'id':'success-note'}).appendTo('#contact-advisor-form');
					jQuery('<h2></h2>').html('Thank You.').appendTo('#contact-advisor-form #success-note');
					jQuery('<p></p>').html('Thank you for your interest, '+$first_name+' '+$last_name+'. An advisor will be in contact with you shortly.').appendTo('#contact-advisor-form #success-note');
				},
				complete: function() {
					jQuery('#contact-form').css({'visibility':'hidden'});
					jQuery('#success-note').fadeIn(100);
				},
				error: function(){
					alert('Error: Data could not be sent.');
				}
			});	// END ajax request.
		} 
		else {
			jQuery('#contact-form #error-msg').remove();
			jQuery('<p></p>').attr({'id':'error-msg'}).html($errmsg).appendTo('#contact-form');
			return false;
		}
	
		return false;
	});
	
		
});



function trim(str) {
   return str.replace(/^\s+|\s+$/g,'');
}

function isEmail(str) {
   var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
	
	return regex.test(str);
}

