function listApplicant(jobId, userId)
{
    $.post("/jobs/changeapplicantstatus",
             { jobId: jobId, userId: userId, status : 'listed'},
             function(data){
                 datas = data.split(':');   
                 if(datas[0] == 'SUCCESS')
                 {
                     showCommonDialog(datas[1], 'Success!', 'green');
                 }
                 if(datas[0] == 'ERROR')
                 {
                     showCommonDialog(datas[1], 'Sorry!', 'red');
                 } 
           }
    );
}

function selectApplicant(jobId, userId)
{
	$.post("/jobs/changeapplicantstatus",
             { jobId: jobId, userId: userId , status : 'selected'},
             function(data){
                 datas = data.split(':');   
                 if(datas[0] == 'SUCCESS')
                 {
                     showCommonDialog(datas[1], 'Success!', 'green');
                 }
                 if(datas[0] == 'ERROR')
                 {
                     showCommonDialog(datas[1], 'Sorry!', 'red');
                 } 
           }
    );
}

function showCommonDialog(message, title, color)
{
	$("#dialog").html('<p><font size=3 color=' + color +'>' + message + '</font></p>');
	$("#dialog").dialog('open');
	$('#ui-dialog-title-dialog').text(title);
	
	// Css tuning
	//$('#dialog').css({height: 'auto', minHeight: '100px'});
}

function apply(jobId)
{
	$.post("/user/apply",
  		   { job: jobId},
  		   function(data){
  		   		if(data == 'success')
  		   		{
  		   			showCommonDialog('Thanks for applying.', 'Success!', 'green');
  		   		}
  		   		if(data == 'applied')
  		   		{
  		   			showCommonDialog('You have already applied for this job.', 'Error!', 'red');
  		   		}
  		   		if(data == 'error')
  		   		{
  		   			showCommonDialog('Feature temporarily unavailable. Please try later', 'Sorry!', 'red');
  		   		}
  		   		
           }
	);
}

function thumbJob(jobId)
{
	$.post("/jobs/thumbjob",
  		   { job: jobId},
  		   function(data){
  		   		if(data == 'success')
  		   		{
  		   			showCommonDialog('Thanks for Thumb Up.', 'Success!', 'green');
  		   		}
  		   		if(data == 'thumbed')
  		   		{
  		   			showCommonDialog('You have already Thumb Up this job.', 'Error!', 'red');
  		   		}
  		   		if(data == 'error')
  		   		{
  		   			showCommonDialog('Feature temporarily unavailable. Please try later', 'Sorry!', 'red');
  		   		}
           }
	);
}


function saveJob(jobId)
{
	$.post("/jobs/savejob",
  		   { job: jobId},
  		   function(data){
  		   		if(data == 'success')
  		   		{
  		   			showCommonDialog('Thanks! Job saved to your panel.', 'Success!', 'green');
  		   		}
  		   		if(data == 'saved')
  		   		{
  		   			showCommonDialog('You have already saved this job.', 'Error!', 'red');
  		   		}
  		   		if(data == 'error')
  		   		{
  		   			showCommonDialog('Feature temporarily unavailable. Please try later', 'Sorry!', 'red');
  		   		}	
           }
	);
}

function checkDuplicate()
{
	var companyName  = $('#company_name').val();
	var jobTitle     = $('#title').val();
	var deadlineDate = $('#deadline').val();
	
	$.post("/harvest/check-harvestable",
  		   { title    : jobTitle, 
  		     company  : companyName,
  		     deadline : deadlineDate},
  		     function(data){
  		   
  		   	     if(data == 'unique')
  		   		 {
  		   			 showCommonDialog('This job is unique! Continue harvesting.', 'Unique!', 'green');
  		   		 }
  		   		 if(data == 'duplicate')
  		   		 {
  		   			 showCommonDialog('Sorry! It\'s going to be duplicate.', 'Duplicate!', 'red');
  		   		 }
  		   		 if(data == 'incomplete')
  		   		 {
  		   		     showCommonDialog('Title, Company and Deadline are required for checking.', 'Incomplete!', 'red');
  		   		 }
             }
	);
} 
 // Approve harvested job trusted agent
function approveHJobByTrusted(job_id)
{
    $.post("/harvest/approve",
             {jobId    : job_id},
             function(data){
                     if(data == 'approved')
                     {
                         if(isset('HARVEST_DETAILS'))
                         {// if details page, show message on dialog
                             showCommonDialog('Thanks. Job Approved.', 'Approved!', 'green');
                             $('.listing-options').fadeOut("slow");
                         }
                         else
                         {// In listing page, make tha background green
                             $('#result-' + job_id).css("background-color", '#dfd');
                             $('#result-' + job_id +' .listing-options').fadeOut("slow");
                         }    
                     }
                     else
                     {
                    showCommonDialog('Something was wrong! Please try later.<br /> <p>Error Code : approveHJobByTrusted#' + job_id + '</p>', 
                    'Error!', 'red');                     
                     }
                                          
           }
    );
}

// Delete harvested job trusted agent
function deleteHJobByTrusted(job_id)
{
    $.post("/harvest/delete",
             {jobId    : job_id},
             function(data){
                     
                     if(data == 'deleted')
                     {
                         if(isset('HARVEST_DETAILS'))
                         {// if details page, show message on dialog
                             showCommonDialog('Job deleted successfully.', 'Deleted!', 'green');
                             $('.listing-options').fadeOut("slow");
                         }
                         else
                         {// In listing page, hide the deleted job
                             $('#result-' + job_id).fadeOut('slow');
                         }
                     }
                     else
                     {
                    showCommonDialog('Something was wrong! Please try later.<br /> <p>Error Code : deleteHJobByTrusted#' + job_id + '</p>', 
                    'Error!', 'red');                     
                     }
                                          
           }
    );
}

// Common utility functions

function isset(varname)
{
  return(typeof(window[varname])!='undefined');
}
function sendMessage(applicantId)
{
   //$('#contactApplicantId').val('0');
   //alert($('#contactsubject').val());
   $('#contact-form :input').val('');
   $("#subject_error").html('');
   $("#message_error").html('');
   
   $('#contactApplicantId').val(applicantId);
    $("#contact-box").dialog({ 
        buttons: { 
            "Send": function() { 
                $error = false; 
                $formData = $('#contact-form').serializeArray();
                jQuery.each($formData, function(i, field){
                    if(field.value == '')
                    {
                        $error = true;
                        $("#" + field.name + "_error").html('Please enter a ' + field.name);
                    }
                    else
                    {
                        $("#" + field.name + "_error").html('');
                    }
                });
               //alert($error);
                if(!$error)        
                {
                    $(this).dialog("close");  
                    $.ajax({
                          type: "POST",
                          url: "/user/contact",
                          data: $('#contact-form').serialize(),
                          success: function(data){
                              if(isNaN(data))
                              {
                                  showCommonDialog(data, 'Error!', 'red')
                              }
                              else
                              {
                                  showCommonDialog('Thank You!<br> Your message is sended', 'Message Send', 'green')
                                 
                              }
                          }
                        });
                }
               
            },
            "Cancel": function() { 
                $(this).dialog("close"); 
            }
        },
        title  : 'Send Message', 
        height : 300,  
        width  : 500  
    });
    $('#contact-box').show();
}

function inviteToApply(applicantId, jobId)
{
   
   $('#contact-form :input').val('');
   $("#subject_error").html('');
   $("#message_error").html('');
   $('#contact-form').prepend('<input type="hidden" name="contactApplicantId" id="contactApplicantId" value="' + applicantId + '">');
   $('#contact-form').prepend('<input type="hidden" name="contactJobId" id="contactJobId" value="' + jobId + '">');
   //$('#contactApplicantId').val(applicantId);
   //$('#contactApplicantId').val(applicantId); 
    $("#contact-box").dialog({ 
        buttons: { 
            "Send": function() { 
                $error = false; 
                $formData = $('#contact-form').serializeArray();
                jQuery.each($formData, function(i, field){
                    if(field.value == '')
                    {
                        $error = true;
                        $("#" + field.name + "_error").html('Please enter a ' + field.name);
                    }
                    else
                    {
                        $("#" + field.name + "_error").html('');
                    }
                });
               
                if(!$error)        
                {
                    $(this).dialog("close");  
                    $.ajax({
                          type: "POST",
                          url: "/jobs/invitetoapply",
                          data: $('#contact-form').serialize(),
                          success: function(data){
                              if(isNaN(data))
                              {
                                  showCommonDialog(data, 'Error!', 'red')
                              }
                              else
                              {
                                  showCommonDialog('Thank You!<br> Your message is sended', 'Message Send', 'green')
                                 
                              }
                          }
                        });
                }
               
            },
            "Cancel": function() { 
                $(this).dialog("close"); 
            }
        },
        title  : 'Send Message', 
        height : 300,  
        width  : 500  
    });
    $('#contact-box').show();
}

function encapsulate(objId)
{     
    $("#" + objId).toggle(); 
}

function changeFormByUserType()
{     
    switch($("#user_type_id option:selected").html())
    {
        case 'agent' : 
                        $('#job_title').parent().parent().hide();
                        $('#company_name').parent().parent().hide();
                        $('#trustedagent').parent().parent().show();
                        break;
        case null    : break;
        default      :  
                        $('#job_title').parent().parent().show();
                        $('#company_name').parent().parent().show();
                        $('#trustedagent').parent().parent().hide();
                        break;
    }
    
}
function toogleassignTo()
{
    switch($("#trustedagent option:selected").html())
    {
         case 'no': 
	        $('#assign_to').parent().parent().show();
	        break;
        default:  
	        $('#assign_to').parent().parent().hide();
	        break;
    }    
}
// MUST BE AT THE END OF THIS FILE
// DOM id ready now
$(document).ready(function () {
	
	$('#joblogo').next().css({color: '#333', fontWeight: 'bold'});

	 // call filter() function on change of filtering select boxes (admin panel)
     if($('.list-filter').size() > 0) $('.list-filter').change(filter);
     
     // Initilize datepicker for deadline field
     if($('#deadline').size() > 0)
     {  
	     $('#deadline').datepicker({minDate: new Date(currentDate),
                                    showOn: "both",
	                                buttonImage: "../images/calendar.gif",
	                                buttonImageOnly: true,
	                                dateFormat: 'yy-mm-dd' });
     }
     
     // Set up dialog box
     if($('#dialog').size() > 0)
     {
	     $('#dialog').dialog({
			autoOpen: false,
			width: 400,
			height: 200,
			buttons: {
				"Ok": function() { 
					$(this).dialog("close"); 
				}
			}
		});  
	}
});

