/***************************************************************************
 *
 * Common javascript functions - applies across pages
 *
 * Dependencies: prototype.js, openrico.js
 ***************************************************************************/


String.prototype.capitalize = function() {
	var val = this;
	var newVal = "";
	val = val.split(' ');
	for(var c=0; c < val.length; c++) {
		newVal += val[c].substring(0,1).toUpperCase() +
			val[c].substring(1,val[c].length) + ' ';
	}
	return newVal;
}

/* ------Style Sheet Switch ----------------------------------------------*/

/**
 * Switches style sheets
 * From A List Apart http://www.alistapart.com/articles/alternate/
 */
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

/**
 * Changes the skin preference.
 * Saves skin preference as a cookie, and loads new skin
 * @param id - the cookie name to set pref under (this allows you to set 
 * different skins for different sections of the site
 * @param skin - the name of the skin
 */
function setSkinByID(id, skin) {
	setCookie(id, skin, getExpDate(120,0,0)); //getExpDate in cookies.js
	setActiveStyleSheet(skin);
}

/**
 * Changes the skin preference.
 * Saves skin preference as a cookie, and loads new skin
 */
function setSkin(title) {
	setSkinByID("skin", title);
}

/**
 * Initialize skin preference.
 * Retrieves skin preference from cookie, and loads new skin.
 */
function initSkinPref(id) {
	var skin = getCookie(id);
	if (skin != null && skin.length>0){
		setSkinByID(id, skin);
	}
}

/* ------ Form Validation & error handling ----------------------------------------------*/

var ErrorDisplay = {
	showErrors: function(errorCollector, errorElem) {
		var s = "<p>Please correct the following errors and submit the form again:</p>";
		s += "<ul>";
		for (var i=0; i < errorCollector.errors.length; i++) {
			s += "<li>" + errorCollector.errors[i].msg + "</li>";
			Element.addClassName(errorCollector.errors[i].field+"_label", "error");
		}
		s += "</ul>";
		$(errorElem).innerHTML = s;

	},

	clear: function(errorElem) {
		$(errorElem).innerHTML = '';
		var labels = document.getElementsByTagName('label');
		for (var i=0; i < labels.length; i++) {
			Element.removeClassName(labels[i], "error");
		}
	}
}

var ErrorCollector = Class.create();
ErrorCollector.prototype = {
	initialize: function() {
		this.errors = new Array();
	},
	errors: null,

	add: function(fieldId, errorMsg) {
		this.errors.push({field: fieldId, msg: errorMsg});
	},

	hasErrors: function() {
		return this.errors.length > 0;
	},

	clear: function() {
		this.errors = new Array();
	}
};

/* ------ Preference Manager ----------------------------------------------*/

var Preferences = {
	
	getPreference: function(name, defaultValue, store) {
		var pref = getCookie(name);
		
		if (pref == null) {
			if (arguments.length > 1 && defaultValue != null) {
				pref = defaultValue;
			}
		}
		
		if (arguments.length == 3 && store == true) {
			this.setPreference(name, defaultValue);
		}
		return pref;
	},
	
	setPreference: function(name, value) {
		var path = "/";
		setCookie(name, value, EXPIRES_IN_YEAR, path);
	}
}


/* ------ Initialization - Rounded Corners----------------------------------------------*/

/**
 * Initialization:
 * - round corners of elements with class "roundedCorners" using openrico js
 * - alternate row colors
 */
function _initialize() {
	// round corners
	/*
	var elemArray = document.getElementsByClassName('roundedCorners');
	for (var i=0; i< elemArray.length; i++) {
		Rico.Corner.round(elemArray[i]);
	}
	*/
	
	// alternate row colors
	if(document.getElementsByTagName) {  
		var tables = document.getElementsByTagName("table");  
		for (var i=0; i < tables.length; i++) {
			var table  = tables[i];
			if (Element.hasClassName(table, 'dataGrid')) {
				var rows = table.getElementsByTagName("tr"); 
				for(var j=0; j < rows.length; j++){          
					if(j % 2 == 0) rows[j].className = "shade";
				}
			}
		}
	}
}

Event.observe(window, 'load', _initialize, false);
	