/*-----------------------------------------------------------------
Copyright © 2005-2006 MCART Group
-----------------------------------------------------------------*/
/*-------------------------- Vars ----------------------------*/
if(window.lan=='es') {// defined on page
    var w1='ADVERTENCIA !\n\n';
    var w2='\n\nEsta acción NO PUEDE ser invertida.';
    var usure="¿Estás seguro que deseas ";
    var delit="suprimir permanentemente?";
    var delits="suprimir permanentemente?";
    var rcyit="poner este artículo en el Recyle Bin?";
    var unlit="desatar esto?";
    var unsub="al unsubscribe permanentemente?";
    var leave="salir de este Web site?";
    var noprnt="Apesadumbrado, su browser no puede imprimir esta página.";
    var req="se requiere";
    var onereq="debe tener un valor para por lo menos una lengua";
    var badform="¡Apesadumbrado!\nLa forma no se podía someter por las razones siguientes:";
    var leave="leave this site?";
}
else if(window.lan=='fr') {
    var w1='AVERTISSEMENT !\n\n';
    var w2='\n\nCette action NE PEUT PAS être renversée.';
    var usure="Êtes-vous sûr que vous voulez ";
    var delit="supprimer de manière permanente?";
    var delits="supprimer ces choses de manière permanente?";
    var rcyit="mettre cet article dans le Corbeille?";
    var unlit="détacher ceci?";
    var unsub="se désinscrire de manière permanente?";
    var leave="quitter ce site web?";
    var noprnt="Désolé, votre browser ne peut pas imprimer cette page.";
    var req="est obligatoire";
    var onereq="doit avoir une valeur pour au moins une langue";
    var badform="Désolé!\nLa formulaire n'a pas pu être soumise pour les raisons suivantes:";
}
else {
    var w1='WARNING !\n\n';
    var w2='\n\nThis action CANNOT be undone.';
    var usure="Are you sure you want to ";
    var delit="delete this item permanently?";
    var delits="delete these items permanently?";
    var rcyit="put this item in the Recyle Bin?";
    var unlit="unlink this?";
    var unsub="unsubscribe permanently?";
    var leave="leave this site to visit this link?";
    var noprnt="Sorry, your browser cannot print this page.";
    var req="is required";
    var onereq="must have a value for at least one language";
    var badform="Sorry!\nThe form could not be submitted for the following reasons:";
}
var here="index.php?what=";
/*-------------------------- Navigation ----------------------------*/
if (top.frames.length!=0) top.location=self.document.location; // Prevent framing
// Take user to edit screen if double click a link Netscape 6 and IE 5 only-
function goEdit(what,id,lan) {
    value = id // Value may be linked, so we need split it
    tmp = value.split("|");
    value1 = trim(tmp[0]); value2 = tmp[1];
    id=(value2)? value2:value1;
    window.location.href=here+what+'&action=edit&rec_id='+id +'&lan_code='+lan;
    }
function goAdd(what,id,lan) {window.location.href=here+what+'&action=add&rec_id='+id +'&lan_code='+lan;} // Take to add form when switching tabs on blank record
function qdb()          {window.location.href=here+'admin&admin=quikdb';} // quikdb main
function qdbc(table)    {window.location.href=here+'admin&admin=quikdb&action=choose&table='+table;} // quikdb add
function qdba(table)    {window.location.href=here+'admin&admin=quikdb&action=add&table='+table;} // quikdb add
function qdbe(table,id) {window.location.href=here+'admin&admin=quikdb&action=edit&table='+table+'&id='+id;} // quikdb edit
function qdbp(table,id) {window.location.href=here+'admin&admin=quikdb&action=pub&table='+table+'&id='+id;} // quikdb edit
function qdbu(table,id) {window.location.href=here+'admin&admin=quikdb&action=unpub&table='+table+'&id='+id;} // quikdb edit
function qdbd(table,id) {return cDel(here+'admin&admin=quikdb&action=dele&table='+table+'&id='+id);} // quikdb delete
function qele(id)       {window.location.href=here+'emaillist&action=edit&id='+id;} // quikemaillist edit
function qeld(table,id) {return cGoUrl(here+'emaillist&action=dele&table='+table+'&id='+id,'',w1+usure+unsub+w2);} // quikemaillist delete
function qtd(id)        {return cDel(here+'tasks&dele='+id);} // QuikTask Delete
function qwa(what)      {window.location.href=here+what+'&action=add'} // Quik What Add
function cDelRec(what,id,lan) {// Confirm delete for records
    if (lan=='') var msg = usure+delit+'\nDeleting a main record also deletes any associated translations, files and links.'
        +'\n(You can delete just one language by clicking "\Clear\" from within the form.)';
    else var msg = usure+'clear (delete) this language version?';
    cGoUrl(here+what+'&action=del&rec_id='+id +'&lan_code='+lan,'',w1+msg+w2);
    }
function cRecycle(what,id) {cGoUrl(here+what+'&action=recycle&rec_id='+id,'',usure+rcyit)}
function cDelPgm(what,id,lan) {// returnto IN progress
    var msg = (lan=='')? usure+delit+'\nDeleting a record from the program deletes any associated translations, files and links.'
    : usure+'clear (delete) this language version?';
    cGoUrl(here+what+'&action=del&rec_id='+id +'&lan_code='+lan,'',w1 + msg + w2)
    }
function cUnlink(what,lnk_what,lnk_towhat,id) {// Confirm unlink for program
    var msg = usure+unlit+'\n\n'
    +'Unlinking simply disconnects the record from this relationship, but otherwise leaves the record untouched.\n\n'
    +'- If you still want this record to appear in other lists, such a person, click \'OK\'.\n'
    +'- If you are sure you do not want this record, you might consider putting it in the Recycle Bin instead.\n'
    cGoUrl(here+what+'&action=unlink&lnk_what='+lnk_what+'&lnk_towhat='+lnk_towhat+'&rec_id='+id,'',msg)
    }
function cDel(url) {cGoUrl(url,'',w1+usure+delit+w2)} // Generic confirm delete
function cSure(str) {return confirm(w1+usure+eval(str)+w2)} // Generic confirm message,e.g., onClick="return cSure('delit')"
function goUrl(url,target) {// Generic go to url
    if (trim(url) !=='' && target=='_blank') window.open(url);
    else if (trim(url) !=='') window.location.href=url;
    else return false;
    }
function cGoUrl(url,target,msg) {if(!target) target='_self'; if (confirm(msg)) goUrl(url,target);}// Generic confirm go to url
function cMsg(str) {confirm(str);}
function cLeave(url) {return cGoUrl(url,'_blank',usure+leave)}
function chkBlnk(e) {if (e.value !=='') e.className='none';}
function movePre(pre_id,eve_id) {window.opener.location.href=window.opener.location.href; self.close();}
function pop_works_in(per_id,org_id,off_id) {
    if (org_id > 0 ) popWinParams('pop.php?pop=win&per_id='+per_id+'&off_id='+off_id+'&org_id='+org_id,'popWin',600,380,0,0,0,0,0,0,1);
    else alert('Organization '+req);
    }
function popWin(url) {
    window.name = 'parentWin';
    newWindow = window.open(url,'popUp','width=310,height=400,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes',100,100);
    newWindow.focus();
    }
function popWinParams(url,name,width,height,toolbar,location,directories,status,menubar,scrollbars,resizable) {
    window.name = 'parentWin';
    newWindow = window.open(url,name,'width='+width+',height='+height+',toolbar='+toolbar+',location='+location+',directories='+directories+',status='+status+',menubar='+menubar+',scrollbars='+scrollbars+',resizable='+resizable);
    newWindow.focus();
    }
function pwp(url,params) {// simpler version of  popWinParams
    window.name = 'parentWin';
    newWindow = window.open(url,'popUp',params);
    newWindow.focus();
    }
function popHelp(hlp) {
    window.name = 'parentWnd';
    newWindow = window.open('pop.php?pop=hlp&hlp='+hlp,'popupWindow','width=530,height=400,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes');
    newWindow.focus();
    }
function popVcard(what,rec_id) {
    window.name = 'parentWnd';
    newWindow = window.open('pop.php?pop=vcard&what='+what+'&rec_id='+rec_id,'popupWindow','width=530,height=400,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes');
    newWindow.focus();
    }
function refreshParent() {window.opener.location.href = window.opener.location.href; window.close();}
function submitParent() {window.opener.editForm.submit();}
function printWindow() {if (window.print) window.print(); else alert(noprnt);}
// Show / Hide divs
function toggleAll(itemname,state) {
    tmp = document.getElementsByTagName('div');
    for (i=0;i<tmp.length;i++) {if (tmp[i].className == itemname) tmp[i].style.display = state;}
    }
function toggle(idname) {s=document.getElementById(idname).style;s.display = (!s.display ||s.display == 'none') ? 'block' : 'none';}
function toggleCloseOthers(idname) { // may need reworking as toggle to avoid double click
    origStatus = (document.getElementById(idname).style.display == 'block') ? 'block' : 'none';
    toggleAll('toggle','none');
    if (origStatus=='none') toggle(idname);
}
function show(object) {if (document.layers && document.layers[object]) document.layers[object].visibility = 'visible'; else if (document.all) document.all[object].style.zIndex=100, document.all[object].style.visibility = 'visible';}
function hide(object) {if (document.layers && document.layers[object]) document.layers[object].visibility = 'hidden'; else if (document.all) document.all[object].style.visibility = 'hidden';}
function timeit(s,func) {setTimeout(eval(func),s*600);} // Run a function after s seconds 18m=1080s 30m=1800s
function logout() {
    if(opener && !opener.closed) {window.opener.location.href = 'index.php?login=false'; window.close();} // if popup
    else {window.location.href = 'index.php?login=false';}
}
/*-------------------------- Reorder  a select list ----------------------------*/
function nudge(list,direction) {
    var length = list.length;
    var temp = new Object();
    if (direction == 'up') {
        for (var i = 0; i < length; i++) {
            if (list[i].selected) {
                for (var n = 0; n < length; n++) {
                    if (n == i) {
                        if (n > 0) {
                            swapListItems(list,n,(n - 1));
                            }}}}}}
    else {
        for (var i = (length - 1); i > -1; i--) {
            if (list[i].selected) {
                for (var n = 0; n < length; n++) {
                    if (n == i) {
                        if (n < (length - 1)) {
                            swapListItems(list,n,(n + 1));
                            }}}}}}}
function swapListItems(list,a,b) {// swaps item a and b in the given form
    var temp = new Object;
    temp.value = list[b].value;
    temp.text = list[b].text;
    temp.selected = list[b].selected;
    temp.className = list[b].className;
    list[b].value = list[a].value;
    list[b].text = list[a].text;
    list[b].selected = list[a].selected;
    list[b].className = list[a].className;
    list[a].value = temp.value;
    list[a].text = temp.text;
    list[a].selected = temp.selected;
    list[a].className = temp.className;
    }
/*-------------------------- Forms ----------------------------*/
function validate(f) {
    var mess = "";
    for (var i=0; i<f.length; i++) {
        var e = f.elements[i]; var thename = e.name; var thetitle = e.title; var thelabel = thetitle; var theclass = e.className; var theid = e.id;  var cls = e.className;
        if (!thetitle) thelabel=thename;
        if (e.type=="button" || e.type=="submit" || e.type=="reset") {continue;}
        if (cls=='blank' || left(cls,2)=='rq' || left(e.id,2)=='rq' || left(right(e.name,3),2)=='rq' || cls==1 || e.id==1) {
            if(trim(e.value)=='') {
                // If multiple languages, is at least one filled out?
                base=left(thename,len(thename)-3); lan=right(thename,3);
                if(lan=='_en' || lan=='_es' || lan=='_fr') {
                    var tmp='';
                    en='f.'+base+'_en'; es='f.'+base+'_es'; fr='f.'+base+'_fr';
                    if(eval(en)) tmp+=eval(en+'.value');
                    if(eval(es)) tmp+=eval(es+'.value');
                    if(eval(fr)) tmp+=eval(fr+'.value');
                    if(tmp=='') {
                        x="''" + base + "'' " + onereq + "\n";
                        if(mess!=replace(x,'',mess)) {
                            mess += "''" + base + "'' " + onereq + "\n";
                            if(eval(en)) eval(en).className='blank';
                            if(eval(es)) eval(es).className='blank';
                            if(eval(fr)) eval(fr).className='blank';
                        }
                    }
                }
                else {
                    mess += "''" + thelabel + "'' " + req + "\n";
                    e.className='blank';
                }
            }
            else{e.className='rq';} // return to previous style if completed
        }
    }
    if (mess == "") {return(true);}
    else {
        var msg = "";
        msg = "_______________________________________________\n\n";
        msg += badform +"\n\n";
        msg += mess;
        msg += "_______________________________________________\n";
        alert(msg);
        e.focus();
        return(false);
    }
}
function selectAllLinked(f) {
    for (var n=0; n<f.length; n++) {
        var list = f.elements[n];
        if (left(list.type,6)=="select" && left(list.name,4)=='lnk_') {
            for (i = 0; i < list.options.length; i++) {
                text=list.options[i].text; // get text of option
                tmp  = text.split("|");        // try to split it into 2
                text2 = tmp[1];    // if can be split, it is linked
                if ((text2) || i==0) list.options[i].selected = true; // select all linked AND the first (form will not exist if none linked)
                else list.options[i].selected = false;
                }
            }
        }
    }
// Toggle checkboxes
function check(f,how) {
    for (var i=0;i< f.elements.length;i++) {
        var e = f.elements[i];
        if (e.name != 'selectAll')  e.checked=(how=='toggle')? !e.checked: how;
    }
}
// Toggle check only certain checkboxes using id
function checkSpecial(f,how,id) {// Used to check only certain records, e.g., ones used
    for (var i=0;i< f.elements.length;i++) {
        var e = f.elements[i];
        if (e.name != 'selectAll' && e.id==id) e.checked=(how=='toggle')? !e.checked: true;
        else if (e.id==id) e.checked = false;
    }
}
// Toggle check only certain checkboxes using class
function checkClass(f,how,cls) { // Used to check only certain records, e.g., ones used
    for (var i=0;i< f.elements.length;i++) {
        var e = f.elements[i];
        if (e.className==cls && how=='toggle') {e.checked = !e.checked; }
        else if (e.className==cls) {e.checked = true;}
    }
}
// Return if a checkbox is checked, e.g, isChecked((this.form.checkboxName);
function isChecked(f,field) {
    for (var i=0;i< f.elements.length;i++) {
        var e = f.elements[i];
        if (e.name == field && e.checked) return true;
    }
    return false;
}
function textCounter(field, countfield, maxlimit) {// call with onblur, onfocus, onkeydown and onkeyup ="textCounter(this,this.form.counter,255);"
    if (field.value.length > maxlimit ) {
        field.value = field.value.substring(0, maxlimit );
        alert('Textarea value can only be '+maxlimit+' characters in length.');
        return false;
    }
    else {countfield.value = maxlimit - field.value.length;}
}
function chktime(e) {// Return if time is in HH:MM format.
    timeStr=e.value;
    var replace_if_err=''; //00:00
    var msg='Please use times such as "00:00"';
    var matchArray = timeStr.match(/^(\d{1,2}):(\d{2})?$/);
    if (matchArray == null) {// if did not enter colons, replace and retry
        var matchArray2 = timeStr.match(/^(\d{1,2})(\d{2})?$/);
        if (matchArray2 == null) {e.value=replace_if_err;alert(msg);return false;}
        e.value=matchArray2[1]+':'+ matchArray2[2];
        chktime(e);
    }
    hour = matchArray[1]; minute = matchArray[2];
    if(hour < 0  || hour > 23) {e.value=replace_if_err;return false;}
    if(minute<0 || minute > 59) {e.value=replace_if_err; return false;}
    return false;
}
function chkfile(e,exts) { // Return if file upload is in array of allowed formats. onChange="chkfile(this.form.uploadfile.value, ['gif', 'jpg', 'png', 'jpeg']);"
    if(!e.value) return;
    filename=e.value; dots=filename.split(".");  ext="."+dots[dots.length-1]; // get the part AFTER the LAST period.
    return(exts.join(".").indexOf(ext) != -1 )? true:false;
}
function isemail(e) {
    if (e.value == "") {return false;}
    else{
        var emailReg = "^[\\w-_\.\'\+]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
        var regex = new RegExp(emailReg);
        return (regex.test(e.value))? true:false;
    }
}
function resetallselects(what) {
    for (var i=0, j=what.elements.length; i<j; i++) {
        if (what.elements[i].type == 'select-one' || what.elements[i].type == 'select-multiple')
            for (var k=0, l=what.elements[i].options.length; k<l; k++) what.elements[i].options[k].selected = false;
    }
}
/*-------------------------- Strings ----------------------------*/
function left(str,n) {
    if (n <= 0) return "";
    else if (n > String(str).length) return str;
    else return String(str).substring(0,n);
    }
function right(str,n) {
    if (n <= 0) return "";
    else if (n > String(str).length) return str;
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
        }
    }
function rtrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
        var i = s.length - 1;
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
            i--;
        s = s.substring(0, i+1);
        }
    return s;
    }
function ltrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(0)) != -1) {
        var j=0, i = s.length;
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
            j++;
        s = s.substring(j, i);
    }
    return s;
    }
function trim(str) {return rtrim(ltrim(str));}
function len(str) {return String(str).length;}
function replace(str,find,replace) {
    var strlen = str.length, txtlen = find.length;
    if ((strlen == 0) || (txtlen == 0)) return str;
    var i = str.indexOf(find);
    if ((!i) && (find != str.substr(0,txtlen))) return str;
    if (i == -1) return str;
    var newstr = str.substr(0,i) + replace;
    if (i+txtlen < strlen) newstr += replace(str.substr(i+txtlen,strlen),find,replace);
    return newstr;
}
function empty(str) {return(str.length > 0)?false:true;}
function stripspaces(str) {return str.replace(/^\W+/,'');}
/*-------------------------- Cookies ----------------------------*/
var kooky = document.cookie;
var today = new Date();
var expiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000); // plus 28 days
function getCookie(name) {// use: getCookie("name");
    var index = kooky.indexOf(name + "=");
    if (index == -1) return null;
    index = kooky.indexOf("=", index) + 1;
    var endstr = kooky.indexOf(";", index);
    if (endstr == -1) endstr = kooky.length;
    alert
    return unescape(kooky.substring(index, endstr));
}
function setCookie(name,value,expiry) {// use: setCookie("name", value);
    if (value != null && value != "")
        document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString();
    kooky = document.cookie; // update kooky
}
function firstCookieVisit() {
    if (getCookie("visited")) self.location.href = "main.html";
    else setCookie ("visited", true);
}
function getCookieVal(offset) {
    var endstr = kooky.indexOf(";", offset);
    if (endstr == -1) endstr = kooky.length;
    return unescape(kooky.substring(offset, endstr));
}
function checkCookies() {
    var cookieName="test"; var cookieVal ="Boo!";
    setCookie (cookieName,"",-10); setCookie (cookieName,"Boo!",expiry);
    if(getCookie(cookieName) == null) alert(getCookieMsg());
}
function getCookieMsg() {
    var msg1 = "Please note that this website uses a cookie to manage your session navigation.\n" +
            "This cookie is used for management of your current session only and is not used\n" +
            "for any other purpose. You must allow cookies for this site in order to use the\n" +
            "features of this site.";
    var msg2 = "\n\nREMEMBER:\n" +
            "Cookies cannot get information from your browser or you (ie. IP, email address, birthday, credit card). \n" +
            "You can not see other peoples cookies. \n" +
            "Cookies do not \'do\' anything, they are only text strings! \n";
    return msg1 + msg2;
}
/*-------------------------- Misc ----------------------------*/
function MM_preloadImages() {//v3.0
    var d=document; if(d.images) {if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0) {d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function disableenterkey(e){var key=(window.event)?event.keyCode:e.which;return (key!=13);} // prevent form submission with return key. onKeyPress=”return disableEnterKey(event)”