/** Storage for all popup handles */
var popupList = {};

/**
 * Opens a named window with the given URL, width, and height.
 * @param winName The name of the window to use
 * @param url The relative or absolute URL to open
 * @param w The requested window width
 * @param h The requested window height
 */
function openWindow( winName, url, w, h, scroll )
{
	if( scroll == null )
	{
		scroll == '0';
	}
	
        closeWindow( winName );
        popupList[winName] = window.open( url, winName, "height=" + h + ",width=" + w + ",channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,scrollbars=" + scroll + ",status=1,toolbar=0","pop" );
        if ( popupList[winName].opener == null )
        {
                popupList[winName].opener = window;
        }
        if ( navigator.appName == 'Netscape' )
        {
                popupList[winName].focus;
        }
}

/**
 * Closes a named window.
 * @param winName The name of the window to close.
 */
function closeWindow( winName )
{
        if ( popupList[winName] != null && typeof( popupList[winName] ) == "object" && !popupList[winName].closed )
        {
                popupList[winName].close();
                popupList[winName] = null;
        }
}

var form_submit = false;
function multi_submit() {
	if( form_submit == false ) {
		form_submit = true;
		return true;
	}
	return false;
}

function addOnload(func) {
	if (!func) {
		return;
	}
	old_onload = window.onload;
	if (old_onload) {
		window.onload = function(ev) {
			old_onload(ev);
			func(ev);
		}
	} else {
		window.onload = func;
	}
}

function FocusError(formName, formNode){
  errors = false;
  var retVal = Validate( formName );
  if ( !retVal ) {
    obj_tmp = document.getElementById( formNode );
    rowsLength = obj_tmp.tBodies[0].rows.length;
    for( var i = 0; i < rowsLength; i++ ){
      if( obj_tmp.tBodies[0].rows[i].className == 'row' ){
        rowObj = obj_tmp.tBodies[0].rows[i];
        cellLength = rowObj.cells.length;
        for( var j = 0; j < cellLength; j++ ){
          nodeLength = rowObj.cells[j].childNodes.length;
          for( var k = 0; k < nodeLength; k++ ){
            if ( rowObj.cells[j].childNodes[k].className == 'client_error' && rowObj.cells[j].childNodes[k].style.display == 'block' ){
              if( !errors ){ 
                document.location.href='#' +rowObj.cells[j].childNodes[k].id;
                errors = true;
              }
            }
          }
          k = 0;  
        }
        j = 0;
      }
    }
  }
  else{
    submitForm( formName );
  }
  return retVal;
}

function submitForm( formName ){
  frm = document.getElementById( formName );
  frm.submit();
}

function validateAndSubmit( formName ){
  var retVal = Validate( formName );
  if( retVal ){
    submitForm( formName );
  }
  return retVal;
}

function checkFullSearch( form ) {
	var cont = false;
    frm = form.elements;
	for( i = 0; i < frm.length; i++ ) {
      if( !checkEmpty( frm[i] ) ){
        cont = true;
        i = frm.length + 1;
      }
	}
	if( cont == true ) {
		form.submit();
	} else {
		alert( "You must provide at least one field of data to perform a donor search." );
		return false;
	}
}

function checkEmpty( control ){
  switch( control.type ){
    case 'radio':
      return ( control.checked ? false : true );
    case 'text':
      return ( control.value != '' ? false : true );
    case 'select-one':
      return ( control.value != '' ? false : true );
    default:
      return true;
  }
}

function checkValue( control ) {
  if( !checkEmpty( control ) ){
    control.style.backgroundColor = '#FCF8ED';
  }
  else{
    control.style.backgroundColor = '';
  }
}

function showTip( id ){
  document.getElementById('instructions').innerHTML = document.getElementById( id ).innerHTML;
}

function restrict_events()
{
	zip_code = $("#zip").attr("value");
	
	hear_about_idx = $("#hear_about_idx").attr("value");
	$.post("/register/ajax.php", { action: "hear_about_us",  zip_code: zip_code },
		function(data){
			if(data != "none") {				
				$("#hear_idx_div").html(data);
				$("#hear_about_sub_event_idx_div").html('');
			}
		});
}

function get_hear_about_sub_events()
{
	zip_code = $("#zip").attr("value");
	hear_about_idx = $("#hear_about_idx").attr("value");
	if(hear_about_idx != "") {
		$.post("/register/ajax.php", { action: "hear_about_sub_events", hear_about_idx: hear_about_idx, zip_code: zip_code },
			function(data){
				$("#hear_about_sub_event_idx_div").html(data);
			});
	}
}

function get_hear_about_sub_details()
{
	hear_about_sub_event_idx = $("#hear_about_sub_event_idx").attr("value");
	if(hear_about_sub_event_idx != "") {
		$.post("/register/ajax.php", { action: "hear_about_sub_details", hear_about_sub_event_idx: hear_about_sub_event_idx },
			function(data){
				$("#hear_about_sub_details").html(data);
			});
	}
}

