/*-------------------------------------------------
Title:		Wise Guide JavaScript Utilities
Updated:	June 4 2009
------------------------------------------------- */





// add load event function
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}



/*
    returns array with all elements with specified class name
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/ 
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};





// prepares links based on XML "rel" attribute
function prepareLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		
		// prepare external links
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}

		// prepare close window links
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "close") {
			anchor.onclick = function() {
				window.close();
			}
		}
	}
}




// prepares form inputs
function prepareInputs() {
	if (!document.getElementById) return;
	
	var inputs = getElementsByClassName('erase');
	
	for (var i=0; i<inputs.length; i++) {
		var input = inputs[i];
		var value = input.value;
		
		input.onfocus = function() {
			if(value == input.value) {
				input.value = '';
				input.style.color = '#000';
			}
		}

		input.onblur = function() {
			if(input.value == '') {
				input.value = value;
				input.style.color = '#999';
			}
		}
	}
	
	var miniE = document.getElementById('cm-jlgw-jlgw');
	
	miniE.onfocus = function() {
		if(this.value=='Type your email address') {
			this.value='';
		}
		this.style.color='#000';
	}
	
	miniE.onblur = function() {
		if(this.value=='') {
			this.value='Type your email address';
			this.style.color='#999'
		}
	}
}





// prepares tab navigation
function prepareTabs() {
	if (!document.getElementById) return;
	
	var navs = getElementsByClassName('tabs');
	var sections = getElementsByClassName('tabSection');
	
	if(navs.length > 0) {
	
		// get the tabs
		for (var i=0; i<navs.length; i++) {
			var tabs = navs[i].getElementsByTagName('LI');
		}

		// get the links
		for (var i=0; i<navs.length; i++) {
			var links = navs[i].getElementsByTagName('A');
		}
		
		for (var i=0; i<links.length; i++) {
			
			links[i].onclick = function() {
				// mute tabs
				for (var i=0; i<tabs.length; i++) {
					tabs[i].className = '';
				}
				
				// active the tab!
				this.parentNode.className = 'active';
				
				// parse the URL
				var ids = this.href.split('#');
				
				// hide other tabSection elements
				for (var i=0; i<sections.length; i++) {
					sections[i].style.display = 'none';
				}
				
				// show the active section element
				document.getElementById(ids[1]).style.display = 'block';
				
				return false;
			}
		}
	}
}



// verifies form by id
function verify(formid) {
	if(document.getElementById(formid)) {
		var theForm = document.getElementById(formid);
		var error = 0;
		
		for (x = 0; x < theForm.elements.length; x++) {
			if (theForm.elements[x].className == "required") {
				if (theForm.elements[x].type == "text" || theForm.elements[x].type == "password") {
					if (theForm.elements[x].value == "") {
						theForm.elements[x].parentNode.className = 'error';
						error = 1;
					} else {
						theForm.elements[x].parentNode.className = '';
					}
				}
			}
		}
		
		if (error == 1) {
			alert("You must fill out all required fields before you can submit the form.");
			return false;
		}
	}
}




// simple function to return an element's id
function get(id) {
	return document.getElementById(id);
}




// simple show
function simpleShow(id) {
	if (!document.getElementById) return;
		
	var element = document.getElementById(id);
	
	if(navigator.appName.indexOf("Microsoft") > -1) {
		element.style.display = 'block';
	}

	else {
		if(element.nodeName == 'TABLE') {
			element.style.display = 'table';
		}
		
		else if(element.nodeName == 'TR') {
			element.style.display = 'table-row';
		}

		else if(element.nodeName == 'TBODY') {
			element.style.display = 'table-row-group';
		}
		
		else {
			element.style.display = 'block';
		}
	}
}




// simple hide
function simpleHide(id) {
	if (!document.getElementById) return;
		
	var element = document.getElementById(id);
	
	if(element) {
		element.style.display = 'none';
	}
	else {
		id.style.display = 'none';
	}
}




// simple toggle
function toggle(id) {
	if (!document.getElementById) return;
	
	var element = document.getElementById(id);
	
	if(element.style.display == 'none') {
		if(navigator.appName.indexOf("Microsoft") > -1) {
			element.style.display = 'block';
		}

		else {
			if(element.nodeName == 'TBODY') {
				element.style.display = 'table-row-group';
			}
			
			else {
				element.style.display = 'block';
			}
		}
	} else {
		element.style.display = 'none';
	}
}




// simple hide
function mask(id) {
	if (!document.getElementById) return;
		
	var element = document.getElementById(id);
	
	element.style.visibility = 'hidden';
}




// simple hide
function popup(url,w,h,type) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	var popupProps = 'width='+w+',height='+h+',left='+winl+',top='+wint;
	
	switch(type) {
		case 'full':
			popupProps += 'resizable=yes,status=1,menubar=1,toolbar=1,scrollbars=1,location=1,directories=1';
			break; 
		
		case 'basic':
			popupProps += 'resizable=yes,toolbar=1,scrollbars=1,location=1';
			break; 
		
		case 'scroll':
			popupProps += 'scrollbars=1';
			break;
		
		default:
			popupProps += '';
	}
	
	window.open(url,'popup',popupProps);
}




// owl rating system
function owlRater() {
	if (!document.getElementById) return;
	
	var owls = document.getElementsByTagName('LI');
	
	for (var i=0; i<owls.length; i++) {
		var owl = owls[i];
		var rated = false;
		
		if(owl.parentNode.parentNode.className=='owlrater') {
			
			// get the current rating
			var rating = owl.parentNode.className;
			
			owl.onmouseover = function() {
				this.parentNode.className=this.className;
				//set hidden field
				var hiddenID = 'value_' + this.parentNode.parentNode.id;
				hiddenField = document.getElementById(hiddenID);
				if (hiddenField) {
				  hiddenField.value=this.className
				}
			}
			
			owl.onclick = function() {
				this.parentNode.className=this.className;
				
				// get the rater's ID
				var raterID = this.parentNode.parentNode.id;
				var len = raterID.length; // get number of characters from the id
				//var num = raterID.substring(len - 2, len) // get just the number
				var num = raterID.substring((raterID.lastIndexOf('_')+1), len) // get just the number
				var ratingID = 'rating_' + num;
				
				// the showrating ID
				var theRating = document.getElementById(ratingID);
				
				if(theRating) {
					switch(this.className) {
						case "rated_1":
							theRating.innerHTML = 'Rated <span class="rating rated_1">1</span> owl out of 5.';
							break;
						case "rated_2":
							theRating.innerHTML = 'Rated <span class="rating rated_2">2</span> owls out of 5.';
							break;
						case "rated_3":
							theRating.innerHTML = 'Rated <span class="rating rated_3">3</span> owls out of 5.';
							break;
						case "rated_4":
							theRating.innerHTML = 'Rated <span class="rating rated_4">4</span> owls out of 5.';
							break;
						case "rated_5":
							theRating.innerHTML = 'Rated <span class="rating rated_5">5</span> owls out of 5.';
							break;
						default:
							theRating.innerHTML = 'Rated <span class="rating rated_0">0</span> owls out of 5.';
					}
				}
				var hiddenID = 'click_value_' + this.parentNode.parentNode.id;
        hiddenField = document.getElementById(hiddenID);
        if (hiddenField){
          hiddenField.value=this.className
        } 
        
				rated = true;
			}
			
			owl.onmouseout = function() {
				
				if(!rated) {
					this.parentNode.className=rating;
					var hiddenID = 'value_' + this.parentNode.parentNode.id;
          hiddenField = document.getElementById(hiddenID);
          if (hiddenField) {
            hiddenField.value=rating
          }
				}
				
			}
		}
	}
}




// load events
addLoadEvent(prepareLinks);
addLoadEvent(prepareInputs);
//addLoadEvent(prepareTabs);
addLoadEvent(owlRater);

