Url = function() { }

Url.prototype =
{
    _relativeRoot: '<%= ResolveUrl("~/") %>',

    resolve: function(relative) {
        var resolved = relative;
        if (relative[0] == '~') resolved = this._relativeRoot + relative.substring(2);
        return resolved;
    }
}

$Url = new Url();

function GetDialogFeatures(dialogWidth, dialogHeight)
{
    return "dialogWidth: " + dialogWidth + "px;dialogHeight: " + dialogHeight + 
            "px;status: no;unadorned: yes; scroll: no; help: no;";
}

function ShowFileUploader1(dialogWidth, dialogHeight, type, showList, hidID, lblID)
{
    var hidControl = $get(hidID);
    var lblControl = $get(lblID);
    var retValue = window.showModalDialog("FileUpload.aspx?type=" + type + "&showList=" + showList, "", GetDialogFeatures(dialogWidth, dialogHeight));   
    
    if (retValue)
    {
        retValue = retValue.replace(/%20/g,' ');
        var start = retValue.lastIndexOf('/')+1;
        var end = retValue.length;
        var fileName = retValue.substring(start, end);

        hidControl.value = retValue;  
        lblControl.innerText = fileName; 
    }
}
function ShowFileUploader2(dialogWidth, dialogHeight, type, showList, hidID, btnID)
{
    var hidControl = $get(hidID);
    var btnClick = $get(btnID);
    var retValue = window.showModalDialog("FileUpload.aspx?type=" + type + "&showList=" + showList, "", GetDialogFeatures(dialogWidth, dialogHeight));   
    if (retValue)
    {
        retValue = retValue.replace(/%20/g,' ');
        hidControl.value=retValue;
        
        if(btnClick != null)
            btnClick.click();
    }
}
function ShowFileUploader3(dialogWidth, dialogHeight, type, showList, hidID, txtID)
{
    var hidControl = $get(hidID);
    var txtControl = $get(txtID);
    var retValue = window.showModalDialog("FileUpload.aspx?type=" + type + "&showList=" + showList, "", GetDialogFeatures(dialogWidth, dialogHeight));   
    
    if (retValue)
    {
        retValue = retValue.replace(/%20/g,' ');
        var start = retValue.lastIndexOf('/')+1;
        var end = retValue.length;
        var fileName = retValue.substring(start, end);
        var attachments = txtControl.value;
       
        if(attachments.length == 0)
        {
            txtControl.value = fileName; 
            hidControl.value = fileName;
        }
        else
        {
            txtControl.value = attachments + '; ' + fileName; 
            hidControl.value = attachments + '; ' + fileName; 
        }
    }   
}
function ShowFileUploader4(dialogWidth, dialogHeight, type, showList, resolveURL)
{
    var retValue = window.showModalDialog("FileUpload.aspx?type=" + type + "&showList=" + showList + "&resolveURL=" + resolveURL, "", GetDialogFeatures(dialogWidth, dialogHeight));   
    
    if (retValue)
    {
        retValue = retValue.replace(/%20/g,' ');
        
        if(type=='Image')
            retValue = '<img src="' + retValue + '"/>';
        else if(type=='Video')
            retValue = getVideoHTML(retValue);
        else 
        {
            var start = retValue.lastIndexOf('/')+1;
            var end = retValue.length;
            var fileName = retValue.substring(start, end);
            retValue = '<a href="' + retValue + '" >' + fileName + '</a>';  
        }
        
        return retValue;
    }
    else
        return '';
}
function getVideoHTML(url)
{
    var html='<embed pluginspage="http://www.microsoft.com/windows/windowsmedia/download/AllDownloads.aspx/" filename="'+ url + '" ' + 
    'showstatusbar="0" showdisplay="0" autostart="0" showcontrols="1" enablefullscreencontrols="1"  allowchangedisplaysize="1" ' + 
    'type="application/x-mplayer2" height="345" src="'+ url + '" width="444" autoload="0" name="MediaPlayer"  ></embed>';  
    
    
    return html;
}
function RemoveImage(lbl, hid)
{
    var lblbx = $get(lbl);
    var hidbx = $get(hid);
    
    hidbx.value = 'removed' + lblbx.innerText;
    lblbx.innerText = ''; 
}
function ClearAttachments(txt, hid)
{
    var txtbx = $get(txt);
    var hidbx = $get(hid);
    
    hidbx.value = '';
    txtbx.value = ''; 
}
function RemoveTranscript(lbl, hid)
{
    var lblbx = $get(lbl);
    var hidbx = $get(hid);
    
    hidbx.value = 'removed';
    lblbx.innerText = ''; 
}

function SaveVideoView(saveArgs)
{      
    wsG2I.SaveVideo(saveArgs)
}

var curIndex = 0;
var callIDs = new Array(1);
var callDate = new Array(1);
var callComments = new Array(1);


function LoadArrays(userID, sportInfoID, collegeID, ctrlIDs)
{   
    callIDs.length = 0;
    callDate.length = 0;
    callComments.length = 0;
    curIndex = 0;
        
    wsG2I.GetCommentsUsageDetail(userID, sportInfoID, collegeID, OnCommentComplete, OnCommentError, ctrlIDs)
}

function LoadVendorArrays(userID, sportID, collegeID, ctrlIDs)
{   
    callIDs.length = 0;
    callDate.length = 0;
    callComments.length = 0;
    curIndex = 0;
        
    wsG2I.GetVendorCommentsUsageDetail(userID, sportID, collegeID, OnCommentComplete, OnCommentError, ctrlIDs)
}

function OnCommentComplete(result, userContext)
{
    var i = 0;
    var delimeter = String.fromCharCode(165);
    var delimeterSections = String.fromCharCode(167);
    var comments = new Array();
    var dates = new Array();
    var ids = new Array();
    var delimitedComments = '';
    var delimitedDates = '';
    var delimitedIDs = '';
   
    if(result.length > 0)
    {
        delimitedComments = result.split(delimeterSections)[0];
        delimitedDates = result.split(delimeterSections)[1];
        delimitedIDs = result.split(delimeterSections)[2];
    }
    
    if(delimitedIDs.length > 0)
    {
        comments = delimitedComments.split(delimeter);
        dates = delimitedDates.split(delimeter);
        ids = delimitedIDs.split(delimeter);
        curIndex = 0;
        
        callIDs.length = ids.length;
        callDate.length = ids.length;
        callComments.length = ids.length;
    }
    
    for(i=0; i<ids.length; i++)
    {
         callIDs[i] = ids[i];
         callComments[i] = comments[i];
         callDate[i] = dates[i];
    }

    var txtcomments = $get(userContext.split(delimeter)[0]);
    var txtDate = $get(userContext.split(delimeter)[1]);
    var hidIDs = $get(userContext.split(delimeter)[2]);
    var saveLnk = $get(userContext.split(delimeter)[3]);
    var cancelLnk = $get(userContext.split(delimeter)[4]);
    var floaterID = userContext.split(delimeter)[5];
    var nextID = userContext.split(delimeter)[6];
    var prevID = userContext.split(delimeter)[7];
    var baseID = userContext.split(delimeter)[8];
    
    if(callIDs.length > 0)
    {
        txtDate.value = callDate[curIndex];
        txtcomments.value = callComments[curIndex];
        hidIDs.value = callIDs[curIndex];
        
        txtDate.disabled = false;
        txtcomments.disabled = false;
    }
    else
    {
        txtDate.disabled = true;
        txtcomments.disabled = true;
    }

    EnableNavs(prevID, nextID);
    
    showUsagePopup(baseID, floaterID, -1, 45);
}
function OnCommentError(result)
{
    alert("Error: " + result.get_message());
}
function ShowComments(direction, date, comments, prevID, nextID, saveID, cancelID, hidIDsID)
{
    var txtcomments = $get(comments);
    var txtDate = $get(date);
    var hidIDs = $get(hidIDsID);
    var saveLnk = $get(saveID);
    var cancelLnk = $get(cancelID);
    
    saveLnk.disabled = true;
    cancelLnk.disabled = true;

    if(direction == 'next')
    {
        if(callDate[curIndex + 1] == undefined)
            return false;
            
        curIndex = curIndex + 1;
    }
    else if(direction == 'previous')
    {
        if(callDate[curIndex - 1] == undefined)
            return false;
    
        curIndex = curIndex - 1;
    }

    if(callIDs.length > 0)
    {
        txtDate.value = callDate[curIndex];
        txtcomments.value = callComments[curIndex];
        hidIDs.value = callIDs[curIndex];
        txtcomments.disabled = false;
        txtDate.disabled = false;
    }

    EnableNavs(prevID, nextID);
    

    return false;
}
        
function NewComment(date, comments, prevID, nextID, newID, hidIDsID, cancelID)
{
    var txtcomments = $get(comments);
    var txtDate = $get(date);
    var hidIDs = $get(hidIDsID);
    var prevLnk = $get(prevID);
    var nextLnk = $get(nextID);
    var newLnk = $get(newID);
    var cancelLnk = $get(cancelID);
    
    hidIDs.value='-1';
    txtcomments.value='';
    txtDate.value='';
    txtcomments.disabled = false;
    txtDate.disabled = false;
    
    prevLnk.disabled = true;
    nextLnk.disabled = true;
    newLnk.disabled = true;
    cancelLnk.disabled = false;
    txtDate.focus();
}
function EnableSave(date, comments, saveID, cancelID, hidIDsID)
{
    var txtcomments = $get(comments);
    var txtDate = $get(date);
    var saveLnk = $get(saveID);
    var cancelLnk = $get(cancelID);
    var hidIDs = $get(hidIDsID);
    
    if(txtcomments.value.length>0 && txtDate.value.length>0)
    {
         saveLnk.disabled = false;
         cancelLnk.disabled = false;
    }
    else
    {
        if(hidIDs.value != "-1")
            cancelLnk.disabled = true;  
              
        saveLnk.disabled = true;
    }
}
function CancelSave(date, comments, saveID, cancelID, hidIDsID, prevID, nextID, newID)
{
    var txtcomments = $get(comments);
    var txtDate = $get(date);
    var saveLnk = $get(saveID);
    var cancelLnk = $get(cancelID);
    var hidIDs = $get(hidIDsID);
    var newLnk = $get(newID);
    
    saveLnk.disabled = true;
    cancelLnk.disabled = true;
    
    if(hidIDs.value=='-1' && callIDs.length == 0)
    {
        txtcomments.value = '';
        txtDate.value = '';
        hidIDs.value = '';
        txtcomments.disabled = true;
        txtDate.disabled = true;
    }
    else
    {
        txtDate.value = callDate[curIndex];
        txtcomments.value = callComments[curIndex];
        hidIDs.value = callIDs[curIndex];
        txtcomments.disabled = false;
        txtDate.disabled = false;
    }
    
    newLnk.disabled = false;
    
    EnableNavs(prevID, nextID);
}
function SaveComment(dateID, commentsID, hidDateID, hidCommentsID, hidIDsID, lnkRealSaveID, lnkSaveID, cancelID, prevID, nextID)
{
    var txtComments = $get(commentsID);
    var txtDate = $get(dateID);
    var hidComments = $get(hidCommentsID);
    var hidDate = $get(hidDateID);
    var hidIDs = $get(hidIDsID);
    var lnkRealSave = $get(lnkRealSaveID);
    var lnkSave = $get(lnkSaveID);
    var cancelLnk = $get(cancelID);
    var prevLnk = $get(prevID);
    var arrayLen = callDate.length + 1;
    
    lnkSave.disabled = true;
    cancelLnk.disabled = true;
    
    hidComments.value = txtComments.value;
    hidDate.value = txtDate.value;
        
    if(hidIDs.value == '-1')
    {
        callDate.length = arrayLen;
        callComments.length = arrayLen;
        callIDs.length = arrayLen;
        
        curIndex = arrayLen-1;
        callDate[arrayLen-1] = txtDate.value;
        callComments[arrayLen-1]=txtComments.value;

        callIDs[arrayLen-1]='-1';
    }
    else
    {
        callDate[curIndex] = txtDate.value;
        callComments[curIndex]=txtComments.value;
    }
    
//    EnableNavs(prevID, nextID);
    lnkRealSave.click();
}
function EnableNavs(prevID, nextID)
{
    var prevLnk = $get(prevID);
    var nextLnk = $get(nextID);
    var recCnt = callDate.length - 1;
    
    if(recCnt < 0)
        recCnt = 0;
         
    if(curIndex == 0)
    {
        prevLnk.disabled = true;
    }
    else
    {
        prevLnk.disabled = false;   
    }
    if(curIndex == recCnt)
    {
        nextLnk.disabled = true;
    }
    else
    {
        nextLnk.disabled = false; 
    }

}
function SetID(newID)
{
    var i = 0;

    for(i=0; i<callIDs.length; i++)
    {
         if(callIDs[i]=='-1')
         {
             callIDs[i]=newID;
         }
    }
}

function OpenPreview(pagePath)
{   
    var WinSettings = 'resizable=1,height=600,width=650,scrollbars=1,top=10,left=50,location=0';
    var popup = window.open(pagePath, 'Preview', WinSettings);
    popup.moveTo(50, 10);
}
function OpenPrint(pagePath)
{   
    var WinSettings = 'resizable=1,height=600,width=658,scrollbars=1,top=10,left=150,location=0';
    var popup = window.open(pagePath, 'Print', WinSettings);
    popup.moveTo(150, 10);
    //popup.focus();
}
function OpenCompose(ListIndex, SportID)
{ 
    var WinSettings = 'resizable=1,height=690,width=742,scrollbars=0,top=10,left=150,location=0';
    var pagePath = 'Compose.aspx?sc=' + ListIndex + '^' + SportID + ''
    var popup = window.open(pagePath, 'Compose', WinSettings);
    popup.moveTo(150, 10);
    //popup.focus();
}
function OpenVendorCompose(ListIndex, SportID)
{ 
    var WinSettings = 'resizable=1,height=690,width=742,scrollbars=0,top=10,left=150,location=0';
    var pagePath = 'VenCompose.aspx?sc=' + ListIndex + '^' + SportID + ''
    var popup = window.open(pagePath, 'Compose', WinSettings);
//    popup.moveTo(150, 10);
//    //popup.focus();
}
function OpenSchedule()
{ 
    var WinSettings = 'resizable=1,height=720,width=870,scrollbars=0,top=10,left=50,location=0';
    var pagePath = 'Schedule.aspx'
    var popup = window.open(pagePath, 'Schedule', WinSettings);
    popup.moveTo(50, 10);
    //popup.focus();
}
function OpenEmail(SportID)
{ 
    var WinSettings = 'resizable=1,height=650,width=858,scrollbars=0,top=10,left=50,location=0';
    var pagePath = 'Email.aspx?spi=' + SportID + ''
    var popup = window.open(pagePath, 'Email', WinSettings);
    popup.moveTo(50, 10);
    //popup.focus();
}
function OpenEmail2(drpID)
{ 
    var drpSport = $get(drpID);
    var SportID = drpSport.options[drpSport.selectedIndex].value;
    var WinSettings = 'resizable=1,height=650,width=858,scrollbars=0,top=10,left=50';
    var pagePath = 'Email.aspx?spi=' + SportID + ''
    var popup = window.open(pagePath, 'Email', WinSettings);
    popup.moveTo(50, 10);
    //popup.focus();
}
function OpenPreviewProfile(drpID)
{ 
    var drpSport = $get(drpID);
    var SportID = drpSport.options[drpSport.selectedIndex].value;
    var WinSettings = 'resizable=1, location=0, menubar=1, toolbar=1, height=350, width=958, scrollbars=1, status=1, fullscreen=no, top=10, left=10';
    var pagePath = 'AthleteSummary.aspx?preview=' + SportID + ''
    var popup = window.open(pagePath, 'Profile', WinSettings);
    //popup.moveTo(10, 10);
}
function OpenPreviewProfile2(SportID)
{ 
    var WinSettings = 'resizable=1, location=0, menubar=1, toolbar=1, height=350, width=958,scrollbars=1, status=1, fullscreen=no, top=10, left=10';
    var pagePath = 'AthleteSummary.aspx?preview=' + SportID + ''
    var popup = window.open(pagePath, 'Profile', WinSettings);
    //popup.moveTo(10, 10);
    //popup.focus();
}
function OpenPreviewVendorProfile(EntryID)
{ 
    var WinSettings = 'resizable=1, location=0, menubar=1, toolbar=1, height=350, width=958, scrollbars=1, status=1, fullscreen=no, top=10, left=10';
    var pagePath = 'VenProfile.aspx?preview=' + EntryID + ''
    var popup = window.open(pagePath, 'Profile', WinSettings);
    //popup.moveTo(10, 10);
    //popup.focus();
}
function OpenBilling(xls)
{   
    var WinSettings = 'resizable=1,height=700,width=400,scrollbars=1,top=10,left=10,location=0';
    var pagePath = 'Media/Reports/' + xls + ''
    var popup = window.open(pagePath, 'Billing', WinSettings);
}
function timeOut(uid, to)
{
        var expr =  "window.location.href='default.aspx?logout=true&id=" + uid + "'"
        window.setTimeout(expr,to)
}
function InsertMergeTag(editorID, drpID)
{
    var oEditor = FCKeditorAPI.GetInstance(editorID);
    var drpMerge = $get(drpID);
    var mergeField = drpMerge.options[drpMerge.selectedIndex].value;
    var tag = '<span class="MergeField" title="Merge Field" id="spn' + mergeField + '" onkeydown="SelectTag(this, \'' + editorID + '\');" onclick="SelectTag(this, \'' +  editorID + '\');">' + mergeField + '</span>';

    if(mergeField.length == 0)
    {
        alert('Please select a Merge Field.');
        return false;
    }
    
    oEditor.InsertHtml(tag);
    return false;
}


function SelectTag(obj)
{
   DeSelectTag();
   if (document.selection) 
   {
      var range = document.body.createTextRange();
      range.moveToElementText(obj);
      range.select();
   }
   else if (window.getSelection) 
   {
      var range2 = document.createRange();
      range2.selectNode(obj);
      window.getSelection().addRange(range2);
   }
}
function DeSelectTag() 
{
   if (editor.EditorDocument.selection)
        editor.EditorDocument.selection.empty();
   else if (editor.EditorWindow.getSelection)
        editor.EditorWindow.getSelection().removeAllRanges();
} 

function disableTo(toID, lblToID)
{
    var txtTo=$get(toID); 
    var lblTo=$get(lblToID)
    txtTo.disabled = true;
    lblTo.disabled = true;
}
function RemoveTemplateMergeToolbars(btnID, toID)
{
    var btn=$get(btnID); 
    var txtTo=$get(toID); 
    var isDisabled = txtTo.getAttribute('TemplatesDisabled');
    
    if(txtTo.value.length == 0 || isDisabled=='true')
        return false;
    else if(confirm('Adding email addresses into the To box will disable Merge and Template\nfunctionaility. Continue?'))
    {
        btn.click();        
        return true;
    }
    else
    {
        txtTo.value = '';
        return false;
    }
}


/******* Test *******/
var floater;

function showUsagePopup(baseID, floaterID, topOffSet, leftOffSet) 
{
    if(floater != null)
    {
        floater.style.height = '0px';
        floater.style.width = '0px';
        floater.style.display = 'none'; 
    }
       
    var base = $get(baseID);
    floater = $get(floaterID);
    topoffset = topOffSet;
    leftoffset = leftOffSet;

    var location = Sys.UI.DomElement.getLocation(base);
    floater.style.position = 'absolute';
    floater.style.top = location.y + topOffSet + 'px';
    floater.style.left = location.x + leftOffSet + 'px';
    
    floater.style.display = 'block'; 
}
function hideUsagePopup() 
{
    if(floater != null)
    {
        floater.style.height = '0px';
        floater.style.width = '0px';
        floater.style.display = 'none'; 
    }
}



function validateChange(dropdown, listIndex)
{
    var newIndex  = dropdown.selectedIndex;
    var newList = dropdown.options[newIndex].text;
    var oldList = dropdown.options[listIndex].text;
    
    
    if(confirm('You have changed the "To List" from ' + oldList + ' to ' + newList + '.\nTo continue click OK.'))           
        return true;
    else
    {
        dropdown.selectedIndex = listIndex;
        return false;
    }
}
