//$Id: lib.js,v 1.4 2007/04/23 05:00:34 edw Exp $
//library of useful js functions


//trim string
function TrimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}



//validate text fields
function ValidateText (field, minLength, maxLength, testReg) {
	
	var fieldLength = TrimString(field.value).length;
	
	if (fieldLength < minLength) return false;
	if ((maxLength > 0) && (fieldLength > maxLength)) return false;
	
	if ((fieldLength > 0) && testReg && !field.value.match(testReg)) return false;
	
	return true;
	
}



//validate that a radio input has been checked
function ValidateRadio (field) {
	
	var i;
	//alert(field);
	for (i = 0; i < field.length; i++) {
		if (field[i].checked) return true;
	}
	
	return false;
	
}



//validate that a select box has been selected
function ValidateSelect (field) {
	
	if (field[field.selectedIndex].value.length < 1) return false;
	
	return true;
	
}



//remove all text and elements from within an element
function EmptyTag (tag) {
	
	var tempNode;
	var last = tag.childNodes.length - 1;
	
	for (var i = last; i >= 0; i--) {
		tempNode = tag.childNodes[i];
		if ((tempNode.nodeType == 1) || (tempNode.nodeType == 3)) {
			tag.removeChild(tempNode);
		}
	}
	
}



//takes a string and converts it to a document fragment, converts newlines to <BR> tags
function CreateDocFrag (str) {
	
	var docFrag = document.createDocumentFragment();
	
	var lines = str.split('\n');
	for (var i = 0; i < lines.length; i++) {
		
		docFrag.appendChild(document.createTextNode(lines[i]));
		docFrag.appendChild(document.createElement('BR'));
		
	}
	
	return docFrag;
	
}



//writes an error message inside a specified tag
function WriteError (tag, str) {
	
	EmptyTag(tag);
	
	tag.appendChild(CreateDocFrag(str));
	
	tag.style.color = '#CF0E01';
	tag.style.fontWeight = 'bold';
	
}



//writes an error message inside a specified tag
function WriteAdminError (tag, str) {
	
	EmptyTag(tag);
	
	tag.appendChild(CreateDocFrag(str));
	
	tag.style.color = '831E00';
	tag.style.fontWeight = 'bold';
	
}




var metroPop = null;


//open pop-up window
function openPop(winPop, winName, url, popWidth, popHeight, scrolling){
  
  if (scrolling === true) scrolling = 'yes';
  
  var details = "height=" + popHeight + ",width=" + popWidth + ",left=100,top=70,status=no,resizable=yes,titlebar=no,toolbar=no,location=no,scrollbars=" + scrolling + ",directories=no";

  if (winPop && winPop.open && !winPop.closed){
    winPop.resizeTo(popWidth, popHeight);				//only works in IE
    winPop.location = url;
    winPop.focus();
  }
  else { 
    winPop = window.open(url, winName, details);
    winPop.focus();
  }
  return winPop;
}


//close pop-up window
function closePop(winPop){
  if (winPop && winPop.open && !winPop.closed){
    winPop.close();
  }
}



// single rollover --

function rollover(imageSource, whichImage, imageWidth, imageHeight ,statusText) {
if (document.images) {
	image = new Image(imageWidth,imageHeight);
	image.src = imageSource;
	
	swapImage(whichImage);
	window.status=statusText;
}
}

function unRollover(imageSource, whichImage, statusText) {
if (document.images) {
	replaceImage(imageSource, whichImage);
	returnStatus(statusText);
}
}

function swapImage(whichImage) {
document.images[whichImage].src = image.src;
}

function replaceImage(imageSource, whichImage) {
document.images[whichImage].src = imageSource;
}


function returnStatus(statusText) {
	changeStatus(statusText);
}

function changeStatus(statusText) {
	window.status=statusText;
}



