﻿function openNewWindow(destination, windowName, x, y, scrollB, reSize)
{
    /* Set the default for ability to Resize window and Display Scrollbars */
    reSize=reSize || 'no'
    scrollB = scrollB || 'no'
    
    winLeft = Math.round((screen.width - x) / 2);
    winTop = Math.round((screen.height - y) / 2);
    
    (window.open(destination,windowName,'width='+x+',height='+y+',resizable=' + reSize + ',scrollbars=' + scrollB + ',top=' +winTop+',left='+winLeft)).focus();
    return false;
    
}

function resizeWindow(x,y)
{
    window.resizeTo(x,y);
    
    winLeft = Math.round((screen.width - x) / 2);
    winTop = Math.round((screen.height - y) / 2);
        
    self.moveTo(winLeft,winTop); 
}

/*
 *If you would like to reload the parent page, pass is 'Reload'
 */    
function closeWindow(reload)
{
    if(reload == 'Reload')
    {
        //Does PostBack   
        window.opener.__doPostBack('PopUpPostBack' ,'');
    }
    if(reload == "Refresh")
    {
        window.opener.location = window.opener.location;
    }
    self.close();
    return true;
}

function OpenTerms(clientID)
{
    link = document.getElementById(clientID);
    openNewWindow(link, 'Terms', '650','500','yes','yes');
}

/*
*  This function will display a warning message before an item is deleted from the grid
*/
function deleteWarning(itemDescription)
{
   if (confirm("You're about to delete " + itemDescription + ".\nAre You Sure?"))
   {
     if (confirm("This Cannot be undone, are you absolutely sure?"))
     {
        return true;
     }
     else
     {
        return false;
     }
   } 
   else
   {
      return false;
   }
}

function nextNearest(value, number) 
{
    if (!isNaN(value))
    {
        var converted = parseFloat(value); // Make sure we have a number
        var decimal = (converted - parseInt(converted, 10));
        decimal = Math.round(decimal * 10);
        
        var numberDecimal = (number - parseInt(number, 10));
        numberDecimal = Math.round(numberDecimal * 10);
        
        if (decimal == numberDecimal) 
        { 
            return (parseInt(converted, 10) + number); 
        }
        if ( (decimal < numberDecimal-2) || (decimal > numberDecimal+2) ) 
        {
            return Math.round(converted);
        } 
        else 
        {
            return (parseInt(converted, 10) + number);
        } 
    }
    else
    {
        return 0;
    }
}
   
function textCounter(field, maxlimit) 
{
    if (field.value.length > maxlimit)
    {
        field.value = field.value.substring(0, maxlimit);
    }
}

function replaceAll (OldString, Find, Replace)
{ 
    var st = OldString;
  if (Find.length == 0)
     return st;
  var idx = st.indexOf(Find);
  while (idx >= 0)        
  {  st = st.substring(0,idx) + Replace + st.substr(idx+Find.length);
     idx = st.indexOf(Find);
  }
  return st;
}

function CommaFormatted(amount)
{
      var delimiter = ","; // replace comma if desired
      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;
}

function CurrencyFormat(num)
{
    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;
    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 + '.' + cents);
}

function PercentFormat(num)
{
    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;
  
    return (((sign)?'':'-') + num + '.' + cents);

}

// Move an element directly on top of another element (and optionally
// make it the same size) Used by AJAX library .. Animation
function Cover(bottom, top, ignoreSize) {
    var location = Sys.UI.DomElement.getLocation(bottom);
    top.style.position = 'absolute';
    top.style.top = location.y + 'px';
    top.style.left = location.x + 'px';
    if (!ignoreSize) {
        top.style.height = bottom.offsetHeight + 'px';
        top.style.width = bottom.offsetWidth + 'px';
    }
}

    
/*
 * This function is used on gridviews to check/uncheck all selections.
 */
function SelectAllCheckboxes(spanChk)
{
    // Added as ASPX uses SPAN for checkbox
    var oItem = spanChk.children;
    var theBox = (spanChk.type == "checkbox") ? spanChk : spanChk.children.item[0];
    xState = theBox.checked;
    elm = theBox.form.elements;

    for (i = 0; i < elm.length; i++)
    {
        if (elm[i].type == "checkbox" && elm[i].id != theBox.id)
        {
            if (elm[i].checked != xState)
                elm[i].click();
        }
    }
}

/*
 * Alerts the user that they need to upgrade their account to user this feature.
 */
 function AlertUpgrade()
 {
    alert("Please upgrade your account to use this feature.");
 }
 
 function RemoveCommas(num)
 {
    var re = /,/g;
	num= num.replace(re,"");
	
	return num
 }
 
//Returns the local time on the clients machine
function GetLocalTime()
{
        var now = new Date();

        var hour        = now.getHours();
        var minute      = now.getMinutes();
        var second      = now.getSeconds();
        var monthnumber = now.getMonth();
        var monthday    = now.getDate();
        var year        = now.getYear();
        var ap = "AM"; 
        if (hour < 10)   {hour = "0"+hour;}
        if (minute < 10) {minute = "0"+minute;}
        if (hour   > 11) { ap = "PM";        }
        if (hour   > 12) { hour = hour - 12; }
        if (hour   == 0) { hour = 12;        }
       
        return hour+":"+minute+" "+ap
}