//FIXME: Add distinct logger for this file?

function toggle_visiblity_by_name(control, name) {
    var elems = document.getElementsByName(name);
    if (elems && elems.length) {
        // Note that if the elems are not of uniform visibility, they will
        // not be uniform afterwards, just the complement!
        // Last one affects control image!
        for (var i = 0; i < elems.length; i++) {
            if (elems[i].style.display == 'none') {
                elems[i].style.display = 'block';
                if (control != null) {
                    control.src="Images/map/minus.png"
                }
            } else {
                elems[i].style.display = 'none';
                if (control != null) {
                    control.src="Images/map/plus.png"
                }
            }
        }
    }
}

function toggle_visibility(control, targetId) {
    var e = document.getElementById(targetId);
    if (e != null) {
        if(e.style.display == 'none') {
            e.style.display = 'block';
            if (control != null) {
                control.src="Images/map/minus.png"
            }
        } else {
            e.style.display = 'none';
            if (control != null) {
                control.src="Images/map/plus.png"
            }
        }
    }
}

function set_visibility(control, targetId, vis) {
    var e = document.getElementById(targetId);
    if (e != null) {
        if(vis) {
            e.style.display = 'block';
            if (control != null) {
                control.src="Images/map/minus.png"
            }
        } else {
            e.style.display = 'none';
            if (control != null) {
                control.src="Images/map/plus.png"
            }
        }
    }
}

function Trim(TRIM_VALUE){
    if(TRIM_VALUE.length < 1){
    return"";
    }
    TRIM_VALUE = RTrim(TRIM_VALUE);
    TRIM_VALUE = LTrim(TRIM_VALUE);
    if(TRIM_VALUE==""){
    return "";
    }
    else{
    return TRIM_VALUE;
    }
} //End Function

function RTrim(VALUE){
    var w_space = String.fromCharCode(32);
    var v_length = VALUE.length;
    var strTemp = "";
    if(v_length < 0){
    return"";
    }
    var iTemp = v_length -1;

    while(iTemp > -1){
    if(VALUE.charAt(iTemp) == w_space){
    }
    else{
    strTemp = VALUE.substring(0,iTemp +1);
    break;
    }
    iTemp = iTemp-1;

    } //End While
    return strTemp;

} //End Function

function LTrim(VALUE){
    var w_space = String.fromCharCode(32);
    if(v_length < 1){
    return"";
    }
    var v_length = VALUE.length;
    var strTemp = "";

    var iTemp = 0;

    while(iTemp < v_length){
    if(VALUE.charAt(iTemp) == w_space){
    }
    else{
    strTemp = VALUE.substring(iTemp,v_length);
    break;
    }
    iTemp = iTemp + 1;
    } //End While
    return strTemp;
} //End Function

//Function for converting map coords into pixel coords
function toPixelXY(inX,inY,width,height,xDistance,yDistance,left,bottom) { 
    pixelX =  xDistance / width;
    // alert(pixelX);
    // alert(yDistance +"/"+iHeight);
    pixelY = yDistance / height; 
    // alert(pixelY);
    //alert(inY+"/"+pixelY+" - "+eBottom+"/"+pixelY);
    //alert(inX+","+inY+","+eLeft+","+eBottom);
    pX = (inX / pixelX) - (left / pixelX); 
    pY = height - ((inY / pixelY) - (bottom / pixelY));
    return pX + "," + pY;                    
}

//Convert mouse coordinates into map coords
function getMapXY(xIn,yIn) {
    mouseX = xIn;
    pixelX = mapXDistance / pixelWidth;
    mapX = pixelX * mouseX + mapLeft;
    mouseY = pixelHeight - yIn;
    pixelY = mapYDistance / pixelHeight;
    mapY = pixelY * mouseY + mapBottom;
    //alert(mapX+","+mapY);
    return mapX+","+mapY;
}

function CommaFormatted(num) {       
    var sVal='';
    var minus='-';
    var CommaDelimiter=',';
    var samount = new String(num);

       for (var i = 0; i < Math.floor((samount.length-(1+i))/3); i++)
      {
         samount = samount.substring(0,samount.length-(4*i+3)) + CommaDelimiter + samount.substring(samount.length-(4*i+3));
       }
   return samount;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function removeNamespace(str) {
    if (NAMESPACE!=null && NAMESPACE!="" && str!=null && str.length >= NAMESPACE.length + 1) {
        var qNamespace = NAMESPACE + ":";
        if (qNamespace==str.substring(0,qNamespace.length)) {
            return str.substring(qNamespace.length);
        }
    }
    return str;
}

function addNamespace(str) {
    if (NAMESPACE!=null && NAMESPACE!="" && str!=null && str!="") {
        return NAMESPACE + ":" + str;
    } else {
        return str;
    }
}

function initCap(str) {
    // Should we do a split on ' ' and initCap each word?
    if (str != null && str!="") {
        var newStr;
        newStr = str.substring(0,1).toUpperCase();
        if (str.length > 1) {
            newStr += str.substring(1,str.length);
        }
        return newStr;
    } else {
        return str;
    }
}

function getElementName(str) {
    if (window.ActiveXObject) {
        return addNamespace(str);
    } else {
        return str;
    }
}

function getBooleanAttribute(element, attrName, dflt) {
    var yesNo = element.getAttribute(attrName);
	if (yesNo != null) {
        yesNo = yesNo.toUpperCase();
        if ("YES" == (yesNo) || "TRUE" == (yesNo) || "Y" == (yesNo) || "T" == (yesNo) || "1" == (yesNo)) {
            dflt = true;
        } else if ("NO" == (yesNo) || "FALSE" == (yesNo) || "N" == (yesNo) || "F" == (yesNo) || "0" == (yesNo)) {
            dflt = false;
        }
    }
    //mapLog.debug("Returning " + dflt + " for " + attrName + " of element " + element);
    return dflt;
}

function preloadImage(imageName, path) {
    if (imageName != null && imageName.length > 0) {
        var baseName = imageName.substring(0,imageName.lastIndexOf("."));
        eval(baseName + " = new Image();");
        eval(baseName + ".src=" + "'" + path + imageName + "';");
    }
}

function XMLRequestWrapper() {
    this._xmlReq = null;
}

XMLRequestWrapper.prototype.getReq = function () {
    return this._xmlReq;
}

XMLRequestWrapper.prototype.createXMLHttpRequest = 
function () {
    var req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
    this._xmlReq = req;
    return req;
}

XMLRequestWrapper.prototype.checkReadyState = 
function checkReadyState() { 
    
    if (this._xmlReq.readyState == 4) { 
        if (this._xmlReq.status == 200) { 
            filterLog.debug("checkReadyState: Successful response");
            return true; 
        } else { 
            // Warn the user? 
            filterLog.error("checkReadyState: Failure status code " + this._xmlReq.status);
            //alert("problem receiving data"); 
        }
    }
}
