function showImage(url, width, height) {
	today= new Date();
	seconds= today.getSeconds();
	width = width + 10;
	height = height + 10;	
	var popup = window.open(url,'Picture','resizable=1,scrollbars=0,statusbar=0,toolbar=0,width='+width+',height='+height+',top=80,left=80');
	popup.focus();
	return false;
}

function SetForm(formname, fieldname, fieldvalue, submit){
	document.forms[formname].elements[fieldname].value	= fieldvalue;
	if (submit)
	{
		document.forms[formname].submit();
	}
	return false;
}

function printAd(url){
	var print = window.open(url, 'Print Ad', 'width=800,height=600,scrollbars=yes');
	print.focus();
	return false;
}

function sendLink(url){
	var send = window.open(url,'Send Link','width=500,height=430,scrollbars=no');
	send.focus();
	return false;
}

function mouse_event(obj, newClass) {
    obj.className = newClass;
	return true;
}

function SortList(sForm, sField, sDirection, bSubmit)
{
	var sPrevSort = document.forms[sForm].field.value;
	var sPrevDirection = document.forms[sForm].direction.value;
	if (sField == sPrevSort)
	{
		if (sPrevDirection=='ASC')
		{
			document.forms[sForm].direction.value = 'DESC';
		}
		else
		{
			document.forms[sForm].direction.value = 'ASC';
		}
	}
	else
	{
		document.forms[sForm].field.value = sField;
		document.forms[sForm].direction.value = sDirection;
	}
	if (bSubmit)
	{
		document.forms[sForm].submit();
	}
	return false;
}

function MultiSelector( list_target, max ){

	// Where to write the list
	this.list_target = list_target;
	// How many elements?
	this.count = 0;
	// How many elements?
	this.id = 0;
	// Is there a maximum?
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};
	
	/**
	 * Add a new file input element
	 */
	this.addElement = function( element ){

		// Make sure it's a file input element
		if( element.tagName == 'INPUT' && element.type == 'file' ){

			// Element name -- what number am I?
			element.name = 'adphoto_' + this.id++;

			// Add reference to this object
			element.multi_selector = this;

			// What to do when a file is selected
			element.onchange = function(){

				// New file input
				var new_element = document.createElement( 'input' );
				new_element.type = 'file';
				new_element.className = 'FormUploadButton';

				// Add new element
				this.parentNode.insertBefore( new_element, this );

				// Apply 'update' to element
				this.multi_selector.addElement( new_element );

				// Update list
				this.multi_selector.addListRow( this );

				// Hide this: we can't use display:none because Safari doesn't like it
				this.style.position = 'absolute';
				this.style.left = '-1000px';
				this.style.display = 'none';

			};
			// If we've reached maximum number, disable input element
			if( this.max != -1 && this.count >= this.max ){
				element.disabled = true;
			};

			// File element counter
			this.count++;
			// Most recent element
			this.current_element = element;
			
		} else {
			// This can only be applied to file input elements!
			alert( 'Error: not a file input element' );
		};

	};

	/**
	 * Add a new row to the list of files
	 */
	this.addListRow = function( element ){

		// Row

		var new_row = document.createElement( 'tr' );
		var td_1 = document.createElement('td');
		var td_2 = document.createElement('td');
		//new_row.style.marginBottom = 5 + 'px';
		// Delete button
		var new_row_button = document.createElement( 'input' );
		new_row_button.type = 'button';
		new_row_button.value = 'Delete';
		new_row_button.className = 'FormUploadButton';
		// References
		new_row.element = element;

		// Delete function
		new_row_button.onclick= function(){

			// Remove element from form

			this.parentNode.parentNode.element.parentNode.removeChild( this.parentNode.parentNode.element );

			// Remove this row from the list
			this.parentNode.parentNode.parentNode.removeChild( this.parentNode.parentNode );

			// Decrement counter
			this.parentNode.parentNode.element.multi_selector.count--;

			// Re-enable input element (if it's disabled)
			this.parentNode.parentNode.element.multi_selector.current_element.disabled = false;

			// Appease Safari
			//    without it Safari wants to reload the browser window
			//    which nixes your already queued uploads
			return false;
		};

		// Set row value
		td_1.innerHTML = element.value + ' ';

		// Add button
		td_2.appendChild( new_row_button );
		new_row.appendChild(td_1);
		new_row.appendChild(td_2);
		// Add it to the list
		var tr = ""; 
		var browser = navigator.appName;
		if(browser == "Microsoft Internet Explorer"){
    		tr = this.list_target.childNodes[0];
    	}
    	else{
    		tr = this.list_target;
    	}
		tr.appendChild( new_row );
		
	};

};

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent)
		{
			if (obj.style.left!='')
			{
				return [curleft, curtop];
			}
			else
			{
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		}
	}
	return [curleft, curtop];
}

function openMenu(){
	var menu = $('my_menu');
	if(!menu) return true;
	
	if('block' != menu.style.display){
		var open = $('menu_open');
		if(!open) return true;
		var pos = findPos(open);
		menu.style.left = (pos[0] - 25) + 'px';
		menu.style.top = (pos[1] + 35) + 'px';
		menu.style.display = 'block';
	} else {
		menu.style.display = 'none';
	}
	return true;
}

function getActiveBlock(id){
	var block = $('ads_block');
	var cl = block.getElementsByTagName('div');
	for(var i=0; i<cl.length; i++){
		if(cl[i].className == 'ad_list_act'){
		 	if(cl[i].id != 'ad_' + id){
				cl[i].className = 'ad_list';
			} else 
				return false;
		}
	}
	var active = $('ad_' + id);
	if(!active) return false;
	active.className = 'ad_list_act';

	return true;
}