function emptyField(textObj) {
	if(textObj.value.length==0) return true;
	for(var i=0; i<textObj.value.length; ++i) {
		var ch = textObj.value.charAt(i);
		if(ch != ' ' && ch != '\t') return false;
	}
	return true;
}

function emptyValue(text) {
	if(text.length==0) return true;
	for(var i=0; i<text.length; ++i) {
		var ch = text.charAt(i);
		if(ch != ' ' && ch != '\t') return false;
	}
	return true;
}

function isEmail (s){
	if (emptyValue(s)) return false;

	var i = 1;
	var sLength = s.length;

	while ((i < sLength) && (s.charAt(i) != "@")) {
		i++;
	}

	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;

	while ((i < sLength) && (s.charAt(i) != ".")) {
		i++;
	}

	if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
	else return true;
}


function confirmNewLocation( message, newlocation ) {
	if ( confirm( message )) return true;
	else return false;
}


/**
 * Retrieve the absolute coordinates of an element.
 *
 * @param element
 *   A DOM element.
 * @return
 *   A hash containing keys 'x' and 'y'.
 */
function getAbsolutePosition(element) {
  var r = { x: element.offsetLeft, y: element.offsetTop };
  if (element.offsetParent) {
    var tmp = getAbsolutePosition(element.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  return r;
};

/**
 * Retrieve the coordinates of the given event relative to the center
 * of the widget.
 *
 * @param event
 *   A mouse-related DOM event.
 * @param reference
 *   A DOM element whose position we want to transform the mouse coordinates to.
 * @return
 *    A hash containing keys 'x' and 'y'.
 */
function getRelativeCoordinates(event, reference) {
  var x, y;
  event = event || window.event;
  var el = event.target || event.srcElement;

  if (!window.opera && typeof event.offsetX != 'undefined') {
    // Use offset coordinates and find common offsetParent
    var pos = { x: event.offsetX, y: event.offsetY };

    // Send the coordinates upwards through the offsetParent chain.
    var e = el;
    while (e) {
      e.mouseX = pos.x;
      e.mouseY = pos.y;
      pos.x += e.offsetLeft;
      pos.y += e.offsetTop;
      e = e.offsetParent;
    }

    // Look for the coordinates starting from the reference element.
    var e = reference;
    var offset = { x: 0, y: 0 }
    while (e) {
      if (typeof e.mouseX != 'undefined') {
        x = e.mouseX - offset.x;
        y = e.mouseY - offset.y;
        break;
      }
      offset.x += e.offsetLeft;
      offset.y += e.offsetTop;
      e = e.offsetParent;
    }

    // Reset stored coordinates
    e = el;
    while (e) {
      e.mouseX = undefined;
      e.mouseY = undefined;
      e = e.offsetParent;
    }
  }
  else {
    // Use absolute coordinates
    var pos = getAbsolutePosition(reference);
    x = event.pageX  - pos.x;
    y = event.pageY - pos.y;
  }
  // Subtract distance to middle
  return { x: x, y: y };
}

/**
 *return a document object by name
 */
function getObjectByName(name) {
	if (document.all) {return document.all[name];}
	else {return document.getElementsByName(name); }
}

/**
 *    return a document object by id
 */
function getObjectById( id )
{
	toRet = "";
	if ( document.all )
		toRet = document.all[ id ];
	else
		toRet = document.getElementById( id );
	return toRet;
}

function findObj(n, d) {
  var p,i,x;
  if(!d) d=document;
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
  	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) x=d.all[n];
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n);
  return x;
}


/**
 *  It's updating the height of an inportant <div> in module creation
 */
function showCreationProjectLineSpacer()
{
	i = 1;
	showObj = findObj ("linewithprojectscontent"+i);

	while(showObj)
	{
		aux = showObj.offsetHeight;
		showObj.style.height= aux+"px";
		i++;
		showObj = findObj ("linewithprojectscontent"+i);
	}


}
/**
*enable / disable radio buttons
**/
function radioButtonSwitchEnabledDisabled ( field, boolVal )
{
	if ( typeof ( field ) != "undefined" )
	{
		for(var i=0;i<field.length; i++)
		{
			field[i].disabled = boolVal;
		}
	}
}

/**
 *return source file of memorial site profile photo
 **/
function get_profile_photo ( msid )
{
	var toRet = "";
	toRet = "uploads/" + msid + "/profile.jpg";
	return toRet;
}



/**
 *	Added at 29.04.2009
 *	Downloaded from: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_str_replace/
 **/
function str_replace(search, replace, subject) {
    // Replaces all occurrences of search in haystack with replace
    //
    // version: 903.3016
    // discuss at: http://phpjs.org/functions/str_replace
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brettz9.blogspot.com)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
    var s = subject;
    var ra = r instanceof Array, sa = s instanceof Array;
    var f = [].concat(search);
    var r = [].concat(replace);
    var i = (s = [].concat(s)).length;
    var j = 0;

    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    }

    return sa ? s : s[0];
}


/**
 *	Global variables
 **/
var enableCloseWarning = true;


function clicked_on_already_registered( felem , forcheck, foruncheck )
{
	var ischecked = felem.checked;
	objContainer1 = $('#' + forcheck);
	objContainer2 = $('#' + foruncheck);
	if ( !ischecked )
	{
		objContainer1.hide();
		objContainer2.show();
	}
	else
	{
		objContainer1.show();
		objContainer2.hide();
	}
}

function showDebug(){
	var showObj = document.getElementById('debug')
	if (showObj.style.display =='none' ){
		showObj.style.display = '';
	}else{
		showObj.style.display = 'none';
	}
}

function checkPressedKey(e){
	var unicode;
	if( !e ) {
	  //if the browser did not pass the event information to the
	  //function, we will have to obtain it from the event register
	  if( window.event ) {
		//Internet Explorer
		e = window.event;
	  } else {
		//total failure, we have no way of referencing the event
		return;
	  }
	}
	if( typeof( e.keyCode ) == 'number'  ) {
	  //DOM
	  e = e.keyCode;
	} else if( typeof( e.which ) == 'number' ) {
	  //NS 4 compatible
	  e = e.which;
	} else if( typeof( e.charCode ) == 'number'  ) {
	  //also NS 6+, Mozilla 0.9+
	  e = e.charCode;
	} else {
	  //total failure, we have no way of obtaining the key code
	  return;
	}

	if ( e == 27 )
	{
		tb_remove();
	}
}

document.onkeyup = checkPressedKey;

function hideHTMLElem ( ContainerId )
{
	objContainer = $('#' + ContainerId);
	objContainer.hide(  );
}


function submitSearchMemorialSites ( form )
{
	var ok = true;
	try {
		if ( emptyValue ( form.search_firstname.value )
			&& emptyValue ( form.search_lastname.value )
			&& emptyValue ( form.search_other.value )
			) ok = false;
	} catch ( e ) {
		ok = false;
	}
	return ok;
}
