//Javascript name: GAMCore.js
//Date created: 09/28/2005
//Scripter: Manjukumar Ramu
//Copyright (c) 2005 Gloabl Animal Management, Inc

var checkflag = false;
function checkAll(field, checkedFlag) 
{
	// 20070629 C. Liang Added checkedFlag for Bug #453
	if (checkedFlag != null)
	{
		checkflag = ! checkedFlag;
	}
	//field = document.animalMovementList.eventKey;
	var singleFlag = false;
	if (checkflag == false) 
	{
		for (i = 0; i < field.length; i++) 
		{
			field[i].checked = true;
			singleFlag = true;
		}
		
		if (!singleFlag)
		{
			field.checked = true;
		}
		checkflag = true;
		return "Uncheck All"; 
	}
	else 
	{
		for (i = 0; i < field.length; i++) 
		{
			field[i].checked = false;
			singleFlag = true;
		}
		if (!singleFlag)
		{
			field.checked = false;
		}    
		checkflag = false;
		return "Check All"; 
	}
}  
 
function popup_window( url, id, width, height )
{
  //extract the url parameters if any, and pass them to the called html
  var tempvar=document.location.toString(); // fetch the URL string
  var passedparams = tempvar.lastIndexOf("?");
  if(passedparams > -1)
  {
     url += tempvar.substring(passedparams);
  }
  popup = window.open( url, id, 'toolbar=no,scrollbars=no,location=no,statusbar=no,menubar=no,resizable=no,width='+ width + ',height=' + height + '' );
  popup.focus();
}

//This function display or hid the given div
  function show (id, visible)
  {
    var el1 = document.getElementById (id);
    el1.style.display = visible;
  }

//Phone number format to (123) 456-7890
function formatPhone(field) 
{
  var strValue, strResult, c;

  strValue= field.value;
  if (strValue == '')
    return;
  strResult='';

  
  for (var i = 0; i < strValue.length; i++) 
  {   
    c=strValue.charAt(i);
    //if (c >= '0' && c <='9') strResult=strResult+c;
    if (c != ')' && c != '(' && c != '.' && c != '-' && c != ' ')
      strResult=strResult+c;
  }
  //alert(strResult);
  //strResult = strValue;

  field.value = '('+strResult.substring(0, 3)+') '+strResult.substring(3, 6)+'-'+strResult.substring(6);
}

// Adds onmouseover, onmouseout, and onclick handlers to each table row.  The onmouseover handler changes the row's class attribute to
// rowMouseOver.  The onmouseout handler changes it back.  The onclick function makes a request for the specified url, including the
// innerHTML of the specified column as a request parameter.
function addRowHandlers(tableId, rowClassName, url, paramName, columnIndex) {
    var previousClass = null;
    var table = document.getElementById(tableId);
    var rows = table.getElementsByTagName("tr");
    for (i = 1; i < rows.length; i++) {
        rows[i].onmouseover = function () {
            previousClass = this.className;
            this.className = this.className + " " + rowClassName ;
        };
        
        rows[i].onmouseout = function () {
            this.className = previousClass;
        };
        /*
        rows[i].onclick = function () {
            var cell = this.getElementsByTagName("td")[columnIndex];
            var paramValue = cell.innerHTML;
            location.href = url + "?" + paramName + "=" + paramValue;
        };*/
    }
}

//Add elements to given select box
function addElementToList (list, value, label)
{
  var option = document.createElement("option");
  option.value = value;
  var labelNode = document.createTextNode(label);
  option.appendChild(labelNode);
  list.appendChild(option);
}

//Clear the given select box
function clearList (list)
{
  while (list.length > 0)    
  {
    list.remove(0);
  }
}

function copyDate(src, dest, checkList) 
{
	if (checkList.length == null)
	{	// Only one element
		if (checkList.checked)
			dest.value = src.value;
	}
	else
	{
		for (i = 0; i < checkList.length; i++) 
			if (checkList[i].checked)
				dest[i].value = src.value;
	}
}

function leftTrim(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function rightTrim(sString)
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function trim(sString)
{
	return leftTrim(rightTrim(sString));
}
