// dynamic form elements script
// $Id: dynform.js,v 1.3 2003/09/15 17:21:55 wedge Exp $

var hasLinks = 0;
var hasImgs = 0;
var hasFiles = 0;

var numlinks = 0;
var numimgs = 0;
var numfiles = 0;
var nextid = 0;

var maxImgs = -1;
var maxFiles = -1;


function addLinkInput(name, url, dbid)
{
    cf = document.getElementById("linkscount");
    if (cf && cf.value)
        numlinks = cf.value;

	linkinput = '<div id="link'+nextid+'"><table border="0"><tr><td width="10%"><span class="standard">Name:</span></td>';
	linkinput += '<td width="30%"><input type="text" size="20" name="linkname[]" value="'+name+'"></td>';
	linkinput += '<td width="10%"><input type="hidden" size="20" name="linkid[]" value="'+dbid+'">';
	linkinput += '<span class="standard">URL:</span></td><td width="30%"><input type="text" size="20" name="linkurl[]" value="'+url+'"></td>';
	linkinput += '<td width="20%" align="center"><button class="mybutton" type="button" name="deletelink" onClick="deleteLink('+nextid+', '+dbid+')" ID="deletebutton'+nextid+'">Link löschen</button></td></tr></table></div>';
	
	numlinks++;
	nextid++;
	
	el = document.getElementById("linkstable");
	if (el)	el.innerHTML += linkinput;
	
	// update count hidden field
	cf.value = numlinks;
}


function deleteLink(num, dbid)
{
	cf = document.getElementById("linkscount");
    if (cf && cf.value)
        numlinks = cf.value;        
    
    el = document.getElementById("linkstable");
	removecmd = '<input type="hidden" name="removelink[]" value="'+dbid+'">';
	
	if (el)
	{
		toremove = document.getElementById("link"+num);
		if (toremove)
		{
			// remove form elements
			el.removeChild(toremove);
			
            numlinks--;
				
			// insert remove command if link was already in db
			if (dbid >= 0)
			{
				if (el)	el.innerHTML += removecmd;
			}
		}
	}
    
    // update count hidden field
    cf.value = numlinks;
}

function addImgInput(filename, name, copyright, dbid)
{
    cf = document.getElementById("imgscount");
    if (cf && cf.value)
        numimgs = cf.value;
    
    if (maxImgs >= 0)
    {
        if (numimgs >= maxImgs)
            return;
    }

    imginput = '<div id="img'+nextid+'"><table border="0"><tr><td width="10%"><span class="standard">Datei:</font></td>';
	imginput += '<td width="700%"><input type="file" accept="image/jpg" size="20" name="imgfilename[]" value="'+filename+'">';
    
    if (dbid >= 0)
        imginput += '&nbsp;&nbsp;&nbsp;<span class="standard">Aktuelles Bild:</span> <a class="snavigation" href="javascript:PopUp(\'/images/uploaded/'+filename+'\', \'400\', \'400\')">'+filename+'</a>';
	imginput += '</td></tr></table><table border="0"><tr><td width="10%"><span class="standard">Name:</span></td><td width="30%"><input type="text" size="20" name="imgname[]" value="'+name+'"></td>';
	imginput += '<input type="hidden" size="20" name="imgid[]" value="'+dbid+'">';
	imginput += '<td width="5%"><span class="standard">©</span></td><td width="30%"><input type="text" size="20" name="imgcopyright[]" value="'+copyright+'"></td>';
	imginput += '<td width="25%" align="center"><button type="button" name="deleteimg" onClick="deleteImg('+nextid+', '+dbid+')" ID="deletebutton'+nextid+'">Bild löschen</button></td></tr></table><hr noshade="0" color="#C0C0C0" style="border-style: dashed"></div>';
	
	numimgs++;
	nextid++;
	
	// add new input elements
	
	el = document.getElementById("imgstable");
	if (el)	el.innerHTML += imginput;
	
    // update count hidden field
    cf.value = numimgs;
}


function deleteImg(num, dbid)
{
	cf = document.getElementById("imgscount");
    if (cf && cf.value)
        numimgs = cf.value;
    
    el = document.getElementById("imgstable");
	removecmd = '<input type="hidden" name="removeimg[]" value="'+dbid+'">';
	
	if (el)
	{
		toremove = document.getElementById("img"+num);
		if (toremove)
		{
			// remove form elements
			el.removeChild(toremove);
			
            numimgs--;
			
            // insert remove command if link was already in db
			if (dbid >= 0)
			{
				if (el)	el.innerHTML += removecmd;
			}
			
		}
	}
   
    // update count hidden field
    cf.value = numimgs;
}


function addFileInput(filename, name, copyright, dbid)
{
    cf = document.getElementById("filescount");
    if (cf && cf.value)
        numfiles = cf.value;
    
    if (maxFiles >= 0)
    {
        if (numfiles >= maxFiles)
            return;
    }

    fileinput = '<div id="file'+nextid+'"><table border="0"><tr><td width="10%"><span class="standard">Datei:</font></td>';
	fileinput += '<td width="70%"><input type="file" size="20" name="filefilename[]" value="'+filename+'">';
    
    if (dbid >= 0)
        fileinput += '&nbsp;&nbsp;&nbsp;<span class="standard">Aktuelle Datei:</span> <a class="snavigation" href="/files/uploaded/'+filename+'" target="_blank">'+filename+'</a>';
	fileinput += '</td></tr></table><table border="0"><tr><td width="10%"><span class="standard">Name:</span></td><td width="30%"><input type="text" size="20" name="filename[]" value="'+name+'"></td>';
	fileinput += '<input type="hidden" size="20" name="fileid[]" value="'+dbid+'">';
	fileinput += '<td width="5%"><span class="standard">©</span></td><td width="30%"><input type="text" size="20" name="filecopyright[]" value="'+copyright+'"></td>';
	fileinput += '<td width="25%" align="center"><button type="button" name="deletefile" onClick="deleteFile('+nextid+', '+dbid+')" ID="deletebutton'+nextid+'">Datei löschen</button></td></tr></table><hr noshade="0" color="#C0C0C0" style="border-style: dashed"></div>';
	
	numfiles++;
	nextid++;
	
	// add new input elements
	
	//document.getElementById("filestable").innerHTML += fileinput;
	el = document.getElementById("filestable");
	if (el)	el.innerHTML += fileinput;
	
    // update count hidden field
    cf.value = numfiles;
}


function deleteFile(num, dbid)
{
	cf = document.getElementById("filescount");
    if (cf && cf.value)
        numfiles = cf.value;
    
    el = document.getElementById("filestable");
	removecmd = '<input type="hidden" name="removefile[]" value="'+dbid+'">';
	
	if (el)
	{
		toremove = document.getElementById("file"+num);
		if (toremove)
		{
			// remove form elements
			el.removeChild(toremove);
			
            numfiles--;
			
            // insert remove command if link was already in db
			if (dbid >= 0)
			{
				if (el)	el.innerHTML += removecmd;
			}
		}
	}
   
    // update count hidden field
    cf.value = numfiles;
}




function checkDelete(hlink)
{
    Check = confirm ("Eintrag loeschen?");
    if (Check == false) exit
    else document.location.href = hlink;
}

function checkCategoryDelete(hlink)
{
    Check = confirm ("Kategorie loeschen?\nAlle Beitrage dieser Kategorie werden ebenfalls geloescht!");
    if (Check == false) exit
    else document.location.href = hlink;
}

function checkCommentDelete(hlink)
{
    Check = confirm ("Kommentar wirklich loeschen?");
    if (Check == false) exit
    else document.location.href = hlink;
}

function validateForm()
{
    thisform = document.forms[0];
    
    for(i=0; i<thisform.elements.length; i++)
    {
        elem = thisform.elements[i];
        
        // Validate image types
        if (elem.name == "imgfilename[]")
        {
            if ((!elem.value.match(/^.*\.(jpg|jpeg|zip)$/i)) && (elem.value != ""))
            {
                alert(elem.value+" ist keine JPEG/ZIP Datei!\nEs koennen nur JPEG/ZIP Datein verwendet werden");
                elem.focus();
                return false;
            }
        }
        
        // Validate link URLs
        if (elem.name == "linkurl[]")
        {
            if (elem.value.indexOf("@") > -1)
            {
                // email address assumed
                if (elem.value.indexOf("mailto:") != 0)
                {
					alert("Links zu Email-Adressen muessen im Format z.B. \"mailto:user@host.com\" eingeben werden!");
	                elem.focus();
	                return false;	
                }
            }
            else
            {
                // http link assumed
                if (elem.value.indexOf("http://") != 0)
                {
					alert("HTTP-Links muessen im Format z.B. \"http://www.ingenium.at\" eingeben werden!");
	                elem.focus();
	                return false;	
                }
            }
        }
    }
}

function PopUp(img, w, h)  {
	window.open(img,'zoom'+w+h,'toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=yes,copyhistory=no,width='+w+',height='+h);
}

