function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
";path=/" +
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

function mylinksAdd (parentLink) {
var url = window.location;
//alert("Adding: " + url);
if(document.getElementById('mylinksAddPopup') == undefined){
var newdiv = document.createElement('div');
   newdiv.setAttribute('id', 'mylinksAddPopup');
//newdiv.style.width = 400;
//newdiv.style.height = 80;
newdiv.style.padding = "0.25em 1em";
newdiv.style.background = "#F4F4F4";
newdiv.style.border = "1px solid #999999";
newdiv.innerHTML = "Adding: " + url + "<br>Link Name: <input type=\"text\" onchange=\"mylinksClose('mylinksAddPopup',true);\"><br><input type=\"button\" value=\"Add MyLink\" onclick=\"mylinksClose('mylinksAddPopup',true);\"> <input type=\"button\" value=\"Close\" onclick=\"mylinksClose('mylinksAddPopup',false);\">";
newdiv.style.position = "absolute";
newdiv.style.left = (findPosX(parentLink)+4)+"px";
newdiv.style.top = (findPosY(parentLink)+12)+"px";

document.body.appendChild(newdiv);
document.getElementById('mylinksAddPopup').getElementsByTagName('input')[0].focus();
}

}

function mylinksDel (recordID) {
updateMyLinks(Base64.encode(recordID),"delete");
}

function mylinksOrder (recordID,direction) {
updateMyLinks(Base64.encode(recordID),direction);
}

function mylinksRemove(parentLink){
if(document.getElementById('mylinksAddPopup') == undefined){
var newdiv = document.createElement('div');
newdiv.setAttribute('id', 'mylinksAddPopup');
//newdiv.style.width = 400;
//newdiv.style.height = 80;
newdiv.style.padding = "0.25em 1em";
newdiv.style.background = "#F4F4F4";
newdiv.style.border = "1px solid #999999";
newdiv.innerHTML = "Are you sure you want to remove this link?<br><input type=\"button\" value=\"Yes\" onclick=\"mylinksRemoveConfirm();\"> <input type=\"button\" value=\"No\" onclick=\"mylinksClose('mylinksAddPopup',false);\">";
newdiv.style.position = "absolute";
newdiv.style.left = (findPosX(parentLink)+4)+"px";
newdiv.style.top = (findPosY(parentLink)+12)+"px";
document.body.appendChild(newdiv);
}
}

function mylinksRemoveConfirm(){
	var urlValue = window.location;
	var myLinks = document.getElementById('myLinksDiv').childNodes;
	var myLinksID;
	for(i=0;i<myLinks.length;i++){
		if(myLinks[i].nodeType==1){
			if(myLinks[i].href==urlValue){
				myLinksID = document.getElementById('myLinksDiv').childNodes[i].id;
				document.getElementById('myLinksDiv').removeChild(myLinks[i]);
				document.getElementById('myLinksDiv').removeChild(myLinks[i]);
				//setCookie('mylinks_items',Base64.encode(document.getElementById('myLinksDiv').innerHTML),14);
				//updateMyLinks(Base64.encode(document.getElementById('myLinksDiv').innerHTML));
				updateMyLinks(Base64.encode(myLinksID.replace('mylinkmenuID_','')),'delete');
				document.getElementById('myLinksModify').innerHTML = "(+) Add Page";
				document.getElementById('myLinksModify').setAttribute("onclick", "mylinksAdd(this);");
				mylinksClose('mylinksAddPopup',false)
			}
		}
	}
}

function mylinksClose (popupid,save) {
	if(save==true){
		var nameValue = document.getElementById(popupid).getElementsByTagName('input')[0].value;
		var urlValue = window.location;
		if(nameValue=="" || nameValue==undefined){
			document.getElementById(popupid).innerHTML = "<span style=\"font-weight:bold;\">A Name must be specified for the link.</span><br>" + document.getElementById(popupid).innerHTML;
		}else{
			var textDiv = document.createTextNode(" | ");
			var newLink = document.createElement('a');
			newLink.href=urlValue;
			newLink.innerHTML=nameValue;
			document.getElementById('myLinksDiv').appendChild(newLink);
			document.getElementById('myLinksDiv').appendChild(textDiv);
			//setCookie('mylinks_items',Base64.encode(document.getElementById('myLinksDiv').innerHTML),14);
			updateMyLinks(Base64.encode('"'+nameValue+'","'+urlValue+'"'),'add');
			document.getElementById('myLinksModify').innerHTML = "(-) Remove Page";
			document.getElementById('myLinksModify').setAttribute("onclick", "mylinksRemove(this);");
			//alert(document.getElementById('myLinks').childNodes);
			document.body.removeChild(document.getElementById(popupid));
		}
	}else{
		document.body.removeChild(document.getElementById(popupid));
	}
}

var updateXML;
function updateMyLinks(data,action)
{
	if(action == null){
		action = "data";
	}
updateXML=null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  updateXML=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  updateXML=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (updateXML!=null)
  {
  //updateXML.onreadystatechange=updateMyLinks_stateChange;
  updateXML.open("GET","/members/myLinksUpdate.php?"+action+"="+data,true);
  updateXML.send(null);
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }
}

function updateMyLinks_stateChange()
{
if (updateXML.readyState==4)
  {// 4 = "loaded"
  if (updateXML.status==200)
    {// 200 = "OK"
    document.getElementById('T1').innerHTML=updateXML.responseText;
    }
  else
    {
    alert("Problem retrieving data:" + updateXML.statusText);
    }
  }
}

