﻿var showtimer;
var closetimer;
var IE = document.all?true:false;  

function EnableControl(ctl, enable){
    
    if (enable == true) {
        ctl.disabled = false;
    }
    else{
        ctl.disabled = true;
    }
}
// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.

// The function returns true if a valid date, false if not.
// ******************************************************************

function isDate(ele) {

    var dateStr = ele.value;
    if (dateStr.length == 0){
        return true;
    }

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        alert("Please enter the date as either MM/DD/YYYY or MM-DD-YYYY.");
        ele.focus();
        return false;
    }

    month = matchArray[1]; // p@rse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("The month must be between 1 and 12.");
        ele.focus();
        return false;
    }

    if (day < 1 || day > 31) {
        alert("The day must be between 1 and 31.");
        ele.focus();
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("The month "+month+" doesn`t have 31 days.")
        ele.focus();
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            alert("February " + year + " doesn`t have " + day + " days.");
            ele.focus();
            return false;
        }
    }
    return true; // date is valid
}

function setfocus(ctl){
    ctl.focus();
}

function ShowTooltip(ele, title, tip)
{
    CloseTooltip();
    var id = ele.id;

    dlgTooltip.set_alignmentElement(id);
    //dlgTooltip.set_title(title);     
    //dlgTooltip.set_content(tip);     

    //showtimer = window.setTimeout('dlgTooltip.Show("' + tip + '", "' + title + '");',500);
    dlgTooltip.Show(tip,title);
    
    closetimer = window.setTimeout('dlgTooltip.Close();',5000);
}

function CloseTooltip()
{
    //window.clearTimeout(showtimer);
    window.clearTimeout(closetimer);
    dlgTooltip.Close();
}

function formatCurrency(num, round) {
    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));
    
    if (round == true) {
        return (((sign)?'':'-') + '$' + num);
    }
    else{
        return (((sign)?'':'-') + '$' + num + '.' + cents);
    }
    
}

function formatPercentage(num, round){

    var n = new String(num);
    var p;

    if (n.indexOf("%", 0) == -1) {
        //remove the percent symbol
        n = n.replace("%", '');
        
        if(isNaN(num)) {
			alert(num + ' is not a percentage, please enter the value as a percentage.');
			return num;
        }
        else{
        
			//Convert to a float
			p = parseFloat(n);
	        
			if (p < 1){
				p = p * 100;
			}
	        
			return p + "%";
        }
    }
    else{
        //Property Formatted Percentage        
        return n;        
    }

}

function numbersonly(e, decimal) {
var key;
var keychar;

if (window.event) {
   key = window.event.keyCode;
}
else if (e) {
   key = e.which;
}
else {
   return true;
}
keychar = String.fromCharCode(key);

if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
   return true;
}
else if ((("0123456789-").indexOf(keychar) > -1)) {
   return true;
}
else if (decimal && (keychar == ".")) { 
  return true;
}
else
   return false;
}

function Navigate(URL){
    if (IE) {        
        window.navigate(URL); //character code is contained in IE's keyCode property
    }
    else{
        window.location.href = URL; //character code is contained in NN4's which property
    }  
}
function DownloadFile(URL){
    var Features = new String('');

    Features = 'toolbar=0,menubar=0,location=0,status=0,titlebar=0,resizable=0,scrollbars=0,height=50,width=50';

    window.open (URL, '', Features); 
    
}             
function OpenURL(URL, Toolbars){
    
    /* Window Features
    status  The status bar at the bottom of the window. 
    toolbar  The standard browser toolbar, with buttons such as Back and Forward. 
    location  The Location entry field where you enter the URL. 
    menubar  The menu bar of the window 
    directories  The standard browser directory buttons, such as What's New and What's Cool 
    resizable Allow/Disallow the user to resize the window. 
    scrollbars  Enable the scrollbars if the document is bigger than the window 
    height Specifies the height of the window in pixels. (example: height='350') 
    width  Specifies the width of the window in pixels. 
    */

    var Features = new String('');

    if (Toolbars == false) {
        Features = 'toolbar=0,';
        Features = 'menubar=0,';
    }

    Features = 'location=1,status=1,resizable=1,scrollbars=1';

    window.open (URL, '', Features); 
}

function HideModalBackground(){
	var d = document.getElementById("dvModal");
	d.className = 'hidden';
}
function ShowModalBackground(){
	var d = document.getElementById("dvModal");
	d.className = 'modal';
}
function ShowMobileModalBackground(){
	var d = document.getElementById("dvModal");
	d.className = 'mobile_modal';
}
function cb_onBeforeCallback(sender, eventArgs){
	ShowModalBackground();
}
function cb_onCallbackComplete(sender, eventArgs){
	HideModalBackground();
}