/*------------------------------------------------------------------------------------------------------------------------------------------------
Created By: Steve Holland
Description: This is a library of all my common javascript that I use on a regular basis.
USAGE: <script language="JavaScript" src="/javascript/SteveJSLibrary.js"></script>
------------------------------------------------------------------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------------------------------------------------------------------------
Open a browser window.
USAGE:
<a href="Javascript:openBrWindow('filename.cfm','windowName','toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=500,height=400');">Link Name or Image</a>
------------------------------------------------------------------------------------------------------------------------------------------------*/
function openBrWindow(theURL,winName,features){
  window.open(theURL,winName,features);
}


/*------------------------------------------------------------------------------------------------------------------------------------------------
Confirmation prompt. To call this JS, simply insert this code into the event handler you are using.
USAGE:
<input type="Submit" onClick="return sure('The message prompt you wish to display');">
------------------------------------------------------------------------------------------------------------------------------------------------*/
function sure(message)
{
returnval = confirm(message);
if (returnval == false) 
	{
	show_single_div('loading');
  return false
	} 
	else 
	{
	return true
	}
}


/*------------------------------------------------------------------------------------------------------------------------------------------------
Submit Once script. Will not work in CFFORM
USAGE:
<form name="" onSubmit="submitonce(this)">
------------------------------------------------------------------------------------------------------------------------------------------------*/
function submitonce(theform){
  //if IE 4+ or NS 6+
    if (document.all||document.getElementById){
    //screen thru every element in the form, and hunt down "submit" and "reset"
    for (i=0;i<theform.length;i++){
    var tempobj=theform.elements[i]
    if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset")
    //disable em
    tempobj.disabled=true
    }
  }
}


/*------------------------------------------------------------------------------------------------------------------------------------------------
Check all check boxes
USAGE:
<input type="checkbox" name="checkalldepts" value="Check all" onClick="this.value=check(document.form1.departments)">
<input type="checkbox" name="departments" value="Accounting">
<input type="checkbox" name="departments" value="Finance">
------------------------------------------------------------------------------------------------------------------------------------------------*/
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
  for (i = 0; i < field.length; i++) {
  field[i].checked = true;}
  checkflag = "true";
  return "uncheck all"; }
else {
  for (i = 0; i < field.length; i++) {
  field[i].checked = false; }
  checkflag = "false";
  return "check all"; }
}


/*------------------------------------------------------------------------------------------------------------------------------------------------
Auto Maximize Window Script
USAGE:
<input type="Button" name="max" value="Maximize Window" onclick="autoMaximize();">
------------------------------------------------------------------------------------------------------------------------------------------------*/
function autoMaximize(){
  top.window.moveTo(0,0);
  if (document.all) {
  top.window.resizeTo(screen.availWidth,screen.availHeight);
  }
  else if (document.layers||document.getElementById) {
  if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth){
  top.window.outerHeight = screen.availHeight;
  top.window.outerWidth = screen.availWidth;
  }
  }
}


/*------------------------------------------------------------------------------------------------------------------------------------------------
Font resizer
USAGE:
<a href="javascript:ts('body',1)">+ Larger Font</a> | <a
href="javascript:ts('body',-1)">+ Smaller Font</a>
------------------------------------------------------------------------------------------------------------------------------------------------*/
//Specify affected tags. Add or remove from list:
var tgs = new Array( 'div','td','tr');

//Specify spectrum of different font sizes:
var szs = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
var startSz = 2;

function ts( trgt,inc ) {
	if (!document.getElementById) return
	var d = document,cEl = null,sz = startSz,i,j,cTags;
	
	sz += inc;
	if ( sz < 0 ) sz = 0;
	if ( sz > 6 ) sz = 6;
	startSz = sz;
		
	if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];

	cEl.style.fontSize = szs[ sz ];

	for ( i = 0 ; i < tgs.length ; i++ ) {
		cTags = cEl.getElementsByTagName( tgs[ i ] );
		for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = szs[ sz ];
	}
}


/*------------------------------------------------------------------------------------------------------------------------------------------------
Disable "Enter" key in Form script
USAGE:
<input type="text" onkeypress="return handleEnter(this, event)"><br>
<input type="text" onkeypress="return handleEnter(this, event)">
------------------------------------------------------------------------------------------------------------------------------------------------*/                
function handleEnter(field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}      

/*------------------------------------------------------------------------------------------------------------------------------------------------
Display date and time
USAGE:
<script language="JavaScript">DateStamp();</script> <script language="JavaScript">TimeStamp();</script>
------------------------------------------------------------------------------------------------------------------------------------------------*/
function DateStamp() {
var d = new Array(
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
);

var m = new Array(
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
);

var today = new Date();
var day = today.getDate();
var year = today.getYear();
if (year < 2000) year += 1900; 
document.write(d[today.getDay()]+", "+m[today.getMonth()]+" "+day+", " + year);
//return d[today.getDay()]+", "+m[today.getMonth()]+" "+day+", " + year;
}

function TimeStamp() {
	var Stamp = new Date();
	var Hours;
	var Mins;
	var Time;
	Hours = Stamp.getHours();
	Mins = Stamp.getMinutes();
	if (Hours >= 12) Time = " P.M.";
		else Time = " A.M.";
	if (Hours > 12) Hours -= 12;
	if (Hours == 0) Hours = 12;
	if (Mins < 10) Mins = "0" + Mins;
	document.write(Hours + ":" + Mins + Time);
}

/*------------------------------------------------------------------------------------------------------------------------------------------------
Image rollover
USAGE:
<body onload="MM_preloadImages('/sysimages/davidpierce_on.jpg')">
<a href="http://www.link.com" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('david','','/sysimages/davidpierce_on.jpg',1)" target="_blank"><img src="/sysimages/davidpierce_off.jpg" alt="Master David Pierce" name="david" width="96" height="96" border="0"></a>
------------------------------------------------------------------------------------------------------------------------------------------------*/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/*------------------------------------------------------------------------------------------------------------------------------------------------
Show Hide Clicker
USAGE:
<a href="" onclick="show_single_div('my_div'); return false;">toggle div</a>
<div style="display: none;" id="my_div">this is the div</div>
------------------------------------------------------------------------------------------------------------------------------------------------*/
//Menu
function show_single_div(div_id) {
	var the_div = document.getElementById(div_id);
	if(the_div.style.display == '' || the_div.style.display == 'none') {
		the_div.style.display = 'block';
	} else {
		the_div.style.display = 'none';
	}
}

/*------------------------------------------------------------------------------------------------------------------------------------------------
Show Hide Clicker
USAGE:
<a href="" onMouseover="show_div('my_div_1'); return false;">Fox News</a> | <a href="" onMouseover="show_div('my_div_2'); return false;">Application</a> | <a href="" onMouseover="show_div('my_div_3'); return false;">Operating Systems</a>
<div id="my_div_1">News - Sports - Politics</div>
<div style="display: none;" id="my_div_2">File - Edit - Properties</div>
<div style="display: none;" id="my_div_3">Windows - Linux - Solaris</div>
------------------------------------------------------------------------------------------------------------------------------------------------*/
function show_div(div_id) {
	// hide all divs
	//document.getElementById('my_div_1').style.display = 'none';

	// show requested div
	document.getElementById(div_id).style.display = 'block';
}

/*------------------------------------------------------------------------------------------------------------------------------------------------
Close window and reload opener
USAGE:
<input type="Submit" value="Close" onclick="closeWinReloadOpener()">
------------------------------------------------------------------------------------------------------------------------------------------------*/
//Menu
function closeWinReloadOpener() {
	window.close();
  window.opener.location.reload();
}

/*------------------------------------------------------------------------------------------------------------------------------------------------
Close window and reload opener
USAGE:
<input type="Submit" value="Close" onclick="closeWinReloadOpener()">
------------------------------------------------------------------------------------------------------------------------------------------------*/
//Menu

function statusMsgOn(txt) {
  self.status = txt
}

function statusMsgOff() {
  self.status = ""
}

/*------------------------------------------------------------------------------------------------------------------------------------------------
Clean MS Word
USAGE:
<input type="Submit" value="Close" onclick="closeWinReloadOpener()">
------------------------------------------------------------------------------------------------------------------------------------------------*/

function cleanHTMLContent(tmp) {
	tmp = tmp.replace(/<\?xml:.*?>/ig, "");
	tmp = tmp.replace(/<H[0-9]+\s?([^>]*)>/ig, "<P $1>");
	tmp = tmp.replace(/<\/H[0-9]+>/ig, "</P>");
	tmp = tmp.replace(/<TT([^>]*)>/ig, "<P $1>");
	tmp = tmp.replace(/<\/TT>/ig, "</P>");
	tmp = tmp.replace(/\sclass=Mso.*?\s/ig, " ");
	tmp = tmp.replace(/<p\s*[^>]*>/ig, "<div>");
	tmp = tmp.replace(/<\/p>/ig, "</div>");
	tmp = tmp.replace(/(<[^>]+)lang=\"?[a-zA-Z]*\"?([^>]*>)/ig, "$1$2");
	tmp = tmp.replace(/("|\s)MARGIN:.*?(;|")/ig, "$1$2");
	tmp = tmp.replace(/("|\s)TEXT-INDENT:.*?(;|")/ig, "$1$2");
	tmp = tmp.replace(/("|\s)FONT-WEIGHT:.*?(;|")/ig, "$1$2");
	tmp = tmp.replace(/("|\s)tab-stops:.*?(;|")/ig, "$1$2");
	tmp = tmp.replace(/("|\s)mso-.*?:.*?(;|")/ig, "$1$2");
	tmp = tmp.replace(/<\/?o:p>/ig, "");
	tmp = tmp.replace(/(style="[^"]*)TEXT-ALIGN:\s?([a-z]*)([^"]*")/ig, "align=$2 $1$3");
	tmp = tmp.replace(/(style="[^"]*)BACKGROUND:\s?([a-z0-9#]*)([^"]*")/ig, "bgcolor=$2 $1$3");
	tmp = tmp.replace(/<SPAN style="FONT-FAMILY: Symbol">\s*<FONT\s+size=[0-9]*>\�<\/FONT>/ig, "<li>");
	tmp = tmp.replace(/(<font[^>]*)>\s*<font/ig, "$1");
	tmp = tmp.replace(/<dir>/ig, "<BLOCKQUOTE>");
	tmp = tmp.replace(/<\/dir>/ig, "</BLOCKQUOTE>");
	tmp = tmp.replace(/\sstyle="[^"]*"/ig, " ");
	tmp = tmp.replace(/\sclass=[A-Z0-9_]+/ig, " ");
	tmp = tmp.replace(/\sclass="[^"]*"/ig, " ");
	
	tmp = tmp.replace(/<SPAN\s*[^>]*><FONT\s*[^>]*>\&nbsp\;<\/FONT><\/SPAN>/ig, "&nbsp;");
	tmp = tmp.replace(/<SPAN\s*[^>]*>\&nbsp\;\s*<\/SPAN>/ig, " ");
	
	tmp = tmp.replace(/<\/div>\r\n<div>/igm, "</div><br><div>");//space between 2 divs
	tmp = tmp.replace(/<div><\/div>/ig, "<div>&nbsp;</div>");//force empty divs to show
	tmp = tmp.replace(/<div>\&nbsp\;<\/div><br>/ig, "<div>&nbsp;</div>");//remove br after empty div 
	
	tmp = tmp.replace(/<div>\&nbsp\;<\/div><br>/ig, "<div>&nbsp;</div>");//remove br after empty div 
	tmp = tmp.replace(/<BR>[\r|\n]*<DIV>\&nbsp\;<\/DIV>/ig,"<div>&nbsp;</div>");
	tmp = tmp.replace(/<\/DIV><BR>[\r|\n]*<DIV></ig,"</DIV>\r\n<DIV><");
	return tmp;//activeActiveX.document.body.innerHTML = tmp;
}

function cleanHTMLContent2(tmp) {
	tmp = tmp.replace(/<H[0-9]+\s?(.*)>/ig, "<P $1>");
	tmp = tmp.replace(/<\/H[0-9]+>/ig, "</P>");

	tmp = tmp.replace(/<TT([^>]*)>/ig, "<P $1>");
	tmp = tmp.replace(/<\/TT>/ig, "</P>");

	tmp = tmp.replace(/style="[^"]*"/ig, "");
	tmp = tmp.replace(/<font[^>]*>/ig, "");
	tmp = tmp.replace(/<\/font>/ig, "");
	tmp = tmp.replace(/\xFE/g, "ţ");
	tmp = tmp.replace(/\xDE/g, "Ţ");

	tmp = tmp.replace(/\xE2/g, "â");
	tmp = tmp.replace(/\xC2/g, "Â");
	
	tmp = tmp.replace(/\xEE/g, "î");
	tmp = tmp.replace(/\xCE/g, "Î");
	
	tmp = tmp.replace(/\xBA/g, "ş");
	tmp = tmp.replace(/\xAA/g, "�");
	
	tmp = tmp.replace(/\xE3/g, "ă"); 
	tmp = tmp.replace(/\xC3/g, "Ă");
	tmp = tmp.replace(/\x92/g, "kk");
	tmp = tmp.replace(/\x96/g, "-");
	tmp = tmp.replace(/–/g, "-");
	tmp = tmp.replace(/’/g, "-");

	tmp = tmp.replace(/class=[^ >]*/ig, "");
	tmp = tmp.replace(/class="[^"]*"/ig, "");

	//tmp = tmp.replace(/(<td[^>]*)(width|height)=[^ >]*([^>]*>)/ig, '$1$3');
	//tmp = tmp.replace(/(<table[^>]*)(width|height)=[^ >]*([^>]*>)/ig, '$1$3');

	tmp = tmp.replace(/<font[^>]*><\/font>/ig, '');
	//tmp = hndlr_load(tmp);
	return tmp;//activeActiveX.document.body.innerHTML = tmp;
}

function cleanHTML(content){
	var tempContent = cleanHTMLContent(content);
	var tempContent = cleanHTMLContent2(tempContent);
	//document.form1.header_info.value = tempContent;
	return tempContent;
}

/*------------------------------------------------------------------------------------------------------------------------------------------------
Clean out a text file for allowed characters
USAGE: <input type="text" name="NAME" value="" size="30" onKeyPress="return formatText(this,event)">
------------------------------------------------------------------------------------------------------------------------------------------------*/
function formatText(myfield, e, dec)
{
	var key;
	var keychar;

	if (window.event)
	   key = window.event.keyCode;
	else if (e)
		 key = e.which;
	else
		return true;
	keychar = String.fromCharCode(key);


	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
	   return true;

	// numbers
	else if ((("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_").indexOf(keychar) > -1))
		 return true;
	else
		return false;
}

/*------------------------------------------------------------------------------------------------------------------------------------------------
Auto tab
USAGE: <input name="ssn1" value="" size="3"  onKeyup="tabIt(this, document.frm1.ssn2)" maxlength="3">
------------------------------------------------------------------------------------------------------------------------------------------------*/

function tabIt(from,to){
if (from.getAttribute&&from.value.length==from.getAttribute("maxlength"))
to.focus()
}

/*------------------------------------------------------------------------------------------------------------------------------------------------
goLink shows the loading screen
USAGE: 
------------------------------------------------------------------------------------------------------------------------------------------------*/

function goLink(url){
	show_single_div('loading');
	//Go link
	location.href = url;
}

/*------------------------------------------------------------------------------------------------------------------------------------------------
clearInitValue clears the appropriate initial value from a form field
USAGE: 
------------------------------------------------------------------------------------------------------------------------------------------------*/

function clearInitValue(obj,initVal){
	var val = obj.value;
  
  if(val == initVal){
    obj.value = '';
  }
}

/*------------------------------------------------------------------------------------------------------------------------------------------------
Copies the content from a form object. WARNING! It only works in IE.
USAGE: doCopy();
------------------------------------------------------------------------------------------------------------------------------------------------*/
function doCopy(obj) {
	obj.select();
  textRange = obj.createTextRange();
	textRange.execCommand('RemoveFormat');
	textRange.execCommand('Copy');
}

// --End Hiding Here -->

/* This is the IE Flash fix for flash objects
Usage:
<script language="JavaScript">
  drawFlashObj('_assets/flash/map.swf',550,400,'FFFFFF','transparent');
</script>

Param1: file location of the flash swf file
Param2: swf width value
Param3: swf height value
Param4: background color
Param5: *wmode

*wmode has different values: transparent, opaque or leave it an empty string '' (if you send it an empty string, flash will use what ever color you defined for the background)
*/

function drawFlashObj(fileName,width,height,bgcolor,wmode){
  document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,47,0" id="home" width="' + width + '" height="' + height + '">\n');
  document.write('<param name="movie" value="' + fileName + '">\n');
  document.write('<param name="bgcolor" value="#' + bgcolor + '">\n');
  document.write('<param name="menu" value="false">\n');
  document.write('<param name="quality" value="high">\n');
  document.write('<param name="wmode" value="' + wmode + '">\n');
  document.write('<param name="allowscriptaccess" value="samedomain">\n');
  document.write('<embed type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" menu="false" wmode="' + wmode + '" width="' + width + '" height="' + height + '" name="home" src="' + fileName + '" bgcolor="#' + bgcolor + '" quality="high" swLiveConnect="true" allowScriptAccess="samedomain"></embed>\n');
  document.write('</object>\n');
}

/*------------------------------------------------------------------------------------------------------------------------------------------------
This code removed the default value of the form field.
USAGE:
<form name="myForm">
<textarea name="txtbox1" wrap="physical" cols="28" rows="1" onKeyDown="twdCount(document.myForm.txtbox1,document.myForm.twd1,25)" onKeyUp="twdCount(document.myForm.txtbox1,document.myForm.twd1,25)"></textarea><br>
<input readonly type="text" name="twd1" size="2" maxlength="25" value="25"> characters left<br><br>
<textarea name="txtbox2" wrap="physical" cols="28" rows="4" onKeyDown="twdCount(document.myForm.txtbox2,document.myForm.twd2,125)" onKeyUp="twdCount(document.myForm.txtbox2,document.myForm.twd2,125)"></textarea><br>
<input readonly type="text" name="twd2" size="3" maxlength="3" value="125"> characters left
</form>
------------------------------------------------------------------------------------------------------------------------------------------------*/
function twdCount(field,cntfield,maxlimit) {
  if (field.value.length > maxlimit)
  field.value = field.value.substring(0, maxlimit);
  else
  cntfield.value = maxlimit - field.value.length;
}

/*------------------------------------------------------------------------------------------------------------------------------------------------
//Creator: Steve Holland
//Email: steve@fusecast.com
//Website: www.fusecast.com
//Version: 1.0
//Usage: <script language="JavaScript">eMasker('steve','email.com','email me');</script>
------------------------------------------------------------------------------------------------------------------------------------------------*/
function eMasker(user,domain,text){     
  var eMaskResult = "Error. Please enter the appropriate parameters.";  
  if(user != "" && domain != ""){
    emailAddr = user + "@" + domain;
    eMaskResult = "<a href=\"mailto:" + emailAddr + "\">";
    if(typeof(text) != "undefined"){
      eMaskResult = eMaskResult + text;
    }else{
      eMaskResult = eMaskResult + emailAddr;
    }    
    eMaskResult = eMaskResult + "</a>";
  }  
  document.write(eMaskResult);
}

/*------------------------------------------------------------------------------------------------------------------------------------------------
This code fades in or out any Element
USAGE: opacity('popup', 0, 100, 2500);
------------------------------------------------------------------------------------------------------------------------------------------------*/
function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
			for(i = opacStart; i >= opacEnd; i--) {
					setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
					timer++;
			}
	} else if(opacStart < opacEnd) {
			for(i = opacStart; i <= opacEnd; i++)
					{
					setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
					timer++;
			}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
} 