var alert_options = [ 'Never', 'Every day', 'Every 3 days','Every week','Every month'];
var alert_text = new Object;
for ( var i = 0; i < alert_options.length; i++ ) { alert_text[alert_options[i]] = alert_options[i] }

var alert_data = null;

var alert_change_list = new Object;

function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
	{
	    while (obj.offsetParent)
		{
		    curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
    else if (obj.x)
	curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
	{
	    while (obj.offsetParent)
		{
		    curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
    else if (obj.y)
	curtop += obj.y;
    return curtop;
}




function display_option_box ( target, options, text, func )
{
  var holder = document.getElementById('displayHolder');
  if ( holder == null ) { return }
 
  if ( document.getElementById('displayOption') ) // If there is a display box open then hide it. 
  {
    clean( holder ); 
  }
  var linkX = findPosX( target ) + 6;
  var linkY = findPosY( target ) + 22;
 
  var display = document.createElement('DIV');
  display.id = 'displayOption';
  display.style.top = linkY + 'px';
  display.style.left = linkX + 'px';
  holder.appendChild(display);

  for ( var i = 0; i < options.length; i++ )
  {
    var di = document.createElement( 'DIV' );
    display.appendChild( di );
    var link = document.createElement( 'A' );
    link.href = 'corum_update_search_freq.cgi?freq='+options[i]+ "&searchid=" + target.rel;    
    di.appendChild( link );
    link.appendChild( document.createTextNode(text[options[i]]) );
    addEvent(link, 'click', func );
  }

  
  addEvent( display, 'click', stop_prop );
  addEvent( document, 'click', hide_display );

}

function update_email_alert()
{
  if ( alert_data == null ) { alert_data = new Object }

  var list = document.getElementsByTagName('A');

  for ( var i = 0; i < list.length; i++ )
  {
    if ( list[i].rel != null && list[i].rel != '' )
    {
      var alert_current_target = list[i].rel;
      if ( alert_data['update_'+alert_current_target] && alert_change_list[alert_current_target]  )
      {
        set_alert_val( alert_current_target, alert_change_list[alert_current_target][1] );
      }
      else if ( alert_change_list[alert_current_target] )
      {
        set_alert_val( alert_current_target, alert_change_list[alert_current_target][0] );
      }
      alert_change_list[alert_current_target] = null;
    }
  }

  alert_data = null;

}

function get_event_target( e )
{
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    return targ;

}

function clean( target )
{
    if ( junk )
	{
	    target.innerHTML = '';
	}
    else
	{
	    while ( target.childNodes.length > 0 )
		{
		    target.removeChild(target.firstChild)
			}
	}

}


function update_alert(e)
{
  var target = get_event_target(e);
   hide_display(e);
  
  var location = target.href.indexOf('?') + 1;
  location = target.href.substring(location, target.href.length);
  var tmp = location.split(/&/);
  var searchstring = tmp[1];
  var searcharray = searchstring.split(/=/);
  
  var freqstring = tmp[0];
  var freqarray = freqstring.split(/=/);
  
  set_alert_val( searcharray[1], 'changing' ); 
  window.location=target.href;
   
  
  return false;

}

function set_alert_val( name, new_val )
{
  var current = '';

  var list = document.getElementsByTagName('A');

  for ( var i = 0; i < list.length; i++ )
  {
    if ( list[i].rel == name ) 
    {
      current = list[i].firstChild.nodeValue;
      list[i].firstChild.nodeValue = new_val;
    
      
        list[i].className='freqLink';
      
    }
  }    

  return current;

}

function hide_display(e)
{
   var t =  document.getElementById('displayOption');
   t.style.display = 'none';
   stop_prop(e);
}

function show_alert_list(e)
{
   var target = get_event_target(e);

   stop_prop(e);

   display_option_box( target, alert_options, alert_text, update_alert );

   return false;
}

function getElementsByClass(searchClass,node,tag)
{
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function showSignOut()
 {
   var targ = document.getElementById('signout');
   if(targ)
   {
     targ.style.display = "inline";
   } 
 }


function addEvent(oTarget, sType, fpDest) {
  sType = 'on'+sType;
  var oOldEvent = oTarget[sType];
  if (typeof oOldEvent != "function") {
    oTarget[sType] = fpDest;
  } else {
    oTarget[sType] = function(e) {
      oOldEvent(e);
      fpDest(e);
    }
  }
}

function hide_nonjs_components()
{
  var targ = document.getElementById('update_search');
  if(targ)
  {
   targ.style.display = 'none';
  }
  
  var targs = getElementsByClass('nonjsalert');
  for (i=0; i < targs.length; i++)
  {
   targs[i].style.display = 'none';
  }
  
  var targs = getElementsByClass('jsalert');
  for(i=0; i < targs.length; i++)
  {
   targs[i].style.display = 'inline';
  }

}


function set_alert_event(t) { addEvent( t, 'click', show_alert_list ) }

function set_up_links()
{
  if ( Safari ) { return }
   
  apply_to_child_class( document.getElementById('alertBox'), 'freqLink', set_alert_event );
  hide_nonjs_components();
  
}


function validateDetails(f)
{
   var required_fields_names  = new Array ('name', 'email', 'password1', 'password2');
    var required_fields_labels = new Array ('name', 'email address', 'password', 'password again');
    for (var t=0; t < required_fields_names.length; ++t)
    {
        if (! f[required_fields_names[t]].value.match(/[A-Za-z0-9]/))
	{
	     alert('Please complete the ' + required_fields_labels[t] + ' field.');
	     return false;
        }
    }

	if(f.password1.value !=  f.password2.value) 
	{
	  alert('Your passwords do not match');
	  return false;
	}
	

 if(f.privacy.checked == false)
  {
  	alert ("Please accept the Privacy policy");
  	return false;
  }
	
	return true;
}



addEvent( window, 'load', set_up_links );
