﻿// REAL-TIME USERNAME PREVIEW
function PreviewURL(divName, signupType, valueTextBoxName, replaceValue) {
    document.getElementById(divName).innerHTML = '<span class="urlPreview">http://noveleros.com/' + signupType + '<strong>' + document.getElementById(valueTextBoxName).value.replace(/(\n|\r|' '|[^a-zA-Z0-9_])/g, replaceValue) + '</strong></span>';
}
function PreviewShowURL(divName, ShowName) {
    var friendlyUrl = makeFriendlyURL(ShowName.toLowerCase(), 64);
    document.getElementById(divName).innerHTML = '<span class="urlPreview">http://noveleros.com/novelas/<strong>' + friendlyUrl + '</strong></span>';
}
function PreviewTalentURL(divName, TalentName) {
    var friendlyUrl = makeFriendlyURL(TalentName.toLowerCase(), 64);
    document.getElementById(divName).innerHTML = '<span class="urlPreview">http://noveleros.com/actors/<strong>' + friendlyUrl + '</strong></span>';
}
function PreviewMemberURL(divName, MemberUsername) {
    var friendlyUrl = makeFriendlyURL(MemberUsername, 16);  //Note: We are not using MemberUsername.toLowerCase() since we don't want to change how they type the username
    document.getElementById(divName).innerHTML = '<span class="urlPreview">http://noveleros.com/user/<strong>' + friendlyUrl + '</strong></span>';
}
function PreviewCountryURL(divName, CountryName) {
    var friendlyUrl = makeFriendlyURL(CountryName.toLowerCase(), 64);
    document.getElementById(divName).innerHTML = '<span class="urlPreview">http://noveleros.com/country/<strong>' + friendlyUrl + '</strong></span>';
}

// Is this obsolete?
function MakeFriendlyURL(PotentialURL) {
    var FriendlyUrl = "";
    return FriendlyUrl;
}

function makeFriendlyURL(ogValue, maxLenght) {
    //ogValue = ogValue.toLowerCase();  //NOTE: Not lower-casing any more
    ogValue = ogValue.replaceAll("  ", "-");     //Double spaces to single spaces

    ogValue = ogValue.replaceAll("à", "a");
    ogValue = ogValue.replaceAll("À", "A");
    ogValue = ogValue.replaceAll("á", "a");
    ogValue = ogValue.replaceAll("Á", "A");
    ogValue = ogValue.replaceAll("â", "a");
    ogValue = ogValue.replaceAll("Â", "A");
    ogValue = ogValue.replaceAll("ã", "a");
    ogValue = ogValue.replaceAll("Ã", "A");
    ogValue = ogValue.replaceAll("ä", "a");
    ogValue = ogValue.replaceAll("Ä", "A");
    ogValue = ogValue.replaceAll("å", "a");
    ogValue = ogValue.replaceAll("Å", "A");

    ogValue = ogValue.replaceAll("è", "e");
    ogValue = ogValue.replaceAll("È", "E");
    ogValue = ogValue.replaceAll("é", "e");
    ogValue = ogValue.replaceAll("É", "E");
    ogValue = ogValue.replaceAll("ê", "e");
    ogValue = ogValue.replaceAll("Ê", "E");
    ogValue = ogValue.replaceAll("ë", "e");
    ogValue = ogValue.replaceAll("Ë", "E");

    ogValue = ogValue.replaceAll("ì", "i");
    ogValue = ogValue.replaceAll("Ì", "I");
    ogValue = ogValue.replaceAll("í", "i");
    ogValue = ogValue.replaceAll("Í", "I");
    ogValue = ogValue.replaceAll("î", "i");
    ogValue = ogValue.replaceAll("Î", "I");
    ogValue = ogValue.replaceAll("ï", "i");
    ogValue = ogValue.replaceAll("Ï", "I");

    ogValue = ogValue.replaceAll("ò", "o");
    ogValue = ogValue.replaceAll("Ò", "O");
    ogValue = ogValue.replaceAll("ó", "o");
    ogValue = ogValue.replaceAll("Ó", "O");
    ogValue = ogValue.replaceAll("ô", "o");
    ogValue = ogValue.replaceAll("Ô", "O");
    ogValue = ogValue.replaceAll("õ", "o");
    ogValue = ogValue.replaceAll("Õ", "O");
    ogValue = ogValue.replaceAll("ö", "o");
    ogValue = ogValue.replaceAll("Ö", "O");

    ogValue = ogValue.replaceAll("ù", "u");
    ogValue = ogValue.replaceAll("Ù", "U");
    ogValue = ogValue.replaceAll("ú", "u");
    ogValue = ogValue.replaceAll("Ú", "U");
    ogValue = ogValue.replaceAll("û", "u");
    ogValue = ogValue.replaceAll("Û", "U");
    ogValue = ogValue.replaceAll("ü", "u");
    ogValue = ogValue.replaceAll("Ü", "U");

    ogValue = ogValue.replaceAll("ñ", "n");
    ogValue = ogValue.replaceAll("Ñ", "N");

    ogValue = ogValue.replaceAll("ß", "B");
    ogValue = ogValue.replaceAll("ç", "c");
    ogValue = ogValue.replaceAll("Ç", "C");

    ogValue = ogValue.replaceAll("_", "-");

    ogValue = ogValue.replaceAll("'", "");

    var newValue = ogValue.replace(/(\n|\r|' '|[^a-zA-Z0-9])/g, "-");

    //Remove all occurances of "--"
    while (newValue.indexOf("--") > -1) {
        newValue = newValue.replaceAll("--", "-");
    }

    var strLen = 0;
    //Do not allow "-" to be the first character
    strLen = newValue.length;
    if (strLen != 0 && newValue.substring(0, 1) == "-") {
        while (strLen != 0 && newValue.substring(0, 1) == "-") {
            newValue = newValue.substring(1, strLen);
            strLen = newValue.length;
        }
    }

    //    //Do not allow "-" to be the last character
    //    strLen = newValue.length; 
    //    if ( strLen != 0 && newValue.substring (strLen-1, strLen) == "-" ){
    //        while ( strLen != 0 && newValue.substring (strLen-1, strLen) == "-" ){
    //            newValue = newValue.substring(0, strLen-1);
    //            strLen = newValue.length;
    //        }
    //    }

    strLen = newValue.length;
    if (strLen > maxLenght) {
        newValue = newValue.substring(0, maxLenght);
    }

    return newValue;
}

function redirectToPagePage(ddlName) {
    box = document.getElementById(ddlName);
    destination = box.options[box.selectedIndex].value;
    if (destination) location.href = destination;
}

//This code is used to provide a reference to the RadWindow "wrapper"
function GetRadWindow() {
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    return oWindow;
}
//SizeToFit RadWindow Pop-up
function SizeToFitRadWindow() {
    window.setTimeout(
        function() {
            var oWnd = GetRadWindow();
            oWnd.SetWidth(document.body.scrollWidth + 30);
            oWnd.SetHeight(document.body.scrollHeight + 70);

        }, 10
    );
}


function AddToBookmarks() {
    var url = "http://noveleros.com/";
    var title = "Noveleros.com - Telenovelas";

    try {
        if (window.sidebar) { // Mozilla Firefox Bookmark
            return window.sidebar.addPanel(title, url, "");
            //window.sidebar.addPanel(unescape(title), unescape(url), "");
        } else if (window.external) { // IE Favorite
            return window.external.AddFavorite(url, title);
        } else if (window.opera && window.print) { // Opera Hotlist
            return true;
        }
    } catch (err) {
        // do nothing
    }

}

//Note: Not using JQuery
function expandCollapse() {
    for (var i = 0; i < expandCollapse.arguments.length; i++) {
        var element = document.getElementById(expandCollapse.arguments[i]);
        element.style.display = (element.style.display == "none") ? "block" : "none";
    }
}

function textboxMultilineMaxlength(objTextbox, intMaxLength, strStatusDivId) {
    if (strStatusDivId != null && document.getElementById(strStatusDivId) != null) {
        if (intMaxLength - objTextbox.value.length >= 30) {
            document.getElementById(strStatusDivId).innerHTML = (intMaxLength - objTextbox.value.length);
        }
        else if (intMaxLength - objTextbox.value.length >= 20 && intMaxLength - objTextbox.value.length < 30) {
            document.getElementById(strStatusDivId).innerHTML = "<span style='color:#FFCC66; font-weight:bold;'>" + (intMaxLength - objTextbox.value.length) + "</span>";
        }
        else if (intMaxLength - objTextbox.value.length >= 10 && intMaxLength - objTextbox.value.length < 20) {
            document.getElementById(strStatusDivId).innerHTML = "<span style='color:#FF9900; font-weight:bold;'>" + (intMaxLength - objTextbox.value.length) + "</span>";
        }
        else if (intMaxLength - objTextbox.value.length < 10) {
            document.getElementById(strStatusDivId).innerHTML = "<span style='color:#F00; font-weight:bold;'>" + (intMaxLength - objTextbox.value.length) + "</span>"
        }

        //        if (objTextbox.value.length >= intMaxLength)
        //            return false;
        //        else 
        return true;
    }
}


//String Replacement
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
// Replaces all instances of the given substring.
String.prototype.replaceAll = function(strTarget, /* The substring you want to replace */strSubString /* The string you want to replace in. */) {
    var strText = this;
    var intIndexOfMatch = strText.indexOf(strTarget);

    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1) {
        // Relace out the current instance.
        strText = strText.replace(strTarget, strSubString)

        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf(strTarget);
    }

    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return (strText);
}

function insertBBcode(fieldId, tag) {
    field = document.getElementById(fieldId);
    //	if(tag=='b' || tag=='i' || tag=='u' || tag == 'php' || tag == 'code')
    //	{

    if (document.selection) {
        field.focus();
        var selected = document.selection.createRange().text;
        sel = document.selection.createRange();
        sel.text = '[' + tag + ']' + selected + '[/' + tag + ']';
    }
    //MOZILLA/NETSCAPE/SAFARI support
    else if (field.selectionStart || field.selectionStart == 0) {
        var startPos = field.selectionStart;
        var endPos = field.selectionEnd;
        var selected = field.value.substring(startPos, endPos);
        field.focus();
        //field.value = field.value.substring(0, startPos) + '[' + tag + '][/' + tag +']' + field.value.substring(endPos, field.value.length);
        field.value = field.value.substring(0, startPos) + '[' + tag + ']' + selected + '[/' + tag + ']' + field.value.substring(endPos, field.value.length);
    }

    return false;
}

//Allow the defined "Default Button" to submit the form for a particular region AND prevent the Enter button from triggering a post back if it's in a TextArea
function FireDefaultButton(event, target) {
    // srcElement is for IE
    var element = event.target || event.srcElement;

    if (13 == event.keyCode && !(element && "textarea" == element.tagName.toLowerCase())) {
        var defaultButton;
        defaultButton = document.getElementById(target);

        if (defaultButton && "undefined" != typeof defaultButton.click) {
            defaultButton.click();
            event.cancelBubble = true;
            if (event.stopPropagation)
                event.stopPropagation();
            return false;
        }
    }
    return true;
}

//Search Box
function searchFocus(q) {
    q.style.background = '#ffffff';
}
function searchBlur(q) {
    if (q.value == '') {
        q.style.background = '#FFFFFF url(http:\x2F\x2Fwww.google.com\x2Fcoop\x2Fintl\x2Fen\x2Fimages\x2Fgoogle_custom_search_watermark.gif) left no-repeat';
    }
}