$(document).ready(attachNavRolls);
$(document).ready(attachErrorBoxFade);

function attachNavRolls()
{
	var SPEED_IN_MILLISECONDS = 200;
	
	$("div#navigation img").mouseover(
		function() 
		{
			$("div#navRolls img").animate( { width:"0" }, { queue:false, duration: SPEED_IN_MILLISECONDS } );
			$("#" + this.id + "On").animate( { width:"82px" }, { queue:false, duration: SPEED_IN_MILLISECONDS } );
		}	
	);
	$("div#navRolls img").mouseout(
		function() 
		{
			$(this).animate( { width:"0" }, { queue:false, duration: SPEED_IN_MILLISECONDS } );
		}	
	)
}

function attachErrorBoxFade()
{
	$("#formErrorsEnvelope").fadeOut(8000);
}

function attachSkillTriggers()
{
	$("ul#skillsTriggers li").click(
		function()
		{
			$("ul#skillsTriggers li ul").slideUp("slow");
			$("ul#skillsTriggers li").removeClass("selected");
			$(this).addClass("selected");
			$("ul", $(this)).slideDown("slow");
			return false;
		}
	);
}

function attachTestimonialActions()
{
	$("ul#fullTestimonials li").click(
		function()
		{
			if (this.className.indexOf("selected") == -1)
			{
				$("ul#fullTestimonials li ul").slideUp("slow");
				$("ul#fullTestimonials li").removeClass("selected");
				$(this).addClass("selected");
				$("ul", $(this)).slideDown("slow");
			}
			return false;
		}
	);
}

function attachRecommendationFormControls()
{
	var SPEED_IN_MILLISECONDS = 1000;
	$("a#toStep2").click(
		function()
		{
			$("div#referralStep1").slideUp(SPEED_IN_MILLISECONDS);
			$("div#referralStep2").slideDown(SPEED_IN_MILLISECONDS);
			return false;
		}
	);
	
	$("a#backToStep1").click(
		function()
		{
			$("div#referralStep2").slideUp(SPEED_IN_MILLISECONDS);
			$("div#referralStep1").slideDown(SPEED_IN_MILLISECONDS);
			return false;
		}
	);
}

function validateForm()
{
	var required = [
		["firstName", "Please enter your first name"], 
		["lastName", "Please enter your last name"], 
		["email", "Please enter a valid email"], 
		["phone", "Please enter a phone number"]
	];
	
	var message = "";
	for (var i = 0; i < required.length; i++)
	{
		if ($("input#" + required[i][0]).val() == "")
		{
			message += required[i][1] + "\n";
		}
	}
	
	var pattern = /\d{1,2}\/\d{1,2}\/\d{2,4}/;
	var dateMatch = new RegExp(pattern);
	var dateAvail = document.forms[0].elements["dateAvailable"].value;
	if (dateAvail.length > 0 && !dateMatch.test(dateAvail))
	{
		message += "Please enter a valid date";
	}
	
	if (message.length > 0)
	{
		alert(message);
		return false;
	}
	return true;
}

var submissions = 0;
function attachBullhornDupeCheck()
{
	$("body#applyForm #applyFormSubmit").click(
		function()
		{
			if (submissions > 0)
			{
				return false;
			}
			submissions++;
			if (!validateForm())
			{
				return false;
			}
			$.get("../job-seekers/dupe-check.aspx", {
				firstName: $("input#firstName").val(), 
				lastName: $("input#lastName").val(), 
				email: $("input#email").val()
				}, function (data){handleDupeResponse(data);}
			);
			
			return false;
		}
	);
}

function handleDupeResponse(data)
{
	// not an existing user
	if (data.indexOf("<NA/>") > -1)
	{
		document.forms[0].submit();
		return;
	}
	
	// look for user id
	var matches = data.match(/<USERID>(\d+)/) || false;
	
	if (!matches)
	{
		document.forms[0].submit();
		return;
	}
	
	if (matches.length < 2)
	{
		document.forms[0].submit();
		return;
	}
	// apply user to job and redirect
	$.get("../job-seekers/_apply-user.aspx", {
		userId: matches[1], 
		jobId: $("input#jobPostingID").val(), 
		notifyId: $("input#notify").val()
		}, function (data){handleApplyResponse(data);}
	);
}

function handleApplyResponse(data)
{
	if (data.indexOf("1") > -1)
	{
		window.location = "job-apply-thank-you.aspx";
		return;
	}
	alert("There was a problem submitting your application for this job. Please contact us directly.");
}

function scrollHomepageTestimonials()
{
	var scrollBox = $("div#testimonialScrollerPull");
	var pullTo = scrollBox.height();
	var timespan = pullTo * 30;
	pullTo -= 170;
	scrollBox.animate({marginTop: "-" + pullTo + "px"}, timespan);
}

/* Bullhorn */
function _CF_onError(form_object, input_object, object_value, error_message)
        {
    	alert(error_message);
           	return false;	
        }
    
    
    function _CF_hasValue(obj, obj_type)
        {
        if (obj_type == "TEXT" || obj_type == "PASSWORD")
    	{
        	if (obj.value.length == 0) 
          		return false;
        	else 
          		return true;
        	}
        else if (obj_type == "SELECT")
    	{
            for (i=0; i < obj.length; i++)
    	    	{
    		if (obj.options[i].selected)
    			return true;
    		}
    
           	return false;	
    	}
        else if (obj_type == "SINGLE_VALUE_RADIO" || obj_type == "SINGLE_VALUE_CHECKBOX")
    	{
    
    		if (obj.checked)
    			return true;
    		else
           		return false;	
    	}
        else if (obj_type == "RADIO" || obj_type == "CHECKBOX")
    	{
    
            for (i=0; i < obj.length; i++)
    	    	{
    		if (obj[i].checked)
    			return true;
    		}
    
           	return false;	
    	}
    	}
    
    function  _CF_checkEditCandidateProfileForm(_CF_this)
        {
        if  (!_CF_hasValue(_CF_this.firstName, "TEXT" )) 
            {
            if  (!_CF_onError(_CF_this, _CF_this.firstName, _CF_this.firstName.value, "The First Name field is required."))
                {
                return false; 
                }
            }
    
        if  (!_CF_hasValue(_CF_this.lastName, "TEXT" )) 
            {
            if  (!_CF_onError(_CF_this, _CF_this.lastName, _CF_this.lastName.value, "The Last Name field is required."))
                {
                return false; 
                }
            }
    
        if  (!_CF_hasValue(_CF_this.email, "TEXT" )) 
            {
            if  (!_CF_onError(_CF_this, _CF_this.email, _CF_this.email.value, "The Email field is required."))
                {
                return false; 
                }
            }
            
        if  (!_CF_hasValue(_CF_this.phone, "TEXT" )) 
            {
            if  (!_CF_onError(_CF_this, _CF_this.phone, _CF_this.phone.value, "The Phone field is required."))
                {
                return false; 
                }
            }
            
        return true;
        }
