/***************************************************************************
 *                            Dolphin Web Community Software
 *                              -------------------
 *     begin                : Mon Mar 23 2006
 *     copyright            : (C) 2006 BoonEx Group
 *     website              : http://www.boonex.com
 *
 *     
 *
 ****************************************************************************/

/***************************************************************************
 *
 *   This is a free software; you can modify it under the terms of BoonEx 
 *   Product License Agreement published on BoonEx site at http://www.boonex.com/downloads/license.pdf
 *   You may not however distribute it for free or/and a fee. 
 *   This notice may not be removed from the source code. You may not also remove any other visible 
 *   reference and links to BoonEx Group as provided in source code.
 *
 ***************************************************************************/

/**
 * Checks/unchecks all tables
 *
 * @param   string   the form name
 * @param   boolean  whether to check or to uncheck the element
 *
 * @return  boolean  always true
 */
function setcookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

var xmlHttp // xmlHttp variable

function GetXmlHttpObject(){ // This function we will use to call our xmlhttpobject.
var objXMLHttp=null // Sets objXMLHttp to null as default.
if (window.XMLHttpRequest){ // If we are using Netscape or any other browser than IE lets use xmlhttp.
objXMLHttp=new XMLHttpRequest() // Creates a xmlhttp request.
}else if (window.ActiveXObject){ // ElseIf we are using IE lets use Active X.
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") // Creates a new Active X Object.
} // End ElseIf.
return objXMLHttp // Returns the xhttp object.
}// Close Function

function CheckUsername()
{
setcookie("username","",0);
setcookie("NickName","",0);
username=document.getElementById("NickName").value; // This function we will use to check to see if a username is taken or not.
xmlHttp=GetXmlHttpObject() // Creates a new Xmlhttp object.
if (xmlHttp==null){ // If it cannot create a new Xmlhttp object.
alert ("Browser does not support HTTP Request") // Alert Them!
return // Returns.
} // End If.

var url="checkname.php?username="+username // Url that we will use to check the username.
xmlHttp.open("GET",url,true) // Opens the URL using GET
xmlHttp.onreadystatechange = function () { // This is the most important piece of the puzzle, if onreadystatechange is equal to 4 than that means the request is done.
if (xmlHttp.readyState == 4) { // If the onreadystatechange is equal to 4 lets show the response text.
document.getElementById("usernameresult").innerHTML="";
document.getElementById("usernameresult").innerHTML = xmlHttp.responseText; // Updates the div with the response text from check.php
} // End If.
}; // Close Function
xmlHttp.send(null); // Sends NULL instead of sending data.
} // Close Function.

function launchChangePass (id_prof)
{   
    var win = "width=300,height=220,left=200,top=100,copyhistory=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=no";
    window.open("changepass.php?ID="+id_prof,'ChangePass',win);
    return false;
}
function setCheckboxes(the_form, do_check)
{

	var elts  = document.forms[the_form].getElementsByTagName('input');
    var elts_cnt  = elts.length;
    var i = 0;
	
    for ( i; i < elts_cnt; i++)
    {
        elts[i].checked = do_check;
		if (the_form + "_submit" == elts[i].name)
		{
			elts[i].disabled = !do_check;
		}
    } // end for
	return true;
} // end of the 'setCheckboxes()' function

function setCheckbox(the_form)
{
    var elts      = document.forms[the_form].getElementsByTagName('input');
    var elts_cnt  = elts.length;
    
    var allUnchecked = true;
	
    for (var i = 0; i < elts_cnt; i++)
    {
        if(elts[i].checked) allUnchecked = false;
    }
    
    for (var i = 0; i < elts_cnt; i++)
    {
        if(elts[i].name == (the_form + "_submit")) elts[i].disabled = allUnchecked;
    }

    return true;
}


var win = "width=500,height=600,left=100,top=100,copyhistory=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=yes";
function get_gallery(id_prof)
{
   window.open("photos_gallery.php?ID="+id_prof,'gallery',win);
}

function launchTellFriend ()
{   
    var win = "width=300,height=300,left=200,top=100,copyhistory=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=no";
    window.open("tellfriend.php",'tellfriend',win);
    return false;
}

function launchTellFriendProfile ( sID )
{
    var win = "width=300,height=300,left=200,top=100,copyhistory=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=no";
    window.open("tellfriend.php?ID="+sID,'tellfriendprofile',win);
    return false;
}

function ShowShowHide ( show_name, show_name2, hide_name )
{
    if (hide_name) hide_name.style.display = 'none';
    if (show_name) show_name.style.display = 'inline';
    if (show_name2) show_name2.style.display = 'inline';
}

function ShowHideHide ( show_name, hide_name, hide_name2 )
{
    if (hide_name) hide_name.style.display = 'none';
    if (hide_name2) hide_name2.style.display = 'none';
    if (show_name) show_name.style.display = 'inline';
}

function charCounter(field,maxLength,countTarget)
{

	field = document.getElementById(field);
	countTarget = document.getElementById(countTarget);
	var inputLength=field.value.length;

	if(inputLength >= maxLength)
	{
		field.value=field.value.substring(0,maxLength);

	}
	countTarget.innerHTML=maxLength-field.value.length;
	
	
}



/**
 * change images onHover mouse action
 */
function show(FileName,jpg1Name)
{
	document.images[FileName].src = jpg1Name;
}

/**
 * set status of the browser window to 's'
 */
function ss(s) 
{
	window.status = s;
	return true;
}

/**
 * set status of the browser window to empty
 */
function ce()
{
	window.status='';
}


/**
 * insert emotion item
 */
function emoticon( txtarea, text ) {

	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}

function launchAddToIM (id)
{
    var win = "width=600,height=160,left=100,top=100,copyhistory=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=yes";
    window.open("explanation.php?explain=imadd&ID="+id,'add_to_im',win);
    return false;
}

function docOpen(text)
{
	newWindow=window.open('','','toolbar=no,resizable=yes,scrollbars=yes,width=400,height=300');
	newWindow.document.open("text/html");
	newWindow.document.write(unescape(text));
	newWindow.document.close();
}

function get_data( container, url, siteUrl )
    {

        if ( container )
        {
	    var container = document.getElementById( container );
	    container.innerHTML = "loading ... ";
        }

        var XMLHttpRequestObject = false;

        if ( window.XMLHttpRequest )
        {
                XMLHttpRequestObject = new XMLHttpRequest();
        }
        else if ( window.ActiveXObject )
        {
                XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
        }

        
	if( XMLHttpRequestObject )
        {
    	    var data_source = siteUrl + 'menu_xml.php' + url;
    	    XMLHttpRequestObject.open( "GET", data_source );
    	    XMLHttpRequestObject.onreadystatechange = function()
    	    {
        	if ( XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200 )
        	{

        	var xmlDocument = XMLHttpRequestObject.responseXML;
            delete XMLHttpRequestObject;
            XMLHttpRequestObject = null;
		    
		   	names = xmlDocument.getElementsByTagName("name");
		    links = xmlDocument.getElementsByTagName("link");
		    
		    list_sublinks(names, links);

        	}
    	    }
    
            XMLHttpRequestObject.send( null );

	}
	
	//container.innerHTML = '';
	
	
	
	
	function list_sublinks(names, links)
	{	    
    	var loopIndex, name, link, maxIndex = names.length;
    
	    container.innerHTML = '';
	    for ( loopIndex = 0; loopIndex < maxIndex; loopIndex++ )
   	    {
	    
		    //if ( values[loopIndex].firstChild.nodeName=="name")
			name = names[loopIndex].firstChild.nodeValue;
		    
		    //if ( values[loopIndex].childNodes[1].nodeName=="link")
			link = links[loopIndex].firstChild.nodeValue;
					    
		    container.innerHTML += '<div class="innerSubmenuDiv"><a href="' + link + '">' + name + '</a></div>';
	    }
	
	}
}


function createNamedElement( type, name )
{
	
    var element;
		
    try
    {
        element = document.createElement('<'+type+' name="'+name+'">');
    } catch (e) { }

    if (!element || !element.name) // Cool, this is not IE !!
    {
        element = document.createElement(type)
        element.name = name;
    }
								
    return element;
}
									
function display_node(node, siteUrl)
{

    var nn = document.getElementById( node );
    
	var sub_name = node.split('_')[1];
    if ( 'none' == nn.style.display )
    {
	nn.style.display='block';
	if ( '' == nn.innerHTML )    
	    get_data( node, '?action=menu&ID=' + sub_name, siteUrl);
    }
    else
    {
		nn.style.display='none';
    }
    
}
function getPositionData(obj, showEvent)
	{
		if ( !showEvent )
			showEvent = window.event;
		
		var pos_X = 0, pos_Y = 0;
		if ( showEvent )
		{
			if ( typeof(showEvent.pageX) == 'number' )
			{
				pos_X = showEvent.pageX; pos_Y = showEvent.pageY;
			}
			else if ( typeof(showEvent.clientX) == 'number' )
			{
				pos_X = showEvent.clientX; pos_Y = showEvent.clientY;
				if ( document.body && ( document.body.scrollTop || document.body.scrollLeft ) && !( window.opera || window.debug || navigator.vendor == 'KDE' ) )
				{
					pos_X += document.body.scrollLeft; pos_Y += document.body.scrollTop;
				}
				else if ( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) && !( window.opera || window.debug || navigator.vendor == 'KDE' ) )
				{
					pos_X += document.documentElement.scrollLeft; pos_Y += document.documentElement.scrollTop;
				}
			}
		}
		
		var scroll_X = 0, scroll_Y = 0;
		if ( document.body && ( document.body.scrollTop || document.body.scrollLeft ) && !( window.debug || navigator.vendor == 'KDE' ) )
		{
			scroll_X = document.body.scrollLeft; scroll_Y = document.body.scrollTop;
		}
		else if ( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) && !( window.debug || navigator.vendor == 'KDE' ) )
		{
			scroll_X = document.documentElement.scrollLeft; scroll_Y = document.documentElement.scrollTop;
		}
		
		var win_size_X = 0, win_size_Y = 0;
		if (window.innerWidth && window.innerHeight)
		{
			win_size_X = window.innerWidth; win_size_Y = window.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientWidth && document.documentElement.clientHeight)
		{
			win_size_X = document.documentElement.clientWidth; win_size_Y = document.documentElement.clientHeight;
		}
		else if (document.body && document.body.clientWidth && document.body.clientHeight)
		{
			win_size_X = document.body.clientWidth; win_size_Y = document.body.clientHeight;
		}
		
		pos_X += 15; pos_Y += 15;
		
		if (obj.offsetWidth && obj.offsetHeight)
		{
			if (pos_X - scroll_X + obj.offsetWidth + 5 > win_size_X)
				pos_X -= (obj.offsetWidth + 25);
			if (pos_Y - scroll_Y + obj.offsetHeight + 5 > win_size_Y)
				pos_Y -= (obj.offsetHeight + 20);
		}
		
		var res = new Array;
		res['posX'] = pos_X;
		res['posY'] = pos_Y;
		res['scrollX'] = scroll_X;
		res['scrollY'] = scroll_Y;
		res['winSizeX'] = win_size_X;
		res['winSizeY'] = win_size_Y;
		
		return res;
	}
	
function showUserDetails(showEvent, tDiv, content)
	{
		if ( !showEvent )
			showEvent = window.event;
		showDiv = document.getElementById(tDiv);
		
		showPos = getPositionData(showDiv, showEvent);
		
		showDiv.style.left = showPos['posX'] + 'px'; showDiv.style.top = showPos['posY'] + 'px';
		showDiv.innerHTML = content;
		showDiv.style.display = 'block';
	}
	
	function hideUserDetails(tDiv)
	{
		showDiv = document.getElementById(tDiv);
		showDiv.style.display = 'none';
	}

//######################################################################
/*
	var NicknameInput_ID = 'Login_NickName';
	var NicknameInput_isEmpty = true;
	var NicknameInput_emptyBackground;
	var NicknameInput_nonEmptyBackground;
*/
	/*
	var NicknameInput_emptyColor = 'rgb(170, 170, 170)';
	var NicknameInput_nonEmptyColor = 'rgb(0, 0, 0)';
	var NicknameInput_emptyText = 'Nickname';
	*/
/*
	var PasswordInput_ID = 'Login_Password';
	var PasswordInput_isEmpty = true;
	var PasswordInput_emptyBackground;
	var PasswordInput_nonEmptyBackground;
*/
	/*
	var PasswordInput_emptyColor = 'rgb(170, 170, 170)';
	var PasswordInput_nonEmptyColor = 'rgb(0, 0, 0)';
	var PasswordInput_emptyText = 'Password';
	var PasswordInput_emptyType = 'text';
	var PasswordInput_nonEmptyType = 'password';
	*/
/*
	function NicknameInput_init(sEmptyBackground, sNonEmptyBackground)
	{
	    var elt = document.getElementById(NicknameInput_ID);
	    
	    NicknameInput_emptyBackground = 'url('+sEmptyBackground+')';
	    NicknameInput_nonEmptyBackground = 'url('+sNonEmptyBackground+')';
	
	    elt.style.backgroundImage = NicknameInput_emptyBackground;
	}
	
	function PasswordInput_init(sEmptyBackground, sNonEmptyBackground)
	{
	    var elt = document.getElementById(PasswordInput_ID);
	    
	    PasswordInput_emptyBackground = 'url('+sEmptyBackground+')';
	    PasswordInput_nonEmptyBackground = 'url('+sNonEmptyBackground+')';

	    elt.style.backgroundImage = PasswordInput_emptyBackground;
	}
	
	function NicknameInput_onfocus()
	{
	    var elt = document.getElementById(NicknameInput_ID);
	
	    if (NicknameInput_isEmpty)
	    {
	        elt.style.backgroundImage = NicknameInput_nonEmptyBackground;
	    }
	
	    return true;
	}
	
	function PasswordInput_onfocus()
	{
	    var elt = document.getElementById(PasswordInput_ID);
	
	    if (PasswordInput_isEmpty)
	    {
	        elt.style.backgroundImage = PasswordInput_nonEmptyBackground;
	    }
	
	    return true;
	}
	
	function NicknameInput_onblur()
	{
	    var elt = document.getElementById(NicknameInput_ID);
	
	    if (elt.value == '')
	    {
	        elt.style.backgroundImage = NicknameInput_emptyBackground;
	        NicknameInput_isEmpty = true;
	    } else {
	        NicknameInput_isEmpty = false;
	    }
	
	    return true;
	}
	
	function PasswordInput_onblur()
	{
	    var elt = document.getElementById(PasswordInput_ID);
	
	    if (elt.value == '')
	    {
	        elt.style.backgroundImage = PasswordInput_emptyBackground;
	        PasswordInput_isEmpty = true;
	    } else {
	        PasswordInput_isEmpty = false;
	    }
	
	    return true;
	}
*/
function displayInfoList(sAction, sDivName, sSiteUrl) {
        var divInfoList = document.getElementById(sDivName);
        divInfoList.innerHTML = "loading...";

        if(divInfoList.style.display == "none") {
                divInfoList.style.display = "block";
                getInfoList(sAction, sDivName, sSiteUrl);
        }
        else divInfoList.style.display = "none";
}

function getInfoList(sAction, sDivName, sSiteUrl) {
        var divInfoList = document.getElementById(sDivName);

        var rRequest = false;
        if ( window.XMLHttpRequest ) {
                        rRequest = new XMLHttpRequest();
        }
        else if ( window.ActiveXObject ) {
                        rRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if(rRequest) {
                rRequest.open( "POST", sSiteUrl + "info_list_xml.php" );
                rRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                rRequest.onreadystatechange = function() {
                        if ( rRequest.readyState == 4 && rRequest.status == 200 ) {
                                        var xmlDocument = rRequest.responseXML;
                                        if(xmlDocument.getElementsByTagName("items")[0].attributes.length != 2) {
                                                divInfoList.innerHTML = "";
                                                divInfoList.style.display = "none";
                                                return;
                                        }

                                        var sContent = "";
                                        var iUserId = xmlDocument.getElementsByTagName("items")[0].getAttribute("id");
                                        var sUserPassword = xmlDocument.getElementsByTagName("items")[0].getAttribute("password");
                                        for(var i=0; i<xmlDocument.getElementsByTagName("item").length; i++) {
                                                var aItem = xmlDocument.getElementsByTagName("item")[i];
                                                var sNick = "<a href=\"" + sSiteUrl + "profile.php?ID=" + aItem.getAttribute("id") + "\" target=\"_blank\" style=\"text-decoration:none; " + (aItem.getAttribute("online") == "true" ? "color:#0059ED;" : "color:gray;") + "\">" + aItem.getAttribute("nick") + "</a>";
                                                var sAmount = aItem.attributes.length == 4 ? aItem.getAttribute("amount") : "";
                                                var sMail = "<a href=\"" + sSiteUrl + "compose.php?ID=" + aItem.getAttribute("id") + "\" target=\"_blank\"><img src=\"" + sSiteUrl + "mail.gif\" border=\"0\" /></a>";
                                                var sIm = aItem.getAttribute("online") == "true" ? "<a href=\"javascript:void(0)\" onClick=\"javascript:IMOpen(" + iUserId + ", '" + sUserPassword + "', " +  aItem.getAttribute("id") + ")\"><img src=\""+ sSiteUrl + "im.gif\" border=\"0\"/></a>" : "";
                                             	sContent += "<tr><td style=\"width:80px;\">" + sNick + "</td><td style=\"font-size:9px; font-weight:normal;\">" + sAmount + "</td><td>" + sMail + "</td><td>" + sIm + "</td></tr>";
					}
					divInfoList.innerHTML = "<table>" + sContent + "</table>";
                        }
                }   
                var date = new Date();
		rRequest.send( "action=" + sAction + "&_s=" + date.getTime());
        }
}