﻿////////////////////////////////////////////////////////////////////////////////
// need a trim function
String.prototype.trim = function () {return this.replace(/^\s*/,"").replace(/\s*$/,"");}

////////////////////////////////////////////////////////////////////////////////
// set the lanuage cookie and bounce to the home page
function setLang(idLang)
{
	var d = new Date()
	d.setFullYear(d.getFullYear() + 1)

	document.cookie = "lang="+idLang+";expires="+d.toUTCString()+";path="+escape("/");
	location.href = location.href;
}
////////////////////////////////////////////////////////////////////////////////
// set a cookie and reload
function setCookie(cname,cval) {
	document.cookie = cname+"="+cval + ";path=" + escape("/");
	location.href = location.href;
}

////////////////////////////////////////////////////////////////////////////////
// clear search form
function clrSearch()
{
	document.getElementById("s4").value = "";
	document.getElementById("cat_id").selectedIndex = 0;
}

////////////////////////////////////////////////////////////////////////////////
// create an XMLHttpRequest Object
function createRequest()
{
	var xhr = false;
	try {
		xhr = new ActiveXObject("Microsoft.XMLHTTP");    // Trying Internet Explorer 
	}
	catch(e)    // Failed 
	{
		xhr = new XMLHttpRequest();    // Other browsers.
	}
	return xhr;
}

////////////////////////////////////////////////////////////////////////////////
// find xy pos of an element in the dom
function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent){
		do {
		curleft += obj.offsetLeft;
		curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}
//////////////////////////////////////////////////////
// expand a div
function m_expando(link) {
	var linktext = link.firstChild.nodeValue;
	var div = document.getElementById(link.rel);
	if (!div)
		return;

	// if the div is "closed" open it
	if (div.style.display == "none") {
		div.style.display = "block";
		link.firstChild.nodeValue = linktext.replace("[+]", "[-]");
	}
	// else its open so close it
	else {
		div.style.display = "none";
		link.firstChild.nodeValue = linktext.replace("[-]", "[+]");
	}
	return false;
}

//////////////////////////////////////////////////////
// language was changed in page edit
function cms_chngLanguage(ptr,param,value)
{
	var lid = ptr.options[ptr.selectedIndex].value;
	var pg = location.href.split("?")[0];
	if (param == "")
		location.href = pg + "?lid=" + lid;
	else
		location.href = pg+"?lid="+lid+"&"+param+"="+value;
}

////////////////////////////////////////////////////////////////////////////////
// pop-up window handler
function cms_do_pop(type,iid)
{
	switch(type){
		case "TAG":
			window.open("m_tag_add.aspx?tt=US&iid="+iid, "ptag", "width=700,height=400,resizable,scrollbars");
			break;
		case "MSG":
			window.open("m_user_message_new.aspx?uid="+iid, "pmsg", "width=700,height=400");
			break;
		case "PW":
			window.open("sendpw.aspx", "ppw", "width=500,height=250");
			break;
		case "PID":
			window.open("sendpid.aspx", "ppid", "width=500,height=250");
			break;
		case "UMSG":
			window.open("umsg_new.aspx?uid=" + iid, "upmsg", "width=600,height=400,resizable,scrollbars");
			break;
	}
	return false;
}

////////////////////////////////////////////////////////////////////////////////
// does what it says - get elements of clsName that are of type tagName
document.getElementsByClassName = function(clsName,tagName)
{
	var retVal = new Array();
	var elements = document.getElementsByTagName(tagName);
	for (var i = 0; i < elements.length; i++) {
		if (elements[i].className.indexOf(" ") >= 0) {
			var classes = elements[i].className.split(" ");
			for (var j = 0; j < classes.length; j++) {
				if (classes[j] == clsName)
					retVal.push(elements[i]);
			}
		}
		else if (elements[i].className == clsName)
			retVal.push(elements[i]);
	}
	return retVal;
}

function do_tips()
{
	$("img.tt").hover(
		function() { return; },
		function() { $(this).next().css({ display: "none" }); }
	);
	$("img.tt").mousemove(function(e) {
		// see if we need to switch sides for the tip pos
		var xoff = 15;
		var ww = $(window).width();
		if (ww < (e.pageX + 250))
			xoff = - 225;
		// now check for off the bottom
		var yoff = 0;
		var wh = $(window).height();
		if (wh < (e.pageY + 100))
			yoff = -1 * ($(this).next().height() + 20);
		// set pos & show
		var mousex = e.pageX + xoff;
		var mousey = e.pageY + yoff;
		$(this).next().css({ top: mousey, left: mousex, display: "block" });
	});
}
