// redirects user to a specified url
function redirectUser(url) { document.location.href = url; }

//get selected listbox values, delimited by ","
function getListboxSelectedValues(id) {
    //do not go any further if invalid id
    if (typeof (id) != "string")
        return "";

    var lstObj = document.getElementById(id);
    if (lstObj == null)
        return "";

    var strValueList = "";
    if (lstObj.type == "select-one") {
        strValueList = lstObj.options[lstObj.selectedIndex].value;
    }
    else if (lstObj.type == "select-multiple") {
        strValueList = getSelectedItems(lstObj.options);
    }

    //remove first comma if one is found
    if (strValueList.charAt(0) == ",")
        strValueList = String(strValueList).substring(1);

    strValueList = String(strValueList).replace(/\s/gim, "");
    
    return strValueList;
}

//show user def type multi-select listbox field
function showUserDefTypeMultiSelectListbox(fieldName, fieldContainerId, intUserDefTypeID, strChildSelectedValues, strParentFieldValues, strOtherAttributes) {
    var strListboxAttr = "";
    if ((strOtherAttributes == null) || (strOtherAttributes == ""))
        strOtherAttributes = "";

    strListboxAttr = "&OtherAttributes=" + strOtherAttributes;
    
    //build the url with necessary params
    var url = "/Components/GetUserDefTypeMultiSelectListBoxForParentField.asp?ParentFieldValues=" + strParentFieldValues +
                    "&Name=" + fieldName + "&UserDefTypeID=" + intUserDefTypeID + "&UserDefTypeValues=" + encodeURI(strChildSelectedValues) +
                    strListboxAttr + "&bubble=1";
    
    var errorMessage = "unable to load this field...";
    var el = document.getElementById(fieldContainerId);
    new Ajax.Request(url,
		    { method: 'get',
		        onSuccess: function(transport) {
		            if (el != null) {
		                el.innerHTML = transport.responseText;
		                prepareInputsForHints('select');

		            }
		        },
		        onFailure: function(transport) {
		            if (el != null)
		                el.innerHTML = transport.responseText; //errorMessage;

		        }
		    });
}

/* function returns [multi]-listbox selected value (if no defaultValue) defaultValue=''   */
function getListBoxSelectedValue(listboxId,defaultValue){
    if (!defaultValue) defaultValue = "";
    var strResult = getSelectedItems(document.getElementById(listboxId));
    return ((Trim(strResult) == "" || strResult == null) ? defaultValue : strResult);
}

// writes revealed contact name or a link. assumes that "-" is passed (assumes default null exp in html table object
function writeName(proposedName, resumeID) {
    if (proposedName == "-") // null expression
        document.write("<a style='color:#FF6600' title='click to reveal contact info' href='/Employer/RevealContacts.asp?rid=" + resumeID + "'>reveal</a>");
    else
        document.write(proposedName);
}

//need to validate text to check for the following contact info. in text
//return validation error message if either of these pattern's test result return true
//1. check for User's First and Lastname not using re patterns as it is buggy
//2. check for phone numbers using re patterns
//2. check for emails using re patterns
function hasTextAnyContactInformation(strText, firstName, lastName) {
    var strMyText = new String(strText).toLocaleLowerCase();
    
    //do not validate if string is empty
    if (strMyText.length == 0)
        return false;

    var emailRe = /\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2,5}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))/gim
    var phoneRe = /((\+\d{1,3}(-| |\.|\/)?\(?\d\)?(-| |\.|\/)?\d{1,5})|(\(?\d{2,6}\)?))(-| |\.|\/)?(\d{3,4})(-| |\.|\/)?(\d{4})(( x| ext| )\d{1,5}){0,1}/gim

    //test each reg expression pattern
    var blnResult = (emailRe.test(strMyText) || phoneRe.test(strMyText))

    //validate additional expression only if result is not already true
    //result being true implies that contact information is already in text, hence 
    //it is not necessary to check additional expressions
    if (!blnResult) {
        //create a name search pattern
        var namePattern = "(\\b" + firstName + "\\b)|(\\b" + lastName + "\\b)";
        var regExp = new RegExp(namePattern, "gim");
        blnResult = regExp.test(strMyText);
    }
    
    return blnResult;
}
