﻿//	JavaScript Functions for Channels
//  
//  Create by Glenn @ 2009/11/17
//  
//	
//  Available Functions:
//
//	loadContent( url, ShowId, HideId, loadImg )
//		Loads text/xml file to a page element. Takes optional variables to to
//		remove an element from view upon call of function, and to use a
//		different "now loading" image from the default.
//
//		Variables:
//		url		- Required. The URL to the text/html/xml to be loaded.
//		ShowId	- Required. The ID of the element where the content will be
//				  loaded. It is recommended that the style for this element is
//				  specified to include "Display: None;" by default.
//		HideId	- Optional. The ID of the element to hide from view. Required
//				  if 'loadImg' variable is specified.
//		loadImg	- Optional. URL to the image file that represents a "now
//				  loading" animation. Default value is "/images/loading.gif"
//
//		Example:
//		<div id="LinkID"><a href="#" onClick="loadContent('message.txt','NewMessage','LinkID');return false;">Show Messages</a></div>
//		<div id="NewMessage" style="Display: None;"></div>
//
//
//	toggleContent( url, contentId, loadImg )
//		Toggles the loaded text/xml file in specified element ID.
//
//		Variables:
//		url			- Required. The URL to the text/html/xml to be loaded.
//		contentId	- Required. The ID of the element to be expand/hide when
//					  the function is called.
//		loadImg		- Optional. URL to the image file that represents a "now
//					  loading" animation. Default value is "/image/loading.gif"
//
//		Example:
//		<a href="#" onClick="toggleContent('message.txt','NewMessage');return false;">Messages</a>
//		<div id="NewMessage" style="Display: None;"></div>
//
//
//	limitText( limitField, limitNum, limitCount )
//		Check the length of a form field as the user types, limit the length to
//		a certain number, and optionally show how many characters are still
//		available.
//
//		Variables:
//		limitField	- Required. The text or textarea field where the limit is
//					  to be applied. Can use "this" within the field.
//		limitNum	- Required. The integer value of the number of characters
//					  the user should be limited to
//		limitCount	- Optional. The field to show the user how many more
//					  characters can still be entered.
//
//		Example:
//		<textarea name="field" onKeyDown="limitText(this,200,this.form.countdown);" onKeyUp="limitText(this,200,this.form.countdown);"></textarea>
//		Characters remaining: <input type="text" name="countdown" value="200" size="3" readonly>
//

var xmlhttp;

function loadContent(url,ShowId,HideId,loadImg) {
	if (loadImg==null) {
		// Set default "loading" image
		var loadImg='/images/loading.gif';
	}
	if (window.XMLHttpRequest) {
		// Code for Firefox, IE7+, Opera, Chrome, etc.
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		// Code for IE5 and IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		alert("Your browser does not support XMLHTTP!");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			// readyState 4 : Request complete
			if (xmlhttp.status==200) {
				document.getElementById(ShowId).innerHTML = xmlhttp.responseText;
			}
			else {
				alert("Problem retrieving data:" + xmlhttp.statusText);
			}
		}
		else if (xmlhttp.readyState!=0) {
			// readyState 0 : Request is uninitialized
			// readyState 1 : Request has been set up
			// readyState 2 : Request has been sent
			// readyState 3 : Request is in process
			// Show a "loading" image while waiting for request to complete
			if (HideId!=null) {
				// Make "HideId" an optional variable
				document.getElementById(HideId).style.display = 'none';
			}
			document.getElementById(ShowId).style.display = 'block';
			document.getElementById(ShowId).innerHTML = '<div style="Text-Align: Center; Margin: 3px;"><img src="' + loadImg + '" alt="Loading..." border="0" /></div>';
		}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function toggleContent(url,contentId,loadImg) {
	if (loadImg==null) {
		var loadImg='/images/loading.gif';
	}
	var contentdiv = document.getElementById(contentId);
	if (contentdiv.style.display == 'none') {
		if (window.XMLHttpRequest) {
			xmlhttp=new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		else {
			alert("Your browser does not support XMLHTTP!");
		}
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				if (xmlhttp.status==200) {
					document.getElementById(contentId).innerHTML = xmlhttp.responseText;
				}
				else {
					alert("Problem retrieving data:" + xmlhttp.statusText);
				}
			}
			else if (xmlhttp.readyState!=0) {
				document.getElementById(contentId).style.display = 'block';
				document.getElementById(contentId).innerHTML = '<div style="Text-Align: Center; Margin: 3px;"><img src="' + loadImg + '" alt="Loading..." border="0" /></div>';
			}
		}
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else {
		contentdiv.style.display = 'none';
	}
}

function limitText(limitField,limitNum,limitCount) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	}
	else {
		if (limitCount!=null) {
			limitCount.value = limitNum - limitField.value.length;
		}
	}
}
