//****** Javascript for opening PDF's in seperate window
function openPDF(url,windowName) {
	open(url,windowName,"resizable=yes,width=750,height=500,scrollbars=1");
}

//****** Javascript that passes stats back to the server using AJAX
var size_timer = false;
function getBrowserSize() {
	/*if(size_timer) return;
	size_timer = true;
	self.setTimeout('resetSizeTimer()',1000);
	var theArray = getSize();
	var url = "/a_BrowserSize.cfm";
	var args = new Array();
	args.push("width");
	args.push(theArray[0]);
	args.push("height");
	args.push(theArray[1]);
	args.push("screenwidth");
	args.push(screen.width);
	args.push("screenheight");
	args.push(screen.height);
	args.push("pagename");
	args.push(window.location);
	ajax(url, args, 'GET', '');*/
}
// Subroutine to reset timer
function resetSizeTimer() {
	size_timer = false;
}
// Subroutine to get the size of the window
function getSize() {
	var myWidth = 0, myHeight = 0;
	if(typeof(window.innerWidth) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [myWidth, myHeight];
}

//****** AJAX - Pass fields to server given a URL and fields in name/value pairs
function ajax(url,args,method,element) {
	// take args array and make into a string
	var argsstring = "";
	for(var i=0; i<args.length; i=i+2) {
		argsstring += args[i] + "=" + escape(args[i+1]) + "&";
	}
	
	// set up ajax communication
	var ajax=null;
	if(window.XMLHttpRequest) {
		ajax=new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		ajax=new ActiveXObject('Microsoft.XMLHTTP');
	} else {
		return;
	}
	
	//send ajax communication
	if(method=='POST') {
		ajax.open('POST',url,true);
		ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4 && element!='') {
				document.getElementById(element).innerHTML=ajax.responseText;
			}
		}
		ajax.send(argsstring);
	} else {
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4 && element!='') {
				document.getElementById(element).innerHTML=ajax.responseXML.getElementsByTagName('data')[0].firstChild.data;
			}
		}
		ajax.open('GET',url+'?'+argsstring,true);
		ajax.send(null);
	}
}

//****** Javascript for opening/closing More box
var MorePageShown = 0;
function openMore(c) {
      var url = "/i_more.cfm"
      var args = new Array();
      ajax(url, args, "POST", "advanced_search_frame");
			MorePageShown = 1;
		if (document.getElementById("advanced_search_frame").style.display == "block") {
		document.getElementById("advanced_search_frame").style.display = "none";
		}
		else {
		document.getElementById("advanced_search_frame").style.left = 40 + "%";
		document.getElementById("advanced_search_frame").style.top  = 100 + "px";
		document.getElementById("advanced_search_frame").style.display = "block";}
}
// subroutine to get the left side of the obj
function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
// subroutine to get the top side of the obj
function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

//****** Javascript for expanding/shrinking boxes
function showMore(c) {
	if (document.getElementById("s"+c).style.display != "block") {
		document.getElementById("s"+c).style.display = "block";
		document.getElementById("l"+c).innerHTML = "&raquo;Less";
	}
	else {
		document.getElementById("s"+c).style.display = "none";
		document.getElementById("l"+c).innerHTML = "&raquo;More";
	}
}

//****** Javascript for validating e-mail addresses
// corrects common mistakes in an email
function MassageEmail(email)
{
	var badChar = email.indexOf(",",0);
	while (badChar > -1) {
		email = email.substring(0,badChar) + "." + email.substring(badChar+1,email.length);
		badChar = email.indexOf(",",0);
	}
	badChar = email.indexOf(" ",0);
	while (badChar > -1) {
		email = email.substring(0,badChar) + "" + email.substring(badChar+1,email.length);
		badChar = email.indexOf(" ",0);
	}
	return email;
}
// subroutine that checks for certain chars common to all email addresses
function ValidEmail(email) {
	if (email.length > 5) {
		if (email.indexOf("@",0) > -1) {
			if (email.indexOf(";",email.indexOf("@",0)+1) == -1) {
				if (email.indexOf("@",email.indexOf("@",0)+1) == -1) {
					if (email.indexOf(".",email.indexOf("@",0)) > -1) {
						return true;
					}
				}
			} else {
				return true;
			}
		}
	}
	return false;
}

//****** Javascript for automatically tabbing to the next box
/* Original:  Cyanide_7 (leo7278@hotmail.com)
   Web Site:  http://members.xoom.com/cyanide_7

   This script and many more are available free online at
   The JavaScript Source!! http://javascript.internet.com */
function autoTab(input,len, e) {
	var keyCode = e.keyCode;
	var filter = [0,8,9,16,17,18,35,36,37,38,39,40,46];
	if (input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}

	function containsElement(arr, ele) {
		var found = false, index = 0;
		while (!found && index < arr.length)
			if (arr[index] == ele)
				found = true;
			else
		index++;
		return found;
	}

	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)index = i;
			else i++;
		return index;
	}
	return true;
}

//****** Javascript for checking if a value IsNumeric
function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber = true;
	var Char;
	
	if (sText.length != 0) {
		for (i = 0; i < sText.length && IsNumber == true; i++) { //>
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) {
				IsNumber = false;
			}
		}
	} else {
		IsNumber = false;
	}
	
	return IsNumber;
}

//****** Javascript for checking the number of words in a Textarea
function WordCount(this_field, e, maxchar, maxwords) {
	if (e.keyCode != 13) {
		var count = CountWords(this_field, false);
		if (count == maxwords+1)
			alert("You have reached the max number of words allowed.");
		else if (this_field.value.length == maxchar)
			alert("You have reached the max number of characters allowed.");
		else if (count > maxwords+1)
			alert("You have more than the max number of words allowed.");
	}
}
// subroutine for counting the number of words in a Textarea
function CountWords(this_field, show_word_count, show_char_count) {
	if (show_word_count == null)
		show_word_count = true;
	if (show_char_count == null)
		show_char_count = false;
	var char_count = this_field.value.length;
	var fullStr = this_field.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length -1;
	if (fullStr.length <2)
		word_count = 0;
	if (word_count == 1)
		wordOrWords = " word";
	else
		wordOrWords = " words";
	if (char_count == 1)
		charOrChars = " character";
	else
		charOrChars = " characters";
	if (show_word_count & show_char_count)
		alert("Word Count:\n" + "    " + word_count + wordOrWords + "\n" + "    " + char_count + charOrChars);
	else {
		if (show_word_count)
			alert("Word Count:  " + word_count + wordOrWords);
		else if (show_char_count)
			alert("Character Count:  " + char_count + charOrChars);
	}
	return word_count;
}
// Restrict a textarea to a maxlength
function MaxLength(textField,len) {
	len += 1;
	if (textField.value.length > len)
		textField.value = textField.value.substring(0,len);
}

function openMedia(id) {
  open("http://www.thonline.com/multimedia/fullsize.cfm?id="+id,"Multimedia","resizable=yes,width=505,height=415,scrollbars=no");
}