function toggleLayer( whichLayer )
{
  var elem, vis;
  var a_elem, a_vis;
  if( document.getElementById ) // this is the way the standards work
   { elem = document.getElementById( whichLayer );
    a_elem = document.getElementById( whichLayer + '_anchor' );}
  else if( document.all ) // this is the way old msie versions work
      {elem = document.all[whichLayer];
      a_elem = document.all[whichLayer + '_anchor'];}
  else if( document.layers ) // this is the way nn4 works
    {elem = document.layers[whichLayer];
    a_elem = document.layers[whichLayer + '_anchor'];}
  vis = elem.style;
  a_vis = a_elem;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
  if (a_vis) {
     if (vis.display==''||vis.display=='block') {a_vis.firstChild.data = a_vis.firstChild.data.replace(/\+/,"-");}
     if (vis.display=='none') { a_vis.firstChild.data = a_vis.firstChild.data.replace(/-/,"+"); }
  }
}

function new_win(width,height,top, left, url, wintype)
{
    var features = 'resizable=yes,scrollbars=yes,toolbar=no,location=0,menubar=0,status=no,'; 
    if (width == -1) {
       width = screen.availWidth - 15;
    }
    
    if (height == -1) {
       height = screen.availHeight -65;
    }

    if (left == -1) { // right justified
      if (screen.availWidth) {
          left = (screen.availWidth - (width + 15));
      }
    }

    if (top == -1) {
      if (screen.availHeight) {
          top = (screen.availHeight - (height + 65));
      }
    }

    if (left == -99) { // center the form horizontally
      if (screen.availWidth) {
          left = Math.ceil(screen.availWidth / 2) - Math.floor(width/2) ;
      }
    }    
    
    if (top == -99) { // center the form horizontally
      if (screen.availHeight) {
          top = Math.ceil(screen.availHeight / 2) - Math.floor(height/2) ;
      }
    }    

    features = features + 'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left;

    var ticketWin = window.open(url, wintype, features);
    ticketWin.focus();
}

function markrow(obj) {
  var szColorTo = '#FF9966';
  var szColorOriginal = '#DFDFDF';
  
  var pTR = obj.parentNode.parentNode;
  if (pTR.className=='odd') {szColorOriginal='#efefef';}
  else {szColorOriginal='#fafafa';}
  
     
  if(pTR.nodeName.toLowerCase() != 'tr')
  {
      return true;
  }
  
  if(obj.checked == true)
  {
      pTR.style.backgroundColor = szColorTo;
  }
  else
  {
      pTR.style.backgroundColor = szColorOriginal;
  }
  return true;
}

function SetAllCheckBoxes(FormName, FieldName, CheckValue  )
{ 
   var pvalue = 'OFF';
   if (CheckValue) pvalue = 'ON';
   alert('you just clicked on Set all checkboxes set to ' + pvalue);
	if(!document.forms[FormName]) {
	   alert('exiting program because formname invalid');
		return;
	}
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes) {
	   alert('exiting program because objCheckBoxes invalid');
		return;
	}
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		{objCheckBoxes.checked = CheckValue;
		 markrow(objCheckBoxes);
		}
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++) {
		   objCheckBoxes[i].checked = CheckValue;
		   markrow(objCheckBoxes[i]);
		}
}

function edit_contract(id)
{
    var features = 'width=460,height=700,top=30,left=30,resizable=yes,scrollbars=yes,toolbar=no,location=no,menubar=no,status=no';
    var contractWin = window.open('contract_renew.php?id=' + id + '&action=edit', '_postNote', features);
    contractWin.focus();
}
function create_contract(id)
{
    var features = 'width=460,height=700,top=30,left=30,resizable=yes,scrollbars=yes,toolbar=no,location=no,menubar=no,status=no';
    var contractWin = window.open('contract_renew.php?id=' + id + '&action=add', '_postNote', features);
    contractWin.focus();
}

/**
 * marks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param    container    DOM element
 */
function markAllRows( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            if ( checkbox.disabled == false ) {
                checkbox.checked = true;
                if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
                    rows[i].className += ' marked';
                    marked_row[unique_id] = true;
                }
            }
        }
    }

    return true;
}

/**
 * marks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param    container    DOM element
 */
function unMarkAllRows( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            checkbox.checked = false;
            rows[i].className = rows[i].className.replace(' marked', '');
            marked_row[unique_id] = false;
        }
    }

    return true;
}

function checkthis(id) 
{
	var field = document.getElementById('ckbList' + id);
	field.checked=true;
	markrow(field);
}
	
function checkAll(field)
{
for (i = 0; i < field.length; i++) {
	field[i].checked = true ;
	markrow(field);
}
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++) {
	field[i].checked = false ;
	markrow(field);
}
}


function addRowToScanTimeTable(tblId,callerid,prodname)
{
  var tbl = document.getElementById(tblId);
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow+callerid;
  var row = tbl.insertRow(lastRow);
  
  // Product Service Cell
  var cellProd = row.insertCell(0);
  var textNode = document.createTextNode(prodname);
  cellProd.appendChild(textNode);
  
  // Week Day - Day of week
  var cellDOWSel = row.insertCell(1);
  var sel = document.createElement('select');
  sel.name = 'dow' + iteration;
  sel.options[0] = new Option('Weekday', '8');
  sel.options[1] = new Option('Saturday', '7');
  sel.options[2] = new Option('Sunday', '7');
  cellDOWSel.appendChild(sel);

  // Peak Checkbox
  var cellPeak = row.insertCell(2);
  var ckb = document.createElement('input');
  ckb.type='checkbox';
  ckb.name = 'apstpeak'+ iteration;
  ckb.id = ckb.name;
  ckb.value = '1';
  cellPeak.appendChild(ckb);
  
  // Start Time
  var cellStartTime = row.insertCell(3);
  var st = document.createElement('input');
  st.type = 'text';
  st.name = 'apststarttime' + iteration;
  st.id = st.name;
  st.size = 5;
  cellStartTime.appendChild(st);

  // End Time
  var cellEndTime = row.insertCell(4);
  var et= document.createElement('input');
  et.type = 'text';
  et.name = 'apstendtime' + iteration;
  et.id = et.name;
  et.size = 5;
  cellEndTime.appendChild(et);
  
  // Interval Minutes
  var cellInterval = row.insertCell(5);
  var intv = document.createElement('input');
  intv.type = 'text';
  intv.name = 'apstinterval' + iteration;
  intv.id = intv.name;
  intv.size = 5;
  cellInterval.appendChild(intv);
  
  //Set the counter field
  var ScanTimeCount = document.getElementById('tot_scantimes');
  ScanTimeCount.Value = iteration; 
  alert("value of tot_scantimes =" + ScanTimeCount.Value);
  
}
function keyPressTest(e, obj)
{
  var validateChkb = document.getElementById('chkValidateOnKeyPress');
  if (validateChkb.checked) {
    var displayObj = document.getElementById('spanOutput');
    var key;
    if(window.event) {
      key = window.event.keyCode; 
    }
    else if(e.which) {
      key = e.which;
    }
    var objId;
    if (obj != null) {
      objId = obj.id;
    } else {
      objId = this.id;
    }
    displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
  }
}
function removeRowFromTable()
{
  var tbl = document.getElementById('tblSample');
  var lastRow = tbl.rows.length;
  if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
function openInNewWindow(frm)
{
  // open a blank window
  var aWindow = window.open('', 'TableAddRowNewWindow',
   'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
   
  // set the target to the blank window
  frm.target = 'TableAddRowNewWindow';
  
  // submit
  frm.submit();
}
function validateRow(frm)
{
  var chkb = document.getElementById('chkValidate');
  if (chkb.checked) {
    var tbl = document.getElementById('tblSample');
    var lastRow = tbl.rows.length - 1;
    var i;
    for (i=1; i<=lastRow; i++) {
      var aRow = document.getElementById('txtRow' + i);
      if (aRow.value.length <= 0) {
        alert('Row ' + i + ' is empty');
        return;
      }
    }
  }
  openInNewWindow(frm);
}


function openHelp(rel_url, topic)
{
    var width = 500;
    var height = 450;
    var w_offset = 30;
    var h_offset = 30;
    var location = 'top=' + h_offset + ',left=' + w_offset + ',';
    if (screen.width) {
        location = 'top=' + h_offset + ',left=' + (screen.width - (width + w_offset)) + ',';
    }
    var features = 'width=' + width + ',height=' + height + ',' + location + 'resizable=yes,scrollbars=yes,toolbar=no,location=no,menubar=no,status=no';
    var helpWin = window.open(rel_url + 'help.php?topic=' + topic, '_help', features);
    helpWin.focus();
}

function msgbox(strMsg)
{
   var x=window.confirm(strMsg)
   if (x)
   window.alert("Agent has been deleted.")
   else
   window.alert("Request Cancelled")
}


function downloadCSV(){
      var f = this.document.csv_form;
      f.submit();
      return false;
      }

/*  function ValidateForm(){

        if (document.frm.assoc_name.value=="") {
            alert("Please enter Sales Associate Name.")
            document.frm.assoc_name.focus()
        return false }        
        else {
            if (!/[^a-zA-Z]/.test(document.frm.assoc_name.value)) {
                alert("some message on alpha requirement");
                return false;
            }
        }
           
    if (!document.frm.option1.checked) {
            alert("Please check why did customer choose not to follow rules.")
            document.frm.option1.focus()
        return false }
        


        if (!document.frm.agree.checked) {
            alert("You must agree to our terms & conditions, please check the checkbox.")
            document.frm.agree.focus()
        return false;}                        
        
    }
*/    
