//////////////////////////////////////////////////////////////////////////////////
// general popup function
    function popup( url, name, w, h, x, y, scroll, status )
    {   if( ( name == "" ) || ( name == null ) )
        {   var Winname = "ALLOY_POPUP";
        }
        else
        {   var Winname = name;
        }
        if( ( scroll == "" ) || ( scroll == null ) )
        {   var scrollBar = "auto";
        }
        else
        {   var scrollBar = scroll;
        }
        if( ( status == "" ) || ( status == null ) )
        {   var statusBar = "auto";
        }
        else
        {   var statusBar = status;
        }
        window.open( url, Winname, 'resizable=yes,menubar=no,toolbar=no,location=no,status='+statusBar+',scrollbars='+scrollBar+',width='+w+',height='+h+',left='+x+',top='+y );
    }
//////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////
    function launchSendToFriend( earl )
    {   if( ( contextPath == "" ) || ( contextPath == null ) )
        {   contextPath = "";
        }
        if( ( earl == "" ) || ( earl == null ) )
        {   popup( contextPath + "/friends/email/sendto/" + escape( document.location.href ) + "/" + document.title, "FRIEND_POPUP", 425, 500, 100, 100 );
        }
        else
        {   popup( contextPath + "/friends/email/sendto/" + escape( earl ) + "/" + document.title, "FRIEND_POPUP", 425, 500, 100, 100 );
        }
    }
    function launchSendToFriend()
    {   if( ( contextPath == "" ) || ( contextPath == null ) )
        {   contextPath = "";
        }
        popup( contextPath + "/friends/email/sendto/" + escape( document.location.href ) + "/" + document.title, "FRIEND_POPUP", 425, 500, 100, 100 );
    }
    function openInviteFriend()
    {   //popup( contextPath + "/friends/invite/email", "STF", 500, 570, 50, 100, 0, 1 )
        document.location = contextPath + "/friends/invite/email";
    }
    function openerInviteFriend()
    {   //popup( contextPath + "/friends/invite/email", "STF", 500, 570, 50, 100, 0, 1 )
        window.opener.document.location = contextPath + "/friends/invite/email";
    }
    function openInviteContest() {
        //popup( contextPath + "/contests/invite/index.htm", "STF", 560, 570, 50, 100, 0, 1 )
        popup( contextPath + "/contests/invite/index.htm", "STF", 560, 400, 50, 100, 0, 1 )
    }
//////////////////////////////////////////////////////////////////////////////////



function topicLocked( topic_id, is_locked ) {
    new Ajax.Updater( 'topic_status_' + topic_id, '/topic/topicLocked.htm', {
        parameters: { topicId: topic_id, locked: is_locked }
    });
}
function topicMessageApprove( topic_message_id, is_approved ) {
    new Ajax.Updater( 'topic_message_status_' + topic_message_id, '/topic/topicMessageApproval.htm', {
        parameters: { topicMessageId: topic_message_id, approved: is_approved }
    });
}
function topicMessageDelete( topic_message_id ) {
    var answer = confirm( "Are you sure you want to delete this message?" );
    if( answer ){
        new Ajax.Updater( 'topic_message_box_' + topic_message_id, '/topic/topicMessageDelete.htm', {
            parameters: { topicMessageId: topic_message_id }
        });
    }
}



//////////////////////////////////////////////////////////////////////////////////
// ajax form validation
function combineDob()
{   var dobMonth = $F('dobMonth');
    var dobDay = $F('dobDay');
    var dobYear = $F('dobYear');
    if( ( dobMonth != null && dobMonth != "" ) &&
        ( dobDay != null && dobDay != "" ) &&
        ( dobYear != null && dobYear != "" ) )
    {   $('birthDate').value = dobMonth + "/" + dobDay + "/" + dobYear;
    }
}
function combinePhone()
{   var areaCode = $F('areaCode');
    var first3Digits = $F('first3Digits');
    var last4Digits = $F('last4Digits');
    if( ( areaCode != null && areaCode != "" ) &&
        ( first3Digits != null && first3Digits != "" ) &&
        ( last4Digits != null && last4Digits != "" ) )
    {   $('phoneNumber').value = areaCode + "-" + first3Digits + last4Digits;
    }
}
function setUsernameAsEmail()
{   $('username').value = $F('email');
}
function ajaxValidate( theForm )
{   combineDob();
    combinePhone();
    setUsernameAsEmail();
    var elements = Form.getElements( theForm );
    for( i = 0; i < elements.length; i++ )
    {   if( elements[i].name != "" )
        {   if( $( elements[i].name + "Error" ) )
            {   $( elements[i].name + "Error" ).innerHTML = "";
            }
        }
    }
    $( 'ajaxForm' ).value = "true";
    formString = Form.serialize( theForm );
    Form.disable( theForm );
    var myAjax = new Ajax.Updater(
        { success: 'validationHolder' },
        theForm.action,
        { method: 'post',
        parameters: formString,
        onFailure: reportError,
        evalScripts: true
        }
    );
}


var myGlobalHandlers = {
    onCreate: function(){
        Element.show( 'systemWorking' );
    },
    onComplete: function() {
        if( Ajax.activeRequestCount == 0 ){
            Element.hide( 'systemWorking' );
        }
    }
};
Ajax.Responders.register( myGlobalHandlers );

function showResponse( request )
{   //put returned XML in the textarea
    if( request.readyState == 4 )
    {   if( request.status == 200 )
        {   eval( request.responseText );
        }
        else
        {   alert( "There was a problem validating this form:\n" + request.statusText );
        }
    }
    //$('validationHolder').value = request.responseText;
}

function reportError( request )
{   alert( "ERROR: There was a problem validating this form:\n" + request.statusText );
}

function reportFilterError( request )
{   alert( "ERROR: There was a problem filtering:\n" + request.statusText );
}

function reportRateError( request )
{   alert( "ERROR: There was a problem rating this group:\n" + request.statusText );
}


function swapImage( imageId, newSrc )
{   $( imageId ).src = newSrc;
}
//////////////////////////////////////////////////////////////////////////////////

