/////////////////////////////////////////////////////////////////////////////////////////
// Removes the leading and trailing spaces in a strings and returns the trimmed string
/////////////////////////////////////////////////////////////////////////////////////////
function trimSpaces(stringValue) {
    // Checks the first occurance of spaces and removes them
    for(i = 0; i < stringValue.length; i++) {
        if(stringValue.charAt(i) != " ") {
            break;
        }
    }
    if(i > 0) {
        stringValue = stringValue.substring(i);
    }
    
    // Checks the last occurance of spaces and removes them
    strLength = stringValue.length - 1;
    for(i = strLength; i >= 0; i--) {
        if(stringValue.charAt(i) != " ") {
            break;
        }
    }
    if(i < strLength) {
        stringValue = stringValue.substring(0, i + 1);
    }
    
    // Returns the string after removing leading and trailing spaces.
    return stringValue;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Checks whether a string is a valid email address.
/////////////////////////////////////////////////////////////////////////////////////////
function checkEmail(emailString) {
	
	   splitVal = emailString.split('@');
    
    if(splitVal.length <= 1) {
        //alert("Please enter a valid email address");
        return false;
    }
    if(splitVal[0].length <= 0 || splitVal[1].length <= 0) {
        //alert("Please enter a valid email address");
        return false;
    }
    
    splitDomain = splitVal[1].split('.');
    if(splitDomain.length <= 1) {
        //alert("Please enter a valid email address");
        return false;
    }
    if(splitDomain[0].length <= 0 || splitDomain[1].length <= 1) {
        //alert("Please enter a valid email address");
        return false;
    }
    return true;
}

jQuery.validator.addMethod("file_size", function(value, element)
{ 
    var element_id = element.id    
    return checkFileSize(element_id);
}, "Maximum allowded size is 2 MB");

function checkFileSize(element_id)
{
    var fileInput = $("#" + element_id)[0];
    
    //alert(fileInput.value);
    if (navigator.appName == "Microsoft Internet Explorer") {
        if (fileInput.value) {
            var oas = new ActiveXObject("Scripting.FileSystemObject");
            var e = oas.getFile(fileInput.value);
            var size = e.size;
        }
    }
    else if (fileInput.value != null && fileInput.files[0] != undefined)
    {
        var size = fileInput.files[0].fileSize;
    }
    if (size > 2097152)
        return false;
    else
        return true;  
}
