var errorMessage = "";
var errorIndex = 1;

$(document).ready(function() {
	$(".link").hover(function() {
		$(this).addClass("hover");
	});
	
	$(".link").mouseout(function() {
		$(this).removeClass("hover");
	});
	
	$(".link").click(function() {
		var fileName = $(this).attr("name");
		if (
			(fileName != ".") && (fileName.indexOf(".asp") < 0) && 
			(fileName.indexOf("http://sterling.live.ptsdirectonline.com") < 0))
			window.open(fileName, "_new");
		else 
			document.location = $(this).attr("name");
	});
	
	$("input.button").hover(function() {
		$(this).addClass("hover");
	});
	
	$("input.button").mouseout(function() {
		$(this).removeClass("hover");
	});
	
	$("#btnSubmit").click(function() {
		if (requiredsAreFilled())
		{
			switch ($("#actionRequested").val().toUpperCase())
			{
				case "EMAIL MARKETING FORM": 
					emailMarketingForm();
					break;
					
				case "EMAIL CONTACT FORM":
					emailContactForm();
					break;
					
				case "EMAIL APPLICATION FORM":
					emailApplicationForm();
					break;
					
				default: break;
			}
		}
		else
		{
			errorMessage = "The form could not be submitted.\n" + 
				"Please make the following corrections, and then resubmit.\n\n" + 
				errorMessage;
				
			alert(errorMessage);
			errorMessage = "";
			errorIndex = 1;
		}
	});
	
	$("#btnReset").click(function() { 
		$(":input","#frmMain")  
			.not(":button, :submit, :reset, :hidden")  
			.val("")  
			.removeAttr("checked")  
			.removeAttr("selected"); 
	});
	
	adjustContainers();
});

$(window).resize(function() {
	adjustContainers();
});

function adjustContainers()
{
	if (!$(document).find("div [id$='-container']").css("top"))
		return false;
		
	var browserVersion = $.browser.version;
	browserVersion = browserVersion.substr(0, browserVersion.indexOf("."));
	
	// Make the necessary layout adjustments based on the browser version
	var centererOuterWidth = "100%";
	var mainBottomNew = 40;
	var footerLeftNew = 34;
	var tickerLeftNew = 34;
	var footerHeightCap = 70;
	
	if ($.browser.msie && parseInt(browserVersion) < 8)
	{
		centererOuterWidth = "99%"
		mainBottomNew = 44;
		footerLeftNew = 0;
		tickerLeftNew = 0;
		footerHeightCap = 83;
	}
		
	$("#centerer-outer").css("width", centererOuterWidth);
	$("#main-bottom").css("bottom", mainBottomNew);
	$("#footer").css("left", footerLeftNew);
	$("#footer").css("left", footerLeftNew);
	$("#image-footer-quote").css("left", footerLeftNew + 1);
	$("#image-footer-ad").css("left", footerLeftNew + 1);
	$("#ticker").css("left", tickerLeftNew);

	var footerTop = $("#footer").offset().top;
	var footerWidth = document.documentElement.clientWidth - 1;
	$("#ticker-container").css("top", footerTop - 24);
	$("#ticker-container").css("width", footerWidth);
	
	$("#footer-container").css("top", footerTop);
	$("#footer-container").css("width", footerWidth);
	
	var footerHeight = $(window).height() - parseInt(footerTop);
	footerHeight = (footerHeight < footerHeightCap) ? 110 : footerHeight;
	$("#footer-container").css("height", footerHeight);
	
	// To prevent movement of #main, adjust #centerer-inner if there is no scrollbar
	var newLeft = $.browser.msie ? -11 : -10;
	if (!isScrolling())
		$("#centerer-inner").css("left", newLeft);
		
	// Adjust the main container's shadow placement
	var shadowTop = ($("#main").height() == 420) ?
		140 : 
		($("#main").height() - $("#main-shadow").height())/2 + 120;
	$("#main-shadow").css("top", shadowTop);
}

function isScrolling() 
{   
	var docHeight = $(document).height() - ($.browser.msie ? 4 : 0);   
	var scroll = $(window).height() + $(window).scrollTop();   
	return !(docHeight == scroll); 
} 

function emailMarketingForm()
{
	var queryString = 
		"actionRequested=" + encodeURIComponent($("#actionRequested").val()) + 
		"&contactName=" + encodeURIComponent($("#contactName").val()) + 
		"&contactEmail=" + encodeURIComponent($("#contactEmail").val()) + 
		"&message=" + encodeURIComponent($("#message").val()) + 
		"&altBox=" + encodeURIComponent($("#altBox").val());
		
	// Create the function to be called on success of ajaxCall
	var onSuccess = function(data) { alert(data); };
	
	// callAjax(ajaxFileName, queryString, fn_beforeSend, fn_onSuccess, hasLoader)
	callAjax("ajaxfunctions.asp", queryString, null, onSuccess, false);
}

function emailContactForm()
{
	var queryString = 
		"actionRequested=" + encodeURIComponent($("#actionRequested").val()) + 
		"&contactName=" + encodeURIComponent($("#contactName").val()) + 
		"&contactEmail=" + encodeURIComponent($("#contactEmail").val()) + 
		"&subject=" + encodeURIComponent($("#subject").val()) + 
		"&message=" + encodeURIComponent($("#message").val()) + 
		"&altBox=" + encodeURIComponent($("#altBox").val());
		
	// Create the function to be called on success of ajaxCall
	var onSuccess = function(data) { alert(data); };
	
	// callAjax(ajaxFileName, queryString, fn_beforeSend, fn_onSuccess, hasLoader)
	callAjax("ajaxfunctions.asp", queryString, null, onSuccess, false);
}

function emailApplicationForm()
{
	var queryString = 
		"actionRequested=" + encodeURIComponent($("#actionRequested").val()) + 
		"&applicantName=" + encodeURIComponent($("#applicantName").val()) + 
		"&applicantEmail=" + encodeURIComponent($("#applicantEmail").val()) + 
		"&positionSought=" + encodeURIComponent($("#label-positionSought").attr("innerHTML")) + 
		"&applicantState=" + encodeURIComponent($("#applicantState :selected").text()) + 
		"&applicantInfo=" + encodeURIComponent($("#applicantInfo").val()) + 
		"&altBox=" + encodeURIComponent($("#altBox").val());
		
	// Create the function to be called before the ajax call is processed
	var beforeSend = function() {
		$("#frmMain")
			.attr("target", "upload_target")
			.attr("action", "fileupload.asp")
			.submit();
	};

	// Create the function to be called on success of ajaxCall
	var onSuccess = function(data) { alert(data); };
	
	// callAjax(ajaxFileName, queryString, fn_beforeSend, fn_onSuccess, hasLoader)
	$("#frmMain")
		.attr("target", "upload_target")
		.attr("action", "fileupload.asp")
		.submit(callAjax("ajaxfunctions.asp", queryString, beforeSend, onSuccess, true));
}

function callAjax(ajaxFileName, queryString, fn_beforeSend, fn_onSuccess, hasLoader)
{
	if (hasLoader)
	{
		$("#loader")
			.ajaxSend(function() {
				$(this).show();
			})
			.ajaxStop(function() { 
				$(this).fadeOut();
			});
	}
	
	var asyncStatus = (fn_beforeSend == null) ? true : false;
	
	$.ajax({
		url:		ajaxFileName,
		cache:		false,
		async:		asyncStatus,
		beforeSend:	fn_beforeSend,
		data:		queryString,
		success:	fn_onSuccess,
		dataType:	"html"
	});
}

function requiredsAreFilled()
{
	var requiredVerb, article, title;
	$(":input","#frmMain")  
		.not(":button, :submit, :reset, :hidden, :file")  
		.each(function() {
			if (trim($(this).attr("value")).length == 0)
			{
				requiredVerb = $(this).attr("id").toUpperCase().
					indexOf("FILE") >= 0 ? 
					" provide " : " enter ";
					
				article = ("AEIOU".indexOf(
					$(this).attr("title").toUpperCase()
					.substring(0, 1)) < 0) ? 
					"a" : "an";
					
				title = $(this).attr("title");
				if (trim(title) != "")
				{
					errorMessage += errorIndex + ") Please" + requiredVerb + article + " " + 
						$(this).attr("title") + ".\n";
					errorIndex++;
				}
			}
		});
		
	// Check for an email address and validate it
	try
	{
		var emailAddress = $("input[id$='Email']").val();
		
		if (emailAddress.length > 0)
			if (!emailIsValid(emailAddress))
			{
				errorMessage += errorIndex + ") Please provide a valid email address.\n";
				errorIndex++;
			}
	}
	catch (e) {}
	
	if (trim(errorMessage).length == 0)
		return true;
	else
		return false;
}

function trim(stringValue)
{
	return stringValue.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");	
}

function emailIsValid(emailAddress)
{
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);

	return regex.test(emailAddress);
}
