
/*
 * This function, called in the body OnLoad event, puts the cursor focus
 *				in the first form field element on the page, if there is any.
 * Date: 10/12/2001
 *
 */
 
function firstFocus()
{
   if (document.forms.length > 0)
   {
	  var TForm = document.forms[0];
	  for (i=0;i<TForm.length;i++)
      {
         if ((TForm.elements[i].type=="text")||
           (TForm.elements[i].type=="textarea")/*||
           (TForm.elements[i].type.toString().charAt(0)=="s")*/)
         {
			document.forms[0].elements[i].focus();
            break;
         }
      }
   }
}

function redLight(element)
	{
   element.className = redlight;
   }

function yellowLight(element)
	{
   element.className = yellowlight;
   }

function greenLight(element)
	{
   element.className = greenlight;
   }

function lengthWarning(fieldName,fieldLength,fieldLimit){
	var msg = "";
	if (fieldLength > fieldLimit) {
		msg = "You entered " + fieldLength + " characters in the " + fieldName + " field. The limit is " + fieldLimit + ".";
	}
	return msg;
}
function formatPhone (field)
{
	field.value = trim(field.value);

    var ov = field.value;
    var v = "";
    var x = -1;

	// is this phone number 'escaped' by a leading plus?
	if (0 < ov.length && '+' != ov.charAt(0)) {	// format it
	    // count number of digits
	    var n = 0;
		if ('1' == ov.charAt(0)) {	// skip it
			ov = ov.substring(1, ov.length);
		}

	    for (i = 0; i < ov.length; i++) {
	        var ch = ov.charAt(i);

	        // build up formatted number
	        if (ch >= '0' && ch <= '9') {
	            if (n == 0) v += "(";
	            else if (n == 3) v += ") ";
	            else if (n == 6) v += "-";
	            v += ch;
	            n++;
	        }
	        // check for extension type section; 
	        // are spaces, dots, dashes and parentheses the only valid non-digits in a phone number?
	        if (! (ch >= '0' && ch <= '9') && ch != ' ' && ch != '-' && ch != '.' && ch != '(' && ch != ')') {
	            x = i;
	            break;
	        }
	    }
	    // add the extension
	    if (x >= 0) v += " " + ov.substring(x, ov.length);

	    // if we recognize the number, then format it
	    if (n == 10 && v.length <= 40) field.value = v;
	}
    return true;
}

// This function rounds and formats dollar values.

// Author   : Jonathan Weesner (http://cyberstation.net/~jweesner/)
// Copyright: Use freely. Keep Author and Copyright lines intact.
var WGdc=".";  
var WGgc="";  
var WGnc="-";  
var WGcs="";
function moneyFormat(A,W) 
	 {
   var N=Math.abs(Math.round(A*100));
   var S=((N<10)?"00":((N<100)?"0":""))+N;
   S=WGcs+((A<0)?WGnc:"")+WGgroup(S.substring(0,(S.length-2)))+WGdc+
      S.substring((S.length-2),S.length)+((A<0&&WGnc=="(")?")":"");
   return (S.length>W)?"Over":S;
	 }
	 
// WGgroup inspired by Bill Dortch's usenet post (www.hidaho.com)
function WGgroup(S) {
   return (S.length<4)?S:(WGgroup(S.substring(0,S.length-3))+
      WGgc+S.substring(S.length-3,S.length));}
// End of WeeGadget's Money Function Freebies

// Duplicates ColdFusion's IsNumeric function
function isnumeric ( n ) 
{
  var v = 1.0 * n ;
  if ( v == 0.0 || isNaN(v) )
      return false ;
  else
      return true ;
}

// Duplicates ColdFusion's Val function
function val ( n ) 
{
  if ( isnumeric(n) == true ){ return parseFloat(n) ; }  else { return parseFloat(0) };
}

function formatDecimal(argvalue, addzero, decimaln) {
  var numOfDecimal = (decimaln == null) ? 2 : decimaln;
  var number = 1;

  number = Math.pow(10, numOfDecimal);

  argvalue = Math.round(parseFloat(argvalue) * number) / number;
  // If you're using IE3.x, you will get error with the following line.
  // argvalue = argvalue.toString();
  // It works fine in IE4.
  argvalue = "" + argvalue;

  if (argvalue.indexOf(".") == 0)
    argvalue = "0" + argvalue;

  if (addzero == true) {
    if (argvalue.indexOf(".") == -1)
      argvalue = argvalue + ".";

    while ((argvalue.indexOf(".") + 1) > (argvalue.length - numOfDecimal))
      argvalue = argvalue + "0";
  }

  return argvalue;
}

function getFront(mainStr,searchStr)
{
     foundOffset = mainStr.indexOf(searchStr)
     if (foundOffset == -1)
     {
          return null
     }
     return mainStr.substring(0,foundOffset)
}

// extract back end of string after searchString
function getEnd(mainStr,searchStr)
{
     foundOffset = mainStr.indexOf(searchStr)
     if (foundOffset == -1)
     {
          return null
     }
     return mainStr.substring(foundOffset+searchStr.length,mainStr.length)
}

function replaceString(mainStr, searchStr, replaceStr)
{
     var front = getFront(mainStr, searchStr)
     var end = getEnd(mainStr,searchStr)
     if (front != null && end != null)
     {
          return front + replaceStr + end
     }
     else
     {
          return mainStr
     }
     return null
}

var lastMouseX;
var lastMouseY;
var curPopupWindow = null;
var helpWindow = null;

function setLastMousePosition(e) {
    e = window.event;
    lastMouseX = e.screenX;
    lastMouseY = e.screenY;
}

/**
 * Calls through to the openPopupFocus() with closeOnLoseFocus set to true.
 */
function openPopup(url, name, pWidth, pHeight, features, snapToLastMousePosition) {
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
		{ 
			setLastMousePosition(event);
		}
	else
 		{
 			snapToLastMousePosition = false;
 		}
	openPopupFocus (url, name, pWidth, pHeight, features, snapToLastMousePosition, true);
}

/**
 * Handles popup windows.
 * If snapToLastMousePosition is true, then the popup will open up near the mouse click.
 * If closeOnLoseFocus is true, then it will close when the user clicks back into the browser window that opened it.
 */
function openPopupFocus(url, name, pWidth, pHeight, features, snapToLastMousePosition, closeOnLoseFocus) {
    closePopup();
    if (snapToLastMousePosition) {
        if (lastMouseX - pWidth < 0) {
            lastMouseX = pWidth;
        }
        if (lastMouseY + pHeight > screen.height) {
            lastMouseY -= (lastMouseY + pHeight + 50) - screen.height;
        }
        lastMouseX -= pWidth;
        lastMouseY += 10;
        features += "screenX=" + lastMouseX + ",left=" + lastMouseX + ",screenY=" + lastMouseY + ",top=" + lastMouseY;
    }
    if (closeOnLoseFocus) {
        curPopupWindow = window.open(url, name, features, false);
        curPopupWindow.focus ();
    } else {
        // assign the open window to a dummy var so when closePopup() is called it won't be assigned to curPopupWindow
        win = window.open(url, name, features, false);
        win.focus ();
    }
}

function closePopup() {
    if (curPopupWindow != null) {
       
        if (!curPopupWindow.closed) {
            curPopupWindow.close();
        }
        curPopupWindow = null;
    }
}

//Centers the popup window
function NewWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

/** Hides Input types of "submit", "button" & "reset" so button can't be clicked twice on form post
  * Author: Kyle Murphy 
  * Date: 3/31/04
  * Comments: I would have liked to disable the buttons, but this makes the buttons undefined & a lot
  * of our self-posting forms are relying on them to be defined.
  * Example: <form name="myForm" action="template.cfm" method="post" onsubmit="return hideSubmits(this);">
*/
function hideSubmits(theform) {
	if (document.all || document.getElementById) {
		for (i = 0; i < theform.length; i++) {
			var tempobj = theform.elements[i];
			if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset" || tempobj.type.toLowerCase() == "button")
			tempobj.style.visibility = "hidden";
		}
		return true;
	}
}

// KM (9/10/04) - Testing for use on profile pages to cut down on string truncation errors.
function checkLen(objElement,intLength){
    if(objElement.value.length > intLength){
        var strData = objElement.value;
        objElement.value = strData.substr(0,intLength);
		alert('You have reached the maximum number of characters for this field (' + intLength + ').');
		objElement.focus();
    } 
}

//BSD (9/20/04) - Opens window specifically for new Time Card
function openTCWindow(orderID,lt_orderID){
	l=(screen.width/2) - 410;
	t=(screen.height/2) - 450;
	window.open('timecards_popup.cfm?lt_orderID='+lt_orderID+'&orderID='+orderID,'win_tc','width=830,height=900,menubar=no,resizable=yes,status=no,scrollbars=yes,titlebar=no,toolbar=no,top=' + t +',left=' + l);
}

/*
function setCheckAll
	Description: 
		toggles all checkboxes as checked or unchecked:
	Arguments:
		theForm (reference to the actual form; example: this.form)
		obj (name of checkbox that controls the toggle; example 'ckBoxToggle'
		elm (name of checkbox elements to toggle as checked/unchecked); example 'userChoices'			
	Sample usage:
		<input type="checkbox" id="ckBoxToggle" name="ckBoxToggle" onClick="setCheckAll(this.form, 'ckBoxToggle','userChoices');"  />Toggle All
*/
function setCheckAll(theForm, obj,elm){
	var setChecked= "";
	if (theForm.elements[obj].checked == true){
		setChecked= true;
	}else{
		setChecked= false;
	}
	if(theForm.elements[elm].length){
		for(var i = 0; i < theForm.elements[elm].length; i++){			
			theForm.elements[elm][i].checked = setChecked;
		}
	}
}
/*
function setCheckAll
	Description: 
		deselects the controller checkbox when one of the elements is unchecked:
	Arguments:
		theForm (reference to the actual form; example: this.form)
		obj (id of checkbox that that determines if the controller should become unchecked; example 'userChoices1'
		elm (name of controller checkbox element that will be unchecked if another item is deselected); example 'ckBoxToggle'
	Sample usage:
		<input type="checkbox" id="userChoices1" name="userChoices"  value="apple" onClick="setUncheckAll(this.form, 'userChoices1','ckBoxToggle')">Apple
		<input type="checkbox" id="userChoices2" name="userChoices"  value="orange" onClick="setUncheckAll(this.form, 'userChoices2','ckBoxToggle')">Orange		
*/
function setUncheckAll(theForm, obj,elm) {
	if (theForm.elements[obj].checked == false){
		document.getElementById(elm).checked = false ;
	}
}
