/***************************
 * SITE-SPECIFIC FUNCTIONS *
 ***************************/
 
 
/*********************
 * UTILITY FUNCTIONS *
 *********************/
 
function sendMail(user,domain,tld,subject,message) {
	if (!user) user = "info";
	if (!domain) domain = "visit-a-village";
	if (!tld) tld = "org";
	locationstring = "mailto:" + user + "@" + domain + "." + tld;
	if (subject) locationstring += "?subject=" + encodeURIComponent(subject);
	if (message) locationstring += "&body=" + encodeURIComponent(message);
	window.location = locationstring;
}

function openWindow(url,features) {
	var newWin = window.open(url,'popup',features);
	newWin.focus();
}

function closeWindow() {
	if (window.opener) {
		self.close();	
	}
}

function externalLinks() { 
	if (!document.getElementsByTagName) return;
	var pattern = /external/;
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && pattern.test(anchor.getAttribute("rel"))) 
		anchor.target = "_blank"; 
	} 
}

function filesToBlank() { 
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && /\.pdf$/i.test(anchor.href)) {
		anchor.target = "_blank";
		if (anchor.className != 'img') anchor.innerHTML += " [PDF]";
		}
		if (anchor.getAttribute("href") && /\.doc$/i.test(anchor.href)) {
		anchor.target = "_blank";
		if (anchor.className != 'img') anchor.innerHTML += " [DOC]";
		}
		if (anchor.getAttribute("href") && /\.pps$/i.test(anchor.href)) {
		anchor.target = "_blank";
		if (anchor.className != 'img') anchor.innerHTML += " [PPS]";
		}
		if (anchor.getAttribute("href") && /\.ppt$/i.test(anchor.href)) {
		anchor.target = "_blank";
		if (anchor.className != 'img') anchor.innerHTML += " [PPT]";
		}
	} 
}

function nullLinks() {
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var re = /\bnull\b/;
		var anchor = anchors[i]; 
		if (re.test(anchor.className)) {
			anchor.onclick = function() { return false; }
		}
	}
}

function printPage() {
	if(window.print != null) {
		window.print();
	}
	else {
		alert("To print this page, please select Print from your browser's File menu.");
	}
}

function preload() {
	var path = "/images/";
	var images = new Array("");
	var preload = new Array();
	var j = images.length;
	for (var i=0; i<images.length; i++) {
		preload[j] = new Image;
		preload[j++].src = path + images[i];
	}
}

function fixIE() { // Revises "suckerfish" CSS menu JS
	if (document.all && document.getElementById) {
		var navRoot = document.getElementById("nav");
		var listItems = navRoot.getElementsByTagName("LI");
		for (var i=0; i<listItems.length; i++) {
			e = listItems[i];
			e.onmouseover=function() {
				this.className+=" over";
			}
			e.onmouseout=function() {
				this.className=this.className.replace(" over","");
			}
		}
	}
}

function makeRollovers() { 
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a");
	var mouseovers = new Array;
	var j = 0;
	for (var i=0; i<anchors.length; i++) {
   	var e = anchors[i]; 
   	if (/\brollover\b/.test(e.className)) {
			mouseovers[j] = new Image;
			mouseovers[j].src = e.childNodes[0].src.replace('_up','_ov');
			j++;
			e.onmouseover = function() { 
				this.childNodes[0].src = this.childNodes[0].src.replace('_up','_ov');
			}
			e.onmouseout = function() {
				this.childNodes[0].src = this.childNodes[0].src.replace('_ov','_up');
			}		
		}
	} 
}

function init() {
	externalLinks();
	filesToBlank();
	nullLinks();
	makeRollovers();
}


/********************************************
 * FORM VALIDATION & MANIPULATION FUNCTIONS *
 ********************************************/
 
function clearFields() {
	for (i = 0; i<arguments.length; i++) {
		var e = document.getElementById(arguments[i]);
		e.value = '';
	}
}

function checkRadio(id) {
	e = document.getElementById(id);
	e.checked = true;
}

function formatNumber(id, digits) {
	var e = document.getElementById(id);
	var stripped = e.value.replace(/\D/g,'');
	if (stripped.length !== digits) {
		alert('Please enter a valid ' + digits + '-digit number.');
	}
	else {
		e.value = stripped;
	}
}

function checkValue(id, minimum, maximum) {
	if (!minimum) minimum = 0;
	if (!maximum) maximum = Number.MAX_VALUE;
	var e = document.getElementById(id);
	if (parseFloat(e.value) < minimum || parseFloat(e.value) > maximum) {
		alert('Please enter a number between ' + minimum + ' and ' + maximum + '.');	
	}
}

function formatPhone(id) { // Pass a form element's ID onchange
	var e = document.getElementById(id);
	var stripped = e.value.replace(/\D/g,''); // Strip out non-numerical characters
	if (e.value.match(/\w/)) { // If it's not empty...
		if (stripped.length != 10) {
			alert('Please enter a phone number with area code.');
			e.focus();
		}
		else {
			e.value = '(' + stripped.substring(0,3) + ') ' + stripped.substring(3,6) + '-' + stripped.substring(6,10);
			// formats number as (xxx) xxx-xxxx and writes to field
		}
	}
}

function formatZip(id) { // Pass a form element's ID onchange
	var e = document.getElementById(id);
	var stripped = e.value.replace(/\D/g,''); // Strip out non-numerical characters
	if (e.value.match(/\w/)) { // If it's not empty...
		if (stripped.length == 5) {
			return true;
		}
		else if (stripped.length == 9) {
			e.value = stripped.substring(0,5) + '-' + stripped.substring(5,9)
		}
		else alert('Please enter a valid 5- or 9-digit ZIP Code.');
	}
}

function formatSSN(id) { // Pass a form element's ID onchange
	var e = document.getElementById(id);
	var stripped = e.value.replace(/\D/g,''); // Strip out non-numerical characters
	if (e.value.match(/\w/)) { // If it's not empty...
		if (stripped.length != 9) {
			alert('Please enter a valid Social Security Number.');
			e.focus();
		}
		else {
			e.value = stripped.substring(0,3) + '-' + stripped.substring(3,5) + '-' + stripped.substring(5,9);
			// formats number as xxx-xx-xxxx and writes to field
		}
	}
}

function checkEmail(id) { // Pass a form element's ID onchange
	var e = document.getElementById(id);
	re = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	if(!re.test(e.value) && e.value.match(/\w/)) { // Passes the Regexp test and is not empty
		alert('"' + e.value + '" does not appear to be a valid email address. Please ensure you have provided an accurate email address before submitting this form.');
		e.select();
		e.focus();
	}
}

function checkURL(id) { // Pass a form element's ID onchange
	var e = document.getElementById(id);
	re = /^((http|https|ftp):\/\/)?([a-z0-9\-\.])+(\.)([a-z]){2,4}([a-z0-9\/\+=%&_\.~?\-])*$/i;
	if(!re.test(e.value) && e.value.match(/\w/)) { // Passes the Regexp test and is not empty
		alert('"' + e.value + '" does not appear to be a valid URL (website address). Please check for accuracy.');
		e.select();
		e.focus();
	}
}

function validate(f,buttontext,waitmessage) {
	var msg = '';
	if (!buttontext) buttontext = 'Submit';
	if (!waitmessage) waitmessage = 'Please Wait';
	f.elements['submit'].disabled = true;
	f.elements['submit'].value = waitmessage;

	for (var i=0; i<f.elements.length; i++) {
		var e = f.elements[i];
		var p = f.elements[i].parentNode;
		p.className = p.className.replace(/\berror\b/,'');
		if (e.required && !e.value.match(/\w/)) {
			msg += e.title + '\n';
			p.className += ' error';
		}
	}
	if (msg != '') {
		alert('The following fields are required:\n' + msg);
		f.elements['submit'].disabled=false;
		f.elements['submit'].value=buttontext;
		return false;
	}
}

