function doRequest(where,post,doFunction,error) {
  this.Display = function(result) { doFunction(result.responseText); }
  this.Fail = function(result){ if(error) alert(error);}
  YAHOO.util.Connect.asyncRequest('POST',where,{success:this.Display,failure:this.Fail},post);       
}

function changeValidateRequired(formname,name,required) {
  for(var i=0; i<validate[formname].length; i++)
    if(validate[formname][i][0] == name) { validate[formname][i][2] = required; break; }  
}

//function set_focus() { document.getElementById('name').focus(); }

function my_popup(module, field_array, call_back_function, form_name) {
  if(!call_back_function) call_back_function = "set_return";
  if(!form_name) form_name = "EditView";
  return open_popup(module, 600, 400, "", true, false, {"call_back_function":call_back_function,"form_name":form_name,"field_to_name_array":field_array});
}

function addEvent(object,eventName,do_function) {
  if(typeof(object) == "string") object = document.getElementById(object);
  if(!object) { alert('No object in function addEvent!'); return; }
  if(object.addEventListener) {
    object.addEventListener(eventName, do_function, false);
	} else {
		object.attachEvent('on'+eventName, do_function);
	} 
}


var tbody_;
var ItemListSave;
var ItemListClear;
var ItemListFil;

addEvent(
  window,
  'load',
  function() {
	  
	var phonesTable = new MyTable('phonesTable');
	
	phonesTable.onRefreshRowIndex = function(row) {
		var data = new Object();
		data['index'] = (row.index+1).toString();
		row.cells.item(0).setData(data);
	}
	
	phonesTable.onCreateRow = function(row) {
		row.newPos = false;
		row.ondblclick = function() {}
		row.onSelect = function() {
			for(var i=0; i<this.myTable.colCount(); i++)
			this.cells.item(i).change(!this.newPos);
		}
		row.onDeselect = function() {
			for(var i=0; i<this.myTable.colCount(); i++)
			this.cells.item(i).change(false);
		}
	}
	
	phonesTable.onCreateCell = function(cell) {
	
		var i = cell.index;
		cell.change = function(select) {};

		if(i == 0) {
			cell.setData = function(data) {
				if(data.index) cell.firstChild.value = data.index;
			};
			cell.getData = function(data) {
				data.index = cell.firstChild.value;
			}
			cell.select = function() { this.selectNext(); }
			var edit = document.createElement('input');
			edit.setAttribute('type','text');
			edit.setAttribute('readOnly','readonly');
			edit.setAttribute('tabIndex',1);
			edit.className = 'inputs';
			cell.appendChild(edit);
		}
  
		if(i == 1) {
			cell.change = function(select) {}
			cell.getData = function(data) {
				var cn = this.getElementsByTagName('input');
				data.phone = cn[0].value;
			}
			cell.setData = function(data) {
				var cn = this.getElementsByTagName('input');
				if(data.phone) cn[0].value = data.phone;
			}
			var edit = '<input type="text" onFocus="this.parentNode.select();" tabindex="1" class="inputs" style="height:16px;" autocomplete="off" onKeyDown="return this.parentNode.myTable.KeyPressed(event,this.parentNode);">';
			cell.innerHTML = edit;
		}
      
		if(i == 2) {
			cell.change = function(select) {}
			cell.selectNext = function() { this.nextSibling.selectNext(); }
			cell.getData = function(data) {
				var cn = this.getElementsByTagName('input');
				data.note = cn[0].value;
			}
			cell.setData = function(data) {
				var cn = this.getElementsByTagName('input');
				if(data.note) cn[0].value = data.note;
			}
			var edit = '<input type="text" onFocus="this.parentNode.select();" tabindex="1" class="inputs" style="height:16px;" autocomplete="off" onKeyDown="return this.parentNode.myTable.KeyPressed(event,this.parentNode);">';
			cell.innerHTML = edit;      
		}

		if(i == 3) {
			var img;
			//insert
			img = document.createElement('img');
			img.setAttribute('alt',MOD['LBL_INSERT_NEW_ROW']);
			img.setAttribute('src','modules/EcmCalls/images/insertrow.gif');
			img.style.cursor = 'pointer';
			img.onclick = function() { 
			this.parentNode.myTable.addRow(this.parentNode.parentNode.index+1);
			};
			//delete
			cell.appendChild(img);
			cell.appendChild(document.createTextNode(" "));
			img = document.createElement('img');
			img.setAttribute('alt',MOD['LBL_DELETE_ROW']);
			img.setAttribute('src','modules/EcmCalls/images/deleterow.gif');
			img.style.cursor = 'pointer';
			img.onclick = function() { this.parentNode.parentNode.deleteRow(); };
			cell.appendChild(img);
			cell.appendChild(document.createTextNode(" "));
			//move up
			img = document.createElement('img');
			img.setAttribute('alt',MOD['LBL_MOVE_ROW_UP']);
			img.setAttribute('src','modules/EcmCalls/images/moverowup.gif');
			img.style.cursor = 'pointer';
			img.onclick = function() { this.parentNode.parentNode.moveUp(); };
			cell.appendChild(img);
			cell.appendChild(document.createTextNode(" "));
			//move down
			img = document.createElement('img');
			img.setAttribute('alt',MOD['LBL_MOVE_ROW_DOWN']);
			img.setAttribute('src','modules/EcmCalls/images/moverowdown.gif');
			img.style.cursor = 'pointer';
			img.onclick = function() { this.parentNode.parentNode.moveDown(); }
			cell.appendChild(img);
		}

	}
	
    phonesTable.onSetCellData = function(row,cell,data) {
		if(cell.innerHTML == '') cell.innerHTML = '&nbsp;';
    }


    ItemListSave = function(json) { 
      var data = new Object();
      var tmp;
	  for(var i=0; i<phonesTable.rowCount(); i++) {
		var tmp_data = phonesTable.row(i).getData();
        if(tmp_data['phone'] != '' && tmp_data['note'] != '') data[i.toString()] = tmp_data;
      }
      var r = json ? JSON.stringifyNoSecurity(data) : data;
	  document.getElementById('phone_list').value = r;
    } 
	
	ItemListClear = function(noNew) {
		while(phonesTable.rowCount()>0) phonesTable.row(0).deleteRow(noNew);
	}

    ItemListFill = function() {
      var pl = document.getElementById('phone_list').value;
      if(pl && pl != '') {
        try {
          pl = eval(pl);        
          for(x in pl) if(typeof(pl[x]) == "object") { var pl_row = pl[x]; phonesTable.addRow().setData(pl_row); }
        } catch(err) { pl = null; };
      }
    
      if(phonesTable.rowCount() == 0) phonesTable.addRow();    
	}
    
	ItemListFill();
	
  }
);