/* -- util.js -- */
/*
function sendBackgroundRequest(url) {

	var req = newRequest();

	req.open("GET", url, true);
	req.send(null);
}
		
function newRequest() {

	var ua = navigator.userAgent;
	var o = null;

	if (ua.indexOf("MSIE") > -1)
		o = new ActiveXObject("Microsoft.XMLHTTP");

	if (ua.indexOf("Gecko") > -1)
		o = new XMLHttpRequest();

	return o;
} */

function textCounter(field, countfield, maxlimit) {
	if ( field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else 
		if (countfield!=null) countfield.value = maxlimit - field.value.length;
}

function setMaxCharLimit( field, countfield, maxlimit ) {

	field.onpaste = function() { return textCounter( field, countfield, maxlimit ); }
	field.onkeypress = function() { return textCounter( field, countfield, maxlimit ); }
	field.oninput = function() { return textCounter( field, countfield, maxlimit ); }
	field.onchange = function() { return textCounter( field, countfield, maxlimit ); }
	
}


function InputGroup( element ) {

	var legend = element.getElementsByTagName('legend')[0];
	var input = legend.getElementsByTagName('input')[0];
	var content = element.getElementsByTagName('div')[0];
	
	
	function disableInputsInsideElement( element ) {
	
		addClass( element, 'disabled');
		
		var inputs = element.getElementsByTagName('input');
		for ( var i = 0; i < inputs.length; i++ ) {
			inputs[i].setAttribute( 'disabled', 'disabled' );
		}
		
		content.onclick = function() { input.checked = true; enableInputsInsideElement( content ); };
	}
	
	function enableInputsInsideElement( element ) {
		
		removeClass( element, 'disabled' );
		
		content.onclick = ''
		
		var inputs = element.getElementsByTagName('input');
		for ( var i = 0; i < inputs.length; i++ ) {
			inputs[i].removeAttribute( 'disabled' );
		}
			
		if ( inputs[0] && inputs[0].select ) { setTimeout( function() { inputs[0].select(); }, 10 );  }
	
	}
	
	
	addEvent( input, 'click', function()  { if ( input.checked ) { enableInputsInsideElement( content ); } else { disableInputsInsideElement( content ); } } );
	
	
	if ( input.checked ) {
	
		enableInputsInsideElement( content );
	}
	
	else {
	
		disableInputsInsideElement( content );
	
	}
	

}

/* -- login/logout js -- */
function _loggedOutForums(req) {
	if (req.readyState != 4) return;
	
	if (req.status != 200) {
		alert("There was an error logging out. Please try again later");
	    return false;
	}
	
 	return true;
}

function _loggedOutSearch(req) {
	if (req.readyState != 4) return;
	
	if (req.status != 200) {
		alert("There was an error logging out. Please try again later");
		return false;
	}
	
	window.location.reload(); 
 	return true;
}

function logoutForums() {
	var callback = function(req) { _loggedOutForums(req); };
	var url = "/forum/my/logout";
	sendRequest(url,'get',callback);
}

function logoutForumsNoAjax(forumsURL, myindeedURL) {
	if (document.images) {
		var img = new Image();
		img.onload = function() { document.location = forumsURL; };
		img.src = myindeedURL+'&zr='+rand();
	}
}

function logoutSearch( url ) {
	var callback = function(req) { _loggedOutSearch(req); };
	sendRequest(url,'get',callback);
}



function loginForums( action, msg, dest, regdest ) {

	var form = document.createElement('form');
	form.setAttribute('method', 'post');
	form.setAttribute('action', action );
	form.style.display = 'none';
	
	var msg_input = document.createElement('input');
	msg_input.setAttribute('type', 'hidden');
	msg_input.name = 'msg';
	msg_input.value = msg;
	form.appendChild( msg_input );
	
	var dest_input = document.createElement('input');
	dest_input.setAttribute('type', 'hidden');
	dest_input.name = 'dest';
	dest_input.value = dest;
	form.appendChild( dest_input );

	var reg_dest_input = document.createElement('input');
	reg_dest_input.setAttribute('type', 'hidden');
	reg_dest_input.name = 'regdest';
	reg_dest_input.value = regdest;
	form.appendChild( reg_dest_input );
	
	document.body.insertBefore( form, document.body.firstChild );
	form.submit();

}




/* -- forumalerts related javascript -- */
function showalertform(visible,prefix) {

	if (prefix==null) prefix='';
	var el = gbid(prefix+'jobalerts');
	
	gbid(prefix+'jobalertspan').onclick = function() { showalertform(!visible,prefix); };
	
	if (visible) {
		removeClass(el, 'closed');
		addClass(el, 'open');
		var inp = gbid(prefix+'alertemail');
		if (inp && inp.focus ) inp.focus(); 
		if (inp && inp.value && inp.select) inp.select(); 
	} else {
		removeClass(el, 'open');
		addClass(el, 'closed');   
	}
}



/* -- forumalerts.js -- */
function addmyalert(forumname, forumtype, topicid, uid, userloc, ip, prefix) {
	return addforumalert(forumname, forumtype, topicid, uid, null, userloc, ip, prefix );
}


function addforumalert(forumname, forumtype, topicid, uid, email, userloc, ip, prefix) {

	setForumAlertMsg( prefix, 'Saving forum alert...', '' );
		
	// NOTE: forumname, forumtype should be urlencoded already.  
	var url = '/forum/createalert?forumname='+forumname+'&forumtype='+forumtype+'&topicid='+topicid+'&uid='+uid+'&email='+urlencode(email)+'&userloc='+urlencode(userloc)+'&ip='+ip+'&prefix='+prefix+'&ts='+rand();

	var callback = function(req) { _forum_alertcb(prefix, req); };
	sendRequest( url,'post',callback);
	return false;

}

function _forum_alertcb(prefix, xmlhttp) {

	if (xmlhttp.readyState != 4) return;

	if (xmlhttp.status != 200) {
		forum_alerterror(prefix, 'There was an error; please try again later');
		return;
	}

	// function wrappers to add the prefix parameter 
	var _alertactive = function(msg, params) { forum_alertactive(prefix, msg, params); }
	
	// var _forum_confirmationsent = function(msg, params) { forum_confirmationsent(prefix, msg, params); }
	var _confirmationsent = function(msg, params) { forum_alertactive(prefix, msg, params); }
	
	var _alerterror = function(msg, params) { forum_alerterror(prefix, msg); }
	
	eval(xmlhttp.responseText);

}

function forum_alertactive(prefix, msg, params) {

	if (params==null) params='';

	setForumAlertMsg( prefix, msg, 'active' );
	
	var el = gbid(prefix+'jobalerttext');
	el.onclick = null;

	var form = gbid(prefix+'jobalertform');
	if ( form ) { form.style.display = 'none'; }

}

function forum_confirmationsent(prefix, msg, params) {
	if (params==null) params='';

	setForumAlertMsg( prefix, '<b><font size="+1">Please check your email</font> - we have sent a confirmation message.</b>', 'confirm' );

	var form = gbid(prefix+'jobalertform');
	if ( form ) { form.style.display = 'none'; }

}

function forum_alerterror(prefix, msg) {

	setForumAlertMsg( prefix, msg, 'error' );

	var el = gbid(prefix+'jobalerttext');
	el.onclick = null;

	el = gbid(prefix+'alertemail');
	if (el && el.select) el.select();
}

function _forum_alertremoved(prefix, msg) {
	// currently unused.
}


function setForumAlertMsg( prefix, msg, className ) {
	
	var el = gbid(prefix+'jobalerttext');
	
	if ( el ) { el.innerHTML = msg; }
	if ( className != undefined ) { el.className = className; }

}


/* --- forum comments -- */
function replyTo(elid) {
    var el = document.getElementById(elid);
    document.forms.CommentForm.c.focus();
    if (el != null)
        document.forms.CommentForm.c.value=el.innerHTML;
}
