/**
 *  This script will validate the inputs, all functions will return either a true
 *  or a false boolean value.
 */



/**
 *  @desc   Method to validate a url
 *  @param  string  url
 *  @return boolean
 */
function isValidUrl(url)
{
    if(url.match(/^(http|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#\=]\w+)*\/?$/i) ||
    url.match(/^mailto\:\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w{2,4}$/i))
    { return true; } else return false;
}

/**
 *  @desc   Method to check if the extention of a file is in fact an image.
 *  @param  string  fileName
 *  @return boolean
 */
function isValidImage(fileName)
{
    // Lower the case.
    fileName = fileName.toLowerCase();

    // Check if the extention is ok.
    if (!fileName.match(/(\.jpg|\.gif|\.png|\.JPG|\.GIF|\.PNG|\.jpeg|\.JPEG)$/)) return false;
    return true;
}

/**
 *  @desc   Method to check if the given e-mailaddress is valid.
 *  @param  string  emailAddress
 *  @return boolean
 */
function isValidEmail(emailAddress)
{
    if (emailAddress.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
    else return false;
}

/**
 *  @desc   Method to check if the given date is valid.
 *  @param  string  $date
 *  @param  string  $notation
 *  @return boolean
 */
function isValidDate(givenDate, notation)
{
    if(notation == 'YYYY-MM-DD')
    {
        trim(givenDate);
        var data = givenDate.split('-');
        return verifyDate(data[2], data[1], data[0]);
    }

    return false;
}

/**
 * @desc	Method to check if a supplied text is numeric.
 * @param 	string sText
 * @return	boolean
 */
function isNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
	   Char = sText.charAt(i); 
       if (ValidChars.indexOf(Char) == -1) 
       {
    	   IsNumber = false;
       }
   }
   
   return IsNumber;
}

/**
 * @desc	Method to check if a supplied text could be a valid directory name.
 * @param 	sText
 * @return	boolean
 */
function isValidDirectoryName(sText)
{
	 pattern = (/^[a-z0-9_\-\s]+$/gi); // Very simple check
	 return pattern.test(sText);
}

// Private method to verify the date.
function verifyDate(day,month,year)
{
    var sourceDate = new Date(year,month,day);
    
    if(year != sourceDate.getFullYear()) return false;
    if(month != sourceDate.getMonth())   return false;
    if(day != sourceDate.getDate())      return false;

    return true;
}


/**
 * @desc	Simple method to trim a value from a field. (used by a few methods in here).
 * @param 	string value
 * @return	trimmed value
 */
function trim(value)
{
    return value.replace(/^\s+|\s+$/,'');
}
