var mutils = new MiscUtils();
var futils = new Form('registerForm');
//EventUtil.addEventHandler(window, 'load', onWindowLoad);

history.navigationMode = 'compatible';
$(document).ready(onWindowLoad);

/////////////////////// FUNCTIONS: BEGIN ////////////////////////////////
//--------------------------------------------------------------
function onWindowLoad() {

  $("#submit_form").bind("click", submitForm);
	
	var combobox = $(".combobox").get();
	for (var i=0; i < combobox.length; i++) {
    $(combobox[i]).textdropdown('large');
  }
	
	var movebuttons = $(".movebutton").get();
	for (var i=0; i < movebuttons.length; i++) {
		$(movebuttons[i]).bind("click", moveTopic);
	}
}

//--------------------------------------------------------------
function moveTopic(event) {
  var buttonId = $(event.target).attr('id');
	var parts = buttonId.split('_');
	var direction = parts[0];
	var topicId = parts[1];
//	var n = topicId.lastIndexOf("0");
//	var topicNumber = topicId.substr(n);
//	var topicName = topicId.substring(0, n);
  var n = topicId.length;
  var topicNumber = topicId.substr(n-2);
	var topicName = topicId.substr(0, n-2);

  if ( direction == 'up' ) {
		// swap selected topic with topic above
		var upTopic = $("#"+topicName+zeroPadded(parseInt(topicNumber, 10)-1))
		var downTopic = $("#"+topicName+topicNumber)
		var upTopicVal = $(upTopic).val();
		var downTopicVal = $(downTopic).val();
//		if (downTopicVal != '') {
  		$(upTopic).val(downTopicVal);
	  	$(downTopic).val(upTopicVal);
//		} //endif
	} else if ( direction == 'down' ) {
		// swap selected topic with topic below
		var upTopic = $("#"+topicName+zeroPadded(parseInt(topicNumber,10)+1))
		var downTopic = $("#"+topicName+topicNumber)
		var upTopicVal = $(upTopic).val();
		var downTopicVal = $(downTopic).val();
//		if (upTopicVal != '') {		
  		$(upTopic).val(downTopicVal);
  		$(downTopic).val(upTopicVal);
//		} //endif

	} else {
		alert('ERROR with up/down buttons - contact webmaster');
	}
}

//--------------------------------------------------------------
function submitForm() {
  if ( validate() ) {
    document.registerForm.submit();
  }
}

//--------------------------------------------------------------
function validate() {  // simple validation
  // make sure the required fields are set
  if (document.getElementById('fname').value == '' || document.getElementById('lname').value == '') {
    alert('You must enter in your Name');
    return false;
  }
  if (document.getElementById('email').value == '') {
    alert('You must enter in your Email Address');
    return false;
  }
  if (document.getElementById('organization').value == '') {
    alert('You must enter in your Organization');
    return false;
  }
  if (document.getElementById('street_address').value == '') {
    alert('You must enter in your Street Address');
    return false;
  }
  if (document.getElementById('city').value == '') {
    alert('You must enter in your City');
    return false;
  }
  if (document.getElementById('state').value == '') {
    alert('You must enter in your State/Province');
    return false;
  }
  if (document.getElementById('country').value == '') {
    alert('You must enter in your Country');
    return false;
  }
	
/*  These fields are OPTIONAL	
	for (var i=1; i <= 9; i++) {
    if (document.getElementById('breakout' + zeroPadded(i)).value == '') {
      alert('You must enter a value for Breakout Topic: ' + i);
      return false;
    } //endif
	} //endfor
	
	for (var i=1; i <= 9; i++) {
    if (document.getElementById('poster' + zeroPadded(i)).value == '') {
      alert('You must enter a value for Poster Topic: ' + i);
      return false;
    } //endif
	} //endfor
*/	

  var futils = new Form('registerForm');
	
  if (futils.getCheckedRadioButtonValue('anniv') == '') {
    alert('You must enter in if you are attending 50th Anniversary Symposium');
    return false;
  }
	
  if (futils.getCheckedRadioButtonValue('tour') == '') {
    alert('You must answer if you are attending SSEC Tour');
    return false;
  }	
	
  if (futils.getCheckedRadioButtonValue('mcidasv') == '') {
    alert('You must answer if you are attending McIDAS-V information session');
    return false;
  }	
	
  return true;
}

//--------------------------------------------------------------
function zeroPadded(n) {
	if (n <= 9) {
    return '0' + String(n);
	} else {
		return n;
	}
}

//--------------------------------------------------------------
function openWindow(url){
  window.open(url, 'newWindow','width=800,height=450,location=no,menubar=no,resizable=yes,scrollbars=yes,toolbar=no');
}

