var qtb = {};

qtb.lang = "";
qtb.web_root = "/";
qtb.quetu_root = "/";
qtb.currentDialog = null;

qtb.isIE = false /*@cc_on || true @*/;
qtb.isIE6 = false;
/*@cc_on
if ( document.all && (navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1) )
	qtb.isIE6 = true;
qtb.restoreFocus = null;
@*/

qtb.getLanguage = function() {
	if ( qtb.lang == "" ) {
		qtb.lang = qtb.getCookie( "lang" );
		if ( qtb.lang == "" )
			qtb.lang = "es";
	}
	return qtb.lang;
}

qtb.setLanguage = function( lang ) {
	if ( lang == "en" || lang == "es" ) {
		qtb.setCookie( "lang", lang );
		document.location.reload( true );
	}
}

qtb.onPageLoaded = function() {
	qtb.wireInputHints();
	qtb.initSlideshow( "slideshow" );
}


qtb.emailPage = function() {
	if ( qtb.currentDialog == null ) {
		var func = "email_friend";
		var params = "&u=" + Utf8.urlEncode(""+document.location) + "&t=" + Utf8.urlEncode(""+document.title);
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.sendEmail = function() {
}

qtb.respondToAd = function( idStr ) {
	if ( qtb.currentDialog == null ) {
		var func = "respond_to_ad";
		var params = "&aid=" + idStr;
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.applyToJob = function( idStr ) {
	if ( qtb.currentDialog == null ) {
		var func = "apply_to_job";
		var params = "&id=" + idStr;
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.contactEmployer = function( idStr ) {
	if ( qtb.currentDialog == null ) {
		var func = "contact_employer";
		var params = "&id=" + idStr;
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.addToMyJobs = function( idStr ) {
	if ( qtb.currentDialog == null ) {
		var func = "add_my_jobs";
		var params = "&id=" + idStr;
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.removeFromMyJobs = function( idStr ) {
	if ( qtb.currentDialog == null ) {
		var func = "remove_my_jobs";
		var params = "&id=" + idStr;
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.contactExpert = function( idStr ) {
	if ( qtb.currentDialog == null ) {
		var func = "contact_expert";
		var params = "&id=" + idStr;
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.rateExpert = function( idStr ) {
	if ( qtb.currentDialog == null ) {
		var func = "rate_expert";
		var params = "&id=" + idStr;
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.showExpertComments = function( idStr ) {
	if ( qtb.currentDialog == null ) {
		var func = "expert_comments";
		var params = "&id=" + idStr;
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.reportAbuse = function( idStr, type ) {
	if ( qtb.currentDialog == null ) {
		var func = "report_abuse";
		var params = "&t=" + type + "&id=" + idStr;
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.wrongCategory = function( idStr, type ) {
	if ( qtb.currentDialog == null ) {
		var func = "wrong_category";
		var params = "&t=" + type + "&id=" + idStr;
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.showContactForm = function( type ) {
	if ( qtb.currentDialog == null ) {
		var func = "contact";
		var params = "&t=" + type;
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.addImageToAd = function() {
	if ( qtb.currentDialog == null ) {
		var limit = parseInt( document.ad_form.image_limit.value );
		var ids = document.ad_form.images.value.split( "," );
		if ( ids.length < limit ) {
			var func = "add_image";
			var params = "&us=classified";
			qtb.currentDialog = qtb.createDialog( func, params );
		}
	}
}

qtb.addImageToPortfolio = function() {
	if ( qtb.currentDialog == null ) {
		var func = "add_image";
		var params = "&us=portfolio";
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.removeImageFromAd = function( id ) {
	qtb.removeImageFromForm( id, "ad_form" );
}

qtb.removeImageFromPortfolio = function( id ) {
	qtb.removeImageFromForm( id, "profile_form" );
}

qtb.removeImageFromForm = function( id, form ) {
	var newids = "";
	var ids = document.forms[form].images.value.split( "," );
	for ( var n = 0; n < ids.length; n++ ) {
		if ( ids[n] == id )
			continue;
		if ( newids.length > 0 )
			newids += ",";
		newids += ids[n];
	}
	document.forms[form].images.value = newids;
	
	qtb.dom.rc( "image_thumbnail_list", "image_thumbnail_" + id );
	
	if ( form == "ad_form" ) {
		var el = document.getElementById( "add_image_btn" );
		if ( el ) {
			el.style.display = "inline";
		}
	}
}


qtb.addImageToProfile = function() {
	if ( qtb.currentDialog == null ) {
		var func = "add_image";
		var params = "&us=profile";
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.removeImageFromProfile = function() {
	document.profile_form.image.value = "";
	qtb.dom.rc( "profile_image_thumbnail", "image_thumbnail" );
}

qtb.onProfileImageUploaded = function( imageId, path ) {
	qtb.closeDialog();
	
	qtb.removeImageFromProfile();
	
	document.profile_form.image.value = imageId;
	
	// show the new image thumbnail:
	var el = document.getElementById( "profile_image_thumbnail" );
	if ( el ) {
		var thumb = qtb.dom.ce( "li", { id:"image_thumbnail"} );
		var html = '<a class="del_img_btn" href="javascript:qtb.removeImageFromProfile()"><img src="' + qtb.web_root + 'i/img/btn_trash_simple.gif" width="16" height="16" border="0" alt="" /></a>';
		html += '<img src="' + qtb.web_root + '_thumbs/' + path + '">';
		thumb.innerHTML = html;
		qtb.dom.ac( el, thumb );
	}
}

qtb.onImageUploaded = function( imageId, path, caption, usage ) {
	qtb.closeDialog();
	
	// update the form value:
	var form;
	if ( usage == "classified" )
		form = "ad_form";
	else if ( usage == "portfolio" )
		form = "profile_form";
	else if ( usage == "profile" ) {
		qtb.onProfileImageUploaded( imageId, path );
		return;
	}
	else
		return;
	
	var ids = document.forms[form].images.value;
	if ( ids.length > 0 )
		ids += ",";
	ids += imageId;
	document.forms[form].images.value = ids;
	
	// show the new image thumbnail:
	var el = document.getElementById( "image_thumbnail_list" );
	if ( el ) {
		var thumb = qtb.dom.ce( "li", { id:"image_thumbnail_" + imageId} );
		var html = '<a class="del_img_btn" href="javascript:qtb.removeImageFromAd(\'' + imageId + '\')"><img src="' + qtb.web_root + 'i/img/btn_trash_simple.gif" width="16" height="16" border="0" alt="" /></a>';
		html += '<img src="' + qtb.web_root + '_thumbs/' + path + '">';
		if ( caption.length > 0 )
			html += '<br />' + caption;
		thumb.innerHTML = html;
		qtb.dom.ac( el, thumb );
	}
	
	if ( usage = "classified" ) {
		var limit = parseInt( document.ad_form.image_limit.value );
		var el = document.getElementById( "add_image_btn" );
		if ( el ) {
			el.style.display = (ids.split(',').length >= limit) ? "none" : "inline";
		}
	}
}

qtb.onAdCategoryChanged = function() {
	var ctag = "";
	for ( var n = 0; n < document.forms.ad_form.c.length; n++ ) {
		var opt = document.forms.ad_form.c[ n ];
		if ( opt.selected ) {
			ctag = opt.value;
			break;
		}
	}
	var el = document.getElementById( "ad_form_table" );
	if ( el ) {
		el.className = "c" + ctag + "tbl";
	}
}

qtb.viewPortfolio = function( expertId ) {
}

qtb.closeDialog = function() {
	if ( qtb.currentDialog ) {
		/*@cc_on
		var fr = window.frames[ "dialog_iframe" ];
		fr.blur();
		if ( qtb.restoreFocus )
			qtb.restoreFocus.focus();
		else
			window.focus();
		@*/
		qtb.currentDialog.hide();
		if ( qtb.isIE6 ) {
			qtb.currentDialog.destroy();
			qtb.dom.rc( document.body, "std_dialog_ie" );
		}
		else {
			qtb.dom.rc( document.body, qtb.currentDialog.containerDiv );
			qtb.dom.rc( document.body, qtb.currentDialog.blockDiv );
		}
		qtb.currentDialog = null;
	}
}

qtb.closeEmbeddedDialog = function( doreload ) {
	if ( doreload )
		window.parent.location.reload();
	else
		window.parent.qtb.closeDialog();
}
qtb.showEmbeddedDialogLoading = function() {
	window.parent.qtb.showDialogLoading();
}

qtb.createDialog = function( func, params ) {
	var dialog = null;
	
	params += "&srcpg=" + escape( document.location.pathname );
	
	var html = "<div id='dialog_header'><a style='float:right' href='javascript:qtb.closeDialog()'>X</a>¿Que Tu Buscas?</div>";
	html += "<iframe id='dialog_iframe' onload='qtb.onDialogLoaded()' src='" + qtb.quetu_root + "service?w=" + func + params + "'></iframe>";
	html += "<div id='dialog_footer'><div id='dialog_loading' class='dialog_loading'><img src='" + qtb.web_root + "i/img/loading.gif' /></div></div>";
	
	/*@cc_on
	qtb.restoreFocus = document.activeElement;
	window.blur();
	@*/
	
	if ( qtb.isIE6 ) {
		if ( document.body.className != "yui-skin-sam" )
			document.body.className = "yui-skin-sam";
		var div = qtb.dom.ce( "div", { id:"std_dialog_ie" } );
		div.innerHTML = html;
		qtb.dom.ac( document.body, div );
		dialog = new YAHOO.widget.Panel(div,  
				{ width:"520px", height: "420px",
					fixedcenter:true,
					close:false, 
					draggable:false, 
					zindex:32760,
					modal:true,
					visible:false
				} 
		);
		dialog.render( document.body );
	}
	else {
		dialog = {};
		dialog.blockDiv = qtb.dom.ce( "div", { id:"std_dialog_b", className:"dialog_block" } );
		qtb.dom.ac( document.body, dialog.blockDiv );
		dialog.containerDiv = qtb.dom.ce( "div", { id:"std_dialog_c", className:"dialog_container" } );
		dialog.containerDiv.innerHTML = "<div class=\"dialog_position\">" + html + "</div>";
		qtb.dom.ac( document.body, dialog.containerDiv );
		dialog.show = function() {
			this.blockDiv.style.display = "block";
			this.containerDiv.style.display = "block";
		}
		dialog.hide = function() {
			this.blockDiv.style.display = "none";
			this.containerDiv.style.display = "none";
		}
	}
	
	dialog.show();
	return dialog;
}


qtb.showDialogLoading = function() {
	var el = document.getElementById( "dialog_loading" );
	if ( el ) {
		el.className = "dialog_loading";
	}
}

qtb.onDialogLoaded = function() {
	var el = document.getElementById( "dialog_loading" );
	if ( el ) {
		el.className = "dialog_loaded";
	}
	/*@cc_on
	var fr = window.frames[ "dialog_iframe" ];
	window.blur();
	fr.focus();
	@*/
}

qtb.newCaptcha = function() {
	var el = document.getElementById( "captcha_image" );
	if ( el ) {
		el.src = qtb.quetu_root + "captcha_image.jpg?t=" + Math.random();
	}
}

qtb.ratingClick = function(e) {

	document.rate_form.rating[0].checked = false;
	document.rate_form.rating[1].checked = true;
	
	qtb.ratingTrack(e);
	return;
	var el = document.getElementById( "rating_background" );
	if ( el ) {
		el.onmousemove = qtb.ratingTrack;
		el.onmouseup = qtb.ratingStopStack;
	}
}

qtb.ratingTrack = function(e) {
	var pos = qtb.getMouse(e);
	var el = document.getElementById( "rating_background" );
	if ( el ) {
		var rpos = qtb.dom.pos( el );
		//alert( "pos="+pos.x +","+pos.y+"; rpos="+rpos.x+","+rpos.y );
		if ( pos.x >= rpos.x && pos.x <= rpos.x + el.scrollWidth
				&& pos.y >= rpos.y && pos.y <= rpos.y + el.scrollHeight ) {
			var r = Math.ceil( ((pos.x - rpos.x) / el.scrollWidth) * 10 );
			document.rate_form.rating[1].value = r;
			el = document.getElementById( "rating_indicator" );
			if ( el ) {
				el.className = "rating_" + (r * 5);
			}
		}
	}
	return false;
}

qtb.ratingStopStack = function() {
	var el = document.getElementById( "rating_background" );
	if ( el ) {
		el.onmousemove = null;
		el.onmouseup = null;
	}
}

qtb.getMouse = function( e ) {
	var posx = 0;
	var posy = 0;
	if ( !e )
		var e = window.event;
	if ( e.pageX || e.pageY ) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if ( e.clientX || e.clientY ) {
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	return { x:posx, y:posy };
}


/**
 * Slideshows
 */
qtb.initSlideshow = function( id ) {
	var slideshow = document.getElementById( id );
	if ( slideshow ) {
		var current = -1;
		var imgs = slideshow.getElementsByTagName('img');
		if ( imgs.length > 1 ) {
			for ( var i = 1; i < imgs.length; i++ ) {
				imgs[i].xOpacity = 0;
				if ( current == -1 && imgs[i].className == "first" ) {
					imgs[i].style.display = 'block';
					imgs[i].xOpacity = .99;
					current = i;
				}
			}
			
			if ( current == -1 ) {
				current = 0;
				imgs[0].style.display = 'block';
				imgs[0].xOpacity = .99;
			}
			
			var desc = {
				mainDiv: slideshow,
				imgs: imgs,
				current: current
			}
		
			setTimeout( function() {qtb.slideshow_xfade(desc);}, 3000 );
		}
	}
}

qtb.slideshow_xfade = function( desc ) {
		
	var cOpacity = desc.imgs[ desc.current ].xOpacity;
	var nIndex = desc.imgs[ desc.current + 1 ] ? desc.current + 1 : 0;
	var nOpacity = desc.imgs[ nIndex ].xOpacity;

	cOpacity-=.05;
	nOpacity+=.05;

	desc.imgs[ nIndex ].style.display = 'block';
	desc.imgs[ desc.current ].xOpacity = cOpacity;
	desc.imgs[ nIndex ].xOpacity = nOpacity;

	setOpacity( desc.imgs[desc.current] );
	setOpacity( desc.imgs[nIndex] );

	if ( cOpacity <= 0 ) {
		desc.imgs[ desc.current ].style.display = 'none';
		desc.current = nIndex;
		setTimeout( function() {qtb.slideshow_xfade(desc);}, 3000 );
	}
	else {
		setTimeout( function() {qtb.slideshow_xfade(desc);}, 50 );
	}

	function setOpacity( obj ) {
		if ( obj.xOpacity > .99 ) {
			obj.xOpacity = .99;
			return;
		}
	
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = 'alpha(opacity=' + (obj.xOpacity*100) + ')';
	}
}


/**
 * Forms
 */
qtb.preSubmit = function() {
	var el = document.getElementById( "form_buttons" );
	if ( el ) {
		el.style.display = "none";
	}
	el = document.getElementById( "form_processing" );
	if ( el ) {
		el.style.display = "inline";
	}
	return true;
}
qtb.embeddedPreSubmit = function() {
	qtb.showEmbeddedDialogLoading();
	return qtb.preSubmit();
}

qtb.onNewExpertCategorySelected = function() {
}


/**
 * Messaging
 */
qtb.messageRequest = null;
qtb.selectedMsg = '';
qtb.selectMessage = function( id ) {
	var el = document.getElementById( "msg" + id );
	if ( el ) {
		
		var unread = el.className == "unread";
		var sent = el.getAttribute( "sent" ) == 1;
		
		// decrement unread count:
		if ( unread ) {
			var el2 = document.getElementById( "unread_count" );
			if ( el2 ) {
				var c = parseInt( el2.innerHTML );
				if ( c > 0 ) {
					c--;
					el2.innerHTML = c;
					if ( c == 0 ) {
						el2 = document.getElementById( "msgunread" );
						if ( el2 ) {
							el2.className = "zed";
						}
					}
				}
			}
		}
		el.className = "selected";
		
		// unselect previously selected message:
		if ( qtb.selectedMsg != '' ) {
			el = document.getElementById( "msg" + qtb.selectedMsg );
			if ( el )
				el.className = "notsel";
		}
		qtb.selectedMsg = id;
		
		// enable the reply & delete buttons:
		el = document.getElementById( "msg_reply_btn" );
		if ( el )
			el.className = sent ? "disabled" : "enabled";
		el = document.getElementById( "msg_delete_btn" );
		if ( el )
			el.className = "enabled";
		/*
		el = document.getElementById( "msg_send_btn" );
		if ( el )
			el.className = "enabled";
		*/
		
		// load the message:
		qtb.loadMessage();
		
	}
}

qtb.loadMessage = function() {

	// cancel out any pending requests:
	if ( qtb.messageRequest != null ) {
		YAHOO.util.Connect.abort( qtb.messageRequest );
		qtb.messageRequest = null;
	}
	
	// ask the server for details:
	// success callback:
	var handleSuccess = function( o ) {
	
		YAHOO.log( "qtb.loadMessage success", "info" );
		var result = {};
		
		if ( o.responseText != undefined ) {
			YAHOO.log( "qtb.loadMessage raw response: " + o.responseText, "debug" );
			result = eval( '(' + o.responseText + ')' );
		}
		var html = "";
		
		var elt = document.getElementById( "msgmessage" );
		if ( elt ) {
			elt.innerHTML = result.msgdisplay;
		}
	}
	
	// failure callback:
	var handleFailure = function( o ) {
		YAHOO.log( "qtb.loadMessage failed", "warn" );
		
		var elt = document.getElementById( "msgmessage" );
		if ( elt ) {
			elt.innerHTML = "<p>An error occurred while trying to access the message - please try again later.</p>";
		}
	}
	
	var el = document.getElementById( "msgmessage" );
	if ( el ) {
		el.innerHTML = "<img src='" + qtb.web_root + "i/img/loading.gif' />";
	}
	
	var callback = { success:handleSuccess, failure:handleFailure };
	
	var vars = "w=get_message&id=" + qtb.selectedMsg;
	
	qtb.messageRequest = YAHOO.util.Connect.asyncRequest( 'POST', qtb.quetu_root + 'service', callback, vars );

}

qtb.replyToMessage = function() {
	if ( qtb.selectedMsg == '' )
		return;
	if ( qtb.currentDialog == null ) {
		var func = "reply_to_msg";
		var params = "&id=" + qtb.selectedMsg;
		qtb.currentDialog = qtb.createDialog( func, params );
	}
}

qtb.deleteMessage = function() {
	if ( qtb.selectedMsg == '' )
		return;
		
	var el = document.getElementById( "msg" + qtb.selectedMsg );
	if ( el ) {
	
		var msg;
		if ( qtb.getLanguage() == "es" )
			msg = "¿Estás seguro de que desea eliminar este mensaje?";
		else
			msg = "Are you sure you want to delete this message?"
		if ( !confirm(msg) )
			return;
		
		// remove the subject line:
		qtb.dom.rc( "msglistbox", el );
		
		// disable the reply & delete buttons:
		el = document.getElementById( "msg_reply_btn" );
		if ( el )
			el.className = "disabled";
		el = document.getElementById( "msg_delete_btn" );
		if ( el )
			el.className = "disabled";
		el = document.getElementById( "msg_send_btn" );
		if ( el )
			el.className = "disabled";
	
		// clear out the message body:
		el = document.getElementById( "msgmessage" );
		if ( el ) {
			el.innerHTML = "";
		}
		
		// success callback:
		var handleSuccess = function( o ) {
			YAHOO.log( "qtb.deleteMessage success", "info" );
		}
		
		// failure callback:
		var handleFailure = function( o ) {
			YAHOO.log( "qtb.deleteMessage failed", "warn" );
		}
		
		var callback = { success:handleSuccess, failure:handleFailure };
		
		var vars = "w=file_message&fldr=trash&id=" + qtb.selectedMsg;
		
		qtb.messageRequest = YAHOO.util.Connect.asyncRequest( 'POST', qtb.quetu_root + 'service', callback, vars );
		
		qtb.selectedMsg = '';
		
	}
}

qtb.emptyTrash = function() {
	var msg;
	if ( qtb.getLanguage() == "es" )
		msg = "¿Estás seguro de que desea vaciar la papelera? Todos los mensajes de la papelera se suprimirá definitivamente.";
	else
		msg = "Are you sure you want to empty your trash folder?  All messages in the trash will be permanently deleted."
	if ( !confirm(msg) )
		return;
	
	// success callback:
	var handleSuccess = function( o ) {
		YAHOO.log( "qtb.emptyTrash success", "info" );
		document.location.reload( true );
	}
	
	// failure callback:
	var handleFailure = function( o ) {
		YAHOO.log( "qtb.emptyTrash failed", "warn" );
	}
	
	var callback = { success:handleSuccess, failure:handleFailure };
	
	var vars = "w=empty_trash";
	
	qtb.messageRequest = YAHOO.util.Connect.asyncRequest( 'POST', qtb.quetu_root + 'service', callback, vars );
}


/**
 * Form field hints
 */
qtb.wireInputHints = function() {
	var fields = { searchterm:{pswd:false}, searchzip:{pswd:false}, login_email:{pswd:false}, login_pswd:{pswd:true} };
	for ( var id in fields ) {
		var field = fields[ id ];
		qtb.wireInputHint( id, field.pswd );
	}
}

qtb.wireInputHint = function( id, isPassword ) {
	var el = document.getElementById( id );
	if ( el ) {
		el.setAttribute( "isPassword", isPassword ? "1" : "0" );
		if ( el.value == "" || el.value == el.getAttribute("hint") ) {
			if ( el.getAttribute("isPassword") == "1" ) {
				if ( qtb.isIE )
					el.className = qtb.getLanguage() + "_pswd_instruct";
				else {
					el.type = 'text';
					el.value = el.getAttribute("hint");
				}
			}
			else
				el.value = el.getAttribute("hint");
			el.style.color = "#999999";
			el.showingInstruction = true;
		}
		else {
			if ( el.getAttribute("isPassword") == "1" ) {
				if ( qtb.isIE )
					el.className = "";
				else {
					el.type = 'password';
				}
			}
			el.style.color = "#000000";
			el.showingInstruction = false;
		}
		el.onfocus = function() {
			if ( this.showingInstruction ) {
				this.value = "";
				this.showingInstruction = false;
				this.style.color = "#000000";
				if ( this.getAttribute("isPassword") == "1" ) {
					if ( qtb.isIE )
						this.className = "dummy";
					else {
						this.type = 'password';
						if ( navigator.userAgent.indexOf("Safari") != -1 ) {
							/* changing the input type on Safari screws up the input focus... */
							this.focus();
							this.select();
						}
					}
				}
			}
		}
		el.onblur = function() {
			if ( this.value == "" ) {
				this.showingInstruction = true;
				this.style.color = "#999999";
				if ( this.getAttribute("isPassword") == "1" ) {
					if ( qtb.isIE ) {
						this.className = qtb.getLanguage() + "_pswd_instruct";
					}
					else {
						this.type = 'text';
						this.value = this.getAttribute("hint");
					}
				}
				else
					this.value = this.getAttribute("hint");
			}
		}
	}
}


qtb.upload = function() {
	//return qtb.Uploader.submit( document.forms.upload_form, {'onStart' : upload_started, 'onComplete' : upload_complete} );
}

qtb.Uploader = {

	frame : function(c) {
		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="FOFUploader.loaded(\''+n+'\')"></iframe>';
		document.body.appendChild(d);
		var i = document.getElementById(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}
		return n;
	},

	submit : function(f, c) {
		var frameName = FOFUploader.frame( c );
		f.setAttribute( 'target', frameName );
		f.target = frameName;
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},

	loaded : function(id) {
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		if (d.location.href == "about:blank") {
			return;
		}
		if (typeof(i.onComplete) == 'function') {
			var image = "";
			var error = "";
			var elt = d.getElementById( "image" );
			if ( elt )
				image = elt.innerHTML;
			var elt = d.getElementById( "error" );
			if ( elt )
				error = elt.innerHTML;
			i.onComplete( image, error );
		}
	}
};


/**
 * Pagination
 */
qtb.showPageStd = function( from ) {
	document.forms.pagination.f.value = from;
	document.forms.pagination.submit();
}
qtb.showSearchResults = function( from ) {
	document.forms.search.f.value = from;
	if ( document.forms.search.q.value == document.forms.search.q.getAttribute("hint") )
		document.forms.search.q.value = "";
	document.forms.search.submit();
}
qtb.showAll = function() {
	document.forms.search.q.value = "";
	document.forms.search.f.value = 0;
	document.forms.search.submit();
}
qtb.preSubmitSearch = function() {
	document.forms.search.f.value = 0;
	if ( document.forms.search.q.value == document.forms.search.q.getAttribute("hint") )
		document.forms.search.q.value = "";
	var ctag = "";
	for ( var n = 0; n < document.forms.search.c.length; n++ ) {
		var opt = document.forms.search.c[ n ];
		if ( opt.selected ) {
			ctag = opt.value;
			break;
		}
	}
	if ( ctag != document.forms.search.lc.value )
		document.forms.search.action = qtb.quetu_root + document.forms.search.area.value + ctag;
	return true;
}
qtb.sort = function( col ) {
	document.forms.search.sortby.value = col;
	document.forms.search.f.value = 0;
	document.forms.search.submit();
}

qtb.preSubmitLogin = function() {
	if ( document.forms.login.email.value == document.forms.login.email.getAttribute("hint") )
		document.forms.login.email.value = "";
	if ( document.forms.login.pswd.value == document.forms.login.pswd.getAttribute("hint") )
		document.forms.login.pswd.value = "";
	return true;
}


/**
 * DOM Utilities
 */
qtb.dom = {};

// createElement
qtb.dom.ce = function( nodeType, attrs ) {
    var el = document.createElement(nodeType);
    for (var i in attrs){ el[i] = attrs[i];}
    return el;
};

// appendChild
qtb.dom.ac = function( parent, child ) {
    if (typeof parent == 'string'){ parent = qtb.dom.id(parent); }
    if (typeof child == 'string'){ child = qtb.dom.id(child); }
    if (!parent || !child){ return false; }

    parent.appendChild(child);
    return child;
};

// removeChild
qtb.dom.rc = function( parent, child ) {
    if (typeof parent == 'string'){ parent = qtb.dom.id(parent); }
    if (typeof child == 'string'){ child = qtb.dom.id(child); }
    if (!parent || !child){ return false; }

    parent.removeChild(child);
    return child;
};

qtb.dom.id = function(str){ return document.getElementById(str);};

qtb.dom.pos = function( elt ) {
    if (typeof elt == 'string'){ elt = qtb.dom.id(elt); }
	var curleft = curtop = 0;
	if (elt.offsetParent) {
		do {
			curleft += elt.offsetLeft;
			curtop += elt.offsetTop;
		} while (elt = elt.offsetParent);
	}
	return { x:curleft, y:curtop };
}


/**
 * cookies
 */
qtb.getCookie = function( cookieName ) {
	var theCookie = "" + document.cookie;
	var ind = theCookie.indexOf( cookieName );
	if ( ind == -1 || cookieName == "" )
		return "";
	var ind1 = theCookie.indexOf( ';', ind );
	if ( ind1 == -1 )
		ind1 = theCookie.length;
	return unescape( theCookie.substring(ind + cookieName.length + 1, ind1) );
}

qtb.setCookie = function( key, val ) {
	document.cookie = key + "=" + escape(val) + ";PATH=/";
}


/**
*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Utf8 = {
 	urlEncode: function (string) {
 		return escape( this.encode(string) );
 	},
 	urlDecode: function (string) {
 		return this.decode( unescape(string) );
 	},
 	
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
};


/* Macromedia Stuff */
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


window.onload = qtb.onPageLoaded;
