//--********************************************************************-->
//--                                                                    -->
//--   Frontier Software Pty Ltd                                        -->
//--                                                                    -->
//--   File Name     : ftrvalid.js                                      -->
//--                                                                    -->
//--   History       :                                                  -->
//--   005876 - 02.07.00 - JavaScript Rewrite.                          -->
//--   008619 - 26.05.03 - Pad user ID.                                 -->
//--   007634 - 29.08.03 - Calendar date fix                            -->
//--   009153 - 23.10.03 - validStringTrim() enhanced                   -->
//--   010146 - 23.09.04 - Changes To Leave Balances                    -->
//--   010219 - 25.10.04 - Null values entering ValidNumber             -->
//--   010208 - 07.12.04 - validFutureDate() addded                     -->
//--   011275 - 09.11.05 - Tidy up Date formats                         -->
//--********************************************************************-->

// *********************************************************************
// returns the translation of the string.
function validTranslation(strField2) {
    var temp = new String(strField2);
    if (temp.indexOf(cSeparator) != -1) {
        var ans = temp.split(cSeparator);
        return ans[1];
    } else {
        return temp;
    }
}

// *********************************************************************
// returns the code of the string
function validCode(strField) {
    var temp = new String(strField);
    if (temp.indexOf(cSeparator) != -1) {
        var ans = temp.split(cSeparator);
        return ans[0];
    } else {
        return temp;
    }
}

// *********************************************************************
// These methods perform various higher level date manipulation
// *********************************************************************
// The following date formats have been revised to handle changes of
// windows date formats.  They cater for changes of formats which occur
// while HR21 is in use.  If an unusable date format is entered in
// windows, the functions default to a Frontier format.
// *********************************************************************

// *********************************************************************
//ZBG 7322 - sets the year correctly if input isn't ccyy
function validCorrectYear(sYearIn) {
    var sYearOut = sYearIn;
    if (sYearIn.length < 4 || (isNaN(sYearIn))) {
        var dDate = new Date();
        var sTempYear = dDate.getFullYear();
        sTempYear = sTempYear.toString();
        var sYearOut = ((sYearIn.length == 2) ? sTempYear.substr(0,2) + sYearOut : sTempYear);
    }
    return sYearOut;
}

// *********************************************************************
// this function converts DMY to MDY if that is what the system expects
function validDMYtoMDY(sDateIn) {
    var dTemp = new Date("30/01/2000");
    var sDateOut, d, m, y, dateArr;
    if (Date.parse(dTemp) > 1000000000000) {
        // this system reads dates M/D/Y
        dateArr = sDateIn.split("/");
        if (dateArr.length == 3) {
            d = dateArr[1];
            m = dateArr[0];
            //ZBG 7322 - set correct year
            y = validCorrectYear(dateArr[2]);
            sDateOut = d + "/" + m + "/" + y;   // Format string for output.
        }
    } else {
        sDateOut = sDateIn;
    }
    return sDateOut;
}

// *********************************************************************
// function to check if sDate is a date
function validIsDate(sDate) {
    var oDate = new Date(sDate);
    var bTemp = isNaN(Date.parse(oDate));
    if (bTemp) {
        return false;
    } else {
        return true;
    }
}

// *********************************************************************
// function returns future date, specified by iDays.
// e.g. validFutureDate("21/11/2204",2) will return "23/11/2004"
function validFutureDate(sDate,iDays) {
    var dTemp = new Date();
    var aDate = sDate.split("/");
    if (aDate.length == 3) {
        dTemp.setMonth(aDate[1] - 1);
        dTemp.setYear(aDate[2]);
        dTemp.setDate(parseInt(aDate[0]) + iDays);
        sDate = dTemp.getDate() + "/" + (dTemp.getMonth() + 1) + "/" + dTemp.getFullYear();
    }
    return sDate;
}

// *********************************************************************
function validCompareDates(strFirstDate,strSecondDate,strDateDelimiter) {
     var firstDate,secondDate,dd,mm,yy;
     var secondsFirst,secondsSecond;
     //Splits the first date to its components
     firstDate = strFirstDate.split(strDateDelimiter);
     dd = firstDate[0];
     mm = firstDate[1];
     yy = firstDate[2];
     firstDate = new Date(yy,mm-1,dd);
     //Splits the second date to its components
     secondDate = strSecondDate.split(strDateDelimiter);
     dd = secondDate[0];
     mm = secondDate[1];
     yy = secondDate[2];
     secondDate = new Date(yy,mm-1,dd);
     //Compare the miliseconds in the dates
     if (parseInt(firstDate.valueOf()) < parseInt(secondDate.valueOf())) return (true)
     else return (false)
}

// *********************************************************************
// These methods perform various higher level string manipulation
// *********************************************************************

// *********************************************************************
// Pads string according to the size parameter
function validStringPad(sInput,iSize) {
    var sOutput = sInput;
    var iLength = iSize-sInput.length;
    for(var i=0; i < iLength; i++) {
          sOutput = "0" + sOutput;
    }
    return(sOutput);
}

// *********************************************************************
// formats a number to intDPs decimal places
function validNumber(numToFormat,intDPs) {
    var strFormattedNumber = "";
    if ((!isNaN(numToFormat)) || (numToFormat != "")) numToFormat = numToFormat.toString();
    if (numToFormat != "") {
        var bNegative = false;
        if (isNaN(numToFormat)) {
            if (numToFormat.indexOf('-') != -1) {
                bNegative = true;
            }
        } else {
            if (numToFormat < 0) {
                bNegative = true;
            }
        }
        numToFormat = (Math.round(parseFloat(numToFormat) * (Math.pow(10,parseInt(intDPs))))/Math.pow(10,parseInt(intDPs))).toString();
        var DPIndex = numToFormat.length;
        if (numToFormat.indexOf(".") != -1) {
            DPIndex = numToFormat.indexOf(".");
        }
        if (DPIndex < 1) {
            strFormattedNumber += "0";
        }
        var maxDPs = Math.max(0,(numToFormat.length - DPIndex - 1));
        strFormattedNumber += numToFormat.substr(0,(DPIndex));
        if (intDPs > 0) {
            strFormattedNumber += ".";  
            if (DPIndex < (numToFormat.length - 1)) {
                strFormattedNumber += numToFormat.substr((DPIndex+1),intDPs);
            }
            if (maxDPs < intDPs) {
                for (var i=0; i<(intDPs-maxDPs); i++) {
                    strFormattedNumber += "0";
                }
            }
        }
        if (bNegative && (strFormattedNumber.indexOf('-') == -1)) {
            strFormattedNumber = "-" + strFormattedNumber;
        }
    }
    // SQA 20525007: Return a numeric value of zero rather than a null string
    if (isNaN(strFormattedNumber)) return 0;
    else return strFormattedNumber;
}

// *********************************************************************
// Removes spaces from date strings
// 009153 - Altered so that any character can be removed, as specified by cInputChar.
// If cInputChar isn't specified then the space character is the default.
function validStringTrim(strToTrim, cInputChar) {
    var cCharToRemove = (cInputChar ? cInputChar : " ");
    var spcInx = strToTrim.indexOf(cCharToRemove);
    while(spcInx != -1){
        if(spcInx > 0){
            strToTrim = strToTrim.substr(0,spcInx) + strToTrim.substr(spcInx + 1);
        } else {
            strToTrim = strToTrim.substr(spcInx + 1);
        }       
        spcInx = strToTrim.indexOf(cCharToRemove);
    }
    return strToTrim;
}   

// *********************************************************************
// Searches for leading cOldChar and replaces with cNewChar in sInput
function validReplaceLeading(sOldValue, cOldChar, cNewChar) {
    var bLoad = false;
    var sNewValue = new String();
    for (var iIndex=0; iIndex<sOldValue.length;iIndex++) {
        var cOldValue = sOldValue.charAt(iIndex);
        if (cOldValue != cOldChar) {
            bLoad = true;
            sNewValue += cOldValue;
        } else if (bLoad) {
            sNewValue += cOldValue;
        } else if (cNewChar) {
            sNewValue += cNewChar;
        }
    }
    return sNewValue;
}   

// *********************************************************************
// Search for cOldChar and replace with cNewChar in sInput
function validReplaceString(sOldValue, cOldChar, cNewChar) {
    var sNewValue = sOldValue;
    for (var iIndex=1; iIndex<sNewValue.length; iIndex++) {
        if (sNewValue.substr(iIndex,1) == cOldChar) {
            // Replace cOldChar with cNewChar
            sNewValue = sNewValue.substr(0,iIndex-1) + cNewChar + sNewValue.substr(iIndex,sNewValue.length-iIndex);
        }
        return sNewValue;
    }
}

// *********************************************************************
// 008676 validStripInteger() will scan through a string until it comes 
// across an integer character. A string will be returned without the 
// integer character.
function validStripInteger(sInputString) {
    var sReturnString = sInputString;
    for (var i = 0; i < sInputString.length; i++) {
        var cChar = sInputString.charAt(i)
        if (!(cChar < "0" || cChar > "9")) return sInputString.substr(0,i);
    }
    return sReturnString;
}

// *********************************************************************
// 008676 validStripUnderScore() will scan through a string until it
// comes across an "_" character. A string will be returned without the 
// character.
function validStripUnderScore(sInputString) {
    var sReturnString = sInputString;
    var iDash = sInputString.indexOf("_");
    if (iDash != -1) sReturnString = sInputString.substr(0,iDash);
    return sReturnString;
}

// *********************************************************************
// Some miscellaneous functions
// *********************************************************************

// *********************************************************************
