var caution = false

function setCookie(name, value, expires, path, domain, secure)
{
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	
	if (!caution || (name + "=" + escape(value)).length <= 4000) {
		document.cookie = curCookie;
	} else {
		if (confirm("Cookie exceeds 4KB and will be cut!")) {
			document.cookie = curCookie;
		}
	}
}

function fixDate(date)
{
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0) { date.setTime(date.getTime() - skew); }
}

function getCookie(name)
{
	var prefix = name + "=";
	var cookieStartIndex = document.cookie.indexOf(prefix);

	if (cookieStartIndex == -1){return null;}
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1){cookieEndIndex = document.cookie.length;}
	return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

var now = new Date();
fixDate(now);
now.setTime(now.getTime() + 24 * 60 * 60 * 1000 );
var current_cc_referrer = getCookie("cc_referrer");

if ((!(current_cc_referrer)) ||
    (current_cc_referrer=='no referrer') ||
    (current_cc_referrer=='bookmarked or typed') ||
    ((document.referrer) &&
        (document.referrer.substring(0,7)=='http://') &&
        (document.referrer.substring(7,23)!='www.onesite.com/') &&
        (document.referrer.substring(7,27)!='hosting.catalog.com/'))) {
	if (!(document.referrer)){
		setCookie("cc_referrer",'bookmarked or typed',now,'/',".onesite.com");
	}else{
		setCookie("cc_referrer",document.referrer,now,'/',".onesite.com");
	}
}
function goPop(url, wide, high)
{
	window.open(url,"popup",'width=' + wide + ',height=' + high);
}

/********************************************************************
** displays the login or find form in the toolbar and hides the other
*********************************************************************/
function toggleLoginSearch( goDo )
{
	formFind = document.getElementById("tbFindPeople");
	formLog = document.getElementById("tbLogin");
	
	if ( goDo == "login" ) {
		
		formFind.style.display = "none";
		formLog.style.display = "inline";
		
		document.loginForm.username.focus();

	} else if ( goDo == "find" ) {
		
		formFind.style.display = "inline";
		formLog.style.display = "none";

	}
	
	return false;
}

/**
* Submits a given form on the "enter" keypress. Used b/c different browsers
* handle the enter key in different ways. Especially if there are multiple forms
* on the page.
*/
function entsub(myform) 
{     
	if (window.event && window.event.keyCode == 13) {
		myform.submit();
		return false; //return false until i can fix this problem
	} else {
		return true;
	}
}


/*
* From toolbar common
*/
function confirm_prompt(text,link) {
    if (confirm(text)) {
      window.location = link;
    }
}

function submitenter(field,e)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    
    if (keycode == 13)
       {
       field.submit();
                    if (field.name == 'Terms'){
                            opencontent('sc4');
                    }
       return false;
       }
    else
    {
       return true;
    }
}
function closecontent (eid)
{
    $(eid).hide();
}

function opencontent (eid)
{
    $(eid).show();
}

function menuexpander(me, length) 
{
	for(i=1;i<=length;i++)
	{
		var uls = document.getElementById("ulmenuitem" + i);
		if(uls)
		{
			uls.style.display = "none";
		}
	}
	
	var ul = document.getElementById("ul" + me.id);
	if(ul.style.display == "none") {
		ul.style.display = "block";
	 }
	else {
		ul.style.display = "none";
	}
	return false;
}



/*  initialize friend search loaded file to false */
var friend_search_loaded = false;
/* initialize and load all of the functions necessary for using the "friend search" with the "send to friend" */
function friend_search_for_send_to_friend(content_id,link,sendtofriend_ajax,friend_search_ajax,sendtofriend_server,friendsearch_server)  {

	//if not already loaded...load files necessary
	if(friend_search_loaded==false)  {	
		
		//dynamically append the send to friend div
		var sendtofriend = document.createElement('div');
		sendtofriend.setAttribute('id', 'sendtofriend_div');		
		sendtofriend.setAttribute('style', 'display:none;');
		document.body.appendChild(sendtofriend);
		
		//dynamically append the friend search holder
		var friendsearchholder = document.createElement('div');
		friendsearchholder.setAttribute('id', 'friendSearchHolder');		
		document.body.appendChild(friendsearchholder);
		
		
		dynamicLoadFile(sendtofriend_ajax,'js');	
		dynamicLoadFile(friend_search_ajax,'js');				
		
		friend_search_for_send_to_friend_loader(sendtofriend_server,friendsearch_server,content_id,link);		
	
		friend_search_loaded = true;
		
		setTimeout("$('sendtofriend_div').style.display = 'block';",1000);
							
	} else {  
		
		//already loaded, so, just format it		
		format_friend_search_div(content_id); 
	}
	
}

//function to load div...logic built in to not actually call the div loader function until the js lib is loaded completely.
function friend_search_for_send_to_friend_loader(sendtofriend_server,friendsearch_server,content_id,link) {
	//see if js file is loaded
	if (!window.process_load_friend_search_div||!window.format_friend_search_div) {
  		//wait .5 seconds and try again
  		
  		setTimeout('friend_search_for_send_to_friend_loader(\''+sendtofriend_server+'\',\''+friendsearch_server+'\',\''+content_id+'\',\''+link+'\')',500);
  		return;
	}
	
	//process_load_friend_search_div(url,cid,link,act)
	process_load_friend_search_div(sendtofriend_server,friendsearch_server,content_id,link,'load');
}

/*  general message to let users know they must log in to access a feature  

relative_to_elem ~ the element that the loading element is set relative to
element ~ the element being loaded
x ~ the x offset
y ~ the y offset
*/
function must_login_to_access(relative_to_elem,element,x,y)  {
	document.getElementById(element).innerHTML = 'You must Login or Sign Up to access';
	
	//alert(document.getElementById(element).innerHTML);
	setPosition(relative_to_elem,element,x,y);
	
	document.getElementById(element).toggle();
}


/*******************************************************************************
* Get the x and y coordinates of the given element.
*/
function getElementCoordinates(obj) {
	var point = { x: 0, y: 0 }; 
	  
	while(obj) {
	    point.x += obj.offsetLeft ;
	    point.y += obj.offsetTop;
	    obj = obj.offsetParent;
	} 
  
	return point;
}

/*  set the position of an element (such as a div) based on the location of another element  */
function setPosition(x,e,offset_left,offset_top)  {
	var pos = getElementCoordinates(e);
	
	

	
	document.getElementById(x).style.left = ((pos.x - offset_left) + 'px' );
	document.getElementById(x).style.top = (pos.y + offset_top + 'px');

}

/***********************************************************************************************/
/***************** FUNCTIONS FOR DYNAMICALLY LOADING AND TOGGLING FILES ***********************/
/*********************************************************************************************/

/*Dynamically loads a file of type 'type' (js/css) anytime during program execution.*/
function dynamicLoadFile(url,type) {
	if (type == 'js') {
  		var e = document.createElement("script");
  		e.type = "text/javascript";
  		e.src = url;
	} else if (type == 'css') {
  		var e = document.createElement("link");
  		e.type = "text/css";
  		e.rel = "stylesheet";
  		e.href = url;
	} else { return; }

	document.getElementsByTagName("head")[0].appendChild(e);
}

/************************************ FINI *****************************************************/

// Reload leaderboard ad
function oneReloadLeaderboardAd() {
	var tmpAdEl = document.getElementById('oneToolbarAdContainer');
	var tmpAdCode = tmpAdEl.innerHTML;
	tmpAdEl.innerHTML = '';
	tmpAdEl.innerHTML = tmpAdCode;
}

// Reload the Usersub TD contents (ad on some pages)
function oneReloadUsersubTdContent() {
	var tmp2AdEl = document.getElementById('usersub-td-right');
	var tmp2AdCode = tmp2AdEl.innerHTML;
	tmp2AdEl.innerHTML = '';
	tmp2AdEl.innerHTML = tmp2AdCode;
}

// Base64 encode / decode
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;
    }
}