
function ClearValClass(theobj,theval,NewClass){
 if (theobj.value==theval) {
  theobj.value="";
  theobj.className=NewClass;
 }
}

function ClearValColor(theobj,theval,NewColor){
 if (theobj.value==theval) {
  theobj.value="";
  theobj.style.color=NewColor;
 } 
}

function TextLimit(theField,theMax){
 if (theField.value.length > theMax) {// if too long...trim it!
  theField.value = theField.value.substring(0, theMax);
  var theAlert = "Only " +theMax+ " characters are allowed. You have exceeded that number.";
  alert(theAlert);
 }
}

String.prototype.trim = function() {
 a = this.replace(/^\s+/, '');
 return a.replace(/\s+$/, '');
};

function ValidateSearch(theField,EnterMess){
 if (!EnterMess) EnterMess = "";
 if (theField.value == "" || theField.value == EnterMess){
  alert("Please enter a word or phrase to search for");
  theField.focus();
  return false;
 }
 return true;
}

function RotateImage(theObj, thePrefix, MaxNum) {
 CurrentNum = theObj.src.substr(theObj.src.length-5,1)
 if (CurrentNum == MaxNum) {
  NextNum=1;
 }else{
  NextNum = eval(CurrentNum) +1;
 }
 
 NextSrc = thePrefix +NextNum +".jpg";
 theObj.src=NextSrc;
}

function ToggleImage(theObj,Image1,Image2) {
 if (theObj.src.search(Image1)>=0 ) {
  theObj.src=Image2;
 }else{
  theObj.src=Image1; 
 }
}
/////////////////////////////////////////////////////////////////////////// GENERIC EDITING

function CheckFocus(Checkfield,trueFocus,falseFocus){ // Moves focus to field based on whether checkbox is checked
 if (Checkfield.checked) {
  trueFocus.focus();
 }else{
  falseFocus.focus();
 }
}

function CheckFocusBlank(Checkfield, Focusfield, Blankfield){
 if (Checkfield.checked){
  Focusfield.focus(); 
 }else{
  Blankfield.value="";
 }
}

function NumberFormat(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function DollarFormat(Num,Width,Cents) {
// Pass Cents as "CENTS" if you want to force the value returned 
// to contain cents even if Num does not.
// Pass Cents as "NO" if you want to round to the nearest $
	
	var theNumber = parseInt(Num);
	
// This version returns blank Num is NaN 
	if ( isNaN(theNumber)) {
		return " ";
	}

	end = "";
//	if (Width < Num.length) Width = 0;
	
	if (Cents == "NO") {
		Num = "" + Math.round(Num);
		
	}else{
		dec = Num.indexOf(".");
		if (dec > -1) end += Num.substring(dec,Num.length);
		
		if (end.length >3) {
			var theCents = Math.round(end.substring(1,3) + "." + end.substring(3,end.length));
			end = "." + theCents;
		}
			 
		if ( (end.length >1 && end.length <3) || Cents == "CENTS"){
			trailing = ".00";
			end += trailing.substring(end.length,3);
		}
		
		Num = "" + theNumber;
	}
	
	var temp1 = "";
	var temp2 = "";
	var count = 0;
	for (var k = Num.length-1; k >= 0; k--) {
		var oneChar = Num.charAt(k);
		if (count == 3) {
			temp1 += ",";
			temp1 += oneChar;
			count = 1;
			continue;
		}else {
			temp1 += oneChar;
			count ++;
	   }
	}
	
	for (var k = temp1.length-1; k >= 0; k--) {
		var oneChar = temp1.charAt(k);
		temp2 += oneChar;
	}
	
	var TotLength = temp2.length +end.length;
	
	if (TotLength < Width){
		var Spaces = "            ";
		temp2 = Spaces.substring(1,Width -TotLength) +temp2;
	}
	temp2 = "$" + temp2 + end;
	return temp2;
}

function MakeNumber(theStr){
 var DollarSpot= theStr.indexOf("$");
 
 if (DollarSpot>=0){
  var stripped = theStr.substring(DollarSpot +1);
 }else{
  var stripped = theStr;
 }
 
 var aStr = stripped.split(",");
 var PureNum="";
 for (var i = 0; i < aStr.length; i++) {
  PureNum = PureNum +aStr[i];
 }
 
 var theNum = parseFloat(PureNum);
	if ( isNaN(theNum) ) theNum=0;
 
 return theNum;
}

function MustBeNumber(theObj){
 theVal = theObj.value;
 if (isNaN(theVal)){
  alert("Please Enter a Number");
  theObj.select();
  return false;
 }
 return true;
}

//////////////////////////////////////////////////////////////////  Browser Windows

function LoginParent() {
 camefrom.document.loginform.username.focus();
 window.close(); 
}

function MoveParent(newURL){
	camefrom.location=newURL;
	window.close(); 
}

function PictureWindow(theImage,theDescrip) {
 if (theDescrip) {
  theURL = "BigPicture.cfm?theImage=" +theImage +"&theDescrip=" +theDescrip;
 }else{
  theURL = "BigPicture.cfm?theImage=" +theImage;
 }

	if (!window.Big_window||Big_window.closed) { 
		Big_window = window.open(theURL,"Bigw","scrollbars,resizable,width=600,height=500"); 
	}else{
		Big_window.location =theURL;
		Big_window.focus();
	}	
 return false;
//	Big_window.camefrom = self;
}


function ImageLinkWin() {
	if (!window.Img_window||Img_window.closed) { 
		Img_window = window.open("ImageLink.cfm","Imgw","scrollbars,resizable,width=600,height=500"); 
	}else{
		Img_window.location =theURL;
		Img_window.focus();
	}	
 return false;
}

function PortraitWindow(theImage,theTitle) {
 theURL="PortraitWindow.cfm?Image=" +theImage
      + "&Title=" +theTitle;

	if (window.portrait_window) {portrait_window.close();}

	portrait_window = window.open(theURL,"LargePortrait","resizable,width=425,height=475");
	portrait_window.camefrom = self;
}
 
function HelpWindow(theURL,WinSize) {
 if (!WinSize){WinSize= "width=600,height=465";}
 WinAttributes = "scrollbars,resizable," +WinSize;
 
	if (!window.Help_window||Help_window.closed) { 
		Help_window = window.open(theURL,"Help",WinAttributes);
	}else{
		Help_window.location =theURL;
		Help_window.focus();
	}	
	Help_window.camefrom = self;
 return false;
}

function UploadImgWin(ImgTagName,ImgFileName,ImgFieldName,ImgPageTitle) {
 if (window.upload_window) upload_window.close();
 var theURL = "Upload.cfm?ImgTagName=" +ImgTagName
             +"&ImgFileName=" +ImgFileName 
             +"&ImgFieldName=" +ImgFieldName 
             +"&ImgPageTitle=" +ImgPageTitle;
	upload_window = window.open(theURL,"Upload","resizable,width=600,height=600");
	upload_window.camefrom = self;
}

////////////////////////////////////////////////////////////////////   MM
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_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
