﻿RampUp.DisplayJobs = function() {
    RampUp.PageMethod("/webservices/DataService.asmx/ListJobs", jQuery.toJSON({}),
	function(result) // Callback
	{
	    RampUp.Jobs = {};

	    if (result.d === undefined)
	        RampUp.Jobs = eval('(' + result + ')');
	    else
	        RampUp.Jobs = eval('(' + result.d + ')');

	    if (RampUp.Jobs !== undefined && RampUp.Jobs.length > 0) {
	        displayJobs();
	        displayFirstJob();
	        windowSize();
	    }
	});

    function displayJobs() {
        jQuery('#listing').empty();

        jQuery.each(RampUp.Jobs, function() {
            var newEl = jQuery('#jobid').clone().attr('id', 'jobID_' + this.JobID);
            newEl.find('.job').html(this.Title);
            newEl.appendTo('#listings');
        });
        windowSize();
    };

    function displayFirstJob() {
        jQuery('#details > li > h3').text(RampUp.Jobs[0].Title);
        jQuery('#details li:eq(1)').text(RampUp.Jobs[0].Desc);
        jQuery('#details .apply').attr('id', 'apply_' + RampUp.Jobs[0].JobID);
    };
};

RampUp.JobSearchSave = function() {
    var jobInfo = {};
    jobInfo.jobID = jQuery('#job .apply').attr('id').substr(6);
    jobInfo.name = jQuery('#applyforjob #txtName').val().trim();
    jobInfo.email = jQuery('#applyforjob #txtEmail').val().trim();
    jobInfo.phone = jQuery('#applyforjob #txtPhone').val().trim();
    jobInfo.resume = jQuery('#applyforjob #txtResume').val().trim();
    jobInfo.fileName = (RampUp.fileName !== undefined) ? RampUp.fileName : '';

    RampUp.PageMethod("/webservices/DataService.asmx/ApplyForJob", jQuery.toJSON(jobInfo));
    RampUp.ClearJobSearchForm();
    alert("Thank you for your submission!");
};

RampUp.GetJobSearchValidationErrors = function() {
    var errors = new Array();
    getNameErrors();
    getEmailErrors();
    getPhoneErrors();
    displayErrors();
    return errors;

    function getNameErrors() {
        if (jQuery('#applyforjob #txtName').val().trim().length == 0)
            errors.push('valName');
    };

    function getEmailErrors() {
        var email = jQuery('#applyforjob #txtEmail').val().trim();

        if (email.length == 0)
            errors.push('valEmail');

        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        if (emailPattern.test(email) == false)
            errors.push('valEmailSyntax');
    };

    function getPhoneErrors() {
        var phone = jQuery('#applyforjob #txtPhone').val().trim();

        if (phone.length == 0)
            errors.push('valPhone');

        //		var phonePattern = /^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/;
        //		if (phone.match(phonePattern) == false)
        //			errors.push('valPhoneFormat');
    };

    function displayErrors() {
        for (var i = 0; i < errors.length; i++)
            jQuery('#applyforjob #' + errors[i]).fadeIn();

        windowSize();
    };
};

RampUp.ClearJobSearchForm = function()
{
	jQuery('#applyforjob #txtName').val('');
	jQuery('#applyforjob #txtEmail').val('');
	jQuery('#applyforjob #txtPhone').val('');
	jQuery('#applyforjob #txtResume').val('');
	windowSize();
};


jQuery(document).ready(function()
{
	RampUp.ClearJobSearchForm();

	jQuery('#applyforjob #imgUpload').fileUpload({
		'uploader': '/swf/uploader.swf',
		'script': '/desktopmodules/rampup-jobsearch/upload.ashx',
		'cancelImg': '/images/cancel.png',
		'folder': '/resumes',
		'scriptData': { 't': 'resume' },
		'multi': false,
		'buttonImg': '/images/postresume/upload.png',
		'width': 91,
		'height': 23,
		'wmode': 'transparent',
		onSelect: function(event, queueID, fileObj, response, data)
		{
			RampUp.fileName = fileObj.name;
		}
	});
	jQuery('#applyforjob #imgUploadQueue').addClass('left');

	jQuery('#applyforjob #dlgUploadStatus').dialog(
	{
		autoOpen: false,
		bgiframe: true,
		resizable: false,
		modal: true,
		width: 550,
		height: 400,
		buttons: {
			"Ok": function()
			{
				jQuery(this).dialog('close');
			}
		}
	});

	jQuery('#applyforjob #imgSave').click(function()
	{
		var errors = RampUp.GetJobSearchValidationErrors();

		if (errors.length == 0)
		{
			var jobTitle = jQuery('#applyforjob #details h3:first').text();
			jQuery('#applyforjob #status').html('Your application for the ' + jobTitle + ' has been processed.');

			jQuery('#applyforjob #dlgUploadStatus').dialog('open');
			jQuery('#applyforjob #imgUpload').fileUploadStart();

			RampUp.JobSearchSave();
		}
	});

    
	jQuery('#applyforjob input[type="text"]').focus(function()
	{
		jQuery(this).val('').parent().find('div').fadeOut();
	});
});

/* Events */

jQuery('#listings li').live('click', function()
{
	var id = this.id.substr(6);

	for (var i = 0; i < RampUp.Jobs.length; i++)
	{
		if (RampUp.Jobs[i].JobID == id)
		{
			jQuery('#applyforjob').hide();
			jQuery('#details').show();
			jQuery('#details > li > h3').text(RampUp.Jobs[i].Title).fadeIn();
			jQuery('#details li:eq(1)').text(RampUp.Jobs[i].Desc).fadeIn();
			jQuery('#details .apply').attr('id', 'apply_' + RampUp.Jobs[i].JobID);
			windowSize();
			break;
		}
	}
});

jQuery('.apply').live('click', function()
{
	RampUp.ClearAppForm();

	var id = this.id.substr(6);
	jQuery('#details').hide();
	jQuery('#applyforjob').show();
	windowSize();
});

