﻿// //////////////////////////////////////////////////////////// //

function ShowLayer(LayerId){
    if (document.getElementById(LayerId) != null) { document.getElementById(LayerId).style.display = ""; }
}
function HideLayer(LayerId) {
    if (document.getElementById(LayerId) != null) { document.getElementById(LayerId).style.display = "none"; }
}

function ShowLayers(LayerIdCsv){
    var layers = new Array();
    layers = LayerIdCsv.split(',');
    for(i=0;i<layers.length;i++){
        ShowLayer(layers[i]);
    }
}
function HideLayers(LayerIdCsv){
    var layers = new Array();
    layers = LayerIdCsv.split(',');
    for(i=0;i<layers.length;i++){
        HideLayer(layers[i]);
    }
}

function showLayersInRange(LayerTag, s, e) {
    for (i = s; i <= e; i++) { ShowLayer(LayerTag + i); }
}

function hideLayersInRange(LayerTag, s, e) {
    for (i = s; i <= e; i++) { HideLayer(LayerTag + i); }
}

function toggle(LayerId) {
    if (document.getElementById(LayerId) != null) { if (document.getElementById(LayerId).style.display == "") { HideLayer(LayerId); } else { ShowLayer(LayerId); } }
}
function toggleLayers(LayerIdCsv) {
    layers = LayerIdCsv.split(',');
    for (i = 0; i < layers.length; i++) { toggle(layers[i]); }
}
function toggleLayersInRange(LayerTag,s,e) {
    for (i = s; i <= e; i++) { toggle(LayerTag + i); }
}
// //////////////////////////////////////////////////////////// //


var submitButtonCounter = 0;
function DisableButton(obj) {
    if (submitButtonCounter == 1) { obj.disabled = true;}
    submitButtonCounter++;
}

// //////////////////////////////////////////////////////////// //
// Moves to the next input after a specific number of chars
// need to apper in a onkeyup="MoveNext(this,'objId',2);" call
function MoveNext(e, obj, nextObjId, numOfChars) {
   evt = e || window.event;
    var keyPressed = evt.which || evt.keyCode;
    if ((keyPressed == 9) || (keyPressed == 16)) {
       obj.select();
    }else{
        if (obj.value.length >= numOfChars) { document.getElementById(nextObjId).focus();}
    }
}

// //////////////////////////////////////////////////////////// //
// obj is the drop down to check against
// layer is a single layer or csv of layers
// stringArray is an array of values to check against.
function CheckDropDown(obj, layer, stringArray) {
    flag = false;
    arr = stringArray.split(',');
    for (i = 0; i < arr.length; i++) { if (arr[i] == obj.value) { flag = true; } }
    layerArray = layer.split(',');
    if (flag) {
        for (i = 0; i < layerArray.length; i++) { ShowLayer(layerArray[i]); }
    }else{
        for (i = 0; i < layerArray.length; i++) {HideLayer(layerArray[i]); }
    }
}


// //////////////////////////////////////////////////////////// //
// obj is the checkbox
// layer is the cell to show
// two function to for showing check and unchecked.
function CheckBoxChecked(obj, layer) {
    if (obj.checked) { ShowLayers(layer);
    } else {  HideLayers(layer); }
}
function CheckBoxUnChecked(obj, layer) {
   if (obj != null) { if (obj.checked) { ShowLayers(layer);
    } else { HideLayers(layer); } }
}

// //////////////////////////////////////////////////////////// //
// obj is the object to set the class on. it does not need a class to start with
// cls is the classs
function SetClass(obj, cls) {
    obj.setAttribute("class", cls);
    obj.setAttribute("className", cls);
}

// //////////////////////////////////////////////////////////// //
// objName is the object you want to have focus
function SetFocus(objName) {
    obj = document.getElementById(objName);
    if (obj != null) { obj.focus(); }
}

// //////////////////////////////////////////////////////////// //
function OpenIllustrationWindow(link) {
    var loc = window.location.toString().replace("http", "https");
    loc = loc.replace(window.location.pathname, "");
    window.open(loc + link, "Illustration", "width=650,height=400,toolbar=no,location=false,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes");
}
function OpenDealingDemo(link) {
    var loc = window.location.toString().replace("http", "https");
    loc = loc.replace(window.location.pathname, "");
    window.open(loc + link, "DealingDemo", "width=650,height=400,toolbar=no,location=false,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes");
}

// //////////////////////////////////////////////////////////// //
function FormatCurrency(obj) 
{
    try {
        if (isNaN(parseFloat(obj.value))) {  }
        else {
            value = obj.value.replace(",", "");
            obj.value = (value != "") ? FormatStringToCurrency(parseFloat(value)) : value;
        }
    }
    catch (e) 
    {
        obj.value = "";
    }
}

function FormatStringToCurrency(i) {

    if (!isNaN(i)) {
        i = Math.round(i * 100) / 100;
        s = new String(i);
        if (s.indexOf('.') < 0) { s += '.00'; }
        if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
        return s;
    }
}

function CommaFormatted(amount) {
    var delimiter = ","; // replace comma if desired
    amount = new String(amount);
    var a = amount.split('.', 2)
    var d = a[1];
    var i = parseInt(a[0]);
    if (isNaN(i)) { return ''; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while (n.length > 3) {
        var nn = n.substr(n.length - 3);
        a.unshift(nn);
        n = n.substr(0, n.length - 3);
    }
    if (n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if (d.length < 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;
    return amount;
}


// //////////////////////////////////////////////////////////// //
// image pre-loader for css images
function PreLoadCssImages() {
    images = new Array();
    images[0] = "/Resources/Images/Interface/Grey_56_Active.png";
    images[1] = "/Resources/Images/Interface/Grey_67_Active.png";
    images[2] = "/Resources/Images/Interface/Grey_89_Active.png";
    images[3] = "/Resources/Images/Interface/Grey_107_Active.png";
    images[4] = "/Resources/Images/Interface/Grey_127_Active.png";
    images[5] = "/Resources/Images/Interface/Grey_147_Active.png";
    images[6] = "/Resources/Images/Interface/Grey_165_Active.png";
    images[7] = "/Resources/Images/Interface/Grey_183_Active.png";
    images[8] = "/Resources/Images/Interface/Grey_205_Active.png";
    images[9] = "/Resources/Images/Interface/login_Active.png";
    images[10] = "/Resources/Images/Interface/Blue_01_Active.png";
    images[11] = "/Resources/Images/Interface/Blue_02_Active.png";
    images[12] = "/Resources/Images/Interface/Blue_03_Active.png";
    images[13] = "/Resources/Images/Interface/Blue_04_Active.png";
    images[14] = "/Resources/Images/Interface/Brown_01_Active.png";
    images[15] = "/Resources/Images/Interface/Brown_02_Active.png";
    images[16] = "/Resources/Images/Interface/Brown_03_Active.png";
    images[17] = "/Resources/Images/Interface/Brown_04_Active.png";
    images[18] = "/Resources/Images/Interface/Green_01_Active.png";
    images[19] = "/Resources/Images/Interface/Green_02_Active.png";
    images[20] = "/Resources/Images/Interface/Green_03_Active.png";
    images[21] = "/Resources/Images/Interface/Green_04_Active.png";
    imageObj = new Image();
    for (var i = 0; i <= 3; i++) { imageObj.src = images[i]; }
}
