/* ============================================================================
* style.lib.js, v1.0.005, 2009-09-15
*
* Authors:	Scott Park (scott@firefallpro.com)
*			Dan Brenner (brenner.dan@firefallpro.com)
*			Joseph Purayidathil (jomy@firefallpro.com)
*			Others where noted
*
* Desc.:	Provides CSS/inline style related functions
*
* Contents:	trim()
*			ltrim()
*			rtrim()
*			show()
*			hide()
*			showByClass()
*			hideByClass()
*			changeClassByElement()
*			changeClassByClass()
*			highlightRow()
*			highlightandCheckAllRows()
*
* Requires: jQuery (optionally)
*
* http://www.firefallpro.com
*
* Copyright 2008 Firefall Pro, LLC, unless otherwise noted. The contents of
* this file may not be reused, resold or redistributed, in part or in whole,
* without the expressed permission of the respective copyright holders.
* ========================================================================== */

// trim(), ltrim(), and rtrim(): Credit: http://www.somacon.com/p355.php
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

// Shows an element, element ID or series of comma separated element IDs
function show(id) {
	var element;
	
	// Makes the element visible
	function showElement(e) {
		
		// Block Level Tags
		if (e.tagName.toLowerCase() in {"p":"","div":"","hr":"","h1":"","h2":"","h3":"","h4":"","h5":"","h6":"","address":"","blockquote":"","center":"","del":"","ins":"","noscript":"","pre":""}) {
			e.style.display = "block";
		
		// Table
		} else if (e.tagName.toLowerCase() == "table") {
			try {
				e.style.display = "table-row";
			} catch(error) {
				e.style.display = "block";
			}
		
		// Table Row
		} else if (e.tagName.toLowerCase() == "tr") {
			try {
				e.style.display = "table-row";
			} catch(error) {
				e.style.display = "block";
			}
		
		// Table Cell
		} else if (e.tagName.toLowerCase() == "td") {
			try {
				e.style.display = "table-cell";
			} catch(error) {
				e.style.display = "block";
			}
		
		// Inline Tags
		} else {
			e.style.display = "inline";
		}
	}
	
	// Validate Input
	if (!id) {
		alert("The element has not been specified in show() or does not exist");
		return false;
	}
	
	// String
	if ((typeof id) == "string") {
		id = id.split(",");
		for(i=0; i < id.length; i++) {
			
			// Check for the element
			if (!(element = document.getElementById(id[i].trim()))) {
				alert("The element ID \""+id[i].trim()+"\"specified in show() does not exist");
			} else {
				showElement(element);
			}
		}

	// Object
	} else {
		showElement(id);
	}
	return true;
}

// Hides an element, element ID or series of comma separated element IDs
function hide(id) {
	var element;
	
	// Validate Input
	if (!id) {
		alert("The element has not been specified in hide() or does not exist");
		return false;
	}
	
	// String
	if ((typeof id) == "string") {
		id = id.split(",");
		for(i=0; i < id.length; i++) {
			
			// Check for the element
			if (!(element = document.getElementById(id[i].trim()))) {
				alert("The element ID \""+id[i].trim()+"\" specified in hide() does not exist");

			// Hide
			} else {
				element.style.display = "none";
			}
		}
		
	// Object
	} else {
		id.style.display = "none";
	}
	return true;
}

// Shows a tag by a class
function showByClass(tag,c,within) {
	var elements;
	var d;
	
	// Validate Input
	if (!tag) {
		alert("A tag has not been specified in showByClass()");
		return false;
	}
	if (!c) {
		alert("A class has not been specified in showByClass()");
		return false;
	}
	if (!within) {
		d = document;
	} else if (!(d = document.getElementById(within.trim()))) {
		alert("The element ID \""+within+"\"specified in showByClass() does not exist");
		return false;
	}
	
	// Find elements
	elements = d.getElementsByTagName(tag.toLowerCase());

	// Show matching elements
	for(i=0; i < elements.length; i++) {
		if (elements[i].className == c) {
			show(elements[i]);
		}
	}
	return true;
}


// Hides a tag by a class
function hideByClass(tag,c,within) {
	var elements;
	var d;
	
	// Validate Input
	if (!tag) {
		alert("A tag has not been specified in hideByClass()");
		return false;
	}
	if (!c) {
		alert("A class has not been specified in hideByClass()");
		return false;
	}
	if (!within) {
		d = document;
	} else if (!(d = document.getElementById(within.trim()))) {
		alert("The element ID \""+within+"\"specified in hideByClass() does not exist");
		return false;
	}
	
	// Find elements
	elements = d.getElementsByTagName(tag.toLowerCase());

	// Hide matching elements
	for(i=0; i < elements.length; i++) {
		if (elements[i].className == c) {
			elements[i].style.display = "none";
		}
	}
	return true;
}

// Changes the class of an element, element ID or series of comma separated element IDs
function changeClassByElement(id,c) {
	var element;
	
	// Validate Input
	if (!id) {
		alert("The element has not been specified in changeClassByElement() or does not exist");
		return false;
	}
	if (!c) c = "";
	
	// String
	if ((typeof id) == "string") {
		id = id.split(",");
		for(i=0; i < id.length; i++) {
			
			// Check for the element
			if (!(element = document.getElementById(id[i].trim()))) {
				alert("The element ID \""+id[i].trim()+"\"specified in changeClassByElement() does not exist");
			} else {
				element.className = c;
			}
		}

	// Object
	} else {
		element = id;
		element.className = c;
	}
	return true;
}

// Changes the class of a tag by a class
function changeClassByClass(tag,find,replace,within) {
	var elements;
	var d;
	
	// Validate Input
	if (!tag) {
		alert("A tag has not been specified in changeClassByClass()");
		return false;
	}
	if (!find) {
		alert("A class to find has not been specified in changeClassByClass()");
		return false;
	}
	if (!replace) replace = "";
	
	if (!within) {
		d = document;
	} else if (!(d = document.getElementById(within.trim()))) {
		alert("The element ID \""+within+"\"specified in changeClassByClass() does not exist");
		return false;
	}
	
	// Find Elements
	elements = d.getElementsByTagName(tag.toLowerCase());

	// Hide matching elements
	for(i=0; i < elements.length; i++) {
		if (elements[i].className == find) {
			elements[i].className = replace;
		}
	}
	return true;
}
var a = 0;
var b = 0;
// Highlights a table row that is the parent of a checkbox with class "selector"

// Table Row Highlighting
$(document).ready(function() {

	$(".selector").change(function () {
			b = b + 1;
		if (this.checked == true) {
		var tableCell = $(this).parent().get(0);
		var tableRow = $(tableCell).parent().get(0);
			$(tableRow).addClass("selected");
		} else {
		var tableCell = $(this).parent().get(0);
		var tableRow = $(tableCell).parent().get(0);
			$(tableRow).removeClass("selected");
		}
	//console.log("b=",b);
	});
		
});
// Un/checks and highlights all checkboxes within a table
function highlightandCheckAllRows(toggleCheckbox){
		a = a + 1;
		if (toggleCheckbox==true) {
		$(".selector").each(function () {
			$(this).attr('checked', false);
			$(this).trigger("change");		
			});} 
		else {
		$(".selector").each(function () {
			$(this).attr('checked', true);
			$(this).trigger("change");		
		});}
		
		
		//console.log(toggleCheckbox);
		//console.log("a=",a);
}