/* $Id: neo.js,v 1.8 2007-10-02 20:55:45 perhans Exp $
 * ----------------------------------------------------------------------
 * General javascript container
 */

function pop_up_win (script, win_id) {
    pop_up_win_custom( script, win_id, 760, 760 );
}

function pop_up_win_custom (script, win_id, height, width) {
    window.open( script, win_id, 'width=' + width + ',height=' + height +
                 ',screenX=1,screenY=1,top=1,left=1,resizable=yes,' +
                 'scrollbars=1,scrolling=yes');
}


/* Extract some integer from the pdflib generated height and width - and
 * scale with individual factors to cope with unknown size of Acrobat's
 * panels, margins etc.
 */
function pdf_window (script, height, width) {
    height = extract_integer( height );
    width = extract_integer( width );

    height = Math.ceil( 1.25 * height );
    width = Math.ceil( 1.4 * width );

    pop_up_win_custom( script, '', height, width );
}


function extract_integer (str) {
    str = str.replace( /^\W+/, '' );
    str = str.replace( /\W+$/, '' );
    str = str.replace( /\..*/, '' );
    str = str.replace( /[^\d]+/, '' );

    return str;
}

function dsp (loc) {
    if ( document.getElementById ) {
        var foc=loc.firstChild;

        foc = loc.firstChild.innerHTML?
            loc.firstChild:
            loc.firstChild.nextSibling;

        foc.innerHTML=foc.innerHTML.indexOf('preservation_info_plus') >= 0 ? '<img src="/gfx/preservation_info_minus.gif"/> ' : '<img src="/gfx/preservation_info_plus.gif"/> ';

        foc = loc.parentNode.nextSibling.style ?
            loc.parentNode.nextSibling:
            loc.parentNode.nextSibling.nextSibling;

        foc.style.display=foc.style.display == 'block' ? 'none' : 'block';
    }
}


function check_all (name, obj) {
    var action;

    if ( !obj.state || ( obj.state == 'unchecked' ) ) {
        action = 'checked';
        obj.state = 'checked';
    } else {
        action = '';
        obj.state = 'unchecked';
    }

    cells = document.getElementsByName( name );

    for ( var i = 0; i < cells.length; i++ ) {
        cell = cells[i];
        cell.checked = action;
    }
}


function count_checked_cells (name) {
    var cells = document.getElementsByName( name );
    var count = 0;

    for ( var i = 0; i < cells.length; i++ ) {
        if ( cells[i].checked )
            count ++;
    }

    return count;
}


function set_action_submit (form, method, msg, msg2) {
    var session_count = count_checked_cells( 'session_keys[]' );
    var mysql_count = count_checked_cells( 'mysql_keys[]' );

    if ( session_count == 0 &&
         mysql_count == 0 ) {

        alert( "You haven't selected any!" );
        return;
    }

    if ( method == 'email' ) {
        var email = form.email.value;
        form.email.value = (email.replace(/^\W+/,'')).replace(/\W+$/,'');

        if ( form.email.value == '' ) {
            alert( 'Wrong email address' );
            return;
        }

        form.annotation.value = prompt(msg2);
    }

    if ( confirm( msg ) ) {
        form.export_method.value = method;

        if ( method == 'print' ||
             method == 'download' ) {

            var old_action = form.action;

            pop_up_win( '', form.target = 'export');
            form.action = form.base_uri.value + '/php/export.php';
            form.submit();

            form.target = window.name;
            form.action = old_action;
            form.export_method.value = 'none';
        } else {
            form.submit();
        }
    }
}


function useradmin_submit (form, action, id, msg) {
    if ( action == 'delete' ) {
        if ( !confirm( msg ) )
           return;

        form.del_userid.value = id;
    } else if (  action == 'edit' ) {
        form.userid.value = id;
    } else {
        alert( 'Unknown action: ' + action );
    }

    form.action.value = action;
    form.submit();
}


function ip_mask_action (form, id, action, text) {
    if ( action == 'update' ) {
        var node_id = 'mask_' + id;
        if ( data_node = document.getElementById( node_id ) ) {
            var old_value = data_node.innerHTML;
            old_value = (old_value.replace(/^\W+/,'')).replace(/\W+$/,'');

            if ( new_value = prompt( text, old_value ) ) {
                form.action.value = action;
                form.update_id.value = id;
                form.update_mask.value = new_value;
                form.submit();
            }
        } else {
            alert( 'Unable to find nodeid: ' + node_id );
        }
    } else if ( action == 'delete' ) {
        if ( confirm( text ) ) {
            form.action.value = action;
            form.delete_id.value = id;
            form.submit();
        }
    } else if ( action == 'add' ) {
        if ( new_value = prompt( text, '' ) ) {
            form.action.value = action;
            form.add_mask.value = new_value;
            form.submit();
        }
    } else if (action == 'category') {
        var node_id = 'cat_' + id;
        var data_node = document.getElementById(node_id)

        if (data_node) {
            form.action.value = 'update';
            form.update_id.value = id;
            form.category.value = data_node.value;
            form.submit();
        }
    } else {
        alert( 'Unknown action: ' + action );
    }
}


function confirm_clone (form, file, msg) {
    if ( confirm( msg ) ) {
        form.clone_file.value = file;
        form.submit();
    }
}


function update_show_hidden (field, form, go) {
    if ( field.checked ) {
        form.show_hidden.value = 1;
    } else {
        form.show_hidden.value = 0;
    }

    if ( go ) {
        form.submit();
    }
}



function update_action (form, action, msg) {
    if ( confirm( msg ) ) {
        form.action.value = action;
        form.submit();
    }
}


function update_date_field (obj, form, name) {
    var cell = form[name];

    if (obj.checked) {
        cell.removeAttribute('readonly');
        cell.style.background = 'white';
        cell.style.color = 'black';
    } else {
        cell.value = '';
        cell.setAttribute('readonly', 'readonly');
        cell.style.background = 'lightgrey';
        cell.style.color = 'grey';
    }
}


function get_form_field (form, field) {
    for (i = 0; i < form.elements.length; i++) {
        if (form.elements[i].name == field) {
            return form.elements[i];
        }
    }

    return 0;

}


function update_reference (doc, field_id, do_path) {
    form = doc.main;
    element = get_form_field(form, field_id);
    display_cell = doc.getElementById('hidden_' + field_id);
    display_cell.innerHTML = element.value = do_path;
    close();
}


function hier_delete (form, hier_del , msg) {
    if ( confirm(msg) ) {
        form.hier_delete.value = hier_del;
        form.submit();
    }
    return false;
}

function validate_comment(form) {
    if (form.name.value == '') {
		alert('Husk at skrive dit navn');
		return false;
    }
    if (form.town.value == '') {
		alert('Husk at udfylde din hjemby');
		return false;
    }
    if ((form.email.value==null)||(form.email.value=="")){
		alert("Husk at skrive din email adresse")
		form.email.focus()
		return false
	}
	if (echeck(form.email.value)==false){
		form.email.value=""
		form.email.focus()
		return false
	}
    if (form.title.value == '') {
		alert('Husk at skrive en titel?');
		return false;
    }
    if (form.text.value == '') {
		alert('Husk at skrive en kommentar?');
		return false;
    }
    submit(form);
}

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Ugyldig email adresse")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Ugyldig email adresse")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("Ugyldig email adresse")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    alert("Ugyldig email adresse")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert("Ugyldig email adresse")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    alert("Ugyldig email adresse")
	    return false
	 }

	 if (str.indexOf(" ")!=-1){
	    alert("Ugyldig email adresse")
	    return false
	 }

	 return true
}

function resize_image (obj) {
    obj.style.display = 'block';
    if (obj.height > 108) {
        obj.height = '108';
		} else if (Number(obj.width) > 180) {
        obj.width = '180';
    }
}

function resize_image_medium (obj) {
  obj.style.display = 'block';
	if (obj.height > 211) {
		obj.height = '211';
	} else if (obj.width > 329) {
		obj.width = '329';
	}
}

function copyToClipboard(s) {
	if( window.clipboardData && clipboardData.setData )
	{
		clipboardData.setData("Text", s);
	}
	else
	{
		// You have to sign the code to enable this or allow the action in about:config by changing user_pref("signed.applets.codebase_principal_support", true);
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

		var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
	   if (!clip) return;

	   // create a transferable
	   var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
	   if (!trans) return;

	   // specify the data we wish to handle. Plaintext in this case.
	   trans.addDataFlavor('text/unicode');

	   // To get the data from the transferable we need two new objects
	   var str = new Object();
	   var len = new Object();

	   var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);

	   var copytext=meintext;

	   str.data=copytext;

	   trans.setTransferData("text/unicode",str,copytext.length*[[[[2]]]]);

	   var clipid=Components.interfaces.nsIClipboard;

	   if (!clip) return false;

	   clip.setData(trans,null,clipid.kGlobalClipboard);
	}
}

function show_object(obj) {
    obj.style.display = 'block';
}

function hide_object(obj) {
    obj.style.display = 'none';
}