/* -----------------------------------------------------------
Copyright: ©2005-2008 Practical eCommerce. All Rights Reserved.
Author: Brian Getting
Company: Practical eCommerce
URL: http://www.practicalecommerce.com
----------------------------------------------------------- */

var liveSearchDefault = "Job Board Live Search...";
var companyDefault = "";
var locationDefault = "Ex: \"Los Angeles, CA USA\" or \"Anywhere\"";
var jobDefault = "To help compose your job posting, you might want to address the following. You can always edit your job posting after it goes live on our site.\n\n- Thorough job description.\n- Requirements for the job, including education and work experience.\n- Information about your company.\n- Job duration: Full time, part time, freelance, intern.\n- Salary or salary range.\n- Benefits (Health, dental, flexible hours, telecommunting policy, other).\n- Special requirements to apply, such as a cover letter, resume, no phone calls, other.\n\nWe recommend that you leave line breaks between paragraphs as you compose your listing.\n\nRemember to use critical keywords that applicants are likely to search for.";
var applyDefault = "Email your resume to jobs@domain.com";

var job = {
	clearDefault: function(evt, defValue) {
		if ($F(Event.element(evt)) == defValue) {
			$(Event.element(evt)).value = "";
		}
	},

	validate: function() {
		var company = $('job_company');
		var position = $('job_position');
		var location = $('job_location');
		var field = $('job_field_id');
		var description = $('job_description');
		var toApply = $('job_to_apply');
		var tags = $('job_tag_list');
		var jobSubmit = $('job_submit');
		var errors = "";
		if (company.value == "") {
			company.setStyle({backgroundColor: errorColor});
			errors += "* Please enter a company name.\n";
		}
		if (position.value == "") {
			position.setStyle({backgroundColor: errorColor});
			errors += "* Please enter a job title.\n";
		}
		if (location.value == "") {
			location.setStyle({backgroundColor: errorColor});
			errors += "* Please enter a location for this position.\n";
		}
		if (field.options[field.selectedIndex].value == "") {
			field.setStyle({borderColor: errorBorder});
			errors += "* Please select a category for this position.\n";
		}
		if (description.value == "" || description.value == jobDefault) {
			description.setStyle({backgroundColor: errorColor});
			errors += "* Please enter a description of this position.\n";
		} else if (description.value.length < 100) {
			description.setStyle({backgroundColor: errorColor});
			errors += "* Pleae create a longer, more useful description.\n";
		}
		if (toApply.value == "" || toApply.value == applyDefault) {
			toApply.setStyle({backgroundColor: errorColor});
			errors += "* Please describe how to apply for this position.\n";
		}
		if (tags.value == "") {
			tags.setStyle({backgroundColor: errorColor});
			errors += "* Please enter at least one tag for this job post.\n";
		}
		if (tags.value.length >= 70) {
			tags.setStyle({backgroundColor: errorColor});
			errors += "* Please limit tags to 128 characters.\n";
		}
		if (errors == "") {
			jobSubmit.value = "Processing..."
			jobSubmit.disabled = true;
			return true;
		} else {
			alert("Please fix the following errors:\n"+errors);
			return false;
		}
	},

	submitPreview: function() {
		$('job_submit').value = "Processing..."
		$('job_submit').disabled = true;
	},

	setup: function() {
		var searchInput = $('search');
		var newForm = $('job-form');
		// Set up the live search if present
		if (searchInput) {
			searchInput.value = liveSearchDefault;
			Event.observe(searchInput, 'focus', job.clearDefault.bindAsEventListener(searchInput, liveSearchDefault));
		}
		// Set up the new listing form if present
		if (newForm) {
		  $('job_submit').value = "Preview Job Posting"
      $('job_submit').disabled = false;
			// set the default values if needed
			companyField = $('job_company');
			urlField = $('job_uri');
			locationField = $('job_location');
			descriptionField = $('job_description');
			applyField = $('job_to_apply');
			if (companyField.value == "") {
				companyField.value = companyDefault;
			}
			if (urlField.value == "") {
				urlField.value = "http://";
			}
			if (locationField.value == "") {
				locationField.value = locationDefault;
			}
			if (descriptionField.value == "") {
				descriptionField.value = jobDefault;
			}
			if (applyField.value == "") {
				applyField.value = applyDefault;
			}
			// clear defaults on some text fields
			Event.observe(locationField, 'focus', job.clearDefault.bindAsEventListener(locationField, locationDefault));
			Event.observe(applyField, 'focus', job.clearDefault.bindAsEventListener(applyField, applyDefault));
			// attach the rich text editor functionality
			Event.observe($('bold'), 'click', richTextEditor.format_text.bindAsEventListener('bold', 'bold', 'job_description'));
			Event.observe($('italic'), 'click', richTextEditor.format_text.bindAsEventListener('italic', 'italic', 'job_description'));
			Event.observe($('list'), 'click', richTextEditor.format_text.bindAsEventListener('list', 'list', 'job_description'));
		}
	}
}

Event.observe(window, 'load', job.setup);