﻿function $(id)
{
    return document.getElementById(id);
}

function left(str, n)
{
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0,n);
}

function isNumeric (s)
{
   var validChars = "0123456789";
   var isNumber = true;
   var c;
   for (i = 0; i < s.length && isNumber; i++) 
    { 
      c = s.charAt(i); 
      if (validChars.indexOf(c) == -1) 
        isNumber = false;
    }
    return isNumber;
 }

function centerDiv(id)
{
    var centerX, centerY;
    var x,y;
    if(window.outerWidth && window.outerHeight){
       centerX = window.outerWidth /2;
       centerY = window.outerHeight /2;       
    }else if(document.all && document.documentElement && document.documentElement.clientWidth && document.documentElement.clientHeight){
       centerX=document.documentElement.clientWidth /2;       
       centerY= document.documentElement.clientHeight /2;       
    }   
    try
    {
        x = parseInt($('__SCROLLPOSITIONX').value) + parseInt(centerX);   
    }
    catch(ex)
    {
        x = parseInt(centerX);
    }
    
    try
    {
        y = parseInt($('__SCROLLPOSITIONY').value) + parseInt(centerY);   
    }
    catch(ex)
    {
        y = parseInt(centerY);
    }  
    
    $(id).style.left =  x - ( $(id).clientWidth / 2) + 'px';          
    $(id).style.top =  y - ( $(id).clientHeight / 2) + 'px';     
}

function toggleAll(toggleAllBox, boxID)
{
    var f = toggleAllBox.form;
    for (var i=0; i<f.elements.length; i++)
    {
        var box = f.elements[i];
        if (box.name.indexOf(boxID) != -1)
            box.checked = toggleAllBox.checked
    }
}

function getNumberChecked(boxID)
{
    var count = 0;
    var elements = document.getElementsByTagName("INPUT");
    for (var i=0; i<elements.length; i++)
    {
        var box = elements[i];
        if (box.name.indexOf(boxID) != -1 && box.checked)
            count++;
    }
    return count;
}

function openPopupWindow(newURL, newWidth, newHeight, resize, scrollbars) 
{
    // Declare and initialize top and left variables
	var calcLeft = 100;
	var calcTop = 100;
	if (scrollbars=="") {scrollbars="no"}
		  
	// Update properties if comp. browser
	if (parseInt(navigator.appVersion) >= 4){
	calcTop = screen.availHeight /2 - newHeight / 2;
	calcLeft = screen.availWidth / 2 - newWidth / 2;}

	// Open the new window using top and left properties
	popup_win = window.open(newURL, 'remote', 'status=no,toolbar=no,menubar=no,location=no,scrollbars=' + scrollbars + ',width=' + newWidth + ',height=' + newHeight + ',left=' + calcLeft + ',top=' + calcTop + ',resizable=yes');
	popup_win.opener.name = "opener";
	popup_win.focus()
}

function toProperCase(text)
{
    var properCaseText = '';
    var words = text.split(' ');
    for (var i=0; i<words.length; i++)
    {
        if ((words[i].length > 2 && !isNumeric(words[i].charAt(0))) || words[i] == 'i' || i == 0)
            properCaseText += words[i].charAt(0).toUpperCase() + words[i].substring(1,words[i].length).toLowerCase() + ' ';
        else if (words[i].length > 0)
            properCaseText += words[i] + ' ';
    }
    return trim(properCaseText);
}

function trim(stringToTrim)
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function prepareFullWindow (target)
{
    window.open('', target, 'resizable=yes,scrollbars=auto,top=0,left=2,width=' + (screen.width - 15) + ',height=' + (screen.height - 60));
}
