$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#ffffff"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#F9F3DF;"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#ffffff"});
  });

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();


   var name = $("input#name").val();
		if (name == "") {
      $("label#name_error").show();
      $("input#name").focus();
      return false;
    }
   else if (!containsAlphabets(name))
    {
     $("label#name_error2").show();
      $("input#name").focus();
      return false;
    }
    

    var contact = $("input#contact").val();
		if (contact == "") {
      $("label#contact_error").show();
      $("input#contact").focus();
      return false;
    }
    else if (isNaN(contact))
    {
      $("label#contact_error2").show();
      $("input#contact").focus();
      return false;
    }
    else if (contact.length != 10)
    {
   	  $("label#contact_error3").show();
      $("input#contact").focus();
      return false;

    }
    
    
    
    var email = $("input#email").val();
	
	if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }
    else if (!checkmail(email))
    {
   	  $("label#email_error2").show();
      $("input#email").focus();
      return false;
    }
    
   

	var education = $("textarea#education").val();
		if (education == "") {
      $("label#education_error").show();
      $("textarea#education").focus();
      return false;
    }
    
  
   var state = $("select#state").val();
		
		if (state == "none") {
      $("label#state_error").show();
      $("input#state").focus();
      return false;
    }
    
	var course = $("select#course").val();
		
		if (course == "none") {
      $("label#course_error").show();
      $("input#course").focus();
      return false;
    }
    
     var timing_hr = $("select#hour").val();
		if (timing_hr == "none") {
      $("label#timing_error_hr").show();
      $("select#hour").focus();
      return false;
    }

    var timing_min = $("select#min").val();
		if (timing_min == "none") {
      $("label#timing_error_min").show();
      $("select#min").focus();
      return false;
    }

   var timing = timing_hr + ":" + timing_min;
    
    var others = $("textarea#others").val();
		if (others == "") {
      $("label#msg_error").show();
      $("textarea#others").focus();
      return false;
    }


	/*
	var verify = $("input#verify").val();
	
	var verificationcode = $("input#verificationcode").val();
		if (verificationcode == "") {
      $("label#verificationcode_error").show();
      $("input#verificationcode").focus();
      return false;
    }
    else if (verify != verificationcode) {
      $("label#verificationcode_error2").show();
      $("input#verificationcode").focus();
      return false;
    }

    */
    
	var dataString ='name=' + name +'&contact='+ contact + '&email='+ email + '&education=' + education + '&state=' + state+ '&course=' + course +'&timing=' + timing+'&others=' + others;
	//alert (dataString);
		
		$.ajax({
      type: "POST",
      url: "ajaxPage/process.php",
      data: dataString,
      success: function() {
        //$('#participate_form').html("<div id='message'></div>");
        $('#message').html("<h2>Thankyou for contact us!</h2>")
        //.append("<p>We will contact you as .</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});


function checkmail(str){
	var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   return false
		}

		else if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		else if (str.indexOf(dot,(lat+2))==-1){
		   return false
		 }
		
		 else if (str.indexOf(" ")!=-1){
		    return false
		 }
		else return true;
}

function containsAlphabets(checkString) {
        var tempString="";
        var regExp = /^[A-Za-z ]$/;
        
	if(checkString != null && checkString != "")
        {
          for(var i = 0; i < checkString.length; i++)
          { 
            if (!checkString.charAt(i).match(regExp))
            {
              return false;
            }
          }
        }
        else
        {
          return false;
        }
	return true;

}

