//
// BackPocket JavaScript                                            
//
// Copyright 2007 by Prophet.
// All rights reserved.
//

// ----------------------------------------------------------------------
// Handler for "standard" forms to check for required field entries. The
// required fields are those that have a label with class "required".
// ----------------------------------------------------------------------

function check_standard_form (form) {
	var message = "";
	var labels = form.getElementsByTagName("LABEL");
	for (var i=0; i<labels.length; i++) {
		var label = labels.item(i);
		if (Element.hasClassName(label, "required")) {
			var field_ok = true;
			var field_name = label.getAttribute("for") ? label.getAttribute("for") : label.getAttribute("htmlFor");
			if (! field_name) { continue; }
			var field = $(field_name);
			if (! field) { continue; }
			var field_tag = field.nodeName.toUpperCase();
			if ((field_tag == "INPUT" || field_tag == "TEXTAREA") && field.value.match(/^\s*$/)) {
				field_ok = false;
			} else if (field_tag == "SELECT" && field.options[field.selectedIndex].value == "") {
				field_ok = false;
			}
			if (! field_ok) {
				field_desc = label.innerHTML.stripTags().replace(/:$/, "");
				message += "Please provide a value for " + field_desc + ".\n";
				Element.addClassName(field, "missing");
			}
		}
	}
	if (message != "") {
		alert(message);
		return false;
	} else {
		return true;
	}
}

// ----------------------------------------------------------------------
// Fetch a cookie value. If it doesn't exist, null is returned.
// ----------------------------------------------------------------------

function get_cookie (name) {
	var cookies = document.cookie.split(";");
	for (var i=0; i < cookies.length; i++) {
		var cookie = cookies[i];
		while (cookie.charAt(0) == " ") {
			cookie = cookie.substring(1,cookie.length);
		}
		if (cookie.indexOf(name+"=") == 0) {
			return cookie.substring(name.length+1,cookie.length);
		}
	}
	return null;
}

// ----------------------------------------------------------------------
// Fetch the current time in milliseconds since the Epoch. Useful if you
// need a per-browser unique value (but only once per millisecond).
// ----------------------------------------------------------------------

function get_current_ms () {
	var now = new Date();
	return now.getTime();
}

// ----------------------------------------------------------------------
// Convenience function for opening a basic popup window.
// ----------------------------------------------------------------------

function popup (url, width, height, extra) {
	var spec = 'width='+width+',height='+height+',status,resizable';
	if (extra) { spec += ',' + extra; }
	var w = window.open(url, 'thepopup', spec);
	if (w) { w.focus(); }
	return w;
}

// ----------------------------------------------------------------------
// End of functions.
// ----------------------------------------------------------------------

// Make admin links open in new windows.

Event.observe(window, "load", function(){
	$A(document.getElementsByTagName("A")).each(function(a){
		if (a.rel == "admin") { a.target = "_blank"; }
	});
});

// Make external links have the option to open in new windows.

Event.observe(window, "load", function(){
	var counter = 0;
	$A(document.getElementsByTagName("A")).each(function(a){
		if (a.rel == "external") {
			a.target = "_blank";
			a.onclick = function () {
				if (this.href.match(/^https?:\/\//i)) {
					var parts = this.href.replace(/^https?:\/\//i, "").split("/");
					var domain = parts.shift().replace(/^www\./, "");
					// var path = parts.join("/");
					var title = this.innerHTML.toLowerCase().replace(/[^a-z0-9]+/g, "-");
					title = title.replace(/^-/, "");
					title = title.replace(/-$/, "");
					var track = domain + "/" + title;
					if (typeof(urchinTracker) == "function") { urchinTracker("/outbound/" + track); }
				}
			}.bind(a);
		}
	});
});
