
// Copyright Babysoft. All Rights Reserved.

// check if the pass-in is the requested characters or not,
// if not add 0 at the front, eg: 31 => 00031 (param2: 5)
// Author: William Wong
// Param1: the value you want to add digit into
// Param2: the number of digits you want the digit to be
// Return: the new number with the correct number of digits
// Note: if the number of digits (param2) is less than or 
//       equals to the org digit, it will just return the 
//       original one
function chkDigitSize(value, digits){
    var size = 0;

    value = String(value);
    size = value.length;
    digits = Number(digits);

    if (size >= digits){
	return value;
    }
    else{
	size = digits - size;
	for (i=0; i < size; i++){
	    value = '0' + String(value);
	}
    }
    return value;

}



// Check weather this is a decimal number or not (xx.xx)
// Param1: the data you wish to check
// Return: true if the date is in valid format, otherwise false. 
function isValueDecimal(value){

    var input = value;
    var ary = new Array();
    var i=0;
    var number = -999;

    // Do not deal with empty case.
    if (input == ''){
	input = 0;
	// return true;
    }

    // check weather this is a full number with dot or not.

    // check all digits now
    /*for (i=0; i < input.length; i++){
        // Error, this is not a number
        if ( ! (((input.charCodeAt(i) <= 57) && (input.charCodeAt(i) >= 48))) &&
		(input.charCodeAt(i) != 46 && input.charCodeAt(i) != 45) ){
            return false;
        }
    }*/

    number = new Number(input);

    // Still an error
    if (isNaN(number) == true){
        return false;
    }

    value = number.toFixed(2);

    return true;

}


// Check is the field all numbers
// Param1: the data required to check
// Return: true if all value is digits, or empty. Otherwise false
function isValueNumber(value){
    var i=0;
    var number = value;

    // do not deal with empty field
    if (number.length == 0){
	return true;
    }

    // check all digits now
    for (i=0; i < number.length; i++){
        // Error, this is not a number
        if ( ! ((number.charCodeAt(i) <= 57) && (number.charCodeAt(i) >= 48)) ){
            return false;
        }
    }
    return true;
}


// Check to see is the postal code is correct format
// Param1: the postal code you wish to check
// Return: true if the postal code is wrong format or is empty, otherwise true
//         it will also automatically assign uppercase letter back to the field
function isValuePostalCode(value){

    var i=0;
    var input = value;

    input = value.toUpperCase();
    input = input.replace(/[ ]/g,'');

    // do not deal with empty field
    if (input.length == 0){
	return true;
    }

    if (input.length != 6){
	return false;
    }

    for (i=0; i < input.length; i++){
        // must be upper case characters
        if (i == 0 || i == 2 || i == 4){
	    // Error, this is not a capital character
	    if ( ! ((input.charCodeAt(i) <= 90) && (input.charCodeAt(i) >= 65)) ){
		return false;
	    }
        }
        // must be numbers
        else if (i == 1 || i == 3 || i == 5){
	    // Error, this is not a number
	    if ( ! ((input.charCodeAt(i) <= 57) && (input.charCodeAt(i) >= 48)) ){
		return false;
	    }
        }
    }

    return true;

}


// Validate email address
// Param1: the email you wish to check
// Return: true if the email field is empty or correct, otherwise false
function isValueEmail(value){

    var input = value;
    input = input.replace(/[ ]/g,'');

    if (input == ''){
        return true;
    }
    /* check valid email */
    else if (!((input.indexOf("@") == input.lastIndexOf("@") && input.indexOf("@")!=-1) && (input.indexOf(".")!=-1))){
        return false;
    }
    return true;

}





