/* ===========================================================================
//	lib_form.js 
// ======================================================================== */


/*	handle values or status (checked, highlighted) of form fields 
//	(e.g. in preselect the searchtool) 
*/
var handleFoFi = {
	
	fillText				: function(id,v) {
								if(document.getElementById(id)) {
									document.getElementById(id).value = v;
								}
							},
	
	setCheckbox 			: function(id,v) {
								if(document.getElementById(id)) {
									document.getElementById(id).checked = v;
								}
							},
	
	selectOptionByValue		: function(id,v) {
								if(document.getElementById(id)) {
									var n = document.getElementById(id);
									for(var i=0; i<n.length; i++) {
										if(n.options[i].value == v) {
											n.options[i].selected = true;
											n.options[i].selectedIndex = true;
										}
									}
								}
							},
	
	highlightOptionByValue	: function(id,v,c) {
								c = c || "hi";
								if(document.getElementById(id)) {
									var n = document.getElementById(id);
									for(var i=0; i<n.length; i++) {
										if(n.options[i].value == v) {
											n.options[i].className = c;
										}
									}
								}
							}
}





var errorMessages = new Array();

errorMessages.repaint = function () {
    var formularErrorBox = document.getElementById("formularErrorBox");
    if (this.length>0) {
        formularErrorBox.style.display = 'block';
        var messageList = formularErrorBox.getElementsByTagName('ul')[0];

        // remove the old list items from the DOM
        var oldMessageNodes = messageList.childNodes;
        for (var i=oldMessageNodes.length-1;i>=0;i--) {
            messageList.removeChild(oldMessageNodes[i]);
        }
                
        //add the new ones
        var listNode;
        var listTextNode;
        for (var i=0;i<this.length;i++) {
            listNode = document.createElement("li");
            listTextNode = document.createTextNode(this[i]);
            listNode.appendChild(listTextNode);
            messageList.appendChild(listNode);            
        }            
        
    } else {
        formularErrorBox.style.display = 'none';
    }    
}

errorMessages.clear = function () {
    while (this.length>0) {
        this.pop();
    }
}

function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}

function isNullOrEmpty(text){
    return (text == null || trim(text) == "");
}

function checkUploadForm() {
    var form=document.uploadVideo;
    errorMessages.clear();
    if (isNullOrEmpty(form.firstname.value))   {
        errorMessages.push("Please enter the author's first name.");
    }        
    if (isNullOrEmpty(form.lastname.value))   {
        errorMessages.push("Please enter the author's last name.");
    }
    if (isNullOrEmpty(form.file.value))   {
        errorMessages.push("Please choose a file.");
    } 
    if (isNullOrEmpty(form.email.value))   {
        errorMessages.push("Please enter the autor's email.");
    }
    if (isNullOrEmpty(form.title.value))   {
        errorMessages.push("Please enter a title.");
    }
    errorMessages.repaint();
    if (errorMessages.length == 0) {
        // openWaitPopUp();
        return true;
    }
    else {
        window.scrollTo(0,0);
        return false;
    }
}

function guessFileFormat(fileName) {
    var videoFormats = new Array("3gp","avi","mov","mp4","wmv"); // order must be the same like in the selectBox
    fileName = trim(fileName).toLowerCase();;
    var lastIndexOfPoint= fileName.lastIndexOf(".");
    if (lastIndexOfPoint >= 1) {
        var extensionLength = fileName.length-lastIndexOfPoint-1;
        if (extensionLength >= 3 && extensionLength<=4) {
            var extension = fileName.substr(lastIndexOfPoint+1,extensionLength);
            //alert(extension);
            for (var i=0;i<videoFormats.length;i++) {
                if (extension == videoFormats[i]) {
                    document.uploadVideo.fileFormat.selectedIndex = i;
                }
            }
        }
    }
}

var myEffect = null;

/* Checks also for the not allowed chars '<' nad '>' */
function checkMaxLength(textarea,leftCharDivID,maxTextLength) {      
    var leftCharDiv = document.getElementById(leftCharDivID);
    var text = textarea.value;
    var leftChars = maxTextLength-text.length;
    var textChanged = false;
    if (text.match(/[\<\>]/)) {
        text = text.replace(/[\<\>]/gi, "");
        leftChars = maxTextLength-text.length;
        textChanged = true;
    }    
    if (leftChars < 0) {
        text = text.substr(0,maxTextLength);
        leftChars = maxTextLength-text.length;
        textChanged = true;
    }
    if (textChanged) textarea.value = text;
    leftCharDiv.innerHTML = leftChars;
    return true;
}




/* eof ==================================================================== */

