/**
 * TSE general functions
 *
 */
function checkProtocol() {
	if(document.location.protocol == "http:")
	{
		document.location.assign(document.location.href.replace("http:", "https:"));
	}
}

function removeChildrenFromNode(nodeName)
{
    var node = $(nodeName);
    if(node == undefined && node == null)
    {
        return;
    }

    while (node.childNodes.length >= 1) {
        node.removeChild(node.firstChild);
    }
}

function setSelectValue(select, value) {
    for(var i = 0; i < select.options.length; i++) {
        if(select.options[i].value == value || select.options[i].text == value) {
            select.selectedIndex = i;
        }
    }
}

function setSelectText(select, value) {
    for(var i = 0; i < select.options.length; i++) {
        if(select.options[i].text == value) {
            select.selectedIndex = i;
        }
    }
}

function findRadioValue(name) {
    var objs = document.getElementsByName(name);
    if(!objs)
        return null;
    if(objs.value) {
        return objs.value;
    }
    for(var i = 0; i < objs.length; i++) {
        if(objs[i].checked){
            return objs[i].value;
        }
    }
    return 0;
}

function checkRadioValue(name, val) {
    var objs = document.getElementsByName(name);
    if(!objs)
        return null;
    if(objs.value) {
        return objs.value;
    }
    for(var i = 0; i < objs.length; i++) {
        if(objs[i].value == val) {
            objs[i].checked = true;
            objs[i].onclick();
        }
    }
    return 0;
}

function formatNumber(num, hideDecimal)
{
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    if(cents != "00")
        hideDecimal = false;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '' + num + ((hideDecimal) ? '' : '.' + cents));

}

function selectedValue(objId) {
    return $(objId).options[$(objId).selectedIndex].value;
}


/* This is much faster than using (el.innerHTML = str) when there are many
existing descendants, because in some browsers, innerHTML spends much longer
removing existing elements than it does creating new ones. */
function replaceHtml(el, html) {
        var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
        var newEl = document.createElement(oldEl.nodeName);
        // Preserve the element's id and class (other properties are lost)
        newEl.id = oldEl.id;
        newEl.className = oldEl.className;
        // Replace the old with the new
        newEl.innerHTML = html;
        oldEl.parentNode.replaceChild(newEl, oldEl);
        /* Since we just removed the old element from the DOM, return a reference
        to the new element, which can be used to restore variable references. */
        return newEl;
}

// For issue with IE 7 not able to create radio elements
function createRadioElement( name, checked ) {
    var radioInput;
    try {
        var radioHtml = '<input type="radio" name="' + name + '"';
        if ( checked ) {
            radioHtml += ' checked="checked"';
        }
        radioHtml += '/>';
        radioInput = document.createElement(radioHtml);
    } catch( err ) {
        radioInput = document.createElement('input');
        radioInput.setAttribute('type', 'radio');
        radioInput.setAttribute('name', name);
        if ( checked ) {
            radioInput.setAttribute('checked', 'checked');
        }
    }

    return radioInput;
}


function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

function formatGB(num) {
    if(num >= 1000) {
        return formatNumber(num / 1000, true) + ' TB';
    } else {
        return formatNumber(num, true) + ' GB';
    }
}

function nl2br(text){
    text = escape(text);
    if(text.indexOf('%0D%0A') > -1){
        re_nlchar = /%0D%0A/g ;
    }else if(text.indexOf('%0A') > -1){
        re_nlchar = /%0A/g ;
    }else if(text.indexOf('%0D') > -1){
        re_nlchar = /%0D/g ;
    }
    return unescape( text.replace(re_nlchar,'<br />') );
}

function printHTML(insideHTML, title){
    var myWindow=window.open(title,'_blank','width=640,height=500,scrollbars=yes,menubar=no,location=no');
    myWindow.document.write(insidehtml);
	myWindow.document.close();
	myWindow.focus();
	myWindow.print();
}

function printView(toUrl, title) {
    var myWindow=window.open(toUrl, title,'width=640,height=500,scrollbars=yes,menubar=no,location=no');
	myWindow.focus();
	myWindow.print();
}

