// general functions, some of which were
// written by Adrian Duyzer

function wopen(url, name, w, h)
{
	w += 32;
	h += 96;
	var win = window.open(url,
		name,
		'width=' + w + ', height=' + h + ', ' +
		'location=yes, menubar=yes, ' +
		'status=yes, toolbar=yes, scrollbars=yes, resizable=no');	
	win.resizeTo(w, h);
	win.focus();
}

function writit(text,id)
{
	if (document.getElementById)
	{
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	}
	else if (document.all)
	{
		x = document.all[id];
		x.innerHTML = text;
	}
}

function cent(amount) {
// returns the amount in the .99 format 
    amount -= 0;
    amount = (Math.round(amount*100))/100;
    return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}

// returns an object consisting of form input values
// in a hash where the key is the form input id
function getFormElementData(form)
{
	var formobject = new Object();
	var formdata = Form.getElements(form);
	formdata.each(function(ele) {
		// if the type is checkbox, use checked value, not actual value
		if (ele.type == 'checkbox') {
			formobject[ele.id] = ele.checked;
		} else {
			formobject[ele.id] = ele.value;
		}
	});
	return formobject;
}

function getTarget(e)
{
	// http://www.quirksmode.org/js/events_properties.html
	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;
}

// returns the id from an element whose id is like item_23, where 23 is the id
function getID(e)
{
	var targ = getTarget(e);
	var record = new Array();
	var record_string = targ.id;
	record = record_string.split('_');
	return record[1];
}

// modifies paging buttons
function check_paging(i) {
	// check to see if we should disable next button
	if (i < 20) {
		$("next_page").disabled = true;
	} else {
		$("next_page").disabled = false;
	}
	// check to see if we should disable previous button
	if ($("limit_start"). value <= 0) {
		$("previous_page").disabled = true;
	} else {
		$("previous_page").disabled = false;
	}
}

// shortens a string, returns it with some dots...
function shorten(string, length) {
	if (string.length > length) {
		return string.substring(0, length) + '...';
	} else {
		return string;
	}
}

// handles quick add clicks
function quick_add()
{
	if ($('quick_add').value) window.location = $('quick_add').value;
}

// some validations for postal codes
function change_postal_validation()
{
	if ($('country').value == 'US') {
		$('postal').className = 'required validate-us-postal';
	} else {
		if ($('country').value == 'CAN') {
			$('postal').className = 'required validate-can-postal';
		} else {
			$('postal').className = 'required';
		}
	}
}
function validate_can_post()
{
	Validation.add('validate-can-postal', 'Please enter a valid Canadian postal code in the form A#A #A#.', function (v) {
		  return Validation.get('IsEmpty').test(v) ||  /^[A-Z]\d[A-Z] \d[A-Z]\d+$/.test(v)
	});	
}
function validate_us_post()
{
	Validation.add('validate-us-postal', 'Please enter a valid American ZIP code in the form 12345, or 12345-1234.', function (v) {
		  return Validation.get('IsEmpty').test(v) ||  /(^\d{5}$)|(^\d{5}-\d{4}$)/.test(v)
	});
}
function ucase_postal()
{
	$('postal').value = $('postal').value.toUpperCase();
}

// BEGIN NOTE FUNCTIONS >>>
// ******************************************************************************
function do_list_notes()
{
	x_displayNotes($('note_id_field').value, $('note_id_field_value').value, 'view_notes', 'add_note_listeners', show_return);
}

function do_add_note() {
	valid = new Validation('frm_add_note', {onSubmit : false});
	if (valid.validate()) {
		// send form data to server for processing
		writit('Please wait...', 'note_add_result');
		var note = getFormElementData('frm_add_note');
		// i don't use the custom callback function capability here because i need my
		// callback function (show_note_add) to be aware if the addition was successful
		x_addNote($('note_id_field').value, $('note_id_field_value').value, escape(JSON.stringify(note)), 'note_add_result', show_note_add);
	}
}

function show_note_add(ret)
{
	show_return(ret);
	if (ret[3]) {
		valid = new Validation('frm_add_note', {onSubmit : false});
		valid.reset();
		$('frm_add_note').reset();
		do_list_notes();
	}
}

function add_note_listeners()
{
	var delete_buttons;
	// use prototype to convert the node list into an array
	delete_buttons = $A(document.getElementsByName('deletenote'));
	delete_buttons.each ( function (item, index) {
		Event.observe(item.id, 'click', delete_note);
	});
}

function delete_note(e)
{
	if (confirm('Are you sure you wish to delete this note?')) {
		var item_id = getID(e);
		x_deleteNote(item_id, do_list_notes);
	}
}
// <<< END NOTE FUNCTIONS
// ******************************************************************************

// function for displaying results and evaluating callback functions
function show_return(ret)
{
	writit(ret[1], ret[0]);
	// handle callback function
	if (ret[2]) {
		eval(ret[2] + '()');
	}
}

// get url variables
var qs = location.search.substring(1);
var nv = qs.split('&');
var url = new Object();
for(i = 0; i < nv.length; i++)	{
	eq = nv[i].indexOf('=');
	url[nv[i].substring(0,eq).toLowerCase()] = unescape(nv[i].substring(eq + 1));
}

// add search terms to a where statement
function append_where(where_add, append)
{
	if (where_add) {
		where_add += ' AND ' + append;
	} else {
		where_add = append;
	}
	return where_add;
}