/************************************************************************************************************
Ajax chained select
Copyright (C) 2006  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2006
Owner of DHTMLgoodies.com

Adapted to DHTML Suite object coding style
Ken Gregg, 2007

************************************************************************************************************/	

DHTMLSuite.linkedSelect = function()
{
	var target;	      // The id of the select we are going to fill with returned options
	var URL;          // Url to call to get the options for the selected value
	var fieldId;      // the field name parameter we are passing
	var objectIndex;	// Index of this object in the DHTMLSuite.variableStorage.arrayDSObjects array

	try{
		if(!standardObjectsCreated)DHTMLSuite.createStandardObjects();	// This line starts all the init methods
	}catch(e){
		alert('You need to include the dhtmlSuite-common.js file');
	}

//	this.objectIndex = DHTMLSuite.variableStorage.arrayDSObjects.length;
//	DHTMLSuite.variableStorage.arrayDSObjects[this.objectIndex] = this;
  this.fieldId = 'f';
}

DHTMLSuite.linkedSelect.prototype = {	

//  init : function() {},

  setTarget : function (targetID) {
    this.target = targetID;
  },

  // the passed url should not contain a trailing ? or &
  // They are added in this function
  setURL : function (URLToCall) {
    if (URLToCall.indexOf('?') == -1)
      this.URL = URLToCall + '?';
    else
      this.URL = URLToCall + '&';    
  },

  // Set the field name of the field we are passing to the back end
  setFieldId : function(fieldId)
  {
    this.fieldId = fieldId;
  },

  execute : function(selField)
  {
  	var obj = document.getElementById(this.target);
  	var val = selField.options[selField.selectedIndex].value;
  	document.getElementById(this.target).options.length = 0;	// Empty target select box

		var url = this.URL + this.fieldId + '=' + val;	// Build the query string
		var index = DHTMLSuite.variableStorage.ajaxObjects.length;	
		try{
			DHTMLSuite.variableStorage.ajaxObjects[index] = new sack();
		}catch(e){	// Unable to create ajax object - send alert message and return from sort method.
			alert('Unable to create ajax object. Please make sure that the sack js file is included on your page');	
			return;
		}
		DHTMLSuite.variableStorage.ajaxObjects[index].requestFile = url;	// Specifying which file to get
		DHTMLSuite.variableStorage.ajaxObjects[index].onCompletion = function(){ eval(DHTMLSuite.variableStorage.ajaxObjects[index].response) };
		DHTMLSuite.variableStorage.ajaxObjects[index].runAJAX();		// Execute AJAX function	
  }
}

function getCityList(propclass, field) {
  var linkedSel = new DHTMLSuite.linkedSelect();
  linkedSel.setTarget('frm_city');
  linkedSel.setURL('/idx/rs.php?rs_method=citylist&class='+ propclass);
  linkedSel.setFieldId('frm_county');
  linkedSel.execute($(field));
}	
