var loadedCss = false;

function loadCss(filename) {
	var head = document.getElementsByTagName('head')[0];

	var tag=document.createElement('link');
	tag.setAttribute('rel', 'stylesheet');
	tag.setAttribute('type', 'text/css');
	tag.setAttribute('href', filename);

	if (typeof tag != 'undefined') {
		head.appendChild(tag);
	}
}

function leavingBYU(obj, churchSite) {
	var content;
	var hyperText = (obj.title)? obj.title : obj.innerHTML;

	var stmnt = '<p>Brigham Young University has provided this link to an Internet site maintained by a third party. Although intended to be of high quality and good value, Brigham Young University&rsquo;s provision of this external link does not imply approval, warrant the accuracy or timelineness of the information, or endorsement of the website, its sponsoring agencies, organizations, views, products or services.</p><hr/>';
	var title = 'You are leaving the BYU Web site';
	var fromLink = '<a href="" onClick="Modalbox.hide();return false" class="left">Back to BYU</a>';
	var toLink = '<a onClick="Modalbox.hide();return true" target="_blank" href="'+obj.href+'" class="right">'+hyperText+'</a>';
	var prompt = '<p class="prompt">Please select an action:</p>';

	if (!churchSite) {
		content = stmnt + prompt + fromLink + toLink;
	} else	{
		content = prompt + fromLink + toLink;
	}

	if (! loadedCss) {
		loadCss('/home/js/modalbox/modalbox.css');
		loadedCss = true;
	}

	/* All scripts loaded as a group, so only need to check for most important one. */
	if (typeof(Modalbox) == 'undefined') {
		$LAB
		//.script('/home/js/prototype/prototype.js').wait()
		.script('/home/js/scriptaculous/effects.js').wait()
		.script('/home/js/scriptaculous/builder.js').wait()
		.script('/home/js/modalbox/modalbox.js')
		.wait(function() { Modalbox.show(content, {title: title, width: 480})
		});
	} else	{
		Modalbox.show(content, {title: title, width: 480})
	}
	return false;
}

function feedback() {
	var formUrl = '/home/fbForm.html';
	var title   = 'Web Feedback Form';

	if (! loadedCss) {
		loadCss('/home/js/modalbox/modalbox.css');
		loadedCss = true;
	}

	/* All scripts loaded as a group, so only need to check for the most important one. */
	if ((typeof Modalbox == "undefined") || typeof Modalbox == null) {
		$LAB
		//.script('/home/js/prototype/prototype.js').wait()
		.script('/home/js/scriptaculous/effects.js').wait()
		.script('/home/js/scriptaculous/builder.js').wait()
		.script('/home/js/modalbox/modalbox.js')
		.wait(function() { Modalbox.show(formUrl, {title: title, width: 420, height: 420}) });
	} else	{
		Modalbox.show(formUrl, {title: title, width: 420, height: 420});
	}
	return false;
}

function submitFbForm() {
	var errFld = $('errMsg');
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	var errText = 'Submit Error: ';
	var dept = document.fbForm.department.value;
	var generalEmail = 'dceweb@byu.edu';
	var bgsEmail = 'bgs@byu.edu';
	var cwEmail = 'cwmail@byu.edu';
	var efyEmail = 'efy@byu.edu';
	var evEmail = 'evening_classes@byu.edu';
	var isEmail = 'indstudy@byu.edu';
	var jcEmail = 'jerusalem@byu.edu';
	var slEmail = 'slc@byu.edu';
	var emailAddr;

	switch (dept) {
	  case 'bgs':
	  	emailAddr = bgsEmail;
		break;
	  case 'cw':
	  case 'edweek':
	  	emailAddr = cwEmail;
		break;
	  case 'ev':
	  	emailAddr = evEmail;
		break;
	  case 'is':
	  	emailAddr = isEmail;
		break;
	  case 'jc':
	  	emailAddr = jcEmail;
		break;
	  case 'sl':
	  	emailAddr = slEmail;
		break;
	  case 'yp':
	  	emailAddr = efyEmail;
		break;
	  default:
	  	emailAddr = generalEmail;
	}

	document.fbForm.feedbackTo.value = emailAddr;

	if (document.fbForm.name.value == "") {
		errFld.innerHTML = errText + 'Please specify your name.';
		document.fbForm.name.focus();
	} else if (document.fbForm.email.value == "") {
		errFld.innerHTML = errText + 'Please specify your email address.';
		document.fbForm.email.focus();
	} else if (! document.fbForm.email.value.match(emailExp)) {
		errFld.innerHTML = errText + 'Invalid email address.';
		document.fbForm.email.focus();
	} else if (document.fbForm.message.value == "") {
		errFld.innerHTML = errText + 'Please provide a message.';
		document.fbForm.message.focus();
	} else if (document.fbForm.message.value.length > 2000) {
		errFld.innerHTML = errText + 'Your message length cannot exceed 2000 characters.';
		document.fbForm.message.focus();
	}  else {
		Modalbox.show('/home/sendFbForm.cfm', {title: 'Sending Feedback', width: 408,  method: 'post', params: Form.serialize('fbForm') });
	}

	return false;
}


function popup(url) { 
	window.open(url,'popup','width=420,height=458,resizable=yes,scrollbars=no'); 
} 

function nonBYUsite(url) {
	var regEx = /mormon|lds|familysearch|ldscatalog|deseret/;
	var churchSite = false;
	var text = url.split('//')[1];
	text = text.split('?')[0];
	text = text.split('/')[0];

	if (text.search(regEx) != -1)
		churchSite = true;

	var obj = {
	href: url,
	text: text
	}

	leavingBYU(obj, churchSite);
}    

