<!--

// **************************************************************************
// Functions used to read XML file and load page elements for AHC sales list.
// **************************************************************************

var xmlDoc;
if (window.ActiveXObject) {	// code for IE
	xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
} else if (document.implementation.createDocument) {	// code for Mozilla, Firefox, Opera, etc.
	xmlDoc=document.implementation.createDocument("","",null);
} else {
	alert('Your browser cannot handle this script');
}
xmlDoc.async=false;
xmlDoc.load("ahc_data/SalesList.xml");

var x = xmlDoc.getElementsByTagName("SalesList");
 
window.onload=init; 

function init() {  
	getSalesContent();
}   

function getSalesContent() {
	if (!xmlDoc) {
		//loadIndex();
		return "";
	}
	var output = "";
	var outputMaresFillies = "";
	var outputStallionsColts = "";
	var outputGeldings = "";
	var outputUnknown = "";
	// Clear page elements
	document.getElementById("salescontent").innerHTML = "";
	
	for (var i=0; i < x.length; i++) {
		// Initialize variables
		var name = "";
		var nameNoSpaces = ""; 
		var sireDam = "";
		var foaled = "";
		var color = "";
		var gender = "";
		var price = "";
		var description = "";
		var imgPath = "";
		var imgWidth = "";
		var imgHeight = "";
		output = "";
		
		// get the current XML values
		if( x[i].getElementsByTagName("Name")[0].hasChildNodes() ) 	// check for empty XML tag to prevent JS error
			name = x[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue;
		if( x[i].getElementsByTagName("SireDam")[0].hasChildNodes() )
			sireDam = x[i].getElementsByTagName("SireDam")[0].childNodes[0].nodeValue;
		if( x[i].getElementsByTagName("Foaled")[0].hasChildNodes() )
			foaled = x[i].getElementsByTagName("Foaled")[0].childNodes[0].nodeValue;
		if( x[i].getElementsByTagName("Color")[0].hasChildNodes() )
			color = x[i].getElementsByTagName("Color")[0].childNodes[0].nodeValue;
		if( x[i].getElementsByTagName("Gender")[0].hasChildNodes() )
			gender = x[i].getElementsByTagName("Gender")[0].childNodes[0].nodeValue;
		if( x[i].getElementsByTagName("Price")[0].hasChildNodes() )
			price = x[i].getElementsByTagName("Price")[0].childNodes[0].nodeValue;
		if( x[i].getElementsByTagName("Description")[0].hasChildNodes() )
			description = x[i].getElementsByTagName("Description")[0].childNodes[0].nodeValue;
		if( x[i].getElementsByTagName("ImagePath")[0].hasChildNodes() )
			imgPath = x[i].getElementsByTagName("ImagePath")[0].childNodes[0].nodeValue;
		if( x[i].getElementsByTagName("ImageWidth")[0].hasChildNodes() )
			imgWidth = x[i].getElementsByTagName("ImageWidth")[0].childNodes[0].nodeValue;
		if( x[i].getElementsByTagName("ImageHeight")[0].hasChildNodes() )
			imgHeight = x[i].getElementsByTagName("ImageHeight")[0].childNodes[0].nodeValue;			

		// replace spaces in name with underscore
		if (name.length > 0)
			nameNoSpaces = name.replace(/ /g, "_");	// parameter 'g' replaces all occurrences
			
		// set image default path, width, and height if no value specified
		if (!imgPath.length > 0)
			imgPath = "images/horses/thumbs/" + nameNoSpaces + ".jpg";
		if (!imgWidth.length > 0)
			imgWidth = "108";
		if (!imgHeight.length > 0)
			imgHeight = "90";
			
		// Build the HTML output
		output += '<div class="saleslist_row">\n';
		output += '<h3><a href="horse_list/' + nameNoSpaces + '.html" title="View pedigree">' + name + '</a>\n';
		output += ' <span style="font-weight: normal">&nbsp; (' + sireDam + ')</span> </h3>\n';
		output += '<a href="horse_list/' + nameNoSpaces + '.html">';
		output += '<img class="floatleft" src="' + imgPath + '" width="' + imgWidth + '" height="' + imgHeight + '" alt="Click to view ' + name + '" title="Click to view ' + name + '" /></a>\n';
		output += '<p>' + foaled + ' &nbsp; &nbsp; ' + color + ' ' + gender + '  &nbsp; &nbsp; <span class="price">' + formatCurrency(price) + '</span> <br /><br />\n';
		output += description + '\n';
		output += '</p>\n';
		output += '</div>\n';
		// Add the current horse to the related page section
		if (gender == "Mare" || gender == "Filly") { outputMaresFillies += output; } 
		else if (gender == "Stallion" || gender == "Colt") { outputStallionsColts += output; } 
		else if (gender == "Gelding") { outputGeldings += output; } 
		else { outputUnknown += output; }
	
	}
	// Build the final output with page section headers
	output = "";
	output += '<h2>Mares &amp; Fillies</h2>\n';
	output += outputMaresFillies;
	if (!outputMaresFillies.length > 0) {
		output += '<p>For information on mares and fillies available for sale, please <a href="contact.html" title="Contact information for the W. K. Kellogg Arabian Horse Center">contact</a> the horse center. </p>\n';
	}
	output += '<a name="stallions"></a>\n';
	output += '<h2>Stallions &amp; Colts</h2>\n';
	output += outputStallionsColts;
	if (!outputStallionsColts.length > 0) {
		output += '<p>For information on stallions and colts available for sale, please <a href="contact.html" title="Contact information for the W. K. Kellogg Arabian Horse Center">contact</a> the horse center. </p>\n';
	}
	output += '<a name="geldings"></a>\n';
	output += '<h2>Geldings</h2>\n';
	output += outputGeldings;
	if (!outputGeldings.length > 0) {
		output += '<p>For information on geldings available for sale, please <a href="contact.html" title="Contact information for the W. K. Kellogg Arabian Horse Center">contact</a> the horse center. </p>\n';
	}
	if (outputUnknown.length > 0) {	// Catches empty or unrecognized gender value
		output += '<h2>Unknown</h2>\n';
		output += outputUnknown;
	}

	document.getElementById("salescontent").innerHTML = output;
}

function formatCurrency(strValue) {
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);
	if(isNaN(dblValue)) { return strValue; }	// Return original value if blank or not a number
	
	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	//return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);		// show cents
	return (((blnSign)?'':'-') + '$' + dblValue);
}


//-->