//function to trim whitespace characters
function trim(str) {
        return str.replace(/^\s+|\s+$/g,"");
}

    
// Function to get the contents of the site from text files uploaded by Ikey

function getFileContents(file) {
		
	if (window.XMLHttpRequest) {
		// code for all new browsers
		oRequest = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		// code for IE5 and IE6
		oRequest=new ActiveXObject("Microsoft.XMLHTTP");
	}

	var localdir = "";

//set path for local access
	if (location.href.match(/josh/g)){ 			
		localdir = "/~josh/RobertsonDesign/2010-05-21";
	}

	var sURL  = "http://" + self.location.hostname + localdir +"/content/text/" + file;

	oRequest.open("GET",sURL,false);
	oRequest.setRequestHeader("User-Agent",navigator.userAgent);
	oRequest.setRequestHeader("Content-Type","text/plain");
	oRequest.send(null);

	return trim(oRequest.responseText);

}










/*	

// Function to split each row from the section string load it into an array
function parseImages(section){
	var imageNames = new Array();
	delim = "\n";
	imageNames = fileContents.split(delim); // Returns array elements[i] = "text here"
	return imageNames;
}
	*/
// Function to split each row from the section string load it into an array
function printCatalogue(siteSection,subSections){
	for (i=0;i<subSections.length; ++i) {
		var imageNames = new Array();
		delim = "\n";
		imageArray = subSections[i].split(delim); // Returns array elements[i] = "text here"
		printDivs(imageArray,siteSection);
	}


}



/*
// Function to split each row from the file contents and load it into an array
function parseTextfile(fileContents){
	var imageNames = new Array();
	delim = "\n";
	imageNames = fileContents.split(delim); // Returns array elements[i] = "text here"
	return imageNames;
}
*/

// Function to split each row from the section string load it into an array
function parseTextfile(fileContents){
	var rawdata = new Array();
	var subSections = new Array();
	var delim = "_";
	rawdata = fileContents.split(delim);
	var txt = "";
	for (i=0;i<rawdata.length; ++i) {
		if (rawdata[i]!=""){
			subSections.push(trim(rawdata[i]));
		}
	}
/* //for testing //
	for (i=0;i<subSections.length; ++i) {
		txt += "[  " + subSections[i] + "   ]\n";
	}
	alert(txt);

*/
	return subSections;
}


	

// function to print all the divs nedded to display stock
function printDivs(imageArray,siteSection){
	var txtout = "";
	var arrayLength = imageArray.length;
	// html should look like this: <div class='imageBox'><img src='content/pendants/086.jpg' alt='item #086' width='150' height='150' />303</div>
	if (arrayLength>1) {
		txtout +="<div class='catalogueSubSection'>";
		txtout +="<h1>";
		txtout +=imageArray[0]; // pass the description of the following items taken from the first line of the text file.
		txtout +="</h1>\n\n";
		txtout +="</div>";
		for (ii=1;ii<arrayLength; ++ii) {
			var item = imageArray[ii];
			if (item) {
				txtout +="<div class='imageBox'><img src='content/" + siteSection + "/" + item;
				txtout += ".jpg' alt='item #";
				txtout += item;
				txtout += "' width='150' height='150' /><br />";
				txtout += item.replace(/&/i," & ");
				txtout += "</div>\n\n";
/*			
			} else {
				txtout +="<div class='imageBox'>&nbsp;</div>\n\n";
*/		
			}		
		}


	}
	document.write(txtout);
}

