function eventToggle(spanId){
	document.getElementById(spanId).onclick = function(){};
	var fw = document.getElementById(spanId).style.fontWeight;
	document.getElementById(spanId).style.fontWeight = (fw == "bold") ? "" : "bold";
	document.getElementById(spanId+'_1').style.border = (fw == "bold") ? "1px solid white" : "1px solid black";
	document.getElementById(spanId).style.color = (fw == "bold") ? "#7F7F81" : "blue";
	var setTo = (fw == "bold") ? false : true;
//	alert(document.getElementById(spanId+'_box').checked+"\nSetting to: "+setTo);
	document.getElementById(spanId+'_box').checked = setTo;
	document.getElementById(spanId).onclick = function(){ eventToggle(spanId); };
}


function confirmDelete(URL, deleteType) {
	var answer = confirm("Are you sure you wish to delete this "+deleteType+"?")
	if (answer){
		window.location = URL;
	}
	else{
//		alert("Deletion Cancelled!")
	}
}

function swapContact(infoList, pageNum){

	var cell = document.getElementById("contactList");
	if (cell.hasChildNodes()){
	    while (cell.childNodes.length >= 1){
	        cell.removeChild(cell.firstChild);
	    } 
	}
	
	var pd = oldPage + 1;
	document.getElementById("tablePage"+oldPage).innerHTML = "<a href=\"javascript:swapContact(info, "+oldPage+")\">"+pd+"</a>";;
	document.getElementById("tablePage"+pageNum).innerHTML = pageNum+1;
	oldPage = pageNum;
	
	var start = pageNum * 15;
	var end = Math.min((pageNum+1)*15, infoList.length);
	
	for(var i = start; i < end; i++){
		var tr = document.createElement('tr');
		tr.setAttribute("height", 30);
		
		if (i % 2 == 0) {
			tr.setAttribute('class', 'tableOne');
			tr.setAttribute('className', 'tableOne');
		}
		else {
			tr.setAttribute('class', 'tableTwo');
			tr.setAttribute('className', 'tableTwo');
		}
		for (var j = 0; j < infoList[i].length; j++){
			var td = document.createElement('td');
			td.setAttribute("style", "border-top: 1px gray solid;");
//			td.appendChild(document.createTextNode(infoList[i][j]));
			td.innerHTML = infoList[i][j];
			

			if (j > 2) { td.align = 'center'; }

			tr.appendChild(td);
		}
		document.getElementById('contactList').appendChild(tr);
	}
	
	$(".contactLink").colorbox({innerWidth: '650px', scrolling: false});
		
}

/**
 * Concatenates the values of a variable into an easily readable string
 * by Matt Hackett [scriptnode.com]
 * @param {Object} x The variable to debug
 * @param {Number} max The maximum number of recursions allowed (keep low, around 5 for HTML elements to prevent errors) [default: 10]
 * @param {String} sep The separator to use between [default: a single space ' ']
 * @param {Number} l The current level deep (amount of recursion). Do not use this parameter: it's for the function's own use
 */
function print_r(x, max, sep, l) {

	l = l || 0;
	max = max || 10;
	sep = sep || ' ';

	if (l > max) {
		return "[WARNING: Too much recursion]\n";
	}

	var
		i,
		r = '',
		t = typeof x,
		tab = '';

	if (x === null) {
		r += "(null)\n";
	} else if (t == 'object') {

		l++;

		for (i = 0; i < l; i++) {
			tab += sep;
		}

		if (x && x.length) {
			t = 'array';
		}

		r += '(' + t + ") :\n";

		for (i in x) {
			try {
				r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
			} catch(e) {
				return "[ERROR: " + e + "]\n";
			}
		}

	} else {

		if (t == 'string') {
			if (x == '') {
				x = '(empty)';
			}
		}

		r += '(' + t + ') ' + x + "\n";

	}

	return r;

}

var_dump = print_r;

function urlencode(str) {
	return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

function urldecode(str) {
	while (str.indexOf('+') != -1){
		str = str.replace('+', ' ');
	}
	str = unescape(str);
	return str;
}

function browserFixSession(sess_id) {
//alert('no cookie found');
	controller = 'cookie.php'; //your page that will grab the session and set cookie
    var url = location.href;
	var url_with_redirect = controller + "?redirect_to=" + url;

	$('body').prepend("<form id='browserFixSession'></form>");
	var f = $('#browserFixSession');
	$(f).attr('method', 'POST');
	$(f).attr('action', url_with_redirect);
	$(f).append('<input type="hidden" name="session" id="browserFixSession_session" />');
	$('#browserFixSession_session').attr('value', sess_id);
	//alert($(f).serialize());
	$(f).submit();
}

