var today = new Date();

$.validator.addMethod("greaterThanZero", function(value, element) {
    return this.optional(element) || (parseFloat(value) > 0);
});

$.validator.addMethod("notEqualTo", function(value, element, param) {
	return this.optional(element) || value != param;
});

$.validator.addMethod('postcode', function (value) { 
    return /^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$/.test(value); 
});

$.validator.addMethod('phoneno', function (value) { 
    return 	/^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/.test(value); 
});

$.validator.addMethod('future', function (value) { 
	value = value.slice(0, -1);
	var mySplitDate = value.split(" ");
	var myDate=new Date();
	myDate.setFullYear(mySplitDate[2],mySplitDate[1]-1,mySplitDate[0]);
	return $("#asap").is(':checked') || (myDate > today);
});

var RecaptchaOptions = {
   theme : 'white'
};


$(document).ready(function(){
//Forms
    $("#startdate_day,#startdate_month,#startdate_year").change(function(){
          var str = "";
          $("#startdate select option:selected").each(function () {
                str += $(this).val() + " ";
              });
          $("#full_date").val(str);
		  $("#full_date").focus();
		  $(this).focus();

        })
        .change();
		
	$(".areyousure").click(function(){
		if($('.check input').is(':checked')){
			if($(".select-actions").val()=="Delete"){
				var answer = confirm("Are you sure you want to delete these items?");
				if (answer){
				}
				else{
					return false;
				}
			}
		}else return false;
	});		
	$(".areyousure2").click(function(){
		if($('.check2 input').is(':checked')){
			if($(".select-actions2").val()=="Delete"){
				var answer = confirm("Are you sure you want to delete these items?");
				if (answer){
				}
				else{
					return false;
				}
			}
		}else return false;
	});	

	 
	$("#post_form textarea,#post_form select,#post_form input:not(.form_image)").focus(function(){
		$(this).parent().addClass("hover");
	});	
	$("#post_form textarea,#post_form input,#post_form select").blur(function(){
		$(this).parent().removeClass("hover");
	});
	$("#asap").click(function(){
		if($(this).is(':checked')){
			$(".day,.month,.year").attr("disabled","disabled");
			$("#startdate").removeClass("error");
			$("#date_error").removeClass("invalid");
			$("#startdate span").replaceWith("");
		}else{
			$(".day,.month,.year").attr("disabled","");
			$("#startdate span").replaceWith("");
		}
	});	
//Validation
	$("#post_form").validate({	
		
		startdate_day: {dateCheck:true},
		
		rules: {
			password: {
			  minlength: 4
			},			
			password2: {
			minlength: 4,
			  equalTo: ".password"
			},
			payrate: {
				greaterThanZero : true,
				number: true
				},
			jobhours: {
				range: [1,48],
				number: true
				},
			phonenumber_company:{phoneno:true},
			jobdesc:{notEqualTo:'e.g. The duties that will be expected from the employee are...'},
			jobtitle:{notEqualTo:'e.g. Waiter, Cleaner'},
			aboutyou:{notEqualTo:'e.g. I am a hard worker...'},
			qualifications:{notEqualTo:'e.g. English GCSE Grade C'},
			postcode_company:{postcode:true},
			full_date:{
				required: true,
				date:true,
				future: true
			}
		},

		errorPlacement: function(error, element) {
			error.appendTo(element.parent());
		},
			
		errorElement: "span",
		errorClass: "invalid",

		highlight: function(element, errorClass) {
			$(element).parent().addClass("error");
			$(element).parent().removeClass("valid");
		},
	   success: function(label) {
		 label.parent().addClass("valid");
		 label.addClass("valid");
		 label.parent().removeClass("error");
	   }
	});
	//Onfocus stuff

	//Job details page
	$("#message_apply").val("I think this job would suit me because...").addClass("grayed");
	$("#message_apply").focus(function(){$(this).val("").removeClass("grayed")});

	//post-job
	$("#jobhours").val("0").addClass("grayed");
	$("#jobhours").focus(function(){$(this).val("").removeClass("grayed")});
	
	$("#payrate").val("0.00").addClass("grayed");
	$("#payrate").focus(function(){$(this).val("").removeClass("grayed")});
});



//top search and find a job page
$("#multisearch").ready(function() {
	var searchBoxes = $("#multisearch,#keyword");
	searchBoxes.addClass("searchformtext");
	
	if ($.trim(searchBoxes.val())!="") {
		searchBoxes.removeClass("searchformtext");
	}
		
	searchBoxes.focus(function() {
		$(this).removeClass("searchformtext");
	});
	
	searchBoxes.blur(function() {
		if ($.trim($(this).val())=="") {
			$(this).addClass("searchformtext");
		}
	});
});
