/**************************************
	Cronus .js Components
	JavaScript Document
	Copyright 2006
	Mindset International
***************************************/

//Swaps an image. duh...
function swapImage(obj,newImage) {
	obj.src = newImage;
}

//Checks all checkboxes in a form
function CheckAll() {
count = document.deleteMail.elements.length;
    for (i=0; i < count; i++) {
    if(document.deleteMail.elements[i].checked == 0) {
    	document.deleteMail.elements[i].checked = 1; 
		}
    else { 
		document.deleteMail.elements[i].checked = 0;
		}
	}
}

//takes the id of a drop down list and returns the value of the selected item
function getSelectedValueById(elementID){	
	var elem = document.getElementById(elementID);
  for (var i=0; i < elem.options.length; i++){
		if (elem.options[i].selected){
	    return(elem.options[i].value);
	    break;
    }
  }
}

//This function displays/undisplays the menus. 
function displayToggle(id) {
	var el = document.getElementById(id).style; 
	if(el.display == "none") {
		el.display = "block";  
  	}
  	else if(el.display == "block") {
  		el.display = "none";
	}
}

function showBox(boxName,theWidth) {
	var theBox = document.getElementById(boxName);
	theBox.style.visibility = "visible";
	//theBox.style.height = theHeight+'px';
	theBox.style.width = theWidth+'px';
}

function hideBox(boxName) {
	document.getElementById(boxName).style.visibility = "hidden";
}

function showBoxWithPosition(whichBox,boxWidth,positionRef,positionFrom){
	showBox(whichBox,boxWidth);
	var newX = findPosX(document.getElementById(positionRef));
	var newY = findPosY(document.getElementById(positionRef));
	objToStyle = document.getElementById(whichBox);
	if(positionFrom == 'right'){
		objWidth = parseFloat(objToStyle.style.width);
		objToStyle.style.left = parseFloat(newX - objWidth)+'px';
	}
	else if(positionFrom == 'left'){
		objToStyle.style.left = newX+'px';
	}
	objToStyle.style.top = newY+'px';
}

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

/* simple HTTPRequest functionality written by Luther Monson (luther.monson@mindsetinternational.com)*/
function createRequestObject() {
  var xmlhttp=false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // JScript gives us Conditional compilation, we can cope with old IE versions.
  // and security blocked creation of the objects.
   try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
    try {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
     xmlhttp = false;
    }
   }
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  	try {
  		xmlhttp = new XMLHttpRequest();
  	} catch (e) {
  		xmlhttp=false;
  	}
  }
  if (!xmlhttp && window.createRequest) {
  	try {
  		xmlhttp = window.createRequest();
  	} catch (e) {
  		xmlhttp=false;
  	}
  }

	return xmlhttp;
}

var http = createRequestObject();

function sendGet(uri, action){
  var request = uri + '?' + action;
	http.open('get', request);
	http.onreadystatechange = handleResponse;
	http.send(null);
}
function sendPost(uri, args){
	http.open('post', uri, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(args);
	http.onreadystatechange = handleResponse;
}

function handleResponse() {
  switch(http.readyState) {
    case 0: // nothing to do
      break;
    case 1: // no request made yet
      break;
    case 2: // server talk established
      break;
    case 3: // downloading
      break;
    case 4: // success
      var response = http.responseText;
		  var update = new Array();
		  //alert(response);
		  break;
		default:
		  break;
  }
}

/* On demand editing written by JJ May (jj.may@mindsetinternational.com)*/

function onDemandEditForm(whichSpan,field,whichSubmit){
	var newFieldValue = document.getElementById(whichSpan).innerHTML;
	document.getElementById(whichSpan+'Container').innerHTML = '<input type="text" id="'+field+'" name="'+field+'" value="'+newFieldValue+'" onBlur="document.getElementById(\''+whichSubmit+'\').click()" />';
	document.getElementById(field).focus();
}

function onDemandEditTextArea(whichSpan,field,whichSubmit){
	var newFieldValue = document.getElementById(whichSpan).innerHTML;
	document.getElementById(whichSpan+'Container').innerHTML = '<textarea id="'+field+'" name="'+field+'" onBlur="document.getElementById(\''+whichSubmit+'\').click()" />'+newFieldValue+'</textarea>';
	document.getElementById(field).focus();
}

function toggleDisplayByClass(elType, sclass) {
	var newElements = getElementsByClassName(document,elType, sclass);
	if(!(newElements[0].style.display=='none')){
		newDisplay = 'none';
	}
	else {
		newDisplay = 'inline';
	}
	for(i=0;i<newElements.length;i++){
		newElements[i].style.display = newDisplay;
	}
}

function moveToCleanup() {
	var imageIcons = document.getElementsByClassName(document,'img','moveTo');
	for(i=0;i<imageIcons.length;i++){
		imageIcons[i].style.display = 'none';
	}
}

function removeEditForm(whichID,newValue) {
	document.getElementById(whichID+'Container').innerHTML = '<span id="'+whichID+'" class="doubleClick" ondblclick="onDemandEditForm(this.id,\'title\',\'submit'+whichID+'\');\">'+newValue+'</span>';
}


function addNewListItem(whichEl,newText){
	var targetList = document.getElementById(whichEl);
	if(targetList.getElementsByTagName('ul').length==0){
		alert('makenew');
		var newList = document.createElement('ul');
		targetList.appendChild(newList);
		targetList = newList;
	}
	var newItem = document.createElement('li');
	newItem.innerHTML = newText;
	targetList.appendChild(newItem);
}

/*Positionting of elements from quirksmode*/
function findPosX(obj){
	var curleft = 0;
	if(obj.offsetParent)
		while(1) {
			curleft += obj.offsetLeft;
			if(!obj.offsetParent)
			break;
			obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
		return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if(obj.offsetParent)
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
			break;
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
		return curtop;
}


/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 * Script featured on Dynamic Drive (http://www.dynamicdrive.com) 12.08.2005
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/

var Drag = {
	obj : null,
	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper){
		o.onmousedown	= Drag.start;
		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;
		o.root = oRoot && oRoot != null ? oRoot : o ;
		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "100px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "100px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "100px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "100px";
		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;
		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;
		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},
	start : function(e){
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);
		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;
		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}
		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}
		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;
		return false;
	},
	drag : function(e){
		e = Drag.fixE(e);
		var o = Drag.obj;
		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;
		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)
		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;
		Drag.obj.root.onDrag(nx, ny);
		return false;
	},
	end : function(){
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},
	fixE : function(e){
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};
// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresarch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// ====================================================================
function URLEncode(szPlaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = szPlaintext;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode(szURLEncoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = szURLEncoded;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   
   return plaintext;
};
