/**
 * Filesnapper written by Morten F. Klausen.
 */

function checkIfFileShouldBeSnappedAndThenSnap(snapFile, modulePath, callbackField)
{
	//alert('snspcheck av filen ' + snapFile);   
	var snapperBusiness = new SnapperRequest(snapFile, modulePath, callbackField);
	
	if(snapperBusiness.checkIfFileToBeSnapped())
	{
		//alert('Skal snappe filen ' + snapFile);
		snapperBusiness.startSnapping();
	}
	else
	{
		//alert('Skal ikke snappe filen ' + snapFile);
	}
}

function SnapperRequest(snapFile, modulePath, callbackField)
{
	var me = this;
	this.xmlhr = null;
	this.antallGangerHentesStatus = 0;
	this.callBackCustomized = snapperFilFerdig;
	this.callbackField = callbackField;
	
	this.modulePath = modulePath;
		
	this.downloadedFile = null;
	this.percentFinish = 0;	
	this.snapFile = snapFile;

	this.callbackX = function()
	{
		snapperCallBack(me.xmlhr, me.callBackCustomized, me);
	}

	this.startSnapping = startSnapping;
	this.checkIfFileToBeSnapped = checkIfFileToBeSnapped;	
}

function checkIfFileToBeSnapped()
{    
	return  this.snapFile.indexOf('http://') > -1 	|| 
			this.snapFile.indexOf('https://') > -1 	|| 
			this.snapFile.indexOf('ftp://') > -1;
}

function startSnapping()
{
	
	this.callbackField.value = 'Ok - processing - snapping started...';
	this.callbackField.style.color = '#ffffff'
	this.callbackField.style.backgroundColor = 'green';
	
	if(this.modulePath != null)
	{
		postCallBackAndCallBack('plugins/snapper/snap?snapfile=snapper://' + this.modulePath + ':' + this.snapFile, this);
	}
	else
	{	
		postCallBackAndCallBack('plugins/snapper/snap?snapfile=snapper://' + this.snapFile, this);
	}
}

function snapperCallBack(xmlresponse, sendDataTilFunksjonNarFerdig, snapperRequest)
{
	//alert('xmlresponse:' + xmlresponse);
	//alert('snapperRequest:' + snapperRequest);
	//alert('sendDataTilFunksjonNarFerdig:' + sendDataTilFunksjonNarFerdig);
	if(xmlresponse != null)
	{
		//alert('xmlresponse.readyState:' + xmlresponse.readyState);
	
		if (xmlresponse.readyState == 4) {
		
			//alert('xmlresponse.status:' + xmlresponse.status);
		
			if (xmlresponse.status == 200)  
			{		
				
				snapperRequest.downloadedFile = xmlresponse.responseText; 
				
				this.xmlhr = null;
				// Peker til en customized funksjon, som gjerne finnes i egen incklude-fil tilpasset gui. Alt som skjer i dette scriptet er generelt.
				sendDataTilFunksjonNarFerdig(snapperRequest);
				
			}
			else if (xmlresponse.status == 204)   // Ikke content (f.eks. ingen uleste ordre)
			{
				//doStop(); 
				//hideTilbakemeldingBoks();
			}
		}
	}
}

function snapperFilFerdig(snapperRequest)
{
	snapperRequest.callbackField.value = snapperRequest.downloadedFile;
	snapperRequest.callbackField.style.backgroundColor = '#ffffff';
	snapperRequest.callbackField.style.color = '#000000';
}


/** 
 * Ettersom det er problemer med en del nettlesere og drag'n drop fra andre nettsider,
 * har jeg laget en metode som sjekker felter merket med snapcheck="true" som fyrer
 * checkIfFileShouldBeSnappedAndThenSnap hvert 5. sekund.
 */
function startSnapperFieldControllListener(formToControll)
{
	this.formToControll = formToControll;

	this.reinvokeSnapperFieldController = function()
	{
			startSnapperFieldControllListener(formToControll);	
	}
	    
	var elem = getElementsByAttribute(formToControll, "input", "snapcheck", "true");             
	try
	{
		for(i=0; i < elem.length; i++)
		{   
			checkIfFileShouldBeSnappedAndThenSnap(elem[i].value, elem[i].getAttribute('modulepath'), elem[i]);
		}
		
		setTimeout ('reinvokeSnapperFieldController()', 5000);
	} 
	catch(e)
	{
		alert('e:' + e);
	}
}
