var clicked = '';

function rowOver(which, what)
{
    var changed = document.getElementById(which);
    if(which != clicked)
    {
        if(what == 1)changed.style.backgroundColor = '#D5DDE9';
        else
        {
            if(which%2)changed.style.backgroundColor = '#F3F3F3';
            else changed.style.backgroundColor = '#FAFAFA';
        }
    }
}

function resetRow(which)
{
    var changed = document.getElementById(which);
    if(which%2)changed.style.backgroundColor = '#F3F3F3';
    else changed.style.backgroundColor = '#FAFAFA';
}

function changeSelect(which)
{
    var changed = document.getElementById(which);
    changed.style.backgroundColor = '#DDEEFF';
    changed.onMouseOver = '';
    changed.onMouseOut = '';
}

function selectRow(which)
{
    if(clicked != '')
    {
        resetRow(clicked);
        clicked = which;
        changeSelect(which);
    }
    else if(clicked == '')
    {
        clicked = which;
        changeSelect(which);
    }
}

function deSelectRow(which)
{
    resetRow(which);
    clicked = '';
}

function confirmDelComment(vcId)
{
    if(confirm('You are about to reset this vacancy to the initial stage.\nDo you want to delete all the comments about this vacancy\'s applicants?')) location.replace('selvacancy.php?vcId=' + vcId + '&action=reset&comments=delete');
    else location.replace('selvacancy.php?vcId=' + vcId + '&action=reset');
}

function addComment(vcId, usrId, vcStage)
{
    if(!usrId && !vcStage)
        window.open('comment.php?vcId=' + vcId + '&action=view','Comment','width=900,height=500,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,copyhistory=0,resizable=1');
    else
        window.open('comment.php?vcId=' + vcId + '&usrId=' + usrId + '&vcStage=' + vcStage,'Comment','width=500,height=300,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,copyhistory=0,resizable=1');
}

function downloadme(file)
{
    var l = "";
    if(location.href.indexOf("/admin") != -1) l = "../";
    myTempWindow = window.open(l + "../uploads/download.php?file=" + file,"SaveMe",'width=0,height=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,copyhistory=0,resizable=0');
}

function fileDelete(file)
{
    if(confirm('Are you sure you want to delete?')) location.replace('./?mod=7&delpath=' + file);
}

function confirmDel(str, path)
{
    if(confirm(str)) location.replace(path);
}

function getvar(selName)
{
    var sel = document.getElementById(selName);
    selIndex = sel.selectedIndex;
}

function selVal(get_post, loc, selName, shiftIfZero)
{
    var selId = document.getElementById(selName);
    if(get_post == 'post')
    {
        frm = document.getElementById(loc);
        frm.submit();
    }
    else if (get_post == 'get' && (selId.selectedIndex != '0' || shiftIfZero)) location.replace(loc + selId.options[selId.selectedIndex].value);
}

function editAdmin(id, code, name, type, field)
{
    var dpmtId = document.getElementById(type + 'Id');
    dpmtId.value = id;
    if(type != "title" && type != "est")
    {
        var dpmtCode = document.getElementById(type + 'Code');
        dpmtCode.value = code;
    }
    var dpmtName = document.getElementById(type + field);
    dpmtName.value = name;
    var btnSubmit = document.getElementById('btnSubmit');
    btnSubmit.name = "btnEdit";
    btnSubmit.value = "Edit";
    if (location.href.indexOf("#edit") == -1) location.href = location.href + "#edit";
        location.replace(location.href);
}

function editAdminLoc(id, name, country, countryName)
{
    var locId = document.getElementById('locId');
    locId.value = id;
    var locName = document.getElementById('locName');
    locName.value = name;
    var vcCountry = document.getElementById('vcCountry');
    if(country != 0)
    {
        vcCountry.disabled = false;
        vcCountry.options[vcCountry.selectedIndex].value = country;
        vcCountry.options[vcCountry.selectedIndex].text = countryName;
    }
    else vcCountry.disabled = true;
    var btnSubmit = document.getElementById('btnSubmit');
    btnSubmit.name = "btnEdit";
    btnSubmit.value = "Edit";
    if (location.href.indexOf("#edit") == -1) location.href = location.href + "#edit";
        location.replace(location.href);
}

function editCancel(form)
{
    var btnSubmit = document.getElementById('btnSubmit');
    btnSubmit.name = "btnAdd";
    btnSubmit.value = "Add";
    var frmForm = document.getElementById(form);
    frmForm.reset();
    if(form == "frmLoc")
    {
        var vcCountry = document.getElementById('vcCountry');
        vcCountry.disabled = false;
        vcCountry.options[vcCountry.selectedIndex].value = 0;
        vcCountry.options[vcCountry.selectedIndex].text = "Countries list";
    }
}







function selLang(id, action)
{
    id = id.split(",");
    var count = 0;
    while(count < id.length)
    {
        if(action == "check" && document.getElementById(id[count]).disabled)document.getElementById(id[count]).disabled = "";
        else if(action == "uncheck" && !document.getElementById(id[count]).disabled)document.getElementById(id[count]).disabled = "disabled";
        count ++;
    }
}

function swapEDControl(myName, controlName, msg)
{
    if(document.getElementById(controlName).disabled)
    {
        if(confirm(msg)) document.getElementById(controlName).disabled=false;
        else document.getElementById(myName).checked=false;
    }
    else document.getElementById(controlName).disabled=true;
}





function selImg(hf, src)
{
    var imgID = document.getElementById("sel" + hf);
    document.images[hf + 'image'].src = src + imgID.options[imgID.selectedIndex].value;
}

function disablewidth(act)
{
    if(act == "disable")
    {
        document.getElementById("txtwidth").disabled = true;
        document.getElementById("selHFAlign").disabled = true;
    }
    else if(act == "enable")
    {
        document.getElementById("txtwidth").disabled = false;
        document.getElementById("selHFAlign").disabled = false;
    }
}

function disableSidebar(act)
{
    if(act == "disable")
    {
        document.getElementById("selSideAlign").disabled = true;
        document.getElementById("txtsidewidth").disabled = true;
    }
    else if(act == "enable")
    {
        document.getElementById("selSideAlign").disabled = false;
        document.getElementById("txtsidewidth").disabled = false;
    }
}

function disable(hf, act)
{
    if(act == "select")
    {
        var imgID = document.getElementById("sel" + hf);
        document.getElementById("sel" + hf).disabled = false;
        if(imgID.options[imgID.selectedIndex].value != 0)
            document.getElementById(hf + "image").style.display = 'inline';
        document.getElementById(hf + "Img").disabled = true;
    }
    else if(act == "disable")
    {
        document.getElementById(hf + "Img").disabled = true;
        document.getElementById("sel" + hf).disabled = true;
        document.getElementById(hf + "image").style.display = 'none';
    }
    else if(act = "upload")
    {
        document.getElementById(hf + "Img").disabled = false;
        document.getElementById("sel" + hf).disabled = true;
        document.getElementById(hf + "image").style.display = 'none';
    }
}

function confirmProfDel(subj)
{
    if(document.getElementById("btnDelete").disabled)
    {
        if(confirm('WARNING!\n If you press the "Delete ' + subj + '" button all the ' + subj + '\'s information will be lost for ever!\n Do you want to continue?'))
        {
            document.getElementById("btnDelete").disabled=false;
        }
        else
        {
            document.getElementById("chkDelete").checked=false;
        }
    }
    else
    {
        document.getElementById("btnDelete").disabled=true;
    }
}



function selOrg(from)
{
    var orgID = document.getElementById('selOrgs');
    if(orgID.options[orgID.selectedIndex].value != 0)
        location.replace('./?mod=' + from + '&org=' + orgID.options[orgID.selectedIndex].value);
    else
        orgID.selectedIndex = selIndex;
}

function presel()
{
    var typeID = document.getElementById('selType');
    selTypeIndex = typeID.selectedIndex;
}

function selOrgType(from)
{
    var typeID = document.getElementById('selType');
    if(typeID.options[typeID.selectedIndex].value != 0)
        location.replace(from + '?type=' + typeID.options[typeID.selectedIndex].value);
    else
        typeID.selectedIndex = selTypeIndex;
}

function selCat(from, prof_search)
{
    var catID = document.getElementById('selCat');
        location.replace('./?mod=' + from + '&prof=' + prof_search + '&cat=' + catID.options[catID.selectedIndex].value);
}



function confirmDelEmail(id, email, profile)
{
    if(confirm('Are you sure you want to delete eMail "' + email + '"?'))
    {
        location.replace('./?mod=addmails.php&del=' + id + '&prof=' + profile);
    }
}

function confirmDelCat(id, cat, profile, numMails)
{
    if(confirm('Are you sure you want to delete category "' + cat + '"?'))
    {
        if(numMails)
        {
            if(numMails == -1){location.replace('ec.php?move&del=' + id + '&prof=' + profile);}
            else if(confirm('Do you want to delete all the emails in this category?\nIf you click "Cancel", they will be moved to the "Uncategorised" folder.'))
            {
                location.replace('ec.php?all&del=' + id + '&prof=' + profile);
            }
            else{location.replace('ec.php?move&del=' + id + '&prof=' + profile);}
        }
        else{location.replace('ec.php?del=' + id + '&prof=' + profile);}
    }
}

function editModeCat(id, cat)
{
    document.getElementById('txtCatName').value = cat;
    document.getElementById('id').value = id;
}

function confirmDelOrgCat(id, cat, numOrgs)
{
    if(confirm('Are you sure you want to delete category "' + cat + '"?'))
    {
        if(numOrgs)
        {
            if(confirm('Warning!\n' + numOrgs + ' organizations will be assigned "Uncategorized" category.\nDo you still want to continue?'))
            {
                location.replace('orgcat.php?move&del=' + id);
            }
        }
        else{location.replace('orgcat.php?del=' + id);}
    }
}

function editOrgCat(id, cat, desc)
{
    document.getElementById('txtCatName').value = cat;
    document.getElementById('txtCatDesc').value = desc;
    document.getElementById('btnClose').style.display='none';
    document.getElementById('btnClear').style.display='inline';
    document.getElementById('id').value = id;
}

function clearOrgCat()
{
    document.getElementById('txtCatName').value = "";
    document.getElementById('txtCatDesc').value = "";
    document.getElementById('btnClear').style.display='none';
    document.getElementById('btnClose').style.display='inline';
    document.getElementById('id').value = "";
}

function editMail(id)
{
    window.open('sme.php?' + id,'sme','width=400,height=200,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,copyhistory=0,resizable=0');
}

function editCat(id)
{
    window.open('ec.php?' + id,'ec','width=400,height=500,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,copyhistory=0,resizable=0');
}

function checkall(chkMail)
{
    for (i = 0; i < chkMail.length; i++)
    {
        chkMail[i].checked = true;
    }
    return "Select all";
}

function uncheck(chkMail)
{
    for (i = 0; i < chkMail.length; i++)
    {
        chkMail[i].checked = false;
    }
    return "Select none";
}

function inverse(chkMail)
{
    for (i = 0; i < chkMail.length; i++)
    {
        if(chkMail[i].checked){chkMail[i].checked = false;}
        else{chkMail[i].checked = true;}
    }
    return "Select Inverse";
}

function search(prof)
{
    if(document.getElementById('txtSearch').value.replace(/^\s+|\s+$/g, ''))
        location.replace('./?mod=addmails.php&prof=' + prof + '&search=' + document.getElementById('txtSearch').value);
    else
        document.getElementById('txtSearch').value = '';
}

function checkEmails(id, action)
{
    id = id.split(",");
    var count = 0;
    if(action == "check")
    {
        while(count <= id.length)
        {
            if(!document.getElementById(id[count]).checked){document.getElementById(id[count]).checked = true;}
            count ++;
        }
    }
    else
    {
        while(count <= id.length)
        {
            if(document.getElementById(id[count]).checked){document.getElementById(id[count]).checked = false;}
            count ++;
        }
    }
}


















function orgCatEdit()
{
    window.open('orgcat.php','orgcat','width=400,height=500,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,copyhistory=0,resizable=0');
}

function orgTypeEdit()
{
    window.open('orgtype.php','orgtype','width=500,height=330,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,copyhistory=0,resizable=0');
}

function editContact(id, name, value)
{
    document.getElementById('contID').value = id;
    document.getElementById('txtContName').value = name;
    document.getElementById('txtContVal').value = value;
    document.getElementById('btnAdd').value = 'Update';
    document.getElementById('btnCancel').style.display='none';
    document.getElementById('btnClear').style.display='inline';
}

function cancel()
{
    document.getElementById('contID').value = "";
    document.getElementById('txtContName').value = "";
    document.getElementById('txtContVal').value = "";
    document.getElementById('btnAdd').value = 'Add';
    document.getElementById('btnClear').style.display='none';
    document.getElementById('btnCancel').style.display='inline';
}

function confirmDelContact(id, name)
{
    if(confirm('Are you sure you want to remove contact "' + name + '?"'))
    {
        location.replace('./?mod=orgs.php&del=' + id);
    }
}

function orgFolder()
{
    window.open('orgfolder.php','orgFolder','width=500,height=400,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,copyhistory=0,resizable=1');
}

function preview(file)
{
    window.open(file,'Preview','width=400,height=350,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,copyhistory=0,resizable=1');
}

function editText(file)
{
    window.open('edittext.php?file=' + file,'Edit','width=650,height=450,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,copyhistory=0,resizable=1');
}

function openArchive(file,action)
{
    window.open('archive.php?file=' + file + '&action=' + action,'Edit','width=500,height=400,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,copyhistory=0,resizable=1');
}

function openerReload()
{
    opener.location.replace('orgfolder.php?path=' + getURLVar('path','opener'));
}
function getURLVar(urlVarName,who)
{
    if(who == 'opener'){var urlHalves = String(opener.location).split('?');}
    else{var urlHalves = String(document.location).split('?');}
    var urlVarValue = '';
    if(urlHalves[1])
    {
        var urlVars = urlHalves[1].split('&');
        for(i=0; i<=(urlVars.length); i++)
        {
            if(urlVars[i])
            {
                var urlVarPair = urlVars[i].split('=');
                if (urlVarPair[0] && urlVarPair[0] == urlVarName){urlVarValue = urlVarPair[1];}
            }
        }
    }
    return urlVarValue;   
}

function check()
{
    var ext = document.frmSender.letterFile.value;
    var ext1 = document.frmSender.letterFile.value;
    ext = ext.substring(ext.length-3,ext.length);
    ext1 = ext1.substring(ext1.length-4,ext1.length);
    ext = ext.toLowerCase(ext);
    ext1 = ext1.toLowerCase(ext1);
    if(ext != 'htm' && ext1 != 'html'){alert('Please select a .htm / .html file.');return false;}
    else{document.frmSender.submit();}
}

function confirmDelLetter(id,prof,name)
{
    if(confirm('Are you sure you want to delete letter "' + name + '"?')){location.replace('./?mod=sender.php&prof=' + prof + '&d=' + id);}
}

function fileDeleteTest(file, empId)
{
    if(confirm('Are you sure you want to delete?')) location.replace('./emp.php?empId=' + empId + '&delpath=' + file);
}

