/**
 *reload sub-field list depend on field
 *reload  software depend on field
 *reload specialty depend on field
 *
 */

$(function(){
    
    //start job filter question
    
    //end of job filter question
    if (typeof(job_id) == 'undefined')
	    var job_id = '';
    //Job AD save , 2 option only save  or save as draft & go to preview page
    
    $("#btn_job_ad_save").live('click', function(){
        
		var error = job_ad_form_validation();
		var job_id = $("#job_id").val();
		
        if(error == 1)
		{
			scroll(0,0);
			
			$("#div_success_msg").html('');
			$("#div_success_msg").hide();
			return false;
		}
		else
		{
			return true;
        }
        //return false;
    });
    
    
    
    //----------------------------------------------------------------------------
    //Job AD save & continue
    
    $("#btn_continue_to_categories").live('click', function(){
		
       var error = job_ad_form_validation();
		
        if(error == 1)
		{
			scroll(0,0);
			$("#div_success_msg").html('');
			$("#div_success_msg").hide();
            return false;
		}
		else
		{
		    return true;
        }
    });
    
    $("#btn_job_categories_save").live('click', function(){
        
       var error = job_ad_categories_form_validation();
	
        if(error == 1){
	    scroll(0,0);
	    return false;
	    $("#div_success_msg").html('');
	}else{
	    $.blockUI({ 
		message: '<h2>Loading <img src="library/image/ajax-loader-small.gif"></h2>', 
		css: { border: '2px solid #333333' } 
	    });
	     $.ajax({
                type: "POST",
                url: 'app/job/job_categories/' + job_id + '/'+ user_id,
                data: $("form").serialize(),
                success: function(data){
		    
		    var url = 'app/common/get_display_message/29';
		    $.get(url, function(data){
			$("#div_success_msg").addClass('success_msg');
			$("#div_success_msg").html(data);
		    });
		    
		    $.unblockUI();
		    scroll(0,0);
                   
                }
            });
	    $.unblockUI(); 
	    return false;
        }
        return false;
    });
     
    $("#btn_continue_to_preview").live('click', function(){
	
       var error = job_ad_categories_form_validation();
        if(error == 1){
	    scroll(0,0);
            return false;
	} else{
	     $.ajax({
                type: "POST",
                url: 'app/job/job_categories/' + $("#job_id").val() + '/'+ user_id,
                data: $("form").serialize(),
                success: function(data){
		    var job_id = data;
		    var url = base_url + 'app/job/preview_job_ad/' + job_id + '/' + user_id;
		    window.location = url;
		    return false;
                }
            });
	    return false;
        }
        return false;
    });
    
    
    function job_ad_form_validation(){
		var error = 0;
        $('#error_text').hide();
		$('#error_text').html('');
        //date compare
		if($('#salary_other').val() == 'Write custom salary')
		{
			$('#salary_other').val('');
		}
	    
		if($("#job_type_id:checked").val() == 3 )
		{
			var err_num_applicants = 1;
		}
		else if($('#job_type_id').val() == '3')
		{
			var err_num_applicants = 1;
		}
		
		if($('#job_type_id').val()=='')
		{
			if($('#job_type_id:checked').val() == '' || $('#job_type_id:checked').val() == undefined)
			{
				$('#error_text').html($('#error_text').html()+'<p>The Job AD Type field is required.</p>');
				$('#job_type_id').addClass("error_validation");
				error = 1;
			}
		}
		
        if($('#job_title').val() == '' || $('#job_title').val() == undefined)
		{
			var jtitle = (is_project) ? 'Project Name' : 'Job Title';
            $('#error_text').html($('#error_text').html()+'<p>The '+jtitle+' field is required.</p>');
			$('#job_title').addClass("error_validation");
            error = 1;
        }
		
		if(err_num_applicants == '1')
		{
			if($('#no_of_application').val() == '' || $('#no_of_application').val() == undefined)
			{
				$('#error_text').html($('#error_text').html()+'<p>The No. of Applicants field is required.</p>');
				$('#no_of_application').addClass("error_validation");
				error = 1;
			}
		}
		
        if($('#work_type_id').val() == '' || $('#work_type_id').val() == undefined)
		{
            $('#error_text').html($('#error_text').html()+'<p>The Work Type field is required.</p>');
			$('#work_type_id').addClass("error_validation");
            error = 1;
        }
		
        if($('#salary_id').val() == '' || $('#salary_id').val() == undefined)
		{
			var jsalary = (is_project) ? 'Project Budget' : 'Salary';
            $('#error_text').html($('#error_text').html()+'<p>The '+jsalary+' field is required.</p>');
			$('#salary_id').addClass("error_validation");
            error = 1;
        }
		
        if($('#job_synopsis').val() == '' || $('#job_synopsis').val() == undefined)
		{
			var jsummary = (is_project) ? 'Project Summary' : 'Job synopsis';
            $('#error_text').html($('#error_text').html()+'<p>The '+jsummary+' field is required.</p>');
			$('#job_synopsis').addClass("error_validation");
            error = 1;
        }
        
		if($('#main_ad').val() == '' || $('#main_ad').val() == undefined)
		{
			var jmain = (is_project) ? 'Project Description' : 'Main Ad'
            $('#error_text').html($('#error_text').html()+'<p>The '+jmain+' field is required.</p>');
			$('#main_ad').addClass("error_validation");
            error = 1;
        }
		
		if (is_project)
		{
			if($('#project_start_date').val() == '' || $('#project_start_date').val() == undefined || ! /^\d{2}\/\d{2}\/\d{4}$/.test($('#project_start_date').val()))
			{
				$('#error_text').html($('#error_text').html()+'<p>The Project Start Date field is required to be a valid date.</p>');
				$('#main_ad').addClass("error_validation");
				error = 1;
			}
		}
		else
		{
			if($('#country_id').val() == '' || $('#country_id').val() == undefined)
			{
				$('#error_text').html($('#error_text').html()+'<p>The Country field is required.</p>');
				$('#start_date').addClass("error_validation");
				error = 1;
			}
			
			if( ($('#state_id').val() == '' || $('#state_id').val() == undefined) && ($('#state_others').val() == '' || $('#state_others').val() == undefined))
			{
				$('#state_id').addClass("error_validation");
				$('#state_others').addClass("error_validation");
				error = 1;
				$('#error_text').html($('#error_text').html()+'<p>The State field is required.</p>');
			}
			
			if( ($('#area_id').val() == '' || $('#area_id').val() == undefined) && ($('#area_others').val() == '' || $('#area_others').val() == undefined) )
			{
				$('#area_id').addClass("error_validation");
				$('#area_others').addClass("error_validation");
				error = 1;
				$('#error_text').html($('#error_text').html()+'<p>The Area field is required.</p>');
			}
		}
		
		if($('#job_level_id').val() == '' || $('#job_level_id').val() == undefined)
		{
			$('#error_text').html($('#error_text').html()+'<p>The Job Level field is required.</p>');
			$('#job_level_id').addClass("error_validation");
			error = 1;
		}
		
		
		if ($('.jobad_apply_method:checked').val() == 3 && ! /^([a-zA-Z0-9\+_\-]+)(\.[a-zA-Z0-9\+_\-]+)*@([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,6}$/.test($('.apply-method-email').val()))
		{
			$('.apply-method-email').addClass("error_validation");
            error = 1;
		    $('#error_text').html($('#error_text').html()+'<p>The external email address must be a valid email address.</p>');
		}
		
		if ($('.jobad_apply_method:checked').val() == 2 && ! /^((http(s?):\/\/)|(www\.))(\w+[^\s\)\<]+)$/i.test($('.apply-method-url').val()))
		{
			$('.apply-method-url').addClass("error_validation");
            error = 1;
		    $('#error_text').html($('#error_text').html()+'<p>The external URL must be a valid URL beginning with http://.</p>');
		}
		
		if(error == 1) $('#error_text').show();
		
		return error;
    }
    
    function job_ad_categories_form_validation(){
	var error = 0;
        $('#error_text').hide();
	$('#error_text').html('');
	
	var len = [];
	$('#specialties :selected').each(function(i, selected){
	    len[i] = $(selected).text();
	});
	if(len.length>5){
	   $('#error_text').html($('#error_text').html()+'<p>Specialties: You can only choose a max of 5 specialties</p>'); 
	}
	
	var len = [];
	$('#software :selected').each(function(i, selected){
	    len[i] = $(selected).text();
	});
	if(len.length>5){
	   $('#error_text').html($('#error_text').html()+'<p>Software: You can only choose a max of 5 softwares</p>'); 
	}
	
	if($('#industry_id').val() == '' || $('#industry_id').val() == undefined)
	{
        $('#error_text').html($('#error_text').html()+'<p>The Industry field is required.</p>');
	    $('#industry_id').addClass("error_validation");
        error = 1;
    }
	
	if($('#job_field').val() == '' || $('#job_field').val() == undefined)
	{
        $('#error_text').html($('#error_text').html()+'<p>The Profession field  is required.</p>');
	    $('#job_field').addClass("error_validation");
        error = 1;
    }
    
	if($('#job_subfield').val() == '' || $('#job_subfield').val() == undefined)
	{
        $('#error_text').html($('#error_text').html()+'<p>The Expertise field is required.</p>');
	    $('#job_subfield').addClass("error_validation");
        error = 1;
    }
	
	
	
	if(error == 1)$('#error_text').show();
	return error;
    }
    
    $('.datepickerstart').datepicker({
        changeMonth: true,
        changeYear: true,
        //maxDate: '-10Y',
        yearRange: '2009:2010',
	dateFormat: 'dd/mm/yy'
    });
    $('.datepickerend').datepicker({
        changeMonth: true,
        changeYear: true,
        //maxDate: '-10Y',
        yearRange: '2009:2010',
	dateFormat: 'dd/mm/yy'
    });
    $("#job_field").change(function(){
        $("#job_subfield").children().remove();
        $("#scroll_box_specialty").children().remove();
        $("#scroll_box_software").children().remove();
        var url = 'app/register/get_job_subfield_specialties_software_list/';
        $.post(
               url,
               { 'job_field_id': $(this).val() },
                function(data){
                    var aData = data.split('||');
                    if(aData[0] != 'no_data')
                        $("#job_subfield").append(aData[0]);
                    if(aData[1] != 'no_data')
                        $("#scroll_box_specialty").append(aData[1]);
                    if(aData[2] != 'no_data')
                        $("#scroll_box_software").append(aData[2]);
                }, "html");
    });
    /*
    $("#job_field").change(function(){
        $("#job_subfields").children().remove();
        $("#specialties").children().remove();
        $("#software").children().remove();
        var url = 'app/register/get_job_subfield_specialties_software_list/';
        $.post(
               url,
               { 'job_field_id': $(this).val() },
                function(data){
                    var aData = data.split('||');
                    if(aData[0] != 'no_data')
                        $("#job_subfield").append(aData[0]);
                    if(aData[1] != 'no_data')
                        $("#specialties").append(aData[1]);
                    if(aData[2] != 'no_data')
                        $("#software").append(aData[2]);
                }, "html");
    });
    */
    $("#country_id").change(function(){
        $.blockUI();
        $("#state_id").children().remove();
        $("#area_id").children().remove();
        $("#area_id").append('<option value="">Select City</option>');
        var url = 'app/register/get_state_list/';
        $.post(
               url,
               { 'country_id': $(this).val() },
                function(data){
                   //alert(data);
                   if(data == 'no_data'){
                        $("#state_id").hide();
                        $("#area_id").hide();
                        $("#state_others").show();
                        $("#area_others").show();
                   }else{
                        $("#state_id").show();
                        $("#area_id").show();
                        $("#state_others").hide();
                        $("#area_others").hide();
                        
                        $("#state_id").append(data);
                   }
				   
				   $.unblockUI();
                }, "html");

        //$("#state_id").append('<option id="foo">foo</option><option id="foo">foo</option><option id="foo">foo</option>');
        
        
       //  alert($("#state_id").val());
    });
    
    $("#state_id").change(function(){
        $.blockUI();
        $("#area_id").children().remove();
        var url = 'app/register/get_area_list/';
        $.post(
               url,
               { 'state_id': $(this).val() },
                function(data){
                   //alert(data);
                    if(data == 'no_data'){
                        $("#area_id").hide();
                        $("#area_others").show();
                    }else{
                        $("#area_id").show();
                        $("#area_others").hide();
                        $("#area_id").append(data);
                    }
					
					$.unblockUI();
                }, "html");

        //$("#state_id").append('<option id="foo">foo</option><option id="foo">foo</option><option id="foo">foo</option>');
        
        
       //  alert($("#state_id").val());
    });
     
    
});
    function setStateArea(country_id, state_id, state_others, area_id , area_others){
       
        //alert(country_id + '-' + state_id +'-' + state_others + '-' + area_id +'-'+ area_others);
        if(country_id){
            var url = 'app/register/get_state_list/';
            $.post(
                   url,
                   { 'country_id': country_id , 'selected': state_id},
                    function(data){
                       //alert(data);
                       if(data == 'no_data'){
                            $("#state_id").hide();
                            $("#area_id").hide();
                            $("#state_others").show();
                            $("#state_others").val(state_others) ;
                            $("#area_others").show();
                             $("#area_others").val(area_others)  ;
                       }else{
                            $("#state_id").children().remove();
                            $("#area_id").children().remove();
                            
                            $("#state_id").show();
                            $("#area_id").show();
                            var url = 'app/register/get_area_list/';
                            $.post(
                            url,
                            { 'state_id': state_id, 'selected': area_id},
                             function(data){
                                //alert(data);
                                 if(data == 'no_data'){
                                     $("#area_id").hide();
                                     $("#area_others").show();
                                 }else{
                                     $("#area_id").show();
                                     $("#area_others").hide();
                                     $("#area_id").append(data);
                                 }
                             }, "html");
                            $("#state_others").hide();
                            $("#area_others").hide();
                            
                            $("#state_id").append(data);
                       }
                    }, "html");
        }
    }
    



