// JavaScript Document

// Select Hack
// Purpose: does something magical and mystical that fixes the bug in IE
//          where viewers are not able to select div'ed text correctly.
//
//          Code modified from: http://blog.tom.me.uk/2003/07/23/boie6selecta.php

if (window.createPopup && document.compatMode && document.compatMode=="CSS1Compat")
{
  document.onreadystatechange = onresize = function fixIE6AbsPos()
  {
    if (!document.body) return;
    if (document.body.style.margin != "0px") document.body.style.margin = 0;
    onresize = null;
    document.body.style.height = 0;
    setTimeout(function(){ document.body.style.height = document.documentElement.scrollHeight+'px'; }, 1);
  }
}

// Generic Delete Confirmation Dialog
function confirmDel(name) 
{
if(name=='') { name = 'this record'; }
var agree=confirm("Are you ABSOLUTELY SURE you wish to DELETE "+name+"?\n\nThis cannot be undone!");
	if (agree) {
		return true ;
	} else {
		return false ;
	}
}


/* highlight v1.0
/* Purpose: Changes a given object's class to the new one specified
/* Usage: 
/*    1) Make sure your object is uniquely identified with an 'id' tag
/*       ex:  <tr id="myRow">
/*    2) Make sure a highlight class exists
/*			 ex:  .hl { background-color:#FFFFCC; }
/*    3) In the focus object's code, add:
/*       onchange="highlight('myRow')"
/*
/*    When the object is changed, the row in this example is highlighted
*/

function highlight(id) 
{
  identity=document.getElementById(id);
  identity.className='hl';
}

function highlightUpdate(id, targetId) 
{
  identity=document.getElementById(id);
  target=document.getElementById(targetId);
  identity.className='hl';
  target.className='hl';
}


//v1.0
//Copyright 2006 Adobe Systems, Inc. All rights reserved.
function AC_AddExtension(src, ext)
{
  if (src.indexOf('?') != -1)
    return src.replace(/\?/, ext+'?'); 
  else
    return src + ext;
}

function AC_Generateobj(objAttrs, params, embedAttrs) 
{ 
  var str = '<object ';
  for (var i in objAttrs)
    str += i + '="' + objAttrs[i] + '" ';
  str += '>';
  for (var i in params)
    str += '<param name="' + i + '" value="' + params[i] + '" /> ';
  str += '<embed ';
  for (var i in embedAttrs)
    str += i + '="' + embedAttrs[i] + '" ';
  str += ' ></embed></object>';

  document.write(str);
}

function AC_FL_RunContent()
{
  var ret = 
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_SW_RunContent()
{
  var ret = 
    AC_GetArgs
    (  arguments, ".dcr", "src", "clsid:166B1BCA-3F9C-11CF-8075-444553540000"
     , null
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_GetArgs(args, ext, srcParamName, classid, mimeType)
{
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();    

    switch (currArg){	
      case "classid":
        break;
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":	
        args[i+1] = AC_AddExtension(args[i+1], ext);
        ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "onafterupdate":
      case "onbeforeupdate":
      case "onblur":
      case "oncellchange":
      case "onclick":
      case "ondblClick":
      case "ondrag":
      case "ondragend":
      case "ondragenter":
      case "ondragleave":
      case "ondragover":
      case "ondrop":
      case "onfinish":
      case "onfocus":
      case "onhelp":
      case "onmousedown":
      case "onmouseup":
      case "onmouseover":
      case "onmousemove":
      case "onmouseout":
      case "onkeypress":
      case "onkeydown":
      case "onkeyup":
      case "onload":
      case "onlosecapture":
      case "onpropertychange":
      case "onreadystatechange":
      case "onrowsdelete":
      case "onrowenter":
      case "onrowexit":
      case "onrowsinserted":
      case "onstart":
      case "onscroll":
      case "onbeforeeditfocus":
      case "onactivate":
      case "onbeforedeactivate":
      case "ondeactivate":
      case "type":
      case "codebase":
        ret.objAttrs[args[i]] = args[i+1];
        break;
      case "width":
      case "height":
      case "align":
      case "vspace": 
      case "hspace":
      case "class":
      case "title":
      case "accesskey":
      case "name":
      case "id":
      case "tabindex":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  return ret;
}

/* Dreamweaver canned functions */

// MM_displayStatusMsg: displays text in browser status bar
function MM_displayStatusMsg(msgStr) 
{ 
  status=msgStr;
  document.MM_returnValue = true;
}


/* Utility functions */

function toggleCBs(srcObj, objPrefix, objCountObj) 
{

	// PURPOSE: To toggle all checkbox elements on a page starting with the objPrefix.
	//          Use this function if you want a single checkbox to control all elements
	//          in a column.  Use toggleCBsOnOff() below if you want to control only
	//          one "direction" (e.g. on or off).
	// NOTE: this function assumes that objPrefx + i (e.g. myObj1, myObj2, myObj3, etc)
	// Params: srcObj = the object that is triggering this function (e.g. "this")
	//         objPrefix = the id of the checkbox elements up to the iterative integer
	//         objCount  = the upper limit of the iterative integer
	
	var onOrOff = srcObj.checked;
	
	var rowCount = document.getElementById(objCountObj).value;

	for(i=0; i<rowCount; i++) 
	{
	
		myObj = document.getElementById(objPrefix + i);
		myObj.checked = onOrOff;
	
	}
	
	return null;
	
}

function toggleCBsOnOff(objPrefix, objCountObj, onOrOff) 
{

	// PURPOSE: to toggle all checkbox elements on a page starting with the objPrefix
	//          Use this function if you want to control only
	//          one "direction" (e.g. on or off). Use toggleCBsOnOff() above if you 
	//          want a single checkbox to control all elements in a column.
	// NOTE: this function assumes that objPrefx + i (e.g. myObj1, myObj2, myObj3, etc)
	// Params: objPrefix = the id of the checkbox elements up to the iterative integer
	//         objCount  = the upper limit of the iterative integer
	//         on        = [bool] true if toggling on, false if toggling off
	
	var rowCount = document.getElementById(objCountObj).value;

	for(i=0; i<rowCount; i++) 
	{
	
		myObj = document.getElementById(objPrefix + i);
		myObj.checked = onOrOff;
	
	}
	
	return null;
	
}

function switchOffToggler(tarObj, objPrefix, objCountObj)
{
	
	var rowCount = document.getElementById(objCountObj).value;
	var tarCB = document.getElementById(tarObj);
	var objChecked = true;
	var thisObj;
	
	for(i=0; i<rowCount; i++)
	{
		thisObj = document.getElementById(objPrefix + i);
		if(thisObj.checked==false){
			objChecked = false;
			break;
		}
		
	}
	
	tarCB.checked = objChecked;
	
	return null;
	
}


function isEven(num) 
{
	// returns true if the int is even
	return !(parseInt(num) & 1);
}


// EDIT/NEW ADMIN PAGE FUNCTIONS
function setRedirPage(val)
{
	// PURPOSE - Works in conjunction with the "Save" button
	//           on various EDIT and NEW pages.  Sets the 
	//           redirPage hidden val.
	var myTar = document.getElementById("redirPage");
	myTar.value = val;
}


// script.aculo.us support functions

var myTimeoutId;

// will fade a specified messageId after n-number of seconds
function fadeMsgInit(msgId, type, delay) {
	var myDelay = delay * 1000;
	myTimeoutId = setTimeout("fadeMsg('"+msgId+"','"+type+"')", myDelay);
}

function fadeMsg(msgId, type) {
	var myObj = document.getElementById('statusMsg');
	
	switch(type)
	{
		case('errorMsg'):
			myObj.className = 'errorMsgNoIcon';
			break;
		case('warningMsg'):
			myObj.className = 'warningMsgNoIcon';
			break;
		default:
			myObj.className = 'statusMsgNoIcon';
			
	}
	Effect.Fade(msgId);
	Effect.Squish(msgId, {duration:2});
	clearTimeout(myTimeoutId);
}



function updateCharLimit(mySrc, myTarId, maxChars, thisLabel) 
{

	// PURPOSE - client-side limits a textarea or field to a certain number
	//           of characters.  Specify the id of an object on the page to
	//           update it's innerHTML with the character count.

	// Set up defaults...
	var label = 'this text';
	var showCount = true;
	var currCharCount = mySrc.value.length;

	// override defaults
	if(thisLabel!='')
	{
		label='the '+thisLabel;
	}
	
	// if myTarId is specified, get ready to update it's innerHTML
	if(myTarId!='') 
	{
		var myTar = document.getElementById(myTarId);
	} else {
		showCount = false;
	}
	
	// if the character count exceeds the max...
	if(currCharCount > maxChars) {
		alert('You are limited to '+maxChars+' for '+label+' field.\n\nYour text has been shortened to fit within this limit.');
		if(showCount) {
			myTar.innerHTML = '<span style="color: #9d0b0e">'+currCharCount+'</span>';
		}
		mySrc.value = mySrc.value.substring(0,maxChars);
		if(showCount)
		{
			myTar.value = maxChars;
		}
	} else if(showCount) {
		myTar.innerHTML = currCharCount;
	}
	
	return null;
	
}

function spamproof_em( name, textsize, domain, bgcolor, textcolor, noisecolor, root ) 
{

	// PURPOSE - this function works in conjunction with the Spam_Proof_Email PHP class.

	var noisecolorstr = '';
	if(noisecolor!='')
	{
		noisecolorstr = '&noisecolor='+noisecolor;
	} 
	var atsign = "@";
	var addr = name + atsign + domain;
	document.write("<" + "a" + " " + "href=" + "mail" + "to:" + addr + "><img src=\"" + root + "dynimages/emtext.php?n=" + addr + "&size=" + textsize + "&bgcolor=" + bgcolor + "&textcolor=" + textcolor + noisecolorstr + "\" alt=\"Click to Contact\" title=\"Click to Contact\" border=\"0\" align=\"baseline\" onmouseover=\"MM_displayStatusMsg('Click to contact by email');return document.MM_returnValue\" onmouseout=\"MM_displayStatusMsg('');return document.MM_returnValue\" /></a>");

}

/******************************************************************************/
/*                           GENERIC AJAX FUNCTIONS                           */
/******************************************************************************/

function createRequestObject() 
{
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}

var http = createRequestObject();

function sndReqGen(divId, procpage, location) 
{
	switch(location)
	{
		case('fe'):
			skindir = './skins/fe/images/';
			break;
		default:
			skindir = '../skins/admin/images/';
	}
	document.getElementById(divId).innerHTML = '&nbsp;<img src="'+skindir+'busy_spinner.gif" alt="Updating" width="16" height="16" border="0" align="absmiddle" />';
	http.open('get', procpage);
	http.onreadystatechange = handleResponseGen;
	http.send(null);
}

// generic updates
function handleResponseGen() 
{
	if(http.readyState == 4){
		var response = http.responseText;
		var update = new Array();

		if(response.indexOf('|' != -1)) {
			update = response.split('|');
			document.getElementById(update[0]).innerHTML = update[1];
		}
	}
}



