// confirm delete
function confirmDelete(item,id)
{
   if (confirm("Are you sure you want to delete this "+item+"?")) {
      document.deleteform.id.setAttribute("value",id);
      document.deleteform.deletesubmit.setAttribute("value",1);
      document.deleteform.submit();
   }
   return false; // we don't want the button to take action
}

//validate insert/edit user form
function checkUserModifyForm()
{
   var sName, sUsername, sEmail, sPassword, sPasswordConfirm, ixUser;
   with(window.document.usermodifyform)
   {
      sName             = name;
      sEmail            = email;
      sPassword         = password;
      sPasswordConfirm  = confirm_password;
      ixUser            = id;
   }

   // validate name
   if(trim(sName.value) == '')
   {
      alert('Please enter a name.');
      sName.focus();
      return false;
   }

   // validate email
   if (!validEmail(sEmail.value))
   {
      alert('Please enter a valid email.');
      sEmail.focus();
      return false;
   }

   // validate password, if this an insert or,
   // if this is an update and the password fields aren't empty
   if (ixUser.value == '0' || trim(sPassword.value) != "")
   {
      // validate password fields
      if (trim(sPassword.value) == '') {
         alert("Please enter a password.");
         sPassword.focus();
         return false;
      }
      if (trim(sPassword.value).length < 6) {
         alert("Please enter a password with at least 6 characters.");
         sPassword.focus();
         return false;
      }
      if (trim(sPassword.value) != trim(sPasswordConfirm.value)) {
         alert("Please enter matching passwords in password and confirm password.");
         sPasswordConfirm.focus();
         return false;
      }   
   }

   return true;
}

//validate login form
function checkAuthLoginForm()
{
   var sEmail            = document.getElementById("email");
   var sPassword         = document.getElementById("password");

   // validate name
   if(trim(sEmail.value) == '')
   {
      alert('Please enter an email address.');
      sEmail.focus();
      return false;
   }

   // validate username
   if(trim(sPassword.value) == '')
   {
      alert('Please enter a password.');
      sPassword.focus();
      return false;
   }

   return true;
}

function checkAuthResetForm()
{
   var sPassword, sPasswordConfirm;
   with(window.document.authresetform)
   {
      sPassword         = password;
      sPasswordConfirm  = confirm_password;
   }

   // validate password fields
   if (trim(sPassword.value) == '') {
      alert("Please enter a password.");
      sPassword.focus();
      return false;
   }
   if (trim(sPassword.value).length < 6) {
      alert("Please enter a password with at least 6 characters.");
      sPassword.focus();
      return false;
   }
   if (trim(sPassword.value) != trim(sPasswordConfirm.value)) {
      alert("Please enter matching passwords in password and confirm password.");
      sPasswordConfirm.focus();
      return false;
   }

   return true;
}

function checkAuthRequestForm()
{
   var sEmail = window.document.authrequestform.email;

   // validate email
   if (!validEmail(sEmail.value))
   {
      alert('Please enter a valid email.');
      sEmail.focus();
      return false;
   }

   return true;
}

//check if valid email
function validEmail(str)
{
   var regex = /^[-_.a-z0-9]+@(([-a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
   return regex.test(trim(str));
}

// check if valid phone number
function validPhone(str) 
{
    
    return trim(str).match(/^[0-9\-\(\) \.x]+$/); 
}

function validStateForCountry(country, state)
{
    // If the country is USA
    // we should check we have a state specified

    if (trim(country) == '25') { // 25 - USA
        if (trim(state) == 0) {
           return false;
        }
    }
    return true;
}

//trim whitespaces from beginning and end
function trim(str)
{
   return str.replace(/^\s+|\s+$/g,'');
}

function checkJobModifyForm() 
{
    var sTitle, sCity, sState, sCountry, sDescription;


    with(window.document.jobmodifyform) 
    {
        sTitle = title;
        sCity = city;
        sState = state;
        sCountry = country;
        sDescription = description;
    }
    
    // validate fields
    if (trim(sTitle.value) == '') {
        alert('Please enter a title');
        sTitle.focus();
        return false;
    }

    if (trim(sCity.value) == '') {
        alert('Please enter a city');
        sCity.focus();
        return false;
    }

    if (trim(sCountry.value) == '') {
        alert('Please enter a country');
        sCountry.focus();
        return false;
    }
    else {
        // We have verified we have a country - now if it's USA
        // we should check we have a state specified

        if (!validStateForCountry(sCountry.value, sState.value)) {
            alert('Please enter a state');
            sState.focus();
            return false;
        }
    }

    // We can't check for a description with the fck field - it doesn't give us any data

    return true;

}

function checkCandidateModifyForm() 
{
    var sTitle, sName, sEmail, sPhone, sCity;
    var sState, sCountry, sDescription;


    with(window.document.candidatemodifyform) 
    {
        sTitle = title;
        sName = name;
        sEmail = email;
        sPhone = phone;
        sCity = city;
        sState = state;
        sCountry = country;
        sDescription = description;
    }
    
    // validate fields
    if (trim(sTitle.value) == '') {
        alert('Please enter a title');
        sTitle.focus();
        return false;
    }

    if (trim(sName.value) == '') {
        alert('Please enter a name');
        sName.focus();
        return false;
    }

    if (!validEmail(sEmail.value))
    {    
      alert('Please enter a valid email');
      sEmail.focus();
      return false;
    }

    if (!validPhone(sPhone.value)) {
        alert('Please enter a valid phone');
        sPhone.focus();
        return false;
    }

    if (trim(sCity.value) == '') {
        alert('Please enter a city');
        sCity.focus();
        return false;
    }

    if (trim(sCountry.value) == '') {
        alert('Please enter a country');
        sCountry.focus();
        return false;
    }
    else {
        // We have verified we have a country - now if it's USA
        // we should check we have a state specified

        if (!validStateForCountry(sCountry.value, sState.value)) {
            alert('Please enter a state');
            sState.focus();
            return false;
        }
    }

    // We can't check for a description with the fck field - it doesn't give us any data

    return true;

}

function checkCountryModifyForm()
{
    var sCountry;

    with(window.document.countrymodifyform) 
    {
        sCountry = country;
    }
    
    if (trim(sCountry.value) == '') {
        alert('Please enter a country');
        sCountry.focus();
        return false;
    }
    return true;
}

// check if valid date format
function validDateFormat(str) 
{
    return trim(str).match(/^\d\d\/\d\d\/\d\d$/); 
}

function checkSearchForm()
{
    var sFromDate;
    var sToDate;
    var d1 = "";
    var d2 = "";

    with(window.document.search) 
    {
        sFromDate = fromdate;
        sToDate = todate;
    }
    
    if (trim(sFromDate.value) != '') {
        
        if (!validDateFormat(sFromDate.value)) {
            alert("Please enter From date in the correct format");
            sFromDate.focus();
            return false;
        }

        if (!validateDate(sFromDate.value, "U", "A")) {
            alert('Please enter a valid from date');
            sFromDate.focus();
            return false;
        }

        d1 = sFromDate.value.split('\/');
        ddf = d1[1]; mmf = d1[0]; yyf = d1[2];
    }

    if (trim(sToDate.value) != '') {

        if (!validDateFormat(sToDate.value)) {
            alert("Please enter To date in the correct format");
            sToDate.focus();
            return false;
        }

        if (!validateDate(sToDate.value, "U", "A")) {
            alert('Please enter a valid to date');
            sToDate.focus();
            return false;
        }
        d2 = sToDate.value.split('\/');
        ddt = d2[1]; mmt = d2[0]; yyt = d2[2];
    }

    // Check that from date is < to date
    if (d1 != '' && d2 != '') {
        if (Date.UTC(yyf,mmf,ddf,0,0,0,0) > Date.UTC(yyt,mmt,ddt,0,0,0,0)) {
            alert("From date is greater than the to date");
            sFromDate.focus();
            return false;
        }
    }

    return true;
}

// Date Validation Javascript
// copyright 30th October 2004, by Stephen Chapman
// http://javascript.about.com

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function valDateFmt(datefmt) {
    myOption = -1;
    for (i=0; i<datefmt.length; i++) {
        if (datefmt[i].checked) {myOption = i;}
    }
    if (myOption == -1) {alert("You must select a date format");return ' ';}
    return datefmt[myOption].value;
}

function valDateRng(daterng) {
    myOption = -1;
    for (i=0; i<daterng.length; i++) {
        if (daterng[i].checked) {myOption = i;}
    }
    if (myOption == -1) {alert("You must select a date range");return ' ';}
    return daterng[myOption].value;
}

function stripBlanks(fld) {
    var result = "";
    for (i=0; i<fld.length; i++) {
        if (fld.charAt(i) != " " || c > 0) {
            result += fld.charAt(i);
            if (fld.charAt(i) != " ") c = result.length;
        }
    }
    return result.substr(0,c);
}
var numb = '0123456789';

function isValid(parm,val) {
    if (parm == "") return true;
    for (i=0; i<parm.length; i++) {
        if (val.indexOf(parm.charAt(i),0) == -1) return false;
    }
    return true;
}

function isNum(parm) { return isValid(parm,numb);}

var mth = new Array(' ','january','february','march','april','may','june','july','august','september','october','november','december');
var day = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function validateDate(fld,fmt,rng) {
    var dd, mm, yy;var today = new Date;var t = new Date;fld = stripBlanks(fld);
    if (fld == '') return false;
    var d1 = fld.split('\/');
    if (d1.length != 3) d1 = fld.split(' ');
    if (d1.length != 3) {
        return false;
    }
    if (fmt == 'u' || fmt == 'U') {
        dd = d1[1]; mm = d1[0]; yy = d1[2];
    }
    else if (fmt == 'j' || fmt == 'J') {
        dd = d1[2]; mm = d1[1]; yy = d1[0];
    }
    else if (fmt == 'w' || fmt == 'W'){
        dd = d1[0]; mm = d1[1]; yy = d1[2];
    }
    else return false;

    var n = dd.lastIndexOf('st');
    if (n > -1) dd = dd.substr(0,n);
    n = dd.lastIndexOf('nd');
    if (n > -1) dd = dd.substr(0,n);
    n = dd.lastIndexOf('rd');
    if (n > -1) dd = dd.substr(0,n);
    n = dd.lastIndexOf('th');
    if (n > -1) dd = dd.substr(0,n);
    n = dd.lastIndexOf(',');
    if (n > -1) dd = dd.substr(0,n);
    n = mm.lastIndexOf(',');
    if (n > -1) mm = mm.substr(0,n);
    if (!isNum(dd)) return false;
    if (!isNum(yy)) return false;
    if (!isNum(mm)) {
        var nn = mm.toLowerCase();
        for (var i=1; i < 13; i++) {
            if (nn == mth[i] ||
                nn == mth[i].substr(0,3)) {mm = i; i = 13;}
        }
    }
    if (!isNum(mm)) return false;
    dd = parseFloat(dd); mm = parseFloat(mm); yy = parseFloat(yy);
    if (yy < 100) yy += 2000;
    if (yy < 1582 || yy > 4881) return false;
    if (mm == 2 && (yy%400 == 0 || (yy%4 == 0 && yy%100 != 0))) day[mm-1]++;
    if (mm < 1 || mm > 12) return false;
    if (dd < 1 || dd > day[mm-1]) return false;
    t.setDate(dd); t.setMonth(mm-1); t.setFullYear(yy);
    if (rng == 'p' || rng == 'P') {
        if (t > today) return false;
    }
    else if (rng == 'f' || rng == 'F') {
        if (t < today) return false;
    }
    else if (rng != 'a' && rng != 'A') return false;
    return true;
}

