﻿// JScript File
 
  var ids = new Array('rateno','Inactive','RateTypes', 'PayFreq', 'Payrate', 'OTRate','Tips','Meals',
  'Department','JobDescr', 'Class', 'WCRate', 'WCCode','FedTypes','AddFed',
  'WorkedStateList');
  var skip_test = 0;
  function ShowStateLocalMessages ()
  {
	var msg = "";
	var bfirst = 1;
    var lo_elem = document.getElementById ("GridViewLocal");
    if (lo_elem == null) {
       var llist = document.getElementById ("LocalList");
       if (llist) {
          msg = "Local tax will NOT be calculated for this rate.\r\n";
          bfirst = 0;
       }
    }
    var st_elem = document.getElementById ("GridViewState");
    if (st_elem == null) {
       var slist = document.getElementById ("StateWithList");
       if (slist) {
          if (bfirst == 0)
             msg += "\r\n";
		  msg += "State Withholding Tax will NOT be calculated for this rate.";
          bfirst = 0;
       } else {
          if (bfirst == 0)
             msg += "\r\n";
		  msg += "State Withholding Tax will NOT be calculated for this rate.\r\n";
		  msg += "State(s) must be added to this employee's State Withholding Tax\r\n";
		  msg += "Setup (on the Employee form) in order for it to be available in this rate.";
          bfirst = 0;
       }
    }
	if (bfirst == 0)
		alert (msg);
	return;
  }
  function conSave (value)
  {
	var wis = document.getElementById ("WorkedStateList");
	if (wis) {
		if (wis.selectedIndex == 0) {
			alert ("Select a worked in state");
			wis.focus ();
			return false;
		}
	}
    if (skip_test == 1) {
        skip_test = 0;
        return true;
    }
	if (Number (document.getElementById (ids[4]).value) == 0) {
		alert ("Payrate can not be zero. Changes can not be saved");
		return false;
	}
	ShowStateLocalMessages ();

    if (conChange (value))
        return true;
    return false; 
  }
  
  function conChange(value)
  {
    var bchanged = 0;
      // check to see if any changes to the data entry fields have been made
	for (var i = 1; i < ids.length; i++) {
		var elem = document.getElementById(ids[i]);
		if (elem) {
			if (elem.type == 'select-one') {
			   if (String (elem.selectedIndex) != elem.attributes ["oldval"].value) {
					bchanged = 1;
					break;
			   }
			}
			if (elem.type == 'checkbox') {
				var tcheck = "0";
				if (elem.checked)
					tcheck = "1";
				if(tcheck != elem.parentNode.attributes ["oldval"].value) {
					bchanged = 1; 
					break;
				}
			}
			if (!(elem.type == 'checkbox' || elem.type == 'radio' || elem.type == 'select-one')) {
				if (elem.value != elem.attributes ["oldval"].value) {
					bchanged = 1;
				}
			}
		}
	}
	// test to see if differences btwn saved sts and values on screen
	var ost_count = 0;
	var saved_rst = document.getElementById ("fs_state");
	var st_elem = document.getElementById ("GridViewState");
	if (st_elem) 
		ost_count = document.getElementById ("GridViewState").rows.length - 1;
	if (String (ost_count) != saved_rst.attributes ["st_count"].value)
		bchanged = 1;
	if (bchanged == 0) {
		if (String (ost_count) != "0" && saved_rst.attributes ["st_count"].value != "0") 
			bchanged = rst_changed ();
	}
	var olo_count = 0;
	var saved_rloc = document.getElementById ("fs_local");
	var loc_elem = document.getElementById ("GridViewLocal");
	if (loc_elem) 
		olo_count = document.getElementById ("GridViewLocal").rows.length - 1;
	if (String (olo_count) != saved_rloc.attributes ["loc_count"].value)
		bchanged = 1;
	if (bchanged == 0) {
		if (String (olo_count) != "0" && saved_rloc.attributes ["loc_count"].value != "0") 
			bchanged = rloc_changed ();
	}
	if (value == 0) {
		if (bchanged == 1) {
		   bchanged = confirm ("Save changes?");
		   if (bchanged == 1) {
			 if (Number (document.getElementById (ids[4]).value) == 0) {
				alert ("Payrate can not be zero. Changes unsaved");
				return false;
			  }
			 else {
			    ShowStateLocalMessages ();
				skip_test = 1;
				document.getElementById ("Sve").click ();
				skip_test = 0;
			}
		   }
		}
	} else {
	  if (bchanged == 1)
	      return true;
	  else
	      return false;
	}
	return true;
  }
  // test saved rate locals against screen values
  function rst_changed ()
  {
    var bchanged = 0;
    var index = 0;
    var estate = document.getElementById ("fs_state");
    var st_elem = document.getElementById ("GridViewState");
    if (st_elem) {
		for (var s = 1; s < st_elem.rows.length; s++) {
			var gridrow = st_elem.rows[s];
			var gridcell = gridrow.cells[0];
			if (estate.attributes["st"+index++].value != gridcell.firstChild.data) {
				bchanged = 1;
				break;
			}

			var st_add = document.getElementById ("GridViewState_ctl0" + (s+1) + "_stadd" + s);
			if (estate.attributes["st"+index++].value != st_add.value) {
				bchanged = 1;
				break;
			}
		} 
	}
	return bchanged;
  }
  
  function rloc_changed ()
  {
    var bchanged = 0;
    var index = 0;
    var eloc_rate= document.getElementById ("fs_local");
    var lo_elem = document.getElementById ("GridViewLocal");
    if (lo_elem) {
		for (var s = 1; s < lo_elem.rows.length; s++) {
			var gridrow = lo_elem.rows[s];
			if (gridrow != null) {
				var gridcell = gridrow.cells[0];
				if (eloc_rate.attributes["lo"+index++].value != gridcell.firstChild.data) {
				    bchanged = 1;
				    break;
				}
				var lo_per = document.getElementById ("GridViewLocal_ctl0" + (s + 1) + "_locpercent" + s);
				if (eloc_rate.attributes["lo"+index++].value != lo_per.value){
					bchanged = 1;
					break;
				}
			}
		} 
	}
	return bchanged;
  }
  function tabstart (event)
  {
	if (event.keyCode == 9) {
		var id = document.getElementById ("RateTypes");
		id.focus ();
	}
  }
  
  function enableFields ()
  {
  }