var numcats = 1;
var lastcat = 1;
var categories = new Array('Action/Adventure','African American','Animals/Pets','Anthology','Autobiography','Biography','Business','Career','Children’s/Juvenile Fiction','Children’s/Juvenile Non-Fiction','Children’s Picture Book','Coffee Table Book/Photography','Cookbook and Home/Garden','Current Events/Social Change','Diet/Nutrition/Food','E-book','Education/Academic','Fiction by Young Authors (18 or younger)','First Novel (under 80,000 words)','First Novel (over 80,000 words)','GLBT (Gay/Lesbian/Bisexual/Transgender)','General Fiction/Novel (Under 80,000 Words)','General Fiction/Novel (Over 80,000 Words)','General Non-Fiction','Health/Wellness','Historical Fiction','Historical Non-Fiction','How To','Humor/Comedy','Inspirational','Memoirs (Historical/Legacy/Career)','Memoirs (Overcoming Adversity/Tragedy/Challenges)','Memoirs (Other)','Military','Motivational','Multicultural Fiction','Multicultural Non-Fiction','Mystery ','New Age','Novella','Parenting/Family','Poetry','Regional Fiction','Regional Non-Fiction','Relationships','Religious Non-Fiction','Religious Fiction','Romance','Science/Nature/Environment','Science Fiction/Fantasy','Self Help','Short Story - Fiction','Spirituality','Suspense/Thriller','Travel/Travel Guide','Women’s Issues','Young Adult Fiction','Best Cover Design - Fiction','Best Cover Design - Non-Fiction','Best Overall Design');

function addOption(selectbox,text,value ) {
	var optn = document.createElement("option");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}
function addcategory(useold) {
	var cats = document.getElementById('categories');
	var x;
	var setname;

	var dropdown;
	var thediv;
	var thespan;

	numcats++;
	lastcat++;

	try {
		dropdown = document.createElement("<select name='category_"+lastcat+"' id='category_"+lastcat+"'>");
		thediv = document.createElement("<div id='cat"+lastcat+"'>");
		thespan = document.createElement("<span id='span"+lastcat+"'>");
	} catch (e) {
		dropdown = document.createElement("select");
		dropdown.setAttribute('name', 'category_' + lastcat);
		dropdown.setAttribute('id', 'category_' + lastcat);
		thediv = document.createElement("div");
		thediv.setAttribute('id', 'cat' + lastcat);
		thespan = document.createElement("span");
		thespan.setAttribute('id', 'span' + lastcat);
	}
	thespan.innerHTML = numcats;
	thediv.appendChild(thespan);
	thediv.innerHTML += '. ';
	addOption(dropdown,'Choose a Category','None');

	if (useold) {
		for (x in oldcategories) {
			addOption(dropdown,oldcategories[x],oldcategories[x]);
		}
	} else {
		for (x in categories) {
			addOption(dropdown,categories[x],categories[x]);
		}
	}

	thediv.appendChild(dropdown);
	thediv.innerHTML += " <a href='javascript:dropcategory("+lastcat+")'>Remove This Category</a>";

	cats.appendChild(thediv);
	updatevalues();
}
function dropcategory(cat) {
	var i;
	var test;
	document.getElementById('categories').removeChild(document.getElementById('cat'+cat));
	for (i = cat + 1; i <= lastcat; i++) {
		test = document.getElementById('span'+i).innerHTML - 1;
		try {
			document.getElementById('span'+i).innerHTML = test;
		} catch (err) {
		}
	}
	numcats--;
	updatevalues();
}
function updatevalues() {
	var price = 25 + (50 * numcats);
	today = new Date();
	earlybird = new Date();
	earlybird.setMonth(1);
	earlybird.setDate(30);
	earlybird.setYear(2009);

	if (today.getTime() < earlybird.getTime() && numcats > 1) price -= 50;
	document.theform.numcats.value = numcats;
	document.theform.price.value = '$'+price+'.00';
}
function checkinfo() {
	var theform = document.theform;
	var emailpattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var phonepattern = /[0-9]{3}.*[0-9]{3}.*[0-9]{4}/;
	var isbnpattern = /^[0-9]{3}[\-]?[0-9][\-]?[0-9]{6}[\-]?[0-9][0-9][\-]?[0-9]$/;
	var i;
	var j;
	var cat1;
	var cat2;
	if (theform.title.value == '' || theform.authors.value == '' || theform.isbn.value == '' || theform.contactname.value == '' || theform.publisher.value == '' || theform.address.value == '' || theform.city.value == '' || theform.province.value == '' || theform.postal.value == '' || theform.phone.value == '' || theform.email.value == '' || theform.heardofus.value == '') {
		alert ("All fields are required in order to submit your entry, including how you heard of us.");
		return false;
	}
	if (!theform.phone.value.match(phonepattern)) {
		alert ("Please enter your phone number including area code.");
		return false;
	}
	if (!theform.email.value.match(emailpattern)) {
		alert ("You must enter a valid email address to continue.");
		return false;
	}
	if (!theform.isbn.value.match(isbnpattern)) {
		alert ("Your ISBN must be 13 digits long");
		return false;
	}
	if (theform.email.value != theform.email2.value) {
		alert ("You must enter the same email address twice to continue.");
		return false;
	}
	for (i = 1; i <= lastcat; i++) {
		try {
			cat1 = document.getElementById('category_'+i);
			if (cat1.value) {
				if (cat1.value == 'None') {
					alert ('You have left a category selection on "Choose a Category"');
					return false;
				}
				for (j = i+1; j <= lastcat; j++) {
					try {
						cat2 = document.getElementById("category_"+j);
						if (cat1.value == cat2.value) {
							alert ("You have selected the same category twice.");
							return false;
						}
					} catch(err){}
				}
			}
		} catch(err){}
	}
	return true;
}
