// JavaScript Document

function numsOnly() {
	
}



function showrows() {
	$("#highesteducation option:selected").each(function () {
			
		if( $(this).val() == 0 || $(this).val() == 1 ) {
			$("#qualname_row").hide();
			$("#qualendyear_row").hide();
		}else if( $(this).val() > 1 && $(this).val() < 4 ) {
			$("#qualname_row").hide();
			$("#qualendyear_row").show();
		}else {
			// show end year
			$("#qualendyear_row").show();
			if( $(this).val() >= 4 ) {
				// Show qual name row
				$("#qualname_row").show();
			}
		}
	});
}

function showavailabilityoptions() {

	if($("input[name='availabilityOptions']:checked").val() == "I") {
		$("#noticeperiodblock").hide("slow");
		$("#fromblock").hide("slow");
	}else if($("input[name='availabilityOptions']:checked").val() == "N") {
		$("#noticeperiodblock").show("slow");
		$("#fromblock").hide("slow");
	}else if($("input[name='availabilityOptions']:checked").val() == "F") {
		$("#noticeperiodblock").hide("slow");
		$("#fromblock").show("slow");
	}
}



$(document).ready(function(){

	$("#wordCnt").html(countWords("#cvpitch") + " words");
	
	$("#cvpitch").keyup(function() {
		
		nWords = countWords("#cvpitch");
		
		if( nWords > 150 ) {
			alert( "Only 150 word please" );
			$("#cvpitch").val( $("#cvpitch").val().substr( 0, $("#cvpitch").val().lastIndexOf(" ") ) );
			nWords = countWords("#cvpitch");
			$("#wordCnt").html(nWords + " words");
		}else {
			$("#wordCnt").html(nWords + " words");
		}

	});
	
	
	$("#cvpitch").focus( function() {
		$("#cvpitchHelp").css({
			top: $("#cvpitch").offset().top - 295,
			left: $("#cvpitch").offset().left - 5,
			display: "block",
			opacity: 0
		}).animate({
			top: $("#cvpitch").offset().top - 315,
			opacity: 1
		}, "slow");
		$("#teltype1Div, #teltype2Div, #country").fadeOut();
	});

	$("#cvpitch").blur( function() {
		$("#cvpitchHelp").css({
			top: $("#cvpitch").offset().top - 305,
			opacity: 1
		}).animate({
			top: $("#cvpitch").offset().top - 325,
			opacity: 0
		}, "slow", function() {$(this).css("display","none");});
		$("#teltype1Div, #teltype2Div, #country").fadeIn();
	});
	
	
	$("#highesteducation").change(function () {
		showrows();
	}).change();
	
	$("input[name='availabilityOptions']").click(function () {
		showavailabilityoptions();
	}).change();
	
	
	$("#email").blur( function() {
		if( $("#email").val() != "" ) {	
			$("#checkemailcontainer").load( "ajax/checkemail.php?email="+$("#email").val() ).show("slow");
		}else {
			$("#checkemailcontainer").html("").hide("slow");
		}
	});
	
	
	$("#salary").keydown(function(e) {
    	// Allow only backspace and delete
    	if ( e.keyCode == 46 || e.keyCode == 8 ) {
    		// let it happen, don't do anything
    	}
    	else {
    		// Ensure that it is a number and stop the keypress
    		if (e.keyCode < 48 || e.keyCode > 57 ) {
    			e.preventDefault();	
    		}	
    	}
    });



	/* Handle submit */
	$("#registerCVForm").submit(function() {
		
		var formvalid = true;
	
		if( $("#fname").val() == "" ) {
			formvalid = false;
			$("#fnameLabel").attr("class", "labelerror");
		}else {
			$("#fnameLabel").attr("class", "label");
		}
	
		if( $("#lname").val() == "" ) {
			formvalid = false;
			$("#lnameLabel").attr("class", "labelerror");
		}else {
			$("#lnameLabel").attr("class", "label");
		}
/*		
		if( $("#registerCVForm #dob_day").val() == "00" || $("#registerCVForm #dob_month").val() == "00" || $("#registerCVForm #dob_year").val() == "0000") {
			formvalid = false;
			$("#dobLabel").attr("class", "labelerror");
			// $("#dob_error").html("Please select your full date of birth").show();
		}else {
			$("#dobLabel").attr("class", "label");
		}
*/
	
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var emailaddress = $("#email").val();
		if(reg.test(emailaddress) == false) {
			formvalid = false;
			$("#emailLabel").attr("class", "labelerror");
		}else if( $("#emailstatus").attr("class") == "red" ) {
			formvalid = false;
			$("#emailLabel").attr("class", "labelerror");
		}else {
			$("#emailLabel").attr("class", "label");
			$("#email_error").hide();
		}
	
	
		if( $("#confirm_email").val() == "" ) {
			formvalid = false;
			$("#confirm_emailLabel").attr("class", "labelerror");
		}else {
			$("#confirm_emailLabel").attr("class", "label");
			$("#confirm_email_errorLabel").hide();
		}
		
		if( $("#email").val() != $("#confirm_email").val() ) {
			formvalid = false;
			$("#confirm_emailLabel").attr("class", "labelerror");
			$("#confirm_email_errorLabel").show();
		}else {
			$("#confirm_emailLabel").attr("class", "label");
			$("#confirm_email_errorLabel").hide();
		}
		
		
		var passwordStr = $("#password").val();
		if( passwordStr == "" ) {
			formvalid = false;
			$("#passwordLabel").attr("class", "labelerror");
		}else {
			if( passwordStr.length < 8 ) {
				formvalid = false;
				$("#passwordLabel").attr("class", "labelerror");
			}else {
				$("#passwordLabel").attr("class", "label");
			}
		}
		
		if( $("#county").val() == "" ) {
			formvalid = false;
			$("#countyLabel").attr("class", "labelerror");
		}else {
			$("#countyLabel").attr("class", "label");
		}
		
		if( $("#country").val() == "" || $("#country").val() == "0" ) {
			formvalid = false;
			$("#countryLabel").attr("class", "labelerror");
		}else {
			$("#countryLabel").attr("class", "label");
		}
		
		if( $("#telno1").val() == "" ) {
			formvalid = false;
			$("#telno1Label").attr("class", "labelerror");
		}else {
			$("#telno1Label").attr("class", "label");
		}
		
		
		if( $("#jobtitle").val() == "" ) {
			formvalid = false;
			$("#jobtitleLabel").attr("class", "labelerror");
			$("#jobtitle_error").show();
		}else {
			$("#jobtitleLabel").attr("class", "label");
			$("#jobtitle_error").hide();
		}
/*
		if( $("#cvpitch").val() == "" ) {
			formvalid = false;
			$("#cvpitchLabel").attr("class", "labelerror");
			$("#cvpitch_error").show();
		}else {
			$("#cvpitchLabel").attr("class", "label");
			$("#cvpitch_error").hide();
		}
*/
		if( $("#cvfile").val() == "" ) {
			formvalid = false;
			$("#cvfileLabel").attr("class", "labelerror");
			$("#cvfile_error").html("Please select your CV to upload.").show();
		}else {
			$("#cvfileLabel").attr("class", "label");
			$("#cvfile_error").hide();
		}
		
		
		if( $("#careerlevel").val() == "0" ) {
			formvalid = false;
			$("#careerlevelLabel").attr("class", "labelerror");
		}else {
			$("#careerlevelLabel").attr("class", "label");
		}
		
		if( $("#experienceyear").val() == "0" ) {
			formvalid = false;
			$("#experienceyearLabel").attr("class", "labelerror");
		}else {
			$("#experienceyearLabel").attr("class", "label");
		}
		
		if( $("#highesteducation").val() == "0" ) {
			formvalid = false;
			$("#highesteducationLabel").attr("class", "labelerror");
		}else {
			$("#highesteducationLabel").attr("class", "label");
		}
		
		if( $("#highesteducation").val() != "0" ) {
					
			if( $("#highesteducation").val() > 1 && $("#highesteducation").val() < 4 ) {
				// Must select a year
				if( $("#qualendyear").val() == "0000" ) {
					formvalid = false;
					$("#qualendyearLabel").attr("class", "labelerror");
				}else {
					$("#qualendyearLabel").attr("class", "label");
				}
				
			}else if( $("#highesteducation").val() >= 4 ) {
				// Must select a year and enter a qualification name
				if( $("#qualendyear").val() == "0000" ) {
					formvalid = false;
					$("#qualendyearLabel").attr("class", "labelerror");
				}else {
					$("#qualendyearLabel").attr("class", "label");
				}
				
				if( $("#qualificationname").val() == "" ) {
					formvalid = false;
					$("#qualificationnameLabel").attr("class", "labelerror");
				}else {
					$("#qualificationnameLabel").attr("class", "label");
				}
				
			}
		}
	
	
		if( $("input[name='availabilityOptions']:checked").val() == "N" ) {
			if( $("#noticeperiod").val() == "0" ) {
				formvalid = false;
				$("#availabilityLabel").attr("class", "labelerror");
			}else {
				$("#availabilityLabel").attr("class", "label");
			}
		}else if( $("input[name='availabilityOptions']:checked").val() == "F" ) {
			availDateValid = true;
			if( $("#availabilityfrom_day").val() == "00" || $("#availabilityfrom_month").val() == "00" || $("#availabilityfrom_year").val() == "0000" ) {
				formvalid = false;
				$("#availabilityLabel").attr("class", "labelerror");
			}else {
				$("#availabilityLabel").attr("class", "label");
			}
		}
		
		
		
		
		var locationselected = false;
		$("#desired_location_box input[type='checkbox']").each(
			function() {
				if( $(this).is(':checked') )
					locationselected = true;
			}
		);
		if( locationselected == false ) {
			formvalid = false;
			$("#locationLabel").attr("class", "labelerror");
			$("#location_error").show();
		}else {
			$("#locationLabel").attr("class", "label");
			$("#location_error").hide();
		}
		
		
		var industryselected = false;
		$("#desired_industry_box input[type='checkbox']").each(
			function() {
				if( $(this).is(':checked') )
					industryselected = true;
			}
		);
		if( industryselected == false ) {
			formvalid = false;
			$("#industryLabel").attr("class", "labelerror");
			$("#industry_error").show();
		}else {
			$("#industryLabel").attr("class", "label");
			$("#industry_error").hide();
		}
		
		
		if( $("#agree").is(":checked") )
			$("#agreeLabel").attr("class", "normtext");			
		else
			$("#agreeLabel").attr("class", "error");
		
		
		if( formvalid == true ) {
			if( $("#agree").is(":checked") ) {
				$("#agreeLabel").attr("class", "normtext");
				return true;
			}else {	
				$("#agreeLabel").attr("class", "error");
				alert( "Please read and agree to our Terms and Conditions first." );
				return false;
			}
		}else {
			alert( "Please enter in all the mandatory infomation (highlighted in red)." );
			return false;
		}
		
	});

});
