//sets a cookie
function setCookie(cookiename, cookieval, daystoexpire){
	if (!daystoexpire){
		daystoexpire = 30;
	}
	dojo.cookie(cookiename, cookieval, { expires: daystoexpire });
}
//gets a cookie
function getCookie(cookiename){
	return(dojo.cookie(cookiename));
}
//deteles a cookie
function deleteCookie(cookiename){
	dojo.cookie(cookiename, null, {expires: -1});
}

function showMe(myId) {
	//extrasLogger('showing: ' + myId);
	if (dojo.byId(myId)) {
		dojo.byId(myId).style.display = 'block'; 
	}
}
function hideMe(myId) {
	//extrasLogger('hiding: ' + myId);
	if (dojo.byId(myId)) {
		dojo.byId(myId).style.display = 'none';
	}
}
//function for doing nothing - on links where we want nothing to happen
function donothing() {
	return(true);
}
function getDocumentHeight() {
    return Math.max(
        Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
        Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
        Math.max(document.body.clientHeight, document.documentElement.clientHeight)
    );
}
function getDocumentWidth() {
    return Math.max(
        Math.max(document.body.scrollWidth, document.documentElement.scrollWidth),
        Math.max(document.body.offsetWidth, document.documentElement.offsetWidth),
        Math.max(document.body.clientWidth, document.documentElement.clientWidth)
    );
}
function getClientHeight() {
    return Math.max(document.body.clientHeight, document.documentElement.clientHeight);
}
function getOffsetHeight() {
    return Math.max(document.body.offsetHeight, document.documentElement.offsetHeight);
}
function getScrollHeight() {
    return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
}
function isDefined(elementID) {
	return typeof(elementID) != 'undefined';
}
function highlight(elementID, duration) {
	if (dojo.byId(elementID)) {
		if (isDefined(duration) === false) {
			duration = 1200;
		}
		var startColor = dojo.byId(elementID).style.backgroundColor;
		if (startColor && startColor.length === 0) {
			startColor = "transparent";
		}
		extrasLogger("highlight:" + elementID + " duration:" + duration + " startColor: " + startColor);
		dojo.animateProperty({
            node: dojo.byId(elementID),
            duration: duration,
            delay: 0,
            properties: { backgroundColor: { start:startColor, end:"#FFFCAC" } }
        }).play();
		dojo.animateProperty({
            node: dojo.byId(elementID),
            duration: duration,
            delay: duration,
            properties: { backgroundColor: { start:"#FFFCAC", end:startColor } }
        }).play();
	}
}
function extrasLogger(message) {
	if (typeof(djConfig) != 'undefined') { // only log to firebug if djConfig.isDebug is defined
		if (djConfig.isDebug === true) {
			console.info(message);
		}
	}
}

function extrasError(message) {
	if (typeof(djConfig) != 'undefined') { // only log to firebug if djConfig.isDebug is defined
		if (djConfig.isDebug === true) {
			console.error(message);
		}
	}
}

function extrasDir(object) {
	if (typeof(djConfig) != 'undefined') { // only log to firebug if djConfig.isDebug is defined
		if (djConfig.isDebug === true) {
			console.dir(object);
		}
	}
}

//ask user to confirm before completing an action (such as deleting a mission)
function confirmAction(msg) {
	if (typeof msg == 'undefined') {
		msg = 'Are you sure you want to delete this?' + "\n" + 'This cannot be undone!!';
	}
	var agree = confirm(msg);
	if (agree) {
		return true;
	} else {
		return false;
	}
}

function showLargeNoImage(image) {
	if (image) {
		image.src = "/imgver2/no-image-200px.gif";
		image.onerror = "";
	}
    return true;
}

function showSmallNoImage(image) {
	if (image) {
		image.src = "/imgver2/no-image-100px.gif";
		image.onerror = "";
	}
    return true;
}

function showLargeUserImage(image) {
	if (image) {
		image.src = "/images/icons_default/user_default.png";
		image.onerror = "";
	}
    return true;
}

function showSmallUserImage(image) {
	if (image) {
		image.src = "/images/icons_default/user_default_sml.png";
		image.onerror = "";
	}
    return true;
}

function setMaxImageWidth(image, maxWidth) {
	if (image.width > maxWidth) {
		image.width = maxWidth;
		image.style.width = maxWidth + "px";
		image.style.maxWidth = maxWidth + "px";
	}
	else {
		image.style.width = image.width + "px";
		image.style.maxWidth = image.width + "px";
	}
}

//track an event in Google Analytics
function gaTrackEvent(category, action, opt_label) {
	// new async style, see: 
	// http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html
	// http://code.google.com/apis/analytics/docs/gaJS/gaJSApi.html
	if (typeof(_gaq) !== 'undefined') { // only push if google analytics is activated
		_gaq.push(['_trackEvent', category, action, opt_label]); //track click in Google Analytics
	}
}

// track a pageview in Google Analytics
function gaTrackPageview(url) {
	// new async style, see: 
	// http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html
	// http://code.google.com/apis/analytics/docs/gaJS/gaJSApi.html
	if (typeof(_gaq) !== 'undefined') { // only push if google analytics is activated
		_gaq.push(['_trackPageview', url]);
	}
}
function onSearchKeyPressed(field,e,placeholder) {
	var keycode;
    if (window.event) {
        keycode = window.event.keyCode;
    } else if (e) { 
    	keycode = e.which; 
    } else { 
        return true;
    }
    if (keycode == 13 || keycode == 10) {
    	if (field.value != placeholder && field.value.length > 0) {
    		field.form.submit();
    	}
    	return false;
    }
    return true;
}
function hideDefaultValue(field,e,placeholder) {
	var defaultValue = (placeholder != null) ? placeholder : field.defaultValue;
	if (field.value == defaultValue) {
		field.value = '';
	}
	return true;
}
function showDefaultValue(field,e,placeholder) {
	var defaultValue = (placeholder != null) ? placeholder : field.defaultValue;
	if (field.value == '') {
		field.value = defaultValue;
	}
	return true;
}

/*
 * iniframe = if called from an iframe
 * targetElement = element to replace with the loging forms
 * oncloseFn = function to run on close
 * positioning = offset or inline
 */
function showLogonRegPrompt(iniframe, targetElement, oncloseFn, positioning) {
	var beextraHtml  = '<div style="text-align:center; margin-top: 0%; font-size: 200%;">';
	beextraHtml 	+= '<div id="loginButton" class="btn" style="font-size:1.9em"><a href="#" onclick="populateGigyaLoginUIPopup(\'login\');">Login</a></div>';
	beextraHtml 	+= '</div>';
	if (iniframe){
		top.showGigyaLoginContainerPopupFromFrame(dojo.byId(targetElement), beextraHtml, oncloseFn, positioning);
	} else {
		showGigyaLoginContainerPopupFromFrame(dojo.byId(targetElement), beextraHtml, oncloseFn, positioning);
	}
}

function empty(obj) {
 return (obj == null || typeof(obj) == 'undefined' || obj  == '');
}

function getHash() {
  var hash = window.location.hash;
  return hash.substring(1); // remove #
}

// http://stackoverflow.com/questions/2568842/jquery-url-validator
// this is a very simple validator that checks only the most basic urls.  a more robust solution might be desireable, but this should be enough for our needs
function validateURL(textval) {
	var urlregex = new RegExp("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
	return urlregex.test(textval);
}

