﻿// JScript File

function $(elementID)
{
    return document.getElementById(elementID);
}

function pageInit()
{
setNavigationCurrentPage();
}

function openSecurityStatement()
{
    var dialogFeatures = "";
    dialogFeatures += "height=600px,";
    dialogFeatures += "width=600px,";
    dialogFeatures += "toolbar=no,";
    dialogFeatures += "scrollbars=yes,";
        
    var dialogUrl = "/SecurityStatement.aspx";
    
    window.open(dialogUrl,null,dialogFeatures);
}

function LTrim(str)
{
    var whitespace = new String(" \t\n\r");
    var s = new String(str);    

    if (whitespace.indexOf(s.charAt(0)) != -1)
    {
        var j=0, i = s.length;
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
        {
            j++;
        }
        s = s.substring(j, i);
    }
    return s;
}

function RTrim(str)
{
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
 
    if (whitespace.indexOf(s.charAt(s.length-1)) != -1)
    {
        var i = s.length - 1;
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
        {
            i--;
        }
        s = s.substring(0, i+1);
    }
    return s;
}

function Trim(str)
{
    return RTrim(LTrim(str));
}

function $(elementID)
{
    if(elementID != null && elementID.length > 0)
    {
        return document.getElementById(elementID);
    }
    else  { return null; }
}


function checkNum(who)
{
    if (who.value.length == 0) 
    {
        return true;
    }
    if (isNaN(who.value))
    {
        alert("The field format is incorrect.");
        who.focus();
        who.select();
        return false;
    }
    return true;
}

function checkNumEnter(who)
{
    if (who.value.length == 0 ) 
    {
        return true;
    }
    if (isNaN(who.value))
    {
        document.body.focus();
        who.focus();
        who.select();
        return false;
    }
    return true;
}

function checkInt(who)
{
    if (who.value.length == 0) 
    {
        return true;
    }
    var numeric = /^\d*$/
    if (!numeric.test(who.value))
    {
        alert("The field format is incorrect.");
        who.select();
        return false;
    }
    return true;
}

function checkIntEnter(who)
{
    if (who.value.length == 0) 
    {
        return true;
    }
    var numeric = /^\d*$/
    if (!numeric.test(who.value))
    {
        document.body.focus();
        who.select();
        return false;
    }
    return true;
}

function checkZip(who)
{
    if (who.value.length == 0) 
    {
        return true;
    }
    var numeric = /\d{5}$/
    if (!numeric.test(who.value))
    {
        alert("The field format is incorrect.");
        setTimeout("setFoucs(\"" + who.id + "\");",2);
        return false;
    }
    return true;
}


function checkZipEnter(who)
{
    if (who.value.length == 0) 
    {
        return true;
    }
    var numeric = /\d{5}$/
    if (!numeric.test(who.value))
    {
        document.body.focus();
        who.focus();
        who.select();
        stopEvent(event);
        return false;
    }
    return true;
}

function checkMail(who)
{
    if (who.value.length == 0) 
    {
        return true;
    }
    var email = /(\S)+@(\S)+(\.)(\S)*/;
    if (!email.test(who.value))
    {
        alert("The field format is incorrect.");
        who.focus();        
        who.select();
        return false;
    }
    return true;
}

function checkMailEnter(who)
{
    if (who.value.length == 0) 
    {
        return true;
    }
    var email = /(\S)+@(\S)+(\.)(\S)*/;
    if (!email.test(who.value))
    {
        document.body.focus();
        who.focus();
        who.select();
        return false;
    }
    return true;
}

function checkUrl(who)
{
    if (who.value.length == 0) 
    {
        return true;
    }    
    if (who.value.toLowerCase().indexOf("http://") < 0)
    {
        alert("The field format is incorrect.");
        who.value = "http://";
        who.focus();        
        who.select();
        return false;
    }
    return true;
}

function checkUrlEnter(who)
{
    if (who.value.length == 0) 
    {
        return true;
    }    
    if (who.value.toLowerCase().indexOf("http://") < 0)
    {
        document.body.focus();
        who.focus();
        who.select();
        return false;
    }
    return true;
}

function checkMonthYear(who)
{
    if (who.value.length == 0) 
    {
        return true;
    }
    var monthYear = /^(0?[1-9]|10|11|12)\/((1[6-9]|[2-9]\d)\d{2})$/; 
    if(!monthYear.test(who.value))
    {
        alert("The field format is incorrect.");
        who.focus();        
        who.select();
        return false;
    }
    return true;
}

function checkMonthYearEnter(who)
{
    if (who.value.length == 0) 
    {
        return true;
    }
    var monthYear = /^(0?[1-9]|10|11|12)\/((1[6-9]|[2-9]\d)\d{2})$/; 
    if(!monthYear.test(who.value))
    {
        document.body.focus();
        who.focus();
        who.select();
        return false;
    }
    return true;
}

function checkMonthDayYear(who)
{
    if (who.value.length == 0) 
    {
        return true;
    }
    var monthYear = /^(0?[1-9]|10|11|12)\/(0?[1-9]|1[0-9]|2[0-9]|3[0-1])\/((1[6-9]|[2-9]\d)\d{2})$/;  
    if(!validateUSDateByString(who.value))
    {
        alert("The field format is incorrect.");
        who.focus();        
        who.select();
        return false;
    }
    return true;
}

function checkMonthDayYearEnter(who)
{
    if (who.value.length == 0) 
    {
        return true;
    }
    var monthYear = /^(0?[1-9]|10|11|12)\/(0?[1-9]|1[0-9]|2[0-9]|3[0-1])\/((1[6-9]|[2-9]\d)\d{2})$/;  
//    if(!monthYear.test(who.value))
    if(!validateUSDateByString(who.value))
    {
        document.body.focus();
        who.focus();
        who.select();
        return false;
    }
    return true;
}
function validateUSDateByString(dateString)
{
    var strs=dateString.split("index.html");
    if(strs!=null && strs.length==3)
    {
        var month = parseInt(strs[0],10);
        var day = parseInt(strs[1],10);
        var year = parseInt(strs[2],10);

        if (month.toString().length > 2 || month.toString().length < 1)
        {return false;}
        
        if (day.toString().length > 2 || day.toString().length < 1)
        {return false;}
        
        if (year.toString().length  != 4)
        {return false;}
        
        
        return validateUSDate(month, day, year);
    }
    
    return false;
}
function validateUSDate(month, day, year)   
{   
    //create a lookup for months not equal to Feb.   
    var arrayLookup = {1:31,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}     

    //check if month value and day value agree   
    if(arrayLookup[month] != null)   
    {   
        if(day <= arrayLookup[month] && day != 0)   
            return true;   //found in lookup table, good date   
    }   

    //check for February   
    if(month==2)
    {
        if((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
        {    if(day<=29) return true;}
        else
        {    if(day<=28) return true;}
//        if(((year % 4 == 0 && day <= 29)||(year % 4 != 0 && day <=28)) && day !=0)
//        {
//            return true;   //Feb. had valid number of days   
//        }  
    } 

    return false;   //any other values, bad date   
}

function stopEvent(e)
{
	if (!e) var e = window.event
	// handle event
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

//this function is used to show a security pop up when a customer click the email of the LO and try to send him a email.
function securityPopUp(pE)
{
    if(confirm("Email is not a secure method of communication and should NOT be used to send personal or financial information."))
    {
        return true;
    }
    else
    {
       if (!pE)
         if (window.event)
	    pE = window.event;
         else
	    return;
       if (pE.cancelBubble != null)
          pE.cancelBubble = true;
       if (pE.stopPropagation)
          pE.stopPropagation();
       if (pE.preventDefault)
          pE.preventDefault();
       if (window.event)
          pE.returnValue = false;
       if (pE.cancel != null)
          pE.cancel = true;
        return   false;   
    }
}





function checkDigits(which)
{

      formatNum(which);
                            //    var testvalue = which.value;
                            //    var numeric = /,/g;
                            //    var outValue = "";
                            //    testvalue = testvalue.replace(numeric,"");
                            //    numeric = /^\d*\.?\d*$/;
                            //    if (numeric.test(testvalue))
                            //    {
                            //        var values = testvalue.split(".");
                            //        var i = values[0].length;
                            //        while(i > 3)
                            //        {
                            //            outValue = "," + values[0].substring(i-3,i) + outValue;
                            //            i = i-3;
                            //        }
                            //        outValue = values[0].substring(0,i) + outValue;
                            //        if (values.length > 1)
                            //        {
                            //            outValue = outValue + "." + values[1];
                            //        }
                            //        
                            //        
                            //        var digit = outValue;
                            //        digit = digit.replace(/,/g,""); 
                            //        var location = digit.lastIndexOf(".");
                            //        if(location >= 0)
                            //        {
                            //            tempnum=digit*100;  
                            //            tempnum=Math.round(tempnum); 
                            //            tempnum=tempnum/100;
                            //            outValue = formatNumValue(tempnum.toString());
                            //        }
                            //    }
                            //    which.value = outValue;
}


function formatNum(which)
{  
    var testvalue = which.value;
    var numeric = /,/g;
    var outValue = "";
    testvalue = testvalue.replace(numeric,"");
    numeric = /^\d*\.?\d*$/;
    if (numeric.test(testvalue))
    {
        if (testvalue.length > 0 )
        {
            testvalue = Number(testvalue);
            testvalue = String(testvalue);
        }
        var values = testvalue.split(".");
        var i = values[0].length;
        while(i > 3)
        {
            outValue = "," + values[0].substring(i-3,i) + outValue;
            i = i-3;
        }
        outValue = values[0].substring(0,i) + outValue;
        if (values.length > 1)
        {
            outValue = outValue + "." + values[1];
        }
        
        
        var digit = outValue;
        digit = digit.replace(/,/g,""); 
        var location = digit.lastIndexOf(".");
        if(location >= 0)
        {
            tempnum=digit*100;  
            tempnum=Math.round(tempnum); 
            tempnum=tempnum/100;
            outValue = formatNumValue(tempnum.toString());
        }
    }
    which.value = outValue;
}


function formatNumValue(testvalue)
{  
    var numeric = /,/g;
    var outValue = "";
    testvalue = testvalue.replace(numeric,"");
    numeric = /^\d*\.?\d*$/;
    if (numeric.test(testvalue))
    {
        var values = testvalue.split(".");
        var i = values[0].length;
        while(i > 3)
        {
            outValue = "," + values[0].substring(i-3,i) + outValue;
            i = i-3;
        }
        outValue = values[0].substring(0,i) + outValue;
        if (values.length > 1)
        {
            outValue = outValue + "." + values[1];
        }
    }
    return outValue;
}


function checkNumsField(which)
{
    var testvalue = which.value;
    while(testvalue.indexOf(",")>0)
    {
        testvalue = testvalue.replace(",","");
    }
    if (!testvalue.length == 0 && isNaN(testvalue))
    {
        alert("The field format is incorrect.");
        return false;
    }
    if (testvalue.length > 0 )
    {
    formatNum(which);
    }  

    return true;
}
