// JavaScript Document
var newwindow = '';
function popitup(url,windowname, w, h, scrollbars) {
	//if (!newwindow.closed && newwindow.location)
	//{
	//	newwindow.location.href = url;
	//}
	//else
	//{
		newwindow=window.open(url,windowname,'height=' + h + ',width=' + w + ',scrollbars=' + scrollbars +',resizable');
		if (!newwindow.opener) newwindow.opener = self;
	//}
	if (window.focus) {newwindow.focus()}
	return false;
}
function closeme() {
	self.close();
}
function formsubmit(form_id) {
	var submit_form = document.getElementById(form_id);
	submit_form.submit();
}

function actionsubmit(form_id,action) {
	var submit_form = document.getElementById(form_id);
	if (item_validator(submit_form )) {
		var action_input = submit_form.elements["action"];
		//alert(action_input);
		action_input.value = action;
		submit_form.submit();
	}
}

function stall_validator(form) {
	var pw = form.elements["pw"];
	var pw_check = form.elements["pw_confirm"];
	if (pw.value != pw_check.value) {
		alert("The passwords are not the same. Try again.");
		pw.focus();
		return false;
	}
	// form is ok...
	return true;
}
function email_validator(form) {
	var email = form.elements["e_mail_from"];
	var str = email.value;
	if ((str.indexOf(".") > 2) && (str.indexOf("@") > 0)) {
		// email is ok...
		return true;
	}
	// email is bad...
	alert("You should make at least a vague attempt\n at a valid e-mail to use the email cart.\nPlease enter a valid e-mail now");
	email.focus();
	return false;
}
function email_signup_validator(form) {
	var email = form.elements["signup_email"];
	var name = form.elements["signup_name"];
	var name_str = name.value;
	var email_str = email.value;
	if (name_str=="Your Name") {
		// name hasn't been entered
		alert("You must type your actual name into the 'Your Name' box");
		name.focus();
		return false;
	} else {
		// name is ok >> check e-mail...
		if ((email_str.indexOf(".") > 2) && (email_str.indexOf("@") > 0)) {
			// email is ok...
			alert("Thankyou " + name_str + ", your signup request has been sent");
			return true;
		}
	}
	// email is bad...
	alert("You should make at least a vague attempt\n at a valid e-mail to signup .\nPlease enter a valid e-mail now");
	email.focus();
	return false;
}

function validate_selector(form,sel_field) {
	//alert(sel_field);
	var selected_index = form.elements[sel_field].selectedIndex;
	if (selected_index==0) {
		// user hasn't selected anything > don't submit form
		return false;
	} else {
		return true;
	}
}

function item_validator(form) {
	var category = form.elements["manage_item_category"];
	if (category.value == "") {
		alert("You must select a category. Try again.");
		category.focus();
		return false;
	}
	var quantity = form.elements["manage_item_quantity"].value;
	if ((quantity == null) || (quantity =="") || (isNaN(quantity))) {
		alert("You must enter a quantity and it must be a number");
		form.elements["manage_item_quantity"].focus();
		return false;
	}
	var price = form.elements["manage_item_price"].value;
	if ((price == null) || (price =="") || (isNaN(price))) {
		alert("You must enter a price as a number. Decimal point is optional");
		form.elements["manage_item_price"].focus();
		return false;
	}
	// form is ok...
	//alert("form is" + form);
	return true;
}
// functions for the photo browser
function show_hide(div_to_show,div_to_hide) {
	// function to help the photobrowser flip between browse and upload...
	var show = document.getElementById(div_to_show);
	var hide = document.getElementById(div_to_hide);
	show.style.display = 'block';
	hide.style.display = 'none';
}
function limit_text(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}