﻿// JScript File

function handleKey(e, button_name)
{
    var key;

     if(window.event)
     {
          key = window.event.keyCode;     //IE
          
          if(key == 13)
          {
            e.returnValue=false; 
            e.cancel = true; 
            document.getElementById(button_name).click();
          }
          else
            return true;
          
     }
     else
     {
         key = e.which;     //firefox
         
         if(key == 13)
         {
            e.cancelBubble = true;
            document.getElementById(button_name).click();
         }
         else
            return true;
         
     }    
}



function popUpMsg(message)
{
    alert(message);
    return;
}

// BEGIN SECTION: functions used by EnhancedProfileControl.ascx //

function description_left(me) {
    if (me.value.length > 3000) {
        me.value = me.value.substring(0, 3000);
    }
    var length = me.value.length;
    var left = 3000 - length;
    document.getElementById("description_left").innerHTML = left;
}



function FormatUrl(me) {
    p = me.value;

    if (p.length > 0 && p.substring(0, 1) != "h") {
        me.value = "http://" + me.value;
    }
    else if (p.length > 6 && p.substring(0, 7) != "http://") {
        me.value = "http://" + me.value;
    }

    if (me.value.length > 300) {
        me.value = me.value.substring(0, 300);
    }
}



function CopyVideoEmbedCode_1() {
    //alert('test');
    //aspnetForm.ctl00_ContentPlaceHolder1_UltraWebTab1__ctl1_MyListingControl_UltraWebTab__ctl1_EnhancedProfileControl1_taVideoEmbedCodeEncoded_1.value = EncodeXmlLtGt(aspnetForm.taVideoEmbedCode_1.value);
    document.getElementById('ctl00_ContentPlaceHolder1_UltraWebTab1__ctl0_MyListingControl_UltraWebTab__ctl1_EnhancedProfileControl1_taVideoEmbedCodeEncoded_1').value = EncodeXmlLtGt(document.getElementById('taVideoEmbedCode_1').value);
    if (submitForm) {
        document.forms[0].submit();
    }
}



function ShowHide(obj, target) {
    if (document.getElementById(target).style.display == "" || document.getElementById(target).style.display == null) {
        document.getElementById(target).style.display = "none";
    }
    else {
        document.getElementById(target).style.display = "";
    }
}


function DeleteVideo(videoNumber) {
    var idFull = 'ctl00_ContentPlaceHolder1_UltraWebTab1__ctl0_MyListingControl_UltraWebTab__ctl1_EnhancedProfileControl1';
    var videoSrcId = '_tbxVideoSrc_';
    var videoFlashVarsId = '_tbxVideoFlashVars_';
    var videoTitleId = '_tbxVideoTitle_';
    var videoDescId = '_tbxVideoDescription_';
    var videoEmbedCodeId = 'taVideoEmbedCode_';
    var videoEmbedCodeEncodedId = '_taVideoEmbedCodeEncoded_';

    var objVideoSrc = document.getElementById(idFull + videoSrcId + videoNumber);
    objVideoSrc.value = '';

    var objVideoFlashVars = document.getElementById(idFull + videoFlashVarsId + videoNumber);
    objVideoFlashVars.value = '';

    var objVideoTitle = document.getElementById(idFull + videoTitleId + videoNumber);
    objVideoTitle.value = '';

    var objVideoDesc = document.getElementById(idFull + videoDescId + videoNumber);
    objVideoDesc.value = '';

    var objVideoEmbedCode = document.getElementById(videoEmbedCodeId + videoNumber);
    objVideoEmbedCode.value = '';

    var objVideoEmbedCodeEncoded = document.getElementById(idFull + videoEmbedCodeEncodedId + videoNumber);
    objVideoEmbedCodeEncoded.value = '';

    document.getElementById(idFull + '_trVideoObject_' + videoNumber).style.display = "none";

    document.getElementById(idFull + '_tbxVideoBottomFocus_' + videoNumber).PostBackFocusTarget = document.getElementById(idFull + '_tbxVideoBottomFocus_' + videoNumber).id;
    //alert(document.getElementById(idFull + '_tbxVideoBottomFocus_' + videoNumber).PostBackFocusTarget);

    document.getElementById(idFull + '_tbxVideoBottomFocus_' + videoNumber).value = document.getElementById(idFull + '_tbxVideoBottomFocus_' + videoNumber).id;

    document.forms[0].submit();
}



function ParseVideoEmbedCode(obj, IdFull, Id2, VideoSrcId, VideoFlashVarsId) {

    submitForm = false;

    var videoNum = obj.id.substring(obj.id.lastIndexOf("_") + 1);
    //alert(videoNum);

    var lblVideoSrcPlainText = document.getElementById('lbl' + VideoSrcId + videoNum).PlainText;

    var objVideoSrc = document.getElementById(IdFull + VideoSrcId + videoNum);
    var objVideoFlashVars = document.getElementById(IdFull + VideoFlashVarsId + videoNum);

    //alert(objVideoSrc.id);
    //alert(objVideoFlashVars.id);

    if (obj.value.length > 0) {

        var objValue = obj.value.replace(/\r|\n|\r\n|\t|\f|\b|/g, "");
        //     \b	Backspace
        //     \f	Form feed
        //     \n	New line
        //     \r	Carriage return
        //     \t	Tab
        //     \'	Single Quote
        //     \"	Double Quote
        //     \\	Backslash

        var srcStartIdx = objValue.indexOf("src=\"") + 5;
        var srcEndIdx = objValue.indexOf("\"", srcStartIdx);
        var flashVarsStartIdx = objValue.toLowerCase().indexOf("flashvars=\"") + 11;
        var flashVarsEndIdx = objValue.indexOf("\"", flashVarsStartIdx);
        //alert(objValue);
        //alert(srcStartIdx);
        //alert(srcEndIdx);
        //alert(flashVarsStartIdx);
        //alert(flashVarsEndIdx);

        if (srcStartIdx < srcEndIdx && srcStartIdx != -1 && srcEndIdx != -1) {

            /// Validate Video src ///
            var objVideoSrcNew = objValue.substring(srcStartIdx, srcEndIdx);
            if (ValidURL(objVideoSrcNew)) {
                if (objVideoSrc.value.length > 0) {
                    if (objVideoSrcNew != objVideoSrc.value) {
                        switch (confirm("Are your sure you want to change your Video Source?")) {
                            case true:
                                {
                                    objVideoSrc.value = objVideoSrcNew;
                                    submitForm = true;
                                }
                                break;
                            case false:
                                {
                                    submitForm = false;
                                }
                                break;
                            case null:
                                {
                                    submitForm = false;
                                }
                                break;
                        }
                    }
                }
                else {
                    objVideoSrc.value = objVideoSrcNew;
                    submitForm = true;
                }
            }

        }
        else {
            alert("Sorry, we are unable to automaticlly parse your Embed Code. Please enter the \"src\" manually.");
        }

        /// Validate FlashVars ///
        if (flashVarsStartIdx < flashVarsEndIdx && flashVarsStartIdx != 10 && flashVarsEndIdx != -1) {

            var objVideoFlashVarsNew = objValue.substring(flashVarsStartIdx, flashVarsEndIdx);

            if (objVideoFlashVars.value.length > 0) {
                if (objVideoFlashVarsNew != objVideoFlashVars.value) {
                    if (submitForm) {
                        objVideoFlashVars.value = objVideoFlashVarsNew;
                    }
                    else {
                        switch (confirm("Are your sure you want to change your Video Source?")) {
                            case true:
                                {
                                    objVideoFlashVars.value = objVideoFlashVarsNew;
                                    submitForm = true;
                                }
                                break;
                            case false:
                                {
                                    submitForm = false;
                                }
                                break;
                            case null:
                                {
                                    submitForm = false;
                                }
                                break;
                        }
                    }
                }
            }
            else {
                objVideoFlashVars.value = objVideoFlashVarsNew;
                submitForm = true;
            }

        }
        else {
            if (objVideoFlashVars.value.length > 0) {
                if (submitForm) {
                    objVideoFlashVars.value = objVideoFlashVarsNew;
                }
                else {
                    switch (confirm("Are your sure you want to change your Video Source?")) {
                        case true:
                            {
                                objVideoFlashVars.value = "";
                                submitForm = true;
                            }
                            break;
                        case false:
                            {
                                submitForm = false;
                            }
                            break;
                        case null:
                            {
                                submitForm = false;
                            }
                            break;
                    }
                }
            }
        }
    }
    else {
        if (objVideoSrc.value.length > 0) {
            switch (confirm("Your \"Flash Player\" Embed code has been removed. Are your sure you want to delete your embeded video?")) {
                case true:
                    {
                        DeleteVideo(videoNum);
                    }
                    break;
                case false:
                    {
                    }
                    break;
                case null:
                    {
                    }
                    break;
            }
        }
    }

    //document.getElementById(IdFull + '_tbxVideoBottomFocus_' + videoNum).focus();

}



function ValidURL(url) {
    /*
    var v = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");                 

        if (!v.test(url)) {
    alert("The \"src\" in the embed code or Video \"src\" is not a valid URL. Please enter a valid URL in the Video \"src\" text box.");
    return false;
    }
    else {
    return true;
    }
    */
    var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    //return regexp.test(s);

    if (!regexp.test(url)) {
        alert("The \"src\" in the embed code or Video \"src\" is not a valid URL. Please enter a valid URL in the Video \"src\" text box.");
        return false;
    }
    else {
        return true;
    }

}



function EncodeXmlLtGt(str) {
    return str.replace(/</g, "&lt;").replace(/>/g, "&gt;");
}



function DecodeXmlLtGt(str) {
    return str.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
}


// END SECTION: functions used by EnhancedProfileControl.ascx //

//for overlib
var ol_border = "0";
var ol_fgcolor = "#5EB9E5";
var ol_textcolor = "#FFFFFF";
var ol_cellpad = "10";
