
// -*- java -*-
//
//   Copyright (C) 2004-2006 Zeljko Stevanovic
//   All Rights Reserved.
//
//   author_email: <zsteva@blic.net>
//

// ..................... //
//   FORMATIRANJE POLJA  //
// ..................... //

// todo:
// - validacija
//
//
//    integer, float, string, decimal, date, time, datetime

// PORTABLE

function getElement(id) {
	return (document.getElementById)
		? document.getElementById(id)
		: document.all[id];
} 

// END PORTABLE

var local_decimal_tsep   = '.';
var local_decimal_comma  = ',';
var system_decimal_comma = '.';

function set_decimal_sep(decimal_tsep, decimal_comma) {
	local_decimal_tsep  = decimal_tsep;
	local_decimal_comma = decimal_comma;
}

function get_decimal_tsep() { return local_decimal_tsep; }
function get_decimal_comma() { return local_decimal_comma; }

function comma(s) {
    s = String(s);
    var rg = /^(.*\d+)(\d{3}\b)/;
    return s == (s = s.replace(rg, "$1" + local_decimal_tsep + "$2"))
	? s : comma(s);
}

function decomma(s) {
    s = String(s);
    var re = new RegExp((local_decimal_tsep == ".") ? "\\." : local_decimal_tsep, 'g');
    var s2 = s.replace(re, "");
    return s2;
}

function parse_date(s) { return s; }
function format_date(s, s2) { return s; }

function parse_time(s) { return s; }
function format_time(s, s2) { return s; }

function parse_datetime(s) { return s; }
function format_datetime(s) { return s; }

function parse_integer(s) {
    s = String(Number(s));
    var p = s.split(local_decimal_comma);
    return Number(p[0]);
}

function format_integer(num, s2) { return num; }

function parse_float(s) {
    s = String(s);
    var p = s.split(local_decimal_comma);
    if (p.length > 2) return NaN;

    p[0] = decomma(p[0]);
    return Number(p.join("."));
}

function format_float(num, s2) {
    num = Number(num);
    s2  = Number(s2);
    if (isNaN(s2)) { s2 = 0; }

    if (isNaN(num)) return "NaN";
    var neg = false;
    if (num < 0) {
	neg = true;
	num = Math.abs(num);
    }
    var t = Number("1e" + s2);
    var s = String(Math.round(num * t) / t);
    var parts = s.split(system_decimal_comma);
    
    if (parts[0] == "") parts[0] = "0";
    if (!parts[1])      parts[1] = "";

    while (parts[1].length < s2)
        parts[1] += "0";

    // parts[0] = comma(parts[0]);

    if (s2 == 0)
	return (neg ? "-" : "" ) + parts[0];
    
    return (neg ? "-" : "" ) + parts.join(local_decimal_comma);
}

function parse_decimal(s, s2) {
    s = String(s);
    var p = s.split(local_decimal_comma);
    if (p.length > 2) return NaN;

    p[0] = decomma(p[0]);
    var num = Number(p.join(system_decimal_comma));
    return num;
}

function format_decimal(num, s2) {
    num = Number(num);
    s2  = Number(s2);
    if (isNaN(s2)) { s2 = 0; }

    if (isNaN(num)) { return "NaN"; }
    var neg = false;
    if (num < 0) {
	neg = true;
	num = Math.abs(num);
    }
    var t = Number("1e" + s2);
    var s = String(Math.round(num * t) / t);
    var parts = s.split(system_decimal_comma);
    
    if (parts[0] == "") parts[0] = "0";
    if (!parts[1])      parts[1] = "";

    while (parts[1].length < s2)
        parts[1] += "0";

    parts[0] = comma(parts[0]);

    if (s2 == 0) return (neg ? "-" : "" ) + parts[0];
    
    return (neg ? "-" : "" ) + parts.join(local_decimal_comma);
}

// .................... //
// ....  FIELD OP  .... //
// .................... //

function get_field(name) {
    return document.getElementById(name);
}

function get_field_value(f) {
    if (f == null || (f.nodeName != "DIV" && f.nodeName != "SPAN" && f.nodeName != "INPUT"))
	return null;
    var type  = f.getAttribute("type");
    var size2 = f.getAttribute("size2") || 2;

    var value = null;
//    if (f.nodeName == "SPAN") {
//	debug_props(f, "f");
//    }
    if (f.nodeName == "DIV" || f.nodeName == "SPAN") {
	if (f.textContent) // moz
		value = f.textContent;
	else // ie
		value = f.innerText;
    }
    if (f.nodeName == "INPUT") {
	value = f.value;
    }


    if (type == "integer")  { return Number(value); }
    if (type == "float")    { return parse_float(value, size2); }
    if (type == "string")   { return String(value); }
    if (type == "decimal")  { return parse_decimal(value, size2); }
    if (type == "date")     { return parse_date(value, size2); }
    if (type == "time")     { return parse_time(value, size2); }
    if (type == "datetime") { return parse_datetime(value, size2); }

    return value;
}

function set_field_value(f, val) {
    if (f == null || (f.nodeName != "DIV" && f.nodeName != "SPAN" && f.nodeName != "INPUT"))
	return false;
    var type  = f.getAttribute("type");
    var size2 = f.getAttribute("size2") || 2;

    var value = val;
    if (type == "integer")  { value = Number(val); }
    if (type == "float")    { value = format_float(val, size2); }
    if (type == "string")   { value = String(val); }
    if (type == "decimal")  { value = format_decimal(val, size2); }
    if (type == "date")     { value = val; }
    if (type == "time")     { value = val; }
    if (type == "datetime") { value = val; }

    if (f.nodeName == "DIV")   { f.textContent = value; return true; }
    if (f.nodeName == "SPAN")  { f.textContent = value; return true; }
    if (f.nodeName == "INPUT") { f.value = value;  return true; }
    return false;
}

function get_tfield_value(tbl, row, field) {
    if (tbl == null || tbl.nodeName != "TABLE") {
	alert("not table"); return null;
    }
    var col  = table_col_by_name(tbl, field);
    var cell = table_div_in_cell(tbl, row, col);
    if (cell == null) { alert("not cell"); return null; }

    return get_field_value(cell);
}

function set_tfield_value(tbl, row, field, value) {
    if (tbl == null || tbl.nodeName != "TABLE") return false;
    var col  = table_col_by_name(tbl, field);
    var cell = table_div_in_cell(tbl, row, col);
    if (cell == null) return false;

    return set_field_value(cell, value);
}


function set_message(msg) {
    var m = getElement("message");
    m.innerHTML = '&nbsp;' + msg;
}

