/*
 * Copyright (c) 2006 yovisto.com - Search Web Search App. All Rights Reserved. *
 * Autor: Joerg Waitelonis
 *
 */

var updater = null;
function validateForm(){

	if ($('title3').value.trim()==""){
		$('status').innerHTML = "Please input a title!";
		return false;
	}
	
	if ( (getSelected( $('speakerChooserOption') ) == 'null') && ($('family_name').value.trim()="")){
		$('status').innerHTML = "Please select a speaker or create a new speaker!";
		return false;
	}	
	
	if ( $('radioVideoURL').checked  && $('videoURL').value.trim()=="" ){
		$('status').innerHTML = "Please specify an url to download a video!";
		return false;
	}
	
	if ( $('radioVideoUpload').checked  && $('videoFile').value.trim()=="" ){
		$('status').innerHTML = "Please pick a video file from your hard disk!";
		return false;
	}
	
	
	return true;
}
function startStatusCheck(validate) {   
	
	if (validate)
		if (!validateForm()) return false;
	
    $('submitButton').disabled = true;
    $('resetButton').disabled = true; 
    $('cancelButton').disabled = false;
    
    updater = new Ajax.PeriodicalUpdater('status', 'UploadStatus', {
	    asynchronous:true, 
		frequency:1, 
		method: 'get', 
		parameters: 'c=status', 
		onFailure: reportError
		});       
    return true;
}

function reportError(request){
    $('submitButton').disabled = false;
    $('resetButton').disabled = false; 
    $('cancelButton').disabled = true;
    $('status').innerHTML = '<div class="error"><b>Error communicating with server. Please try again.</b></div>';
    
    
    
}

function killUpdate(message){  
    $('submitButton').disabled = false;
    $('resetButton').disabled = false; 
    $('cancelButton').disabled = true;    
    updater.stop();
	$('organisationId').value="null";
    if(message != ''){
        $('status').innerHTML = '<div class="error">' + message + '</div>';
    } else {
        $('status').innerHTML = '<div class="error"><b>Upload finished.</b></div>';       
        if ($('title')!=null){ 
        	$('uploadQueue').innerHTML = $('title').value  + ' <br> '+ $('uploadQueue').innerHTML;
        	$('uploadQueuePane').style.display='inline';
        }
    }
}
   

function cancelUpload(){
    var url = '/UploadStatus;jsessionid=' + jsid;
    var pars = 'cancel=0'; 
    var myAjax = new Ajax.Request( url, {
	    method: 'get', 
        parameters: pars,
        onComplete: cancelUploadCallback
        });
    $('submitButton').disabled = false;    
    $('resetButton').disabled = false; 
    $('cancelButton').disabled = true;
}  
function cancelUploadCallback(transport){
    //nothing ?
}
  
function radioDissAll(){
    $("st").style.display = "none";
    $("vi").style.display = "none";
    $("vif").style.display = "none";
    $("smi").style.display = "none";
    $("lec").style.display = "none";
    $("st2").style.display = "none";
    $("vi2").style.display = "none";
    $("vif2").style.display = "none";
    $("smi2").style.display = "none";
    $("lec2").style.display = "none";
    $("streamURL").disabled = true;
    $("videoURL").disabled = true;
    $("videoFile").disabled = true;
    $("smilURL").disables = true;
    $("lectURL").disables = true;
    $("submitButton").disabled = false;

    $("goo").style.display="none";
    $("goo2").style.display="none";
    $("googleURL").disables=true;
}

function radio(){  
    radioDissAll();
    if ($("radioStreamingURL").checked){
        $("st").style.display="inline";                
       $("st2").style.display="inline";
        $("streamURL").disabled=false;
    }
    if ($("radioVideoURL").checked){
        $("vi").style.display="inline";
        $("vi2").style.display="inline";           
        $("videoURL").disabled=false;
    }

    if ($("radioVideoUpload").checked){
        $("vif").style.display="inline";          
        $("vif2").style.display="inline";
        $("videoFile").disabled=false;
    }
    if ($("radioSynchPres").checked){
        $("smi").style.display="inline";
        $("smi2").style.display="inline";
        $("smilURL").disables=false;
    } 
    if ($("radioLect1_5_0").checked){
        $("lec").style.display="inline";          
        $("lec2").style.display="inline";
        $("lectURL").disables=false;
    }
    if ($("radioWatermark").checked){
        $("smi").style.display="inline";
        $("smi2").style.display="inline";
        $("smilURL").disables=false;

        $("hasTranscript").checked = true;

        $("trans").style.display = "inline";
        $("trans2").style.display = "inline";
        $("transcriptFile").disabled = false;
    }

    if ($("radioGoogle").checked){
        $("goo").style.display="inline";
        $("goo2").style.display="inline";
        $("googleURL").disables=false;

        $("hasTranscript").checked = false;

        $("trans").style.display = "none";
        $("trans2").style.display = "none";
        $("transcriptFile").disabled = true;
   }
}

function uploadReset(){
    $("st").style.display = "none";
    $("vi").style.display = "none";
    $("vif").style.display = "none";
         
    $("st2").style.display = "none";
    $("vi2").style.display = "none";
    $("vif2").style.display = "none";

    $("streamURL").disabled = true;
    $("videoURL").disabled = true;
    $("videoFile").disabled = true;

    $("submitButton").disabled = true;
  
    $("trans").style.display = "none";
    $("trans2").style.display = "none";
    $("transcriptFile").disabled = true;
}

  
function checkbox(){
    if ($("hasTranscript").checked == true){
        $("trans").style.display = "inline";
        $("trans2").style.display = "inline";
        $("transcriptFile").disabled = false;
        $("trans2_2").style.display = "none";
        $("trans2_3").style.display = "none";
        $("trans2_4").style.display = "none";
        
        if($("radioGoogle").checked){
            $("hasTranscript").checked = false;
            
            $("trans").style.display = "none";
            $("trans2").style.display = "none";
            $("transcriptFile").disabled = true;
            $("trans2_2").style.display = "none";
            $("trans2_3").style.display = "none";
            $("trans2_4").style.display = "none";
        }

    } else{
        if (!$("radioWatermark").checked){        
            $("trans").style.display = "none";
            $("trans2").style.display = "none";
            $("transcriptFile").disabled = true;
            $("trans2_2").style.display = "none";
            $("trans2_3").style.display = "none";
            $("trans2_4").style.display = "none";

        }else{
            $("hasTranscript").checked = true;
        }
    }
}


function speaker2(){
   $("speaker2").style.display = "block";
}
function speaker3(){
  $("speaker3").style.display = "block";
}
function speaker4(){
   $("speaker4").style.display = "block";
}

function transcript2(){
   $("trans2_2").style.display = "block";
}
function transcript3(){
   $("trans2_3").style.display = "block";
}
function transcript4(){
   $("trans2_4").style.display = "block";
}



function res2(){
   $("res2").style.display = "block";
}
function res3(){
  $("res3").style.display = "block";
}
function res4(){
   $("res4").style.display = "block";
}



/* Load a chooser for a user set of collection */
function loadCollectionChooser(selected){		
	var url = '/NewCollection;jsessionid=' + jsid;
	var pars = 'selected='+selected+"&upload=1"; 
	var myAjax = new Ajax.Request( url, {
	    method: 'get', 
	    parameters: pars, 
	    onComplete: loadCollectionChooserCallback
	    });
}
function loadCollectionChooserCallback(transport) {  
    $("MyYovisto_CollectionChooser").innerHTML = transport.responseText;
}

function loadOrganisationsTree(){
    $('OrganisationsTree').innerHTML = "";
    tree=new dhtmlXTreeObject("OrganisationsTree","100%","100%",0);
    tree.setImagePath("/img/tree/");
    tree.enableCheckBoxes(true);
    tree.loadXML("/GetOrganisationXMLTree");  
}

 
function invalidateOrgId(){
	if (document.forms.xmlUploadForm.organisationSelect1.selectedIndex < 0){
		$("status").innerHTML = "Please select an organisation in step 1! <br> Hint: You can use the filter to find your university in the list simply by typing the university's name into the filter input field. If your organisation does not appear in this list please create a <a href=\"javascript:showOrganisations('lecture')\"> new organisation entry</a>.";
		$('organisationId').value = null;
	}else{
		$('organisationId').value = document.forms.xmlUploadForm.organisationSelect1[document.forms.xmlUploadForm.organisationSelect1.selectedIndex].value
 	}
 	//alert($('organisationId').value);
}
