﻿function firtLetterToUpper(name) 
{
    if( name.length > 1 )
        name = name.toString().charAt(0).toUpperCase() + name.substr(1);
    else
        name = name.toString().charAt(0).toUpperCase();
    return name;
}
function formatCurrency(num) 
{
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    num = Math.floor(num/100).toString();
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3)) + ',' +  num.substring(num.length-(4*i+3));
    return (((sign)?'':'-')+ num);
}
function validateEmpty(fld) {
    var error = "";
   fld.value = trim(fld.value);
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Mandatory field is empty!\n";//"Mandatory field is empty!\n";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You have not Entered the email address!\n";//"You have not Entered the email address!\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 28)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length.(less than 27 & more than 5)\n";//"The username is the wrong length.(less than 27 & more than 5)\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters.\n";//"The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
    var standardPass = /(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{7,15})$/; 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You have not entered the password!\n";//"You have not entered the password!\n";
    } else if ((fld.value.length < 8)) {
        error = "The password is the wrong length.(less than 14 & more than 7)\n";//"The password is the wrong length.(less than 14 & more than 7)\n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";//"The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    }
    else if( !standardPass.test(fld.value))
    {
        if((fld.value.length > 15))
        {
            error = "The password is the wrong length.(less than 14 & more than 7)\n";            
        }
        else
        {
            error = "The password should contains at least one digit.\n";//"The password contains illegal characters.\n";
        }
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
} 
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;///^([0][9][0-9]{0,11})$/;/////^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\'\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You have not entered the email address!\n";//"You have not entered the email address!\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Email address is not valid!\n";//"Email address is not valid!";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";//"The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateDigit(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var digitFilter = /^[0-9]*[0-9]$/;
    //var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        error="";
    } else if (!digitFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Just numbers will be accepted for this field!\n";//"Just numbers will be accepted for this field!";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateAreaCode(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var areaCodeFilter = /^(0){1}[1-9]{1}([0-9]*[0-9])$/;///^(0)([1-9])$/;//*([0-9])
    //var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You have not entered the area code!\n";//"You have not entered the area code!";
    } else if (!areaCodeFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "The area code is not valid!\n";//"The area code is not valid!";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
