var browser = navigator.appName;
var dataNode = new Array;

function imgLoader(tmpImages){
	imgPreload = new Image();
	
	for(var a = 0; a < tmpImages.length; a++){
	    imgPreload.src = tmpImages[a];
		
		/*if(imgPreload.complete == true){
		    alert("Img #" + a + " has finished loading!");
		}*/
	}
}

function nav_chng(tmpNav){
    var srcNav = tmpNav.src;
    if(srcNav != null){
        if(srcNav.search("_Over") != -1){
            tmpNav.src = srcNav.replace(/_Over/, "_Up");
        }else if(srcNav.search("_Up") != -1){
            tmpNav.src = srcNav.replace(/_Up/, "_Over");
        }
    }
}

//tmpURL = URL of XML document to load
//tmpNodes = String array of nodes in XML document
//tmpTable = Table where XML information will load
//rwItems = # of items to display per row
//tmpList = Tags & index values for the items in tmpNodes to show in the created cells
//          The order of the index values in tmpList is the order the items are shown in the cells
function loadXML(tmpURL, tmpNodes, tmpTable, rwItems, tmpList){
	var xml_request = null;
	var xml_feed = tmpURL;

	if(window.ActiveXObject){
	    xml_request = new ActiveXObject("Microsoft.XMLHTTP");
	}else if(window.XMLHttpRequest){
  	    xml_request = new XMLHttpRequest();
	    if(browser != "Microsoft Internet Explorer"){
		    xml_request.overrideMimeType("text/xml");
	    }
	}
	
	xml_request.open("GET", xml_feed, false);
	xml_request.setRequestHeader("Cache-Control", "no-cache");
	xml_request.send(null);

	xml_text = xml_request.responseXML;
	xmlNodes(xml_text, tmpNodes, tmpTable, rwItems, tmpList);
}

function xmlNodes(xml_text, tmpNodes, tmpTable, rwItems, tmpList){
    if(tmpNodes.length >= 2){
        //alert(tmpNodes[0]);
	    var mainNode = findChild(xml_text, tmpNodes[0]);
	    //alert(mainNode);
    	
    	if(mainNode != null){
	        var counter = 0;
    	    
  	        for(var parent = mainNode.firstChild; parent != null; parent = parent.nextSibling){
  	            //alert(parent.nodeName);
   		        if(parent.nodeName == tmpNodes[1])
   		        {
   		            dataNode[counter] = new Array;
   		            for(var a = 2; a < tmpNodes.length; a++){
    			        dataNode[counter][a - 2] = findChild(parent, tmpNodes[a]);
    			        dataNode[counter][a - 2] = chkNull(dataNode[counter][a - 2].firstChild);		
				    }
				    counter++;
		        }else if(parent.nodeName != "#text"){
		            alert("XML document is missing the parent node at position #" + counter + "!");
		        }
  	        }
  	    }else{
  	        alert("mainNode is null!");
  	    }
    	
	    /*
	    for(var b = 0; b < counter; b++)
	    {
		    alert(dataNode[b][0]);
	    }*/
	    
	}else{
	    alert("XML document must contain at least 2 nodes!");
	}
	
	crtTables(tmpTable, rwItems, counter, tmpList);
}

function findChild(element, nodeName){
 	for(var child = element.firstChild; child != null; child = child.nextSibling){
			//alert(child);
			if(child.nodeName == nodeName){
   				return child;
   			}
 	}
 	
	return null;
}

function chkNull(tmpChild){
    if(tmpChild == null){
        return "";
    }else{
        return tmpChild.data;
    }
}

var mytable;
var mytable2;
var mytablebody;
var mytablebody2;
var mycurrent_row;
var mycurrent_row2;	
var mycurrent_cell;
var mycurrent_cell2;
var currenttext;
var currenttag = new Array;
var tblCount;
var width;

function crtTables(tmpTable, rwItems, counter, tmpList){
	tblCount = Math.round(counter / rwItems);
	if((counter % rwItems) / rwItems < .50){
		tblCount = tblCount + 1;
	}
	
	//alert("tblCount = " + tblCount);	
	var aryCounter = 0;
	
	// references a table by ID
    mytable = document.getElementById(tmpTable);
	if(browser == 'Microsoft Internet Explorer'){
			mytablebody = document.createElement("TBODY");
			mycurrent_row = document.createElement("TR");
			mycurrent_cell = document.createElement("TD");
		    for(t = 0; t < tblCount; t++){
		        mytable2 = document.createElement("TABLE");
		        mytablebody2 = document.createElement("TBODY");
    	        // creating all cells	
        	    for(r = 0; r < 1; r++){
					aryCounter = crtCells(rwItems, tmpList, counter, aryCounter);
				    // appends the row TR into mytable
			        mytablebody2.appendChild(mycurrent_row2);
	            }
			    mytable2.appendChild(mytablebody2);
			    mytable2.setAttribute("width","100%");
			    mycurrent_cell.appendChild(mytable2);
			    mycurrent_row.appendChild(mycurrent_cell);
			    mytablebody.appendChild(mycurrent_row);
			    mytable.appendChild(mytablebody);
		    }
		}else{
    	    mycurrent_row = document.createElement("TR");
			mycurrent_cell = document.createElement("TD");
		    for(t = 0; t < tblCount; t++){
		        mytable2 = document.createElement("TABLE");
        	    // creating all cells
	            for(r = 0; r < 1; r++) {
            	    aryCounter = crtCells(rwItems, tmpList, counter, aryCounter);
				    // appends the row TR into mytable2
				    mytable2.appendChild(mycurrent_row2);
				    mytable2.setAttribute("width","100%");
	            }
	            mycurrent_cell.appendChild(mytable2);
	            mycurrent_row.appendChild(mycurrent_cell);
	            mytable.appendChild(mycurrent_row);
	        }
		}

    // set attributes
    mytable.setAttribute("border","0");
    mytable.setAttribute("width","100%");
}

function crtCells(rwItems, tmpList, counter, aryCounter){	
	// creates an element whose tag name is TR
    mycurrent_row2 = document.createElement("TR");
	if(t < tblCount - 1){
		width = rwItems;
	}else{
		width = rwItems - ((tblCount * rwItems) - counter);
	}
	//alert("width = " + width);
	for(c = 0; c < width; c++){
		// creates an element whose tag name is TD
		mycurrent_cell2 = document.createElement("TD");
		for(ct = 0; ct < tmpList.length; ct++){
			crtTags(tmpList[ct], aryCounter, ct);
			mycurrent_cell2.appendChild(currenttag[ct]);
			mycurrent_cell2.setAttribute("align", "center");
		}
		aryCounter = aryCounter + 1;
		// appends the Text Node we created into the cell TD
		// appends the cell TD into the row TR
		mycurrent_row2.appendChild(mycurrent_cell2);
	}
	
	return aryCounter;
}

function crtTags(dataTag, aryCounter, ct){
	var spltTag = dataTag.split(";");
	var tmpNode = "";
	
	if(spltTag[0] == "href_func"){
		//Creates a Link Node
		currenttag[ct] = document.createElement("A");
		currenttag[ct].setAttribute("href", "javascript:" + spltTag[2] + "('" + (aryCounter + 1) + "', '" + spltTag[3] + "');");
		if(spltTag.length >= 5){
		    currenttag[ct].setAttribute("id", spltTag[4]);
		}
		//Creates a Text Node
		currenttext = document.createTextNode(dataNode[aryCounter][parseInt(spltTag[1]) - 2]);
		currenttag[ct].appendChild(currenttext);
	}else if(spltTag[0] == "img_func"){
		//Creates a Link Node
		currenttag[ct] = document.createElement("IMG");
		currenttag[ct].setAttribute("src", spltTag[4] + dataNode[aryCounter][parseInt(spltTag[1]) - 2]);
		if(spltTag[2] != "null"){
		    if(spltTag[3] != "null"){
		        currenttag[ct].setAttribute("onclick", "javascript:" + spltTag[2] + "('" + (aryCounter + 1) + "', '" + spltTag[3] + "');");
		    }else{
		        currenttag[ct].setAttribute("onclick", "javascript:" + spltTag[2] + "('" + (aryCounter + 1) + "');");
		    }
		}
		if(spltTag.length >= 6){
		    currenttag[ct].setAttribute("id", spltTag[5]);
		}
	}else if(spltTag[0] == "sold_func"){
	    currenttag[ct] = document.createElement("IMG");
	    currenttag[ct].setAttribute("src", spltTag[2]);
	    if(dataNode[aryCounter][parseInt(spltTag[1]) - 2] == "Y"){
	        currenttag[ct].setAttribute("id", spltTag[4]);
	    }else if(dataNode[aryCounter][parseInt(spltTag[1]) - 2] == "N"){
		    currenttag[ct].setAttribute("id", spltTag[3]);
		}
	}else if(spltTag[0] == "title_func"){
	    currenttag[ct] = document.createElement("DIV");
	    if(spltTag.length >= 3){
	        currenttag[ct].setAttribute("class", spltTag[2]);
	    }
        currenttext = document.createTextNode(dataNode[aryCounter][parseInt(spltTag[1]) - 2]);
        currenttag[ct].appendChild(currenttext);
	}else if(spltTag[0] == "text_func"){
	    currenttag[ct] = document.createElement("DIV");
	    if(spltTag.length >= 3){
	        currenttag[ct].setAttribute("class", spltTag[2]);
	    }
	    currenttag[ct].innerHTML = frmtText(dataNode[aryCounter][parseInt(spltTag[1]) - 2]);
	}else if(spltTag[0] == "note_func"){
	    currenttag[ct] = document.createElement("DIV");
	    if(spltTag.length >= 3){
	        currenttag[ct].setAttribute("class", spltTag[2]);
	    }
	    currenttag[ct].innerHTML = frmtNotes2(dataNode[aryCounter][parseInt(spltTag[1]) - 2], "*");
	}else if(spltTag[0] == "note_img_link_func"){
	    currenttag[ct] = document.createElement("DIV");
	    if(spltTag.length >= 3){
	        currenttag[ct].setAttribute("class", spltTag[2]);
	    }
	    currenttag[ct].innerHTML = frmtImgLinkNotes(dataNode[aryCounter][parseInt(spltTag[1]) - 2], dataNode[aryCounter][parseInt(spltTag[3]) - 2], dataNode[aryCounter][parseInt(spltTag[4]) - 2]);
	}else if(spltTag[0] == "bio_func"){
	    currenttag[ct] = document.createElement("DIV");
	    if(spltTag[2] != "null"){
	        currenttag[ct].setAttribute("class", spltTag[2]);
	    }
	    currenttag[ct].innerHTML = innerImg(dataNode[aryCounter][parseInt(spltTag[3]) - 2], spltTag[4], spltTag[5]);
	    currenttag[ct].innerHTML = currenttag[ct].innerHTML + frmtText(dataNode[aryCounter][parseInt(spltTag[1]) - 2]);
    }else if(spltTag[0] == "link_func"){
	    currenttag[ct] = document.createElement("A");
	    if(dataNode[aryCounter][parseInt(spltTag[1]) - 2] != "null"){
	        tmpNode = dataNode[aryCounter][parseInt(spltTag[1]) - 2].split(";");
	        
	        if(spltTag.length >= 3 && spltTag[2] == "br"){
	            currenttext = document.createElement("BR");
	            currenttag[ct].appendChild(currenttext);
	        }
	    
	        currenttag[ct].setAttribute("href", tmpNode[1]);
	        if(spltTag.length >= 4 ){
	            currenttag[ct].setAttribute("class", spltTag[3]);
	        }
	        //Creates a Text Node
		    currenttext = document.createTextNode(tmpNode[0]);
		    currenttag[ct].appendChild(currenttext);
		    
		    if(spltTag.length >= 3 && spltTag[2] == "br"){
	            currenttext = document.createElement("BR");
	            currenttag[ct].appendChild(currenttext);
	        }
	    }
	}else if(spltTag[0] == "br"){
		currenttag[ct] = document.createElement("BR");
	}
}

var myrow;
var mycell;
var new_cell;
var fading;

function shwData(tmpID, tmpTable, tmpOpac){
    //alert("tmpID = " + tmpID + "\ntmpTable = " + tmpTable);
    tmpID = tmpID - 1;
    mytable = document.getElementById(tmpTable);
    shwOpac();
    setTable(mytable);
    mytable.style.visibility = "visible";

    // The cell value where the next text will be placed
	myrow = mytable.getElementsByTagName("tr").item(0);
   	mycell = myrow.getElementsByTagName("td").item(0);
   	new_cell = document.createElement("TD");

	//Calls the display function and returns the text that will be placed in the cell from above
   	new_cell.innerHTML = innerHTML(tmpID, dataNode, tmpTable);

	// Controls the color of the text to give it a fading appearance
    fading = 255;
    new_cell.style.color = "rgb("+fading+","+fading+","+fading+")";
    new_cell.setAttribute("width", "100%");
    new_cell.setAttribute("height", "100%");
    new_cell.setAttribute("valign", "top");
    myrow.replaceChild(new_cell, mycell);
    setTimeout("fadeitem()", 20);    
}

function hidData(tmpTable){
    mytable = document.getElementById(tmpTable);
    mytable.style.visibility = "hidden";
    hidOpac();
}

function fadeitem(){
    if (fading > 0){
        myrow = mytable.getElementsByTagName("tr").item(0);
        mycell = myrow.getElementsByTagName("td").item(0);           
                
        mycell.style.color = "rgb("+fading+","+fading+","+fading+")";

        //alert(mycell.style.color);
        fading = fading - 10;
        setTimeout("fadeitem()", 50);
    }
}

var windowHeight;
var windowWidth;

function getWindowHeight(){
    windowHeight = 0;    

	if(typeof(window.innerHeight) == 'number'){
	    windowHeight = window.innerHeight;
	}else if(document.documentElement && document.documentElement.clientHeight){
        windowHeight = document.documentElement.clientHeight;
    }else if(document.body && document.body.clientHeight){
		windowHeight = document.body.clientHeight;
	}

    return windowHeight;
}

function getWindowWidth(){
    windowWidth = 0;    

	if(typeof(window.innerWidth) == 'number'){
	    windowWidth = window.innerWidth;
	}else if(document.documentElement && document.documentElement.clientWidth){
        windowWidth = document.documentElement.clientWidth;
    }else if(document.body && document.body.clientWidth){
		windowWidth = document.body.clientWidth;
	}

    return windowWidth;
}

function setTable(tmpTable){
    var tmpHeight = 0;
    var tmpWidth = 0;
    
    if(document.getElementById){
	    windowHeight = getWindowHeight();
	    windowWidth = getWindowWidth();
		if(windowHeight > 0){
            var tblHeight = tmpTable.offsetHeight;
            tmpHeight = ((windowHeight / 2) - (tblHeight / 2));
            if(typeof(document.documentElement.scrollTop) != "undefined"){
                tmpHeight = tmpHeight + document.documentElement.scrollTop;
            }else if(typeof(document.body.scrollTop) != "undefined"){
                tmpHeight = tmpHeight + document.body.scrollTop;
            }else if(typeof(window.pageYOffset) != "undefined"){
                tmpHeight = tmpHeight + window.pageYOffset;
            }
			tmpTable.style.top = tmpHeight + "px";
		}
		if(windowWidth > 0){
            var tblWidth = tmpTable.offsetWidth;
            tmpWidth = ((windowWidth / 2) - (tblWidth / 2)) + "px";
			tmpTable.style.left = tmpWidth;
		}
	}
}

function shwImg(prntID, imgID, tmpDir, tmpImg, addLeft, addTop){
    var prntElement = document.getElementById(prntID); 
    var imgElement = document.getElementById(imgID);
    
    imgElement.src = tmpDir + tmpImg;
	//alert(imgElement.src);
	imgElement.style.left = prntElement.offsetLeft + parseInt(addLeft) + "px";
    imgElement.style.top = prntElement.offsetTop + parseInt(addTop) + "px";
	imgElement.style.visibility = "visible";	
}

function hidImg(tmpID){
    var imgElement = document.getElementById(tmpID);
    imgElement.src = "";
    imgElement.style.visibility = "hidden";
}

function frmtNotes(tmpNotes, tmpWidth, tmpSym){
    var tmpNotes2 = "";
	tmpNotes = tmpNotes.split(";");
	//alert(tmpNotes.length);
	
   	for(var n = 0; n < (tmpNotes.length - 1); n++){
   	    if(tmpNotes.length > 1){
   	        tmpNotes2 = tmpNotes2 + "<tr><td width='" + tmpWidth + "px' valign='top' align='right'>"
   	            + tmpSym + "</td><td align='left'>" + tmpNotes[n] + "</td></tr>";
   	    }   	    
   	}   	
   	return tmpNotes2;
}

function frmtNotes2(tmpNotes, tmpSym){
    var tmpNotes2 = "";
	tmpNotes = tmpNotes.split(";");
	//alert(tmpNotes.length);
	
	if(tmpNotes != ""){
   		for(var n = 0; n < tmpNotes.length; n++){		
   		        tmpNotes2 = tmpNotes2 + tmpSym + " " + tmpNotes[n] + "<br/>";	    
	   	}
	}
	
   	return tmpNotes2;
}

function frmtImgLinkNotes(tmpNotes, tmpImg, tmpLink){
    var tmpNotes2 = "";
	var tmpImg2 = "";
	var tmpLink2 = "";
	tmpNotes = tmpNotes.split(";");
	tmpImg = tmpImg.split(";");
	tmpLink = tmpLink.split(";");
	//alert(tmpNotes.length);
	
	if(tmpNotes != ""){
   		for(var n = 0; n < tmpNotes.length; n++){
				if(tmpImg[n] != "null"){
					if(tmpLink[n] != "null"){
						tmpLink2 = "window.location='" + tmpLink[n] + "';";
						tmpImg2 = "<img src='" + tmpImg[n] + "' onclick=" + tmpLink2 + " /><br/>";
					}else{
						tmpImg2 = "<img src='" + tmpImg[n] + "' /><br/>";
					}
				}
				if(tmpLink[n] != "null"){
					tmpNotes2 = tmpNotes2 + tmpImg2 + "<a href='" + tmpLink[n] + "'>" + tmpNotes[n] + "</a><br/>";	    
				}else{
   		        	tmpNotes2 = tmpNotes2 + tmpImg2 + tmpNotes[n] + "<br/>";	    
				}
				
				tmpImg2 = "";
	   	}
	}
	
   	return tmpNotes2;
}

function frmtText(tmpText){
    var tmpText2 = "";
    tmpText = tmpText.split(";");    
    //alert(tmpText.length);
    
    for(var t = 0; t < tmpText.length; t++){
        if(t < tmpText.length - 1){
   	        tmpText2 = tmpText2 + tmpText[t] + "<br/><br/>";   
   	    }else{
   	        if(tmpText[t].indexOf('*') > -1){
   	            tmpText2 = tmpText2 + "<span style='font-size:9px;'>" + tmpText[t]; + "</span>";
   	        }else{
   	            tmpText2 = tmpText2 + tmpText[t];
   	        }
   	    }
   	}  
   	
   	//alert(tmpText2);
   	return tmpText2;
}

function innerImg(tmpImg, tmpLoc, tmpPos){
    var innerImg2 = "";
    
    innerImg2 = innerImg2 + "<img id='img" + tmpPos + "' src='" + tmpLoc + tmpImg + "' />";
    return innerImg2;
}

function shwContact(tmpID, tmpOpac){
	var contElement = document.getElementById(tmpID);
	shwOpac();
	setTable(contElement);
	contElement.style.visibility = "visible";
}

function hidContact(tmpID){
	var contactUs = document.getElementById(tmpID);
	contactUs.style.visibility = "hidden";
	hidOpac();
}

function shwThankYou(tmpID){
	var chkSubmit = location.href
	
	if(chkSubmit.indexOf('?') > 0){
		chkSubmit = chkSubmit.substring(chkSubmit.indexOf('?') + 1);
		chkSubmit = chkSubmit.split("=");

		if(chkSubmit[1] = 1){
				var tyElement = document.getElementById(tmpID);
				shwOpac();
				setTable(tyElement);
				tyElement.style.visibility = "visible";
		}
	}
}

function hidThankYou(tmpID){
	var tyElement = document.getElementById(tmpID);
	tyElement.style.visibility = "hidden";
	hidOpac();
}

function findPos(tmpObj, tmpOff){
	var offLeft = 0;
	var offTop = 0;
	
	if(tmpObj.offsetParent){
		do{
			offLeft = offLeft + tmpObj.offsetLeft;
			offTop = offTop + tmpObj.offsetTop;
		}while(tmpObj = tmpObj.offsetParent);
	}
	
	if(tmpOff == "Left"){
	    return offLeft;
	}else if(tmpOff == "Top"){
	    return offTop;
	}
}

function flshElement(tmpID, tmpSrc, tmpWidth, tmpHeight){
	document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' name='" + tmpID + "' id='" + tmpID + "' hspace='0' vspace='0' width='" + tmpWidth + "' height='" + tmpHeight + "'>");
	document.write("<param name='movie' value='" + tmpSrc + "' />");
	document.write("<param name='quality' value='high' />");
	document.write("<param name='SCALE' value='exactfit' />");
	document.write("<param name='wmode' value='transparent'>");
	document.write("<embed src='" + tmpSrc + "' wmode='transparent' width='" + tmpWidth + "' height='" + tmpHeight + "' hspace='0' vspace='0' quality='high' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' name='" + tmpID + "' id='" + tmpID + "' scale='exactfit'></embed>");
	document.write("</object>");
}

function spacerHeight(tmpPos, tmpWidth){
    var tmpHeight = getWindowHeight();
    
    if(tmpPos == "bottom"){
        tmpHeight = tmpHeight - 75;
        document.write("<img src='Images/Spacer.gif' width='" + tmpWidth + "px' height='" + tmpHeight + "px' />");
    }
}

function shwOpac(){
    var divElement = document.getElementById("opacTable");
    
    divElement.style.opacity = 50/100;
    divElement.style.MozOpacity = 50/101;
	divElement.style.filter = 'alpha(opacity=' + 50 + ')';
}

function hidOpac(){
    var divElement = document.getElementById("opacTable");
    
    divElement.style.opacity = 100/100;
    divElement.style.MozOpacity = 100/101;
	divElement.style.filter = 'alpha(opacity=' + 100 + ')';
}

//window.onresize = function(){
    //setTable(document.getElementById(""));
//}

//window.onscroll = function(){
	//setTable(document.getElementById(""));
//}
