/*
SimplestNet 3 Base js
Eric Grossi <egrossi@simplestnet.com>
11 apr 2006
Copyright 2004-2006 SimplestNet Inc.
See the enclosed file GPL for license information (GPL).  If you
did not receive this file, see http://www.gnu.org/copyleft/gpl.html.
*/

/* images preload */
preloadImages('images/layout/footer.png','images/layout/bodybk.png','images/layout/r-arrow.png',
              'images/layout/header_01.png','images/layout/header_02.png','images/layout/header_04.png',
              'images/layout/menubar_01.png','images/layout/menubar_03.png','images/layout/menubar_05.png',
              'images/layout/mn-login.png','images/layout/mn-login-i.png',
              'images/layout/mn-logout.png','images/layout/mn-logout-i.png',
              'images/layout/disabled.png','images/layout/user-disabled.png','images/layout/user-info.png',
              'images/layout/security.png','images/layout/security-off.png',
              'images/layout/notif.png','images/layout/notif-off.png',
              'images/layout/property.png','images/layout/delete.png','images/layout/delete-off.png',
              'images/layout/user.png','images/layout/group.png',
              'images/layout/reservation-on.png','images/layout/reservation-off.png',
              'images/layout/reservation-off-off.png','images/layout/reservation-on-off.png',
              'images/layout/edit.png','images/layout/edit-off.png',
              'images/layout/mn-item.png','images/layout/mn-item-i.png','images/layout/mn-item-h.png',
              'images/layout/mn-subitem.png','images/layout/mn-subitem-i.png',
              'images/layout/mn-subitem-s.png','images/layout/mn-subitem-f.png',
              'images/layout/login-panel.png','images/layout/login-panel-err.png',
              'images/layout/button.png','images/layout/obutton.png','images/layout/download.png',
              'images/layout/gbutton.png','images/layout/ogbutton.png','images/layout/mailto.png',
              'images/layout/oproperty_01.png','images/layout/oproperty_03.png','images/layout/oproperty_05.png',
              'images/layout/path-list_03.png','images/layout/path-list_05.png','images/layout/path-list_07.png',
              'images/layout/path-list_12.png','images/layout/path-list_14.png',
              'images/layout/path-list_18.png','images/layout/path-list_19.png','images/layout/path-list_20.png',
              'images/layout/oblist_01.png','images/layout/oblist_03.png','images/layout/oblist_05.png',
              'images/layout/oblist_09.png','images/layout/oblist_10.png','images/layout/oblist_11.png',
              'images/layout/oblist_15.png','images/layout/oblist_16.png','images/layout/oblist_17.png',
              'images/layout/oblist_odd.png','images/layout/oblist_hover.png',
              'images/layout/obltop_01.png','images/layout/obltop_03.png','images/layout/obltop_05.png',
              'images/layout/sort_asc.png','images/layout/sort_desc.png',
              'images/layout/oblbottom_01.png','images/layout/oblbottom_03.png','images/layout/oblbottom_05.png',
              'images/layout/end.png','images/layout/next.png','images/layout/first.png','images/layout/prev.png',
              'images/layout/buttbk.png','images/layout/trash.png','images/layout/emptytrash.png',
              'images/layout/search.png','images/layout/execute.png',
              'images/layout/green-pearl.png','images/layout/red-pearl.png','images/layout/disabled-pearl.png',
              'images/layout/save.png','images/layout/save-off.png',
              'images/layout/checked.png','images/layout/unchecked.png',
              'images/layout/restoretrash.png','images/layout/restoretrash-off.png','images/layout/rank.png',
              'images/layout/mn-help.png','images/layout/mn-help-i.png',
              'images/layout/webdisable.png');

/* authentication menu def */
var st_login = "width: 90px; height: 20px; cursor: pointer; padding-top: 5px; padding-left: 30px; padding-right: 0px;"+
               " border: none; font-weight: bold; font-size: 11px;";
var ist_login = " color: #CEE2F2; background: url(images/layout/mn-login-i.png) top left no-repeat;";
var ost_login = " color: #B4CFE4; background: url(images/layout/mn-login.png) top left no-repeat;";
var ist_logout = " color: #CEE2F2; background: url(images/layout/mn-logout-i.png) top left no-repeat;";
var ost_logout = " color: #B4CFE4; background: url(images/layout/mn-logout.png) top left no-repeat;";

var mnLogin = {
    'orient'    : "h"
};

/* language menu def */
var st_lang = "height: 16px; padding-top: 1px; padding-left: 5px; width: 70px; border: solid 1px #004C42;"+
              " cursor: pointer;";
var st_lang_it = "height: 16px; padding-top: 1px; padding-left: 5px; width: 70px; border: solid 1px #004C42;"+
                 " border-top: none; cursor: pointer;";
var st_lang_it_o = " color: #B4CFE4; background-color: #2C6B97;";
var st_lang_it_i = " color: #CEE2F2; background-color: #00749C;";

var mnLanguage = {
    'clkactive' : true,
    'orient'    : "h",
    'items'     : [
        {
            'text'      : "",
            'offostyle' : st_lang+" color: #C5D8E7; background-color: #2779b0;",
            'offistyle' : st_lang+" color: #C5D8E7; background-color: #3184b9;",
            'outstyle'  : st_lang+" color: #4B6B83; background-color: #63acd2;",
            'instyle'   : st_lang+" color: #4B6B83; background-color: #63acd2;",
            'orient'    : "h"
        }
    ]
};

/* help menu def */
var st_help = "width: 30px; height: 20px; cursor: pointer; padding-top: 5px; padding-left: 0px; padding-right: 0px;"+
              " margin-left: 10px; border: none; font-weight: bold; font-size: 11px;";
var ist_help = " color: #CEE2F2; background: url(images/layout/mn-help-i.png) top left no-repeat;";
var ost_help = " color: #B4CFE4; background: url(images/layout/mn-help.png) top left no-repeat;";
var help_url = "index.php";
var help_page = null;

var mnHelp = {
    'orient'    : "h",
    'items'     : [
        {
            'text'      : "",
            'outstyle'  : st_help+ost_help,
            'instyle'   : st_help+ist_help,
            'script'    : "openHelp(help_url, help_page)"
        }
    ]
};

/* main menu def */
var st_main = "width: 100px; height: 20px; cursor: pointer; padding-top: 5px; padding-left: 0px; padding-right: 0px; "+
              " font-weight: bold; font-size: 11px; text-align: center; margin-right: 2px; border: none;";
var ist_main = " color: #CEE2F2; background: url(images/layout/mn-item-i.png) top left no-repeat;";
var ost_main = " color: #B4CFE4; background: url(images/layout/mn-item-h.png) top left no-repeat;";
var offist_main = " color: #CEE2F2; background: url(images/layout/mn-item-h.png) top left no-repeat;";
var offost_main = " color: #B4CFE4; background: url(images/layout/mn-item.png) top left no-repeat;";

var st_sub = "width: 198px; height: 22px; cursor: pointer; padding-top: 2px; padding-left: 12px; padding-right: 10px; "+
             " font-weight: bold; font-size: 11px; text-align: left; border: none;";
var st_subs = "width: 174px; height: 20px; cursor: pointer; padding-top: 4px; padding-left: 36px; padding-right: 10px; "+
             " font-weight: bold; font-size: 11px; text-align: left; border: none;";
var ist_sub = " color: #CEE2F2; background: transparent url(images/layout/mn-subitem-i.png);"+
              " _background: none; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/layout/mn-subitem-i.png', sizingMethod='scale');";
var ost_sub = " color: #B4CFE4; background: transparent url(images/layout/mn-subitem.png);"+
              " _background: none; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/layout/mn-subitem.png', sizingMethod='scale');";

var st_sub_f = "width: 220px; height: 8px;  cursor: pointer; border: none; font-size: 0px;"+
              " background: transparent url(images/layout/mn-subitem-f.png);"+
              " _background: none; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/layout/mn-subitem-f.png', sizingMethod='scale');";
var obj_final = { 'text' : "", 'instyle' : st_sub_f, 'outstyle' : st_sub_f };
var st_sub_s = "width: 220px; height: 8px;  cursor: pointer; border: none; font-size: 0px;"+
              " background: transparent url(images/layout/mn-subitem-s.png);"+
              " _background: none; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/layout/mn-subitem-s.png', sizingMethod='scale');";
var obj_start = { 'text' : "", 'instyle' : st_sub_s, 'outstyle' : st_sub_s };

var mnMain = {
    'clkactive' : true,
    'orient'    : "h",
    'frstyle'   : "padding-left: 5px;",
    'items'     : []
};

/* open help view */
function openHelp(url, page) {
    //var webdisable = sn_webdisable;
    //sn_webdisable = null;
    var wnd=window.open(url+"?view=help"+(page ? "&page="+page : ""), "help",
        "top=50,left=50,height=600,width=900,scrollbars=yes");
    wnd.focus();

    //sn_webdisable = webdisable;
}

/* restore selected objects from trash */
function restoreTrash(url, oid, id, view) {
    var save = document.getElementById("savesecu");
    var lst = document.getElementsByName('ids');

    if ( 0==lst.length ) return;

    if ( !save.isOn ) return;

    var s = "<input type='hidden' name='action' value='restore'/>";
    s += "<input type='hidden' name='oid' value='"+oid+"'/>";
    s += "<input type='hidden' name='id' value='"+id+"'/>";
    s += "<input type='hidden' name='view' value='"+view+"'/>";

    for ( var i=0; i<lst.length; i++ ) {
        if ( !lst[i].checked ) continue;

        s+= "<input type='hidden' name='ids["+lst[i].id+"]' value='1'/>";
    }

    var frm = document.createElement("form");
    frm.action = url;
    frm.enctype = "multipart/form-data";
    frm.method = "post";
    frm.id = "trashfrm";
    frm.style.cssText = "display: none; width: 200px; height: 200px;";
    frm.innerHTML = s;
    document.body.appendChild(frm);
    frm.submit();
}

/* set trash selection modified state to on */
function trashModified() {
    var save = document.getElementById("savesecu");
    var lst = document.getElementsByName('ids');
    var ok = false;

    if ( !save ) return;

    for ( var i=0; i<lst.length; i++ ) {
        if ( !lst[i].checked ) continue;

        ok = true;
        break;
    }

    if ( ok ) {
        save.isOn = true;
        save.src = "images/layout/restoretrash.png";
        save.style.cursor = "pointer";
    } else {
        save.isOn = false;
        save.src = "images/layout/restoretrash-off.png";
        save.style.cursor = "default";
    }
}

/* save security params */
function saveSecurity(url, oid, view) {
    var save = document.getElementById("savesecu");
    var lst = document.getElementsByName('gid');
    var rlst = document.getElementsByName('read');
    var wlst = document.getElementsByName('write');
    var elst = document.getElementsByName('execute');

    if ( 0==lst.length ) return;

    if ( !save.isOn ) return;

    if ( !confirm(msgSaveModif) ) return;

    sn_permanentDisableInterface();
    var id,s = "<input type='hidden' name='action' value='modrights'/>";
    s += "<input type='hidden' name='oid' value='"+oid+"'/>";
    s += "<input type='hidden' name='view' value='"+view+"'/>";

    for ( var i=0; i<lst.length; i++ ) {
        if ( !lst[i].isModified ) continue;

        id = lst[i].id.substring(4);

        if ( lst[i].isOn ) s+= "<input type='hidden' name='gids["+id+"]' value='1'/>";
        else s+= "<input type='hidden' name='gids["+id+"]' value='0'/>";

        if ( rlst[i].checked ) s+= "<input type='hidden' name='read["+id+"]' value='1'/>";
        else s+= "<input type='hidden' name='read["+id+"]' value='0'/>";

        if ( wlst[i].checked ) s+= "<input type='hidden' name='write["+id+"]' value='1'/>";
        else s+= "<input type='hidden' name='write["+id+"]' value='0'/>";

        if ( elst[i].checked ) s+= "<input type='hidden' name='exec["+id+"]' value='1'/>";
        else s+= "<input type='hidden' name='exec["+id+"]' value='0'/>";
    }

    var frm = document.createElement("form");
    frm.action = url;
    frm.enctype = "multipart/form-data";
    frm.method = "post";
    frm.id = "secufrm";
    frm.style.cssText = "display: none; width: 200px; height: 200px;";
    frm.innerHTML = s;
    document.body.appendChild(frm);
    frm.submit();
}

/* set security modified state to on */
function securityModified(id) {
    var save = document.getElementById("savesecu");
    var cur = document.getElementById("gid_"+id);

    if ( !save || !cur ) return;

    cur.isModified = true;
    cur.isOn = true;
    save.isOn = true;
    save.src = "images/layout/save.png";
    save.style.cursor = "pointer";
}

/* save subscriptions */
function saveSubscription(url, oid, view, use) {
    var save = document.getElementById("savesecu");
    var lst = document.getElementsByName('current');

    if ( 0==lst.length ) return;

    if ( !save.isOn ) return;

    if ( !confirm(msgSaveModif) ) return;

    sn_permanentDisableInterface();
    var el,dp,id,s = "<input type='hidden' name='action' value='subscribe'/>";
    s += "<input type='hidden' name='oid' value='"+oid+"'/>";
    s += "<input type='hidden' name='view' value='"+view+"'/>";

    for ( var i=0; i<lst.length; i++ ) {
        if ( !lst[i].isModified ) continue;

        id = lst[i].id.substring(4);

        if ( !(dp = document.getElementById("deep_"+id)) ) continue;

        s+= "<input type='hidden' name='deep["+id+"]' value='"+dp.value+"'/>";

        for ( var j=0; j<use.length; j++ ) {
            if ( !(el = document.getElementById(use[j]+"_"+id)) ) continue;

            if ( el.checked ) s+= "<input type='hidden' name='_"+use[j]+"["+id+"]' value='1'/>";
        }
    }

    var frm = document.createElement("form");
    frm.action = url;
    frm.enctype = "multipart/form-data";
    frm.method = "post";
    frm.id = "subsfrm";
    frm.style.cssText = "display: none; width: 200px; height: 200px;";
    frm.innerHTML = s;
    document.body.appendChild(frm);
    frm.submit();
}

/* set subscription modified state to on */
function subscriptionModified(id) {
    var save = document.getElementById("savesecu");
    var cur = document.getElementById("cur_"+id);

    if ( !save || !cur ) return;

    cur.isModified = true;
    save.isOn = true;
    save.src = "images/layout/save.png";
    save.style.cursor = "pointer";
}

/* switch security control */
function switchSecurity(id) {
    var save = document.getElementById("savesecu");
    var cur = document.getElementById("gid_"+id);
    var rs = document.getElementById("rid_"+id);
    var ws = document.getElementById("wid_"+id);
    var es = document.getElementById("eid_"+id);

    if ( !save || !cur || !rs || !ws || !es ) return;

    if ( rs.disabled ) {
        if ( !confirm(msgAdd) ) return;

        cur.src = "images/layout/green-pearl.png";
        cur.isOn = true;
        rs.disabled = false;
        ws.disabled = false;
        es.disabled = false;
    } else {
        if ( !confirm(msgRemove) ) return;

        cur.src = "images/layout/red-pearl.png";
        cur.isOn = false;
        rs.disabled = true;
        ws.disabled = true;
        es.disabled = true;
    }

    cur.isModified = true;
    save.isOn = true;
    save.src = "images/layout/save.png";
    save.style.cursor = "pointer";
}

/* save groupuser */
function saveGroupUser(url, oid, view) {
    var save = document.getElementById("savesecu");
    var lst = document.getElementsByName('gid');

    if ( 0==lst.length ) return;

    if ( !save.isOn ) return;

    if ( !confirm(msgSaveModif) ) return;

    sn_permanentDisableInterface();
    var id,s = "<input type='hidden' name='action' value='groupusr'/>";
    s += "<input type='hidden' name='oid' value='"+oid+"'/>";
    s += "<input type='hidden' name='view' value='"+view+"'/>";

    for ( var i=0; i<lst.length; i++ ) {
        if ( !lst[i].isModified ) continue;

        id = lst[i].id.substring(5);

        if ( lst[i].isOn ) s+= "<input type='hidden' name='gids["+id+"]' value='1'/>";
        else s+= "<input type='hidden' name='gids["+id+"]' value='0'/>";
    }

    var frm = document.createElement("form");
    frm.action = url;
    frm.enctype = "multipart/form-data";
    frm.method = "post";
    frm.id = "grpfrm";
    frm.style.cssText = "display: none; width: 200px; height: 200px;";
    frm.innerHTML = s;
    document.body.appendChild(frm);
    frm.submit();
}

/* switch group in/out user */
function switchGroup(id) {
    var save = document.getElementById("savesecu");
    var curi = document.getElementById("igid_"+id);
    var curo = document.getElementById("ogid_"+id);
    var cur;

    if ( !save || (!curi && !curo) ) return;

    if ( curo ) {
        cur = curo;

        if ( !cur.isModified ) cur.isOn = false;
    } else {
        cur = curi;

        if ( !cur.isModified ) cur.isOn = true;
    }

    if ( !cur.isOn ) {
        if ( !confirm(msgAdd) ) return;

        cur.src = "images/layout/green-pearl.png";
        cur.isOn = true;
    } else {
        if ( !confirm(msgRemove) ) return;

        cur.src = "images/layout/red-pearl.png";
        cur.isOn = false;
    }

    cur.isModified = true;
    save.isOn = true;
    save.src = "images/layout/save.png";
    save.style.cursor = "pointer";
}

/* save usergroup */
function saveUserGroup(url, oid, view) {
    var save = document.getElementById("savesecu");
    var lst = document.getElementsByName('uid');

    if ( 0==lst.length ) return;

    if ( !save.isOn ) return;

    if ( !confirm(msgSaveModif) ) return;

    sn_permanentDisableInterface();
    var id,s = "<input type='hidden' name='action' value='usergrp'/>";
    s += "<input type='hidden' name='oid' value='"+oid+"'/>";
    s += "<input type='hidden' name='view' value='"+view+"'/>";

    for ( var i=0; i<lst.length; i++ ) {
        if ( !lst[i].isModified ) continue;

        id = lst[i].id.substring(5);

        if ( lst[i].isOn ) s+= "<input type='hidden' name='uids["+id+"]' value='1'/>";
        else s+= "<input type='hidden' name='uids["+id+"]' value='0'/>";
    }

    var frm = document.createElement("form");
    frm.action = url;
    frm.enctype = "multipart/form-data";
    frm.method = "post";
    frm.id = "grpfrm";
    frm.style.cssText = "display: none; width: 200px; height: 200px;";
    frm.innerHTML = s;
    document.body.appendChild(frm);
    frm.submit();
}

/* switch user in/out group */
function switchUser(id) {
    var save = document.getElementById("savesecu");
    var curi = document.getElementById("iuid_"+id);
    var curo = document.getElementById("ouid_"+id);
    var cur;

    if ( !save || (!curi && !curo) ) return;

    if ( curo ) {
        cur = curo;

        if ( !cur.isModified ) cur.isOn = false;
    } else {
        cur = curi;

        if ( !cur.isModified ) cur.isOn = true;
    }

    if ( !cur.isOn ) {
        if ( !confirm(msgAdd) ) return;

        cur.src = "images/layout/green-pearl.png";
        cur.isOn = true;
    } else {
        if ( !confirm(msgRemove) ) return;

        cur.src = "images/layout/red-pearl.png";
        cur.isOn = false;
    }

    cur.isModified = true;
    save.isOn = true;
    save.src = "images/layout/save.png";
    save.style.cursor = "pointer";
}

/* Process empty trash */
function emptyTrash(url) {
    if ( !confirm(msgEmptyTrash) ) return;

    sn_permanentDisableInterface();
    window.location = url+"?action=emptytrash";
}

/* Process delete single object */
function deleteObject(oname, url, id, view, pid) {
    if ( !confirm(msgDelObject+" "+oname) ) return;

    sn_permanentDisableInterface();
    window.location = url+"?action=delete&id="+id+"&view="+view+"&pid="+pid;
}

/* Process delete selection */
function deleteSelection(url, view, pid) {
    if ( !confirm(msgDelSelection) ) return;

    sn_permanentDisableInterface();
    window.location = url+"?action=delete&view="+view+"&pid="+pid;
}

/* Create object selection set */
function setSel(url, view, id) {
    var lst = document.getElementsByName('ids');

    if ( 0==lst.length ) return;

    var s = "<input type='hidden' name='action' value='select'/>";
    s += "<input type='hidden' name='view' value='"+view+"'/>";
    s += "<input type='hidden' name='id' value='"+id+"'/>";

    for ( var i=0; i<lst.length; i++ ) {
        if ( lst[i].checked ) s+= "<input type='hidden' name='"+lst[i].id+"' value='1'/>";
        else s+= "<input type='hidden' name='"+lst[i].id+"' value='0'/>";
    }

    var frm = document.createElement("form");
    frm.action = url;
    frm.enctype = "multipart/form-data";
    frm.method = "post";
    frm.id = "selfrm";
    frm.style.cssText = "display: none; width: 200px; height: 200px;";
    frm.innerHTML = s;
    document.body.appendChild(frm);
    frm.submit();
}

/* object list click event */
function oblClick(id) {
    var el = document.getElementById(id);

    if ( !el ) return;

    el.focus();

    if ( el.target && "_self"!=el.target )
        window.open(el.href, el.target);
    else
        window.location = el.href;
}

function stopBubble(ev) {
    if ( !ev && !(ev = window.event) ) return;

    if ( ( /msie/i.test(navigator.userAgent) &&
		   !/opera/i.test(navigator.userAgent) ) ) {
		ev.cancelBubble = true;
	} else {
		ev.stopPropagation();
	}
}

function stopEventWebDisabled(ev) {
    if ( !ev && !(ev = window.event) ) return;

    if ( SimplestNetMenu.is_ie ) {
		ev.cancelBubble = true;
		ev.returnValue = false;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
}

/* main window sizing */
var sn_origWidth = 0;
var sn_origHeight = 0;
window.onresize = sn_windowOnResize;

function sn_windowOnResize() {
    if ( document.body.offsetWidth == sn_origWidth && document.body.offsetHeight == sn_origHeight )
        return;

    var wb = document.body.offsetWidth;
    var hb = document.body.offsetHeight;

    sn_origWidth = wb;
    sn_origHeight = hb;

    if ( sn_webdisable ) {
        sn_webdisable.style.width = wb+"px";
        sn_webdisable.style.height = hb+"px";
    }

    if ( sn_imgload ) {
        sn_imgload.style.width = wb+"px";
        sn_imgload.style.height = hb+"px";
    }
}

/* disable web interface on unload */
var sn_webdisable = null;
window.onbeforeunload = sn_disableInterface;
function sn_disableInterface() {
    if ( sn_webdisable ) sn_webdisable.style.display = "";
}

function sn_permanentDisableInterface() {
    if ( sn_webdisable ) sn_webdisable.style.display = "";
    sn_webdisable = null;
}

window.onfocus = sn_onGetFocus;
function sn_onGetFocus() {
    if ( sn_webdisable ) sn_webdisable.style.display = "none";
}

/* preload function */
function addOnLoad(fctname) {
    sn_preload[sn_preload.length] = fctname;
}

window.onload = sn_windowOnload;
var sn_preload = new Array();

function sn_windowOnload() {
    var el = document.createElement("div");
    var w = document.body.offsetWidth;
    var h = document.body.offsetHeight;

    if ( !( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) ) )
        el.style.cssText = "display: none; cursor: wait; position: absolute; top: 0px; left: 0px; "+
                           "width: "+w+"px; height: "+h+"px; "+
                           "background: url(images/layout/webdisable.png) repeat;";
    else
        el.style.cssText = "display: none; cursor: wait; position: absolute; top: 0px; left: 0px; "+
                           "width: "+w+"px; height: "+h+"px; background: url(noimage.png) repeat; "+
                           "filter:progid:DXImageTransform.Microsoft."+
                           "AlphaImageLoader(src='images/layout/webdisable.png', sizingMethod='scale');";

    if ( el.attachEvent ) { // IE
		el.attachEvent("onclick", stopEventWebDisabled);
	} else if ( el.addEventListener ) { // Gecko / W3C
		el.addEventListener("click", stopEventWebDisabled, true);
	} else {
		el["onclick"] = stopEventWebDisabled;
	}

    sn_webdisable = el;
    preloadImagesChk();
    document.body.appendChild(el);

    for ( var i=0; i<sn_preload.length; i++ )
        eval(sn_preload[i]+";");
}

/* debug function: dump object propertys */
function _odump(obj) {
    var s="", cnt = 0;

    for ( var key in obj ) {
        if ( typeof(obj[key])!="function" )
            s += "  "+key+", "+obj[key]+"**";

        if ( 0 == cnt % 5 ) s+= "\r\n";
        cnt++;
    }

    alert(s);
}

/* resource preload callbacks */
var sn_imgload = null;
function onimgload(tot,loaded) {
    if ( !sn_imgload && loaded!=tot ) {
        var el = document.createElement("div");
        var w = document.body.offsetWidth;
        var h = document.body.offsetHeight;

        el.style.cssText = "cursor: wait; position: absolute; top: 0px; left: 0px; "+
                           "width: "+w+"px; height: "+h+"px; "+
                           "background: #f5f5f5; text-align: center; "+
                           "font-family: Bitstream Vera Sans, Verdana, Arial, Helvetica, sans-serif; "+
                           "font-size: 18px; font-weight: bold; color: #000000;";

        el.innerHTML = "<br/><br/><br/><br/><br/><br/><br/>Resource loading...";
        sn_imgload = el;
        document.body.appendChild(el);
    }
}

function onimgloaded() {
    if ( sn_imgload ) sn_imgload.style.display = "none";
}

/* Preload web image */
function preloadImages() {
	if ( !window.pre_img )
		window.pre_img = new Array();

	var l = window.pre_img.length;

	for (var i=0; i<preloadImages.arguments.length; i++) {
		window.pre_img[i+l] = new Image();
		window.pre_img[i+l].src = preloadImages.arguments[i];
	}
}

var sn_loadstep = 0;
function preloadImagesChk() {
    var ok = true, i, s="";
    sn_loadstep++;

    for (i=0; i<window.pre_img.length; i++) {
        if ( !window.pre_img[i].complete || (window.pre_img[i].complete && 0==window.pre_img[i].width) ) {
            ok = false;
            s = window.pre_img[i].src+" - "+window.pre_img[i].complete;
            break;
        }
    }

    if ( 300<sn_loadstep ) {
        alert("Can't load resource :"+s);
        onimgloaded();
        return;
    }

    if ( !ok ) {
        if ( typeof(onimgload)=="function" )
            onimgload(window.pre_img.length, i);

        window.setTimeout(preloadImagesChk, 50);
    } else if ( typeof(onimgloaded)=="function" )
        onimgloaded();
}

/* Write a flash object */
function writeFlash(src, w, h, q, extra) {
    if (!extra) extra = "";

    var line = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '+
                   'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" '+
                   'width="'+w+'" height="'+h+'" '+extra+'><param name="movie" value="'+src+'">'+
                   '<param name="quality" value="'+q+'">'+
                   '<embed src="'+src+'" quality="'+q+'" width="'+w+'" height="'+h+'" type="application/x-shockwave-flash" '+
                   'pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" '+extra+
                   '></embed></object>';
    document.write(line);
}

/* Check login value */
function chkLogin(frm) {
    disableButton();

    with (frm) {
        if ( trim(name.value)=="" ) {
    		alert(msgEmptyUname);
    		enableButton();
    		name.focus();
    		return false;
    	}
        if ( trim(pwrd.value)=="" ) {
    		alert(msgEmptyPassword);
    		enableButton();
    		pwrd.focus();
    		return false;
    	}
    }

	return true;
}

/* Check name value */
function chkName(frm) {
    disableButton();

    with (frm) {
        if ( trim(name.value)=="" ) {
    		alert(msgEmptyName);
    		enableButton();
    		name.focus();
    		return false;
    	}
    }

	return true;
}

/* Check folder value */
function chkFolder(frm) {
    disableButton();
    var valid_numbers 	= '0123456789';

    with (frm) {
        if ( trim(name.value)=="" ) {
    		alert(msgEmptyName);
    		enableButton();
    		name.focus();
    		return false;
    	}
        if ( pagesz.value !="" && !isValid(pagesz.value, valid_numbers) ) {
    		alert(msgPageSizeFormat);
    		enableButton();
    		pagesz.focus();
    		return false;
    	}
    }

	return true;
}

/* Check file value */
function chkFile(frm, chkfile) {
    disableButton();

    with (frm) {
        if ( chkfile ) {
            if ( trim(file.value)=="" ) {
        		alert(msgEmptyFile);
        		enableButton();
        		file.focus();
        		return false;
        	}
        }
        if ( trim(name.value)=="" && trim(file.value)!="" ) {
    		var name1 = file.value.match(/[^(\\&\/)]*$/);
    		name.value = name1;
    	}
        if ( trim(name.value)=="" ) {
    		alert(msgEmptyName);
    		enableButton();
    		name.focus();
    		return false;
    	}
    }

	return true;
}

/* Check user value */
function chkUser(frm, chkpwrd) {
    disableButton();
    var p1=document.getElementById("pwrd1");
    var p2=document.getElementById("pwrd2");

    with (frm) {
        if ( trim(name.value)=="" ) {
    		alert(msgEmptyName);
    		enableButton();
    		name.focus();
    		return false;
    	}
        if ( chkpwrd && !p1.value.length ) {
            alert(msgEmptyPassword);
            enableButton();
        	p1.focus();
        	return false;
        }
        if ( p1.value!=p2.value ) {
            alert(msgWrongPassword);
            enableButton();
        	p1.focus();
        	return false;
        }
    }

	return true;
}

/* Check category value */
function chkCategory(frm, chkxslt) {
    disableButton();

    with (frm) {
        if ( trim(name.value)=="" ) {
    		alert(msgEmptyName);
    		enableButton();
    		name.focus();
    		return false;
    	}
        if ( trim(icon.value)=="" ) {
    		alert(msgEmptyIcon);
    		enableButton();
    		icon.focus();
    		return false;
    	}
        if ( chkxslt ) {
            if ( trim(xslt.value)=="" ) {
        		alert(msgEmptyXslt);
        		enableButton();
        		xslt.focus();
        		return false;
        	}
        }
    }

	return true;
}

/* Check transformation value */
function chkTransformation(frm, chkxslt) {
    disableButton();

    with (frm) {
        if ( trim(name.value)=="" ) {
    		alert(msgEmptyName);
    		enableButton();
    		name.focus();
    		return false;
    	}
        if ( chkxslt ) {
            if ( trim(xslt.value)=="" ) {
        		alert(msgEmptyXslt);
        		enableButton();
        		xslt.focus();
        		return false;
        	}
        }
    }

	return true;
}

/* Check module value */
function chkModule(frm, chkphp) {
    disableButton();

    with (frm) {
        if ( trim(name.value)=="" ) {
    		alert(msgEmptyName);
    		enableButton();
    		name.focus();
    		return false;
    	}
        if ( chkphp ) {
            if ( trim(php.value)=="" ) {
        		alert(msgEmptyPHP);
        		enableButton();
        		php.focus();
        		return false;
        	}
        }
    }

	return true;
}

/* Check import value */
function chkImport(frm) {
    disableButton();

    with (frm) {
        if ( trim(file.value)=="" ) {
    		alert(msgEmptyFile);
    		enableButton();
    		file.focus();
    		return false;
    	}
        if ( trim(pid.value)=="" ) {
    		alert(msgEmptyPID);
    		enableButton();
    		pid.focus();
    		return false;
    	}
    }

	return true;
}

/* disable buttons */
function disableButton() {
	var send = document.getElementById("send");
    var send1 = document.getElementById("send1");
	var cancel = document.getElementById("cancel");

	if ( send ) {
        send.className = "obutton";
		send.disabled = true;
    }

    if ( send1 ) {
        send1.className = "obutton";
		send1.disabled = true;
    }

	if ( cancel ) {
        cancel.className = "obutton";
		cancel.disabled = true;
    }
}

/* enable buttons*/
function enableButton() {
    var send = document.getElementById("send");
    var send1 = document.getElementById("send1");
	var cancel = document.getElementById("cancel");

	if ( send ) {
        send.className = "button";
		send.disabled = false;
    }

    if ( send1 ) {
        send1.className = "button";
		send1.disabled = false;
    }

	if ( cancel ) {
        cancel.className = "button";
		cancel.disabled = false;
    }
}

/* check string value for key pressed and go to given location on enter */
function onKeyPressStr(event, obj, onenter) {
    var code=event.keyCode;

    if ( !( /msie/i.test(navigator.userAgent) &&
		   !/opera/i.test(navigator.userAgent) ) ) {
        if ( 46==code ) return true;

        if (0==code ) code=event.charCode;
    }

    if ( 13==code && onenter ) {
        window.location=onenter;
        return false;
    }

    return true;
}

/* check integer value for key pressed and go to given location on enter */
function onKeyPressNum(event, obj, min, max, onenter) {
    var code=event.keyCode;

    if ( !( /msie/i.test(navigator.userAgent) &&
		   !/opera/i.test(navigator.userAgent) ) ) {
        if ( 46==code ) return true;

        if (0==code ) code=event.charCode;
    }

    if ( 13==code && onenter && (false!=min ? min<=obj.value : true) && (false!=max ? max>=obj.value : true) )
        window.location=onenter;

    if ( (47<code && 58>code ) || 13==code || 8==code || 43==code  || 45==code || 46==code )
        return true;

    return false;
}

/* Trim wihte space in left and right*/
function trim(strText) {
    //var res=strText.match(/[\S*]/);

    // this will get rid of trailing spaces
    while (strText.substring(0,1) == ' ')
        strText = strText.substring(1, strText.length);
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

    return strText;
}

/* Validate allowed variables */
function isValid(string,allowed) {
	if (!string)
		return false;
    for (var i=0; i< string.length; i++)
	{
		if (allowed.indexOf(string.charAt(i)) == -1)
          return false;
    }

	return true;
}