//USA phone number validation to limit mis-typed numbers
function check_usphone(phonenumber,useareacode) { 
	if(!useareacode)useareacode=1;
	if((phonenumber.match(/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null) && ((useareacode!=1) && (phonenumber.match(/^[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null))) return false; 
	return true; 
} 

$(document).ready(function() {
	//animated page scrolling
	$('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 - 10;
        $('html,body')
        .animate({scrollTop: targetOffset}, 500);
       return false;
      }
    }
  });
													 
	//open [rel="external"] links in a new window
	$('a[rel=external]').click(function(){
			 this.target = "_blank";
	});
	
	//fix PNG transparency for IE6													 
	$(document).pngFix();
	
	//zebra-striped table rows
	$("tr:odd").addClass("alt");
	
	//protect contact email from spambots
	$('.contact-email').attr('href', 'mailto:info@368design.com?subject=368 Design Website Submission').text('info@368design.com');
	
	//contact form submission
  //check for spam (from http://docs.jquery.com/Tutorials:Safer_Contact_Forms_Without_CAPTCHAs)
  $.get("token.php",function(txt){
    $("sendEmail").append('<input type="hidden" name="ts" value="'+txt+'" />');
  });
	$("#submit").click(function(){					   				   
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var phoneReg = /^\(*[2-9]\d{2}\)*[- \.]\d{3}[- \.]\d{4}$/;
		
		var phoneVal = $("#phone").val();
		if(phoneVal == '') {
			$("#phone").after('<span class="error">Please enter your phone number.</span>');
			hasError = true;
		} else if(!phoneReg.test(phoneVal)) {	
			$("#phone").after('<span class="error">Please enter a valid phone number.</span>');
			hasError = true;
		}
		
		var email_fromVal = $("#email_from").val();
		if(email_fromVal == '') {
			$("#email_from").after('<span class="error">Please enter your email address.</span>');
			hasError = true;
		} else if(!emailReg.test(email_fromVal)) {	
			$("#email_from").after('<span class="error">Please enter a valid email address.</span>');
			hasError = true;
		}
		
		var first_nameVal = $("#first_name").val();
		if(first_nameVal == '') {
			$("#first_name").after('<span class="error">Please enter your first name.</span>');
			hasError = true;
		}
		
		var last_nameVal = $("#last_name").val();
		if(last_nameVal == '') {
			$("#last_name").after('<span class="error">Please enter your last name.</span>');
			hasError = true;
		}
		
		var referredVal = $("#referred").val();
		if(referredVal == '') {
			$("#referred").after('<span class="error">Please let us know how you heard about us.</span>');
			hasError = true;
		}
		
		var messageVal = $("#message").val();
		if(messageVal == '') {
			$("#message").after('<span class="error">Please enter your message.</span>');
			hasError = true;
		}
		
		
		if(hasError == false) {
			$("ul.buttons .text-blue").html('&nbsp;');
			$(this).hide();
			$("#loading").css('display', 'block');
			
			$.post("sendemail.php", $("#sendEmail").serialize(),
   				//{ phone: phoneVal, email_from: email_fromVal, first_name: first_nameVal, last_name: last_nameVal, message: messageVal },
   					function(data){
						$("#sendEmail").slideUp("normal", function() {				   
							$("#sendEmail").before(data);											
						});
   					}
				 );
		}
		
		return false;
	});
	
	//scrollable elements from jQuery Tools: http://www.flowplayer.org/tools/scrollable.html
	$("#thumbnails").scrollable({size: 4, clickable: false});
	
	//flash embed from jQuery Tools: http://www.flowplayer.org/tools/flashembed.html
	// wmode parameter allows html over flash
	$("#flash .section-inner").flashembed({src: "swf/splash.swf", wmode: 'opaque', version: [9, 0]}); 
});