function input_focus() {
	if( document.location.hash != "" ) {
		return;
	}
	f = document.getElementsByTagName( "input" );
	for( i = 0; i < f.length; i++ ) {
		if( f[i].type == "text" ) {
			f[i].focus();
			return;
		}
	}
}

function focus( field ) {
	var v = field.value;
	var r = field.createTextRange();
	r.moveStart( "character", v.length );
	r.select();
}

function get_input( desc ) {
	do {
		input = prompt( desc, "" );
	} while( input == "" );
	if( input == null || input == "undefined" ) {
		return null;
	}
	return input;
}

function add_link( field ) {
        do {
        	URL = prompt( "URL:", "http://" );
        } while( URL == "http://" )
	if( URL == null || URL == "undefined" ) {
		return;
	}
	desc = get_input( "Teksti:" );
	if( desc == null ) {
		return;
	}
	field.value += "<a href='" + URL + "'>" + desc + "</a> ";
	focus( field );
}

function add_tags( field, tag ) {
	string = get_input( "Sana/lause:" );
	if( string == null ) {
		return;
	}
	field.value += "<" + tag + ">" + string + "</" + tag + "> ";
	focus( field );
}

function add_break( field ) {
	field.value += "[b]\n\n";
	focus( field );
}

function trim( string ) {
	return string.replace( /^\s+/g, "" ).replace( /\s+$/g, "" );
}

function is_valid_email( address ) {
	a = address.indexOf( "@" )
	d = address.lastIndexOf( "." )
	if( a < 1 || d - a < 2 ) { 
		return false;
	}
	return true;
}

function validate( form ) {
	e = form.elements;
	for( i = 0; i < e.length; i++ ) {
		if( e[i].type == "text" || e[i].type == "password" || e[i].type == "textarea" ) {
			if( e[i].name == "email" && !is_valid_email( e[i].value ) ) {
				alert( "Syötä validi sähköpostiosoite!" );
				return false;
			}
			if( trim( e[i].value ) == "" && e[i].className != "notm" ) {
				alert( "Täytä kaikki kentät!" );
				return false;
			}
		}
	}
	return true;
}

function validate_eu( form, su ) {
        e = form.elements;
        for( i = 0; i < e.length; i++ ) {
                if( e[i].type == "text" || e[i].type == "textarea" ) {
                        if( e[i].name == "email" && !is_valid_email( e[i].value ) ) {
                                alert( "Syötä validi sähköpostiosoite!" );
                                return false;
                        }
                        if( trim( e[i].value ) == "" ) {
                                alert( "Täytä kaikki kentät!" );
                                return false;
                        }
                }
        }
        if( form.password.value != form.password2.value ) {
                alert( "Salasanat eivät täsmää!" );
                return false;
        }
        if( !su && form.user_group.value == 2 ) {
                return confirm( "Oletko varma, että haluat luopua pääkäyttäjän oikeuksista? Sinut kirjataan ulos muutoksen yhteydessä." );
        }
        return true;
}

function validate_nu( form ) {
	validate( form );
	if( form.password.value != form.password2.value ) {
		alert( "Salasanat eivät täsmää!" );
		return false;
	}
	if( form.user_group.value == 2 ) {
		return confirm( "Oletko varma, että haluat luopua pääkäyttäjän oikeuksista? Sinut kirjataan ulos muutoksen yhteydessä." );
	}		 
        return true;	
}

function mail( address ) {
	eval( "address=String.fromCharCode(" + address + ")" );
	document.location.href = "mailto:" + address;
}

function confirm_relo( text, url ) {
	ok = confirm( text );
	if( ok ) {
		document.location.href = url;
	} else {
		return false;
	}
}

function add_upload_input( div_id ) {
	input = document.createElement( "input" );
	input.setAttribute( "type", "file" );
	input.setAttribute( "name", "images[]" );
	input.setAttribute( "onclick", "add_upload_input('" + div_id + "')" );
	document.getElementById( div_id ).appendChild( input );
	document.getElementById( div_id ).appendChild( document.createElement( "br" ) );
        input = document.createElement( "input" );
        input.setAttribute( "type", "text" );
        input.setAttribute( "name", "new_description[]" );
	input.setAttribute( "maxsize", "512" );
	input.setAttribute( "class", "notm" );
        input.setAttribute( "onclick", "add_upload_input('" + div_id + "')" );
  	document.getElementById( div_id ).appendChild( input );
        document.getElementById( div_id ).appendChild( document.createElement( "br" ) );
}

function show_image( img_id, width, height ) {
	window.open( "view/image.php?id=" + img_id, "iw", "width=" + width + ",height=" + height +",menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no" );
}

function toggle( div_id ) {
	eval( "d = document.getElementById( '" + div_id + "' ).style.display" );
	eval( "document.getElementById('" + div_id + "').style.display='" + ( d == "" ? "none" : "" ) + "'" );
}

function preview( type ) {
	if( type == "post" ) {
		p = "preview_post";
	} else {
		p = "preview_page";
	}
	window.open( "view/" + p + ".php", "pw", "" );
}

