



// ------------------------------------------------------------- HTML Algorithms

function htmlFormatNumber(val, decimals, min_len) {
    var v, str; 
    
    if (typeof(val) == "number") v = val; 
    else if (exIsControl(val)) v = val; 
    
    v = exToNumber(val); 
    if (isNaN(v)) v = 0; 
    
    str = new Number(v).toFixed(decimals); 
    if (min_len > 1)
        if (str.length < min_len)
            str = exRepeat("0", min_len - str.length) + str; 
        
    return str; 
}

function htmlFormatMoney(val, delim) {
    var v, str; 
    
    if (delim == undefined) delim = ","; 
    
    if (typeof(val) == "number") v = val; 
    else if (exIsControl(val)) v = val; 
    
    v = exToNumber(val); 
    if (isNaN(v)) v = 0; 
    
    str = htmlFormatNumber(v, 2); 
    for (var r = str.length - 7; r >= 0; r -= 3)
        str = str.substring(0, r+1) + delim + str.substring(r+1); 
    
    return str; 
}

function htmlIsStringSafe(str) {
    if (typeof(str) != "string") return false; 
    
    return str.match(/[\[\]\{\}\|\'\"\`]/) == null; 
}


// ------------------------------------------------------------- HTML Utilities

function htmlIsEmail(str) {
   if (typeof(str) != "string") return false; 
   
   var at = str.indexOf("@"); 
   if (at < 1) return false; 
   
   str = exTrim(str.substring(at+1)); 
   at = str.indexOf("."); 
   return (at > 0) && (at < str.length-1); 
}

function htmlIsPhone(str) {
    if (typeof(str) != "string") return false; 
    
    // contains only 0 1 2 3 4 5 6 7 8 9 + - * # p
    if (str.match(/^[ 0-9\+\-\*#p]*$/) == null) return false; 

    // contains 7+ digits.  
    return str.replace(/[ \+\-\*#p]/g, "").length >= 7; 
}

function htmlIsCitizenID(str) {
    if (typeof(str) != "string") return false; 
    return str.match(/^[0-9]{15}([0-9(x|X)]{3})?$/) != null; 
}

function htmlIsZipCode(str) {
    if (typeof(str) != "string") return false; 
    return str.match(/^[0-9]{6}$/) != null; 
}



// ------------------------------------------------------------- HTML Document

function htmlAppend(str) {
    document.body.innerHTML += str; 
}

function htmlPrint(str) {
    htmlAppend("<pre>" + str + "</pre>"); 
}

function htmlRequestEncode(str) {
    return escape(str); 
}

function htmlRedirect(url) {
    window.location.href = url; 
    return true; 
}

function htmlPopup(url, name, width, height, more) {
    var features = "scrollbars=no, toolbar=no, menubar=no, resizable=no"
        + ", location=no, status=no, left=0, top=0"; 
    if (!isNaN(width) && !isNaN(height))
        features += ", width="+width + ", height="+height; 
    if (more != undefined) features += ", " + more; 
    return window.open(url, name, features); 
}

function htmlFullscreen(url, name) {
    return htmlPopup(url, name, null, null, "fullscreen=1"); 
}

function htmlAutoClose(timeout) {
    setTimeout("self.close()", timeout); 
}


// ------------------------------------------------------------- HTML Controls

function htmlCheckCount(obj) {
    if (obj == null) return 0; 
    
    if (typeof(obj) == "string")
        obj = document.all.item(obj); 
    
    if (exIsArray(obj)) {
        var count = 0; 
        for (var i = 0; i < obj.length; i++)
            if (obj[i].checked) count++; 
        return count;
    }
    
    return obj.checked ? 1 : 0; 
}

function htmlCheckFirstIndex(obj) {
    if (obj == null) return 0; 
    
    if (typeof(obj) == "string")
        obj = document.all.item(obj); 
    
    if (exIsArray(obj)) {
        for (var i = 0; i < obj.length; i++)
            if (obj[i].checked) return i; 
        return -1;      // NONE WAS SELECTED
    }

    return 0; 
}

function htmlCheckFirstValue(obj) {
    if (obj == null) return 0; 
    
    if (typeof(obj) == "string")
        obj = document.all.item(obj); 
    
    if (exIsArray(obj)) {
        for (var i = 0; i < obj.length; i++)
            if (obj[i].checked) return obj[i].value; 
        return null;    // NONE WAS SELECTED
    }

    return obj.value; 
}

function htmlCheckRedirect(url, obj) {
    if (url == null || obj == null) return false; 
    if (obj == undefined) return false; 
    if (url == undefined) url = ""; 
    
    if (typeof(obj) == "string")
        obj = document.all.item(obj); 
        
    var check_count = htmlCheckCount(obj); 
    if (check_count == 0) return false; 
    if (check_count > 1) {
        alert(MSG_INVALID_CHECKS + check_count); 
        return false; 
    }
    
    var check_value = htmlCheckFirstValue(obj); 
    if (url.indexOf("?") < 0)
        url += "?" + obj.name + "=" + htmlRequestEncode(check_value); 
    else
        url += "&" + obj.name + "=" + htmlRequestEncode(check_value); 
        
    return htmlRedirect(url); 
}


function htmlCheckRedirectWithGate(url, obj, gate_obj) {
    if (url == null || obj == null) return false; 
    if (obj == undefined) return false; 
    if (url == undefined) url = ""; 
    
    if (typeof(obj) == "string")
        obj = document.all.item(obj); 
    if (typeof(gate_obj) == "string")
        gate_obj = document.all.item(gate_obj); 
    if (gate_obj == null) 
        return htmlCheckRedirect(url, obj); 
    
    var check_count = htmlCheckCount(obj); 
    if (check_count == 0) return false; 
    if (check_count > 1) {
        alert(MSG_INVALID_CHECKS + check_count); 
        return false; 
    }
    
    var check_index = htmlCheckFirstIndex(obj); 
    var gate_value =  exElementAt(gate_obj, check_index); 
    if (gate_value != null)
        if (exToNumber(gate_value.value) != 1) {
            alert(MSG_GATE_FAILED + gate_value.value); 
            return false; 
        }
    
    var check_value = htmlCheckFirstValue(obj); 
    if (url.indexOf("?") < 0)
        url += "?" + obj.name + "=" + htmlRequestEncode(check_value); 
    else
        url += "&" + obj.name + "=" + htmlRequestEncode(check_value); 
    
    return htmlRedirect(url); 
}

function htmlCheckRedirectWithURLs(obj, url_obj) {
    if (obj == null || url_obj == null) return false; 
    if (obj == undefined || url_obj == undefined) return false; 
    
    if (typeof(obj) == "string")
        obj = document.all.item(obj); 
    if (typeof(url_obj) == "string")
        url_obj = document.all.item(url_obj); 
    
    var check_count = htmlCheckCount(obj); 
    if (check_count == 0) return false; 
    if (check_count > 1) {
        alert(MSG_INVALID_CHECKS + check_count); 
        return false; 
    }
    
    var check_index = htmlCheckFirstIndex(obj); 
    var check_value = htmlCheckFirstValue(obj); 
    var url_value   = exElementAt(url_obj, check_index); 
    if (url_value == null) return null; 
    
    if (url_value.indexOf("?") < 0)
        url_value += "?" + obj.name + "=" + htmlRequestEncode(check_value); 
    else
        url_value += "&" + obj.name + "=" + htmlRequestEncode(check_value); 
        
    return htmlRedirect(url_value); 
}


function htmlSelectMove(source, target) {
    var index = source.selectedIndex; 
    if (index != -1) {
        var opt = document.createElement("option"); 
        opt.value = source.options[index].value; 
        opt.text  = source.options[index].text; 
        target.add(opt); 
        source.remove(index); 
    }
}

function htmlSelectMoveAll(source, target) {
    var opt;
    while (source.length > 0) {
        opt = document.createElement("option"); 
        opt.value = source.options[0].value; 
        opt.text  = source.options[0].text; 
        target.add(opt); 
        source.remove(0); 
    }
}

function htmlSelectAll(obj, chk) {
    var i, count = 0;
    if (obj == null) {
        for (i = 0; i < document.forms.length; i++) {
            count += htmlSelectAll(document.forms[i], chk); 
        }
    } else {
        if (typeof(obj) == "string")
            obj = document.all.item(obj); 
        
        if (exIsArray(obj)) {
            for (i = 0; i < obj.elements.length; i++) {
                var elm = obj.elements[i];
                var t = elm.type.toLowerCase();
                if (t == "checkbox") {
                    if (chk == 'x') {
                        elm.checked = ! elm.checked; 
                    } else {
                        elm.checked = chk;
                    }
                    count++;
                }
            }
        }
    }
    return count; 
}

function htmlRemoveAll(obj) {
    if (obj == null) return 0; 
    
    if (typeof(obj) == "string")
        obj = document.all.item(obj); 
    
    while (obj.length > 0)
        obj.remove(0);
}

function htmlFindOption(obj, value) {
    if (obj == null) return 0; 
    
    if (typeof(obj) == "string")
        obj = document.all.item(obj); 
    
	for (var i = 0; i < obj.length; i++) {
		if (obj.options[i].value == value) {
			obj.options[i].selected = true;
		}
	}
}

var clsid_OWC = new Array(
    '0002E500-0000-0000-C000-000000000046', // OWC9
    '0002E556-0000-0000-C000-000000000046', // OWC10
    '0002E55D-0000-0000-C000-000000000046' // OWC11
    ); 
function htmlCreateChartSpace(id, width, height) {
    var obj = tryCreateObject(clsid_OWC, 'Charts'); 
    obj.id = id; 
    obj.width = width; 
    obj.height = height; 
    return obj; 
}
function htmlGetOWCVersion() {
    var index = tryCreateObject_Index(clsid_OWC, 'Charts'); 
    if (index >= 0)
        index += 9;
    return index;
}
