
/*******************************************************************************
 *                                                                             *
 * time4sleep - javascript client side validation code                         *
 *                                                                             *
 *******************************************************************************/

function properCase(strInput, strSplitChar)
{
    // converts a string to Proper Case
    if (strSplitChar == undefined)
    {
          // use space by default
          var strSplitChar = " ";
    }

    strArray = strInput.split(strSplitChar);

    for (var i = 0; i < strArray.length; i++)
    {
        if (strArray[i].length > 1)
        {
            strArray[i] = strArray[i].charAt(0).toUpperCase() + strArray[i].substr(1).toLowerCase();
        }
        else if (strArray[i].length == 1)
        {
            strArray[i] = strArray[i].toUpperCase();
        }
    }
    return strArray.join(" ");
}

function validFileType(fullpath, validtypes)
{
    // returns true if the file extension of the file is in the validtypes string
    // eg validtype = "gif,jpg,jpeg" (without a dot)

    var valid = false; // assume not a valid type
    var ext_start = fullpath.lastIndexOf(".");

    if (ext_start != -1)
    {
        var ext = fullpath.substring(ext_start + 1);
        ext = ext.replace(/(^\s)|(\s$)/g,"");
        ext = ext.toLowerCase()

        if (validtypes != "")
        {
            var arr = validtypes.split(",");

            for (var i = 0; i < arr.length; i++)
            {
                var type = arr[i];
                type = type.replace(/(^\s)|(\s$)/g,"");
                type = type.toLowerCase()

                if (type == ext)
                {
                    valid = true;
                    break;
                }
            }

        }

    }

    return valid;
}

/*
 * validate email address, check for illegal characters, check there is one
 * @ sign and at least one . after it. return true or false
 */
function validateEmail(tempEmail)
{

   var valid = true;
   len = tempEmail.length;

   if(len==0){
        valid = false;
   }

   spaces = tempEmail.indexOf(' ');
        // check for spaces
        if(spaces != -1)
                valid = false;

   ampers = tempEmail.indexOf('&');
        // check for ampersands
        if(ampers != -1)
                valid = false;

   at = tempEmail.indexOf('@');
        // check there is a at sign
        if(at == -1)
                valid = false;

   atmore = tempEmail.indexOf('@',(at+1));
        // check for more at signs
        if(atmore != -1)
                valid = false;

   dot = tempEmail.indexOf('.',at);
        // check for a dot after the at sign
        if(dot== -1)
                valid = false;

   if((at == 0)||(at== len))
   {
        // check where the at sign is
        valid = false;
   }

   return valid;
}

function isNumber(inputVal)
{
    oneDecimal = false;
    inputStr = inputVal.toString();
    for (var i = 0; i < inputStr.length; i++)
    {
        var oneChar = inputStr.charAt(i);
        if (i == 0 && oneChar == "-")
        {
            continue;
        }
        if (oneChar == "." && !oneDecimal)
        {
            oneDecimal = true;
            continue;
        }
        if (oneChar < "0" || oneChar > "9")
        {
            return false;
        }
    }
    return true;
}

