function isUndefined(a) {
    return typeof a == 'undefined';
}



// Session counter
function session_warning(){
    endin = setTimeout('end_session()', 30000);
    if(confirm('Your session is about to time out, please click OK to continue otherwise you will be logged out.')){
        clearTimeout(endin);
        window.location = 'index.php';
    } else { 
        end_session();
    }
}

// Ending a session
function end_session(){
    window.location = 'index.php?logout';
}


function showPreview(url, id){
settings=
"toolbar=no,location=no,directories=no,"+
"status=no,menubar=no,scrollbars=yes,"+
"resizable=yes,width=800,height=600";
    window.open(url,'mywindow',settings);
}

function setActive(obj, set, id){
    if(set == 'activate'){
        obj.act.value = 'activate';
    } else if(set == 'deactivate') {
        obj.act.value = 'deactivate';
    }
    obj.id.value = id;
    obj.submit();
}

function deleteSingleItem(obj, id, act){
    if(confirm('Are you sure you want to delete this item?')){
        obj.id.value = id;
        obj.act.value = act;
        obj.submit();
    }
}

function deleteMultipleItems(obj){
    if(confirm('Are you sure you want to delete these items?')){
        obj.act.value = 'multi_delete';
        obj.submit();
    }
}

function activateMultiple(obj){
        obj.act.value = 'multi_activate';
        obj.submit();
}

function deactivateMultiple(obj){
        obj.act.value = 'multi_deactivate';
        obj.submit();
}

function exportItems(obj){
    obj.act.value = 'export';
    obj.submit();
}

function checkUncheck(obj, value){
    if(value == 1) {
        obj.checked = true
    } else {
        obj.checked = false
    }
}

function selectAll(obj, field, value) {
    if(isUndefined(obj.elements[field].length)){
        checkUncheck(obj.elements[field], value);
    } else {
        for (var i = 0;i < obj.elements[field].length;i++) {
            checkUncheck(obj.elements[field][i], value);
        }
    }
}

function editItem(obj, id){
    obj.act.value = 'edit';
    obj.id.value = id;
    obj.submit();
}

function editAttributes(obj, id){
    obj.act.value = 'edit_attr';
    obj.id.value = id;
    obj.submit();
}

function viewItem(obj, id){
    obj.act.value = 'view';
    obj.id.value = id;
    obj.submit();
}

function showHide(theid){ 
    if (document.getElementById) {
        var switch_id = document.getElementById(theid);
        menu_status[theid] = switch_id.className;
        if(menu_status[theid] != 'visible') {
            switch_id.className = 'visible';
            menu_status[theid] = 'visible';
        }else{
            switch_id.className = 'hidden';
            menu_status[theid] = 'hidden';
        }
    }
}
    
// Removes leading and trailing spaces and checks if the str has any contents... 
function isEmpty(str){
  str = this != window? this : str;
  str = str.replace(/^\s+/g, '').replace(/\s+$/g, '');
  if(str.length == 0){
      return true;
  } else {
      return false;
  }
}

/** Validation Stuff **/
function hasIllegalChars(strValue){
    var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
    if (strValue.match(illegalChars)) {
        return true;
    }
}


function validateInteger(strValue) {
  var objRegExp  = /(^-?\d\d*$)/;

  //check for integer characters
  return objRegExp.test(strValue);
}

function validateEmail( value) {
    if(hasIllegalChars(value)){
        return false;
    }
    
    apos = value.indexOf("@");
    dotpos=value.lastIndexOf(".");
    if (apos < 1 || dotpos-apos < 2) {
            return false
    }
    return true;
}

function hasChecked(obj, field){
    var listList = '';
    if (!obj.elements[field].length && obj.elements[field].checked == true){
        return true;
    } else {
        for (var i = 0;i < obj.elements[field].length;i++) {
            if(obj.elements[field][i].checked == true){
                return true;
            }
        }
    }
    return false;
}

function pauseScript(millis){
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate-date < millis);
}

