$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

	  var name = $("input#name");
	  var phone = $("input#phone");
	  var phone1 = $("input#phone1");
	  var email = $("input#email");
	  var besttime = $("select#besttime");
	  var message = $("textarea#message");
	  var security_code = $("input#security_code");
	  var DropDownTimezone = $("select#DropDownTimezone");
	  
	  name.blur(validateName);
	  name.keyup(validateName);
	  
	  phone.blur(validatePhone);
	  phone.keyup(validatePhone);
	  
	  phone1.blur(validatePhone1);
	  phone1.keyup(validatePhone1);
	  
	  email.blur(validateEmail);
	  email.keyup(validateEmail);
	  
	  besttime.blur(validateBesttime);
	  besttime.keyup(validateBesttime);
	  
/*	  message.blur(validateMessage);
	  message.keyup(validateMessage);
*/	  
	  security_code.blur(validateSecuritycode);
	  security_code.keyup(validateSecuritycode);
	  
	function validateName(){
		if(name.val().length < 4){
			name.addClass("error");
			return false;
		}
		else{
			name.removeClass("error");
		}
	}
	function validatePhone(){
		if(phone.val().length < 2){
			phone.addClass("error");
			return false;
		}
		else{
			phone.removeClass("error");
		}
	}
	function validatePhone1(){
		if(phone1.val().length < 6){
			phone1.addClass("error");
			return false;
		}
		else{
			phone1.removeClass("error");
		}
	}
	function validateEmail(){
		var a = email.val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email
		if(filter.test(a)){
			email.removeClass("error");
			//emailInfo.text("Valid E-mail please, you will need it to log in!");
			//emailInfo.removeClass("error");
			//return true;
		}
		//if it's NOT valid
		else{
			email.addClass("error");
			//emailInfo.text("Stop cowboy! Type a valid e-mail please :P");
			//emailInfo.addClass("error");
			return false;
		}
	}
	function validateBesttime(){
		if(besttime.val().length < 3){
			besttime.addClass("error");
			return false;
		}
		else{
			besttime.removeClass("error");
		}
	}
	function validateMessage(){
		if(message.val().length < 10){
			message.addClass("error");
			return false;
		}
		else{
			message.removeClass("error");
		}
	}
	function validateSecuritycode(){
		if(security_code.val().length < 5){
			security_code.addClass("error");
			return false;
		}
		else{
			security_code.removeClass("error");
		}
	}

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
   // $('.error').hide();


		if(name.val().length < 4)
		{
		name.addClass("error");
        }
		
		if(phone.val().length < 2)
		{
		phone.addClass("error");
        }

		if(phone1.val().length < 5)
		{
		phone1.addClass("error");
        }

/*		if(message.val().length < 10)
		{
		message.addClass("error");
        }
*/
		if(besttime.val().length < 5)
		{
		besttime.addClass("error");
        }

		if(security_code.val().length < 5)
		{
		security_code.addClass("error");
        }

		if(email.val().length < 5)
		{
		var a = email.val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		if(filter.test(a)){
			email.removeClass("error");
		}
		else{
			email.addClass("error");
		}
        
		}

		if((name.val().length < 4) ||  (phone.val().length < 2) ||  (phone1.val().length < 5) ||   (besttime.val().length < 5) ||  (security_code.val().length < 5) ||  (email.val().length < 5) )
		return false;
	
		var dataString = 'name='+ name.val() + '&email=' + email.val() + '&phone=' + phone.val() + '&phone1=' + phone1.val() + '&message=' + message.val() + '&besttime=' + besttime.val() + '&security_code=' + security_code.val() + '&DropDownTimezone=' + DropDownTimezone.val();
		
		//var dataString = 'name='+ name;
		//alert (dataString);//return false;
		
		$.ajax({
      type: "POST",
      url: "jquery-ajax/bin/process.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h2>Thank you <br>for contacting us .</h2>")
        .append("<p>An email has been sent to "+email.val()+" as confirmation.<br><br>One of our sales representative will be in touch with you within 24-48 hours on your preferred time via the contact information you have provided. Meanwhile please visit following few important links. </p><br><p>- <a target=_blank href=summer-workation.php>View case studies</a><br>- <a href=technology-center.php>Technology center</a><br>- <a href=about-us.php>Company overview</a><br>- <a  href=engagement-model.php>Engagement Model</a><br>- <a target=_blank href=http://www.konstantlab.com>Browse Portfolio</a><br>- <a href=our-network.php>Browse Our Network</a></p>")
		.hide()
        .fadeIn(1500, function() {
          //$('#message').append("<img id='checkmark' src='jquery-ajax/images/check.png' />");
       });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});


