var CPEItems = new Class({
	initialize: function(options){
		this.options 	= options;
		this.xmlDoc		= null;
		this.items		= null;
		
		this.xmlDoc = new DOMParser().parseFromString(this.options.xml, 'text/xml');
		this.xmlDoc.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'"); // the following two lines are needed for IE
		this.xmlDoc.setProperty("SelectionLanguage", "XPath");
	},
	filter: function(id) {
		var attr = new Array();
		switch(id) {
		case 'devices':
			attr.push('(@type<2)');
			if( $('type') && $('type').value != '' ) 						{ attr.push('(@type=\''+$('type').value+'\')'); } 
			if( $('channels') && $('channels').value != '' ) 		{ attr.push('(@channels=\''+$('channels').value+'\')'); }
			if( $('type') && $('type').value != '0') {
				if( $('ethernet') && $('ethernet').value != '' ) 	{ attr.push('(@ethernet=\''+$('ethernet').value+'\')'); }
				if( $('utopia') && $('utopia').checked ) 					{ attr.push('(@utopia=\''+$('utopia').value+'\')'); }
			}
			break;
		case 'serviceplans':
			attr.push('(@type=2)');
			if( $('os') && $('os').value != '' ) 						{ attr.push('(@os=\''+$('os').value+'\')'); } 
			if( $('package') && $('package').value != '' ) 	{ attr.push('(@package=\''+$('package').value+'\')'); }
			break;
		default:
		}
		filterStr = (attr.length>0) ? '['+attr.join(' and ')+']' : '';
		this.items = this.xmlDoc.selectNodes("//items/item"+filterStr); 
	},
	render: function(id) {
		var output = '<table><tr><th>Name</th><th>Description</th>';
		if(id=='serviceplans') { output		+= '<th>&nbsp;</th>' };
		output		+= '</tr>';
		if(this.items.length>0) {
			for(var i=0;i<this.items.length;i++) {
				output += '<tr class="line'+i%2+'"><td width="20%">';
				output += this.items[i].childNodes[0].firstChild.nodeValue;
				output += '</td>';
				output += '<td width="65%">'+this.items[i].childNodes[1].firstChild.nodeValue;
				if(!isNaN(this.items[i].childNodes[2].firstChild.nodeValue)) { output += ' <a href="/web/documents/document.jsp?docid='+this.items[i].childNodes[2].firstChild.nodeValue+'" title="View Product Brief"><img src="http://www.mindspeed.com/mspd/images/icon_pdf.gif" alt=""/></a>'; }
				output += '</td>';
				if(id=='serviceplans') {
					output += '<td align="center">';
					output += (this.items[i].getAttribute('status')=='1') ? 'Available' : '<a href="/web/sales/index.html" title="">Contact Sales</a>';
					output += '</td>'; 
				}
				output += '</tr>';
			}
		} else {
			output += '<tr><td colspan="2" align="center">No matching devices found.  Please revise your filter criteria and try again.</td></tr>';
		}
		output += '</table>';
		$(id).innerHTML = output;
	}
});

function update(id) {
	cpeDevices.filter(id);
	cpeDevices.render(id);
};
function showAll(id) {
	$('frm'+id).reset();
	update(id);
	switch(id) {
	case 'devices':
		changeFilterOptions(); break;
	default:
	}
};
function changeFilterOptions() {
	if( $('type').value != '0' ) {
		$('routerOptions').style.display = '';
	} else {
		$('routerOptions').style.display = 'none';
	}
};
