//version1


//address of receiver script
var receiver = 'test.asp';




//create XML function
function createXML()
{
	//matrix for storing the exracted data
	var data = [];

	//get div elements
	var divs = document.getElementsByTagName('div');

	//for each one
	var len = divs.length;
	for(var i=0; i<len; i++)
	{
		//if it's a main box
		if(divs[i].className.indexOf('dbx-box')!=-1)
		{
			//add a new member to the data matrix
			//an object literal this time, for ease of key-indexing
			//which is simpler than lots of object testing
			data[data.length] = {

				//title
				'title' : divs[i].getElementsByTagName('h3')[0].firstChild ? divs[i].getElementsByTagName('h3')[0].firstChild.nodeValue : '',
				//options
				'options' : divs[i].getElementsByTagName('textarea')[0].value,
				//image
				'image' : divs[i].getElementsByTagName('img')[0].src,
				//special price
				'specialprice' : divs[i].getElementsByTagName('input')[0].value,
				//stock number
				'stockno' : divs[i].getElementsByTagName('li')[0].firstChild ? divs[i].getElementsByTagName('li')[0].firstChild.nodeValue : '',
				//vin
				'vin' : divs[i].getElementsByTagName('li')[1].firstChild ? divs[i].getElementsByTagName('li')[1].firstChild.nodeValue : '',
				//was
				'was' : divs[i].getElementsByTagName('li')[2].getElementsByTagName('input')[0].value,
				//save
				'save' : divs[i].getElementsByTagName('li')[3].getElementsByTagName('span')[0].firstChild ? divs[i].getElementsByTagName('li')[3].getElementsByTagName('span')[0].firstChild.nodeValue : ''

				};
		}
	}


	//begin compiling serialized XML
	var xml = '<?xml version="1.0" encoding="UTF-8"?>' + '\r\n'
		+ '<root>' + '\r\n'
		+ '<list>' + '\r\n\r\n';

	//for each member of the data matrix
	len = data.length;
	for(i=0; i<len; i++)
	{
		//open unit element
		xml += '<unit>' + '\r\n';

		//for each item in this member
		for(var j in data[i])
		{
			//if it's our data (not a prototyped function or something)
			if(typeof data[i][j] == 'string')
			{
				//if this is the image
				if(j == 'image')
				{
					//add an element with its name and href attribute
					xml += '<' + j + ' href="' + data[i][j] + '"/>' + '\r\n';
				}
				//if it's anything else
				else
				{
					//add an element with its name and node data
					xml += '<' + j + '>' + data[i][j] + '</' + j + '>' + '\r\n';
				}
			}
		}

		//close unit element
		xml += '</unit>' + '\r\n\r\n';
	}

	//finish XML
	xml += '</list>' + '\r\n'
		+ '</root>' + '\r\n';




	//request object
	var request = null;

	//try to establish a request
	if(typeof window.ActiveXObject != 'undefined')
	{
		try { request = new ActiveXObject('Microsoft.XMLHTTP'); }
		catch(err) { request = null; }
	}
	else if(typeof window.XMLHttpRequest != 'undefined')
	{
		request = new XMLHttpRequest();
	}

	//if we're good
	if(request != null)
	{
		//bind readyState change handler
		request.onreadystatechange = function()
		{
			//if document has loaded and the status is okay or not-modified
			//(or there is no status number, which is opera viewing a page locally)
			if(request.readyState == 4 && /^(0|200|304)$/.test(request.status))
			{
				//open a window with the data
				var test = open();
				test.document.open();
				test.document.write('<xmp>' + request.responseText + '</xmp>');
				test.document.close();
			}
		};


		//make the request
		request.open('POST', receiver, true);

		//set content-type to form-post data
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		//send the data
		request.send('xml=' + encodeURI(xml));
	}




};


