String.prototype.replaceAll = function( searchStr, replaceStr ) 
{ 
	var temp = this; 
	while( temp.indexOf( searchStr ) != -1 ) temp = temp.replace( searchStr, replaceStr ); 
	return temp; 
} 

function sel_se_options(selObj, type)
{
	var val = selObj.value;
	if(val==0)
	{
		if(type == 'state')
			select_empty(document.getElementById('search_city'),'Select City');
		select_empty(document.getElementById('search_zip'),'Select zipcode');
	}else{
		var tocken = (type=='state')? 'city':'zip';
		var obj = document.getElementById('search_'+tocken);
		var target_url = "ajax/process.php?type="+type+"&val="+escape(selObj.value);
		if(type == 'city')
		{
			var stateObj = document.getElementById('search_state');
			target_url = "ajax/process.php?type="+type+"&state="+escape(stateObj.value)+"&val="+escape(selObj.value);
		}else{
			select_empty(document.getElementById('search_zip'),'Select zipcode');
		}

		server_requerst(target_url, obj, type);
	}
}

function server_requerst ( target_url, obj, type ) {
	
	type = (type == 'state')? 'City':'zipcode';

	var funcRef = function( msg ) {
		msg = msg.replaceAll('\r','');		
		msg = msg.replaceAll('\n','');

		var array_list = msg.split("#");
		var count = array_list.length;

		var optgroups = obj.childNodes;
		for(i = optgroups.length - 1 ; i >= 0 ; i--)
		{
			obj.removeChild(optgroups[i]);
		}

		var new_node = document.createElement("option");
		
		try {
			obj.add(new_node, null); // standards compliant; doesn't work in IE
		}
		catch(ex) {
			obj.add(new_node); // IE only
		}

		new_node.text = 'Select '+type;
		new_node.value = 0;

		for (i=0; i<count; i++) {
			var new_node = document.createElement("option");

			var id = array_list[i];
			var val = array_list[i];

			try {
				obj.add(new_node, null); // standards compliant; doesn't work in IE
			}
			catch(ex) {
				obj.add(new_node); // IE only
			}
			new_node.text = val;
			new_node.value = id;
		}
	}
	httpRequest( target_url, funcRef );
}

function select_empty(obj, txt)
{
	var optgroups = obj.childNodes;
	for(i = optgroups.length - 1 ; i >= 0 ; i--)
	{
		obj.removeChild(optgroups[i]);
	}

	var new_node = document.createElement("option");
	new_node.text = txt;
	new_node.value = 0;

	try {
		obj.add(new_node, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		obj.add(new_node); // IE only
	}
}