// HTTP_Request object
function createRequestObject()
{
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	else
		ro = new XMLHttpRequest();
	return ro;
}

function openWindow(theURL,winName,features)
{
	shopwindow = window.open(theURL,winName,features);
	shopwindow.focus();
}

// Standard AJAX function to get output from an external page web page
// Uses the HTTP_Request object to open a external(from itself) page, loads a loading.gif where it will put it's content,
// uses the HTTP_Request object and an anonymous function to update the given element on completion of the request
function getAjaxStyle(element, link)
{
	var http = createRequestObject();

	var loadingHTML = '<div style="height:39px;"><div style="width:201px; height:39px; float:left; text-align:center; padding-top:9px;">&nbsp;</div><div style="width:201px; height:39px; float:left; text-align:center; padding-top:9px;">&nbsp;</div><div style="width:201px; height:39px; float:left; text-align:center; padding-top:9px;">&nbsp;</div></div>';
	document.getElementById(element).innerHTML = loadingHTML;

	http.open('get', link);
	http.onreadystatechange = function()
	{
		if(http.readyState == 4)
		{
			document.getElementById(element).innerHTML = http.responseText;
			if(link == 'careerscontent.php?idx=2' || link == 'trainingcontent.php?idx=1' || link == 'trainingcontent.php?idx=3' || link == 'searchcontent.php?idx=3' || link == 'trainingcontent.php?idx=2')
			{
				window.addEvent('domready', function() {
					// Create our Accordion instance
					var myAccordion = new Accordion($('accordion'), 'div.accordionToggler', 'div.accordionElement', {
						opacity: true,
						onActive: function(toggler, element){
							toggler.setStyle('color', '#666666');
						},
						onBackground: function(toggler, element){
							toggler.setStyle('color', '#0f4aa2');
						}
					});
				});
			}
		}
	};
	http.send(null);
}

//Processes 'link'
function processAjax(link)
{
	var http = createRequestObject();
    http.open('get', link, false);
    http.send(null);
}

// Generic function to collect information from a form(formName)
// Constructs the proper link and call getAjaxStyle
// Allowed getAjaxStyle to place the loading gif or else the information we want to retrieve will be lost
function postAjaxStyle(formName, link, updateElement)
{
	var f = document.forms[formName];
//TODO Account for checkboxes
//alert("Parameters passed: FORMNAME => " + formName + ", LINK => " + link + ", UPDATE ELEMENT => " + updateElement + ", FORM_ELEMENTS_LENGTH => " + f.elements.length);
	for(var i = 0; i < f.elements.length; i++)
	{
		if(f.elements[i].type != 'submit' && f.elements[i].type != 'button' && f.elements[i].type != 'image' && f.elements[i].type != 'file')
		{
			if(i == 0)
				link = link + '?';
			else
				link = link + '&';
			if(f.elements[i].type == 'select')	// Not sure if 'select' is the correct return string
				link = link + f.elements[i].id + '=' + urlencode(f.elements[i].options[f.elements[i].selectedIndex].value);
			else if(f.elements[i].type == 'text' || f.elements[i].type == 'textarea')
				link = link + f.elements[i].id + '=' + urlencode(f.elements[i].value);
		}
	}
	getAjaxStyle(updateElement, link);

	return true;
}

// Execute the given display action on the given element
function showHide(element, action)
{
	var ele = document.getElementById(element);
	ele.style.display = action;
}

function openLightBox(ele, width)
{
	$(ele).setStyle('opacity','0');
	$(ele).setStyle('top', '14%');
	$(ele).setStyle('left', '50%');
	$(ele).setStyle('margin-left', '-' + (width / 2) + 'px');
	if(width != null)
		$(ele).setStyle('width', width + 'px');
	else
		$(ele).setStyle('width', '300px');
	$(ele).setStyle('display', 'block');
	$('coverDiv').setStyle('opacity', '0');
	$('coverDiv').setStyle('display', 'block');
	var editBox = new Fx.Tween(ele);
	var coverBox = new Fx.Tween('coverDiv');
	editBox.start('opacity', '0', '1');
	coverBox.start('opacity', '0', '.7');
}

function getScrollXY(type)
{
	var scrOfX = 0, scrOfY = 0;
	if(typeof(window.pageYOffset) == 'number')
	{
		// Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	}
	else if(document.body && (document.body.scrollLeft || document.body.scrollTop))
	{
	    // DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	}
	else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
	{
		// IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	
	if(type == 'X')
		return scrOfX;
	else if(type == 'Y')
		return scrOfY;
	else
		return [scrOfX, scrOfY];
}

function openLightBoxOnCenter(ele, width, yPadding, cover)
{
	$(ele).setStyle('opacity', '0');
	if(yPadding == null || yPadding == 'undefined' || yPadding == 0)
		yPadding = 50;

	var scrollX = 0;
	var scrollY = 0;

	if(window.innerWidth)
	{
		bsw = window.innerWidth;
	}
	else if(document.documentElement)
	{
		bsw = document.documentElement.clientWidth;
	}
	else if(document.body)
	{
		bsw = document.body.clientWidth;
	}

	scrollX = getScrollXY('X');
	$(ele).setStyle('left', ((bsw / 2) + scrollX) + 'px');
	$(ele).setStyle('margin-left', '-' + (width / 2) + 'px');

	scrollY = getScrollXY('Y');
	$(ele).setStyle('top', (scrollY + parseInt(yPadding)) + 'px');
	
	if(width != null)
		$(ele).setStyle('width', width + 'px');
	else
		$(ele).setStyle('width', '400px');

	$(ele).setStyle('display', 'block');
	if(cover != false)
	{
		$('coverDiv').setStyle('opacity', '0');
		$('coverDiv').setStyle('display', 'block');
	}
	var lightBox = new Fx.Tween(ele);
	if(cover != false)
	{
		var coverBox = new Fx.Tween('coverDiv');
		coverBox.start('opacity', '0', '.7');
	}
	lightBox.start('opacity', '0', '1');
}

function closeLightBox(ele)
{
	var editBox = new Fx.Tween(ele);
	var coverBox = new Fx.Tween('coverDiv');
	editBox.start('opacity', '1', '0');
	coverBox.start('opacity', '.7', '0');
}

function validateContactForm(formName)
{
	var f = document.forms[formName];
	var firstName = f.firstName.value;
	if(firstName == '')
	{
		alert('-First Name is required-\n\nPlease fill it out to continue.\n\nThank you.');
		f.firstName.focus();
		return false;
	}
	var lastName = f.lastName.value;
	if(lastName == '')
	{
		alert('-Last Name is required-\n\nPlease fill it out to continue.\n\nThank you.');
		f.lastName.focus();
		return false;
	}
	var email = f.email.value;
	if(email == '')
	{
		alert('-Email is required-\n\nPlease fill it out to continue.\n\nThank you.');
		f.email.focus();
		return false;
	}
	if(!validEmail(email))
	{
		alert('-Email must be in a proper format-\n\nPlease enter a valid email address to continue.\n\nThank You.');
		f.email.focus();
		return false;
	}

	return true;
}

function validateResumeForm(formName)
{
	var f = document.forms[formName];
	var firstName = f.firstName.value;
	if(firstName == '')
	{
		alert('-First Name is required-\n\nPlease fill it out to continue.\n\nThank you.');
		f.firstName.focus();
		return false;
	}
	var lastName = f.lastName.value;
	if(lastName == '')
	{
		alert('-Last Name is required-\n\nPlease fill it out to continue.\n\nThank you.');
		f.lastName.focus();
		return false;
	}
	var email = f.email.value;
	if(email == '')
	{
		alert('-Email is required-\n\nPlease fill it out to continue.\n\nThank you.');
		f.email.focus();
		return false;
	}
	if(!validEmail(email))
	{
		alert('-Email must be in a proper format-\n\nPlease enter a valid email address to continue.\n\nThank You.');
		f.email.focus();
		return false;
	}
	var file_cover_status = f.file_cover_status.value;
	if(file_cover_status == '')
	{
		alert('-A Cover Letter is required-\n\nPlease fill it out to continue.\n\nThank you.');
		f.file_cover_status.focus();
		return false;
	}
	var file_resume_status = f.file_resume_status.value;
	if(file_resume_status == '')
	{
		alert('-A Resume is required-\n\nPlease fill it out to continue.\n\nThank you.');
		f.file_resume_status.focus();
		return false;
	}

	return true;
}

function validEmail(email)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(!filter.test(email))
		return false;

	return true;
}

//Dynamically places an input and calendar instance in the given container and connects it to the correct form.
//N_CALNUM keeps track of how many calendars have been produced so that we never have any 'same id' errors
var N_CALNUM = 1;
function createCalendar(container, form, name, value)
{
	var div = document.getElementById(container);
	div.innerHTML = '<input type="text" id="' + name + '" name="' + name + '" value="' + value + '" />'
		+ '<img title="Open Calendar" class="tcalIcon" onclick="A_TCALS[\'myCalID' + N_CALNUM + '\'].f_toggle();" id="tcalico_myCalID' + N_CALNUM + '" src="img/cal.gif" /><br />';

	new tcal ({
		'formname': form,
		'controlname': name,
		'id': 'myCalID' + N_CALNUM
	});

	N_CALNUM++;
}
