/**
 * @author vaibhaw
 */
function logIn(emailId, pwdId){
    return isValidEmail(document.getElementById(emailId).value.trim()) && checkPwd(pwdId);
}

function checkPwd(pwdId){
    if (document.getElementById(pwdId).value.length < 1) {
        alert("Password can not be blank.");
        return false;
    }
    return true;
}

function register(hostNameId, portId, emailId, pwdId, mobileId){
    var hostName = document.getElementById(hostNameId).value.trim();
    if (hostName.length == 0) {
        alert('Host name can not be empty.');
        return false;
    }
    
    var portNum = document.getElementById(portId).value.trim();
    if (!portNum.match("default") && !isNum(portNum)) {
        alert('Either enter "default" or type any numeric value as port number.');
        return false;
    }
    
    if (!isValidlogInPhone(emailId, pwdId, mobileId)) 
        return false;
    
    return true;
}

function easyReg(emailId, pwdId, mobileId){
    if (!isValidlogInPhone(emailId, pwdId, mobileId)) 
        return false;
    
    return true;
}

function update(allowdId, blockedId, pwdId, mobileId){
    if (!checkPwd(pwdId)) 
        return false;
    
    var mobile = document.getElementById(mobileId).value.trim();
    if (!isNum(mobile)) {
        alert('Phone must contains numbers only (0-9).');
        return false;
    }

    if (isNumberStartsWithZero(mobile)) {
        alert('Please enter the valid mobile number and don\'t add 0 as prefix.');
        return false;
    }
    
    return isFiltersConfigured(allowdId, blockedId);
}

function isValidStatus(statusId){
    if (document.getElementById(statusId).selectedIndex == 0) {
        alert('Please select a valid status.');
        return false;
    }
    return true;
}

function isValidlogInPhone(emailId, pwdId, mobileId){
    if (!logIn(emailId, pwdId)) 
        return false;
    
    var mobile = document.getElementById(mobileId).value.trim();
    if (!isNum(mobile)) {
        alert('Phone must contains numbers only (0-9).');
        return false;
    }
    
    if (isNumberStartsWithZero(mobile)) {
        alert('Please enter the valid mobile number and don\'t add 0 as prefix.');
        return false;
    }
    
    return true;
}

function isValidEmail(mail){
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var res = reg.test(mail);
    if (!res) 
        alert("Invalid Email-ID provided.\nValid format for Email-ID is: abc@pqr.yz");
    return res;
}

function isNum(num){
    return (/(^\d+$)/.test(num));
}

function isNumberStartsWithZero(mobile){
    return (/^0/.test(mobile));
}

function isFiltersConfigured(allowedId, blockedId){
    var allowEle = document.getElementById(allowedId).value.trim();
    var blockEle = document.getElementById(blockedId).value.trim();
    
    if (allowEle.length == 0 && blockEle.length == 0) 
        return confirm("Allowed and Blocked Lists both are empty. Configuring them correctly will help you to get relevant mails on mobile. Click on \"?\" image to know more about how to configure?.\n\nPress OK to update and Cancel to configure the lists?");
    
    return true;
}

String.prototype.trim = function(){
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
