$(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"});
  });

  $(".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 address = $("textarea#address").val();
		if (address == "") {
      $("label#address_error").show();
      $("textarea#address").focus();
      return false;
    }
    
    var country  = $("select#country").val();
		
		if (country == "none") {
      $("label#country_error").show();
      $("input#country").focus();
      return false;
    }
    
    var city = $("input#city").val();
		if (city == "") {
      $("label#city_error").show();
      $("input#city").focus();
      return false;
    }
   else if (!containsAlphabets(city))
    {
     $("label#city_error2").show();
      $("input#city").focus();
      return false;
    }


    var phone = $("input#phone").val();
		if (phone == "") {
      $("label#phone_error").show();
      $("input#phone").focus();
      return false;
    }
    else if (isNaN(phone))
    {
      $("label#phone_error2").show();
      $("input#phone").focus();
      return false;
    }
    else if (phone.length < 6)
    {
   	  $("label#phone_error3").show();
      $("input#phone").focus();
      return false;

    }
    
    var mobile = $("input#mobile").val();
		if (mobile != "") 
		{
      		if (isNaN(mobile))
    		{
      			$("label#mobile_error").show();
      			$("input#mobile").focus();
      			return false;
    		}
    		else if (mobile.length < 6)
    		{
   				$("label#mobile_error2").show();
      			$("input#mobile").focus();
      			return false;
		   	}
		}    
    	else
    		mobile = "None";
        
    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 web = $("input#web").val();
	
	if (web != "http://") {
    	if (!urlChecker(web))
   		{
   	  		$("label#web_error").show();
      		$("input#web").focus();
      		return false;
    	}
    }
    else
    	web = "None";
    
   var query = $("textarea#query").val();
   if (query == "") {
      $("label#query_error").show();
      $("textarea#query").focus();
      return false;
    }

	var captcha_value = $("input#captcha_value").val();
		if (captcha_value == "failed") {
      $("label#captcha_value_error").show();
      $("input#txtCaptcha").focus();
      return false;
    }
	
  
	var dataString ='name=' + name +'&address='+ address + '&country=' + country + '&city=' + city + '&phone=' + phone + '&mobile=' + mobile + '&email='+ email + '&web=' + web + '&query=' + query + '&captcha_value=' + captcha_value;

/*

var dataString ='course=' + course +'&timing=' + timing+'&others=' + others +'&captcha_value='+ captcha_value;
*/

	//alert (dataString); return false;
		
		$.ajax({
      type: "POST",
      url: "ajaxPage/process.php",
      data: dataString,
      success: function() {
        //$('#participate_form').html("<div id='message'></div>");
        $('#message').html("<h2>Thankyou for contacting us!</h2>")
        .append("<p>We will contact you as soon as possible.</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;
}

function urlChecker(url)
{
var url= url;
var is_protocol_ok=url.indexOf('http://');
var is_dot_ok=url.indexOf('.');
if ((is_protocol_ok==-1) || (is_dot_ok==-1))
{ 
	return false
}
else
	return true;
} //end of urlChecker


