
var form =document.forms['form_nuffieprofile'];
var errors =new Array();

$(document).ready( function(){

	//----------ADD LANGUAGE SPOKEN-----------//
	$(".add_langspoken").click( function(){
		if( $(".lang_spoken_td").length <5 ){
			var formdata =$("tr.lang_spoken_td:first").html();
			
			$("tr.lang_spoken_td:last").after( '<tr class="lang_spoken_td">'+ formdata +'</tr>' );
			//empty last row's text and select input
			$("input[name=lang_spoken]","tr.lang_spoken_td:last").val('');
			$("select[name=lang_spoken_prof]","tr.lang_spoken_td:last").val('');
			$(".txtlabel","tr.lang_spoken_td:last").text('');
			//include 'remove' link to newly added row
			$(".lang_spoken_lbl","tr.lang_spoken_td:last").append("<br \/>[<a href=\"javascript: void(0)\" class=\"langspoken_remove\">remove<\/a>]");
		
			//----------------REMOVE LANGUAGE SPOKEN ROW-----------------//
			$(".langspoken_remove").click( function(){
				$(this).parent("label").parent("td").parent("tr",".lang_spoken_td").remove();
			});
		
		}
	});
	
	//---------ADD LANGUAGE WRITTEN----------// 
	$(".add_langwritten").click( function(){
		if( $(".lang_written_td").length <5 ){ 
			var formdata =$("tr.lang_written_td:first").html();
			
			$("tr.lang_written_td:last").after( '<tr class="lang_written_td">'+ formdata +'</tr>' );
			//empty last row's text and select input
			$("input[name=lang_written]","tr.lang_written_td:last").val('');
			$("select[name=lang_written_prof]","tr.lang_written_td:last").val('');
			$(".txtlabel","tr.lang_written_td:last").text('');
			//include 'remove' link to newly added row
			$(".lang_written_lbl","tr.lang_written_td:last").append("<br \/>[<a href=\"javascript: void(0)\" class=\"langwritten_remove\">remove<\/a>]");
			
			//--------------REMOVE LANGUAGE WRITTEN ROW------------------//
			$(".langwritten_remove").click( function(){
				$(this).parent("label").parent("td").parent("tr",".lang_written_td").remove();
			}); 
		}
	});


	//-----------ADD JOB SKILLS ROW----------//
	$(".addskills").click( function(){
		if( $(".jobskill_td").length <5 ){ 
			var formdata =$("tr.jobskill_td:first").html();
			
			$("tr.jobskill_td:last").after( '<tr class="jobskill_td">'+formdata+'</tr>');
			//empty the last jobskill row's text and select input
			$("input[name=jobskill_name]","tr.jobskill_td:last").val('');
			$("select[name=jobskill_exp]","tr.jobskill_td:last").val('');
			$("select[name=jobskill_prof]","tr.jobskill_td:last").val('');
			$(".txtlabel","tr.jobskill_td:last").text('');
			//rename the last jobskill row's text label
			$(".jobskill_lbl","tr.jobskill_td:last").append("<br \/>[<a href=\"javascript: void(0)\" class=\"jobskill_remove\">remove<\/a>]");
			
			//----------------- REMOVE JOB SKILL ROW ------------------------//
			$(".jobskill_remove").click( function(){
				$(this).parent("label").parent("td").parent("tr",".jobskill_td").remove();
			});
		}	
	});
	
	//-----------ADD RECENT TRAININGS ROW-------//
	$(".addtrainings").click( function(){
		if( $(".recent_training_td").length <5 ){ 
			var formdata=$("tr.recent_training_td:first").html();
			$("tr.recent_training_td:last").after( '<tr class="recent_training_td">'+ formdata +'</tr>' );
			//empty the last recent training's text input
			$("input[name=recent_training_loc]","tr.recent_training_td:last").val('');
			$("input[name=recent_training_title]","tr.recent_training_td:last").val('');
			$("select[name=recent_training_start_day]","tr.recent_training_td:last").val('');
			$("select[name=recent_training_start_month]","tr.recent_training_td:last").val('');
			$("select[name=recent_training_start_year]","tr.recent_training_td:last").val('');
			$("select[name=recent_training_end_day]","tr.recent_training_td:last").val('');
			$("select[name=recent_training_end_month]","tr.recent_training_td:last").val('');
			$("select[name=recent_training_end_year]","tr.recent_training_td:last").val('');
			$(".txtlabel","tr.recent_training_td:last").text('');
			//rename the last recent training's text label
			$(".recent_training_lbl","tr.recent_training_td:last").append( "<br \/>[<a href=\"javascript: void(0)\" class=\"recent_training_remove\">remove<\/a>]");
			
			//-------------------REMOVE RECENT TRAINING ROW--------------//
			$(".recent_training_remove").click( function(){
				$(this).parent("label").parent("td").parent("tr",".recent_training_td").remove();
			});
		}	
	});
	
	
	//------------------ character counting for textarea
	$( form.current_add ).keyup(
		function(){
			if(  parseInt( $("#current_add_charcount").html() ) >=190 ){
				$(this).val( $(this).val().substring(0,190) );
			}
			$("#current_add_charcount").html( $(this).val().length );
		}
	).keyup();
	
	$( form.permanent_add ).keyup(
		function(){
			if(  parseInt( $("#permanent_add_charcount").html() ) >=190 ){
				$(this).val( $(this).val().substring(0,190) );
			}
			$("#permanent_add_charcount").html( $(this).val().length );
		}
	).keyup();
	
	$( form.work_desc1 ).keyup(
		function(){
			if( parseInt( $("#work_desc1_charcount").html() ) >=250 ){
				$(this).val( $(this).val().substring(0,250) );
			}
			$("#work_desc1_charcount").html( $(this).val().length );
		}
	).keyup();
	
	$( form.work_desc2 ).keyup(
		function(){
			if( parseInt( $("#work_desc2_charcount").html() ) >=250 ){
				$(this).val( $(this).val().substring(0,250) );
			}
			$("#work_desc2_charcount").html( $(this).val().length );
		}
	).keyup();
	
	$( form.work_reason ).keyup(
		function(){
			if( parseInt( $("#work_reason_charcount").html() ) >1000 ){
				$(this).val( $(this).val().substring(0,1000) );
			}
			$("#work_reason_charcount").html( $(this).val().length );
		}
	).keyup();
	
	
	$(form).submit( function(){
		errors.splice(0, errors.length);  //clear array					 
		$("#errordiv").html( "").hide();
		
		//---------------- VALIDATE INPUT ------------------------------------------------//
	
		if( $(form.fullname).val() == "")
			errors.push("<a href='#q1a'>Please provide Full Name</a>");	
		if( $(form.job_title).val() == "")
			errors.push("<a href='#q2a'>Please provide Position Title</a>");
		if( $(form.dob_day).val() == "" || $(form.dob_month).val() == "" || $(form.dob_year).val() == "" )
			errors.push("<a href='#q3a'>Please provide full D.O.B.</a>");
		if(  $("input[name=ic_passport]").length >0 && $(form.ic_passport).val() == "")
			errors.push("<a href='#q4a'>Please provide I.C./Passport No.</a>");		
		if( $(form.gender).val() == "")	
			errors.push("<a href='#q5a'>Please provide Gender</a>");
		if( $(form.race).val() == "")	
			errors.push("<a href='#q6a'>Please provide Race</a>");
		if( $(form.maritialstat).val() == "" )
			errors.push("<a href='#q7a'>Please provide Maritial Status</a>");
		if( $(form.religion).val() == "" )
			errors.push("<a href='#q8a'>Please provide Religion</a>");
		if( $(form.nationality).val() == "" )
			errors.push("<a href='#q9a'>Please provide Nationality</a>");
		if( $(form.residentialstat).val() == "" )
			errors.push("<a href='#q10a'>Please provide Residential Status</a>");
			
		if( $(form.current_add).val() == "" )
			errors.push("<a href='#q1b'>Please provide Current Residential Address</a>");		
		if( $(form.permanent_add).val() == "" )
			errors.push("<a href='#q2b'>Please provide Permanent Residential Address</a>");
		if( $(form.tel_home).val() == "" && $(form.tel_mobile).val() == "" )
			errors.push("<a href='#q3b'>Please provide either Home or Mobile Contact No.</a>");	
			
		if( $(form.email).val() == "" )
			errors.push("<a href='#q5b'>Please provide E-mail</a>");
		else if( !isValidEMail( $(form.email).val() ) )		
			errors.push("<a href='#q5b'>Please provide valid E-mail</a>");	
				
		if( $(form.edu_lvl1).val() =="" )
			errors.push("<a href='#q1c'>Please provide Highest Education Qualification</a>");
		if( $(form.studyfield1).val() =="" )
			errors.push("<a href='#q2c'>Please provide Highest Education's Field of Study</a>");
		if( $(form.majoring1).val() =="" )
			errors.push("<a href='#q3c'>Please provide Highest Education's Majoring Course</a>");
		if( $(form.cgpa1).val() =="" )
			errors.push("<a href='#q4c'>Please provide Highest Education's Grade achieved</a>");
		if( $(form.edu_name1).val() =="" )
			errors.push("<a href='#q5c'>Please provide Education Instituition's name under Highest Education</a>");
		if( $(form.edu_location1).val() =="" )
			errors.push("<a href='#q6c'>Please provide Education Instituition's location under Highest Education</a>");
		if( $(form.grad_day1).val() =="" || $(form.grad_month1).val() =="" || $(form.grad_year1).val() =="" )
			errors.push("<a href='#q7c'>Please provide complete Graduation Date under Highest Education</a>");
			
		//compile data from language spoken row(s) into one hidden textfield
		var lang_spoken_compile =new Array();
		var lang_spoken_prof_compile =new Array();
		
		$(".lang_spoken_td").each( function(){
			if( $(this).find("input").val() !="" )
				lang_spoken_compile.push( $(this).find("input").val() );
			if( $(this).find("select").val() !="" )	
				lang_spoken_prof_compile.push( $(this).find("select").val() );
		});
		if( lang_spoken_prof_compile.length != lang_spoken_compile.length ){
			errors.push("<a href='#q1e'>Please complete each Language Spoken and Proficiency level stated</a>");
			
		} else if( lang_spoken_compile.length < 1 && lang_spoken_prof_compile.length < 1 ){
			errors.push("<a href='#q1e'>Please provide at least one Language Spoken and Proficiency level");
			
		} else{
			$(form.lang_spoken_all).val( lang_spoken_compile.join("||") );
			$(form.lang_spoken_prof_all).val( lang_spoken_prof_compile.join("||") );	
		}
				
		//compile data from language written row(s)
		var lang_written_compile =new Array();
		var lang_written_prof_compile =new Array();
			
		$(".lang_written_td").each( function(){
			if( $(this).find("input").val() !="" )
				lang_written_compile.push( $(this).find("input").val() );
			if( $(this).find("select").val() !="" )		
				lang_written_prof_compile.push( $(this).find("select").val() );
		});	
		
		if( lang_written_prof_compile.length != lang_written_compile.length ){
			errors.push("<a href='#q2e'>Please complete each Language Written and Proficiency level stated</a>");
			
		} else if( lang_written_compile.length < 1 && lang_written_prof_compile.length < 1 ){
			errors.push("<a href='#q2e'>Please provide at least one Language Written and Proficiency level</a>");
			
		} else{
			$(form.lang_written_all).val( lang_written_compile.join("||") );
			$(form.lang_written_prof_all).val( lang_written_prof_compile.join("||") );	
		}
		
		if( $(form.work_reason).val() == "" )
			errors.push("<a href='#q1g'>Please tell us why you like to work in Nuffnang</a>");
			
		//compile data from job skill row(s) into one hidden textfield
		var jobskill_name_compile =new Array();
		var jobskill_exp_compile =new Array();
		var jobskill_prof_compile =new Array();
		
		$(".jobskill_td").each( function(){
			if( $(this).find("input").val() != ""){
			jobskill_name_compile.push( $(this).find("input").val() );
			jobskill_exp_compile.push( $(this).find("select:first").val() );
			jobskill_prof_compile.push( $(this).find("select:last").val() );
			}
		});
		
		if( jobskill_name_compile.length >0 )
			$(form.jobskill_name_all).val( jobskill_name_compile.join("||") );
		if( jobskill_exp_compile.length >0 )
			$(form.jobskill_exp_all).val( jobskill_exp_compile.join("||") );
		if( jobskill_prof_compile.length >0 )
			$(form.jobskill_prof_all).val( jobskill_prof_compile.join("||") );
		
		//compile data from recent training row(s) into one hidden textfield 				
		var recent_training_startdate_compile =new Array();
		var recent_training_enddate_compile =new Array();
		var recent_training_loc_compile =new Array();
		var recent_training_title_compile =new Array();
		
		$(".recent_training_td").each( function(){
			if( $(this).find("input:first").val() != "" ){
			recent_training_startdate_compile.push( $(this).find("select:first").next().next().val() + "/" 
			+ $(this).find("select:first").next().val() + "/" 
			+ $(this).find("select:first").val() );
			recent_training_enddate_compile.push( $(this).find("select:first").next().next().next().next().next().val() + "/" 
			+ $(this).children("td").find("select:first").next().next().next().next().val() + "/" 
			+ $(this).children("td").find("select:first").next().next().next().val() );
			recent_training_loc_compile.push( $(this).find("input:first").val() );
			recent_training_title_compile.push( $(this).find("input:last").val() );
			}
		});
		
		if( recent_training_loc_compile.length >0 )
			$(form.recent_training_loc_all).val( recent_training_loc_compile.join("||") );
		if( recent_training_title_compile.length >0 )	
			$(form.recent_training_title_all).val( recent_training_title_compile.join("||") );
		if( recent_training_startdate_compile.length >0 )
			$(form.recent_training_startdate_all).val( recent_training_startdate_compile.join("||") );
		if( recent_training_enddate_compile.length >0 )
			$(form.recent_training_enddate_all).val( recent_training_enddate_compile.join("||") );
		
		//if no errors, submit form
		if( errors.length >0){
			showErrors( errors);
			return false;
			
		}else return true;
		
	});	
	
	function showErrors( msg){
		var html ="<div class='error'><strong>The following errors occured:</strong><br /><ul>";
		for( var i=0; error=msg[i]; i++){
			html +="<li>"+ error +"</li>";
		}
		html +="</ul><br /><a class='closebox' href='javascript: void(0)'>[close]</a></div>";
		
		$("#errordiv").html( html).slideDown('normal');
	
		$( ".closebox").click( function(){
			$(this).parent().slideUp('slow',function(){
				$(this).remove();
			});
			$("#errordiv").hide();
		});
	}

	function isValidEMail( str){
		
		if( str.search(/^([\w-._])+@([\w_-]+)(.[\w_-]+)?.(com|net|org|info|biz|tv|us|cc|name|bz|edu|de|be|co|at|tk|sg|my|au|ph|fr)(.[a-zA-Z]{2})?$/) == -1){
			return false;
		}else { return true; }	
	}	
});
