﻿function DivToOpen(divOpenId, divFadeId)
{
    document.getElementById(divOpenId).style.display = 'block';
    if (divFadeId != null && document.getElementById(divFadeId) != null)
        document.getElementById(divFadeId).style.display = 'block';
    return false;
}


// special validation method using new Gen ControlUIDesignValidator methode

function TSpecialValidationControl(objCtrlToValidate, strMessage)
{

    var objControl = null;
    if (document.getElementById(objCtrlToValidate) == null)
        alert(objCtrlToValidate + " not found!");
    objControl = document.getElementById(objCtrlToValidate);
    ControlUIDesignValidator(objControl.id, strMessage, true);
    return true;

}



function DivToClose(divCloseId, divFadeId)
{
    document.getElementById(divCloseId).style.display = 'none';
    if (divFadeId != null && document.getElementById(divFadeId) != null)
        document.getElementById(divFadeId).style.display = 'none';
    return false;
}

function GetURLParamsValue(name)
{
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var tmpURL = window.location.href;
    var results = regex.exec(tmpURL);
    if (results == null) return "";
    else return results[1];
}
function TCheckEmailID(paraEmail)
{
    var email = document.getElementById(paraEmail).value;
    var re = /^(([^<>()[\]\=\\.,;:$#%?|+!*\s@^{}\'\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    if (email.match(re))
        return true;
    else
        return false;
}

function TCheckSpecialChar(vObject, vType, strCharsToExclude)
{
    var iChars;
    var tResult = true;
    if (vType == "PhoneNumber")
    {
        if (document.getElementById(vObject).value.length < 5)
        {
            tResult = false;
        }
        iChars = "`!@#$%^&*()=[]~\\\;,./{}|\:<>? ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'''";
    }
    else if (vType == "Name")
    {
        iChars = "`!@#$%^&*()=[]~\\\;,./{}|\:<>?1234567890";
    }
    else if (vType == "FileName")
    {
        iChars = "*\\\/|\":<>?.#"; // -@()+=[]`!#$%^;,{}
    }
    else if (vType == "Title")
    {
        iChars = "`~;|<>";  // ?.,/[]!-()'":
    }
    else if (vType == "ZipCode")
    {
        iChars = "`!@#$%^&*()+=[]\\\';,./{}|\":<>?";
    }
    else if (vType == "ZipCodeNumericWithDash")
    {
        iChars = "`!@#$%^&*()+=[]\\\';,./{}|\":<>?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    }
    else if (vType == "CompanyName")
    {
        iChars = "`#$%^*+=\\\{}|<>?"; //![]"()
    }
    else if (vType == "Address")
    {
        iChars = "`$%^/\"<>?"; //[]!()
    }
    else if (vType == "Password")
    {
        iChars = "<>'";
    }
    else if (vType == "HtmlTag")
    {
        iChars = "<>";
    }
    else if (vType == "Numeric")
    {
        iChars = "`!@#$%^&*()+=-[]\\\;,./{}|\:<>? ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'''";
    }

    var strCharsToIgnore = (strCharsToExclude == undefined) ? new String() : strCharsToExclude;

    if (tResult == false)
        return tResult;
    for (var i = 0; i < document.getElementById(vObject).value.length; i++)
    {
        if (strCharsToIgnore.indexOf(document.getElementById(vObject).value.charAt(i)) != -1)
        {
            continue;
        }
        if (iChars.indexOf(document.getElementById(vObject).value.charAt(i)) != -1)
        {
            document.getElementById(vObject).focus();
            return false;
        }
    }
    return true;
}

function TCheckWebSiteUrl(obj)
{
    var strValue = new String();
    strValue = obj.value;
    var regexp = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_%&\?\/.=]+$/;
    var match = strValue;
    var strFixPrefix = "http://";
    var strPrefix = strValue.substr(0, 7); // "http://";
    if (strPrefix == "http://")
    {
        match = strValue.substr(7, strValue.length);
        strFixPrefix = "http://";
    }
    else
    {
        strPrefix = strValue.substr(0, 8);
        if (strPrefix == "https://")
        {
            match = strValue.substr(8, strValue.length);
            strFixPrefix = "https://";
        }
    }

    if (!regexp.test(match))
        return false;
    else
    {
        obj.value = strFixPrefix + match;
        return true;
    }
}

function TValidateMinimumTextLength(objTextArea, iLength, strFieldName) // function returns boolean.
{
    if (objTextArea.value.length < (iLength))
    {
        WarningBox(strFieldName + ' is too small.\nMinimum allowed length is ' + iLength + ' characters.')
        return false;
    }
    return true;
}

function CheckMaxLength(obj, maxLength)
{
    var max = maxLength;
    if (document.getElementById(obj).value.length > max)
    {
        return false;
    }
    else
    {
        return true;
    }
}

function TIsWhiteSpace(strValue)
{
    var tResult = true;
    if (strValue.indexOf(' ') >= 0)
        tResult = false;
    return tResult;

}

function ReloadParentPage(strMesage)
{
    InfoBox(strMesage, "SpanTopStatus");
    var t = setTimeout("window.parent.location = window.parent.location.href;", 3000);

}

/**************************************************************/
/*  Validate US Phone number automatically through javascript */
/**************************************************************/

function checkPhoneAndFaxLength(object)
{
    var control = document.getElementsByName(object);

    if (control[0].value != "")
    {
        if (document.getElementById(object) != null)
        {
            var text = document.getElementById(object).value;
            var iLen = text.length;
            if (iLen < 14)
            {
                return false;
            }
        }
    }

    return true;
}

var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 14;
var phonevalue1;
var phonevalue2;
var cursorposition;

function ParseForNumber1(object)
{
    phonevalue1 = ParseChar(object.value, zChar);
}

function ParseForNumber2(object)
{
    phonevalue2 = ParseChar(object.value, zChar);
}

function backspacerUP(object, e)
{
    if (e)
    {
        e = e
    } else
    {
        e = window.event
    }
    if (e.which)
    {
        var keycode = e.which
    } else
    {
        var keycode = e.keyCode
    }

    ParseForNumber1(object)

    if (keycode >= 48)
    {
        ValidatePhone(object)
    }
}

function backspacerDOWN(object, e)
{
    if (e)
    {
        e = e
    } else
    {
        e = window.event
    }
    if (e.which)
    {
        var keycode = e.which
    } else
    {
        var keycode = e.keyCode
    }
    ParseForNumber2(object)
}

function GetCursorPosition()
{

    var t1 = phonevalue1;
    var t2 = phonevalue2;
    var bool = false
    for (i = 0; i < t1.length; i++)
    {
        if (t1.substring(i, 1) != t2.substring(i, 1))
        {
            if (!bool)
            {
                cursorposition = i
                window.status = cursorposition
                bool = true
            }
        }
    }
}

function ValidatePhone(object)
{

    var p = phonevalue1

    p = p.replace(/[^\d]*/gi, "")

    if (p.length < 3)
    {
        object.value = p
    } else if (p.length == 3)
    {
        pp = p;
        d4 = p.indexOf('(')
        d5 = p.indexOf(')')
        if (d4 == -1)
        {
            pp = "(" + pp;
        }
        if (d5 == -1)
        {
            pp = pp + ")";
        }
        object.value = pp;
    } else if (p.length > 3 && p.length < 7)
    {
        p = "(" + p;
        l30 = p.length;
        p30 = p.substring(0, 4);
        p30 = p30 + ") "

        p31 = p.substring(4, l30);
        pp = p30 + p31;

        object.value = pp;

    } else if (p.length >= 7)
    {
        p = "(" + p;
        l30 = p.length;
        p30 = p.substring(0, 4);
        p30 = p30 + ") "

        p31 = p.substring(4, l30);
        pp = p30 + p31;

        l40 = pp.length;
        p40 = pp.substring(0, 9);
        p40 = p40 + "-"

        p41 = pp.substring(9, l40);
        ppp = p40 + p41;

        object.value = ppp.substring(0, maxphonelength);
    }

    GetCursorPosition()

    if (cursorposition >= 0)
    {
        if (cursorposition == 0)
        {
            cursorposition = 2
        } else if (cursorposition <= 2)
        {
            cursorposition = cursorposition + 1
        } else if (cursorposition <= 4)
        {
            cursorposition = cursorposition + 3
        } else if (cursorposition == 5)
        {
            cursorposition = cursorposition + 3
        } else if (cursorposition == 6)
        {
            cursorposition = cursorposition + 3
        } else if (cursorposition == 7)
        {
            cursorposition = cursorposition + 4
        } else if (cursorposition == 8)
        {
            cursorposition = cursorposition + 4
            e1 = object.value.indexOf(')')
            e2 = object.value.indexOf('-')
            if (e1 > -1 && e2 > -1)
            {
                if (e2 - e1 == 4)
                {
                    cursorposition = cursorposition - 1
                }
            }
        } else if (cursorposition == 9)
        {
            cursorposition = cursorposition + 4
        } else if (cursorposition < 11)
        {
            cursorposition = cursorposition + 3
        } else if (cursorposition == 11)
        {
            cursorposition = cursorposition + 1
        } else if (cursorposition == 12)
        {
            cursorposition = cursorposition + 1
        } else if (cursorposition >= 13)
        {
            cursorposition = cursorposition
        }

        var txtRange = object.createTextRange();
        txtRange.moveStart("character", cursorposition);
        txtRange.moveEnd("character", cursorposition - object.value.length);
        txtRange.select();
    }

}

function ParseChar(sStr, sChar)
{

    if (sChar.length == null)
    {
        zChar = new Array(sChar);
    }
    else zChar = sChar;

    for (i = 0; i < zChar.length; i++)
    {
        sNewStr = "";

        var iStart = 0;
        var iEnd = sStr.indexOf(sChar[i]);

        while (iEnd != -1)
        {
            sNewStr += sStr.substring(iStart, iEnd);
            iStart = iEnd + 1;
            iEnd = sStr.indexOf(sChar[i], iStart);
        }
        sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

        sStr = sNewStr;
    }

    return sNewStr;
}

String.prototype.startsWith = function (str)
{
    return !this.indexOf(str);
}
/**************************************************************/
/*  Validate US Phone number automatically through javascript */
/**************************************************************/

/* Start - Date validation related to date with month and day per months and leap year */

// Declaring valid date character, minimum year and maximum year
var dtCh = "/";
var minYear = 1900;
var maxYear = 2075;

function isInteger(s)
{
    var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary(year)
{
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
function DaysArray(n)
{
    for (var i = 1; i <= n; i++)
    {
        this[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
        if (i == 2) { this[i] = 29 }
    }
    return this
}

function isDate(dtStr, strValueToCheck)
{
    var daysInMonth = DaysArray(12)
    var pos1 = dtStr.indexOf(dtCh)
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
    var strMonth = dtStr.substring(0, pos1)
    var strDay = dtStr.substring(pos1 + 1, pos2)
    var strYear = dtStr.substring(pos2 + 1)
    strYr = strYear
    if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
    if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
    for (var i = 1; i <= 3; i++)
    {
        if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
    }
    month = parseInt(strMonth)
    day = parseInt(strDay)
    year = parseInt(strYr)
    if (pos1 == -1 || pos2 == -1)
    {
        WarningBox("The " + strValueToCheck + " date format should be : mm/dd/yyyy");
        return false
    }
    if (strMonth.length < 1 || month < 1 || month > 12)
    {
        WarningBox("Please enter a valid month in " + strValueToCheck + "!");
        return false
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month])
    {
        WarningBox("Please enter a valid day in " + strValueToCheck + "!");
        return false
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear)
    {
        WarningBox("Invalid value for year in " + strValueToCheck + "! Please enter a valid year between " + minYear + " and " + maxYear + ".");
        return false
    }
    if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false)
    {
        WarningBox("Please enter a valid " + strValueToCheck + "!");
        return false
    }
    return true
}

/* End - Date validation related to date with month and day per months and leap year */

function Expand(oLink, idObj, strAnchorId)
{
    var oObj = document.getElementById(idObj);
    var strVal = $('#ulNewsList li #' + idObj)[0].style.display;
    $('#ulNewsList li div').hide();
    $('#ulNewsList li a').removeClass('newsactivetab');
    if (strVal == 'none')
    {
        $('#ulNewsList li #' + strAnchorId).addClass('newsactivetab');
        $('#ulNewsList li #' + idObj).delay().fadeIn().delay(100);
    }
    else
    {
        $('#ulNewsList li #' + idObj).fadeOut().delay(1000);

    }

}

function GetPageName()
{
    var strURL = document.URL;
    var astrURL = strURL.split('?');
    return astrURL[0];
}

function SetFrontTabClass()
{
    var strPage = GetPageName();
    $("#ulMainTab li a").removeClass("active");
    if (strPage.indexOf("employment.aspx") > -1)
    {
        $("#ulMainTab li #aEmployment").addClass("active");
    }
    else if (strPage.indexOf("contact-us.aspx") > -1)
    {
        $("#ulMainTab li #aContactUs").addClass("active");
    }
    else if (strPage.indexOf("comment-card.aspx") > -1)
    {
        $("#ulMainTab li #aCommentCard").addClass("active");
    }
    else if (strPage.indexOf("news.aspx") > -1 || strPage.indexOf("news-archive.aspx") > -1)
    {
        $("#ulMainTab li #aNews").addClass("active");
    }
    else if (strPage.indexOf("default.aspx") > -1)
    {
        $("#ulMainTab li #aHome").addClass("active");
    }
    else if (strPage.indexOf("fleet.aspx") > -1)
    {
        $("#ulMainTab li #aFleet").addClass("active");
    }
    else
    {
        $("#divNews").removeClass("right-news");
        $("#divNews").addClass("right-news1");
    }

}


function DispalyMessage(strMessage)
{
    document.getElementById("divfade").style.display = "none";
    document.getElementById("divProcessing").style.display = "none";
    InfoBox(strMessage);
    setTimeout("RedirectToPage();", 3000);
}

function RedirectToPage()
{
    window.location = "default.aspx";
}

function InfoMessage(strMessage)
{
    InfoBox(strMessage);
    setTimeout("ClearTopStatus();", 2000);

}

function SessionExpirationAdmin(strMessage)
{

    ErrorBox(strMessage);

    setTimeout("window.parent.location.href='login.aspx?logout=true'", 3000);

}


function BackToPreviousPage(strPageName)
{
    var strCurrentPage = GetCurrentPageName();

    var strUrl = document.referrer;

    if (strUrl == '')
        strUrl = strPageName;

    if (strUrl.indexOf("login.aspx") > 0)
        window.location = strPageName;
    else if (strUrl.indexOf(strCurrentPage) > 0)
        window.location = strPageName;
    else
        window.location = strUrl;
}

function GetCurrentPageName()
{
    var strPath = window.location.pathname;
    var strPage = strPath.substring(strPath.lastIndexOf('/') + 1);
    return strPage.toLowerCase();
}

function PrintData(divId, strPrintType)
{
    var strPrintType = strPrintType;
    var strHeader = "";
    if (strPrintType == "Inquiry")
        strHeader = "<h3 id=\"hTitle\" class=\"title1\">Inquiry Details</h3>";
    else if (strPrintType == "CommentCard")
        strHeader = "<h3 id=\"hTitle\" class=\"title1\">Comment Card Details</h3>";



    var prtContent = document.getElementById(divId);
    var strDivInnerHTML = prtContent.innerHTML;

    var strFinalContent = strHeader + strDivInnerHTML;

    var WinPrint = window.open("", "", 'left=0,top=0,width=900,height=445,toolbar=0,scrollbars=1,status=0');
    WinPrint.document.write("<link type=text/css rel=stylesheet >");
    WinPrint.document.write(strFinalContent);
    WinPrint.document.close();
    WinPrint.focus();
    WinPrint.print();
    WinPrint.close();
}
