/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

$(document).ready(function(){
	//global vars
	var form = $("#apply");
	var lead = $("#lead");
	var passportForm = $("#passportForm");
	var medicalForm = $("#medicalForm");
	var refForm = $("#referenceForm");
	var cvForm = $("#cvForm");
	var docForm = $("#doc");
	var nowForm = $("#now");
	var name = $("#name");
	var nameInfo = $("#nameInfo");
	
	var email1 = $("#email1");
	var emailInfo = $("#emailInfo");
	var email2 = $("#email2");
	var email2Info = $("#email2Info");
	var pass1 = $("#pass1");
	var pass1Info = $("#pass1Info");
	var pass2 = $("#pass2");
	var pass2Info = $("#pass2Info");
	var message = $("#message");
	
	 $('.Passport').hide();
	
	

 
    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');
		
     
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width()-20;
     
        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});
         
        //transition effect     
        $('#mask').fadeIn(200);    
        $('#mask').fadeTo("slow",0.8);  
     
        //Get the window height and width
		
        var winH = screen.height;
        var winW = screen.width;
              
        //Set the popup window to center
        $(id).css('top',  (winH/2)-($(id).height()/2)-50);
        $(id).css('left', winW/2-$(id).width()/2);
     
        //transition effect
        $(id).fadeIn(200); 
     
    });
     
    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });     
     
    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });         
     

	
$("input[id^=name]").blur(function(){
 var id = $(this);
var str=$(this).attr("name");


 if(id.val().length < 3){
			id.addClass("error");
			$("span[id^="+str+"]").text("Must contain minimum of 3 characters");
			
			$("span[id^="+str+"]").addClass("error");
			
			return false;
		}
		//if it's valid
		else{
			id.removeClass("error");
			$("span[id^="+str+"]").text("");
			$("span[id^="+str+"]").removeClass("error");
			return true;
		}
});
	
	//On blur
	//name.blur(validateName);
	
	email1.blur(validateEmail);
	email2.blur(validateEmail2);
	pass1.blur(validatePass1);
	pass2.blur(validatePass2);
	name.blur(checkName);
	//On key press
	//name.keyup(validateName);
	
	pass1.keyup(validatePass1);
	pass2.keyup(validatePass2);
	message.keyup(validateMessage);
	//On Submitting
	
	docForm.submit(function(){
	
	
	openDiv($('select[name=FileSelection]').val());
	
	return false;
	});
	
	
	refForm.submit(function(){
	if(validateEmail3()){
	return true;
	}
	
	return false;
	});
	
	passportForm.submit(function(){
	if(fileCheck2()){
	return true;
	}
	
	return false;
	});
	
	cvForm.submit(function(){
	
	
	return true;
	});
	
	medicalForm.submit(function(){
	
	
	return true;
	});
	
	form.submit(function(){
	
	
	checkDropdown();
	if(!$('#accept').is(':checked')){
	alert("You must agree to the terms and conditions");
	return false;
	}
	
	
	
	
	
		if(validateEmail() & checkName() & fileCheck() & checkNumber()){
		return true;
		}
		alert("Sections of the form have not been completed");
			return false;
			
			
	});
	
	
	
	
	
	
	lead.submit(function(){
	
	
	
	
		if(checkRate()){
		return true;
		}
		alert("You must provide a day rate");
			return false;
			
			
	});
	
	function openDiv(id){
	
	
	var id = '#'+id;
		
     //alert(id);
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width()-20;
     
        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});
         
        //transition effect     
        $('#mask').fadeIn(200);    
        $('#mask').fadeTo("slow",0.8);  
     
        //Get the window height and width
		
        var winH = screen.height;
        var winW = screen.width;
              
        //Set the popup window to center
        $(id).css('top',  (winH/2)-($(id).height()/2)-50);
        $(id).css('left', winW/2-$(id).width()/2);
     
        //transition effect
        $(id).fadeIn(200); 
	
	}
	
	
	function fileCheck(){
	var file='file';
	var ext;
	$("input[id^="+file+"]").each(function(){
	var s=$(this).val().split('.').pop().toLowerCase();
	
		if(s){
			ext =$(this).val().split('.').pop().toLowerCase();
		}
	});
		//var ext = $('#file').val().split('.').pop().toLowerCase();
		if(!ext){
		return false;
		}
if($.inArray(ext, ['doc','txt','docx','rtf','pdf']) == -1) {
    alert('Your CV must be a word file or PDF');
	return false;
}
	return true;
	}
	
	function fileCheck2(){
	var file='passportFile';
	$("input[id^="+file+"]").each(function(){
		//alert($(this).attr("id"));
	});
		var ext = $('#file').val().split('.').pop().toLowerCase();
		//alert(ext);
if($.inArray(ext, ['doc','txt','docx','rtf','pdf','jpg','png','gif']) == -1) {
    alert('Your CV must be of type doc,docx,pdf,jpg,png or gif');
	return false;
}
	return true;
	}
	
	function checkNumber(){
	var check=true;
	$("input[id^=tel]").each(function(){
	
	 var id = $(this);
	 
var str=$(this).attr("name");
	
	 if(id.val().length !=''){
	 
	  var value = id.val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    var intRegex = /^\d+$/;
    if(!intRegex.test(value)) {
        id.addClass("error");
			$("span[id^="+str+"]").text("Only numbers accepted");
			
			$("span[id^="+str+"]").addClass("error");
			
			check=false;
    }else{
	id.removeClass("error");
			$("span[id^="+str+"]").text("");
			$("span[id^="+str+"]").removeClass("error");
			
	
	}
			
		}//if it's valid
		else{
			id.addClass("error");
			$("span[id^="+str+"]").text("Contact number required");
			
			$("span[id^="+str+"]").addClass("error");
			
			check= false;
		}
	
	});
return check;
	}
	
	function checkRate(){
	var check=true;
	$("input[id^=rate]").each(function(){
	
	 var id = $(this);
var str=$(this).attr("name");
	
	 if(id.val().length !=''){
	 
	  var value = id.val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    var intRegex = /^\d+$/;
    if(!intRegex.test(value)) {
        id.addClass("error");
			$("span[id^="+str+"]").text("Only numbers accepted");
			
			$("span[id^="+str+"]").addClass("error");
			
			check=false;
    }else{
	id.removeClass("error");
			$("span[id^="+str+"]").text("");
			$("span[id^="+str+"]").removeClass("error");
			
	
	}
			
		}//if it's valid
		else{
			id.addClass("error");
			$("span[id^="+str+"]").text("Rate required");
			
			$("span[id^="+str+"]").addClass("error");
			
			check= false;
		}
	
	});
return check;
	}
	
	function checkName(){
	
	var check=true;
	$("input[id^=name]").each(function(){
 var id = $(this);
 //if(id.get(0).form.id==me);
var str=$(this).attr("name");


 if(id.val().length < 3){
			id.addClass("error");
			$("span[id^="+str+"]").text("Must contain minimum of 3 characters");
			//alert(str);
			$("span[id^="+str+"]").addClass("error");
			
			check=false;
		}
		//if it's valid
		else{
			id.removeClass("error");
			$("span[id^="+str+"]").text("");
			$("span[id^="+str+"]").removeClass("error");
			
		}
});
return check;
	}
	
	function checkDropdown(){
	var check=true;
	$("select[id^=drop]").each(function(){
 var id = $(this);
var str=$(this).attr("name");
str= str.substr(0,str.length - 2);


 if(id.attr("selectedIndex")==0){
			id.addClass("error");
			//$("span[id^="+str+"]").text("Please select " + str);
			
			$("span[id^="+str+"]").addClass("error");
			
			check=false;
		}
		//if it's valid
		else{
			id.removeClass("error");
			$("span[id^="+str+"]").text("");
			$("span[id^="+str+"]").removeClass("error");
			
		}
});
return check;
	}
	function validateEmail3(){
		
		//testing regular expression
		var a = $("#email3").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)){
			email1.removeClass("error");
			emailInfo.text("Valid E-mail Thanks");			
			emailInfo.removeClass("error");
			return true;
		}
		//if it's NOT valid
		else{
			email1.addClass("error");
			emailInfo.text("Type a valid e-mail please");			
			emailInfo.addClass("error");			
			return false;
		}
	}
	function validateEmail(){
		
		//testing regular expression
		var a = $("#email1").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)){
			email1.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{
			email1.addClass("error");
			emailInfo.text("Type a valid e-mail please");			
			emailInfo.addClass("error");			
			return false;
		}
	}
	
	function validateEmail2(){
		var a = $("#email1");
		var b = $("#email2");
		//are NOT valid
		//alert(email1.val());
		if( email1.val() != email2.val() ){
			email2.addClass("error");
			email2Info.text("Email doesn't match!");
			email2Info.addClass("error");
			return false;
		}
		//are valid
		else{
			email2.removeClass("error");
			email2Info.text("Match");
			email2Info.removeClass("error");
			return true;
		}
	}
	
	function validateName(){
	
		//if it's NOT valid
		if(name.val().length < 3){
			name.addClass("error");
			nameInfo.text("Must contain minimum of 3 characters");
			nameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			name.removeClass("error");
			nameInfo.text("");
			nameInfo.removeClass("error");
			return true;
		}
	}
	
	function validateMessage(){
		//it's NOT valid
		if(message.val().length < 10){
			message.addClass("error");
			return false;
		}
		//it's valid
		else{			
			message.removeClass("error");
			return true;
		}
	}
	function validatePass1(){
		var a = $("#password");
		var b = $("#password2");

		//it's NOT valid
		if(pass1.val().length <5){
			pass1.addClass("error");
			pass1Info.text("Ey! Remember: At least 5 characters: letters, numbers and '_'");
			pass1Info.addClass("error");
			return false;
		}
		//it's valid
		else{			
			pass1.removeClass("error");
			pass1Info.text("At least 5 characters: letters, numbers and '_'");
			pass1Info.removeClass("error");
			validatePass2();
			return true;
		}
	}
	function validatePass2(){
		var a = $("#password1");
		var b = $("#password2");
		//are NOT valid
		if( pass1.val() != pass2.val() ){
			pass2.addClass("error");
			pass2Info.text("Passwords doesn't match!");
			pass2Info.addClass("error");
			return false;
		}
		//are valid
		else{
			pass2.removeClass("error");
			pass2Info.text("Confirm password");
			pass2Info.removeClass("error");
			return true;
		}
	}
});
