var emptyString         = /^\s*$/;
var err_blank           = "Please don't leave any fields blank";
var nbsp                = 160;    // non-breaking space char
var valid_chars         = "Please use only valid characters";
var number_gt_zero      = " should be a number and greater than Zero (only 2 decimals allowed)";
var fill_mandetory_flds = "Please fill all mandetory fields";
var confirm_delete      = "Are you sure you want to delete this record?";
var confirm_delete_pic  = "Are you sure you want to delete the picture?";
var only_numbers        = " should be only numbers and greater than Zero ";
var telephone_error     = " should be more than 9 digits and only numbers and dashes are allowed (e.g. 416-564-4534)";
var email_error         = " is invalid Email";


//--------------------------------
//checks database related invalid
//characters - chars that shouldn't
//be entered in a database related
//form
//--------------------------------
function common_input_validation(str){
    var passed=false;
    var fltr= /^[a-z\s0-9\-\\\/:\_\.\@]*$/i;
    var str_error="";

    if(fltr.test(str)){
        passed=true;
    }
    return passed;
}

function check_mail(email){
    var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    if(re.test(email)){
        return true
    }
    else{
        return false
    }
}

//--------------------------------
//this neat function displays the
//errors in the form where errors
//are supposed to be shown
//all you have to do is pass it
//the tag id of the table and it
//display it in that table and
//in that tag
//--------------------------------
function msg(fld,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
  // setting an empty string can give problems if later set to a
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  if (emptyString.test(message))
    dispmessage = String.fromCharCode(nbsp);
  else
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;
  elem.className = msgtype;
};

function is_number(val){
    var re = /^\d{1,}\.?\d{0,2}$/;
    if(re.test(val)){
        return true;
    }
    else
        return false;
}

function trim(s)
{
    var i, j = s.length;

    for (i = 0; isWhite(s.charAt(i)) && (i < j); i++)
        ;
    if (i == j) return ""; //whole string is white
    for (; isWhite(s.charAt(j-1)); j--)
        ;
    return s.substring(i, j);
}

function isWhite(c)
{
    return ((c == '\t') || (c == ' '));
}

function is_only_number(val){
    var re= /^\d{1,}$/;
    if(re.test(val)){
        return true;
    }
    else
        return false;
}

function is_telephone(val){
    var re= /^[0-9\-]{9,}$/;
    if(re.test(val)){
        return true;
    }
    else
        return false;
}

function is_name(val){
    var re= /^[A-Za-z\-\'\s]{2,}$/;
    if(re.test(val)){
        return true;
    }
    else
        return false;
}

//-------------------
//validate and submit
//login form
//-------------------
function login(form){
    //do form validation
    var username=form.username.value;
    var password=form.password.value;

    if(emptyString.test(username) || emptyString.test(password)){
        msg("error","error",err_blank);
    }
    else if(!common_input_validation(username) || !common_input_validation(password)){
        msg("error","error",valid_chars);
    }
    else{
        form.act.value = "login";
        form.submit();
    }
}