function closeAllSpans(name) {
    var allSpans = document.getElementsByTagName("SPAN")
	for (a=0; a < allSpans.length; a++) {
		if (allSpans[a].className == name) {
		    allSpans[a].style.display = "none";
		}
	}	
}


//########################################################
//Tegn  HTML-værdi  Decimalværdi    Oktalværdi  Unicode-værdi 
//########################################################
//Å     &Aring;     &#197;          \305        \u00C5 
//Æ     &AElig;     &#198;          \306        \u00C6 
//Ø     &Oslash;    &#216;          \330        \u00D8 
//å     &aring;     &#229;          \345        \u00E5 
//æ     &aelig;     &#230;          \346        \u00E6
//ø     &slash;     &#248;          \370        \u00F8  
//########################################################
    
function editPicturetxt(picName, folder) {
	closeAllSpans("EditPictureTextSpan");
	
	picField = document.getElementById("picTxtEditField" + picName);
	var data = getAjaxPage("/Default.aspx?CMD=GetGalleryTxt&PictureName="+ picName +"&FolderPath="+ folder +"&CacheKiller=" + new Date().getTime());

    data = replaceSubstring(data, "&#230;", "\u00E6");
    data = replaceSubstring(data, "&#248;", "\u00F8");
    data = replaceSubstring(data, "&#229;", "\u00E5");

	picField.value = data;
	
    document.getElementById("picTxtEdit" + picName).style.display = '';
    document.getElementById("picTxtEditField" + picName).style.display = '';
}

function savePicturetxt(picName, folder) {
	closeAllSpans("EditPictureTextSpan");
	picField = document.getElementById("picTxtEditField" + picName);
	var txt = picField.value;

    if (txt.indexOf("\u00E6") > -1 || txt.indexOf("\u00F8") > -1 || txt.indexOf("\u00E5") > -1) {
        txt = replaceSubstring(txt, "\u00E6", "#ae#");
        txt = replaceSubstring(txt, "\u00F8", "#oe#");
        txt = replaceSubstring(txt, "\u00E5", "#aa#");    
    } 

	var data = getAjaxPage("/Default.aspx?CMD=GetGalleryTxt&PictureSave=true&PictureTxt="+ txt +"&PictureName="+ picName +"&FolderPath="+ folder +"&CacheKiller=" + new Date().getTime());

    if (data.indexOf("æ") > -1 || data.indexOf("ø") > -1 || data.indexOf("å") > -1) {
        data = replaceSubstring(data, "æ", "&aelig;");
        data = replaceSubstring(data, "ø", "&slash;");
        data = replaceSubstring(data, "å", "&aring;");    
    } else {
    }      
    
    document.getElementById("picTxtEditField" + picName).style.display = 'none';
    document.getElementById(picName + "_" + folder).innerHTML = data;
}

var xhttp = false;
function getXHTTPObject() {     
	if (window.ActiveXObject) {  
        try {  
			// IE 6 and higher 
			xhttp = new ActiveXObject("MSXML2.XMLHTTP"); 
			xhttp.onreadystatechange=getParameters_callback; 
        } catch (e) { 
            try { 
                // IE 5 
                xhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
				xhttp.onreadystatechange=getParameters_callback; 
            } catch (e) { 
                xhttp = false; 
            } 
        } 
	} 
    else if (window.XMLHttpRequest) { 
        try { 
            // Mozilla, Opera, Safari ... 
            xhttp = new XMLHttpRequest();
            /* little hack */
            xhttp.onreadystatechange = function () {
				getParameters_callback();
			};

        } catch (e) { 
            xhttp=false;
        } 
    } 
}

function getParameters_callback() {
    var value;                 
    if (xhttp.readyState == 4) {
		if(xhttp.status==200) {
			try {
				value = xhttp.responseText
				execScripts(parameterdiv.innerHTML);
			} catch(e) {
				/*alert('exception' + e);*/
				/*parameterdiv.innerHTML = "Error in AJAX" + e.Message;*/
			}
		}
    }

    return value;
}		
		
function getAjaxPage(url) {
	getXHTTPObject();
	
	if (!xhttp) { 
        return; 
    } 
	
	/* lets get data */
	xhttp.open("GET",url,false);
	xhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    xhttp.send(null);
    
    return xhttp.responseText;
}

function replaceSubstring(inputString, fromString, toString) {
	var temp = inputString;
   	if (fromString == "") {
		return inputString;
   	}
   	
   	fromString = ""+ fromString;
   	toString = ""+ toString;
   	
   	var toTheLeft;
   	var toTheRight;
   	
 	if (toString.indexOf(fromString) == -1) {
    	while (temp.indexOf(fromString) != -1) {
        	toTheLeft = temp.substring(0, temp.indexOf(fromString));
        	toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
        	temp = toTheLeft + toString + toTheRight;
      	}
   	} else { 
    	var midStrings = new Array("~", "`", "_", "^", "#");
      	var midStringLen = 1;
      	var midString = "";
	
		while (midString == "") {
        	for (var i=0; i < midStrings.length; i++) {
        		var tempMidString = "";
        		for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
        		if (fromString.indexOf(tempMidString) == -1) {
			    	midString = tempMidString;
              		i = midStrings.length + 1;
		        }
         	}
		} 
	
		while (temp.indexOf(fromString) != -1) {
        	toTheLeft = temp.substring(0, temp.indexOf(fromString));
		   	toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
        	temp = toTheLeft + midString + toTheRight;
		}

		while (temp.indexOf(midString) != -1) {
        	toTheLeft = temp.substring(0, temp.indexOf(midString));
        	toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
	       	temp = toTheLeft + toString + toTheRight;
      	}
   	}
   	return temp;
}