/**
 * @author ivan
 */

/* 
 * Based on http://www.quirksmode.org/dom/getstyles.html
 */
function getStyle( element, styleProp ) {
	var x = document.getElementById(element);

	if  ( x.currentStyle ) {
		var y = x.currentStyle[styleProp];
	} else if ( document.defaultView ) {
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	}

	return y;
}

function disenable( element ) {
	var item = document.getElementById(element);
	var vis = getStyle( element, "visibility" );

	if ( vis == "visible" ) {
		item.style.visibility = "hidden";
	} else {
		item.style.visibility = "visible";
	}
}

/*
 * Set a color triplet in the form, which will be passed to the server
 */
function setColor( element, colorStr ) {
	colorStr = colorStr.toUpperCase();

	document.getElementById(element + "box").style.backgroundColor = "#" + colorStr;
	loadHSV( element, colorStr );
}

/*
 * Sets a form value
 */
function setValue( element, newvalue ) {
	document.getElementsByName( element )[0].value = newvalue;
}

/*
 * Called by the color picker plugin to make changes from the user's selection
 */
function mkColor(o,v) {
	$S( o+'box' ).background = '#' + v;
	setValue( o+'color', v );
}


/*
 * Store or restore a value from the form in a javascript variable.
 * 
 * After storing the value, it will disable the form field so the
 * user can't make any more changes
 * 
 * If the field is already disabled, it will restore the previous
 * value and re-enable the field.
 * 
 */
function stash( element, varname ) {
	var item = document.getElementsByName(element)[0];

	if ( item.disabled ) {
		eval( "item.value = " + varname );
		eval( varname + " = ''");
	} else {
		eval( varname + " = item.value");
		item.value = '';
	}

	item.disabled = !(item.disabled);
}

function sizeBox( element ) {
	var list = document.getElementsByName( element );

	for ( i=0; i < list.length; i++ ) {
		if ( list[i].checked ) {
			if ( list[i].value == "other"  ) {
				document.getElementById( element + '_custom' ).style.visibility = 'visible';
			} else {
				document.getElementById( element + '_custom' ).style.visibility = 'hidden';
			}
		}
	}
}

/*
 * Takes an array with names of form elements,
 * and what to set as the new value
 */
function changeUnits( unitArray, value ) {
	for ( i=0; i < unitArray.length; i++ ) {
		document.getElementsByName(unitArray[i])[0].value = value;
	}
}

/*
 * Will send d page view event to google analytics, used to track
 * when users follow to normally not trackable pages ( like the
 * graphpaper pdf scripts
 */
function trackFile( form ) {
	// Check to see if google analytics is loaded
	if ( typeof pageTracker != "undefined" ) {
		file = form.action.replace(/(.*\/\/[^\/]+\/)(.*)/,'/$2');
		pageTracker._trackPageview( file );
	}
}


