/**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
*
**/

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}




var sCertifiedsDiv;
var sCertifiedsSection;
var oBox;
var oBoxTitle;
var oBoxText;
var oXHR = null;




function setCertifiedsDiv (sDivID)
{
    sCertifiedsDiv = sDivID;
}

function setCertifiedsSection (sSection)
{
    sCertifiedsSection = sSection;
}



function populateCertifieds () 
{
	var oXMLHttpRequest = new XMLHttpRequest; 
	oXMLHttpRequest.open("GET", "/certifieds/"+sCertifiedsSection+".xml", true); 
	oXMLHttpRequest.onreadystatechange = function() { 
		if (this.readyState == XMLHttpRequest.DONE) { parseCountryInfo(oXMLHttpRequest); } 
	} 
	oXMLHttpRequest.send(null); 
	
}


function popupBio (oEvent, sName, sInfoFile, sPicFile)
{
    oXMLHttpRequest = null;
	
    // If IE, else FF, etc.
    if (window.event) {
        cursorX = oEvent.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
        cursorY = oEvent.clientY + document.documentElement.scrollTop + document.body.scrollTop;
    } else {
        cursorX = oEvent.clientX + window.scrollX;
        cursorY = oEvent.clientY + window.scrollY;
    }
    
    
    var oXMLHttpRequest = new XMLHttpRequest; 
		oXMLHttpRequest.open("GET", "/certifieds/bios/"+sInfoFile+".txt", true); 
		oXMLHttpRequest.onreadystatechange = function() { 
		if (this.readyState == XMLHttpRequest.DONE) { showBox(sName, oXMLHttpRequest.responseText, sPicFile, cursorX, cursorY); } 
	} 
	oXMLHttpRequest.send(null); 
    
    // Prevent browser direction
    return false;
    
}



function showBox (sTitle, sData, sPicFile, iXPos, iYPos)
{
    if (oBox == null) {
        oBox = document.getElementById("bio-box");
        oBoxTitle = document.getElementById("bio-title-text");
        oBoxText = document.getElementById("bio-info");
		//oBoxPic = document.getElementById("bio-info-pic");
        
        oBox.style.zIndex = 2;
    }
    
    
    oBoxTitle.innerHTML = sTitle;
    oBoxText.innerHTML = "<img src=\"/certifieds/certImage.php?id=" + sPicFile +"&size=l\" align=\"left\" hspace=\"8\" />" + sData;
	//oBoxPic.src = "/certifieds/certImage.php?id=" + sPic +"&size=l";
	//oBoxPic.alight = "left";
 // oBox.style.left = iXPos +"px";
    oBox.style.top = iYPos +"px";
    oBox.style.visibility = "visible"
}



function hideBox()
{
    oBox.style.visibility = "hidden";
}



function parseCountryInfo (oXmlDom) {
    var oDivToWrite = document.getElementById(sCertifiedsDiv);
    var oRoot = oXmlDom.responseXML; //documentElement;
    var oFragment = document.createDocumentFragment();
    
    
    var aRegions = oRoot.getElementsByTagName("region");
    var aCountry = oRoot.getElementsByTagName("country");
    
    var sCountry = aCountry[0].getAttribute("name");
    aCountry = null;
    
    var divCountryInfo = document.createElement("div");
    var divCountryIcon = document.createElement("div");
    var divCountryRecords = document.createElement("div");
    var imgCountryIcon = document.createElement("img");
    
    divCountryInfo.className = "country-info";
    divCountryIcon.className = "country-icon";
    divCountryRecords.className = "country-records";
    imgCountryIcon.src = "/certifieds/"+sCertifiedsSection+".png";
    
    var sRegion, sFName, sLName, sEmail, sBio, sBioData, sAddress, sCity, sState, sZip, sPhone, sURL;
    var spanCountry, spanRegion, divRegion;
    
    var divRecord, imgRecPic, imgBreak, spanRecTxt, spanRecName, spanRecEmail, hrefRecEmail, spanRecBio, hrefBioLink, spanRecAddress, spanRecPhone, hrefRecURL, spanRecURL;
    
    
    /** Step through all regions and construct document **/
    for (var i = 0; i < aRegions.length; i++) {
        
        /** Initialize variables **/
        sRegion = aRegions[i].getAttribute("name");
        aRecords = aRegions[i].getElementsByTagName("record");
        
        /** Create region-specific elements **/
        spanCountry = document.createElement("span");
        spanCountry.className = "country-name";
        spanCountry.appendChild(document.createTextNode(sCountry));
        
        divRegion = document.createElement("div");
        divRegion.className = "region-info";
        
        spanRegion = document.createElement("span");
        spanRegion.className = "region-name";
        spanRegion.appendChild(document.createTextNode(sRegion));
        
        divCountryRecords.appendChild(spanCountry);
        divCountryRecords.appendChild(document.createElement("br"));
        divCountryRecords.appendChild(spanRegion);
        
        
		
        /** Populate all info from current record **/
        for (var iRecNumber = 0; iRecNumber < aRecords.length; iRecNumber++) {
            
            sPic = "default";
			
            var oCurrentChild = aRecords[iRecNumber].firstChild;

            do {
                switch (oCurrentChild.tagName) {
                    case "first-name":
                        sFName = oCurrentChild.text;
                    break;
                    case "last-name":
                        sLName = oCurrentChild.text;
                    break;
                    case "e-mail":
                        sEmail = oCurrentChild.text;
                    break;
                    case "bio":
                        sBio = oCurrentChild.text;
                    break;
                    case "bio-data":
                        sBioData = oCurrentChild.text;
                    break;
                    case "address":
                        sAddress = oCurrentChild.text;
                    break;
                    case "city":
                        sCity = oCurrentChild.text;
                    break;
                    case "state":
                        sState = oCurrentChild.text;
                    break;
                    case "zip":
                        sZip = oCurrentChild.text;
                    break;
                    case "phone":
                        sPhone = oCurrentChild.text;
                    break;
                    case "url":
                        sURL = oCurrentChild.text;
                    break;
					case "pic":
                        sPic = oCurrentChild.text;
                    break;
                    
                    default:
                    break;
                }
            } while (oCurrentChild = oCurrentChild.nextSibling);
            
            
			sPic = trim(sPic);
			
            /** Create elements for record **/
            divRecord = document.createElement("div");
            divRecord.className = "record-info";
			
			
			
			// New (2009-10-15)
			imgRecPic = document.createElement("img");
            imgRecPic.src = "/certifieds/certImage.php?id=" + sPic +"&size=s";
			imgRecPic.align = "left";
			imgRecPic.hspace = 5;
			
			imgBreak = document.createElement("br");
			imgBreak.clear = "all";
			// End of new
			
			
			spanRecTxt = document.createElement("span");
            spanRecTxt.className = "record-txt";
            
            spanRecName = document.createElement("span");
            spanRecName.className = "record-name";
            spanRecName.appendChild(document.createTextNode(sFName +" "+ sLName +"\xa0\xa0"));
            
            hrefRecEmail = document.createElement("a");
            hrefRecEmail.href = "mailto:"+ sEmail;
            hrefRecEmail.appendChild(document.createTextNode(sEmail));
            
            spanRecEmail = document.createElement("span");
            spanRecEmail.className = "record-email";
            spanRecEmail.appendChild(hrefRecEmail);
            
            
            spanRecPhone = document.createElement("span");
            spanRecPhone.className = "record-phone";
            spanRecPhone.appendChild(document.createTextNode(sPhone));
            
            hrefRecURL = document.createElement("a");
            hrefRecURL.href = "http://"+ sURL;
			hrefRecURL.target = "new";
            hrefRecURL.appendChild(document.createTextNode(sURL));
            
            spanRecURL = document.createElement("span");
            spanRecURL.className = "record-url";
            spanRecURL.appendChild(hrefRecURL);
            
            
            spanRecBio = document.createElement("span");
            spanRecBio.className = "record-bio";
			
            /** Only attach link if bio is needed **/
            if (sBio == "Yes") {
                hrefBioLink = document.createElement("a");
                hrefBioLink.href = "#";
				//oTempFn = function () { return popupBio(event, trim(sFName) +" "+ trim(sLName), trim(sBioData)); return false; };
                
				if (hrefBioLink.addEventListener) {
					//hrefBioLink.addEventListener("onclick", oTempFn, false);
					hrefBioLink.setAttribute("onclick","return popupBio(event, '"+ trim(sFName) +" "+ trim(sLName) +"', '"+ trim(sBioData) +"', '"+ sPic +"');");
				} else if (hrefBioLink.attachEvent) {
					eval("oTempFn = function () { return popupBio(event, '"+ trim(sFName) +" "+ trim(sLName) +"', '"+ trim(sBioData) +"', '"+ sPic +"');};");
					hrefBioLink.attachEvent("onclick", oTempFn);
				}
                hrefBioLink.appendChild(document.createTextNode("view bio"));
                
                spanRecBio.appendChild(document.createTextNode("\xa0\xa0("));
                spanRecBio.appendChild(hrefBioLink);
                spanRecBio.appendChild(document.createTextNode(")"));
            }
            if (sBio == "No") {    
                spanRecBio.appendChild(document.createTextNode("\xa0\xa0(no bio available)"));
            }
            
            spanRecAddress = document.createElement("span");
            spanRecAddress.className = "record-address";
            spanRecAddress.appendChild(document.createTextNode(sAddress +", "+ sCity +" "+ sState +" "+ sZip));
            
            
            /** Append elements to container **/
			
			// OLD CODE
			divRecord.appendChild(imgRecPic);
			
			divRecord.appendChild(spanRecName);
            divRecord.appendChild(spanRecEmail);
            divRecord.appendChild(spanRecBio);
            divRecord.appendChild(document.createElement("br"));
            divRecord.appendChild(spanRecAddress);
			
			
			
            if (sPhone != "" || sURL != "") {
                divRecord.appendChild(document.createElement("br"));
            }
            
            divRecord.appendChild(spanRecPhone);
			/** Only echo pipe symbol if there is a phone and URL **/
			if (sPhone != "" && sURL != "") {
				divRecord.appendChild(document.createTextNode(" | "));
			}
            divRecord.appendChild(spanRecURL);
			divRecord.appendChild(imgBreak);
            
            divRegion.appendChild(divRecord);
            
            
        } /** End of Record loop **/
        
        divCountryRecords.appendChild(divRegion);
        
        /** Final appendings **/
        divCountryIcon.appendChild(imgCountryIcon);
        divCountryInfo.appendChild(divCountryIcon);
        divCountryInfo.appendChild(divCountryRecords);
        
        oFragment.appendChild(divCountryInfo);
        
    } /** End of Region loop **/
    
    oDivToWrite.appendChild(oFragment);
}
