/* standard small functions */
function $m(quem){
 return document.getElementById(quem)
}
function micox_isWebKit(){
	return RegExp("AppleWebKit/").test(navigator.userAgent);
}
function micox_remove(quem){
 quem.parentNode.removeChild(quem);
}
function micox_addEvent(obj, evType, fn){
 // elcio.com.br/crossbrowser
    if (obj.addEventListener)
        obj.addEventListener(evType, fn, true)
    if (obj.attachEvent)
        obj.attachEvent("on"+evType, fn)
}
function micox_removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, fn );
  } else {
    obj.removeEventListener( type, fn, false ); }
}


function enableBtn_new (btnBox, btn)
{
	$(btn).show ();
	$(btnBox).removeChild ($(btn + '_disable'));
}

function validateFields (form, imgCat_id)
{
	var fields_error = false;
	var allowed_files = 'jpg,gif,pdf,doc,docx';


	if (form.ImageTitle != undefined)
	{
		var title = form.ImageTitle.value;	

		if (title == '')
		{
			fields_error = true;
			$('img_title_error_' + imgCat_id).innerHTML = 'Bitte eine &Uuml;berschrift eingeben.';
		}
		else $('img_title_error_' + imgCat_id).innerHTML = ''
	}
		

	var file = form.ImageFile.value		
	file = file.lastIndexOf ('\\') == -1 ? file : file.substr (file.lastIndexOf ('\\') + 1);
	
	if (file == '')
	{
		fields_error = true;
		$('img_file_error_' + imgCat_id).innerHTML = 'Bitte eine Datei ausw&auml;hlen.';
	}
	else
	{
		if (file.lastIndexOf ('.') == -1)
		{
			fields_error = true;
			$('img_file_error_' + imgCat_id).innerHTML = 'Bitte eine g&uuml;ltige Datei ausw&auml;hlen.';
		}
		else
		{
			var ext = file.substr (file.lastIndexOf ('.') + 1).toLowerCase ();
			
			if (allowed_files.indexOf (ext) == -1)
			{
				fields_error = true;
				$('img_file_error_' + imgCat_id).innerHTML = 'Bitte einen g&uuml;ltigen Dateityp ausw&auml;hlen.';
			}
			else $('img_file_error_' + imgCat_id).innerHTML = '';
		}
	}


	return fields_error;
}

/* THE UPLOAD FUNCTION */
function micoxUpload (form, url_action, id_element, html_show_loading, html_error_http, imgCat_id)
{
	/******
	* Used by: /images/add
	*
	* micoxUpload - Submit a form to hidden iframe. Can be used to upload
	* Use but dont remove my name. Creative Commons.
	* Vers�o: 1.0 - 03/03/2007 - Tested no FF2.0 IE6.0 e OP9.1
	* Author: Micox - N�iron JCG - elmicoxcodes.blogspot.com - micoxjcg@yahoo.com.br
	* Parametros:
	* form - the form to submit or the ID
	* url_action - url to submit the form. like action parameter of forms.
	* id_element - element that will receive return of upload.
	* html_show_loading - Text (or image) that will be show while loading
	* html_error_http - Text (or image) that will be show if HTTP error.
	*******/

	var erro="";
	var detectWebKit = micox_isWebKit();

	//testing if 'form' is a html object or a id string
	form = typeof(form)=="string"?$m(form):form;
 
	if(form==null || typeof(form)=="undefined"){ erro += "The form of 1st parameter does not exists.\n";}
	else if(form.nodeName.toLowerCase()!="form"){ erro += "The form of 1st parameter its not a form.\n";}

	if($m(id_element)==null){ erro += "The element of 3rd parameter does not exists.\n";}
	if(erro.length>0)
	{
		alert("Error in call micoxUpload:\n" + erro);
		return;
	}

		
	// upload n save data
	if (!validateFields (form, imgCat_id))
	{
		//creating the iframe
		var iframe = document.createElement("iframe");
		iframe.setAttribute("id","micox-temp");
		iframe.setAttribute("name","micox-temp");
		iframe.setAttribute("width","0");
		iframe.setAttribute("height","0");
		iframe.setAttribute("border","0");
		iframe.setAttribute("style","width: 0; height: 0; border: none;");
	 
		//add to document
		form.parentNode.appendChild(iframe);
		if (navigator.appName.indexOf ('Internet Explorer') != -1) window.frames['micox-temp'].name="micox-temp"; //ie sucks
	 
		//add event
		var carregou = function ()
		{ 
			micox_removeEvent( $m('micox-temp'), "load", carregou);

			// loaded ----------------
			enableBtn_new ("new_imgBtnBox_" + imgCat_id, "new_imgBtn_" + imgCat_id); 
			loadImages (imgCat_id);
			// --------------------------
			
			var cross = "javascript: ";
			cross += "window.parent.$m('" + id_element + "').innerHTML = document.body.innerHTML; void(0); ";

			form.parentNode.removeChild (form);
			$m(id_element).innerHTML = html_error_http;
			$m('micox-temp').src = cross;

			//del the iframe
			if(detectWebKit){ micox_remove($m('ajax-temp')); }
			else { setTimeout(function(){ micox_remove($m('micox-temp'))}, 250);}
		}

		micox_addEvent( $m('micox-temp'), "load", carregou)
	 
		//properties of form
		form.setAttribute("target", "micox-temp");
		form.setAttribute("action", url_action);
		form.setAttribute("method", "post");
		form.setAttribute("enctype", "multipart/form-data");
		form.setAttribute("encoding", "multipart/form-data");

		//submit
		form.submit();
	 
		//while loading
		if(html_show_loading.length > 0)
		{
			//$m(id_element).innerHTML = html_show_loading;
			setBtnCaption (html_show_loading[0], html_show_loading[1]);
		}
	}
}


function micoxUpload_2 (form, url_action, id_element, html_show_loading, html_error_http, imgCat_id, img_id)
{
	/******
	* Used by: /images/edit_file
	* (made a copy because at the beginning needed many changes/excpetions)
	*******/
	

	var erro="";
	var detectWebKit = micox_isWebKit();

	//testing if 'form' is a html object or a id string
	form = typeof(form)=="string"?$m(form):form;
 
	if(form==null || typeof(form)=="undefined"){ erro += "The form of 1st parameter does not exists.\n";}
	else if(form.nodeName.toLowerCase()!="form"){ erro += "The form of 1st parameter its not a form.\n";}

	if($m(id_element)==null){ erro += "The element of 3rd parameter does not exists.\n";}
	if(erro.length>0)
	{
		alert("Error in call micoxUpload:\n" + erro);
		return;
	}

		
	// upload n save data
	if (!validateFields (form, img_id))
	{
		//creating the iframe
		var iframe = document.createElement("iframe");
		iframe.setAttribute("id","micox-temp");
		iframe.setAttribute("name","micox-temp");
		iframe.setAttribute("width","0");
		iframe.setAttribute("height","0");
		iframe.setAttribute("border","0");
		iframe.setAttribute("style","width: 0; height: 0; border: none;");
	 
		//add to document
		form.parentNode.appendChild(iframe);
		if (navigator.appName.indexOf ('Internet Explorer') != -1) window.frames['micox-temp'].name="micox-temp"; //ie sucks
	 
		//add event
		var carregou = function ()
		{ 
			micox_removeEvent( $m('micox-temp'), "load", carregou);

			// loaded/completed ----------------
			//updateImg_loaded (html_show_loading[0], html_show_loading[1])
			Effect.toggle ('updateImgBox_' + img_id, 'blind'); // <-------------------
			loadImages (imgCat_id);
			// --------------------------
			
			var cross = "javascript: ";
			cross += "window.parent.$m('" + id_element + "').innerHTML = document.body.innerHTML; void(0); ";

			form.parentNode.removeChild (form);
			$m(id_element).innerHTML = html_error_http;
			$m('micox-temp').src = cross;

			//del the iframe
			if(detectWebKit){ micox_remove($m('ajax-temp')); }
			else { setTimeout(function(){ micox_remove($m('micox-temp'))}, 250);}
		}

		micox_addEvent( $m('micox-temp'), "load", carregou)
	 
		//properties of form
		form.setAttribute("target", "micox-temp");
		form.setAttribute("action", url_action);
		form.setAttribute("method", "post");
		form.setAttribute("enctype", "multipart/form-data");
		form.setAttribute("encoding", "multipart/form-data");

		//submit
		form.submit();
	 
		//while loading
		if(html_show_loading.length > 0)
		{
			//setBtnCaption (html_show_loading[0], html_show_loading[1]);
			updateImg_saving (html_show_loading[0], html_show_loading[1])
		}
	}
}
