﻿// JScript File
function floatValue(strValue)
{
    if (strValue != '')
    {
        var newStr = strValue
        newStr = newStr.replace(/%/g, "")
        newStr = newStr.replace(/\$/g, "")
        newStr = newStr.replace(/,/g, "")
        return parseFloat(newStr)
    }    
    else
    {
        return 0        
    }    
} 

function numberValue(strValue)
{
    if (strValue != '')
    {
        var newStr = strValue
        newStr = newStr.replace(/%/g, "")
        newStr = newStr.replace(/\$/g, "")
        newStr = newStr.replace(/,/g, "")  
        return Number(newStr)
    }    
    else
    {
        return 0        
    }    
} 

function getNodeValue(xmlDoc, nodeName)
{
    var tempNode  = xmlDoc.getElementsByTagName(nodeName)
    if (tempNode.length > 0)
    {
        if (tempNode[0].childNodes.length > 0)
           return tempNode[0].firstChild.text
        else
           return ''        
    }    
    else
    {
        return ''        
    }    
} 

function formatCurrency(strValue)
{
    strValue = strValue.toString().replace(/\$|\,/g,'');
    dblValue = parseFloat(strValue);

    blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
    dblValue = Math.floor(dblValue*100+0.50000000001);
    intCents = dblValue%100;
    strCents = intCents.toString();
    dblValue = Math.floor(dblValue/100).toString();
    if(intCents<10)
	    strCents = "0" + strCents;
    for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
	    dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
	    dblValue.substring(dblValue.length-(4*i+3));
    return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}

function formatCurrencyFlat(strValue)
{
    strValue = strValue.toString().replace(/\$|\,/g,'');
    dblValue = parseFloat(strValue);

    blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
    dblValue = Math.floor(dblValue*100+0.50000000001);
    dblValue = Math.floor(dblValue/100).toString();
    for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
	    dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
	    dblValue.substring(dblValue.length-(4*i+3));
    return (((blnSign)?'':'-') + '$' + dblValue);
}    
    
var IsShiftDown = false;
var IsCTRLDown = false;
var IsALTDown = false;

function MaskDate(field)
{
    var txtDate = field.value;
    var txtDateLength = txtDate.length;
    var slashPos = txtDate.length;
    if (txtDate != "")
    {
        slashPos = txtDate.indexOf("/",0)
        while (slashPos > -1)
        {            
            if (slashPos != 2 && slashPos != 5)
            {
                txtDate = txtDate.substring(0,slashPos) + txtDate.substring(slashPos + 2)
                slashPos = txtDate.indexOf("/",0)
            }
            else
            {
                slashPos = txtDate.indexOf("/",slashPos + 1)
            }
        }          
        if (txtDateLength >= 3 && txtDate.substring(2,3) != "/")
        {
            txtDate = txtDate.substring(0,2) + "/" + txtDate.substring(2)
        }
        if (txtDateLength >= 6 && txtDate.substring(5,6) != "/")
        {
            txtDate = txtDate.substring(0,5) + "/" + txtDate.substring(5)
        }   
        field.value = txtDate     
    }
}   
    
function FilterTextBoxKeyUp(eventKeyCode)
{
    // Capture Shift
    if (eventKeyCode == 16)
    {
        IsShiftDown = false;
    }
    
    // Capture CTRL
    if (eventKeyCode == 17)
    {
        IsCTRLDown = false;
    }
        
    // Capture ALT
    if (eventKeyCode == 18)
    {
        IsALTDown = false;
    } 
}    

function FilterTextBoxKeyDown(eventKeyCode, field, filterType, validChars)
{
    // Capture Shift
    if (eventKeyCode == 16)
    {
        IsShiftDown = true;
        return true;
    }
    
    // Capture CTRL
    if (eventKeyCode == 17)
    {
        IsCTRLDown = true;
        return true;
    }
        
    // Capture ALT
    if (eventKeyCode == 18)
    {
        IsALTDown = true;
        return true;
    }
            
    //alert("eventKeyCode=" + eventKeyCode + " field.value=" + field.value + " filterType=" + filterType + " validChars=" + validChars);
    
    // If There Is No Filter Type Then Just Retrun True
    if (filterType == "")    
    {
        return true;
    }

    // Allow All Auxillary Keys
    if ((eventKeyCode <= 46) || (eventKeyCode >= 91 && eventKeyCode <= 93) || (eventKeyCode >= 112 && eventKeyCode <= 145))
    {
        return true;     
    }   
    
    // Number Filter
    if ((eventKeyCode >= 48 && eventKeyCode <= 57 && IsShiftDown == false) || (eventKeyCode >= 96 && eventKeyCode <= 105))
    {
        if (filterType.indexOf("Numbers") != -1)    
        {
            return true;     
        }            
    }        

    // Upper Case Letter Filter
    if (eventKeyCode >= 65 && eventKeyCode <= 90 && IsShiftDown == true)
    {
        if (filterType.indexOf("UppercaseLetters") != -1)    
        {
            return true;     
        } 
    }        

    // Lower Case Letter Filter    
    if (eventKeyCode >= 65 && eventKeyCode <= 90 && IsShiftDown == false)
    {
        if (filterType.indexOf("LowercaseLetters") != -1)    
        {
            return true;     
        } 
    }  
    
    if (filterType.indexOf("Custom") != -1)    
    {
        // $
        if (IsShiftDown == true && eventKeyCode == 52 && validChars.indexOf("$") != -1)
        {
            return true; 
        }
        
        // %
        if (IsShiftDown == true && eventKeyCode == 53 && validChars.indexOf("%") != -1)
        {
            return true; 
        }        
        
        // (
        if (IsShiftDown == true && eventKeyCode == 57 && validChars.indexOf("(") != -1)
        {
            return true; 
        }                
        
        // )
        if (IsShiftDown == true && eventKeyCode == 48 && validChars.indexOf(")") != -1)
        {
            return true; 
        }          
        
        // - 
        if (((IsShiftDown == false && eventKeyCode == 109) || eventKeyCode == 189) && validChars.indexOf("-") != -1)
        {
            return true; 
        }                        
        
        // . 
        if ((IsShiftDown == false && eventKeyCode == 110 || eventKeyCode == 190) && validChars.indexOf(".") != -1)
        {
            return true; 
        }        
        
        // ,
        if ((IsShiftDown == false && eventKeyCode == 188) && validChars.indexOf(",") != -1)
        {
            return true; 
        }   
           
        // / (Foward Slash)
        if (eventKeyCode == 191 && validChars.indexOf("/") != -1)
        {
            return true; 
        }                                       
        
        // / (Back Slash)
        if (eventKeyCode == 220 && validChars.indexOf("\\") != -1)
        {
            return true; 
        }                                               
    }

    return false;
}
    
function ValidateFilteredTextBox(field, clientValidationFunction)
{
    if (clientValidationFunction != "")
    {
        alert("clientValidationFunction=" + clientValidationFunction);
    }
    return true;
}

function ProcessFilteredTextBoxChange(field)
{
    if (field.value != '')
    {
        field.style.color = "#0000ff";
        field.style.fontWeight = "bold";
    }
}
    
//Generating Pop-up Print Preview page
function getPrint(print_area, autoPrint)
{
    //Creating new page
    var pp = window.open();

    //Adding HTML opening tag with <HEAD> … </HEAD> portion
    pp.document.writeln('<HTML><HEAD><title>SMT.NET Print Preview</title>')
    pp.document.writeln('<LINK href="../App_Themes/Default/PrintStyle.css" type="text/css" rel="stylesheet">')
    pp.document.writeln('<LINK href="../App_Themes/Default/PrintStyle.css" type="text/css" rel="stylesheet" media="print">')
    pp.document.writeln('<base target="_self"></HEAD>')

    //Adding Body Tag
    pp.document.writeln('<body MS_POSITIONING="GridLayout" bottomMargin="0"');
    pp.document.writeln(' leftMargin="0" topMargin="0" rightMargin="0">');

    //Adding form Tag
    pp.document.writeln('<form method="post">');
    
    //Creating two buttons Print and Close within a HTML table
    pp.document.writeln('<TABLE style="vertical-align: top; text-align: left; width: 100%;" cellpadding="0" cellspacing="0" ><TR><TD></TD></TR><TR><TD align=right>');
    pp.document.writeln('<INPUT ID="PRINT" type="button" value="Print" ');
    pp.document.writeln('onclick="javascript:location.reload(true);window.print();">');
    pp.document.writeln('<INPUT ID="CLOSE" type="button" value="Close" onclick="window.close();">');
    pp.document.writeln('</TD></TR><TR><TD></TD></TR></TABLE>');
    
    var printHTML;
    // Code to Disable Inputs. The CSS properties do not work in IE
    printHTML = document.getElementById(print_area).innerHTML;
    printHTML = printHTML.replace(/<INPUT/gi, "<INPUT readonly='readonly'");
    printHTML = printHTML.replace(/<A/gi, "<A disabled='disabled'");
    printHTML = printHTML.replace(/<SELECT/gi, "<SELECT disabled='disabled'");
    printHTML = printHTML.replace(/<TEXTAREA/gi, "<TEXTAREA readonly='readonly'");
    printHTML = printHTML.replace(/window.onload/gi, "//window.onload");
    printHTML = printHTML.replace(/SMT.css/gi, "PrintStyle.css");
    printHTML = printHTML.replace(/overflow:scroll/gi, "overflow:visible");
    printHTML = printHTML.replace(/overflow: scroll/gi, "overflow:visible");
    printHTML = printHTML.replace(/overflow : scroll/gi, "overflow:visible");
    printHTML = printHTML.replace(/overflow-y:scroll/gi, "overflow:visible");
    printHTML = printHTML.replace(/overflow-y: scroll/gi, "overflow:visible");
    printHTML = printHTML.replace(/overflow-y : scroll/gi, "overflow:visible");
    printHTML = printHTML.replace(/overflow-x:scroll/gi, "overflow:visible");
    printHTML = printHTML.replace(/overflow-x: scroll/gi, "overflow:visible");
    printHTML = printHTML.replace(/overflow-x : scroll/gi, "overflow:visible");        
    // Remove All Javascript
    var startPos = 0
    var endTagPos = 0
    var endPos = 0
    
    startPos = printHTML.toUpperCase().indexOf("<!--NOPRINTBEGIN-->")
    endPos = printHTML.toUpperCase().indexOf("<!--NOPRINTEND-->")        
    while (startPos > -1 && endPos  > -1)
    {
        //alert(printHTML.substring(startPos,endPos + 17));
        printHTML = printHTML.substring(0,startPos) + printHTML.substring(endPos + 17)
        
        startPos = printHTML.toUpperCase().indexOf("<!--NOPRINTBEGIN-->")
        endPos = printHTML.toUpperCase().indexOf("<!--NOPRINTEND-->")            
    }
        
    startPos = printHTML.toUpperCase().indexOf("<SCRIPT")
    endPos = printHTML.toUpperCase().indexOf("</SCRIPT>")        
    while (startPos > -1 && endPos  > -1)
    {
        //alert(printHTML.substring(startPos,endPos + 9));
        printHTML = printHTML.substring(0,startPos) + printHTML.substring(endPos + 9)
        
        startPos = printHTML.toUpperCase().indexOf("<SCRIPT")
        endPos = printHTML.toUpperCase().indexOf("</SCRIPT>")            
    }
    startPos = printHTML.toUpperCase().indexOf("<TEXTAREA");
    if (startPos > -1)
        endTagPos = printHTML.toUpperCase().indexOf(">",startPos);
    while (startPos > -1 && endTagPos > -1)
    {
        printHTML = printHTML.substring(0,startPos) + "<table cellpadding=\"3\" cellspacing=\"3\" style=\"text-align: left; table-layout: fixed;\"><tr><td><pre style=\"text-align: left; width: 760px;\">" + printHTML.substring(endTagPos + 1)
        endPos = printHTML.toUpperCase().indexOf("</TEXTAREA>");
        printHTML = printHTML.substring(0,endPos) + "</pre></td></tr></table>" + printHTML.substring(endPos + 11)

        startPos = printHTML.toUpperCase().indexOf("<TEXTAREA");
        if (startPos > -1)
            endTagPos = printHTML.toUpperCase().indexOf(">",startPos);
    }    
    startPos = printHTML.toUpperCase().indexOf("<A ");
    if (startPos > -1)
        endTagPos = printHTML.toUpperCase().indexOf(">",startPos);
    while (startPos > -1 && endTagPos > -1)
    {
        printHTML = printHTML.substring(0,startPos) + "<DIV>" + printHTML.substring(endTagPos + 1)
        endPos = printHTML.toUpperCase().indexOf("</A>");
        printHTML = printHTML.substring(0,endPos) + "</DIV>" + printHTML.substring(endPos + 4)

        startPos = printHTML.toUpperCase().indexOf("<A ");
        if (startPos > -1)
            endTagPos = printHTML.toUpperCase().indexOf(">",startPos);
    }    
    
    //Writing print area of the calling page       
    pp.document.writeln(printHTML);

    //Ending Tag of </form>, </body> and </HTML>
    pp.document.writeln('</form></body>');
    if (autoPrint == true)
    {
        pp.document.writeln('<script type="text/javascript">window.print();window.close();</script>');
    }
    pp.document.writeln('</HTML>');
}   

function fireClickEvent(control)
{    
    if (document.all)
    {
        control.click();
    }
    else
    {
        var evt = document.createEvent("MouseEvents");
        evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        control.dispatchEvent(evt);        
    }
}

