
//
// switches images for rollover
//
function setImage(name, state) {
	if (document.images) {
		if (state == 'enabled') {
			document.images[name].src = normalImages[name].src ;
			return true ;
		} else if (state == 'disabled') {
			document.images[name].src = disabledImages[name].src ;
			return true ;
		}
	}
	return false ;
}

//
// general form initialization
//
function initForm() {
  if (document.forms.length > 0) {
    var myForm = document.forms[0] ;
    if (myForm.id == 'ImagesForm')   { resetCheckboxes() ; }
    if (myForm.id == 'PostForm')     { toggleHiddenCheckbox(true) ; }
    if (myForm.id == 'MediaSetForm') { toggle('status_div', 'off') ; }
		Form.focusFirstElement(myForm) ;
  }
}

//
// clear all checkboxes
//
function resetCheckboxes() {
  // find all the checkboxes...
  var inputs = document.getElementsByTagName("input") ;
  for(index = 0; index < inputs.length; index++) {
    // uncheck if it's a checkbox
    if(inputs[index].type == 'checkbox') {
      inputs[index].checked = 0 ;
		}
  }
}

//
// toggle a hidden checkbox on and off
// to trigger observe_form
//
function toggleHiddenCheckbox(firstTime) {
	id = 'HiddenCheckbox' ;
	elem = document.getElementById(id) ;
	if (elem) {
		elem.checked = !elem.checked ;
		if (firstTime) {
			self.setTimeout('toggleHiddenCheckbox();', 1000) ;
		}
	}	
}

//
// shows the first element and hides the second
//
function showHide(elemShow, elemHide) {
	toggle(elemShow, 'on') ;
	toggle(elemHide, 'off') ;
}

//
// validates upload form data before submission
//
// we are only concerned about two things 
// currently ... that a title  is set and
// that the upload is in the proper format
//
function validateForm(form) {
  var myForm = document.forms[0];
	switch (myForm.id) {
		case 'PostForm':
			// check title
			if ((validateElement('event_title', 'not_empty')) || 
			    (validateElement('news_title', 'not_empty')) || 
			    (validateElement('pastors_page_title', 'not_empty'))) {
				return 'Title must not be empty!' ;
			}
			if (validateElement('prayer_request_title', 'not_empty')) {
				return 'Name must not be empty!' ;
			}
			// check post body
			if ((validateElement('event_body', 'not_empty')) || 
			    (validateElement('news_body', 'not_empty')) || 
			    (validateElement('pastors_page_body', 'not_empty'))) {
				return 'Post Body must not be empty!' ;
			}
			if (validateElement('prayer_request_body', 'not_empty')) {
				return 'Request must not be empty!' ;
			}
		  break ;
		case 'UploadForm':
			// check upload
			if (validateElement('data', 'not_empty')) {
				return 'You must choose a file to upload!' ;
			} 
			break ;
		case 'MediaSetForm':
			// check title
			if ((validateElement('photo_set_title', 'not_empty')) || 
			    (validateElement('audio_set_title', 'not_empty')) || 
			    (validateElement('video_set_title', 'not_empty'))) {
				return 'Title must not be empty!' ;
			}
			// check upload
			if (validateElement('upload', 'not_empty')) {
				return 'You must choose a zip file to upload!' ;
			} 
			if (validateElement('upload', 'is_zipfile')) {
				return 'Uploaded files must be zipped!' ;
			} 
			break ;
		case 'UploadImageForm':
			// check upload
			if (validateElement('upload', 'not_empty')) {
				return 'You must choose an image file to upload!' ;
			} 
			if (validateElement('upload', 'is_image')) {
				return 'Uploaded images must be in GIF, JPG, or PNG format!' ;
			} 
			break ;
	}
	return 'OK' ;
}

function validateElement(id, type) {
	var elem = document.getElementById(id) ;
	if (elem) {
		switch (type) {
			case 'not_empty':
				var rx = /\w/ ;
				break ;
			case 'is_zipfile':
				var rx = /\.zip\s*$/i ;
				break ;
			case 'is_image':
				var rx = /\.(gif|jpe?g|png)\s*$/i ;
				break ;
		}
		return !rx.exec(elem.value) ;
	}
	return false ;
}

//
// wraps a submit call so we can validate
//
function submitForm(form) {
  msg = validateForm(form) ;
	if (msg == 'OK') {
		$(form).submit() ;
	} else {
		alert(msg) ;
	}
}

//
// toggles the visible state of an element
//
function toggle(id, state) {
	elem = document.getElementById(id) ;
	if (elem) {
		if (elem.style.display == 'none') {
			if (state != 'off') { Effect.Appear(id) ; }	
		} else {
			if (state != 'on') { Effect.Fade(id) ; }	
		}	
	}	
}

//
// shows an element and pulses it to grab
// the user's attention
//
function showAndPulse(id) {
	elem = document.getElementById(id) ;
	if (elem) {
		toggle(elem, 'on') ;
    Effect.Pulsate(id) ;
	}
}

//
// toggles the selected border when an image
// checkbox is clicked
//
function toggleSelect(id) {
	elem = document.getElementById(id) ;
	cbox = document.getElementById('checkbox-' + id) ;
	if ((elem) && (cbox)) {
		new Effect.Highlight(id)
		if (cbox.checked) {
			elem.style.backgroundColor = '#990000' ;
		} else {
			elem.style.backgroundColor = '#FFFFFF' ;
		}
	}
}

//
// shows the textile help in a popup window
//
function showTextileQuickReference() {
  window.open("/help/textile/textile.html",
		          "Textile Help",
		          "height=600,width=550,channelmode=0,dependent=0," +
		          "directories=0,fullscreen=0,location=0,menubar=0," +
		          "resizable=0,scrollbars=1,status=1,toolbar=0") ; 
}

