/* Create, then append, a new TextNode object. */

function newTextElement(elemID,arrayname,num,prefix) {
arrayname = eval(arrayname);
var newelement = document.getElementById(elemID); 
var newtext = document.createTextNode(prefix + arrayname[num]);
newelement.appendChild(newtext);
}



/* Create the dynamic content for front page. */ 

function showcase(num) {

/*
newTextElement('featuredesc','productdesc',num,"");
*/


/*
newTextElement('featuremore','moredesc',num,"");
*/



/* Create the new IMG object. */ 

var PicObj = document.createElement("IMG");
PicObj.id = "picture";
PicObj.width =largeimgWdth[num];
PicObj.height =largeimgHt[num];
PicObj.src = largeimg[num];

document.getElementById('productpic').appendChild(PicObj);

}


/* obtain a random positive number between lownum and highnum */ 

function randomize(lownum, highnum) {
return Math.floor(Math.abs(Math.random() * (highnum - lownum + 1) - 0.0000000001)) + lownum;
}


/* Display a random product on the first page. */ 
function randomshowcase() {
showcase (randomize (0, largeimg.length-1));
}




/* Create the dynamic content for the second (list) page. */ 



function createProductDesc(num) {

var headnum = "head" + num;
var picsrcnum = "img" + num;
var descnum = "p" + num;
var pricenum = "pr" + num;


newTextElement(headnum,productname,num,"");

newTextElement(descnum,productdesc,num,"");

newTextElement(pricenum,origprice,num,"Price: ");

if (saleprice[num]) { 

newTextElement(pricenum,saleprice,num,"......On Sale for ");

 }

document.getElementById(picsrcnum).src = smallimg[num].src;

}




/* display the product listings */ 

function ProductList(arraystart, arrayend) {
for (i=arraystart; i <= arrayend; i++) {
  createProductDesc(i);
 }
}



/* show detailed product description  */ 

function showdetail(num) {
var detailelem = "detail" + num;
var lastNodeValue = document.getElementById(detailelem).lastChild.nodeValue;
if (lastNodeValue == null || lastNodeValue.charAt(0) == "" || lastNodeValue.charAt(0) == "\n" || lastNodeValue.length < 3) {
newTextElement(detailelem,moredesc,num,"::: ");
 }
}


/* 
Note: the above code for checking the value of the text node to see if it's "empty" works for this particular function. If you want to do a more complete check of the text string, you can use Regular Expressions. Use the following code:

function showdetail(num) {

var detailelem = "detail" + num;

var lastNodeValue = document.getElementById(detailelem).lastChild.nodeValue;

if (lastNodeValue != null) {
lastNodeValue = lastNodeValue.replace(/\s+/g, "");
  }
  if (lastNodeValue == null || lastNodeValue == "") {

newTextElement(detailelem,moredesc,num,"::: ");
 }
}

*/ 

