/*=================================================================================
XAJAX_UPLOAD.JS
VERSION:1.0.1
DESCRIPTION:  THIS IS THE REQUIRED JAVASCRIPT FILE FOR XAJAX UPLOAD:UPLOADER WITH PROGRESS BAR VERSION:1.0.1
AUTHOR: JEREMY M. DILL
REQUIREMENTS:  SEE UPLOAD_CONFIG_INC.PHP
=================================================================================
* Copyright (c) 2007 JEREMY M. DILL (trydobe.com)
* This work is licensed under a Creative Commons Attribution-Share Alike 3.0  License
* http://creativecommons.org/licenses/by-sa/3.0/
* AUTHOR'S NOTE: THANKS TO THE OTHER DEVELOPERS OUT THERE WHO ESTABLISHED THE FOUNDATIONS OF THIS WORK.
									BITS AND PIECES OF CONTRIBUTIONS FROM VARIOUS SOURCES INCLUDING THE FOLLOWING:
									http://www.raditha.com/megaupload/
									http://lists.geeklog.net/pipermail/geeklog-cvs/2005-June/000525.html
									http://labs.beffa.org/w2box/demo/
									http://tomas.epineer.se/archives/3
									http://jszen.blogspot.com/2005/05/secure-iframe-gotcha.html
									http://obokaman.obolog.com/mensaje/1596
									http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
									(sorry if any of these links become invalid)
================================================================================= */
var frameSet=false;

//GLOBAL DECLARATIONS
var cgiPath,blankHTMLPath,language;
var locallang = new Array();

function ams_libStartOver(formId) {
	// set iframe parameters, if debug is activated
	var iframe = document.createElement("iframe");
	iframe.setAttribute("src", blankHTMLPath);
	iframe.setAttribute("id", formId+"iframe");
	iframe.setAttribute("name", formId+"iframe");
	if($('#'+formId+' input[name=cgidebug]')[0].value == 1) {
	iframe.setAttribute("height", "150px");
	iframe.setAttribute("width", "100%");
	iframe.setAttribute("frameBorder", "1");
	}
	else {
	iframe.setAttribute("height", "0");
	iframe.setAttribute("width", "0");
	iframe.setAttribute("frameBorder", "0");
	}
	if( $('#'+formId)[0].appendChild(iframe) ) {
		frameload = $('#'+formId+' input[name=frameload]')[0];
		frameload.value = true;
		$('#'+formId)[0].target = formId+"iframe";
	}
	// FIX FOR IE.  ATTRIBUTES FOR NAME AND ID ARE NOT SET USING DOM.
	window.frames[window.frames.length-1].id=iframe.id;
	window.frames[window.frames.length-1].name=iframe.name;
	// END FIX
}

// SUBMIT THE FORM AND START MONITORING WITH XAJAX
function ams_libUpload(formId, onComplete) {
	// gibt es etwas hochzuladen?
	files = $('#'+formId+' :file');
	uploads = new Array();
	for (i=0; i<files.length; i++) {
		if (files[i].value) {
			uploads.push(files[i]);
		}
	}
	if (uploads.length > 0) {
		$('#'+formId+'uploadact').css('width', '0%');
		$('#'+formId+'uploadcancel').css('display', 'block');
		$('#'+formId+'uploadbar').css('display', 'block');
		$('#'+formId+'uploadinfo').css('display', 'block');
		$('#'+formId+'uploadinfo').text(locallang[language]['upload.uploadInitiated']);
		sendButton = $('#'+formId+'send')[0];
		if (sendButton) {
			sendButton.disabled=true;
		}

	form = $('#'+formId);
		if (form) {
			form.attr('method', 'post');
			form.attr('enctype', 'multipart/form-data');
			form.attr('encoding', 'multipart/form-data');
			form.attr('action', cgiPath+"?sid="+$('#'+formId+' input[name=sid]')[0].value
													+"&amp;maxfile="+$('#'+formId+' input[name=maxfilesize]')[0].value
													+"&amp;cgidebug="+$('#'+formId+' input[name=cgidebug]')[0].value
													+"&amp;speed="+$('#'+formId+' input[name=speed]')[0].value
							);
			form[0].submit();
		ams_libGetProgress(onComplete, formId, 0);
		}
		else {
			alert('no form');
		}
	}
	else {
		//nichts hochzuladen
		eval(onComplete);
	}
	return false;
}

// CALL THE XAJAX UPLOAD HANDLER.
function ams_libGetProgress(onComplete, formId, count) {
	if($('#'+formId+' input[name=frameload]')[0].value == 'true') {
		count += 1;
		ams_libuploadHandler($('#'+formId+' input[name=sid]')[0].value, count, onComplete, formId);
	}
	else {
	ams_libuploadHandler($('#'+formId+' input[name=sid]')[0].value, -1, onComplete, formId);  // CANCELLED BY USER
	}
}


function ams_libCheckFileInput(elem) {
	if(elem) {
		// CHECK FOR APPROPRIATE EXTENSIONS,
		filetypes = '';
		form = ams_libGetForm(elem);
		if (form) {
			filetypes = $('#'+form.id+' input[name='+elem.name+'filetypes]')[0].value;
		}
		if (filetypes){
			filepath = elem.value.split("\\");
			filename = filepath[filepath.length-1];
			extensionpath = filename.split(".");
			extension = extensionpath[extensionpath.length-1];
			ftypes=","+filetypes.toLowerCase()+",";
			tst=ftypes.indexOf(","+extension.toLowerCase()+",");
			if (tst == -1 ) {
				elem.value = '';
				messageTag = $('#'+form.id+' input[name='+elem.name+']').parent().next()[0];
				if (messageTag) {
					messageTag.innerHTML = locallang[language]['upload.errorFileTypeForFileName'].replace(/%s/, filename);
					messageTag.style.display = 'block';
				}
				return;
			}
		}
		// Pr�fung erfolgreich durchlaufen, daher eventuelle Fehlermeldung ausblenden
		messageTag = $('#'+form.id+' input[name='+elem.name+']').parent().next()[0];
		if (messageTag) {
			messageTag.style.display = 'none';
		}
	}
}

function ams_libGetForm(elem) {
	if (elem) {
		if (elem.tagName && (elem.tagName.toUpperCase() == "FORM")) {
			return elem;
		}
		else {
			if (elem.parentNode) {
				return ams_libGetForm(elem.parentNode);
			}
			else {
				return '';
			}
		}
	}
	else {
		return '';
	}
}


// CANCEL BUTTON ACTION
function ams_libCancel(formId) {
	$('#'+formId+' input[name=frameload]')[0].value = false;
	return false;
}
