function flashNote(note) {
	$('flash_note').innerHTML = note;
	$('flash_note').style.display = "block";
	new Effect.Highlight('flash_note');
}

function navigate_search_page(form_name, async, dir_field, dir) {
	o = $(dir_field);
	if (o) o.value = dir;
	frm = $(form_name);
	if (async)
		frm.onsubmit();
	else
		frm.submit();
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} 
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function selectRange(textBox, start, end) {
   if (textBox.setSelectionRange) { 
		// Mozilla
        textBox.setSelectionRange(start, end);
    }
    else if (textBox.createTextRange) { 
		// IE
        var textRange = textBox.createTextRange();
        textRange.moveStart("character", start);
        textRange.moveEnd("character", end - textBox.value.length);
        textRange.select();
    }
    else {
        textBox.select();
    }
}

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;
  }
  
  function findWidth(obj)
  {
		return document.defaultView.getComputedStyle(obj, null).getPropertyValue('width');
  }

  function findHeight(obj)
  {
		return document.defaultView.getComputedStyle(obj, null).getPropertyValue('height');
  }

  function $$() {
	var results = [], element;
	for (var i = 0; i < arguments.length; i++) {
		element = arguments[i];
		if (typeof element == 'string')
		element = parent.document.getElementById(element);
		results.push(Element.extend(element));
	}
  return results.length < 2 ? results[0] : results;
}

/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.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)
}

// This function will recurse down to the last element
function getByClassNameRecursive(oElm, className) {
  var arr = new Array();
  var children = oElm.all; 
  for (var i = 0; i < children.length; i++) {
    c = children[i];
    if (c.className.match(className))
      arr.push(c);
    arr.concat(getByClassNameRecursive(c, className)); 
  }
  return arr;
}

var formDirty = false;

function markFormDirty(dirty) {
	formDirty = dirty;
}

function markFormClean() {
	markFormDirty(false);
}

function isFormDirty() {
	return formDirty;
}

function promptSave() {
	if (isFormDirty()) 
		return "Your changes will be lost.";
}

function valueChanged() {
	markFormDirty(true);
}

function saveOnExit() {
	window.onbeforeunload = promptSave;
	
	window.onload = registerForValueChange;
}
	
function registerForValueChange() {
	for (i = 0; i < document.forms.length; i++) {
		// alert(document.forms[i].childNodes.length);
		form = document.forms[i];
		children = form.childNodes;
		for (j = 0; j < children.length; j++) {
			try {
				children[j].onchange = valueChanged;
			}
			catch(e) {
			}
		}
		form.onsubmit = markFormClean;
	}
}

/// Copies the selected state of masterBox into all checkboxes having className
function selectCheckboxesLikeThis(masterBox, className) {
	boxes = getElementsByClassName(document, "input", className);
	for (i = 0; i < boxes.length; i++) {
		boxes[i].checked = masterBox.checked;
	}
}

function getCheckboxSelectCount(className) {
	checkCount = 0;
	boxes = getElementsByClassName(document, "input", className);
	for (i = 0; i < boxes.length; i++) {
		if (boxes[i].checked) {
			checkCount++;
		}
	}
	
	return checkCount;
}

function getDocumentElement() {
	return window.document.documentElement;
}

function clearDocument() {
	window.document.documentElement.innerHTML = "";
}

/// Top scroll position in the document
function getScrollTop() {
	return (document.all)?document.body.scrollTop:window.pageYOffset;
}

function getScrollLeft() {
	return (document.all)?document.body.scrollLeft:window.pageXOffset;
}

function setSize(element, width, height) {
	if (element.style.pixelWidth) {
		if (width)
			element.style.pixelWidth = width;
		if (height)
			element.style.pixedHeight = height;
	}
	else {
		if (width)
			element.style.width = width;
		if (height)
			element.style.height = height;
	}
}

function moveTo(element, x, y) {
	if (element.style.pixelLeft) {
		if (x)
			element.style.pixelLeft = x;
		if (y)
			element.style.pixelTop = y;
	}
	else {
		if (x)
			element.style.left = x;
		if (y)
			element.style.top = y;
	}
}

function centerElement(elementId) {
	element = $(elementId);
	if (element) {
		width = parseInt(element.style.width);
		height = parseInt(element.style.height);
		docWidth = document.body.clientWidth;
		docHeight = document.body.clientHeight;
		leftOffset = (docWidth - width) / 2;
		topOffset = (docHeight - height) / 2;
		
		moveTo(element, getScrollLeft() + leftOffset, getScrollTop() + topOffset);
	}
}

function dimBackground(doIt) {
	panel = $('dim_background');
	if (doIt) {
		setSize(panel, document.body.scrollWidth, document.body.scrollHeight);

		panel.style.display = "block";
	}
	else {
		panel.style.display = "none";
	}
}

function positionPopup(popupName) {
	popup = $(popupName);
//	docWidth = document.body.clientWidth;
//	if (docWidth * 0.8 > parseInt(popup.style.width)) {
//		setSize(popup, docWidth * 0.8, null);	
//	}
	centerElement(popup);
}

function dialogMessage(dialogName, msg) {
	$("inner_dialog_message_" + dialogName).innerHTML = msg;
	slider = 'dialog_message_' + dialogName;
	Effect.SlideDown(slider);
}

function dialogMessageHide(dialogName) {
	slider = 'dialog_message_' + dialogName;
	Effect.SlideUp(slider);
}

// Changes the cursor to an hourglass
function setCursorWait() {
	document.body.style.cursor = 'wait';
}

// Returns the cursor to the default pointer
function setCursorDefault() {
	document.body.style.cursor = 'default';
}

function set_url_id(url, id) {
  url = url.replace(new RegExp("\\?.*"), "");
  url = url.replace(new RegExp("/[0-9]*$"), "");
  url += "/" + id;
  return url;
}
