
function LZ(x) {
    return(x<0||x>9?"":"0")+x;
}
// ------------------------------------------------------------------
// formatDate (date_object, format)
// Returns a date in the output format specified.
// The format string uses the same abbreviations as in getDateFromFormat()
// ------------------------------------------------------------------
function formatHeaderDate(month_name, weekday_name, date,format) {
    format=format+"";
    var result="";
    var i_format=0;
    var c="";
    var token="";
    var y=date.getYear()+"";
    var M=date.getMonth()+1;
    var d=date.getDate();
    var E=date.getDay();
    var H=date.getHours();
    var m=date.getMinutes();
    var s=date.getSeconds();
    
    // Convert real date parts into formatted versions
    var value=new Object();
    if (y.length < 4) {
        y=""+(y-0+1900);
    }
    value["y"]=""+y;
    value["yyyy"]=y;
    value["yy"]=y.substring(2,4);
    value["M"]=M;
    value["MM"]=LZ(M);
    value["MMM"]=month_name[M];
    value["NNN"]=month_name[M];
    value["d"]=d;
    value["dd"]=LZ(d);
    value["E"]=weekday_name[E];
    value["EE"]=weekday_name[E];
    value["H"]=H;
    value["HH"]=LZ(H);
    if (H==0){
        value["h"]=12;
    }
    else if (H>12){
        value["h"]=H-12;
    }
    else {
        value["h"]=H;
    }
    value["hh"]=LZ(value["h"]);
    if (H>11){
        value["K"]=H-12;
    } else {
        value["K"]=H;
    }
    value["k"]=H+1;
    value["KK"]=LZ(value["K"]);
    value["kk"]=LZ(value["k"]);
    if (H > 11) {
        value["a"]="PM";
    }
    else {
        value["a"]="AM";
    }
    value["m"]=m;
    value["mm"]=LZ(m);
    value["s"]=s;
    value["ss"]=LZ(s);
    while (i_format < format.length) {
        c=format.charAt(i_format);
        token="";
        while ((format.charAt(i_format)==c) && (i_format < format.length)) {
            token += format.charAt(i_format++);
        }
        if (value[token] != null) {
            result=result + value[token];
        }
        else {
            result=result + token;
        }
    }
    return result;
}

function is_opera_browser(){
    var agent = navigator.userAgent.toLowerCase();
    return (agent.indexOf("opera") != -1);
}
function is_safari_browser(){
    var agent = navigator.userAgent.toLowerCase();
    return (agent.indexOf("safari") != -1);
}
function is_IE_browser(){
    var agent = navigator.userAgent.toLowerCase();
    return (agent.indexOf("msie") != -1);
}

/**
 * Open a windows, return the window
 */
function popup_window(url,name, width, height, modal) {

    var x = 0, y = 0;
    x = (self.screen.width - width) / 2;
    y = (self.screen.height - height) / 2;

    if (is_opera_browser() || is_safari_browser() || is_IE_browser()){
        //Opera and Safari browser does not support showModalDialog,
        //but windows.showModalDialog is not null.
        //IE does not support search in dialog. So we must show it as modaless.
        modal = false;
    }

    if (url.indexOf("?")>=0 ){
        url = url + "&popupwin=true";
    }else{
        url = url + "?popupwin=true";
    }

    if ( modal && window.showModalDialog ){
        param = "toolbar:0;status=1;resizable=1;";
        param += ";dialogLeft="   + x + "px";
        param += ";dialogTop="    + y + "px";
        param += ";dialogWidth="  + width  + "px";
        param += ";dialogHeight=" + height + "px";
        var args = {
            opener: self
        }
        window.showModalDialog(url, args, param);
    }else{
        param = "toolbar=no,location=no,directories=no,status=1,menubar=no,scrollbars=yes,resizable=yes";
        param += ",width="  + width;
        param += ",height=" + height;
        param += ",top="    + y;
        param += ",left="   + x;
        if ( modal ){
            param += ", modal=yes";
        }
        var wnd = window.open(url , name , param);
        wnd.focus();
    }

}

function getParentWindow(){
    var parent = window.opener;
    if ( !parent && window.dialogArguments.opener ){
        parent = window.dialogArguments.opener;
    }
    return parent;
}

function trim(stringToTrim){
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ltrim(stringToTrim){
    return stringToTrim.replace(/^\s+/,"");
}

function rtrim(stringToTrim){
    return stringToTrim.replace(/\s+$/,"");
}

function getCurrentDate(lang){

    var month = new Array(12);
    var day= new Array(7);
    var format;

    if ( lang == 'de'){

        month[1]="Januar";
        month[2]="Februar";
        month[3]="M\u00E4rz";
        month[4]="April";
        month[5]="Mai";
        month[6]="Juni";
        month[7]="Juli";
        month[8]="August";
        month[9]="September";
        month[10]="Oktober";
        month[11]="November";
        month[12]="Dezember";

        day[0]="Sonntag";
        day[1]="Montag";
        day[2]="Dienstag";
        day[3]="Mittwoch";
        day[4]="Donnerstag";
        day[5]="Freitag";
        day[6]="Samstag";
        format = "dd. NNN yyyy";

    }else if ( lang == 'it'){

        month[1]="Gennaio";
        month[2]="Febbraio";
        month[3]="Marzo";
        month[4]="Aprile";
        month[5]="Maggio";
        month[6]="Giugno";
        month[7]="Luglio";
        month[8]="Agosto";
        month[9]="Settembre";
        month[10]="Ottobre";
        month[11]="Novembre";
        month[12]="Dicembre";

        day[0]="Domanica";
        day[1]="Luned\u00EC";
        day[2]="Marted\u00EC";
        day[3]="Mercoled\u00EC";
        day[4]="Gioved\u00EC";
        day[5]="Venerd\u00EC";
        day[6]="Sabato";

        format = "dd NNN, yyyy";

    }else if ( lang == 'fr' ){


        month[1]="Janvier";
        month[2]="F\u00E9vrier";
        month[3]="Mars";
        month[4]="Avril";
        month[5]="Mai";
        month[6]="Juin";
        month[7]="Juillet";
        month[8]="Ao\u00FBt";
        month[9]="Septembre";
        month[10]="Octobre";
        month[11]="Novembre";
        month[12]="D\u00E9cembre";

        day[0]="Dimanche";
        day[1]="Lundi";
        day[2]="Mardi";
        day[3]="Mercredi";
        day[4]="Jeudi";
        day[5]="Vendredi";
        day[6]="Samedi";
        format = "NNN dd, yyyy";
    }else{
        month[1]="January";
        month[2]="February";
        month[3]="March";
        month[4]="April";
        month[5]="May";
        month[6]="June";
        month[7]="July";
        month[8]="August";
        month[9]="September";
        month[10]="October";
        month[11]="November";
        month[12]="December";

        day[0]="Sunday";
        day[1]="Monday";
        day[2]="Tuesday";
        day[3]="Wednesday";
        day[4]="Thursday";
        day[5]="Friday";
        day[6]="Saturday";
        format = "NNN dd, yyyy";
    }

    var time= new Date();
    return formatHeaderDate(month,day,time,format);
}

function getTimezoneName() {
    tmSummer = new Date(Date.UTC(2005, 6, 30, 0, 0, 0, 0));
    so = -1 * tmSummer.getTimezoneOffset();
    tmWinter = new Date(Date.UTC(2005, 12, 30, 0, 0, 0, 0));
    wo = -1 * tmWinter.getTimezoneOffset();

    if (-660 == so && -660 == wo) return 'Pacific/Midway';
    if (-600 == so && -600 == wo) return 'Pacific/Tahiti';
    if (-570 == so && -570 == wo) return 'Pacific/Marquesas';
    if (-540 == so && -600 == wo) return 'America/Adak';
    if (-540 == so && -540 == wo) return 'Pacific/Gambier';
    if (-480 == so && -540 == wo) return 'US/Alaska';
    if (-480 == so && -480 == wo) return 'Pacific/Pitcairn';
    if (-420 == so && -480 == wo) return 'US/Pacific';
    if (-420 == so && -420 == wo) return 'US/Arizona';
    if (-360 == so && -420 == wo) return 'US/Mountain';
    if (-360 == so && -360 == wo) return 'America/Guatemala';
    if (-360 == so && -300 == wo) return 'Pacific/Easter';
    if (-300 == so && -360 == wo) return 'US/Central';
    if (-300 == so && -300 == wo) return 'America/Bogota';
    if (-240 == so && -300 == wo) return 'US/Eastern';
    if (-240 == so && -240 == wo) return 'America/Caracas';
    if (-240 == so && -180 == wo) return 'America/Santiago';
    if (-180 == so && -240 == wo) return 'Canada/Atlantic';
    if (-180 == so && -180 == wo) return 'America/Montevideo';
    if (-180 == so && -120 == wo) return 'America/Sao_Paulo';
    if (-150 == so && -210 == wo) return 'America/St_Johns';
    if (-120 == so && -180 == wo) return 'America/Godthab';
    if (-120 == so && -120 == wo) return 'America/Noronha';
    if (-60 == so && -60 == wo) return 'Atlantic/Cape_Verde';
    if (0 == so && -60 == wo) return 'Atlantic/Azores';
    if (0 == so && 0 == wo) return 'Africa/Casablanca';
    if (60 == so && 0 == wo) return 'Europe/London';
    if (60 == so && 60 == wo) return 'Africa/Algiers';
    if (60 == so && 120 == wo) return 'Africa/Windhoek';
    if (120 == so && 60 == wo) return 'Europe/Amsterdam';
    if (120 == so && 120 == wo) return 'Africa/Harare';
    if (180 == so && 120 == wo) return 'Europe/Athens';
    if (180 == so && 180 == wo) return 'Africa/Nairobi';
    if (240 == so && 180 == wo) return 'Europe/Moscow';
    if (240 == so && 240 == wo) return 'Asia/Dubai';
    if (270 == so && 210 == wo) return 'Asia/Tehran';
    if (270 == so && 270 == wo) return 'Asia/Kabul';
    if (300 == so && 240 == wo) return 'Asia/Baku';
    if (300 == so && 300 == wo) return 'Asia/Karachi';
    if (330 == so && 330 == wo) return 'Asia/Calcutta';
    if (345 == so && 345 == wo) return 'Asia/Katmandu';
    if (360 == so && 300 == wo) return 'Asia/Yekaterinburg';
    if (360 == so && 360 == wo) return 'Asia/Colombo';
    if (390 == so && 390 == wo) return 'Asia/Rangoon';
    if (420 == so && 360 == wo) return 'Asia/Almaty';
    if (420 == so && 420 == wo) return 'Asia/Bangkok';
    if (480 == so && 420 == wo) return 'Asia/Krasnoyarsk';
    if (480 == so && 480 == wo) return 'Australia/Perth';
    if (540 == so && 480 == wo) return 'Asia/Irkutsk';
    if (540 == so && 540 == wo) return 'Asia/Tokyo';
    if (570 == so && 570 == wo) return 'Australia/Darwin';
    if (570 == so && 630 == wo) return 'Australia/Adelaide';
    if (600 == so && 540 == wo) return 'Asia/Yakutsk';
    if (600 == so && 600 == wo) return 'Australia/Brisbane';
    if (600 == so && 660 == wo) return 'Australia/Sydney';
    if (630 == so && 660 == wo) return 'Australia/Lord_Howe';
    if (660 == so && 600 == wo) return 'Asia/Vladivostok';
    if (660 == so && 660 == wo) return 'Pacific/Guadalcanal';
    if (690 == so && 690 == wo) return 'Pacific/Norfolk';
    if (720 == so && 660 == wo) return 'Asia/Magadan';
    if (720 == so && 720 == wo) return 'Pacific/Fiji';
    if (720 == so && 780 == wo) return 'Pacific/Auckland';
    if (765 == so && 825 == wo) return 'Pacific/Chatham';
    if (780 == so && 780 == wo) return 'Pacific/Enderbury'
    if (840 == so && 840 == wo) return 'Pacific/Kiritimati';
    return ''; // empty means web-server timezone
}


function get_language_change_href(lang){
    var href   = window.location.href;
    var anchor  = "";
    
    if ( href.indexOf("#")>=0 ){
        anchor = href.substr(href.indexOf("#")+1);
        href   = href.substr(0,href.indexOf("#"));
    }

    if ( href.indexOf("language=") >=0 ){
        href1 = href.substr(0, href.indexOf("language="));
        href2 = href.substr(href.indexOf("language=")+9);
        if ( href2.indexOf('&')>=0) {
            href2 = href2.substr(href2.indexOf('&'));
            href  = href1 + 'language=' + lang + href2;
        }else{
            href  = href1 + 'language=' + lang;
        }
    }else if ( href.indexOf("?")>=0 ) {
        href = href + "&language=" + lang;
    }else{
        href = href + "?language=" + lang;
    }

    if ( anchor != null && anchor != "" ){
        href = href + "#" + anchor;
    }

    return href;
}

function change_language(){
    var sel = document.getElementById("language_selection");
    if ( sel==null ){
        return;
    }
    var index = sel.selectedIndex;
    if ( index==0 ){
        return;
    }
    var val = sel.options[index].value;
    var href = get_language_change_href(val);
    window.location.href = href;
}

function loadjscssfile(filename, filetype){
    var fileref;
    if (filetype=="js"){ //if filename is a external JavaScript file
        fileref=document.createElement('script');
        fileref.setAttribute("type","text/javascript");
        fileref.setAttribute("src", filename);
    }
    else if (filetype=="css"){ //if filename is an external CSS file
        fileref=document.createElement("link");
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", filename);
    }
    if (typeof fileref!="undefined"){
        document.getElementsByTagName("head")[0].appendChild(fileref);
    }
}

function removejscssfile(filename, filetype){
    var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
    var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
    var allsuspects=document.getElementsByTagName(targetelement)
    for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
        if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1){
            allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
        }
    }
}

function getBodyHeight(){
    var body_h=0;
    if (document.innerHeight ){
        body_h = document.innerHeight;
    }
    if ( body_h==0 && document.documentElement && document.documentElement.clientHeight ){
        body_h = document.documentElement.clientHeight;
    }
    if ( body_h==0 && document.body && document.body.clientHeight){
        body_h = document.body.clientHeight;
    }
    return body_h;
}

function getBodyWidth(){
    var body_w=0;
    if (document.innerWidth ){
        body_w = document.innerWidth;
    }
    if ( document.documentElement && document.documentElement.clientWidth ){
        body_w = document.documentElement.clientWidth;
    }
    if ( document.body && document.body.clientWidth){
        body_w = document.body.clientWidth;
    }
    return body_w;
}

function validation_integer_field(value){
    if ( value == "") return true;
    var validate = true;
    var mych;
    if ( value.substring(0,2) == "0x" || value.substring(0,2) == "0X" ){
        for ( i=2;i < value.length;i++){
            mych = value.charAt(i,i+1);
            if ( (mych<'0'||mych>'9') && (mych<'a' || mych>'f') && (mych<'A' || mych>'F')){
                validate = false;
                break;
            }
        }
    }else{
        for ( i=0;i < value.length;i++){
            mych = value.charAt(i);
            if ( mych<'0' || mych>'9' ){
                validate = false;
                break;
            }
        }
    }
    return validate;
}

function move_option_exceptfirst(from,to,countexcept){
    var  selectedItem;
    var count = 0;
    for ( var i=from.options.length-1; i>=countexcept; i--){
        if ( from.options[i].selected ){
            count++;
            selectedItem = from.options[i];
            from.remove(i);
            if ( document.all ){  //Internet Explorer
                to.add(selectedItem);
            }else{
                to.appendChild(selectedItem);
            }
        }
    }
    return count;
}

function move_option(from,to){
    return move_option_exceptfirst(from,to,0);
}

function remove_spaces(s){
    return s.split(' ').join('');
}

function remove_spaces_field(field){
    field.value = remove_spaces(field.value);
}

function LimitText(fieldObj,maxChars){
    var result = true;
    if (fieldObj.value.length >= maxChars){
        result = false;
    }
    if (window.event){
        window.event.returnValue = result;
    }
    return result;
}

function TruncateText(fieldObj, maxChars) {
    if (fieldObj.value.length > maxChars) {
        fieldObj.value = fieldObj.value.substring(0, maxChars);
    }
}

function TrackCount(fieldObj,countFieldName,maxChars){
    if ( fieldObj==null ) return;
    var countField = eval("fieldObj.form."+countFieldName);
    var diff = maxChars - fieldObj.value.length;
    if (diff < 0){
        fieldObj.value = fieldObj.value.substring(0,maxChars);
        diff = maxChars - fieldObj.value.length;
    }
    countField.value = diff;
}

