/**
 * Benoetigter Code fuer die Klasse "Component"
 * Zum zaehlen eingegebener Zeichen
 * und Ein-/Auschecken von Radio-/Combobox
 * 
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2010 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/?GUI[0]=license     New BSD License
 * 
 * */


/*
    Funktionen zum vewalten der Klasse Component

    Autor           : Joachim Ruf
    Revision        : 1.5
*/




/**
* Zaehlt die Anzahl der Zeichen in einem Textfeld und speichert das Ergebnis als innerHTML in einem anderen Tag ab
* @param string idTextfield 	: ID des anzusprechenden Elements
* @param string idOutputField 	: ID des Tags in welches das Egebnis gespeichert werden soll
* @param int $maxLength			: Maximale Anzahl zulaessiger Zeichen
* @return void
*/
function countChar( idTextfield, idOutputField, maxLength )
{
	var textLength = idTextfield.value.length;
	if (textLength > maxLength) {
		textLength = '<font style="color: red">'+textLength+'</font>';
	}
    document.getElementById(idOutputField).innerHTML = textLength + '/' + maxLength;
}


/**
* Checkt eine Checkbox per Klick auf den CheckBox-Text ein
* @param string secId ID des anzusprechenden Elements
* @return void
*/
function checkboxInOut( secId )
{
    obj = document.getElementById(secId);
	if(obj.checked == false)
		obj.checked = true;
	else
		obj.checked = false;
}

/**
* Checkt eine Checkbox per Klick ein
* @param string secId ID des anzusprechenden Elements
* @return void
*/
function checkboxIn( secId )
{
	document.getElementById(secId).checked = true;
}

/**
* Checkt eine Checkbox per Klick ein
* @param string secId ID des anzusprechenden Elements
* @return void
*/
function checkboxOut( secId )
{
	document.getElementById(secId).checked = false;
}










/**
* Fügt einem Element dynamisch eine neue Upload-Box hinzu
* @param string secId ID des anzusprechenden Elements
* @param int $maxLength Maximale Anzahl zulaessiger Zeichen
* @return void
*/
/*
function addUploadBox( secId, i )
{
	j = i+1;
    output = ''
    + '<input type="file" name="img['+i+']" value="" onChange="addUploadBox(\''+secId+'\', '+j+')" />'
    + '<div id="uploadBox'+j+'">'
    + '&nbsp;'
    + '</div>';

    document.getElementById(secId+i).innerHTML = output;
}
*/


/**
* Prüft ob es sich beim Upload um eine gültige Grafik handelt
* @param int image Pfad der Grafikdatei
* @return bool
*/
function checkUpload( image )
{
    dotPos = image.lastIndexOf(".");
    fileFormat = image.substr(dotPos, image.length - dotPos);
	fileFormat = fileFormat.toLowerCase();
    
    //alert(fileFormat);
	if( fileFormat == ".jpg" || fileFormat == ".bmp" || fileFormat == ".gif" || fileFormat == ".png")
		return true;
	else
		return  false;
}





/**
* Erstellt eine Vorschau der abzuloadenden Grafik
* @param string uploadFormId ID des Upload-Formulars
* @param string secId ID des anzusprechenden Elements
* @return void
*/
function setUploadImage( uploadFormId, secId, size )
{
 	if( checkUpload(uploadFormId.value) == true ) // Upload-Format gueltig
 	{
 		// Bildbreite ermitteln um Bild der groesse als Vorschau entsprechend anzuzeigen
 		var img = new Image();
 		img.src = uploadFormId.value;

 		var aspectRatio = img.width / img.height;
 		//alert(uploadFormId.value);
    	document.getElementById(secId).src = uploadFormId.value; 	
    	document.getElementById(secId).width = parseInt(size * aspectRatio);
    	document.getElementById(secId).height = parseInt(size);
 	}
	else //Upload-Format ungueltig
	{
		alert("Dateiformat ungültig. Es sind nur folgende Dateiformate zulässig:\n.jpg, .bmp, .gif, .png");	
	}
}





/**
* Erstellt eine Vorschau der abzuloadenden Grafik
* @param string secId ID des anzusprechenden Elements
* @return void
*/
function expandImage( secId )
{
	filePath = secId.src;
	if( filePath.indexOf("component/fileUploadImg.png") == -1 )
	{
		img = new Image();
		img.src = secId.src;

		aspectRatio = img.width / img.height;

		secId.width = parseInt(140 * aspectRatio);
		secId.height = 140;
	}
}





/**
* Erstellt eine Vorschau der abzuloadenden Grafik
* @param string secId ID des anzusprechenden Elements
* @return void
*/
function collapseImage( secId )
{
	filePath = secId.src;
	if( filePath.indexOf("component/fileUploadImg.png") == -1 )
	{
		img = new Image();
		img.src = secId.src;

		aspectRatio = img.width / img.height;

		secId.width = parseInt(70 * aspectRatio);
		secId.height = 70;
	}
}






