/*
 * Sets the browser's window to the given location.
 */
function go(href) {
	//alert("go(" + href + ")");
	window.location.href = href;
}

/*
 * Opens a popup window for the given HREF.
 */
function openPopup(href, width, height, name) {
	// Check the parameters
	if(width == null) {
		// Set default width
		width = 840;
	}
	if(height == null) {
		// Set default height
		height = 650;
	}
	if(name == null || name == "") {
		name = "_blank";
	}
	
	//alert("opening popup: " + href + ", " + width + ", " + height);
	//var left = getWindowWidth() - width - 10;
	var left = (getWindowWidth() - width)/2 - 10;
	
	var attr = "status,scrollbars,resizable";
	attr += ",left=" + left;
	attr += ",screenX=" + left;
	attr += ",width=" + width;
	attr += ",height=" + height;
	window.open(href, name, attr);
	return;
}

/*
 * Opens a full-screen popup window for the given HREF.
 */
function openPopupFull(href, name) {
	// Check the parameters
	if(name == null || name == "") {
		name = "_blank";
	}
	
	var attr = "status,scrollbars,fullscreen,type=fullWindow";
	window.open(href, name, attr);
	return;
}

/*
 * Gets the current window's width.
 */
function getWindowWidth() {
	if(navigator.appName == "Netscape") {
		return window.outerWidth;
	}
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return document.body.offsetWidth;
	}
	return 0;
}

/*
 * Gets the current window's height.
 */
function getWindowHeight() {
	if(navigator.appName == "Netscape") {
		return window.outerHeight;
	}
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return document.body.offsetHeight;
	}
	return 0;
}



/*
 * Gets the value of the given radio button group.
 */
function getRadioValue(radioGroup) {
	// Check the parameters
	if(radioGroup == null) {
		return null;
	}

	var value = null;
	if(radioGroup.length == null) {
		// Only one radio button, not an array
		var radio = radioGroup;
		if(radio.checked) {
			value = radio.value;
		}
	} else {
		// Traverse the array of radio buttons
		for(var i = 0; i < radioGroup.length; i++) {
			if(radioGroup[i].checked) {
				value = radioGroup[i].value;
			}
		}
	}
	return value;
}

/*
 * Gets the given select box's first selected value.
 */
function getSelectValue(select) {
	// Check the parameters
	if(select == null) {
		return null;
	}
	
	var value = null;
	if(select.length == null) {
		// Only a single element
		value = select.value;
	} else {
		// Traverse the array of options
		var options = select.options;
		for(var i = 0; i < options.length; i++) {
			if(options[i].selected) {
				value = options[i].value;
				break;
			}
		}
	}
	return value;
}

/*
 * Gets the given select box's first selected value or text.
 */
function getSelectValueText(select) {
	// Check the parameters
	if(select == null) {
		return null;
	}
	
	var value = null;
	if(select.length == null) {
		// Only a single element
		value = select.value;
	} else {
		// Traverse the array of options
		var options = select.options;
		for(var i = 0; i < options.length; i++) {
			if(options[i].selected) {
				value = options[i].value;
				if(value == null || value == "") {
					value = options[i].text;
				}
				break;
			}
		}
	}
	return value;
}

/*
 * Sets the given select box's value to that given.
 */
function setSelectValue(select, value) {
	// Check the parameters
	if(select == null) {
		return;
	}
	
	if(select.length == null) {
		// Only a single element
		select.value = value;
	} else {
		// Traverse the array of options
		var options = select.options;
		for(var i = 0; i < options.length; i++) {
			if(options[i].value == value) {
				options[i].selected = true;
			}
		}
	}
}

/*
 * Determines whether the given value is empty.
 */
function isEmpty(value) {
  //if(value == null) {
  if(value == null || value == "") {
    return true;
  }
  return false;
}

/*
 * Submits a reply/feedback form.
 */
function submitReply(form, reqFullContact) {
	// Check the values
	if(isEmpty(form.email.value)) {
		alert('E-mail is required');
		return false;
	}
	if(reqFullContact) {
		if(isEmpty(form.name.value)) {
			alert('Name is required');
			return false;
		}
		if(isEmpty(form.company.value)) {
			alert('Company is required');
			return false;
		}
		if(isEmpty(form.phone.value)) {
			alert('Phone is required');
			return false;
		}
	}
	
	// Add subject detail
	if(form.subject != null) {
		if(form.company != null && !isEmpty(form.company.value)) {
			form.subject.value += " from " + form.company.value;
		} else if(form.name != null && !isEmpty(form.name.value)) {
			form.subject.value += " from " + form.name.value;
		}
	}
	
	// Copy the values to the message body
	var message = 'page: ' + form.page_name.value + '\n'
			+ 'department: ' + getSelectValue(form.department) + '\n'
			+ '----------\n'
			+ 'name: ' + form.name.value + '\n'
			+ 'position: ' + form.position.value + '\n'
			+ 'company: ' + form.company.value + '\n'
			+ 'e-mail: ' + form.email.value + '\n'
			+ 'phone: ' + form.phone.value + '\n'
			+ 'fax: ' + form.fax.value + '\n'
			+ 'address: ' + form.address.value + '\n'
			+ 'city: ' + form.city.value + '\n'
			+ 'state: ' + form.state.value + '\n'
			+ 'country: ' + form.country.value + '\n'
			+ 'postal code: ' + form.postal_code.value + '\n'
			+ 'channel: ' + getSelectValue(form.channel) + '\n'
			+ '----------\n'
			+ form.comments.value;
	form.message.value = message;
	
	//alert('message...\n' + message);
	
	return true;
}
