var sessionId = null;

function removeSection( groupId, sessionId, sectionId ) {
    if ( confirm( "Click OK to remove this section from your group.  You can add it back by using the 'Set Options' function." )) 
    {
        document.getElementById( sectionId ).style.display = "none";
        
        var ajax = new Ajax();
        ajax.post( SYSTEM_URL + "/groups/edit_group.php", null, [
            ["type", "display"],
            ["groupId", encodeURI( groupId )],
            ["sessionId", encodeURI( sessionId )], 
            ["sectionId", encodeURI( sectionId )]
        ] );
    }
}


function showBulletins( groupId, memberId, bulletinStart ) {
    var ajax = new Ajax();
    ajax.post2html( SYSTEM_URL + "/groups/edit_bulletin.php", "bulletin_list", [
        ["action", "show"],
        ["groupId", encodeURI( groupId )], 
        ["memberId", encodeURI( memberId )], 
        ["start", encodeURI( bulletinStart )] 
    ] );
}

function deleteBulletin( groupId, memberId, bulletinId, bulletinStart ) {
    if ( confirm( "Click OK to permanently delete this bulletin message." )) {
        var ajax = new Ajax();
        ajax.post2html( SYSTEM_URL + "/groups/edit_bulletin.php", "bulletin_list", [
            ["action", "delete"],
            ["groupId", encodeURI( groupId )], 
            ["memberId", encodeURI( memberId )], 
            ["bulletinId", encodeURI( bulletinId )],
            ["start", encodeURI( bulletinStart )]  
        ] );
    }
}


function openBulletin( groupId, memberId, bulletinId, b_no, bulletinStart ) 
{
        var ajax = new Ajax();
        ajax.post2html( SYSTEM_URL + "/groups/edit_bulletin.php", "bulletin_list", [
            ["action", "open_list"],
            ["groupId", encodeURI( groupId )], 
            ["memberId", encodeURI( memberId )],    
            ["bulletinId", encodeURI( bulletinId )],
            ["b_no", encodeURI( b_no )],
            ["start", encodeURI( bulletinStart )] 
        ] );
}

function writeBulletin( groupId, memberId ) { 
    var form = document.getElementById( "bulletin_form" );
    if ( form.bulletin_text.value.length == 0 ) {
        alert( "No text was entered in the bulletin." );
        form.bulletin_text.focus();
        return false;
    }
    
    if(form.se_check.checked)
    {
        var se_go = "send";
    }
    else
    {
        var se_go = "no";
    }
    
    var ajax = new Ajax();
    ajax.post2html( SYSTEM_URL + "/groups/edit_bulletin.php", "bulletin_list", [
        ["action", "add"],
        ["groupId", encodeURI( groupId )], 
        ["memberId", encodeURI( memberId )], 
        ["text", encodeURI( form.bulletin_text.value )],
        ["se_go", encodeURI( se_go )] 
    ] );
    form.submit.className = "small_btn_process";
    form.submit.disabled = true;
    return false;
}

function gchatCheckBrowser( groupId, memberId, browser_version, session_id ) 
{   
    var ajax = new Ajax();
    ajax.post2html( SYSTEM_URL + "/groups/edit_group.php", "chatbox", [
        ["br_mode", "check"],
        ["groupId", encodeURI( groupId )], 
        ["memberId", encodeURI( memberId )], 
        ["b_version", encodeURI( browser_version )],  
        ["sessionId", encodeURI( session_id )]  
    ] );
    
}


function selectProfileTab( id ) {
    var network = document.getElementById( "network" );
    var groups = document.getElementById( "groups" );
    if ( id == "network" ) {
        network.style.display = "";
        groups.style.display = "none";
    } else if ( id == "groups" ) {
        network.style.display = "none";
        groups.style.display = "";
    }
}

function selectContactCountry( countryCode ) {
    if ( countryCode == "US" ) {
        document.getElementById( "edit_postal_code" ).innerHTML = "ZIP Code";
    } else {
        document.getElementById( "edit_postal_code" ).innerHTML = "Postal Code";
    }
    selectCountry( countryCode, "edit" );
}

function selectCountry( countryCode, prefix ) {
    if ( countryCode == "US" ) {
        document.getElementById( prefix + "_state" ).style.display = "";
        document.getElementById( prefix + "_region" ).style.display = "none";
    } else {
        document.getElementById( prefix + "_state" ).style.display = "none";
        document.getElementById( prefix + "_region" ).style.display = "";
    }
}

function setPublic() {
    var open = document.getElementById( "openaccess" );
    if ( document.getElementById( "pubaccess" ).checked ) {
        open.checked = false;
        open.disabled = false;
    } else {
        open.checked = true;
        open.disabled = true;
    }
}

function toggleDialog( id ) {
    var back = document.getElementById( "blur" );
    var item = document.getElementById( id );
    if ( item.style.display == "none" ) {
        item.style.display = "";
        item.style.left = ( document.body.clientWidth - item.offsetWidth ) / 2;
        item.style.top = ( document.body.clientHeight - item.offsetHeight ) / 2 + document.body.scrollTop;
        back.style.zIndex = 1;
        back.style.width = 940;
        back.style.height = screen.height + document.body.scrollTop;
    } else {
        back.style.width = 1;
        back.style.height = 1;
        item.style.display = "none";
        back.style.zIndex = -1;
    }
}

function toggleSection( id, link ) {
    var item = document.getElementById( id );
    if ( item.style.display == "none" ) {
        item.style.display = "";
        link.innerHTML = "-";
    } else {
        item.style.display = "none";
        link.innerHTML = "+";
    }
}

function updateBasic( groupId, sessionId ) 
{    
	var form = document.forms["basic_form"];
	if ( form.gr_name.value == "" ) {
		alert( "Invalid Group Name" );
		form.gr_name.focus();
		return;
	}
	if ( form.gr_website.value == "" ) {
		alert( "Invalid Website." );
		form.gr_website.focus();
		return;
	}
     
    xmlHttp = GetXmlHttpObject()
    if ( xmlHttp == null ) {
        alert ( "Browser does not support HTTP Request" );
        return;
    }

     
	send( SYSTEM_URL + "/groups/edit_group.php", basicStateChanged, "type=basic&groupId=" + encodeURI( groupId ) + "&sessionId=" + encodeURI( sessionId ) + "&gr_name=" + encodeURI( form.gr_name.value ) + "&gr_cate=" + encodeURI( form.gr_category.value ) + "&gr_website=" + encodeURI( form.gr_website.value )); 
}

function updateAbout( groupId, sessionId ) 
{    
    var form = document.forms["about_form"];
    if ( form.description.value == "" ) {
        alert( "Invalid Description." );
        form.description.focus();
        return;
    }
    if ( form.recentnews.value == "" ) {
        alert( "Invalid Recent News." );
        form.recentnews.focus();
        return;
    }
 
    xmlHttp = GetXmlHttpObject()
    if ( xmlHttp == null ) {
        alert ( "Browser does not support HTTP Request" );
        return;
    }


send( SYSTEM_URL + "/groups/edit_group.php", aboutStateChanged, "type=g_about&groupId=" + encodeURI( groupId ) + "&sessionId=" + encodeURI( sessionId ) + "&gr_description=" + encodeURI( form.description.value ) + "&gr_recentnews=" + encodeURI( form.recentnews.value ));
}


function send( url, callback, params ) 
{
	xmlHttp.onreadystatechange = callback;
	xmlHttp.open( "POST", url, true );
	xmlHttp.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
	xmlHttp.setRequestHeader( "Content-length", params.length );
	xmlHttp.setRequestHeader( "Connection", "close" );
	xmlHttp.send( params );
}


function basicStateChanged() { 
     
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" ) { 
        document.getElementById( "basic_info" ).innerHTML = xmlHttp.responseText;
		// todo update experience
	} 
}

function aboutStateChanged() { 
    if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" ) { 
        document.getElementById( "about" ).innerHTML = xmlHttp.responseText;
    } 
}

function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } catch ( e ) {
        //Internet Explorer
        try {
            xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" );
        } catch ( e ) {
            xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
        }
    }
    return xmlHttp;
}

function toggleProfile( id ) {
    var item = document.getElementById( id );
    if ( item.style.display == "none" ) {
        item.style.display = "";
    } else {
        item.style.display = "none";
    }
}

function toggleSection_peoplebrowser( id, id2, link, sessionId ) 
{
    var ajax = new Ajax();
       
    var item = document.getElementById( id );
    var item2 = document.getElementById( id2 );
    
    if ( item.style.display == "none" && item2.style.display == "none") 
    {                                           
        ajax.post( SYSTEM_URL + "/groups/browser_session.php", null, [ ["sessionId", sessionId], ["p_command", "open"], ["b_cate", "people"] ] ); 
        
        item.style.display = "";
        item2.style.display = "";
        link.innerHTML = "-";
    } 
    else 
    {
        ajax.post( SYSTEM_URL + "/groups/browser_session.php", null, [ ["sessionId", sessionId], ["p_command", "close"], ["b_cate", "people"] ] ); 
           
        item.style.display = "none";
        item2.style.display = "none";
        link.innerHTML = "+";
    }
}

function toggleSection_groupbrowser( id, id2, link, sessionId ) 
{
    var ajax = new Ajax();
       
    var item = document.getElementById( id );
    var item2 = document.getElementById( id2 );
    
    if ( item.style.display == "none" ) 
    {                                           
        ajax.post( SYSTEM_URL + "/groups/browser_session.php", null, [ ["sessionId", sessionId], ["g_command", "open"], ["b_cate", "group"] ] ); 
        
        item.style.display = "";
        item2.style.display = "";
        
        link.innerHTML = "-";
    } 
    else 
    {
        ajax.post( SYSTEM_URL + "/groups/browser_session.php", null, [ ["sessionId", sessionId], ["g_command", "close"], ["b_cate", "group"] ] ); 
           
        item.style.display = "none";
        item2.style.display = "none";
        
        link.innerHTML = "+";
    }
}
