/*
 * What it is:
 * Javascript functions for the website
 *
 * Author:
 * Jason Stechschulte <jasons@wcoil.com>
 *
 * Description:
 * This page is mainly for javascript functions that are to be used throughout
 * the website.  There could be some page specific stuff here as well.  Mainly
 * this exists because I like things as modular as possible, so separating this
 * from the main page makes me happy.
 *
 */


/*
 * This function confirms that the user actually wants to go to the url passed
 * to this function.
 */

function confirmdelete(message, url) {
	if(confirm(message)) location.href = url;
}


function goToPage(url) {
	location.href = url;
}

/*
 * The next three function work together to swap classes and status messages
 * for links.  With all three, links can look like buttons if you use the
 * correct styles to go with them.
 */

function over(field, link, myclass) {
	field.className = myclass + "2";
	if(link != '') {
		if(document.getElementById) {
			document.getElementById(link).className = myclass + "link2";
			window.status = document.getElementById(link).href;
		}
		else {
			document.all(link).className = myclass + "link2";
			window.status = document.all(link).href;
		}
	}
	return true;
}

function out(field, link, myclass) {
	field.className = myclass;
	if(link != '') {
		if(document.getElementById) {
			document.getElementById(link).className = myclass + "link";
		}
		else {
			document.all(link).className = myclass + "link";
		}
	}
	window.status = '';
	return true;
}

function down(field, link, myclass) {
	field.className = myclass + "3";
	if(link != '') {
		if(document.getElementById) {
			document.getElementById(link).className = myclass + "link3";
		}
		else {
			document.all(link).className = myclass + "link3";
		}
	}
	return true;
}

function opennew(link, name) {
	newwin = open(link, name);
}


/*
 * This function checks to see if a message has been typed, if not, it is
 * assumed that the user accidentally hit the enter key, so rather than
 * submitting the form, we simply tab them to the first empty field that
 * follows a filled in field.  This in effect makes the enter key work somewhat
 * like the tab key for them.
 */

function contactFormSubmit() {
	if(document.contact.message.value == '') {
		var keepGoing = true;
		document.contact.message.focus();
		if(document.contact.phone.value == '') {
			document.contact.phone.focus();
		}
		else {
			keepGoing = false;
		}
		if(keepGoing && document.contact.email.value == '') {
			document.contact.email.focus();
		}
		else {
			keepGoing = false;
		}
		if(keepGoing && document.contact.name.value == '') {
			document.contact.name.focus();
		}
		return false;
	}
	return true;
}


/*
 * This function is used for making a thumbnail larger.  You send it the image,
 * image width, and image height.  It then opens a new window just large enough
 * for the full size image to be displayed in, and displays the image.
 */

function viewImage(image, w, h) {
	var winl;
	var wint;
	w = w * 1 + 25;
	h = h * 1 + 25;
	if(screen.width){
		winl = (screen.width - w) / 2;
		wint = (screen.height - h) / 2;
		if(winl < 0) winl = 0;
		if(wint < 0) wint = 0;
	}
	else{
		winl = 0;
		wint = 0;
	}
	var settings = 'height=' + h + ',';
	settings += 'width=' + w + ',';
	settings += 'top=' + wint + ',';
	settings += 'left=' + winl + ',';
	settings += 'toolbar=no,scrollbars=yes';
	win = window.open(image, "Picture", settings);
	win.window.focus();
}

var item2move = -1;
var moving2 = -1;

function reorder(redirect, item, category) {
	if(item2move == -1) {
		item2move = item;
		document.getElementById("item"+item).style.backgroundColor = '#aaffaa';
	}
	else if(item2move == item) {
		item2move = -1;
	}
	else {
		moving2 = item;
		var url = redirect + "?mode=reorder&item2Move=" + item2move + "&moving2=" + moving2 + "&category=" + category;
		location.href= url;
	}
}

function itemOver(field) {
	document.getElementById("item" + field).style.backgroundColor = '#ffaaaa';
}

function itemOut(field, bgcolor) {
	if(field == item2move) {
		document.getElementById("item" + field).style.backgroundColor = '#aaffaa';
	}
	else {
		document.getElementById("item" + field).style.backgroundColor = bgcolor;
	}
}

function copyAddress() {
	document.checkout.shipping_name.value = document.checkout.billing_first_name.value + " " 
				+ document.checkout.billing_last_name.value;
	document.checkout.shipping_address1.value = document.checkout.billing_address1.value;
	document.checkout.shipping_address2.value = document.checkout.billing_address2.value;
	document.checkout.shipping_city.value = document.checkout.billing_city.value;
	document.checkout.shipping_state.value = document.checkout.billing_state.value;
	document.checkout.shipping_zip_code.value = document.checkout.billing_zip_code.value;
}


function copyName() {
	document.checkout.credit_name_on_card.value = document.checkout.billing_first_name.value + " "
				+ document.checkout.billing_last_name.value;
}
