/* a whole bunch of convenience scripts, some by me, some not
   www.joostschuttelaar.nl
 */

/* makes links with attribute rel="external" open in a new window */
function externalLinks() { 
	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") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank"; 
		}
	}
}

if (window.addEventListener) {
	window.addEventListener('load', externalLinks, false); 
} else if (window.attachEvent) { 
	window.attachEvent('onload', externalLinks);
}

/* popup new window in the center of the screen */
function popup(pageUrl, width, height) {
	var left = screen.width / 2 - width / 2;
	var top = screen.height / 2 - height / 2;
	
	window.open(pageUrl, '', 'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + ",scrollbars=yes,resizable=no");
}

/* returns the fragment of the URL after the # */
function getUrlFragment() {
	if (window.location.hash != undefined &&
		window.location.hash != '') {
		var hasHash = window.location.hash.indexOf('#');
		if (hasHash == -1) {
			return window.location.hash;
		} else if (window.location.hash.length > 1) {
			return window.location.hash.substr(hasHash + 1);
		} else {
			return false;
		}
	}
	return false;
}

/* from http://developer.apple.com/internet/safari/faq.html */
function isAppleWebkit() {
	var kitName = "applewebkit/";
	var tempStr = navigator.userAgent.toLowerCase();
	var pos = tempStr.indexOf(kitName);
	return (pos != -1);
}


function isNumericString(validationString) {
	return validationString.match('^[0-9]+$');
}

String.prototype.isEmailAddress = function() { 
	var emailTestRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	return this.match(emailTestRegex);
};

// two string methods from http://www.ditchnet.org/wp/2005/04/04/i-want-my-javalang/

/**
 *  String convenience method to trim leading and
 *  trailing whitespace.
 *  @returns string
 */
String.prototype.trim = function() { 
	return this.replace(/^\s+|\s+$/g, '');
};

/**
 *  String convenience method for checking if the
 *  end of this string equals a given string.
 *
 *  @returns boolean
 *  @throws IllegalArgumentException for parameters
 *                          not of type String
 */
String.prototype.endsWith = function (s) {
    if ('string' != typeof s) {
        throw('IllegalArgumentException: Must pass a ' +
            ' string to String.prototype.endsWith()');
    }
    var start = this.length - s.length;
    return this.substring(start) == s;
};

// from http://www.irt.org/script/1621.htm
function wordcount(string) {
  var a = string.split(/\s+/g); // split the sentence into an array of words
  return a.length;
}

/* cookie methods from http://www.quirksmode.org/js/cookies.html */

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

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

function eraseCookie(name) {
	createCookie(name,"",-1);
}

$(document).ready(function() {
	$('table.spreadsheet tr:even').addClass('even');
	$('table.spreadsheet tr:odd').addClass('odd');
});
