// JavaScript Document

//date picker inputs
var regfieldname=/_[\d]+$/;
var regfieldtype=new RegExp(/type-(\w)/);
	function log() {
	if (window.console && window.console.log)
		window.console.log('[cycle] ' + Array.prototype.join.call(arguments,' '));
	
	
}

				
				
				jQuery.fn.CharCount=function(options){
				
				var defaultSettings={
					maxChar:200,
					pretext:allowed_char
				};
				
				var settings=$.extend(options,defaultSettings);
				var group=this;
				
				var obj,name;
				checkChars=function(){
						name=$(this).attr('name');
						var charcount=$('#count_'+name);
						var text = $(this).val(); 
						var textlength = text.length;
							
							
							
							visible=settings.maxChar-textlength;
							if(textlength>settings.maxChar){
							$(this).val(text.substr(0,settings.maxChar));
							visible=0;
							}
							//else
							charcount.text(settings.pretext+' '+visible);	
															 
															 
				 }
				group.each(function(o,i){
					
					obj=$(this);
					name=$(this).attr('name');
					var charcount=$('#count_'+name);
					var charlength=this.value.length;
					charcount.text(settings.pretext+' '+parseInt(settings.maxChar-charlength));
					
					obj.attr('maxlength',settings.maxChar)
						.keyup(checkChars)
						.focus(checkChars)
						.blur(checkChars)
						.change(checkChars);		
									
									
				});
				 return group;
			}
			
				//odd even
				$.fn.odd_even=function(){
						var group=$(this);
					group.each(function(i){
							
					$ols=$(this).children('li.set-wrap').removeClass('even')
							.filter(':visible:even')
							
							.addClass('even')
							;
							
													  });
				}
			
			//EXPERIENCE RADIO BUTTONS
			var startExp=function(){
				
				
			var hasExp=$('#exp-choice input:checked').attr('value');
			
				
			if(hasExp==1){
				//$('#yes_exp').attr('checked','checked');
			}
			else{
				//$('#no_exp').attr('checked','checked');
				
				$('li.set-wrap:gt(0)','#step43').hide();
			}
			}
			
			//CLEAR INPUTS TO BE DELETED
			var clearInputs=function($set,hide){
				
				
				$set.filter('.required').removeClass('filled').addClass('not-filled')
				
				
				
				$inputs_to_clear=$set.find('input[name!=set_id],textarea');
				$inputs_to_clear.each(function(){
									  
									 
						 if($(this).attr('type')!='radio'&&$(this).attr('type')!='checkbox')
									 	 this.value='';
							 else
							 if($(this).attr('type')!='checkbox')//checkboxes simply do nothing
									$(this).attr('checked','');
										
									  });
				$inputs_to_clear=$set.find('select');
				$inputs_to_clear.each(function(){
						$(this).find('option:first').attr('selected', 'selected');
									  
									  
									  });
				
				if(hide){
					$set.parent().parent().hide();
					var cstep=parseInt($('.cur-step','#steps').attr('rel'));
					
					$('.cv-set-inputs','#step'+(++cstep)).odd_even();
				}
				
			}
			
			jQuery.fn.datePicker=function(options){
			//var datePicker=function(){
				
				$('.date-picker').each(function(){
					
					
					reg=/^company_/;
					reg2=/^institution_/;
					
					
					
					name=$(this).attr('name');
					if(reg.test(name)||reg2.test(name))
						format='yy-mm';
					else
						format='yy-mm-dd';
					hidedays=function(){$('.ui-datepicker','#ui-datepicker-div').hide();}
					
					var beforeShow_fn=beforeShowDay_fn=null;
					if($(this).attr('name')!='date_of_birth_1')
						{
							beforeShow_fn= function(input){ 
									$('#ui-datepicker-div').addClass('special');																														 									};
							beforeShowDay_fn= function(date)  {
							
								if(date.getDate()!=1)
									return [false,'no-day',''];
								return [true,'yes-day',''];
							//.find('td[onclick]:eq(0)').addClass('first');	
							onSelect_fn=function(){
								
								}
						}
							
						}
					//log($(this).attr('name'),typeof beforeShow_fn,typeof beforeShowDay_fn);
					$(this).removeClass('hasDatepicker').datepicker({yearRange:'-100:+100',dateFormat:format, beforeShow:beforeShow_fn,  beforeShowDay:beforeShowDay_fn
});
					
					//$.datepicker._destroyDatepicker($(this));
				});
			 return this;
			}
			
			
			
			//COMPANY TO MOD.
			var $text;
			
			jQuery.fn.company_to=function(){
			//var company_to=function(){
				$('input[name^=company_to]').each(function(){
							 
							$text=$(this);
							
							$check=$text.next().next().next().next();
							
							if($check.attr('checked')) {
								
								$text.attr('value','')
								.siblings('.date-select')
								.find('option:eq(0)').attr('selected','selected');
								
								hideDateInputs($text);
								}
							else
							showDateInputs($text);
							//$text.show();
							
							$check.click(function(){
							checkname=$(this).attr('name');
							textname=checkname.replace('current_','');
							
							$text2=$('input[name^='+textname+']');
							//$text2.toggle();
							if($(this).attr('checked')) {
								
								$text2.attr('value','')
								.siblings('.date-select')
								
								.find('option:eq(0)').attr('selected','selected');
								;
								hideDateInputs($text2);
								}
							else
							//$text2.show();	
							showDateInputs($text2);
							});
							/*$check.change(function(){
							
							if($(this).attr('checked')) {$text.attr('value','').hide();}
							else
							$text.show();					  
							});*/
								 });
				 return this;
			}
			 
			
			//END COMPANY TO MOD.
			$.fn.tagName = function() {
				return this.get(0).tagName;
			}
			
			$.fn.driving = function() {
				$input1=$('input.radio.driving_license:eq(0)');
				$input2=$('input.radio.driving_license:eq(1)');
				if($input1.is(':checked'))
					$('li.driving_license_loc-li').show();
				else{
					$('input[name=driving_license_loc_1]').attr('value','');
					$('li.driving_license_loc-li').hide();
				}
				$input1.click(function(){
										
							if($(this).is(':checked'))
								$('li.driving_license_loc-li').show();
							
									   });
				$input2.click(function(){
										
							if($(this).is(':checked')){
								$('input[name=driving_license_loc_1]').attr('value','');
								$('li.driving_license_loc-li').hide();
							}
									   });
				return this;
			}
			
			var hideDateInputs=function($theinput){
				
				
				$theinput.hide().siblings('.ymd').hide();
				}
			var showDateInputs=function($theinput){
				
				$theinput.show().siblings('.ymd').show();
				
				}
				
			$.fn.gradate = function() {
				$check=$('input.checkbox.current_institution_graduation_date');
				$check.each(function(){
				$c=$(this);	
				var rel=$c.attr('rel');
				var $theinput=$('input[name='+rel+']');
				
				if($c.is(':checked')){
					$theinput.attr('value','2100-01-01');
					hideDateInputs($theinput);
				}
				else{
					$theinput.attr('value','');
					showDateInputs($theinput);
				}
				$c.click(function(){
					$theinput=$('input[name='+$(this).attr('rel')+']');
					//console.log($(this).attr('rel'));
					if($(this).is(':checked')){
						$theinput.attr('value','2100-01-01');
						hideDateInputs($theinput);
					}
					else{
					$theinput.attr('value','')
					.siblings('.ymd').find('option:selected').attr('selected','');
					showDateInputs($theinput);
					}
							
									   });				 
									 
									 
					});
				
				
				return this;
			}
			
			var fixie_req=function(){
			  if (!window.XMLHttpRequest) {
				  
				
					
				$('.required.not-filled','#cv_form').addClass('required-not-filled')
													.removeClass('required-filled')
													;
					
					
				$('.required.filled label','#cv_form').addClass('required-filled')
														.removeClass('required-not-filled')
														;
													
				
			  }
			}
			
//READY			
$(document).ready(
			
		function() {
			
			$("#apply-job").click(function(){
										   $("#cv-list").slideToggle(400);});
			
			/////////////////////////////////////////////*CV FORM */
			$cv_id=$('#cv_id').attr('value');
			
			$phpfile='_ajax-cv.php';
			if(BO)
				$phpfile='../_ajax-cv.php';
			
			//SET UP FUNCTIONS HERE
			
			$(this)
			.company_to()
			.datePicker()
			.driving()
			.gradate();
			startExp();
			//$('li.required.not-filled .error.req','#cv_form').show();
			$('.textarea.objective','#cv_form').CharCount({});
			
			//SPELLING
			var spelling= function (){
	
errorhover=function(){
$('.error').hover(function(){
				  $(this).find('.suggestions').fadeIn('normal');
				  
					   }
				,
					function(){
						  $(this).find('.suggestions').fadeOut('normal');
							  }
					 );	
}

$('.spelling').livequery('click',function(e){
		source=$(this);		
		source.parent().addClass('top');	
		if(!source.hasClass('loader')){
		
		
		$inputtocheck=$(this).attr('rel');
		
		$textarea=$('textarea[name='+$inputtocheck+']');
		texttocheck=$textarea.attr('value');
		
		if(texttocheck.length>0){
				source.addClass('loader');
				file='_ajax-spelling.php';
				lang=$('input[name=lang]').attr('value');
				
		h=(screen.height);
		w=(screen.width);
		t=(h/2)-175;
		l=(w/2)-175;
		if(typeof results!='undefined')
			results.close()
		results=window.open(file+'?popup=1&lang='+lang+'&text='+texttocheck,'results',"status=0,toolbar=0,location=0,menubar=0,resizable=0,scrollbars=0,height=550,width=550,top="+t+",left="+l);
		source.removeClass('loader');
		return false;
				$.get(file,{'text':texttocheck,'lang':lang},function(data){
						$('#'+$inputtocheck+'-spelling .spr').html(data).parent().show(300);
						errorhover();
						$('#'+$inputtocheck+'-spelling .close').click(
							  function(){
								  $(this).parent().hide(500);
								  source.parent().removeClass('top');	
									 });
						source.removeClass('loader');
				 });
			}
		}
		
		return false;
		
	});

			}
			spelling();
			//END SPELLING
			
			
			var showAddFormToggle=function($current){
				if($current=='3')
						$('#add-new-form').show();
						else
							$('#add-new-form').hide();	
				
			};
			
			function jsstr(s) {
				if(typeof s!='undefined'){
				s = s.replace(/\r\n|\r/g, '\n');
				s = s.replace(/\\/g, '\\\\');
				s = s.replace(/'/g, '\\\'');
				s = s.replace(/\n/g, '\\n');
				}
				else
					s='';
				return  s ;
				}

		var prepareDateInput=function($input){
						$year=$input.next();
								$yearvalue=$year.find('option:selected').attr('value');
						$month=$year.next();
								$monthvalue=$month.find('option:selected').attr('value');
						$day=$month.next();
								$dayvalue=$day.find('option:selected').attr('value');
								if(!$dayvalue&&$yearvalue&&$monthvalue)
										$dayvalue='1';
								
								if(!$yearvalue&&!$monthvalue)
										$dayvalue=null;
											
						$datearray=Array();
							$yearvalue?$datearray.push($yearvalue):'';
							$monthvalue?$datearray.push($monthvalue):'';
							$dayvalue?$datearray.push($dayvalue):'';
							
							$datevalue='';
							if($datearray.length==3)			
							$datevalue=$datearray.join('-');
							
								
							if($input.attr('value')!='2100-01-01')
								$input.attr('value',$datevalue);	
							var input_name=$input.attr('name');
							
							if(input_name&&$input.attr('value')==''&&input_name.match(/^company_to/))
								$input.attr('value','undefined');
								
							/*if(input_name&&input_name.match(/^institution/))
								alert($input.attr('value'));*/
							
			}
			var prepareData=function($Inputs){
				$cv_id=$('#cv_id').attr('value');
				$js_id=$('#js_id').attr('value');
				$cv_type=$('#cv_type input:checked').attr('value');
				$is_exp=$('#exp-choice input:checked').attr('value');
				$data='';
				
				$Inputs.each(function(i){
								
								
								$input=$(this).find('label').next();
								
								
								$fieldname=$input.attr('name');
								
								tag=($input.tagName());
								if(tag=='LABEL'){
									$input=$input.find('input');
									$fieldname=$input.attr('name');
									
									}
								if($input.attr('type')=='radio'){
									$input=$(this).find('input:checked');
									if($fieldname=='can_start_work_1'){
										if($input.attr('rel')!=''){
											$text=$input.attr('rel');
											$textinput=$('input[name='+$text+']');
											prepareDateInput($textinput);
											$input.attr('value',$textinput.attr('value'));
											
										}
									}
									//console.log($input.attr('value'));
								}
								
								if($(this).hasClass('type-date')){
									
									prepareDateInput($input);
									
									//log($fieldname+': '+$datevalue);
								}
								
								
								$fieldvalue=$input.attr('value');
								if(tag=='TEXTAREA'||true){
									
									$fieldvalue=jsstr($fieldvalue);
									
									
								}
								
								
								if($fieldname=='preference_job_category_all'){
									$fieldvalue='';$flag=0;
									
									$fieldname='preference_job_category_1';
									$('select[name='+$fieldname+'] option').each(function(){
									 if($flag!=0)sep='-';
									else
									sep='';
									$flag++;
									$fieldvalue+=sep+$(this).attr('value');
									 });
									
								}
								
								if($fieldname=='preference_job_location_all'){
									$fieldvalue='';$flag=0;
									
									$fieldname='preference_job_location_1';
									$('select[name='+$fieldname+'] option').each(function(){
									if($flag!=0)sep='-';
									else
									sep='';
									$flag++;
									$fieldvalue+=sep+$(this).attr('value');
									 });
									
								}
								//console.log($fielvalue);
								$data+="'"+$fieldname+"': '"+$fieldvalue+"',";
								
									});
				
				if($data!=''){
				$data_wrap='{'+$data;
				$data_wrap+="'is_exp':"+$is_exp+",'cv_type':"+$cv_type+",'cv_id':"+$cv_id+",'js_id':"+$js_id+",BO:"+(BO?1:0)+"}";
				
				//console.log($data_wrap);
									
				}
			
			return $data_wrap;	
			};
			var r_validate=function(){
				$lis=$('.set-wrap:visible li.required','#cv_form');
				//$('#cv_form').find('span.req').hide();
				notcomplete=false;		
				var email_filter =/^.+\@.+..{2,3}$/ ;
				var number_filter =/^[0-9-\+]{4,}$/;
				var reg_number=/(phone|mobile|fax)+/;
				var reg_email=/email/;

				$lis.each(function(i){
								  
						
						$input=$(this).find('label').next();
						$realinput=$input;
						
						tag=($input.tagName());
							if(tag=='LABEL'){
							$input=$input.find('input');
							$realinput=$input;
							
						}
						if($input.attr('type')=='radio')

							$realinput=$(this).find('input:checked');
								
						if($input.hasClass('select')){
							$realinput=$input.find('option:selected');
							$value=$realinput.attr('value');
							$name=$realinput.attr('name');
							if($value==''){
								$realinput=$input.parent().next().children('input:eq(0)');
							}
							
						}
						if($input.hasClass('mselect_all')){
							$for=$(this).find('label').attr('for');
							$realinput=$(this).find('select[name='+$for+']');
							$value=$realinput.attr('value');
							$name=$realinput.attr('name');
							if($value==''){
								$realinput=$input.parent().next().children('input:eq(0)');
							}
						}
						if($realinput.length>0){
							$value=$realinput.attr('value');
							
							//check for numbers
							attr_name=$realinput.attr('name');
							if(reg_number.test(attr_name)){
									if(!number_filter.test($value))
										$value='';
										
							}
							if(/^company_to/.test(attr_name)){
									if(
									   ($value==''||$value=='undefined')
									   &&
									   !($realinput.next().next().next().next().is(':checked'))
									   )
										$value='';
								
									
							}
						}
						else
							$value='';
							
						if($value==''){
							
							$(this).removeClass('filled').addClass('not-filled');
							
							notcomplete=true;
						}
						else{
							$(this).removeClass('not-filled').addClass('filled');
							
						}
						
				});
				
				return notcomplete;
			}
			var sendData=function($data,submitflag){
			var canpost=notecomplete=false;
			//SEND DATA
			
			var callback=function(data){
				{
					
				if(data=='no-session'){ alert(signin); location.reload(true); return false;}
				if(data=='wrong-access'){ alert(signin);location.reload(true); return false;}
				canpost=data;
				//console.log(canpost);
				/*if(submitflag){
					window.location=$('#submit').attr('href');
					return false;
				}*/
					
				
				
				notcomplete=r_validate();
				if(BO) {alert(cvsaved); $('#save').removeClass('loader'); }
				else
				//IF CV CAN BE SUBMITTED
				if( $('#upload').hasClass('loader'))
					noremove=true;
				if(!notcomplete){
					
					//ENABLE SUBMIT BUTTON
					$('#submit').removeClass('disabled');
					
					//SUBMIT CLICK AFTER SAVE (CALLED FROM THE CLICK SUBMIT)
					if(submitflag&&canpost=='submit'){
							var noremove=true;
							//log('should submit');
							$('#submit').removeClass('saving');
							$('#submit').click();
							
							
					}
					
					//CAN'T SUBMIT
					else if(submitflag&&canpost=='no-submit'){
						//cvsaved='cv saved';
						//log('should submit but can\'t');
						
						//DISABLE THE SUBMIT
						$('#submit').addClass('disabled');
						
							
						}
					else
						if(!submitflag&&canpost=='submit'){
							var noremove=true;
							//log('Can submit but shouldn\'t but will illegaly');
						//ILLEGAL SUBMIT	
						oldsubmit=$('#submit').attr('href');
						newsubmit=oldsubmit.replace(/status=saved/,'status=submit');
						 $('#save').addClass('loader'); 
						$.get(newsubmit,function(){
												 
												 alert(cvsaved_nosubmit);
												 $('#save').removeClass('loader'); 
												 return false;
												 });
						
						
							
					}
					else if(submitflag&&$('#submit').hasClass('saving')){
						var noremove=true;
						$('#submit').removeClass('saving');
						$('#submit').click();
						}
					
				}//END CV CAN BE SUBMITTED
				//IF CV CANNOT BE SUBMITTED
				else{
					$('#submit').addClass('disabled');
					if(notcomplete)
					{
					
					if($('#save','#buttons').hasClass('loader'))
							
						
							alert(fill_req);
					
					
					}
				
				}
				
				if(!noremove){
				alert(cvsaved_nosubmit);
				 $('#save').removeClass('loader'); 
				}
				
				}//end callback
				
				
				}
			if($data!=''){
			$('#submit').addClass('disabled'); 	
			eval("$.post($phpfile,"+$data+",function(data){callback(data)})");
			}
			
			
			};
			
			var align=function($toAlign){
				
			var maxWidth=0;var temp;
			$toAlign.each(function(){
								temp=maxWidth=0;
								
								$(this).find('label.label').each(function(){
								var span=$(this).find('span.label');
								//$li=$(this).parent();
								//span.css('height','100%');
								
								temp=span.width();
								
								//log.debug(temp);
								if(temp>maxWidth){
									maxWidth=temp;
									
								}
								
								});
						
			$(this).find('label.label span').css('width',maxWidth);
			
			$(this).find('a.spchk').css('margin-left',parseInt(maxWidth)+3);
			});
			}
			
			//align labels
			align($('.fieldset'));
			
			
			$('.cv-set-inputs').odd_even();
			
			
			//SPLIT STEPS
			$("#steps span:eq(0)").addClass("cur-step");
			$('#cv_form .fieldset:gt(0)').hide();
			$('#back').addClass('disabled');
			var $TOTALSTEPS=$('#cv_form .fieldset').size();
				$('.stepnumber .step').text('1');
				$('.stepnumber .total').text($TOTALSTEPS);
				
			$('#steps span').click(function(){
					rel=$(this).attr('rel');
					$('.stepnumber .step').text((parseInt(rel)+1));
					$('#back').removeClass('disabled');
					$('#next').removeClass('disabled');
					if(rel==0)$('#cv-intro,#cv_type').show();
					else
						$('#cv-intro,#cv_type').hide();
					if(rel==0){
						$('#back').addClass('disabled');
						
					}
					if(rel==($TOTALSTEPS-1)){
						$('#next').addClass('disabled');
					}
					
					if(rel==2)
						$('#add-new-form').show();
						else
							$('#add-new-form').hide();
							
					$('#steps span.cur-step').removeClass('cur-step');
					$(this).addClass('cur-step');
					$('#cv_form .fieldset').hide();	
					$('#cv_form .fieldset:eq('+rel+')').show();		
											
			});
			
			//BUTTONS ACTIONS
			
			
			
			//2. CLONE SubForms
			$('.add-new').click(function(){
				ol_id=$(this).attr('rel');	
				
				var firstlast='last';
				//if(ol_id=='company')
					//firstlast='first';
					
				var $last_before_add=$('#cv_form .'+ol_id+'-inputs:'+firstlast);
				
					
				
				$clone=$last_before_add.clone();
				$newForm=$clone;
				
				$set_id=$last_before_add.parent().attr('rel');
				$set_id++;
				
				if(firstlast=='first'){
					$last_before_add.parent().before($newForm);
				}
				else
					$last_before_add.parent().after($newForm);
					
				var count=$('#cv_form .'+ol_id+'-inputs:visible').length;
								
				var oddeven;
					if((count-1)%2!=0)
						oddeven='odd';
					else
						oddeven='even';
				
				
				$set_inputs_last=$('#cv_form .'+ol_id+'-inputs:'+firstlast);
				$set_inputs_last.show()
				.wrap('<li class="set-wrap '+oddeven+'" rel="'+$set_id+'" ></li>')
				.find('input[name^=set_id]').attr('value',$set_id).end()
				
				//.find('span.stareq').remove().end()
				.find('li')
					.filter('.required')
					.removeClass('filled')
					.addClass('not-filled').end()
				
				;
				
				//reset combo boxes	
				$set_inputs_last=$('#cv_form .'+ol_id+'-inputs:'+firstlast);
				$set_inputs_last.find('select').each(function(){
																				 
								$(this).find('option:first').attr('selected', 'selected');
								
								$name=$(this).attr('name');	
								$name=$name.replace(regfieldname,'_'+$set_id);
								$(this).siblings('label:eq(0)').attr('for',$name);
								
								$(this).attr('name',$name);	
																				 });
				//reset textarea		
				$set_inputs_last.find('textarea').each(function(){
								
								$name=$(this).attr('name');	
								$name=$name.replace(regfieldname,'_'+$set_id);
								$(this).siblings('label:eq(0)').attr('for',$name);
								
								$(this).attr('name',$name);	
								$(this).attr('value','');
								
								$a=$(this).next().next();
								$a.attr('rel',$name);
								
								$sp=$(this).next().next().next();
								$sp.attr('id',$name+'-spelling');
																				 });
				
				//reset inputs
				$inputs=$set_inputs_last.find('input[name!=set_id]');
				$inputs.each(function(){
								
								
								
								$name=$(this).attr('name');	
								$name=$name.replace(regfieldname,'_'+$set_id);
								
								$rel=$(this).attr('rel');
								$relattr='';
								if(typeof $rel !='undefined'){
									$rel=$rel.replace(regfieldname,'_'+$set_id);
									$relattr='rel="'+$rel+'" ';
								}
								$(this).siblings('label:eq(0)').attr('for',$name);
								
								$(this).attr('name',$name);	
								
								
								if($(this).attr('type')!='text'){
								copy='<input style="'+$(this).attr('style')+'" class="'+$(this).attr('class')+'" type="'+$(this).attr('type')+'" value="'+$(this).attr('value')+'" name="'+$name+'" '+$relattr+'/>';	
								$(this).replaceWith(copy);
								}
								
								else{
									
									$(this).attr('value','');
								}
								}
				)				
				.company_to()
				//.datePicker()
				.gradate();
				if (!window.XMLHttpRequest) {
			//	$lih=$set_inputs_last.find('.help-icon').remove().parent().parent().html();
				//$set_inputs_last.replaceWith($lih);
				//DD_belatedPNG.fix('.help-icon');
            	//$set_inputs_last.find('.help-icon').css('z-index',5).css('margin-top',-5).css('margin-left',-2);
		
		
				}
				help();
				
				return false;
			});
			
			//1.submit
			var submitaftersave;
			$('#submit').click(function(){
				
				if(!$('#save').hasClass('loader')&&!$('#submit').hasClass('saving')){					
					//notcomplete=r_validate();
					notcomplete=false;
					if(notcomplete){
						
						
						//r_validate();
						alert(submitfail);
						
						return false;
					}
					else
					if(!notcomplete){
						if(!$('#submit').hasClass('disabled'))
						$('#submit').addClass('saving');
						$('#save').click();
						
						
						return false;
					}
					
					return false;
				}	
				
				else
					window.location.href=$(this).attr('href');
			 });
			//2. SAVE
			$('#save').click(function(){
			
				if(!$(this).hasClass('loader')){
				//add loader
				$(this).addClass('loader');
				
				//new
				$inputs=$('#cv_form').find('.input-li');
				$data=prepareData($inputs);
				submitaftersave=false;
				if($('#submit').hasClass('saving'))
					submitaftersave=true;
					
				sendData($data,submitaftersave);
				
				//end new
				
				
				
				}//end if
				
				//DISABLE HYPERLINK
				return false;
				
			});
			
			//3.NEXT
			$('#next').click(function(){
			
			if(!$(this).hasClass('disabled')){
				cur=$('#steps span.cur-step').attr('rel');
				var nextstep=(parseInt(cur)+1)%$TOTALSTEPS;
				
				
				
				$('#steps span:eq('+nextstep+')').click();
				
			}
			
				return false;
			
			});
			
			//4.BACK
			$('#back').click(function(){
									  
				if(!$(this).hasClass('disabled')){
					
				cur=$('#steps span.cur-step').attr('rel');
				next=(parseInt(cur)-1)%$TOTALSTEPS;
				var nextstep=(parseInt(cur)-1)%$TOTALSTEPS;
				$('#steps span:eq('+nextstep+')').click();
				}
				return false;
				
			});
			
			
			//CLEAR SET
			
			var set,hide;
			$('a.clear-set').livequery('click',function(){
						
					if(confirm(confirmclear)){
					$inputs=$(this).parent().siblings('.input-li');
					
					var set=$(this).parent().parent().parent().attr('rel');
					var hide=set>1;
					clearInputs($inputs,hide);
					
					//var data=prepareData($inputs);
					//sendData(data,false);
					
						}
					return false;
			});
			
			var no_exp=function(){ 
								$('#step43').addClass('hide-inputs');
								var inputsToClear=$('.input-li','#step43');
								clearInputs(inputsToClear,false);
								}
			var yes_exp=function(){
			//$('li:gt(0)','#step43').show();
			$('#step43').removeClass('hide-inputs');
			$('li.set-wrap:gt(0)','#step43').hide();
			}
			
			
			var yesExpHandler=function(){
					if($(this).is(':checked'))
						yes_exp();
						
			};
			$('#yes_exp')
							.change(yesExpHandler)
							.click(yesExpHandler);
							
			var noExpHandler=function(){
					if($(this).is(':checked'))
						if(confirm(confirmnoexp))no_exp();
						else{
						$(this).attr('checked','');
						$('#yes_exp').attr('checked','checked');
						return false;
						}
						
			};
			$('#no_exp')
						//.change(noExpHandler)
						.click(noExpHandler);
			
			
			
			
			//5.View Photo
			$('a#view').lightBox(
				{
					imageLoading: 'images/loading_mid.gif',
					imageBtnClose: 'images/close.gif',
					overlayOpacity: 0.4
				}
			);
			
			if($('input[name=photo]').attr('value')==''){
			$('#view').hide(); $('#remove').hide();
				
			}
			
			//6.remove Photo
			$('#remove').click(function(){
			$('#upload').addClass('loader');
			$imagefile='_ajax-cv-image.php';
			if(BO)
				$imagefile='../_ajax-cv-image.php';
		$.post($imagefile,
	  	 {BO:(BO?1:0),'cv_id':$cv_id,remove:'remove','photo_1':$('input[name=photo]').attr('value')},function(data){
						
					
					$('#cv_form input[name=photo]').attr('value','');
					
					$input=$('#cv_form input[name=photo]').parent();
					$data=prepareData($input);
					sendData($data,false);
					 $('#upload').removeClass('loader');
					 $('#view').hide(); $('#remove').hide();
					}
					);
						
				return false;
						});
			
			//7.UPLOAD PHOTO
			$('#cv_form').each(function(){
			$imagefile='_ajax-cv-image.php';
			if(BO)
				$imagefile='../_ajax-cv-image.php';
			$.ajax_upload('#upload', {
				  // Location of the server-side upload script
				  action: $imagefile,                 
				  // File upload name
				  name: 'photo_1',
				  // Additional data to send
				  data: {
					cv_id : $cv_id
					,BO:(BO?1:0)
				  },
				  // Fired when user selects file
				  // You can return false to cancel upload
				  onSubmit: function(file, extension) {
					  $('#upload').addClass('loader');
					  },
				  // Fired when file upload is completed
				  onComplete: function(file, response) {
					  if(response!='error'){
						//console.log(response);
						 $responseA=response.split('/');
					 	$('#cv_form input[name=photo]').attr('value',$responseA[$responseA.length-1]);
						$input=$('#cv_form input[name=photo]').parent();
						$data=prepareData($input);
						sendData($data,false);
				  		$('#remove').show();
						$('#view').show();
						$('#view').attr('href', response+'?'+Math.random());
						
						
					  }
					  else{
					  $('#cv_form input[name=photo]').attr('value',"Invalid Image.")
					  .parent().find('.error.photo').text(photostuff).addClass('error-photo-shown');
					  alert(photostuff);
					  }
					  $('#upload').removeClass('loader');
					  },
				  // Fired when server returns the "success" string                             
				  onSuccess: function(file){
					 //log.debug('sdjfd');
											},
				  // Fired when server return something else
				  onError: function(file, response){}
				});
										});
			
			
			//MULTIPLE SELECTS
			$('.add-multiple').click(function() {
				$(this).parent().find('.mselect').addClass('current-mselect');
				$(this).parent().find('.mselect_all option:selected').remove().appendTo('.current-mselect');
				$(this).parent().find('.mselect').removeClass('current-mselect');
				return false;
				 });  
				$('.remove-multiple').click(function() { 
				$(this).parent().find('.mselect_all').addClass('current-mselect');
				$(this).parent().find('.mselect option:selected').remove().appendTo('.current-mselect'); 
				$(this).parent().find('.mselect_all').removeClass('current-mselect');
				return false;
				});
				
			$('.add-multiple').click();	
				//PREPARING THE DATA
				/*$('#select2 option').each(function(i) {  
				$(this).attr("selected", "selected");  
				});*/  
				//END PREPARINT THE DATA
			/////////////////////////////////////////////////END CV FORM
			
			
			}
		);
