$(document).ready(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#3D78B4"});
	$(this).css({color:"#FFFFFF"});
	$(this).attr('value') = Trim($(this).attr('value'));
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
	$(this).css({color:"#000000"});
  });
  $('textarea.text-input').css({backgroundColor:"#FFFFFF"});
  $('textarea.text-input').focus(function(){
    $(this).css({backgroundColor:"#3D78B4"});
	$(this).css({color:"#FFFFFF"});
  });
  $('textarea.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
	$(this).css({color:"#000000"});
  });
  $('.enter').click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
			
	  
	  var fname = $('#firstname').attr('value');
	  if(fname == ""){
			$('#firstname').focus();
			return false;
	  }
	  var lname = $('#lastame').attr('value');
	  var phone = $('#phone').attr('value');
	  var fax = $('#fax').attr('value');
	  var email = $('#email').attr('value');
	  /*
	  if (email == "" || !(IsEmail(email))){
		  	$('#email').focus();
	  		return false;
	  }	
	  */
	  var address = $('#address').attr('value');
	  var city = $('#city').attr('value');
	  var state = $('#state').attr('value');
	  var debt = $('#debt').attr('value');
	  var comment = $('#comment').attr('value');
	  var besttime = $('#besttime').attr('value');
		
	  var dataString = 'fname='+ fname + 
	   				    '&lname' + lname + 
						'&phone=' + phone + 
						'&fax=' + fax +
						'&email=' + email +
						'&address=' + address +
						'&city=' + city + 
						'&state=' + state +
						'&debt=' + debt +
						'&comment=' + comment +
						'&besttime=' + besttime;
		
		$.ajax({
			  type: "GET",
			  url: "php/bigemail.php",
			  dataType: "html",
			  data: dataString,
			  success: function(response){
				  $(':input')
 					.not(':button, :submit, :reset, :hidden')
 					.val('')
			 		.removeAttr('checked')
 					.removeAttr('selected');
			   alert(response);
			   // if sucessful; response will contain some stuff echo-ed from .php
			   // quick tect: alert("Here is your response: " + response);
			   // Append this response to some <div> => let's append it to div with id "serverMsg"
			  },
			  error: function(){
			   alert("Could not communicate with server please try again later");
			  }
			 });
    return false;
	});
});
runOnLoad(function(){
  $("input#txtName").select().focus();
});

function IsEmail(email) {
            var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            return (regex.test(email));
}  
function IsPhoneNumber(phone){
			var regex = /^\(\d{3}\)\d{3}-\d{4}$/; 
			return (regex.test(phone)); 
}
function LTrim(str)
{
  var whitespace = new String(" ");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(0)) != -1) {
    // We have a string with leading blank(s)...
    var j=0, i = s.length;
    // Iterate from the far left of string until we
    // don't have any more whitespace...
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
    j++;
    // Get the substring from the first non-whitespace
    // character to the end of the string...
    s = s.substring(j, i);
  }
  return s;
}
function RTrim(str)
{
  // We don't want to trip JUST spaces, but also tabs,
  // line feeds, etc.  Add anything else you want to
  // "trim" here in Whitespace
  var whitespace = new String(" ");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
    // We have a string with trailing blank(s)...
    var i = s.length - 1;       // Get length of string
    // Iterate from the far right of string until we
    // don't have any more whitespace...
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
      i--;
    // Get the substring from the front of the string to
    // where the last non-whitespace character is...
    s = s.substring(0, i+1);
  }
  return s;
}
function Trim(str)
{
  return RTrim(LTrim(str));
}
