

var msgRequest = null;

function displayDocumentDetails(id){

	//Move to Anchor on top of page
	window.location = "#top";
	
    //Display Loading Div
    displayLoadingDivSimple('loading_div_meta');
    displayLoadingDivSimple('loading_div_files');
	    
	//Make Call To Server Side
	msgRequest = createXMLRequest();

	universalPostToServer("/PostData/PostDataClientOnly.aspx", "documentloadmeta", "id="+id, true, displayDocumentDetailsCallBackFunction, msgRequest, true);

}



function displayDocumentDetailsCallBackFunction() {

	if (msgRequest.readyState == 4) {

		if (msgRequest.status == 200) {

			var message = msgRequest.responseXML;

			//Remove Loading Display:
			document.getElementById('loading_div_content_document').innerHTML = "";
			
			if (message) {

                var resultArray = new Array();
                
                resultArray = convertXMLResponseToArray(message,"documents","document");

				//Add document details
                addDocumentDetails(resultArray[0]);
                
				//Start loading file details
                displayFileDetails(resultArray[0]['id']);
			
				//Hide Loading Div
				hideLoadingDivSimple('loading_div_meta', 100);
			
			}
						
		} 
			
	} 

}


function addDocumentDetails(childNodeArray){

    //Add Document Meta Data to Screen
    var html = "";
    
    html +='<br/><p>';
	html +='<b>Title</b><br/>';
	html +=childNodeArray['title'] + '<br/>';
	html +='<br/><b>Description</b><br/>';
	html +=childNodeArray['description'] + '<br/>';
    html +='</p>';
	
	document.getElementById('loading_div_content_document').innerHTML = html;
	
	document.getElementById('disclaimer_required').value = childNodeArray['disclaimer_required'];
}



function displayFileDetails(id){

	//Make Call To Server Side
	msgRequest = createXMLRequest();

	universalPostToServer("/PostData/PostDataClientOnly.aspx", "documentloadfiles", "id="+id, true, displayFileDetailsCallBackFunction, msgRequest, true);

}


function displayFileDetailsCallBackFunction() {

	if (msgRequest.readyState == 4) {

		if (msgRequest.status == 200) {

			var message = msgRequest.responseXML;

			//Remove Loading Display:
			document.getElementById('loading_div_content_file').innerHTML = "";
			
			if (message) {

                var resultArray = new Array();
                
                resultArray = convertXMLResponseToArray(message,"files","file");

				for(i = 0; i < resultArray.length; i++){
                	
					addDocumentFileDetails(resultArray[i]);
                
				}
			
			}
			
			//Hide Loading Div
			hideLoadingDivSimple('loading_div_files', 100);
						
		} 
			
	} 


}



function addDocumentFileDetails(childNodeArray){
 
    //Add Document Files to Screen
    var html = document.getElementById('loading_div_content_file').innerHTML;
    
    html +='<br/><table style="width:100%;" border="0" cellpadding="5" cellspacing="0">';
	html +='<tr><td style="padding-left:0px;text-align:right;vertical-align:top;"><a href="javascript:;" onclick="downloadFile(\'' + childNodeArray['id'] + '\',\'' + childNodeArray['filename'] + '\');">' + childNodeArray['filename'] + '.' + childNodeArray['extension'] + '</a></td>';
	html +='<td style="text-align:right;vertical-align:top;width:50px;">' + formatFileSizeToKB(childNodeArray['filesize']) + '</td><tr>';
	html +='<tr><td colspan="2" style="text-align:right;"><a href="#" onclick="downloadFile(\'' + childNodeArray['id'] + '\',\'' + childNodeArray['filename'].replace('\'','') + '\');" style="vertical-align:middle;">Download <img src="images/icons/download.png" border="0" alt="" style="padding-left:5px;padding-bottom:2px;vertical-align:middle;"/></a></td></tr>';
    html +='</table>';
    html +='</div>';
    
    document.getElementById('loading_div_content_file').innerHTML = html;
    
}

function downloadFile(id, filename){

    var disclaimer_required = "";
    
    try{
    
        disclaimer_required = document.getElementById('disclaimer_required').value;

    }catch(err){
    
        //Fail silently as data validation is done in the ValidInput.js file        

    }

    if(disclaimer_required == 't'){

        startLyteFrame(filename,'DownloadFile.aspx?id=' + id);
   
    }else{

        window.location = 'http://www.dioro.com.au/DownloadFile.aspx?id=' + id;
    
    }

}

function searchDocumentList(){

    var searchString = document.getElementById('txtSearch').value;
    var index = 0;
    
    if(searchString != "Search" && searchString != ""){

        var url = window.location;
        url += "";
        index = url.indexOf("?");
        
        if(index != -1){

            url = url.substring(0,index);
            
        }
        
        url += "?search=" + searchString;
            
        window.location = url;
    
    }
    
}

function submitSearchDocumentList(keyEvent){

	the_key = keyEvent.keyCode;

	if(the_key==13){
		
		searchDocumentList();
		
	}

}

function loadSearchField(value){

    document.getElementById('txtSearch').value = value;
    
}

function clearField(id, value){

    if(document.getElementById(id).value == value){
    
        document.getElementById(id).value = "";
        
    }
    
}