﻿
// Login
function ValidateLogin()
{
    var bValid = true;
    
    if( ($("#txtUserName").val().length == 0) || ($("#txtPassword").val().length == 0) )
    {   
        $("#tdError div").fadeIn(300);
        document.getElementById("lblErrorMessage").innerHTML = " Please enter a Username and Password.";
        bValid = false;
    }
    
    return bValid;
}

function ValidateNumber(field) {
	var valid = "0123456789.$,"
	var ok = "yes";
	var temp;
	var fieldValue = field.value;
	
	if(fieldValue.indexOf("-") == 0)
	    fieldValue = fieldValue.replace("-", "");
	
	fieldValue = fieldValue.replace(/\$/g,'');
	fieldValue = fieldValue.replace(/\,/g,'');
	
	for (var i = 0; i < fieldValue.length; i++) 
	{
	    temp = "" + fieldValue.substring(i, i + 1);
	    if (valid.indexOf(temp) == "-1") {ok = "no";}
	}
	///////////////////
	periodcounter=0;
       for (x = 0; x < fieldValue.length; x++) {
          if (fieldValue.charAt(x) == ".")  {periodcounter++;}  
       }
    if (periodcounter>=2) {ok = "no";}
    
//    if(fieldValue.length == 0)
//     field.value = 0;
    
	if (ok == "no") 
    {
        alert("Invalid entry!  Only numeric values area accepted for this field");
        setTimeout('focusOn("' + field.id + '");',10);
        return false;
        //field.focus();
        //field.select();
    }
    else
        return true;
   
}


function ValidateNotNegative(objText)
{
    var sError = "";
    if(objText.value.length > 0)
    {
        if(!isNaN(objText.value))
        {
            if(objText.value < 0)
                sError = "Please enter a positive number.";
        }
    }
    
    if(sError.length > 0)
    {
        alert(sError);
        setTimeout('focusOn("' + objText.id + '");',10);
        return false;
    }
    else
        return true;
}

function focusOn( id ) {
var field = document.getElementById( id );
if ( field ) {
  field.focus();    // May throw "Permission denied" (uncatchable)
  field.select();
} else {
  alert( 'unknown section id "' + id + '"' );
}
}

function ScrubNumber(MyNum)
{
    MyNum = MyNum.replace(/\$/g,'');
    MyNum = MyNum.replace(/\,/g, '');
    MyNum = MyNum.replace(/\%/g, '');

    if (isNaN(MyNum) || MyNum == "")
    {
        MyNum = 0;
    }

	return MyNum;
}


 function RoundNumber(num, dec) {
    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    
    if(dec > 0)
    {
        var pos =  (result+"").indexOf(".");
        var iLoop = 0;
        
        if(pos == -1)
        {
            result += "."
            iLoop = dec;
        }
        else
        {
            var tmp = (result + "").substring(pos + 1, (result + "").length);
            iLoop = dec - tmp.length;
        }
        
        
        for(var x = 0; x < iLoop; x++)
        {
            result += "0";
        }
    }
   
    return result;
 }
 
 function AddCommas(nStr)
 {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function HTMLEncode(objID)
{
    var obj = document.getElementById(objID);
    var sEncode = obj.value.replace(/\</g, "&lt;").replace(/\>/g, "&gt;");
    alert(obj.value);
    alert(sEncode);
    obj.value = sEncode;
}

//For the iPad, Since it doesnt handle scrollable divs well, we will expand each of them so they do not scroll
var _bIsIPad = false;
function iPadExpandDivHeight()
{
    if (_bIsIPad)
    {
        $(".overflow").each(function () { if (this.scrollHeight > this.clientHeight) { $(this).height(this.scrollHeight + "px"); } });
    }
}

function ScriptTest(sMSSG)
{
    alert(sMSSG);
}

