/*
Function : localeSpecificDate
Description: Transforms the incoming dateString value to either an English or 
French format, based upon a two character locale value (EN = English, FR = French). 
The incoming dateString value must be in the format of "MM/DD/YYYY". 
*/
function localeSpecificDate(dateString, locale) {
    
    var d=new Date(dateString);
    var day = null
    var month = null;
    var year = null;
    var monthName = null;
    var monthNameArray = null;
    
    day = d.getDate();
    month = d.getMonth();
    year = d.getFullYear();
    newDateString = "";
    
    if ( locale == "en" || locale == null ) {
        monthNameArray = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
        monthName = monthNameArray[month];
        newDateString = monthName + " " + day + ", " + year;

    } else if ( locale == "fr" ) { 
        monthNameArray = new Array("janvier","f&eacute;vrier","mars","avril","mai","juin","juillet","ao&ucirc;t","septembre","octobre","novembre","d&eacute;cembre")
        monthName = monthNameArray[month];
        newDateString = day + " " + monthName + " " + year;
    
    } else {
        return null;
    }

    return newDateString;
}



/*
Function : buildWebFilePath
Description: Based upon the incoming attributes of a content item, this function 
creates a string that represents the web file path to retrieve the content item 
from the CMS. Used primarily to retrieve images. 
*/
function buildWebFilePath(securityGroup, account, docType, contentID, extension) {

    var baseWebFilePath = "groups/<SECURITYGROUP>/@<ACCOUNT>/documents/<DOCTYPE>/<CONTENTID>.<EXTENSION>";
    var newWebFilePath = "";

    newWebFilePath = baseWebFilePath;
    newWebFilePath = newWebFilePath.replace("<SECURITYGROUP>", securityGroup);
    
    if (account != "") {
        newWebFilePath = newWebFilePath.replace("<ACCOUNT>", account);
    } else {
        newWebFilePath = newWebFilePath.replace("/@<ACCOUNT>", "");
    }
    
    newWebFilePath = newWebFilePath.replace("<DOCTYPE>", docType);
    newWebFilePath = newWebFilePath.replace("<CONTENTID>", contentID);
    newWebFilePath = newWebFilePath.replace("<EXTENSION>", extension);
    newWebFilePath = newWebFilePath.toLowerCase();
    
    return newWebFilePath;
}



/*
Function : removeExtraQuotes
Description: Removes unmatched double quotes within a string. If strInput contains 
an odd number of double quotes ("), this function will remove the last double quote 
from the string.
*/
function removeExtraQuotes(strInput) {

    var ds = new Array();
    var di = 0;
    var quote = "\"";
    var pos = 0;
    var start = 0;
    var numberOfElements = 0;
    var newString = "";

    // Get the position of the first double quote
    pos = strInput.indexOf(quote, start);

    // Add an element to array ds that contains the position of every double quote
    // in the string strInput
    while (pos != -1) {
        ds[di++] = pos;
        start = pos + 1;
        pos = strInput.indexOf(quote, start);
    }

    numberOfElements = ds.length;

    // If there is an odd number of elements in ds, then remove the last double quote
    // from string strInput, otherwise return the string unchanged
    if ( (numberOfElements % 2) != 0 ) {
        pos = ds[numberOfElements - 1];
        newString = strInput.substring(0, pos) + strInput.substring(pos + 1);
    } else {
        newString = strInput;
    }

    return newString;

}



/*
Function : getKeywords
Description: Builds an array of elements that contains the keywords within a string, delimited 
by a space or by group of words within double quotes.
*/
function getKeywords(strInput) {
    var ds = new Array();
    var di = 0;

    var singleChar = "";
    var keyWord = "";
    var foundFirstQuote = false;
    var quote = "\"";

    // Loop through and examine each character in the string strInput
    for (i = 0 ; i < strInput.length ; i++) {
        singleChar = strInput.substr(i,1);

        // If the current character is a space and it is not within a keyword
        // within double quotes, then add a new element to array ds with the 
        // current value of keyWord.
        if ( singleChar == " " && !foundFirstQuote ) {
            if ( keyWord.length > 0 ) {
                ds[di++] = keyWord;
                keyWord = "";
            }
        
        // If the current character is a double quote, and we are already adding 
        // a keyword within double quotes (foundFirstQuote == true) then add the
        // current keword to the array and reset foundFirstQuote to false. 
        // Otherwise, add the current character to the variable keyWord
        } else if ( singleChar == quote ) {
            if ( foundFirstQuote ) {
                if ( keyWord.length > 0 ) {
                    keyWord = keyWord + singleChar;
                    ds[di++] = keyWord;
                    keyWord = "";
                    foundFirstQuote = false;
                }
            } else {
                keyWord = keyWord + singleChar;
                foundFirstQuote = true;
            }
        } else {
            keyWord = keyWord + singleChar;
        }
    }

    // After looping through all the characters in strInput, add the current value 
    // of keyWord to the array
    if ( keyWord.length > 0 ) {
        ds[di++] = keyWord;
        keyWord = "";
    }

    return ds;

}



/*
Function : buildVeritySearchString
Description: Builds a Verity search string based upon an array of keywords.
*/
function buildVeritySearchString(keyWords) {


    var basicVerityClause = "<MANY><THESAURUS>xxxx <ACCRUE> <MANY><STEM>yyyy"
    var newClause = "";
    var newSearchString = "";
    var i = 0;
    var quote = "\"";
    var keyWord = "";



    // Loop through the keyWords array and build a verity search clause for each
    // keyWord that is not enclosed in double quotes.
    // If the keyWord is enclosed in double quotes, just append the keyWord to the verity search string.
    for (i = 0 ; i < keyWords.length ; i++) {
        keyWord = keyWords[i];

        if ( keyWord.indexOf(quote) == -1 ) {
            // Special case if keyWord is 'and', 'or', or 'not'. It must be wrapped in single quotes to cooperate with <accrue>.
            if (keyWord == "and" || keyWord == "or" || keyWord == "not") {
                keyWord = "'" + keyWord + "'";
            }
            newClause = basicVerityClause.replace("xxxx", keyWord);
            newClause = newClause.replace("yyyy", keyWord);
            newSearchString = newSearchString + newClause;
        } else {
            newSearchString = newSearchString + keyWord;
        }
        
        if ( i < keyWords.length - 1) {
            newSearchString = newSearchString + " <ACCRUE> ";
        }
    }

    return newSearchString;

}



/*
Function : stripChars
Description: Examines strInput and removes any characters that are contained in strCharsToRemove.
*/
function stripChars(strInput, strCharsToRemove) {

    var cleanString = "";
    var singleChar = "";
    
    if ( strInput == null ) {
            return strInput;
    }
    
    if ( strCharsToRemove == null || strCharsToRemove.length == 0 ) {
        return strInput;
    }

    for (i = 0 ; i < strInput.length ; i++) {
        singleChar = strInput.substr(i,1);
        if ( strCharsToRemove.indexOf(singleChar) == -1 ) {
            cleanString = cleanString + singleChar;
        }
    }
    return cleanString;
}



/*
Function : buildSearchString
Description: Examines strInput, removes reserved characters and builds a search string.
*/
function buildSearchString(strInput, reservedChars) {
    
    var searchString = strInput;

    if ( searchString == null || searchString.length == 0  ) {
        return searchString;
    }

    reservedChars = stripChars(reservedChars, " ");

    // Removes any reserved character from searchString
    searchString = stripChars(searchString, reservedChars);
    
    // Removes any unmatching double quotes
    searchString = removeExtraQuotes(searchString);
    
    // Returns an array of keyWords, delimited by spaces    
    keyWords = getKeywords(searchString);
    
    // Builds a Verity based search string based upon an
    // array of keywords
    veritySearchString = buildVeritySearchString(keyWords);

    return veritySearchString;

}


/*
Function : new_window
Description: Opens a new browser window with a URL based on the incoming value.
*/
function new_window(location) 
{
    window.open(''+location+'','', 'width=650,height=450,left=100,top=100,toolbar=1,location=1,directories=1,status=1,resizable=1,scrollbars=1,menubar=1')
}



/*
Function : searchHelp
Description: Opens a new browser window with a URL based on the incoming value.
*/
function searchHelp(location) {
    var helpWindow = null;
    
    helpWindow = window.open(''+location+'','', 'width=575,height=200,left=250,top=250,toolbar=0,location=0,directories=0,status=0,resizable=1,scrollbars=1,menubar=0')
}



/*
Function : contactUsForm
Description: Opens a new browser window with a URL based on the incoming value.

Aug 21/2007 - BOBMA - DEPRECIATED
New email component with new calling function created at eMedia request.
Please use contactUs(name, email, nodeId, siteId);
*/
function contactUsForm(stellentUser, nodeId, siteId) {
    
    var newUrl = "";
	newUrl += g_httpCgiUrl;
	newUrl += "nodeId=" + nodeId;
	if( typeof siteId != _U && siteId != null )
	{
		newUrl += "&siteId=" + siteId;
	}
	newUrl += "&toStellentUser=" + stellentUser;
    
    window.open(''+newUrl+'','', 'width=550,height=610,left=250,top=30,toolbar=0,location=0,directories=0,status=1,resizable=1,scrollbars=1,menubar=0')
}


/*
Aug 21/2007 - new function for use with new email component

Function : contactUs
Description: Opens a new browser window with a URL based on the incoming value.
*/
function contactUs(name, emailAddr, nodeId, siteId) {
    
    var newUrl = "";
	newUrl += g_httpCgiUrl;
	newUrl += "nodeId=" + nodeId;
	if( typeof siteId != _U && siteId != null )
	{
		newUrl += "&siteId=" + siteId;
	}
	newUrl += "&name=" + name;
	newUrl += "&email=" + emailAddr;
    
    window.open(''+newUrl+'','', 'width=550,height=610,left=250,top=30,toolbar=0,location=1,directories=0,status=1,resizable=1,scrollbars=1,menubar=0');
}



/*
Function : printer_friendly
Description: Opens a new browser window with a URL based on the incoming value.
*/
function printer_friendly(location) 
{
    window.open(''+location+'','', 'width=700,height=550,left=100,top=100,toolbar=0,location=0,directories=0,status=1,resizable=1,scrollbars=1,menubar=0')
}



/*
Function : getParmValue
Description: Returns a parameter value based on the incoming paramName 
value from the incoming url string. Returns null, if not found.
*/
function getParmValue(url, paramName) {

    var tempValue = "";
    var paramValue = null;
    var pos = url.indexOf( paramName );
    var i = 0;
        
    if( pos != -1 )    {
        pos = pos + paramName.length + 1;
    
        while ( 1 == 1) {
        
            if ( (pos + i) >  url.length ) {
                break;
            }
            
            var singleChar = url.substr( pos+i, 1 );
            if ( singleChar == "&" ) {
                break;
            }
            i++;
            tempValue += singleChar;
        }
        paramValue = tempValue;
    } 

    return paramValue;

}



/*
Function : removeParmValue
Description: Returns a string based on the incoming url value but 
with the incoming parmName and associated value removed.
*/
function removeParmValue(url, parmName) {
    var parmValue = "";
    var newUrl = "";
    
    parmValue = getParmValue(url, parmName);
    newUrl = url.replace("&" + parmName + "=" + parmValue, "");
    
    return newUrl;
}



/*
Function : appendExistingSubNodes
Description: Appends an array of existing nodes (newSubNodes) to the 
targetNode subNode array.
*/
function appendExistingSubNodes(targetNode, newSubNodes) {

    var numberOfNewSubNodes = 0;
    var numberOfExistingSubNodes = 0;
    var i = 0;
    
    if (newSubNodes != null)  {
        numberOfNewSubNodes = newSubNodes.length;
  
        if (targetNode.m_subNodes != null) {
            numberOfExistingSubNodes = targetNode.m_subNodes.length;
        } 
        
        for (i = 0 ; i < numberOfNewSubNodes ; i++) {
            targetNode.m_subNodes[numberOfExistingSubNodes + i] = newSubNodes[i];
        }  
    }     
}    



/*
Function : walkNode
Description: This function returns a node object based upon the 
incoming id value. A node object is returned if the incoming node 
or any of its children match the id value. If none are found, a null 
object is returned.
*/
function walkNode(node, id) {
    var targetNode = null;
    var subNode = null;
    var numberOfSubNodes = 0; 
    var i = 0;
    
    numberOfSubNodes = node.m_subNodes.length;
    if (node.m_id == id) {
            targetNode = node;
    } else {
        for (i = 0 ; i < numberOfSubNodes ; i++) {
            subNode = node.m_subNodes[i]
            targetNode = walkNode(subNode, id);
            if (targetNode != null) {
                break;
            }
        }
    }
    return targetNode;
}

