
// Test browser type
NS4 = (document.layers) ? true : false;

function checkEnter(event,thisForm) {    
    // function to submit form when user presses enter
    var code = 0;

    if (NS4)
        code = event.which;
    else
        code = event.keyCode;
    if (code==13)
        thisForm.submit();
}  // end function checkEnter

function checkValue(defaultValue,curValue,action) {
    var returnValue='';
    if(action=='focus') {
        if(defaultValue==curValue) {
            returnValue='';
        }
        else {
            returnValue=curValue;
        }
    }
    else {
        // Action is not focus, must be blur
        if(curValue=='') {
            returnValue=defaultValue;
        }
        else {
            returnValue=curValue;
        }
    }
    return(returnValue);
}

function sortStaff(refArray,dispDept,dispTitle) {
    // Sort the staff directory based on the order in refArray
    var result='<table border="0" width="100%" class="directoryDisplay">\n<thead>\n<tr>\n<th><a href="javascript:sortStaff(jsArrayOrderName,'+dispDept+','+dispTitle+');" title="Sort by Name">Name</a></th>\n';
    if(dispDept) {
        // only display department if dispDept true
        result+='<th><a href="javascript:sortStaff(jsArrayOrderDept,'+dispDept+','+dispTitle+');" title="Sort by Department">Department</a></th>\n';
    }
    if(dispTitle) {
        // only display title if dispTitle true
        result+='<th><a href="javascript:sortStaff(jsArrayOrderTitle,'+dispDept+','+dispTitle+');" title="Sort by Title">Title</a></th>\n';
    }
    result+='<th><a href="javascript:sortStaff(jsArrayOrderLocation,'+dispDept+','+dispTitle+');" title="Sorty by Location">Location</a></th>\n</tr>\n</thead>\n<tbody>\n';
    for(x=0;x<refArray.length;x++){
        var y=refArray[x];
        result+='<tr>\n<td style="white-space: nowrap;"><a href="display_directory.php?directory='+jsArrayDirectory[y]+'">'+jsArrayName[y]+'</a></td>';
        if(dispDept) {
            result+='<td>'+jsArrayDept[y]+'</td>';
        }
        if(dispTitle) {
            result+='<td>'+jsArrayTitle[y]+'</td>';
        }
        result+='<td style="white-space: nowrap;">'+jsArrayLocation[y]+'</td></tr>\n';    
    }
    result+='</tbody>\n</table>\n';
    document.getElementById('jsStaffDirectory').innerHTML=result;
}

function sortFavourites(refArray) {
    // Sort the Favourites based on the order in refArray
    var result='<table border="0" width="100%" class="directoryDisplay">\n<thead>\n<tr>\n<th><a href="javascript:sortFavourites(jsArrayOrderName);" title="Sort by Name">Name</a></th>\n<th><a href="javascript:sortFavourites(jsArrayOrderCompany);" title="Sort by Company">Company</a></th>\n<th><a href="javascript:sortFavourites(jsArrayOrderCompanyType);" title="Sorty by Company Type">Company Type</a></th>\n</tr>\n</thead>\n<tbody>\n';
    for(x=0;x<refArray.length;x++){
        if(!(x % 2)) {
            var rowClass='evenRow';
        }
        else {
            var rowClass='oddRow';
        }
        var y=refArray[x];
        result+='<tr valign="top" class="'+rowClass+'">\n';
        if(jsArrayDir[y]=='') {
            result+='<td>'+jsArrayName[y]+'</td><td class="boldText"><a href="display_directory.php?company='+jsArrayCo[y]+'">'+jsArrayCompany[y]+'</td>';
        }
        else {
            result+='<td class="boldText"><a href="display_directory.php?directory='+jsArrayDir[y]+'">'+jsArrayName[y]+'</a></td><td>'+jsArrayCompany[y]+'</td>';
        }
        result+='<td>'+jsArrayCompanyType[y]+'</td></tr>\n';    
        result+='<tr valign="top" class="'+rowClass+'">\n<td></td><td colspan="2">'+jsArrayNotes[y]+' - <a href="javascript:;" onclick="javascript:window.open(\'add_favourite.php?edit='+jsArrayFavourite[y]+'\',\'Favourites\',\'scrollbars=yes,resizable=yes,width=350,height=240\');">edit</a></td></tr>\n';
    }
    result+='</tbody>\n</table>\n';
    document.getElementById('jsFavouritesDirectory').innerHTML=result;
}


function updateCities(province,citySelect) {
    // function to update the city drop-down based on the province, uses the provinceArray and citiesArray
    var chosenProvince;
   
    for(var x=0;x<provinceArray.length;x++) {
        if(provinceArray[x]==province) {
            chosenProvince=x;
        }
    }
    
    var numCities = citiesArray[chosenProvince].length;
    if(numCities>0) {
        citySelect.length=numCities;
        for(var x=0;x<numCities;x++) {
            citySelect.options[x].value=citiesArray[chosenProvince][x][0];
            citySelect.options[x].text=citiesArray[chosenProvince][x][1];
        }
    }
    else {
        citySelect.length=1;
        citySelect.options[0].text='No cities currently associated';
        citySelect.options[0].value='';
    }
}


function updateCompanies(type,companySelect) {
    // function to update the company drop-down based on the company type that has been selected
    // Uses the companyArray
    var numCompanies = companyArray[type].length;
    if(numCompanies>0) {
        companySelect.length=numCompanies;
        for(var x=0;x<numCompanies;x++) {
            companySelect.options[x].value=companyArray[type][x][0];
            companySelect.options[x].text=companyArray[type][x][1];
        }
    }
    else {
        companySelect.length=1;
        companySelect.options[0].text='No companies of this type';
        companySelect.options[0].value='';
    }
}


function getElementPosition(elementName,axis) {
    // Get the x and y co-ordinates for an element on the page
    var x=0,y=0;
    if(axis=='x') {
        x=getElementOffsetLeft(document.getElementById(elementName));
        coordinate=x;
    }
    else if(axis=='y') {
        y=getElementOffsetTop(document.getElementById(elementName));
        coordinate=y;
    }
    return coordinate;
}


function getElementOffsetLeft(element) {
    var offsetL=element.offsetLeft;
    while((element=element.offsetParent) != null) {
        offsetL += element.offsetLeft;
    }
    return offsetL;
}


function getElementOffsetTop(element) {
    var offsetT=element.offsetTop;
    while((element=element.offsetParent) != null) {
        offsetT += element.offsetTop;
    }
    return offsetT;
}


function updatePhones(id,formName,loopNum) {
    // Update the form's telephone, toll-free, and fax info based on id
    var evalString='document.'+formName+'.directoryDirectPhone_'+loopNum+'.value=telArray[id];';
    eval(evalString);
    var evalString='document.'+formName+'.directoryTollFree_'+loopNum+'.value=tfArray[id];';
    eval(evalString);
    var evalString='document.'+formName+'.directoryFax_'+loopNum+'.value=faxArray[id];';
    eval(evalString);
}


function continueButton(tabToShow,numberOfTabs,maxTabNumber) {
    // function called when user presses Continue... button on location and config employees entry pages
    if(tabToShow>tabsVisible[maxTabNumber]) {
        // If clicking continue and tab not visible it will be next tab to left
        scrollTabs('left',numberOfTabs,maxTabNumber);
        showTab(tabToShow,numberOfTabs,maxTabNumber);
    }
    else {
        showTab(tabToShow,numberOfTabs,maxTabNumber);
    }
}


function scrollTabs(direction,numberOfTabs,maxTabNumber) {
    // function to scroll tabs to the left or right
    var regexAHREF = /(<[a|A] [a-zA-Z0-9:(),;="._ ]*>)([a-zA-Z0-9 "'()<>.=\-\/]*)(<\/[a|A]>)/;
    var regexSPAN = /(<(SPAN|span)[a-zA-Z=" ]*>)([a-zA-Z0-9.()\-\/<>' ]*)(<\/(SPAN|span)>)/;
    var shift=0;

    if(numberOfTabs>maxTabNumber) {
        var tabLoop=maxTabNumber;
    }
    else {
        var tabLoop=numberOfTabs;
    }

    if((direction=='right')&&(tabsVisible[1]!=1)) {
        shift=-1;
    }
    else {
        if((direction=='left')&&(tabsVisible[tabLoop]!=numberOfTabs)) {
            shift=1;
        }
    }
 
    for(x=1;x<=tabLoop;x++) {
        // Loop through and find the currently highlighted tab
        var currentTab='tab'+x;
        var tabTextHTML=document.getElementById(currentTab).innerHTML;
        // Strip the <a></a> stuff
        var firstResult=tabTextHTML.match(regexAHREF);
        // Stip any <span></span> from within tabTextHTML
        var secondResult=firstResult[2].match(regexSPAN);
        if(secondResult != null) {
            // We have a match, this is the currently highlighted tab 
            var currentHighlight=tabsVisible[x];
        }
    }

    for(x=1;x<=tabLoop;x++) {
        // Loop through the visible tabs and shift
        tabsVisible[x]=tabsVisible[x]+shift;

        //if the currently highlighted tab has been scrolled off screen move highlight to next tab
        if((x==1)&&(currentHighlight<tabsVisible[x])&&(shift==1)) {
            currentHighlight=tabsVisible[x];
        }
        else {
            if((x==tabLoop)&&(currentHighlight>tabsVisible[x])&&(shift==-1)) {
                currentHighlight=tabsVisible[x];
            }
        }

        //Update the HTML for the tab text
        var currentTab='tab'+x;
        var tabTextHTML='<a href="javascript:showTab('+tabsVisible[x]+','+numberOfTabs+','+tabLoop+');">';
        
        if(tabsVisible[x]==currentHighlight) {
            tabTextHTML=tabTextHTML+'<span class="tabTextHighlighted">';
        }
        
        tabTextHTML=tabTextHTML+'<nobr>'+tabsText[tabsVisible[x]]+'</nobr>';
        
        if(tabsVisible[x]==currentHighlight) {
            tabTextHTML=tabTextHTML+'</span>';
        }
        
        tabTextHTML=tabTextHTML+'</a>';

        document.getElementById(currentTab).innerHTML=tabTextHTML;
    }

    // Hide or show the scroll arrows as necessary
    if(tabsVisible[1]==1) {
        document.getElementById('tabScrollLess').style.visibility='hidden';
    }
    else {
        document.getElementById('tabScrollLess').style.visibility='visible';
    }
    if(tabsVisible[(tabsVisible.length-1)]==numberOfTabs) {
        document.getElementById('tabScrollMore').style.visibility='hidden';
    }
    else {
        document.getElementById('tabScrollMore').style.visibility='visible';
    }

    // Show the currently highlighted tabs contents
    if(shift!=0) {
        showTab(currentHighlight,numberOfTabs,maxTabNumber);
    } 

}


function showTab(tabToShow,numberOfTabs,maxTabNumber) {
    // function to show/hide the indicated tab

    if(numberOfTabs>maxTabNumber) {
        var tabLoop=maxTabNumber;
    }
    else {
        var tabLoop=numberOfTabs;
    }

    // Find tab to highlight
    for(x=1;x<tabsVisible.length;x++) {
        if(tabsVisible[x]==tabToShow) {
            var highlightTab=x;
        }
    }

    // first we will deal with the tabs
    var regexAHREF = /(<[a|A] [a-zA-Z0-9:(),;="._ ]*>)([a-zA-Z0-9 "'()<>.=\-\/]*)(<\/[a|A]>)/;
    var regexSPAN = /(<(SPAN|span)[a-zA-Z=" ]*>)([a-zA-Z0-9.()\-\/<>' ]*)(<\/(SPAN|span)>)/;
    var newHTML='<img src="images/tabs_'+tabLoop+'_'+highlightTab+'highlight.gif" width="480" height="20">';
    document.getElementById('tabImage').innerHTML=newHTML;

    for(x=1;x<=tabLoop;x++) {
        // Loop through the tab text and set the correct one to be highlighted
        var currentTab='tab'+x;
        var tabTextHTML=document.getElementById(currentTab).innerHTML;
        // Strip the <a></a> stuff
        var firstResult=tabTextHTML.match(regexAHREF);
        // Stip any <span></span> from within tabTextHTML
        var secondResult=firstResult[2].match(regexSPAN);
        if(secondResult != null) {
            // We have a match, secondResult[3] will be the tab's text
            tabText=secondResult[3];
        }
        else {
            tabText=firstResult[2];
        }
        // Highlight the text for the selected tab
        if(tabsVisible[x]==tabToShow) {
            newTabText='<SPAN class="tabTextHighlighted">'+tabText+'</SPAN>';
        }
        else newTabText=tabText;
        document.getElementById(currentTab).innerHTML=firstResult[1]+newTabText+firstResult[3];
    }

    // second we will deal with the tab contents
    for(x=1;x<=numberOfTabs;x++) {
        // First we'll loop through the tabs and find the visible one, getting its position
        // info and hiding it
        var currentTab='tabContents'+x;
        if(document.getElementById(currentTab).style.visibility=='visible') {
            var currentLeft=document.getElementById(currentTab).style.left;
            var currentTop=document.getElementById(currentTab).style.top;
            document.getElementById(currentTab).style.visibility='hidden';
        }
    }
    // Position the tabToShow and make it visible
    var newTab='tabContents'+tabToShow;
    document.getElementById(newTab).style.left=currentLeft;
    document.getElementById(newTab).style.top=currentTop;
    document.getElementById(newTab).style.visibility='visible';

    // Resize container to match newly visible div height
    document.getElementById('mainwrapper').style.height=(document.getElementById(newTab).clientHeight+100)+'px';
}

function confirmDelete(type, companyName) {
    if(type=='company') {
        confirmText='Are you sure you want to DELETE '+companyName+' and all associated records?'; 
    }
    if(type=='location') {
        confirmText='Are you sure you want to DELETE this '+companyName+' location and all associated records? Note that this will include all staff at this locatin AND if this is the company\'s only location it will also include the deletion of the entire company.';
    }
    if(type=='staff') {
        confirmText='Are you sure you want to DELETE '+companyName+' and all associated records?';
    }
    if(type=='favourite') {
        confirmText='Are you sure you want to DELETE '+companyName+' from your favourites?';
    }
    if(type=='credit') {
        confirmText='Are you sure you want to DELETE '+companyName+' from your credits?';
    }
    return confirm(confirmText+'\n\nNote: This can NOT be undone.');
}

function toggleAll(currentForm) {
    // Function to toggle the checkboxes in form formName 
    for (var i = 0; i < currentForm.elements.length; i++) {
        if(currentForm.elements[i].type == 'checkbox'){
            currentForm.elements[i].checked = !(currentForm.elements[i].checked);
        }
    }
}

function checkProvince(country,provinceElement) {
    // Function to show provinces or not applicable depending on the chosen country
    if(country!='Canada') {
        // Set province to n/a
        provinceElement.selectedIndex=provinceElement.length-1;
    }
}

function checkAnchor() {
    // Function to check if there is an anchor in the URL.  If there is display the
    // locations tab
    if(self.document.location.hash!='') {
        // We have a hash
        showTab(2,4,5);
    }
}
