// Setup functions

window.addEvent('domready', function() {
	setupPopups();
	setupForms();
});

setupPopups = function()
{
	elements = $$('a[rel=external]');
	elements.each(function(ele) {
		ele.addEvent('click', function(e) {
			e.stop();
			window.open(ele.getProperty('href'));
		});
	});
};

setupForms = function()
{
	$$('.friendlyForm .inputField').each(function(elem) {
		elem.blur();
		elem.addEvent('focus', function() {
			elem.getParent('li').getChildren('label').addClass('focus');
			var helpers = elem.getParent('li').getChildren('.helper');
			if (helpers.length) {
				var helper = helpers[0];
				helper.setStyles({
					display:'block',
					opacity: 0
				});
				helper.set('tween').fade(1);
				helper.style.display = 'block';
			}
			elem.addClass('focus');
		});
		elem.addEvent('blur', function() {
			elem.getParent('li').getChildren('label').removeClass('focus');
			var helpers = elem.getParent('li').getChildren('.helper');
			if (helpers.length) {
				var helper = helpers[0];
				helper.set('tween',{
					onComplete: function() {
						helper.style.display = 'none';
					}
				}).fade(0);
			}
			elem.removeClass('focus');
		});
	});
}

// Common functions / utils

function go(loc)
{
	document.location.href = loc;
}

function goBack()
{
	history.back();
}

function goHide(obj)
{
	obj.style.display = 'none';
}

function goFocus(obj)
{
	obj.focus();
}

function goShow(obj)
{
	obj.style.display = 'block';
}

function goDisable(obj)
{
	obj.disabled = true;
}

function goEnable(obj)
{
	obj.disabled = false;
}

function goClearField(obj, text)
{
	if (obj.value == text) obj.value = '';
}

function goFillField(obj, text)
{
	if (obj.value == '') obj.value = text;
}

function goClearFieldBg(elem)
{
	if (elem.value == '') {
		elem.style.backgroundImage = 'none';
	}
}

function goFillFieldBg(elem, bg)
{
	if (elem.value == '') {
		elem.style.backgroundImage = 'url(' + bg + ')';
	}
}
	
function goPopup(where)
{
	window.open(where);
}

function goPopupMinimal(loc,w,h)
{
	var leftVal = (screen.width / 2) - (w/2);
	var topVal = (screen.height / 2) - (h/2);
	window.open (loc, 'new_window', 'left=' +leftVal+ ',top=' + topVal + ',toolbar=no,width=' + w + ',height=' + h + ',status=no,resizable=yes,menubar=no,location=no,scrollbars=yes');
}

function goPopupImage(loc)
{
	var w = 640;
	var h = 480;
	var leftVal = (screen.width / 2) - (w/2);
	var topVal = (screen.height / 2) - (h/2);
	window.open (loc, 'new_window', 'left=' +leftVal+ ',top=' + topVal + ',toolbar=no,width=' + w + ',height=' + h + ',status=no,resizable=yes,menubar=no,location=no,scrollbars=no');
}

function doAjaxRequest(strUrl, elem)
{
	ajax = new sack();
	ajax.method = "GET";
	ajax.requestFile = strUrl;
	ajax.element = elem;
	ajax.runAJAX();
}

function getFormFields(objForm)
{
	/* Return false if the form could not be found */
	if (! objForm) return false;

	/* Create object for storing form fields and their values */
	objParams = new Object;

	/* Get all input fields */		
	inputFields = objForm.getElementsByTagName("input");

	/* Loop input fields */
	for (i=0;i<inputFields.length;i++) {

		strName = inputFields[i].name;
		strType = inputFields[i].type;
		strValue = inputFields[i].value;

		switch (strType) {
			case 'text':
			case 'hidden':
			case 'password':
			case 'button':
			case 'submit':
			case 'reset':
			case 'image':
			case 'file':
				objParams[strName] = strValue;
			break;
			case 'checkbox':
			case 'radio':
				if (inputFields[i].checked)
					objParams[strName] = strValue;
			break;
		}
	}
	
	/* Get all single/multiple select fields */
	selectFields = objForm.getElementsByTagName("select");

	/* Loop select fields */
	for (i=0;i<selectFields.length;i++) {

		strName = selectFields[i].name;
		strType = '';
		strValue = '';

		for (j=0;j<selectFields[i].options.length;j++) {
			if (selectFields[i].options[j].selected) {
				if (strValue.length)
					strValue += ',';
				strValue += selectFields[i].options[j].value;
			}
		}
		objParams[strName] = strValue;
	}
	
	/* Return object of form field/value pairs */
	return objParams;
}

function goSwapImage(strId, strImage, strAlt)
{
	var theElem = document.getElementById(strId);
	
	if (! theElem) {
		return;
	}
	
	if (theElem.tagName.toLowerCase() == 'img') {
		theElem.src = strImage;
		if (strAlt !== null) {
			theElem.alt = strAlt;
		}
	}
}