// Default instructive message used in the form validation functions.
var DEFAULT_MSG = 'Please enter a value in this form field';

// Nav bar values
var offSet = 0;
var position = 109;
var menus = null;
if (navigator.userAgent.indexOf('Gecko') != -1) offSet -= 7; 

if (location.href.indexOf('home.html') != -1) {
	position = 259;
	var menus = new Array(
		new ypSlideOutMenu("menu1", "down", 30, position + offSet, 150, 88, null),
		new ypSlideOutMenu("menu3", "down", 181, position + offSet, 150, 240, null),
		new ypSlideOutMenu("menu4", "down", 331, position + offSet, 150, 44, null)
	);
} else {
	var menus = new Array(
		new ypSlideOutMenu("menu1", "down", 30, position + offSet, 149, 88, null),
		new ypSlideOutMenu("menu3", "down", 180, position + offSet, 149, 240, null),
		new ypSlideOutMenu("menu4", "down", 330, position + offSet, 149, 44, null)
	);
}

ypSlideOutMenu.writeCSS();

/**
 * Creates a new image object in cache for the preloadImages() function below.
 */
function gray (td, toggle) {
	var tdEl = document.getElementById(td);
	tdEl.style.background = (toggle == 'on') ? '#666' : '#000';
}

/**
 * Creates a new image object in cache for the preloadImages() function below.
 */
function newImage (arg) {
	if (document.images) {
		var rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}
	
/**
 * Swaps the image named in the first argument with the one in the second.
 */
function changeImages () {
	if (document.images && (preloadFlag == true)) {
		for (var i = 0; i < changeImages.arguments.length; i += 2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i + 1];
		}
	}
}

/**
 * Preloads the horizontal navigation images onLoad of the page so that there is
 * no initial delay when the user rolls over them.
 */
var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		var nav1_off = newImage('/atlas/img/nav/nav1_off.gif');
		var nav1_on = newImage('/atlas/img/nav/nav1_on.gif');
		var nav2_off = newImage('/atlas/img/nav/nav2_off.gif');
		var nav2_on = newImage('/atlas/img/nav/nav2_on.gif');
		var nav3_off = newImage('/atlas/img/nav/nav3_off.gif');
		var nav3_on = newImage('/atlas/img/nav/nav3_on.gif');
		var nav4_off = newImage('/atlas/img/nav/nav4_off.gif');
		var nav4_on = newImage('/atlas/img/nav/nav4_on.gif');
		var nav5_off = newImage('/atlas/img/nav/nav5_off.gif');
		var nav5_on = newImage('/atlas/img/nav/nav5_on.gif');
		preloadFlag = true;
	}
}

/**
 * Jumps to the selected item in the dropdown list.
 */
function jumpURL (dropdown) {
	location.href = dropdown.options[dropdown.selectedIndex].value;
}

/** 
 * Validates the login form.  
 */
function validateForm (objForm) {  
	var arrEls = objForm.elements;
	var isValid = true;
	errorActionReset();
	
	if (arrEls['first_name'] != null && arrEls['first_name'].value == '') {
		errorAction('first_name');
		isValid = false;
	}
	
	if (arrEls['last_name'] != null && arrEls['last_name'].value == '') {
		errorAction('last_name');
		isValid = false;
	}
	
	if (arrEls['email'] != null && (arrEls['email'].value == '' | !checkEmail(arrEls['email'].value))) {
		errorAction('email', 'This email address is not valid');
		isValid = false;
	}
	
	if (arrEls['company_name'] != null && arrEls['company_name'].value == '') {
		errorAction('company_name');
		isValid = false;
	}
	
	if (arrEls['descr'] != null && arrEls['descr'].options[arrEls['descr'].selectedIndex].value == '0') {
		errorAction('descr');
		isValid = false;
	}
	
	if (arrEls['state'] != null && arrEls['state'].options[arrEls['state'].selectedIndex].value == '0') {
		errorAction('state');
		isValid = false;
	}	
	
	if (arrEls['address_1'] != null && arrEls['address_1'].value == '') {
		errorAction('address_1');
		isValid = false;
	}
	
	if (arrEls['city_town'] != null && arrEls['city_town'].value == '') {
		errorAction('city_town');
		isValid = false;
	}
	
	if (arrEls['zip_code'] != null && arrEls['zip_code'].value == '') {
		errorAction('zip_code');
		isValid = false;
	}
	
	if (arrEls['telephone'] != null && arrEls['telephone'].value == '') {
		errorAction('telephone');
		isValid = false;
	}
	
	if (!isValid) {
		alert('The form was not filled out correctly.  Please review the data entered in the form fields marked by a blue bar.');
	}
	
	return isValid;
}

/**
 * Validates email address.
 */
function checkEmail (e) {

	var checkTLD = 1;
	var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = e.match(emailPat);

	if (matchArray == null) {
		return false;
	}
	var user = matchArray[1];
	var domain = matchArray[2];

	for (i = 0; i < user.length; i++) {
		if (user.charCodeAt(i) > 127) {
			return false;
	   	}
	}
	for (i = 0; i < domain.length; i++) {
		if (domain.charCodeAt(i) > 127) {
			return false;
	   	}
	}
	
	if (user.match(userPat) == null) {
		return false;
	}
	
	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null) {
		for (var i = 1; i <= 4; i++) {
			if (IPArray[i] > 255) {
				return false;
	   		}
		}
		return true;
	}
	
	var atomPat = new RegExp("^" + atom + "$");
	var domArr = domain.split(".");
	var len = domArr.length;
	for (i = 0; i < len; i++) {
		if (domArr[i].search(atomPat) == -1) {
			return false;
	  	}
	}
	
	if (checkTLD && domArr[domArr.length - 1].length != 2 && domArr[domArr.length - 1].search(knownDomsPat) == -1) {
		return false;
	}
	
	if (len < 2) {
		return false;
	}
	
	return true;
}

/**
 * Highlights the given span with bold red text.
 */
function errorAction (item, msg) {
	if (msg == null || msg == '') msg = DEFAULT_MSG;
	var spanEl = document.getElementById('span_' + item);
	spanEl.style.color = '#9cf';
	spanEl.title = msg;
}

/**
 * Resets all the spans to black and regular weight text.
 */
function errorActionReset () {
	for (var i = 0; i < document.getElementsByTagName('span').length; i++) {
		var spanEl = document.getElementsByTagName('span').item(i);
		if (spanEl.getAttribute('id') != null && spanEl.getAttribute('id').search('span_') != -1) {
			spanEl.style.color = '#000';
			spanEl.style.fontWeight = 'normal';
			spanEl.title = '';
		}
	}
}

/**
 * Open the news release windows.
 */
function openNews (file) {
	var newsWin;
	if (!newsWin || newsWin.closed) {
		newsWin = open(
			'/atlas/news/' + file + '.html',
			'news',
			"width=565,height=450,resizable=no,toolbar=no,status=no,menubar=no,scrollbars=yes"
		);
		newsWin.focus();
	} else if (newsWin || !newsWin.closed) {
		newsWin.focus();
	}
}

function checkError () {
	if (location.href.indexOf('?error') != -1) {
        return '<tr><td width="10" height="39"><img src="/img/blue-bar.gif" width="4" height="16" alt="" border="0"></td><td width="100%" valign="middle" class="maintext11">&nbsp;<strong>' + "<span style='color: red'>You've left a required field empty.</span>" + '</td></tr>';	
	} else {
		return '';
	}
}
