﻿/*** Utilities ***/

function buildQueryString(arr)
{
	// Declare the query string
	var queryString = "";
	
	if(typeof(arr) == "object")
	{
		for(var item in arr)
		{
			// Get the item value
			var value = arr[item];
			
			// If the item is equal to our global filtering properties, we need to add it to the query string manually
			if(item == "Location" && typeof(value) == "string")
			{
				// If the location value is longer than zero
				if(value.length > 0)
				{
					queryString += "&Location=" + value;
				}
			}
			else if(item != "Location" && typeof(value) == "string")
			{
				// Convert special characters to urlencoded values
				value = urlencodeSpecialChars(value);
				
				// Add this to the query string
				queryString += "&" + item.replace("_", "%20") + "=" + value;
			}
			else if(item != "Location" && typeof(value) == "object")
			{
				// Begin the query string
				queryString += "&" + item.replace("_", "%20") + "=";
				
				// Begin a temporary array
				querySegment = new Array();
				
				for(var subItem in value)
				{
					// Action to take if "All" is specified for a particular field.
					if(subItem == "All" && value[subItem] == true)
					{
						// Empty the query segment
						querySegment = [];
						
						// Push the "All" value into the array
						querySegment.push("All");
						
						// Break out of this loop
						break;
					}
					else if(value[subItem] == true)
					{
						// Convert special characters to urlencoded values
						subItem = urlencodeSpecialChars(subItem);
					
						// Push the sub item into the query segment
						querySegment.push(subItem);
					}
				}
				
				// Loop through the query segment values
				for(var x in querySegment)
				{
					// Append the query segment value to the query string
					queryString += querySegment[x];
					
					// If we're not on the last item of the array, separate with a pipe symbol
					if(x < (querySegment.length - 1))
					{
						queryString += "|";
					}
				}
			}
		}
	}
	
	// Return the querystring
	return queryString.substr(1, queryString.length);
}

function objectifyQueryString(queryString)
{
	// Split the string into an array
	var queryStringArr = queryString.split("&");
	
	// Loop through the array and build the object filters
	for(var i in queryStringArr)
	{
		// Split the key/value by the equal sign
		var tempArr = queryStringArr[i].split("=");
		
		// Replace spaces with underscores
		tempArr[0] = spaceToUnderscore(tempArr[0]);
		
		if(tempArr[0] == "Job_Field" || tempArr[0] == "Job_Level" || tempArr[0] == "Shift")
		{
			// Split the string by pipes
			var pipedArr = tempArr[1].split("|");
			
			// If this filter isn't defined, define it as an object
			if(jQuery.filters[pipedArr[0]] == undefined)
			{
				jQuery.filters[tempArr[0]] = {};
			}
			
			// Cycle through the piped array and add its values to the target filter
			for(var x in pipedArr)
			{
				// Remove odd characters
				var tempFilterString = urldecodeSpecialChars(pipedArr[x]);
				
				jQuery.filters[tempArr[0]][tempFilterString] = true;
			}
		}
		else if(tempArr[0] == "Location")
		{
			if(urldecodeSpecialChars(tempArr[1]) != "Enter a location (city or state)")
			{
				if(tempArr[1].length > 0)
				{
					jQuery.filters.Location = tempArr[1];
				}
				else
				{
					jQuery.filters.Location = {};
				}
			}
		}
		else if(tempArr[0] == "Free_Text")
		{
			if(urldecodeSpecialChars(tempArr[1]) != "Enter keyword (e.g. sales) job title or job number" && urldecodeSpecialChars(tempArr[1]) != "Enter key word (e.g. sales) job title or job number")
			{
				jQuery.filters.Free_Text = tempArr[1];
			}
		}
		else if(tempArr[0] == "Country")
		{
			if(urldecodeSpecialChars(tempArr[1].toLowerCase()) != "select a country")
			{
				jQuery.filters.Country = tempArr[1];
			}
		}
		else
		{
			// Apply the key/value pair to the jQuery filters object
			jQuery.filters[tempArr[0]] = tempArr[1];
		}
	}
}

// Sort helpers
function postDateDescending(a, b)
{
	if(a.ODE != null && b.ODE != null)
	{
		return convertToYMD(b.ODE) - convertToYMD(a.ODE);
	}
}

function postDateAscending(a, b)
{
	if(a.ODE != null && b.ODE != null)
	{
		return convertToYMD(a.ODE) - convertToYMD(b.ODE);
	}
}

function jobTitleDescending(a, b)
{
	if(a.T != null && b.T != null)
	{
		var sortArr = removeSpecialCharacters(a.T, b.T);
		
		if(sortArr[0] > sortArr[1])
		{
			return -1;
		}
		
		if(sortArr[0] < sortArr[1])
		{
			return 1;
		}
		
		return 0;
	}
}

function jobTitleAscending(a, b)
{
	if(a.T != null && b.T != null)
	{
		var sortArr = removeSpecialCharacters(a.T, b.T);
		
		if(sortArr[0] > sortArr[1])
		{
			return 1;
		}
		
		if(sortArr[0] < sortArr[1])
		{
			return -1;
		}
		
		return 0;
	}
}

function locationDescending(a, b)
{
	if(a.SP != null && b.SP != null )
	{
		var sortArr = removeSpecialCharacters(a.SP, b.SP);
		
		if(sortArr[0] > sortArr[1])
		{
			return -1;
		}
		
		if(sortArr[0] < sortArr[1])
		{
			return 1;
		}
		
		return 0;
	}
	else
	{
		return -1;
	}
}

function locationAscending(a, b)
{
	if(a.SP != null && b.SP != null)
	{
		var sortArr = removeSpecialCharacters(a.SP, b.SP);
		
		if(sortArr[0] > sortArr[1])
		{
			return 1;
		}
		
		if(sortArr[0] < sortArr[1])
		{
			return -1;
		}
		
		return 0;
	}
	else
	{
		return -1;
	}
}

function organizationNameDescending(a, b)
{
	if(a.BS != null && b.BS != null)
	{
		var sortArr = removeSpecialCharacters(a.BS, b.BS);
		
		if(sortArr[0] > sortArr[1])
		{
			return -1;
		}
		
		if(sortArr[0] < sortArr[1])
		{
			return 1;
		}
		
		return 0;
	}
}

function organizationNameAscending(a, b)
{
	if(a.BS != null && b.BS != null)
	{
		var sortArr = removeSpecialCharacters(a.BS, b.BS);
		
		if(sortArr[0] > sortArr[1])
		{
			return 1;
		}
		
		if(sortArr[0] < sortArr[1])
		{
			return -1;
		}
		
		return 0;
	}
}

// Function to remove all non-consequential characters from a string
function removeSpecialCharacters(a, b)
{
	// Create the regex pattern
	var specialChars = /[^a-z0-9]/gi;
	
	// Assign the variables
	var aSort = a.toLowerCase();
	var bSort = b.toLowerCase();
	
	// Remove the special characters in the sort variables
	aSort = aSort.replace(specialChars, "");
	bSort = bSort.replace(specialChars, "");
	
	return [aSort, bSort];
}

function urlencodeSpecialChars(str)
{
	// Convert special characters to urlencoded values
	str = str.replace("&", "%26");
	str = str.replace(" ", "%20");
	str = str.replace("=", "%3d");
	str = str.replace("#", "%23");
	str = str.replace(",", "%2c");
	
	// Return the string
	return str;
}

function urldecodeSpecialChars(str)
{
	// Convert special characters to urlencoded values
	str = str.replace("%26", "&");
	str = str.replace("%20", " ");
	str = str.replace("%3d", "=");
	str = str.replace("%3D", "=");
	str = str.replace("%23", "#");
	str = str.replace("%2c", ",");
	str = str.replace("%2C", ",");
	str = str.replace(/\+/gi, " ");
	
	// Return the string
	return str;
}

function spaceToUnderscore(str)
{
	// Convert spaces and urlencoded equivalents to underscores
	str = str.replace(" ", "_");
	str = str.replace("%20", "_");
	str = str.replace(/\+/gi, " ");
	
	// Return the string
	return str;
}

// Email validation function
function validateEmail(email)
{
	with(email)
	{
		apos = indexOf("@")
		dotpos = lastIndexOf(".")

		if(apos < 1 || dotpos - apos < 2) 
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}

// Standard date to sortable date helper function
function convertToYMD(dateStr)
{
	// Check to see if the string has the required separators
	if(dateStr.indexOf("/") != -1)
	{
		// Split the date string by its separators
		dateArr = dateStr.split("/");
		
		// Return the ymd
		return parseInt(dateArr[2] + dateArr[0] + dateArr[1]);
	}
	else
	{
		return false;
	}
}