/*
	var Constants = {
		INVALID_MSG : "Invalid Value",
		INVALID_URL : "/errors/invalid.php"
	}
	alert(Constants.INVALID_MSG;
		  
	gMake or ANT Build software
*/
ajax_url = 'tech/gateway.inc.php'

//Debugging Utility
function print_r(theObj){
  if(theObj.constructor == Array || theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array|| theObj[p].constructor == Object){
		document.write("<li>["+p+"] => "+typeof(theObj)+"</li>")
        document.write("<ul>")
        print_r(theObj[p])
        document.write("</ul>")
      } else {
		document.write("<li>["+p+"] => "+theObj[p]+"</li>")
      }
    }
    document.write("</ul>")
  }
}	
Array.find = function(ary, element){
    for(var i=0; i<ary.length; i++){
        if(ary[i] == element){
            return i;
        }
    }
    return -1;
}
function addslashes(str) {
	str = str.replace(/\'/g,'\\\'');
	return str;
}
function IsNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

//Toggle Open / Close any Nav Item
function Open (that) {
	if (that.style.display == 'none') {
		that.style.display = 'block'
	} else {
		that.style.display = 'none'
	}
}



//Create User List
	//Used on FindFriends.php and MyFriends.php
function generateUserMngr (users, current_id, from) {
	userMngr = new Object ()
	for (var i in users) {
		userMngr[i] = new createDspUser (users[i], current_id, from)
		document.getElementById('FeedContainer').appendChild(userMngr[i])
	}
}
	//Assemple a user object for viewing a users information
	function createDspUser (user, current_id, from) {
		this.user = new div('', 'UserContainer', '', '','')
		this.user.id 			= 	user.id
		this.user.firstname 	= 	user.firstname
		this.user.lastname 		= 	user.lastname
		this.user.registered 	= 	user.registered
		this.user.lastlogon 	= 	user.lastlogon
		this.user.groups 		= 	user.groups
		this.user.mf 			= 	user.mf
		this.user.icon 			= 	user.icon
		this.user.type 			= 	user.type
		
		if (this.user.type == 'friend' && from != 'MyFriends') {
			this.user.className = 'UserContainerFriend'
		}
		
		this.table1 = new table('','','','')
		this.tr1 = new tr()
		this.td1 = new td('top','left','UserIcon','','80px')		
		this.table1.appendChild(this.tr1)
		this.user.appendChild(this.table1)
		this.tr1.appendChild(this.td1)
		this.icon = new img('media/img/usericons/' + user.icon, '70px', '', '1')
		this.icon.setAttribute('style', 'padding: 3px;')
		this.td1.appendChild(this.icon)
		this.td2 = new td('top','left','UserInfo','', '70px')
		this.td2.setAttribute('style', 'padding-top: 10px;')
		this.tr1.appendChild(this.td2)
		this.td3 = new td('top', 'left', 'Info', '')
		this.td3.setAttribute('style', 'padding-top: 10px;')
		this.tr1.appendChild(this.td3)
		
		this.td2.innerHTML += '<br /><span class="smTitle">Last On:</span>'
		this.td2.innerHTML += '<br /><span class="smTitle">Registered:</span>'
		
		this.td3.innerHTML += '<span class="Text"><a href="UserProfile.php?uid=' + user.id + '" style="text-decoration: underline;">' + user.firstname + ' ' + user.lastname + '</a></span>'
		this.td3.innerHTML += '<br /><span class="SmText">' + user.lastlogon + '</span>'	
		this.td3.innerHTML += '<br /><span class="SmText">' + user.registered + '</span>'
	
		
		this.td4 = new td('top', 'left', 'Optiopns', '')
		this.td4.setAttribute('width', '100px')
		this.tr1.appendChild(this.td4)
		this.td4.innerHTML  = '<a href="DraftMail.php?to_id=uid-' + user.id + '">Send Message</a>'
			
		if (user.type == 'friend') {
			this.td4.innerHTML += '<br /><a href="#" onClick="RemoveFriend(this,' + this.user.id + ', \'' + user.firstname + ' ' + user.lastname + '\');return false;" >Remove Friend</a>'
		} else {
			if (user.friendstatus == 'initiated') {
				this.td4.innerHTML += '<br />Requested'
			} else {
				this.td4.innerHTML += '<br /><span id="' + user.id + '-FriendStatus"><a href="#" onClick="AddFriend(this,\'' + user.id + '\', \'' + user.id + '-FriendStatus\');return false;" >Add Friend</a></span>'
			}
		}
			
		return this.user
	}

/*
	Remove Friendship
		Called: By an anchor tag usually with the contents 'Remove Friend'
		Objective: When the user is viewing other users mini-profile or their lg-profile, if the user is their friend they have the option of removing the user as a friend
		PRE: Expects the item calling the request, the friends uname and id to remove
		POST: Calls Remove_Friend PHP Functions via the gateway, using RemoveFriendResponse() as the callback
*/
function RemoveFriend (that, uid, friend_name) {
	var confirmRemove = confirm('Are you sure you wish to remove ' + friend_name + '?')
	if (confirmRemove) {
		var action 		= '?action=Remove_Friend'
			uid 		= '&uid=' + uid
		var that_id		= '&thatid=' + that.id
		var callback	= '&callback=RemoveFriendResponse'
		
		var get_vars 	= action 
		var post_vars	= uid + that_id + callback
		makePOSTRequest(ajax_url + get_vars, post_vars);
	}
}
	/*
		Remove Friend Response
			Called: Upon completion of PHP removing a users friend, usually summoned by the RemoveFriend Javascript Function
			PRE: Excpects a str response from from the PHP function Remove_Friend, and the users id to remove from the UI
			POST: Remove the users display from the current feed (Usually done within FindFriends.php or MyFriends.php)
	*/
	function RemoveFriendResponse (response, id, that_id) {
		
		
		if (that_id == 'UserProfileRem') {
			document.getElementById('FriendControl').innerHTML = "<a href=\"#\" onClick=\"AddFriend(this,'" + id + "', '" + id + "-FriendStatus');return false;\" >Add Friend</a>"
		} else {
			var FeedContainer = document.getElementById('FeedContainer')
			FeedContainer.removeChild(document.getElementById(id))
			
			alert(FeedContainer.innerHTML)
			
			if (FeedContainer.innerHTML == '') {
				FeedContainer.innerHTML = 'You don\'t have any friends yet, <a href="FindFriends.php">Find Some</a>.'
			}
		}
	}

/*
	Join Group
		PRE: Excpects the reference which called, and the groupname to join
		POST: Gateway summons JoinGroup_Callback to display a success dialog
*/
function JoinGroup (group_id, from) {
			var action 		= 'Join_Group'
			gid 			= '&gid=' + group_id
			from 			= '&from=' + from
			var callback 	= '&callback=JoinGroup_Callback'
			var get_vars 	= '?action=' + action + gid + from + callback
			var url = ajax_url + get_vars
			makePOSTRequest(url)
		}
	function JoinGroup_Callback (res, gid, from) {
		if (res == 'Success!') {
			if (from == 'JoinGroups') {
				for (var i in GroupMngr) {
					if (GroupMngr[i].id == gid) {
						GroupMngr[i].className = 'GroupContainerUserIn'
					}
				}			
			}
			if (from == 'GroupProfile') {
				location.reload(true)
				//TBD
				//document.getElementById('GroupControl').innerHTML = '<a href="#" onClick="RemoveGroup(\'' + groupname + '\', \'GroupProfile\')">Remove Group</a>'
			}
			if (from == 'Index') {
				window.location = 'Group.php?gid=' + gid
			}
		} else {
			alert(res)
		}
	}
	
/*
	Remove Group
		PRE: Expects the name of the group which you want to remove
		POST: Gateway summons RemoveGroupCleanup upon completion of remove the group to display any errors
*/
function RemoveGroup (gid, group_name, caller) {
	if (confirm('Are you sure you wish to remove the group ' + group_name + '?')) {		
		var action 		= 'Remove_Group'
			gid 		= '&gid=' + gid
			group_name	= '&group_name=' + group_name
			caller 		= '&caller=' + caller
		var callback 	= '&callback=RemoveGroupCleanup'
		var post_vars 	=  gid + group_name + caller + callback
		get_vars = '?action=' + action
		makePOSTRequest(ajax_url + get_vars, post_vars)
	}
}
//Cleanup from removing the group
//RemoveGroupCleanup('Error', 'You must instead select a new group owner.', 'MyGroups', 0, '6', 'Scripting and Such');
//RemoveGroupCleanup('Success', '', 'ViewingGroup', 1, '14', 'World of Warcraft');
function RemoveGroupCleanup (status, error, caller, redirect, gid, group_name) {
	if (status == 'Success') {
		var groupLink = document.getElementById('ViewGroupLink-' + gid)
		var groupsLinkList = document.getElementById('MyGroupsContent')
		groupsLinkList.removeChild(groupLink)
		if (redirect == 1) {
			window.location = 'MyGroups.php'
		}
	} else {
		alert(error)
	}
}
